Version bump
[mirrors/libpurple-core-answerscripts.git] / answerscripts.c
1 #define _GNU_SOURCE
2 //#define __WIN32__
3 #ifndef __WIN32__
4 #define ANSWERSCRIPT_EXT ""
5 #else
6 #define ANSWERSCRIPT_EXT ".exe"
7 #endif
8 #define ANSWERSCRIPT "answerscripts" ANSWERSCRIPT_EXT
9 #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
10 #define ANSWERSCRIPTS_LINE_LENGTH 4096
11 #define ENV_PREFIX "ANSW_"
12 #define PROTOCOL_PREFIX "prpl-"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <string.h>
18
19 #ifndef __WIN32__
20 #include <fcntl.h>
21 #else
22 #include <windows.h>
23 #endif
24
25 /* Purple plugin */
26 #define PURPLE_PLUGINS
27 #include <libpurple/account.h>
28 #include <libpurple/blist.h>
29 #include <libpurple/conversation.h>
30 #include <libpurple/core.h>
31 #include <libpurple/debug.h>
32 #include <libpurple/plugin.h>
33 #include <libpurple/savedstatuses.h>
34 #include <libpurple/signals.h>
35 #include <libpurple/status.h>
36 #include <libpurple/util.h>
37 #include <libpurple/value.h>
38 #include <libpurple/version.h>
39
40 char *message = NULL;
41 char *hook_script = NULL;
42
43 typedef struct {
44 FILE *pipe;
45 PurpleConversation *conv;
46 } answerscripts_job;
47
48 const void *check_null(const void *pointer) {
49 if(pointer == NULL) return "";
50 return pointer;
51 }
52
53 int answerscripts_process_message_cb(answerscripts_job *job) {
54 int i;
55 char response[ANSWERSCRIPTS_LINE_LENGTH+1]; response[0]='\0';
56 FILE *pipe = job->pipe;
57 PurpleConversation *conv = job->conv;
58
59 if (pipe && !feof(pipe)) {
60 if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe)
61 && (errno == EWOULDBLOCK || errno == EAGAIN) //WARNING! Not compatible with windows :-(
62 ) return 1;
63
64 for(i=0;response[i];i++) if(response[i]=='\n') response[i]='\0';
65 if(response[0]!='\0') {
66 if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
67 purple_conv_chat_send(purple_conversation_get_chat_data(conv), response);
68 } else {
69 purple_conv_im_send(purple_conversation_get_im_data(conv), response);
70 }
71 }
72 if(!feof(pipe)) return 1;
73 }
74 pclose(pipe);
75 free(job);
76 return 0;
77 }
78
79 static void received_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) {
80 if (conv == NULL) conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, who); //* A workaround to avoid skipping of the first message as a result on NULL-conv: */
81
82 PurpleBuddy *buddy = purple_find_buddy(account, who);
83 PurplePresence *presence = purple_buddy_get_presence(buddy);
84
85 //Get message
86 message = purple_markup_strip_html(buffer);
87
88 //Get conversation type
89 const char *action, *roomname;
90 switch(purple_conversation_get_type(conv)) {
91 case PURPLE_CONV_TYPE_IM:
92 action = "IM";
93 roomname = "";
94 break;
95 case PURPLE_CONV_TYPE_CHAT:
96 action = "CHAT";
97 roomname = purple_conversation_get_name(conv);
98 //PurpleConvChat *chat = purple_conversation_get_chat_data(conv);
99 break;
100 default:
101 action = "UNKNOWN";
102 }
103
104 //LOCAL USER:
105 const char* local_name = (char *) purple_account_get_name_for_display(account);
106 const char* local_alias = purple_account_get_alias(account);
107 if(local_alias == NULL) local_alias = local_name;
108
109 //Do not respond to messages sent by myself
110 if(strcmp(local_name, who) == 0) return;
111
112 //Was my nick said?
113 char *highlighted;
114 if(flags & PURPLE_MESSAGE_NICK || purple_utf8_has_word(buffer, local_name))
115 highlighted = "true";
116 else
117 highlighted = "false";
118
119 //REMOTE USER (Buddy):
120
121 //Get buddy alias
122 const char* remote_alias = purple_buddy_get_alias(buddy);
123 if(remote_alias == NULL) remote_alias = who;
124 //if(remote_alias == NULL) remote_alias = "";
125
126 //Get buddy group
127 PurpleGroup *group = purple_buddy_get_group(buddy);
128 const char *from_group = group != NULL ? purple_group_get_name(group) : ""; //return empty string if not in group
129
130 //Get protocol ID
131 const char *protocol_id = purple_account_get_protocol_id(account);
132 if(!strncmp(protocol_id,PROTOCOL_PREFIX,strlen(PROTOCOL_PREFIX))) protocol_id += strlen(PROTOCOL_PREFIX); //trim out PROTOCOL_PREFIX (eg.: "prpl-irc" => "irc")
133
134 //Get status
135 PurpleStatus *status = purple_account_get_active_status(account);
136 PurpleStatusType *type = purple_status_get_type(status);
137 //remote
138 PurpleStatus *r_status = purple_presence_get_active_status(presence);
139 PurpleStatusType *r_status_type = purple_status_get_type(r_status);
140
141 //Get status id
142 const char *status_id = NULL;
143 status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type));
144 //remote
145 const char *r_status_id = NULL;
146 r_status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(r_status_type));
147
148 //Get status message
149 const char *status_msg = NULL;
150 if (purple_status_type_get_attr(type, "message") != NULL) {
151 status_msg = check_null(purple_status_get_attr_string(status, "message"));
152 } else {
153 status_msg = (char *) check_null(purple_savedstatus_get_message(purple_savedstatus_get_current()));
154 }
155 //remote
156 const char *r_status_msg = NULL;
157 if (purple_status_type_get_attr(r_status_type, "message") != NULL) {
158 r_status_msg = check_null(purple_status_get_attr_string(r_status, "message"));
159 } else {
160 r_status_msg = "";
161 }
162
163 //Export variables to environment
164 setenv(ENV_PREFIX "ACTION", action, 1); //what happend: IM/CHAT/UNKNOWN, show setting dialog, event, etc...
165 setenv(ENV_PREFIX "MSG", message, 1); //text of the message
166 setenv(ENV_PREFIX "MSG_HIGHLIGHTED", highlighted, 1); //was my nick mentioned in message? true/false
167 setenv(ENV_PREFIX "PROTOCOL", protocol_id, 1); //protocol used to deliver the message. eg.: xmpp, irc,...
168 setenv(ENV_PREFIX "R_NAME", who, 1); //ID of remote user - "buddy"
169 setenv(ENV_PREFIX "R_GROUP", from_group, 1); //group which contains that buddy OR empty string
170 setenv(ENV_PREFIX "R_ALIAS", remote_alias, 1); //buddy's OPTIONAL alias, server alias, contact alias, username OR empty string
171 setenv(ENV_PREFIX "R_STATUS", r_status_id, 1); //unique ID of remote user's status. eg.: available, away,...
172 setenv(ENV_PREFIX "R_ROOM_NAME", roomname, 1); //Chatroom name
173 setenv(ENV_PREFIX "R_STATUS_MSG", r_status_msg, 1); //status message set by your buddy
174 setenv(ENV_PREFIX "L_NAME", local_name, 1); //ID of local user
175 setenv(ENV_PREFIX "L_ALIAS", local_alias, 1); //OPTIONAL alias of local user OR empty string
176 setenv(ENV_PREFIX "L_STATUS", status_id, 1); //unique ID of local user's status. eg.: available, away,...
177 setenv(ENV_PREFIX "L_STATUS_MSG", status_msg, 1); //status message set by local user
178
179 //Launch job on background
180 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
181 job->pipe = popen(hook_script, "r");
182 if(job->pipe == NULL) {
183 fprintf(stderr,"Can't execute %s\n", hook_script);
184 return;
185 }
186 job->conv = conv;
187
188 #ifndef __WIN32__
189 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
190 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
191 #else
192 //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows
193 #endif
194
195 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
196 }
197
198 static gboolean plugin_load(PurplePlugin * plugin) {
199 asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT);
200 void *conv_handle = purple_conversations_get_handle();
201 purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_msg_cb), NULL);
202 purple_signal_connect(conv_handle, "received-chat-msg", plugin, PURPLE_CALLBACK(received_msg_cb), NULL);
203 return TRUE;
204 }
205
206 static gboolean plugin_unload(PurplePlugin * plugin) {
207 free(hook_script);
208 return TRUE;
209 }
210
211 static PurplePluginInfo info = {
212 PURPLE_PLUGIN_MAGIC,
213 PURPLE_MAJOR_VERSION,
214 PURPLE_MINOR_VERSION,
215 PURPLE_PLUGIN_STANDARD,
216 NULL,
217 0,
218 NULL,
219 PURPLE_PRIORITY_DEFAULT,
220
221 "core-answerscripts",
222 "AnswerScripts",
223 "0.5.4",
224 "Framework for hooking scripts to process received messages for libpurple clients",
225 "\nThis plugin will execute script \"~/.purple/" ANSWERSCRIPT "\" "
226 "(or any other executable called \"" ANSWERSCRIPT "\" and found in purple_user_dir()) "
227 "each time when instant message is received.\n"
228 "\n- Any text printed to STDOUT by this script will be sent back as answer to received message."
229 "\n- Following environment values will be set, so script can use them for responding:\n"
230 "\t- " ENV_PREFIX "* (see documentation or env for more)\n"
231 "\nPlease see sample scripts, documentation, website and source code for more informations...\n"
232 "\n(-; Peace ;-)\n",
233 "Tomas Mudrunka <harviecz@gmail.cz>",
234 "http://github.com/harvie/libpurple-core-answerscripts",
235
236 plugin_load,
237 plugin_unload,
238 NULL,
239 NULL,
240 NULL,
241 NULL,
242 NULL,
243 NULL,
244 NULL,
245 NULL,
246 NULL
247 };
248
249 static void init_plugin(PurplePlugin * plugin) {
250 //Export static environment variables
251 //#ifndef __x86_64__ //Workaround for x86_64 (where this causes problems for unknown reason)
252 const char * core_ui = check_null(purple_core_get_ui());
253 const char * core_version = check_null(purple_core_get_version());
254 setenv(ENV_PREFIX "L_AGENT", (char *) core_ui, 1); //ID of IM client used with answerscripts
255 setenv(ENV_PREFIX "L_AGENT_VERSION", (char *) core_version, 1); //Version of client
256 //#endif
257 }
258
259 PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.415428 seconds and 4 git commands to generate.