Commit | Line | Data |
---|---|---|
73f7c2ff | 1 | //#define __WIN32__ |
b44d7b66 H |
2 | #define PURPLE_PLUGINS |
3 | ||
4 | /* Purple headers */ | |
5 | #include <libpurple/debug.h> | |
6 | #include <libpurple/version.h> | |
7 | #include <libpurple/conversation.h> | |
73f7c2ff | 8 | //#include <libpurple/log.h> |
b44d7b66 | 9 | #include <libpurple/plugin.h> |
73f7c2ff | 10 | //#include <libpurple/signals.h> |
b44d7b66 H |
11 | #include <libpurple/util.h> |
12 | #include <libpurple/notify.h> | |
13 | ||
14 | #include <stdio.h> | |
15 | #include <stdlib.h> | |
81749a99 | 16 | #include <errno.h> |
b44d7b66 | 17 | |
81749a99 H |
18 | #ifndef __WIN32__ |
19 | #include <fcntl.h> | |
20 | #endif | |
21 | ||
73f7c2ff H |
22 | #define ANSWERSCRIPT "answerscripts.exe" |
23 | #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250 | |
24 | #define ANSWERSCRIPTS_LINE_LENGTH 4096 | |
25 | ||
b44d7b66 H |
26 | char *buff = NULL; |
27 | char *hook_script = NULL; | |
6ec3c19f | 28 | char response[ANSWERSCRIPTS_LINE_LENGTH+1]; |
b44d7b66 H |
29 | int i; |
30 | ||
6ec3c19f H |
31 | typedef struct { |
32 | FILE *pipe; | |
33 | PurpleConversation *conv; | |
34 | } answerscripts_job; | |
35 | ||
81749a99 | 36 | int answerscripts_process_message_cb(answerscripts_job *job) { |
6ec3c19f H |
37 | FILE *pipe = job->pipe; |
38 | PurpleConversation *conv = job->conv; | |
39 | ||
81749a99 H |
40 | if (pipe && !feof(pipe)) { |
41 | if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe) | |
42 | && (errno == EWOULDBLOCK || errno == EAGAIN) | |
43 | ) return 1; | |
44 | ||
d852a694 | 45 | for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0; |
6ec3c19f | 46 | purple_conv_im_send(purple_conversation_get_im_data(conv), response); |
81749a99 H |
47 | |
48 | if(!feof(pipe)) return 1; | |
d852a694 H |
49 | } |
50 | pclose(pipe); | |
6ec3c19f H |
51 | free(job); |
52 | return 0; | |
d852a694 H |
53 | } |
54 | ||
73f7c2ff H |
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: */ | |
b44d7b66 H |
57 | |
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 | |
61 | ||
62 | setenv("PURPLE_FROM", who, 1); | |
63 | setenv("PURPLE_MSG", buff, 1); | |
64 | ||
6ec3c19f H |
65 | |
66 | answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job)); | |
67 | job->pipe = popen(hook_script, "r"); | |
68 | job->conv = conv; | |
69 | ||
81749a99 H |
70 | #ifndef __WIN32__ |
71 | int fflags = fcntl(fileno(job->pipe), F_GETFL, 0); | |
72 | fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK); | |
73 | #endif | |
74 | ||
75 | purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job); | |
b44d7b66 H |
76 | } |
77 | ||
78 | ||
79 | static gboolean plugin_load(PurplePlugin * plugin) { | |
6ec3c19f | 80 | asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT); |
b44d7b66 | 81 | void *conv_handle = purple_conversations_get_handle(); |
73f7c2ff H |
82 | purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL); |
83 | return 0; | |
b44d7b66 H |
84 | } |
85 | ||
86 | static gboolean plugin_unload(PurplePlugin * plugin) { | |
87 | free(hook_script); | |
73f7c2ff | 88 | return 0; |
b44d7b66 H |
89 | } |
90 | ||
91 | static PurplePluginInfo info = { | |
92 | PURPLE_PLUGIN_MAGIC, | |
93 | PURPLE_MAJOR_VERSION, | |
94 | PURPLE_MINOR_VERSION, | |
95 | PURPLE_PLUGIN_STANDARD, | |
96 | NULL, | |
97 | 0, | |
98 | NULL, | |
99 | PURPLE_PRIORITY_DEFAULT, | |
100 | ||
101 | "core-answerscripts", | |
102 | "AnswerScripts", | |
4446826a | 103 | "0.1.1", |
b44d7b66 | 104 | "Framework for hooking scripts to received messages for various libpurple clients", |
6ec3c19f | 105 | "This plugin will call ~/.purple/" ANSWERSCRIPT " (or wherever purple_user_dir() points) " |
b44d7b66 H |
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", | |
113 | ||
114 | plugin_load, | |
115 | plugin_unload, | |
116 | NULL, | |
117 | NULL, | |
118 | NULL, | |
119 | NULL, | |
120 | NULL, | |
121 | NULL, | |
122 | NULL, | |
123 | NULL, | |
124 | NULL | |
125 | }; | |
126 | ||
127 | static void init_plugin(PurplePlugin * plugin) { | |
128 | ||
129 | } | |
130 | ||
131 | PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info) |