diff options
Diffstat (limited to 'src/ivy-xchat-plugin.c')
-rw-r--r-- | src/ivy-xchat-plugin.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/src/ivy-xchat-plugin.c b/src/ivy-xchat-plugin.c new file mode 100644 index 0000000..ba67c5d --- /dev/null +++ b/src/ivy-xchat-plugin.c @@ -0,0 +1,225 @@ +/* + * ivy-xchat-plugin, an xchat plugin that relays events to and from an Ivy bus + * + * Copyright (C) 2002 + * IntuiLab + * + * only file + * + * Author: Stephane Chatty <chatty@intuilab.com> + * + * $Id$ + * + * Distributed under the LGPL license + * + */ + + +#define USE_PLUGIN +#include <stdio.h> +#include <ivy.h> +#include <ivygtkloop.h> +#include <string.h> +#include "xchat.h" +#include "xchatc.h" +#include "plugin.h" +#include "text.h" +#include "inbound.h" + +extern struct module *module_find (char *name); +extern void module_add_cmds (struct module_cmd_set *xc_cmds); + +/* a bunch of global variables */ +static char *module_name = "ivy-xchat-plugin"; +static char *module_desc = "This plugin relays basic events from a channel to an Ivy bus."; +static int dorelay = 1; + +/* the commands defined by the module */ + +static struct module_cmd_set cmd_set; + +static int cmd_on (struct session *sess, char *tbuf, char *word[], char *word_eol[]); +static int cmd_off (struct session *sess, char *tbuf, char *word[], char *word_eol[]); +static int cmd_bind (struct session *sess, char *tbuf, char *word[], char *word_eol[]); + +static struct commands cmds[] = { + /* name, callback, needserver, needchannel, help */ + {"IvyOn", cmd_on, 0, 0, "/ivyon"}, + {"IvyOff", cmd_off, 0, 0, "/ivyoff"}, + {"IvyRelay", cmd_bind, 0, 0, "/ivyrelay regexp"}, + {"IvyBind", cmd_bind, 0, 0, "/ivybind regexp"}, + {0, 0, 0, 0, 0} +}; + +/* the event subscriptions and their gear, calbacks, etc */ + +struct xp_signal chanmsg_sig; +int (*chanmsg_next) (void *, void *, void *, void *, void *, char); + + +static int +chanmsg (struct server *serv, char* chan, char *from, char *text, char *d, char fromme) +{ + if (dorelay) + IvySendMsg ("IRC [%s] <%s> %s", chan, from, text); + + XP_CALLNEXT (chanmsg_next, serv, chan, from, text, d, fromme); +} + +/* the Ivy callbacks and their help function */ + +static void +IvyBuildMessage (char* p, IvyClientPtr app, int argc, char *argv[]) +{ + int i; + + p += sprintf (p, "%s sent", IvyGetApplicationName (app)); + + /* should limit size of what I add to buf! */ + for (i = 0; i < argc; i++) + p += sprintf (p, " '%s'", argv[i]); + sprintf (p, "\n"); +} + +static void +IvyBindCallback (IvyClientPtr app, void *user_data, int argc, char **argv) +{ + struct session *sess = (struct session*) user_data; + char buf [512]; + + /* forget it if there's no session to print to */ + if (!sess) + return; + + /* print the message */ + IvyBuildMessage (buf, app, argc, argv); + PrintText (sess, buf); +} + +static void +IvyRelayCallback (IvyClientPtr app, void *user_data, int argc, char **argv) +{ + struct session *sess = (struct session*) user_data; + char buf1 [512]; + char buf2 [512]; + + /* forget it if there's no session to print to */ + if (!sess) + return; + + /* relay the message to channel, and print it */ + IvyBuildMessage (buf1, app, argc, argv); + sprintf (buf2, "PRIVMSG %s :%s\r\n", sess->channel, buf1); + tcp_send (sess->server, buf2); + EMIT_SIGNAL (XP_TE_UCHANMSG, sess, sess->server->nick, buf1, "moi", 0, 0); +} + + +/* the commands */ + +static int +cmd_on (struct session *sess, char *tbuf, char *word[], char *word_eol[]) +{ + if (dorelay == 0) { + PrintText (sess, "Relaying to Ivy is on!\n"); + dorelay = 1; + } else { + PrintText (sess, "Relaying to Ivy was already on\n"); + } + return 0; +} + +static int +cmd_off (struct session *sess, char *tbuf, char *word[], char *word_eol[]) +{ + if (dorelay == 1) { + PrintText (sess, "Relaying to Ivy is off!\n"); + dorelay = 0; + } else { + PrintText (sess, "Relaying to Ivy was already off\n"); + } + return 0; +} + +static int +cmd_bind (struct session *sess, char *tbuf, char *word[], char *word_eol[]) +{ + char buf[512]; + char* p; + MsgCallback cb; + const char* action; + + /* determine which command was used, then skip it */ + p = strstr (word[1], "ivyrelay"); + cb = p ? IvyRelayCallback : IvyBindCallback; + action = p ? "Relaying" : "Binding"; + p = p ? p+8 : word[1]+7; + + /* skip white space and detect empty regexps */ + while (*p == ' ') + ++p; + if (*p == '\0') { + PrintText (sess, "Usage : /ivybind <regexp> or /ivyrelay <regexp>\n"); + return 0; + } + + /* bind and provide confirmation */ + IvyBindMsg (cb, sess, p); + sprintf (buf, "%s Ivy regexp %s\n", action, p); + PrintText (sess, buf); + + return 0; +} + + + +/* and finally the module init and cleanup functions */ + +int +module_init (int ver, struct module *mod, struct session *sess) +{ + /** first let's do our connection work as an xchat plugin **/ + + /* version check */ + if (ver != MODULE_IFACE_VER) + return 1; + + /* check whether we are already loaded */ + if (module_find (module_name) != 0) { + PrintText (sess, "Module ivy-xchat-plugin already loaded\n"); + return 1; + } + + /* make an announcement and register our name and description */ + PrintText (sess, "Loaded module ivy-xchat-plugin\n"); + mod->name = module_name; + mod->desc = module_desc; + + /* register our commands */ + cmd_set.mod = mod; + cmd_set.cmds = cmds; + module_add_cmds (&cmd_set); + + + /* and now register our event subscriptions */ + + chanmsg_sig.signal = XP_CHANMSG; + chanmsg_sig.callback = XP_CALLBACK (chanmsg); + chanmsg_sig.naddr = &chanmsg_next; + chanmsg_sig.mod = mod; + hook_signal (&chanmsg_sig); + + /** then let's connect to Ivy **/ + IvyGtkChannelInit (); + IvyInit ("ivy-xchat-plugin", 0, 0, 0, 0, 0); + IvyStart (0); + return 0; +} + +void +module_cleanup (struct module *mod, struct session *sess) +{ + PrintText (sess, "Unloading ivy-xchat-plugin module.\n"); + IvyStop (); +} + |