Commit | Line | Data |
---|---|---|
73f7c2ff | 1 | //#define __WIN32__ |
53478f8c H |
2 | #define ANSWERSCRIPT "answerscripts.exe" |
3 | #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250 | |
4 | #define ANSWERSCRIPTS_LINE_LENGTH 4096 | |
b44d7b66 H |
5 | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
81749a99 | 8 | #include <errno.h> |
b44d7b66 | 9 | |
81749a99 H |
10 | #ifndef __WIN32__ |
11 | #include <fcntl.h> | |
12 | #endif | |
13 | ||
53478f8c H |
14 | /* Purple plugin */ |
15 | #define PURPLE_PLUGINS | |
16 | #include <libpurple/debug.h> | |
17 | #include <libpurple/version.h> | |
18 | #include <libpurple/conversation.h> | |
19 | #include <libpurple/plugin.h> | |
20 | #include <libpurple/signals.h> | |
21 | #include <libpurple/util.h> | |
73f7c2ff | 22 | |
b44d7b66 H |
23 | char *buff = NULL; |
24 | char *hook_script = NULL; | |
6ec3c19f | 25 | char response[ANSWERSCRIPTS_LINE_LENGTH+1]; |
b44d7b66 H |
26 | int i; |
27 | ||
6ec3c19f H |
28 | typedef struct { |
29 | FILE *pipe; | |
30 | PurpleConversation *conv; | |
31 | } answerscripts_job; | |
32 | ||
81749a99 | 33 | int answerscripts_process_message_cb(answerscripts_job *job) { |
6ec3c19f H |
34 | FILE *pipe = job->pipe; |
35 | PurpleConversation *conv = job->conv; | |
36 | ||
81749a99 H |
37 | if (pipe && !feof(pipe)) { |
38 | if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe) | |
39 | && (errno == EWOULDBLOCK || errno == EAGAIN) | |
40 | ) return 1; | |
41 | ||
d852a694 | 42 | for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0; |
6ec3c19f | 43 | purple_conv_im_send(purple_conversation_get_im_data(conv), response); |
81749a99 H |
44 | |
45 | if(!feof(pipe)) return 1; | |
d852a694 H |
46 | } |
47 | pclose(pipe); | |
6ec3c19f H |
48 | free(job); |
49 | return 0; | |
d852a694 H |
50 | } |
51 | ||
73f7c2ff H |
52 | static void received_im_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) { |
53 | 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: */ | |
b44d7b66 H |
54 | |
55 | buff = purple_markup_strip_html(buffer); | |
56 | //printf("\nHarvie received: %s: %s\n", who, buff); //debug | |
57 | //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug | |
58 | ||
59 | setenv("PURPLE_FROM", who, 1); | |
60 | setenv("PURPLE_MSG", buff, 1); | |
61 | ||
6ec3c19f H |
62 | |
63 | answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job)); | |
64 | job->pipe = popen(hook_script, "r"); | |
65 | job->conv = conv; | |
66 | ||
81749a99 H |
67 | #ifndef __WIN32__ |
68 | int fflags = fcntl(fileno(job->pipe), F_GETFL, 0); | |
69 | fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK); | |
70 | #endif | |
71 | ||
72 | purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job); | |
b44d7b66 H |
73 | } |
74 | ||
75 | ||
76 | static gboolean plugin_load(PurplePlugin * plugin) { | |
6ec3c19f | 77 | asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT); |
b44d7b66 | 78 | void *conv_handle = purple_conversations_get_handle(); |
73f7c2ff | 79 | purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL); |
53478f8c | 80 | return TRUE; |
b44d7b66 H |
81 | } |
82 | ||
83 | static gboolean plugin_unload(PurplePlugin * plugin) { | |
84 | free(hook_script); | |
53478f8c | 85 | return TRUE; |
b44d7b66 H |
86 | } |
87 | ||
88 | static PurplePluginInfo info = { | |
89 | PURPLE_PLUGIN_MAGIC, | |
90 | PURPLE_MAJOR_VERSION, | |
91 | PURPLE_MINOR_VERSION, | |
92 | PURPLE_PLUGIN_STANDARD, | |
93 | NULL, | |
94 | 0, | |
95 | NULL, | |
96 | PURPLE_PRIORITY_DEFAULT, | |
97 | ||
98 | "core-answerscripts", | |
99 | "AnswerScripts", | |
53478f8c H |
100 | "0.2.1", |
101 | "Framework for hooking scripts to process received messages for libpurple clients", | |
102 | "This plugin will execute script ~/.purple/" ANSWERSCRIPT " " | |
103 | "or any other executable called " ANSWERSCRIPT " and found in purple_user_dir() " | |
104 | "for each single instant message received.\n" | |
105 | "\n- Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry " | |
106 | "informations about message text and sender so script can respond to that message." | |
107 | "\n- Any text printed to STDOUT by the script will be sent back as answer to message." | |
108 | "\n\nPlease see example scripts, documentation or source code for more informations...", | |
b44d7b66 | 109 | "Harvie <harvie@email.cz>", |
53478f8c | 110 | "http://github.com/harvie/libpurple-core-answerscripts", |
b44d7b66 H |
111 | |
112 | plugin_load, | |
113 | plugin_unload, | |
114 | NULL, | |
115 | NULL, | |
116 | NULL, | |
117 | NULL, | |
118 | NULL, | |
119 | NULL, | |
120 | NULL, | |
121 | NULL, | |
122 | NULL | |
123 | }; | |
124 | ||
125 | static void init_plugin(PurplePlugin * plugin) { | |
126 | ||
127 | } | |
128 | ||
129 | PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info) |