aboutsummaryrefslogtreecommitdiff
path: root/src/ivy-xchat-plugin.c
blob: da27fdff298c00d29a3fb311e5a6c824307ff7f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 *	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"
#include "cfgfiles.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)
{
	FILE* conf;
	char optionfile[256];
	char* bus = 0;
	char busbuf[512];

	/** 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 **/

	/* read bus configuration file */
	snprintf (optionfile, sizeof (optionfile), "%s/ivy.conf", get_xdir ());
	conf = fopen (optionfile, "r");
	if (conf) {
		if (fscanf (conf, "bus = %512s", busbuf) == 1)
			bus = busbuf;
	}

	IvyGtkChannelInit ();
	IvyInit ("ivy-xchat-plugin", 0, 0, 0, 0, 0);
	IvyStart (bus);
	return 0;
}

void
module_cleanup (struct module *mod, struct session *sess)
{
	PrintText (sess, "Unloading ivy-xchat-plugin module.\n");
	IvyStop ();
}