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>
20 #define ANSWERSCRIPT "answerscripts.exe"
21 #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
22 #define ANSWERSCRIPTS_LINE_LENGTH 4096
31 char *hook_script
= NULL
;
32 char response
[ANSWERSCRIPTS_LINE_LENGTH
+1];
37 PurpleConversation
*conv
;
40 int answerscripts_process_message_cb(answerscripts_job
*job
) {
41 FILE *pipe
= job
->pipe
;
42 PurpleConversation
*conv
= job
->conv
;
44 if (pipe
&& !feof(pipe
)) {
45 if(!fgets(response
, ANSWERSCRIPTS_LINE_LENGTH
, pipe
)
46 && (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
49 for(i
=0;response
[i
];i
++) if(response
[i
]=='\n') response
[i
]=0;
50 purple_conv_im_send(purple_conversation_get_im_data(conv
), response
);
52 if(!feof(pipe
)) return 1;
60 received_im_msg_cb(PurpleAccount
*account
, char *who
, char *buffer
, PurpleConversation
*conv
, PurpleMessageFlags flags
, void *data
) {
62 /* A workaround to avoid skipping of the first message as a result on NULL-conv: */
63 if (conv
== NULL
) conv
= purple_conversation_new(PURPLE_CONV_TYPE_IM
, account
, who
);
65 buff
= purple_markup_strip_html(buffer
);
66 //printf("\nHarvie received: %s: %s\n", who, buff); //debug
67 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
69 setenv("PURPLE_FROM", who
, 1);
70 setenv("PURPLE_MSG", buff
, 1);
73 answerscripts_job
*job
= (answerscripts_job
*) malloc(sizeof(answerscripts_job
));
74 job
->pipe
= popen(hook_script
, "r");
78 int fflags
= fcntl(fileno(job
->pipe
), F_GETFL
, 0);
79 fcntl(fileno(job
->pipe
), F_SETFL
, fflags
| O_NONBLOCK
);
82 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL
, (GSourceFunc
) answerscripts_process_message_cb
, (gpointer
) job
);
87 static gboolean
plugin_load(PurplePlugin
* plugin
) {
88 asprintf(&hook_script
,"%s/%s",purple_user_dir(),ANSWERSCRIPT
);
90 void *conv_handle
= purple_conversations_get_handle();
92 purple_signal_connect(conv_handle
, "received-im-msg",
93 plugin
, PURPLE_CALLBACK(received_im_msg_cb
),
98 static gboolean
plugin_unload(PurplePlugin
* plugin
) {
103 static PurplePluginInfo info
= {
105 PURPLE_MAJOR_VERSION
,
106 PURPLE_MINOR_VERSION
,
107 PURPLE_PLUGIN_STANDARD
,
111 PURPLE_PRIORITY_DEFAULT
,
113 "core-answerscripts",
116 "Framework for hooking scripts to received messages for various libpurple clients",
117 "This plugin will call ~/.purple/" ANSWERSCRIPT
" (or wherever purple_user_dir() points) "
118 "script (or any executable) for each single message called."
119 "Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry "
120 "informations about message text and sender so script can respond to that message. "
121 "Any text printed to STDOUT by the script will be sent back as answer to message. "
122 "Please see example scripts for more informations...",
123 "Harvie <harvie@email.cz>",
124 "http://github.com/harvie",
139 static void init_plugin(PurplePlugin
* plugin
) {
143 PURPLE_INIT_PLUGIN(autoanswer
, init_plugin
, info
)