987
|
1 /*
|
|
2 * gaim - IRC Protocol Plugin
|
|
3 *
|
|
4 * Copyright (C) 2000, Rob Flynn <rob@tgflinux.com>
|
|
5 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
|
|
6 *
|
|
7 * This program is free software; you can redistribute it and/or modify
|
|
8 * it under the terms of the GNU General Public License as published by
|
|
9 * the Free Software Foundation; either version 2 of the License, or
|
|
10 * (at your option) any later version.
|
|
11 *
|
|
12 * This program is distributed in the hope that it will be useful,
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 * GNU General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU General Public License
|
|
18 * along with this program; if not, write to the Free Software
|
|
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 *
|
|
21 */
|
|
22
|
|
23 #ifdef HAVE_CONFIG_H
|
|
24 #include "../config.h"
|
|
25 #endif
|
|
26
|
|
27
|
|
28 #include <netdb.h>
|
|
29 #include <gtk/gtk.h>
|
|
30 #include <unistd.h>
|
|
31 #include <errno.h>
|
|
32 #include <netinet/in.h>
|
|
33 #include <arpa/inet.h>
|
|
34 #include <string.h>
|
|
35 #include <stdlib.h>
|
|
36 #include <stdio.h>
|
|
37 #include <time.h>
|
|
38 #include <sys/socket.h>
|
|
39 #include <sys/stat.h>
|
|
40 #include "multi.h"
|
|
41 #include "prpl.h"
|
|
42 #include "gaim.h"
|
|
43 #include "aim.h"
|
|
44 #include "gnome_applet_mgr.h"
|
|
45
|
|
46 #include "pixmaps/cancel.xpm"
|
|
47 #include "pixmaps/ok.xpm"
|
|
48
|
1011
|
49 /* FIXME: We shouldn't have hard coded servers and ports :-) */
|
|
50 #define IRC_SERVER "irc.mozilla.org"
|
|
51 #define IRC_PORT 6667
|
|
52
|
|
53 #define IRC_BUF_LEN 4096
|
|
54
|
|
55 static int chat_id = 0;
|
|
56
|
|
57 struct irc_channel {
|
|
58 int id;
|
|
59 gchar *name;
|
|
60 };
|
|
61
|
|
62 struct irc_data {
|
|
63 int fd;
|
|
64
|
|
65 GList *channels;
|
|
66 };
|
|
67
|
1008
|
68 static char *irc_name() {
|
|
69 return "IRC";
|
|
70 }
|
|
71
|
|
72 char *name() {
|
|
73 return "IRC";
|
|
74 }
|
|
75
|
|
76 char *description() {
|
|
77 return "Allows gaim to use the IRC protocol";
|
|
78 }
|
|
79
|
1011
|
80 void irc_join_chat( struct gaim_connection *gc, int id, char *name) {
|
|
81 struct irc_data *idata = (struct irc_data *)gc->proto_data;
|
|
82 gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN+1);
|
|
83
|
|
84 g_snprintf(buf, IRC_BUF_LEN, "JOIN %s\n", name);
|
|
85 write(idata->fd, buf, strlen(buf));
|
1008
|
86
|
1011
|
87 g_free(buf);
|
|
88 }
|
|
89
|
|
90 void irc_send_im( struct gaim_connection *gc, char *who, char *message, int away) {
|
|
91
|
|
92 struct irc_data *idata = (struct irc_data *)gc->proto_data;
|
|
93 gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN + 1);
|
|
94
|
|
95 /* Before we actually send this, we should check to see if they're trying
|
|
96 * To issue a /me command and handle it properly. */
|
|
97
|
|
98 if ( (g_strncasecmp(message, "/me ", 4) == 0) && (strlen(message)>4)) {
|
|
99 /* We have /me!! We have /me!! :-) */
|
|
100
|
|
101 gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN+1);
|
|
102 strcpy(temp, message+4);
|
|
103 g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG %s :%cACTION %s%c\n", who, '\001', temp, '\001');
|
|
104 g_free(temp);
|
|
105 }
|
|
106 else
|
|
107 {
|
|
108 g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG %s :%s\n", who, message);
|
|
109 }
|
|
110
|
|
111 write(idata->fd, buf, strlen(buf));
|
|
112
|
|
113 g_free(buf);
|
|
114 }
|
|
115
|
|
116 int find_id_by_name(struct gaim_connection *gc, char *name) {
|
|
117 gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN + 1);
|
|
118 GList *templist;
|
|
119 struct irc_channel *channel;
|
|
120
|
|
121 templist = ((struct irc_data *)gc->proto_data)->channels;
|
|
122
|
|
123 while (templist) {
|
|
124 channel = (struct irc_channel *)templist->data;
|
|
125
|
|
126 g_snprintf(temp, IRC_BUF_LEN, "#%s", channel->name);
|
|
127
|
|
128 if (g_strcasecmp(temp, name) == 0) {
|
|
129 g_free(temp);
|
|
130 return channel->id;
|
|
131 }
|
|
132
|
|
133 templist = templist -> next;
|
|
134 }
|
|
135
|
|
136 g_free(temp);
|
|
137
|
|
138 /* Return -1 if we have no ID */
|
|
139 return -1;
|
|
140 }
|
|
141
|
|
142 struct irc_channel * find_channel_by_name(struct gaim_connection *gc, char *name) {
|
|
143 gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN + 1);
|
|
144 GList *templist;
|
|
145 struct irc_channel *channel;
|
|
146
|
|
147 templist = ((struct irc_data *)gc->proto_data)->channels;
|
|
148
|
|
149 while (templist) {
|
|
150 channel = (struct irc_channel *)templist->data;
|
|
151
|
|
152 g_snprintf(temp, IRC_BUF_LEN, "%s", channel->name);
|
|
153
|
|
154 if (g_strcasecmp(temp, name) == 0) {
|
|
155 g_free(temp);
|
|
156 return channel;
|
|
157 }
|
|
158
|
|
159 templist = templist -> next;
|
|
160 }
|
|
161
|
|
162 g_free(temp);
|
|
163
|
|
164 /* If we found nothing, return nothing :-) */
|
|
165 return NULL;
|
|
166 }
|
|
167
|
|
168 struct irc_channel * find_channel_by_id (struct gaim_connection *gc, int id) {
|
|
169 struct irc_data *idata = (struct irc_data *)gc->proto_data;
|
|
170 struct irc_channel *channel;
|
|
171
|
|
172 GList *temp;
|
|
173
|
|
174 temp = idata->channels;
|
|
175
|
|
176 while (temp) {
|
|
177 channel = (struct irc_channel *)temp->data;
|
|
178
|
|
179 if (channel->id == id) {
|
|
180 /* We've found our man */
|
|
181 return channel;
|
|
182 }
|
|
183
|
|
184 temp = temp->next;
|
|
185 }
|
|
186
|
|
187
|
|
188 /* If we didnt find one, return NULL */
|
|
189 return NULL;
|
|
190 }
|
|
191
|
|
192 void irc_chat_send( struct gaim_connection *gc, int id, char *message) {
|
|
193
|
|
194 struct irc_data *idata = (struct irc_data *)gc->proto_data;
|
|
195 struct irc_channel *channel = g_new0(struct irc_channel, 1);
|
|
196 gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN + 1);
|
|
197
|
|
198 /* First lets get our current channel */
|
|
199 channel = find_channel_by_id(gc, id);
|
|
200
|
|
201
|
|
202 if (!channel) {
|
|
203 /* If for some reason we've lost our channel, let's bolt */
|
|
204 return;
|
|
205 }
|
|
206
|
|
207
|
|
208 /* Before we actually send this, we should check to see if they're trying
|
|
209 * To issue a /me command and handle it properly. */
|
|
210
|
|
211 if ( (g_strncasecmp(message, "/me ", 4) == 0) && (strlen(message)>4)) {
|
|
212 /* We have /me!! We have /me!! :-) */
|
|
213
|
|
214 gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN+1);
|
|
215 strcpy(temp, message+4);
|
|
216 g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG #%s :%cACTION %s%c\n", channel->name, '\001', temp, '\001');
|
|
217 g_free(temp);
|
|
218 }
|
|
219 else
|
|
220 {
|
|
221 g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG #%s :%s\n", channel->name, message);
|
|
222 }
|
|
223
|
|
224 write(idata->fd, buf, strlen(buf));
|
|
225
|
|
226 /* Since AIM expects us to receive the message we send, we gotta fake it */
|
|
227 serv_got_chat_in(gc, id, gc->username, 0, message);
|
|
228
|
|
229 g_free(buf);
|
1008
|
230 }
|
|
231
|
1011
|
232 void irc_callback ( struct gaim_connection * gc ) {
|
|
233
|
|
234 int i = 0;
|
|
235 char c;
|
|
236 gchar buf[4096];
|
|
237 gchar **buf2;
|
|
238 int status;
|
|
239 struct irc_data *idata;
|
|
240
|
|
241 idata = (struct irc_data *)gc->proto_data;
|
|
242
|
|
243 do {
|
|
244 status = recv(idata->fd, &c, 1, 0);
|
|
245
|
|
246 if (!status)
|
|
247 {
|
|
248 exit(1);
|
|
249 }
|
|
250 buf[i] = c;
|
|
251 i++;
|
|
252 } while (c != '\n');
|
|
253
|
|
254 buf[i] = '\0';
|
|
255
|
|
256 /* And remove that damned trailing \n */
|
|
257 g_strchomp(buf);
|
|
258
|
|
259 /* For now, lets display everything to the console too. Im such a bitch */
|
|
260 printf("IRC:'%'s\n", buf);
|
|
261
|
|
262 if ( (strstr(buf, " JOIN ")) && (buf[0] == ':') && (!strstr(buf, " NOTICE "))) {
|
|
263
|
|
264 gchar u_channel[128];
|
1012
|
265 gchar u_nick[128];
|
|
266
|
1011
|
267 struct irc_channel *channel;
|
|
268 int id;
|
|
269 int j;
|
|
270
|
1012
|
271 for (j = 0, i = 1; buf[i] != '!'; j++, i++) {
|
|
272 u_nick[j] = buf[i];
|
|
273 }
|
|
274
|
|
275 u_nick[j] = '\0'; i++;
|
|
276
|
|
277 for (j = 0; buf[i] != '#'; j++, i++) {
|
1011
|
278 }
|
|
279
|
|
280 i++;
|
|
281
|
|
282 strcpy(u_channel, buf+i);
|
|
283
|
|
284 /* Looks like we're going to join the channel for real now. Let's create
|
|
285 * a valid channel structure and add it to our list. Let's make sure that
|
|
286 * we are not already in a channel first */
|
|
287
|
|
288 channel = find_channel_by_name(gc, u_channel);
|
|
289
|
|
290 if (!channel) {
|
|
291 chat_id++;
|
|
292
|
|
293 channel = g_new0(struct irc_channel, 1);
|
|
294
|
|
295 channel->id = chat_id;
|
|
296 channel->name = strdup(u_channel);
|
|
297
|
|
298 idata->channels = g_list_append(idata->channels, channel);
|
|
299
|
|
300 serv_got_joined_chat(gc, chat_id, u_channel);
|
|
301 printf("IIII: I joined '%s' with a strlen() of '%d'\n", u_channel, strlen(u_channel));
|
|
302 } else {
|
|
303 /* Someone else joined. */
|
1012
|
304 printf("%s has joined #%s\n", u_nick, u_channel);
|
1011
|
305 }
|
|
306
|
|
307 return;
|
|
308 }
|
|
309
|
|
310 if ( (strstr(buf, " PART ")) && (buf[0] == ':') && (!strstr(buf, " NOTICE "))) {
|
|
311
|
|
312 gchar u_channel[128];
|
|
313 gchar u_nick[128];
|
|
314
|
|
315 struct irc_channel *channel = g_new0(struct irc_channel, 1);
|
|
316 int id;
|
|
317 int j;
|
|
318 GList *test = NULL;
|
|
319
|
|
320 for (j = 0, i = 1; buf[i] != '!'; j++, i++) {
|
|
321 u_nick[j] = buf[i];
|
|
322 }
|
|
323 u_nick[j] = '\0';
|
|
324
|
|
325 i++;
|
|
326
|
|
327 for (j = 0; buf[i] != '#'; j++, i++) {
|
|
328 }
|
|
329
|
|
330 i++;
|
|
331
|
|
332 strcpy(u_channel, buf+i);
|
|
333
|
|
334
|
|
335 /* Now, lets check to see if it was US that was leaving. If so, do the
|
|
336 * correct thing by closing up all of our old channel stuff. Otherwise,
|
|
337 * we should just print that someone left */
|
|
338
|
|
339 if (g_strcasecmp(u_nick, gc->username) == 0) {
|
|
340
|
|
341 /* Looks like we're going to join the channel for real now. Let's create
|
|
342 * a valid channel structure and add it to our list */
|
|
343
|
|
344 channel = find_channel_by_name(gc, u_channel);
|
|
345
|
|
346 if (!channel) {
|
|
347 return;
|
|
348 }
|
|
349
|
|
350 serv_got_chat_left(gc, channel->id);
|
|
351
|
|
352 idata->channels = g_list_remove(idata->channels, channel);
|
|
353 g_free(channel);
|
|
354 return;
|
|
355 }
|
|
356
|
|
357 /* Otherwise, lets just say someone left */
|
|
358 printf("%s has left #%s\n", u_nick, u_channel);
|
|
359
|
|
360 return;
|
|
361 }
|
|
362
|
1012
|
363 if ( (strstr(buf, " PRIVMSG ")) && (buf[0] == ':')) {
|
1011
|
364 gchar u_nick[128];
|
|
365 gchar u_host[255];
|
|
366 gchar u_command[32];
|
|
367 gchar u_channel[128];
|
|
368 gchar u_message[IRC_BUF_LEN];
|
|
369 int j;
|
|
370 int msgcode = 0;
|
|
371
|
|
372 for (j = 0, i = 1; buf[i] != '!'; j++, i++) {
|
|
373 u_nick[j] = buf[i];
|
|
374 }
|
|
375
|
|
376 u_nick[j] = '\0'; i++;
|
|
377
|
|
378 for (j = 0; buf[i] != ' '; j++, i++) {
|
|
379 u_host[j] = buf[i];
|
|
380 }
|
|
381
|
|
382 u_host[j] = '\0'; i++;
|
|
383
|
|
384 for (j = 0; buf[i] != ' '; j++, i++) {
|
|
385 u_command[j] = buf[i];
|
|
386 }
|
|
387
|
|
388 u_command[j] = '\0'; i++;
|
|
389
|
|
390 for (j = 0; buf[i] != ':'; j++, i++) {
|
|
391 u_channel[j] = buf[i];
|
|
392 }
|
|
393
|
|
394 u_channel[j-1] = '\0'; i++;
|
|
395
|
|
396
|
|
397 /* Now that everything is parsed, the rest of this baby must be our message */
|
|
398 strncpy(u_message, buf + i, IRC_BUF_LEN);
|
|
399
|
|
400 /* Now, lets check the message to see if there's anything special in it */
|
|
401 if (u_message[0] == '\001') {
|
|
402 if (g_strncasecmp(u_message, "\001ACTION ", 8) == 0) {
|
|
403 /* Looks like we have an action. Let's parse it a little */
|
|
404 strcpy(buf, u_message);
|
|
405
|
|
406 strcpy(u_message, "/me ");
|
|
407 for (j = 4, i = 8; buf[i] != '\001'; i++, j++) {
|
|
408 u_message[j] = buf[i];
|
|
409 }
|
|
410 u_message[j] = '\0';
|
|
411 }
|
|
412 }
|
|
413
|
|
414
|
|
415 /* Let's check to see if we have a channel on our hands */
|
|
416 if (u_channel[0] == '#') {
|
|
417 /* Yup. We have a channel */
|
|
418 int id;
|
|
419
|
|
420 id = find_id_by_name(gc, u_channel);
|
|
421 if (id != -1) {
|
|
422 serv_got_chat_in(gc, id, u_nick, 0, u_message);
|
|
423 }
|
|
424 }
|
|
425 else {
|
|
426 /* Nope. Let's treat it as a private message */
|
|
427 serv_got_im(gc, u_nick, u_message, 0);
|
|
428 }
|
|
429
|
|
430 return;
|
|
431 }
|
|
432
|
|
433 /* Let's parse PING requests so that we wont get booted for inactivity */
|
|
434
|
|
435 if (strncmp(buf, "PING :", 6) == 0) {
|
|
436 buf2 = g_strsplit(buf, ":", 1);
|
|
437
|
|
438 /* Let's build a new response */
|
|
439 g_snprintf(buf, IRC_BUF_LEN, "PONG :%s\n", buf2[1]);
|
|
440 write(idata->fd, buf, strlen(buf));
|
|
441
|
|
442 /* And clean up after ourselves */
|
|
443 g_strfreev(buf2);
|
|
444
|
|
445 return;
|
|
446 }
|
|
447
|
|
448 }
|
|
449
|
|
450 void irc_handler(gpointer data, gint source, GdkInputCondition condition) {
|
|
451 irc_callback(data);
|
|
452 }
|
|
453
|
|
454 void irc_close(struct gaim_connection *gc) {
|
|
455 struct irc_data *idata = (struct irc_data *)gc->proto_data;
|
|
456 gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN);
|
|
457
|
|
458 g_snprintf(buf, IRC_BUF_LEN, "QUIT :GAIM [www.marko.net/gaim]\n");
|
|
459 write(idata->fd, buf, strlen(buf));
|
|
460
|
|
461 g_free(buf);
|
|
462 close(idata->fd);
|
|
463 g_free(gc->proto_data);
|
|
464 }
|
|
465
|
|
466 void irc_chat_leave(struct gaim_connection *gc, int id) {
|
|
467 struct irc_data *idata = (struct irc_data *)gc->proto_data;
|
|
468 struct irc_channel *channel;
|
|
469 gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN+1);
|
|
470
|
|
471 channel = find_channel_by_id(gc, id);
|
|
472
|
|
473 if (!channel) {
|
|
474 return;
|
|
475 }
|
|
476
|
|
477 g_snprintf(buf, IRC_BUF_LEN, "PART #%s\n", channel->name);
|
|
478 write(idata->fd, buf, strlen(buf));
|
|
479
|
|
480 g_free(buf);
|
|
481 }
|
|
482
|
|
483 void irc_login(struct aim_user *user) {
|
|
484 int fd;
|
|
485 struct hostent *host;
|
|
486 struct sockaddr_in site;
|
|
487 char buf[4096];
|
|
488
|
|
489 struct gaim_connection *gc = new_gaim_conn(PROTO_IRC, user->username, user->password);
|
|
490 struct irc_data *idata = gc->proto_data = g_new0(struct irc_data, 1);
|
|
491 char c;
|
|
492 int i;
|
|
493 int status;
|
|
494
|
|
495 set_login_progress(gc, 1, buf);
|
|
496
|
|
497 while (gtk_events_pending())
|
|
498 gtk_main_iteration();
|
|
499
|
|
500 host = gethostbyname(IRC_SERVER);
|
|
501 if (!host) {
|
|
502 hide_login_progress(gc, "Unable to resolve hostname");
|
|
503 destroy_gaim_conn(gc);
|
|
504 return;
|
|
505 }
|
|
506
|
|
507 site.sin_family = AF_INET;
|
|
508 site.sin_addr.s_addr = *(long *)(host->h_addr);
|
|
509 site.sin_port = htons(IRC_PORT);
|
|
510
|
|
511 fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
512 if (fd < 0) {
|
|
513 hide_login_progress(gc, "Unable to create socket");
|
|
514 destroy_gaim_conn(gc);
|
|
515 return;
|
|
516 }
|
|
517
|
|
518 if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) {
|
|
519 hide_login_progress(gc, "Unable to connect.");
|
|
520 destroy_gaim_conn(gc);
|
|
521 return;
|
|
522 }
|
|
523
|
|
524 idata->fd = fd;
|
|
525
|
|
526 g_snprintf(buf, sizeof(buf), "Signon: %s", gc->username);
|
|
527 set_login_progress(gc, 2, buf);
|
|
528
|
|
529 /* This is where we will attempt to sign on */
|
|
530
|
|
531 /* FIXME: This should be their servername, not their username. im just lazy right now */
|
|
532
|
|
533 g_snprintf(buf, 4096, "NICK %s\nUSER %s localhost %s :GAIM (www.marko.net/gaim)\n", gc->username, gc->username, gc->username);
|
|
534 write(idata->fd, buf, strlen(buf));
|
|
535
|
|
536
|
|
537 /* Now lets sign ourselves on */
|
|
538 account_online(gc);
|
|
539
|
|
540 if (mainwindow)
|
|
541 gtk_widget_hide(mainwindow);
|
|
542
|
|
543 show_buddy_list();
|
|
544 refresh_buddy_window();
|
|
545
|
|
546 serv_finish_login(gc);
|
|
547 gaim_setup(gc);
|
|
548
|
|
549 gc->inpa = gdk_input_add(idata->fd, GDK_INPUT_READ, irc_handler, gc);
|
|
550 }
|
1008
|
551
|
987
|
552 struct prpl *irc_init() {
|
|
553 struct prpl *ret = g_new0(struct prpl, 1);
|
|
554
|
1008
|
555 ret->protocol = PROTO_IRC;
|
|
556 ret->name = irc_name;
|
|
557 ret->login = irc_login;
|
1011
|
558 ret->close = irc_close;
|
|
559 ret->send_im = irc_send_im;
|
987
|
560 ret->set_info = NULL;
|
|
561 ret->get_info = NULL;
|
|
562 ret->set_away = NULL;
|
|
563 ret->get_away_msg = NULL;
|
|
564 ret->set_dir = NULL;
|
|
565 ret->get_dir = NULL;
|
|
566 ret->dir_search = NULL;
|
|
567 ret->set_idle = NULL;
|
|
568 ret->change_passwd = NULL;
|
|
569 ret->add_buddy = NULL;
|
|
570 ret->add_buddies = NULL;
|
|
571 ret->remove_buddy = NULL;
|
|
572 ret->add_permit = NULL;
|
|
573 ret->add_deny = NULL;
|
|
574 ret->warn = NULL;
|
|
575 ret->accept_chat = NULL;
|
1011
|
576 ret->join_chat = irc_join_chat;
|
987
|
577 ret->chat_invite = NULL;
|
1011
|
578 ret->chat_leave = irc_chat_leave;
|
987
|
579 ret->chat_whisper = NULL;
|
1011
|
580 ret->chat_send = irc_chat_send;
|
987
|
581 ret->keepalive = NULL;
|
|
582
|
|
583 return ret;
|
|
584 }
|
|
585
|
|
586 int gaim_plugin_init(void *handle) {
|
|
587 protocols = g_slist_append(protocols, irc_init());
|
|
588 return 0;
|
|
589 }
|