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
10 #define ENV_PREFIX "ANSW_"
21 #define PURPLE_PLUGINS
22 #include <libpurple/debug.h>
23 #include <libpurple/version.h>
24 #include <libpurple/conversation.h>
25 #include <libpurple/plugin.h>
26 #include <libpurple/signals.h>
27 #include <libpurple/util.h>
30 char *hook_script
= NULL
;
31 char response
[ANSWERSCRIPTS_LINE_LENGTH
+1];
36 PurpleConversation
*conv
;
39 int answerscripts_process_message_cb(answerscripts_job
*job
) {
40 FILE *pipe
= job
->pipe
;
41 PurpleConversation
*conv
= job
->conv
;
43 if (pipe
&& !feof(pipe
)) {
44 if(!fgets(response
, ANSWERSCRIPTS_LINE_LENGTH
, pipe
)
45 && (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
48 for(i
=0;response
[i
];i
++) if(response
[i
]=='\n') response
[i
]=0;
49 purple_conv_im_send(purple_conversation_get_im_data(conv
), response
);
51 if(!feof(pipe
)) return 1;
58 static void received_im_msg_cb(PurpleAccount
*account
, char *who
, char *buffer
, PurpleConversation
*conv
, PurpleMessageFlags flags
, void *data
) {
59 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: */
62 message
= purple_markup_strip_html(buffer
);
63 //printf("\nHarvie received: %s: %s\n", who, message); //debug
64 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
67 PurpleStatus
*status
= purple_account_get_active_status(account
);
68 PurpleStatusType
*type
= purple_status_get_type(status
);
71 const char *status_id
= NULL
;
72 status_id
= purple_primitive_get_id_from_type(purple_status_type_get_primitive(type
));
75 const char *status_msg
= NULL
;
76 if (purple_status_type_get_attr(type
, "message") != NULL
) {
77 status_msg
= purple_status_get_attr_string(status
, "message");
79 status_msg
= (char *) purple_savedstatus_get_message(purple_savedstatus_get_current());
82 //Export variables to environment
83 setenv(ENV_PREFIX
"FROM", who
, 1);
84 setenv(ENV_PREFIX
"MSG", message
, 1);
85 setenv(ENV_PREFIX
"STATUS", status_id
, 1);
86 setenv(ENV_PREFIX
"STATUS_MSG", status_msg
, 1);
88 //Launch job on background
89 answerscripts_job
*job
= (answerscripts_job
*) malloc(sizeof(answerscripts_job
));
90 job
->pipe
= popen(hook_script
, "r");
94 int fflags
= fcntl(fileno(job
->pipe
), F_GETFL
, 0);
95 fcntl(fileno(job
->pipe
), F_SETFL
, fflags
| O_NONBLOCK
);
98 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL
, (GSourceFunc
) answerscripts_process_message_cb
, (gpointer
) job
);
101 static gboolean
plugin_load(PurplePlugin
* plugin
) {
102 asprintf(&hook_script
,"%s/%s",purple_user_dir(),ANSWERSCRIPT
);
103 void *conv_handle
= purple_conversations_get_handle();
104 purple_signal_connect(conv_handle
, "received-im-msg", plugin
, PURPLE_CALLBACK(received_im_msg_cb
), NULL
);
108 static gboolean
plugin_unload(PurplePlugin
* plugin
) {
113 static PurplePluginInfo info
= {
115 PURPLE_MAJOR_VERSION
,
116 PURPLE_MINOR_VERSION
,
117 PURPLE_PLUGIN_STANDARD
,
121 PURPLE_PRIORITY_DEFAULT
,
123 "core-answerscripts",
126 "Framework for hooking scripts to process received messages for libpurple clients",
127 "This plugin will execute script ~/.purple/" ANSWERSCRIPT
" "
128 "or any other executable called " ANSWERSCRIPT
" and found in purple_user_dir() "
129 "for each single instant message received.\n"
130 "\n- Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry "
131 "informations about message text and sender so script can respond to that message."
132 "\n- Any text printed to STDOUT by the script will be sent back as answer to message."
133 "\n\nPlease see example scripts, documentation or source code for more informations...",
134 "Harvie <harvie@email.cz>",
135 "http://github.com/harvie/libpurple-core-answerscripts",
150 static void init_plugin(PurplePlugin
* plugin
) {
154 PURPLE_INIT_PLUGIN(autoanswer
, init_plugin
, info
)