3 #define ANSWERSCRIPT_EXT ""
5 #define ANSWERSCRIPT_EXT ".exe"
7 #define ANSWERSCRIPT "answerscripts" ANSWERSCRIPT_EXT
8 #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
9 #define ANSWERSCRIPTS_LINE_LENGTH 4096
20 #define PURPLE_PLUGINS
21 #include <libpurple/debug.h>
22 #include <libpurple/version.h>
23 #include <libpurple/conversation.h>
24 #include <libpurple/plugin.h>
25 #include <libpurple/signals.h>
26 #include <libpurple/util.h>
29 char *hook_script
= NULL
;
30 char response
[ANSWERSCRIPTS_LINE_LENGTH
+1];
35 PurpleConversation
*conv
;
38 int answerscripts_process_message_cb(answerscripts_job
*job
) {
39 FILE *pipe
= job
->pipe
;
40 PurpleConversation
*conv
= job
->conv
;
42 if (pipe
&& !feof(pipe
)) {
43 if(!fgets(response
, ANSWERSCRIPTS_LINE_LENGTH
, pipe
)
44 && (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
47 for(i
=0;response
[i
];i
++) if(response
[i
]=='\n') response
[i
]=0;
48 purple_conv_im_send(purple_conversation_get_im_data(conv
), response
);
50 if(!feof(pipe
)) return 1;
57 static void received_im_msg_cb(PurpleAccount
*account
, char *who
, char *buffer
, PurpleConversation
*conv
, PurpleMessageFlags flags
, void *data
) {
58 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: */
60 buff
= purple_markup_strip_html(buffer
);
61 //printf("\nHarvie received: %s: %s\n", who, buff); //debug
62 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
64 setenv("PURPLE_FROM", who
, 1);
65 setenv("PURPLE_MSG", buff
, 1);
68 answerscripts_job
*job
= (answerscripts_job
*) malloc(sizeof(answerscripts_job
));
69 job
->pipe
= popen(hook_script
, "r");
73 int fflags
= fcntl(fileno(job
->pipe
), F_GETFL
, 0);
74 fcntl(fileno(job
->pipe
), F_SETFL
, fflags
| O_NONBLOCK
);
77 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL
, (GSourceFunc
) answerscripts_process_message_cb
, (gpointer
) job
);
81 static gboolean
plugin_load(PurplePlugin
* plugin
) {
82 asprintf(&hook_script
,"%s/%s",purple_user_dir(),ANSWERSCRIPT
);
83 void *conv_handle
= purple_conversations_get_handle();
84 purple_signal_connect(conv_handle
, "received-im-msg", plugin
, PURPLE_CALLBACK(received_im_msg_cb
), NULL
);
88 static gboolean
plugin_unload(PurplePlugin
* plugin
) {
93 static PurplePluginInfo info
= {
97 PURPLE_PLUGIN_STANDARD
,
101 PURPLE_PRIORITY_DEFAULT
,
103 "core-answerscripts",
106 "Framework for hooking scripts to process received messages for libpurple clients",
107 "This plugin will execute script ~/.purple/" ANSWERSCRIPT
" "
108 "or any other executable called " ANSWERSCRIPT
" and found in purple_user_dir() "
109 "for each single instant message received.\n"
110 "\n- Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry "
111 "informations about message text and sender so script can respond to that message."
112 "\n- Any text printed to STDOUT by the script will be sent back as answer to message."
113 "\n\nPlease see example scripts, documentation or source code for more informations...",
114 "Harvie <harvie@email.cz>",
115 "http://github.com/harvie/libpurple-core-answerscripts",
130 static void init_plugin(PurplePlugin
* plugin
) {
134 PURPLE_INIT_PLUGIN(autoanswer
, init_plugin
, info
)