added some notes for future development
[mirrors/libpurple-core-answerscripts.git] / answerscripts.c
CommitLineData
73f7c2ff 1//#define __WIN32__
058e63ec
H
2#ifndef __WIN32__
3 #define ANSWERSCRIPT_EXT ""
4#else
5 #define ANSWERSCRIPT_EXT ".exe"
6#endif
7#define ANSWERSCRIPT "answerscripts" ANSWERSCRIPT_EXT
53478f8c
H
8#define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
9#define ANSWERSCRIPTS_LINE_LENGTH 4096
0636d441 10#define ENV_PREFIX "ANSW_"
2bcad731 11#define PROTOCOL_PREFIX "prpl-"
b44d7b66
H
12
13#include <stdio.h>
14#include <stdlib.h>
81749a99 15#include <errno.h>
2bcad731 16#include <string.h>
b44d7b66 17
81749a99
H
18#ifndef __WIN32__
19 #include <fcntl.h>
b86255fd
H
20#else
21 #include <windows.h>
81749a99
H
22#endif
23
53478f8c
H
24/* Purple plugin */
25#define PURPLE_PLUGINS
26#include <libpurple/debug.h>
27#include <libpurple/version.h>
28#include <libpurple/conversation.h>
29#include <libpurple/plugin.h>
30#include <libpurple/signals.h>
31#include <libpurple/util.h>
73f7c2ff 32
ca489436 33char *message = NULL;
b44d7b66 34char *hook_script = NULL;
b44d7b66 35
6ec3c19f
H
36typedef struct {
37 FILE *pipe;
38 PurpleConversation *conv;
39} answerscripts_job;
40
81749a99 41int answerscripts_process_message_cb(answerscripts_job *job) {
64ac1ebf
H
42 int i;
43 char response[ANSWERSCRIPTS_LINE_LENGTH+1];
6ec3c19f
H
44 FILE *pipe = job->pipe;
45 PurpleConversation *conv = job->conv;
46
81749a99
H
47 if (pipe && !feof(pipe)) {
48 if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe)
b86255fd 49 && (errno == EWOULDBLOCK || errno == EAGAIN) //WARNING! Not compatible with windows :-(
81749a99
H
50 ) return 1;
51
d852a694 52 for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0;
6ec3c19f 53 purple_conv_im_send(purple_conversation_get_im_data(conv), response);
81749a99
H
54
55 if(!feof(pipe)) return 1;
d852a694
H
56 }
57 pclose(pipe);
6ec3c19f
H
58 free(job);
59 return 0;
d852a694
H
60}
61
73f7c2ff
H
62static void received_im_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) {
63 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 64
ca489436
H
65 //Get message
66 message = purple_markup_strip_html(buffer);
67 //printf("\nHarvie received: %s: %s\n", who, message); //debug
b44d7b66
H
68 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
69
c77055a9
H
70 /* Here are prototypes of some functions interesting to implement github feature request #3
71
72 LOCAL USER:
73 const char* purple_account_get_alias ( const PurpleAccount * account )
74 const gchar* purple_account_get_name_for_display ( const PurpleAccount * account )
75 REMOTE USER (Buddy):
76 const char * purple_contact_get_alias (PurpleContact *contact)
77 const char * purple_buddy_get_name (const PurpleBuddy *buddy)
78 const char * purple_buddy_get_alias_only (PurpleBuddy *buddy)
79 const char * purple_buddy_get_server_alias (PurpleBuddy *buddy)
80 const char * purple_buddy_get_contact_alias (PurpleBuddy *buddy)
81 const char * purple_buddy_get_local_alias (PurpleBuddy *buddy)
82 const char * purple_buddy_get_alias (PurpleBuddy *buddy)
83 PurpleGroup * purple_buddy_get_group (PurpleBuddy *buddy)
84 const char * purple_group_get_name (PurpleGroup *group)
85 */
86
2bcad731
H
87 //Get protocol ID
88 const char *protocol_id = purple_account_get_protocol_id(account);
89 if(!strncmp(protocol_id,PROTOCOL_PREFIX,strlen(PROTOCOL_PREFIX))) protocol_id += strlen(PROTOCOL_PREFIX); //trim out protocol prefix (eg.: "prpl-irc" => "irc")
90
7a45e05c
H
91 //Get status
92 PurpleStatus *status = purple_account_get_active_status(account);
93 PurpleStatusType *type = purple_status_get_type(status);
94
95 //Get status id
96 const char *status_id = NULL;
97 status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type));
98
99 //Get status message
100 const char *status_msg = NULL;
101 if (purple_status_type_get_attr(type, "message") != NULL) {
102 status_msg = purple_status_get_attr_string(status, "message");
103 } else {
104 status_msg = (char *) purple_savedstatus_get_message(purple_savedstatus_get_current());
105 }
106
ca489436 107 //Export variables to environment
51b65634 108 setenv(ENV_PREFIX "MSG", message, 1);
2bcad731
H
109 setenv(ENV_PREFIX "FROM", who, 1);
110 setenv(ENV_PREFIX "PROTOCOL", protocol_id, 1);
51b65634
H
111 setenv(ENV_PREFIX "STATUS", status_id, 1);
112 setenv(ENV_PREFIX "STATUS_MSG", status_msg, 1);
6ec3c19f 113
ca489436 114 //Launch job on background
6ec3c19f
H
115 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
116 job->pipe = popen(hook_script, "r");
117 job->conv = conv;
118
81749a99
H
119 #ifndef __WIN32__
120 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
121 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
b86255fd
H
122 #else
123 //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows
81749a99
H
124 #endif
125
126 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
b44d7b66
H
127}
128
b44d7b66 129static gboolean plugin_load(PurplePlugin * plugin) {
6ec3c19f 130 asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT);
b44d7b66 131 void *conv_handle = purple_conversations_get_handle();
73f7c2ff 132 purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL);
53478f8c 133 return TRUE;
b44d7b66
H
134}
135
136static gboolean plugin_unload(PurplePlugin * plugin) {
137 free(hook_script);
53478f8c 138 return TRUE;
b44d7b66
H
139}
140
141static PurplePluginInfo info = {
142 PURPLE_PLUGIN_MAGIC,
143 PURPLE_MAJOR_VERSION,
144 PURPLE_MINOR_VERSION,
145 PURPLE_PLUGIN_STANDARD,
146 NULL,
147 0,
148 NULL,
149 PURPLE_PRIORITY_DEFAULT,
150
151 "core-answerscripts",
152 "AnswerScripts",
37436d76 153 "0.3.1",
53478f8c 154 "Framework for hooking scripts to process received messages for libpurple clients",
37436d76
H
155 "\nThis plugin will execute script \"~/.purple/" ANSWERSCRIPT "\" "
156 "(or any other executable called \"" ANSWERSCRIPT "\" and found in purple_user_dir()) "
157 "each time when instant message is received.\n"
158 "\n- Any text printed to STDOUT by this script will be sent back as answer to received message."
159 "\n- Following environment values will be set, so script can use them for responding:\n"
160 "\t- ANSW_MSG\n"
161 "\t- ANSW_FROM\n"
162 "\t- ANSW_PROTOCOL\n"
163 "\t- ANSW_STATUS\n"
164 "\t- ANSW_STATUS_MSG\n"
165 "\nPlease see sample scripts, documentation, website and source code for more informations...\n"
166 "\n(-; Peace ;-)\n",
167 "Tomas Mudrunka <harvie@email.cz>",
53478f8c 168 "http://github.com/harvie/libpurple-core-answerscripts",
b44d7b66
H
169
170 plugin_load,
171 plugin_unload,
172 NULL,
173 NULL,
174 NULL,
175 NULL,
176 NULL,
177 NULL,
178 NULL,
179 NULL,
180 NULL
181};
182
183static void init_plugin(PurplePlugin * plugin) {
184
185}
186
187PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.306567 seconds and 4 git commands to generate.