44fa0c12ed20972d76c6ef6d6cfc369bd5051f3a
[mirrors/libpurple-core-answerscripts.git] / answerscripts.c
1 //#define __WIN32__
2 #ifndef __WIN32__
3 #define ANSWERSCRIPT_EXT ""
4 #else
5 #define ANSWERSCRIPT_EXT ".exe"
6 #endif
7 #define ANSWERSCRIPT "answerscripts" ANSWERSCRIPT_EXT
8 #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
9 #define ANSWERSCRIPTS_LINE_LENGTH 4096
10 #define ENV_PREFIX "ANSW_"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <errno.h>
15
16 #ifndef __WIN32__
17 #include <fcntl.h>
18 #endif
19
20 /* Purple plugin */
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>
28
29 char *message = NULL;
30 char *hook_script = NULL;
31 char response[ANSWERSCRIPTS_LINE_LENGTH+1];
32 int i;
33
34 typedef struct {
35 FILE *pipe;
36 PurpleConversation *conv;
37 } answerscripts_job;
38
39 int answerscripts_process_message_cb(answerscripts_job *job) {
40 FILE *pipe = job->pipe;
41 PurpleConversation *conv = job->conv;
42
43 if (pipe && !feof(pipe)) {
44 if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe)
45 && (errno == EWOULDBLOCK || errno == EAGAIN)
46 ) return 1;
47
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);
50
51 if(!feof(pipe)) return 1;
52 }
53 pclose(pipe);
54 free(job);
55 return 0;
56 }
57
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: */
60
61 //Get message
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
65
66 //Get status
67 PurpleStatus *status = purple_account_get_active_status(account);
68 PurpleStatusType *type = purple_status_get_type(status);
69
70 //Get status id
71 const char *status_id = NULL;
72 status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type));
73
74 //Get status message
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");
78 } else {
79 status_msg = (char *) purple_savedstatus_get_message(purple_savedstatus_get_current());
80 }
81
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);
87
88 //Launch job on background
89 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
90 job->pipe = popen(hook_script, "r");
91 job->conv = conv;
92
93 #ifndef __WIN32__
94 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
95 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
96 #endif
97
98 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
99 }
100
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);
105 return TRUE;
106 }
107
108 static gboolean plugin_unload(PurplePlugin * plugin) {
109 free(hook_script);
110 return TRUE;
111 }
112
113 static PurplePluginInfo info = {
114 PURPLE_PLUGIN_MAGIC,
115 PURPLE_MAJOR_VERSION,
116 PURPLE_MINOR_VERSION,
117 PURPLE_PLUGIN_STANDARD,
118 NULL,
119 0,
120 NULL,
121 PURPLE_PRIORITY_DEFAULT,
122
123 "core-answerscripts",
124 "AnswerScripts",
125 "0.3.0",
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",
136
137 plugin_load,
138 plugin_unload,
139 NULL,
140 NULL,
141 NULL,
142 NULL,
143 NULL,
144 NULL,
145 NULL,
146 NULL,
147 NULL
148 };
149
150 static void init_plugin(PurplePlugin * plugin) {
151
152 }
153
154 PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.313746 seconds and 3 git commands to generate.