4 #include <libpurple/debug.h>
5 #include <libpurple/version.h>
6 #include <libpurple/conversation.h>
7 #include <libpurple/debug.h>
8 #include <libpurple/log.h>
9 #include <libpurple/plugin.h>
10 #include <libpurple/pluginpref.h>
11 #include <libpurple/prefs.h>
12 #include <libpurple/signals.h>
13 #include <libpurple/util.h>
14 #include <libpurple/notify.h>
19 #define ANSWERSCRIPT "answerscripts.exe"
20 #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
21 #define ANSWERSCRIPTS_LINE_LENGTH 4096
24 char *hook_script
= NULL
;
25 char response
[ANSWERSCRIPTS_LINE_LENGTH
+1];
30 PurpleConversation
*conv
;
33 int answerscripts_process_message(answerscripts_job
*job
) {
34 //TODO: process scripts and send response asynchronously
35 FILE *pipe
= job
->pipe
;
36 PurpleConversation
*conv
= job
->conv
;
38 if (pipe
&& fgets(response
, ANSWERSCRIPTS_LINE_LENGTH
, pipe
)) {
39 for(i
=0;response
[i
];i
++) if(response
[i
]=='\n') response
[i
]=0;
40 purple_conv_im_send(purple_conversation_get_im_data(conv
), response
);
49 received_im_msg_cb(PurpleAccount
*account
, char *who
, char *buffer
, PurpleConversation
*conv
, PurpleMessageFlags flags
, void *data
) {
51 /* A workaround to avoid skipping of the first message as a result on NULL-conv: */
52 if (conv
== NULL
) conv
= purple_conversation_new(PURPLE_CONV_TYPE_IM
, account
, who
);
54 buff
= purple_markup_strip_html(buffer
);
55 //printf("\nHarvie received: %s: %s\n", who, buff); //debug
56 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
58 setenv("PURPLE_FROM", who
, 1);
59 setenv("PURPLE_MSG", buff
, 1);
62 answerscripts_job
*job
= (answerscripts_job
*) malloc(sizeof(answerscripts_job
));
63 job
->pipe
= popen(hook_script
, "r");
66 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL
, answerscripts_process_message
, (gpointer
) job
);
71 static gboolean
plugin_load(PurplePlugin
* plugin
) {
72 asprintf(&hook_script
,"%s/%s",purple_user_dir(),ANSWERSCRIPT
);
74 void *conv_handle
= purple_conversations_get_handle();
76 purple_signal_connect(conv_handle
, "received-im-msg",
77 plugin
, PURPLE_CALLBACK(received_im_msg_cb
),
82 static gboolean
plugin_unload(PurplePlugin
* plugin
) {
87 static PurplePluginInfo info
= {
91 PURPLE_PLUGIN_STANDARD
,
95 PURPLE_PRIORITY_DEFAULT
,
100 "Framework for hooking scripts to received messages for various libpurple clients",
101 "This plugin will call ~/.purple/" ANSWERSCRIPT
" (or wherever purple_user_dir() points) "
102 "script (or any executable) for each single message called."
103 "Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry "
104 "informations about message text and sender so script can respond to that message. "
105 "Any text printed to STDOUT by the script will be sent back as answer to message. "
106 "Please see example scripts for more informations...",
107 "Harvie <harvie@email.cz>",
108 "http://github.com/harvie",
123 static void init_plugin(PurplePlugin
* plugin
) {
127 PURPLE_INIT_PLUGIN(autoanswer
, init_plugin
, info
)