asynchronous execution finaly working
[mirrors/libpurple-core-answerscripts.git] / answerscripts.c
CommitLineData
b44d7b66
H
1#define PURPLE_PLUGINS
2
3/* Purple headers */
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>
15
16#include <stdio.h>
17#include <stdlib.h>
81749a99 18#include <errno.h>
b44d7b66 19
6ec3c19f
H
20#define ANSWERSCRIPT "answerscripts.exe"
21#define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
22#define ANSWERSCRIPTS_LINE_LENGTH 4096
d852a694 23
81749a99
H
24//#define __WIN32__
25
26#ifndef __WIN32__
27 #include <fcntl.h>
28#endif
29
b44d7b66
H
30char *buff = NULL;
31char *hook_script = NULL;
6ec3c19f 32char response[ANSWERSCRIPTS_LINE_LENGTH+1];
b44d7b66
H
33int i;
34
6ec3c19f
H
35typedef struct {
36 FILE *pipe;
37 PurpleConversation *conv;
38} answerscripts_job;
39
81749a99 40int answerscripts_process_message_cb(answerscripts_job *job) {
6ec3c19f
H
41 FILE *pipe = job->pipe;
42 PurpleConversation *conv = job->conv;
43
81749a99
H
44 if (pipe && !feof(pipe)) {
45 if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe)
46 && (errno == EWOULDBLOCK || errno == EAGAIN)
47 ) return 1;
48
d852a694 49 for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0;
6ec3c19f 50 purple_conv_im_send(purple_conversation_get_im_data(conv), response);
81749a99
H
51
52 if(!feof(pipe)) return 1;
d852a694
H
53 }
54 pclose(pipe);
6ec3c19f
H
55 free(job);
56 return 0;
d852a694
H
57}
58
b44d7b66 59static void
6ec3c19f
H
60received_im_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) {
61
b44d7b66
H
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);
64
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
68
69 setenv("PURPLE_FROM", who, 1);
70 setenv("PURPLE_MSG", buff, 1);
71
6ec3c19f
H
72
73 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
74 job->pipe = popen(hook_script, "r");
75 job->conv = conv;
76
81749a99
H
77 #ifndef __WIN32__
78 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
79 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
80 #endif
81
82 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
6ec3c19f 83
b44d7b66
H
84}
85
86
87static gboolean plugin_load(PurplePlugin * plugin) {
6ec3c19f 88 asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT);
b44d7b66
H
89
90 void *conv_handle = purple_conversations_get_handle();
91
92 purple_signal_connect(conv_handle, "received-im-msg",
93 plugin, PURPLE_CALLBACK(received_im_msg_cb),
94 NULL);
95 return TRUE;
96}
97
98static gboolean plugin_unload(PurplePlugin * plugin) {
99 free(hook_script);
100 return TRUE;
101}
102
103static PurplePluginInfo info = {
104 PURPLE_PLUGIN_MAGIC,
105 PURPLE_MAJOR_VERSION,
106 PURPLE_MINOR_VERSION,
107 PURPLE_PLUGIN_STANDARD,
108 NULL,
109 0,
110 NULL,
111 PURPLE_PRIORITY_DEFAULT,
112
113 "core-answerscripts",
114 "AnswerScripts",
4446826a 115 "0.1.1",
b44d7b66 116 "Framework for hooking scripts to received messages for various libpurple clients",
6ec3c19f 117 "This plugin will call ~/.purple/" ANSWERSCRIPT " (or wherever purple_user_dir() points) "
b44d7b66
H
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",
125
126 plugin_load,
127 plugin_unload,
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 NULL
137};
138
139static void init_plugin(PurplePlugin * plugin) {
140
141}
142
143PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.274452 seconds and 4 git commands to generate.