| 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> |
| 18 | |
| 19 | #define RESPONSE_LINE_LENGTH 4096 |
| 20 | #define HOOK_SCRIPT "answerscripts.exe" |
| 21 | |
| 22 | char *buff = NULL; |
| 23 | char *hook_script = NULL; |
| 24 | char response[RESPONSE_LINE_LENGTH+1]; |
| 25 | int i; |
| 26 | |
| 27 | static void |
| 28 | received_im_msg_cb(PurpleAccount * account, char *who, char *buffer, |
| 29 | PurpleConversation * conv, PurpleMessageFlags flags, |
| 30 | void *data) { |
| 31 | /* A workaround to avoid skipping of the first message as a result on NULL-conv: */ |
| 32 | if (conv == NULL) conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, who); |
| 33 | |
| 34 | buff = purple_markup_strip_html(buffer); |
| 35 | //printf("\nHarvie received: %s: %s\n", who, buff); //debug |
| 36 | //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug |
| 37 | |
| 38 | setenv("PURPLE_FROM", who, 1); |
| 39 | setenv("PURPLE_MSG", buff, 1); |
| 40 | |
| 41 | FILE* pipe = popen(hook_script, "r"); //TODO: process scripts and send response asynchronously |
| 42 | while (pipe && fgets(response, RESPONSE_LINE_LENGTH, pipe)) { |
| 43 | for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0; |
| 44 | purple_conv_im_send(purple_conversation_get_im_data(conv), response); |
| 45 | } |
| 46 | pclose(pipe); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static gboolean plugin_load(PurplePlugin * plugin) { |
| 51 | asprintf(&hook_script,"%s/%s",purple_user_dir(),HOOK_SCRIPT); |
| 52 | |
| 53 | void *conv_handle = purple_conversations_get_handle(); |
| 54 | |
| 55 | purple_signal_connect(conv_handle, "received-im-msg", |
| 56 | plugin, PURPLE_CALLBACK(received_im_msg_cb), |
| 57 | NULL); |
| 58 | return TRUE; |
| 59 | } |
| 60 | |
| 61 | static gboolean plugin_unload(PurplePlugin * plugin) { |
| 62 | free(hook_script); |
| 63 | return TRUE; |
| 64 | } |
| 65 | |
| 66 | static PurplePluginInfo info = { |
| 67 | PURPLE_PLUGIN_MAGIC, |
| 68 | PURPLE_MAJOR_VERSION, |
| 69 | PURPLE_MINOR_VERSION, |
| 70 | PURPLE_PLUGIN_STANDARD, |
| 71 | NULL, |
| 72 | 0, |
| 73 | NULL, |
| 74 | PURPLE_PRIORITY_DEFAULT, |
| 75 | |
| 76 | "core-answerscripts", |
| 77 | "AnswerScripts", |
| 78 | "0.1", |
| 79 | "Framework for hooking scripts to received messages for various libpurple clients", |
| 80 | "This plugin will call ~/.purple/" HOOK_SCRIPT " (or wherever purple_user_dir() points) " |
| 81 | "script (or any executable) for each single message called." |
| 82 | "Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry " |
| 83 | "informations about message text and sender so script can respond to that message. " |
| 84 | "Any text printed to STDOUT by the script will be sent back as answer to message. " |
| 85 | "Please see example scripts for more informations...", |
| 86 | "Harvie <harvie@email.cz>", |
| 87 | "http://github.com/harvie", |
| 88 | |
| 89 | plugin_load, |
| 90 | plugin_unload, |
| 91 | NULL, |
| 92 | NULL, |
| 93 | NULL, |
| 94 | NULL, |
| 95 | NULL, |
| 96 | NULL, |
| 97 | NULL, |
| 98 | NULL, |
| 99 | NULL |
| 100 | }; |
| 101 | |
| 102 | static void init_plugin(PurplePlugin * plugin) { |
| 103 | |
| 104 | } |
| 105 | |
| 106 | PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info) |