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