5 #include <libpurple/debug.h>
6 #include <libpurple/version.h>
7 #include <libpurple/conversation.h>
8 //#include <libpurple/log.h>
9 #include <libpurple/plugin.h>
10 //#include <libpurple/signals.h>
11 #include <libpurple/util.h>
12 #include <libpurple/notify.h>
22 #define ANSWERSCRIPT "answerscripts.exe"
23 #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
24 #define ANSWERSCRIPTS_LINE_LENGTH 4096
27 char *hook_script
= NULL
;
28 char response
[ANSWERSCRIPTS_LINE_LENGTH
+1];
33 PurpleConversation
*conv
;
36 int answerscripts_process_message_cb(answerscripts_job
*job
) {
37 FILE *pipe
= job
->pipe
;
38 PurpleConversation
*conv
= job
->conv
;
40 if (pipe
&& !feof(pipe
)) {
41 if(!fgets(response
, ANSWERSCRIPTS_LINE_LENGTH
, pipe
)
42 && (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
45 for(i
=0;response
[i
];i
++) if(response
[i
]=='\n') response
[i
]=0;
46 purple_conv_im_send(purple_conversation_get_im_data(conv
), response
);
48 if(!feof(pipe
)) return 1;
55 static void received_im_msg_cb(PurpleAccount
*account
, char *who
, char *buffer
, PurpleConversation
*conv
, PurpleMessageFlags flags
, void *data
) {
56 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: */
58 buff
= purple_markup_strip_html(buffer
);
59 //printf("\nHarvie received: %s: %s\n", who, buff); //debug
60 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
62 setenv("PURPLE_FROM", who
, 1);
63 setenv("PURPLE_MSG", buff
, 1);
66 answerscripts_job
*job
= (answerscripts_job
*) malloc(sizeof(answerscripts_job
));
67 job
->pipe
= popen(hook_script
, "r");
71 int fflags
= fcntl(fileno(job
->pipe
), F_GETFL
, 0);
72 fcntl(fileno(job
->pipe
), F_SETFL
, fflags
| O_NONBLOCK
);
75 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL
, (GSourceFunc
) answerscripts_process_message_cb
, (gpointer
) job
);
79 static gboolean
plugin_load(PurplePlugin
* plugin
) {
80 asprintf(&hook_script
,"%s/%s",purple_user_dir(),ANSWERSCRIPT
);
81 void *conv_handle
= purple_conversations_get_handle();
82 purple_signal_connect(conv_handle
, "received-im-msg", plugin
, PURPLE_CALLBACK(received_im_msg_cb
), NULL
);
86 static gboolean
plugin_unload(PurplePlugin
* plugin
) {
91 static PurplePluginInfo info
= {
95 PURPLE_PLUGIN_STANDARD
,
99 PURPLE_PRIORITY_DEFAULT
,
101 "core-answerscripts",
104 "Framework for hooking scripts to received messages for various libpurple clients",
105 "This plugin will call ~/.purple/" ANSWERSCRIPT
" (or wherever purple_user_dir() points) "
106 "script (or any executable) for each single message called."
107 "Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry "
108 "informations about message text and sender so script can respond to that message. "
109 "Any text printed to STDOUT by the script will be sent back as answer to message. "
110 "Please see example scripts for more informations...",
111 "Harvie <harvie@email.cz>",
112 "http://github.com/harvie",
127 static void init_plugin(PurplePlugin
* plugin
) {
131 PURPLE_INIT_PLUGIN(autoanswer
, init_plugin
, info
)