Thu, 26 Oct 2000 07:22:32 +0000
[gaim-migrate @ 1036]
X-Idle support. Thanks bryner and bmiller!
Both of you guys sent me similar patches :)
committer: Rob Flynn <gaim@robflynn.com>
| 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 :-) */ |
| 1021 | 50 | #define IRC_SERVER "irc.mozilla.org" |
| 1011 | 51 | #define IRC_PORT 6667 |
| 52 | ||
| 53 | #define IRC_BUF_LEN 4096 | |
| 54 | ||
| 1022 | 55 | |
| 1011 | 56 | static int chat_id = 0; |
| 57 | ||
| 58 | struct irc_channel { | |
| 59 | int id; | |
| 60 | gchar *name; | |
| 61 | }; | |
| 62 | ||
| 63 | struct irc_data { | |
| 64 | int fd; | |
| 65 | ||
| 1022 | 66 | int timer; |
| 67 | ||
| 68 | int totalblocks; | |
| 69 | int recblocks; | |
| 70 | ||
| 71 | GSList *templist; | |
| 1011 | 72 | GList *channels; |
| 73 | }; | |
| 74 | ||
| 1008 | 75 | static char *irc_name() { |
| 76 | return "IRC"; | |
| 77 | } | |
| 78 | ||
| 79 | char *name() { | |
| 80 | return "IRC"; | |
| 81 | } | |
| 82 | ||
| 83 | char *description() { | |
| 84 | return "Allows gaim to use the IRC protocol"; | |
| 85 | } | |
| 86 | ||
| 1011 | 87 | void irc_join_chat( struct gaim_connection *gc, int id, char *name) { |
| 88 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 89 | gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN+1); | |
| 90 | ||
| 91 | g_snprintf(buf, IRC_BUF_LEN, "JOIN %s\n", name); | |
| 92 | write(idata->fd, buf, strlen(buf)); | |
| 1008 | 93 | |
| 1011 | 94 | g_free(buf); |
| 95 | } | |
| 96 | ||
| 1022 | 97 | void irc_update_user (struct gaim_connection *gc, char *name, int status) { |
| 98 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 99 | struct irc_channel *u; | |
| 100 | GSList *temp = idata->templist; | |
| 101 | ||
| 102 | /* Loop through our list */ | |
| 103 | ||
| 104 | while (temp) { | |
| 105 | u = (struct irc_channel *)temp->data; | |
| 106 | if (g_strcasecmp(u->name, name) == 0) { | |
| 107 | u->id = status; | |
| 108 | return; | |
| 109 | } | |
| 110 | ||
| 111 | temp = g_slist_next(temp); | |
| 112 | } | |
| 113 | return; | |
| 114 | } | |
| 115 | ||
| 116 | void irc_request_buddy_update ( struct gaim_connection *gc ) { | |
| 117 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 118 | GSList *grp = groups; | |
| 119 | GList *person; | |
| 120 | struct group *g; | |
| 121 | struct buddy *b; | |
| 122 | struct irc_channel *u; | |
| 123 | gchar buf[IRC_BUF_LEN+1]; | |
| 124 | ||
| 125 | if (idata->templist != NULL) | |
| 126 | return; | |
| 127 | ||
| 128 | idata->recblocks = 0; | |
| 129 | idata->totalblocks = 1; | |
| 130 | ||
| 131 | /* Send the first part of our request */ | |
| 132 | write(idata->fd, "ISON", 4); | |
| 133 | ||
| 134 | /* Step through our list of groups */ | |
| 135 | while (grp) { | |
| 136 | ||
| 137 | g = (struct group *)grp->data; | |
| 138 | person = g->members; | |
| 139 | ||
| 140 | while (person) { | |
| 141 | b = (struct buddy *)person->data; | |
| 142 | ||
| 143 | /* We will store our buddy info here. I know, this is cheap | |
| 144 | * but hey, its the exact same data structure. Why should we | |
| 145 | * bother with making another one */ | |
| 146 | ||
| 147 | u = g_new0(struct irc_channel, 1); | |
| 148 | u->id = 0; /* Assume by default that they're offline */ | |
| 149 | u->name = strdup(b->name); | |
| 150 | ||
| 151 | write(idata->fd, " ", 1); | |
| 152 | write(idata->fd, u->name, strlen(u->name)); | |
| 153 | idata->templist = g_slist_append(idata->templist, u); | |
| 154 | ||
| 155 | person = person->next; | |
| 156 | } | |
| 157 | ||
| 158 | grp = g_slist_next(grp); | |
| 159 | } | |
| 160 | write(idata->fd, "\n", 1); | |
| 161 | } | |
| 162 | ||
| 163 | ||
| 1011 | 164 | void irc_send_im( struct gaim_connection *gc, char *who, char *message, int away) { |
| 165 | ||
| 166 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 167 | gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN + 1); | |
| 168 | ||
| 169 | /* Before we actually send this, we should check to see if they're trying | |
| 170 | * To issue a /me command and handle it properly. */ | |
| 171 | ||
| 172 | if ( (g_strncasecmp(message, "/me ", 4) == 0) && (strlen(message)>4)) { | |
| 173 | /* We have /me!! We have /me!! :-) */ | |
| 174 | ||
| 175 | gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN+1); | |
| 176 | strcpy(temp, message+4); | |
| 177 | g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG %s :%cACTION %s%c\n", who, '\001', temp, '\001'); | |
| 178 | g_free(temp); | |
| 179 | } | |
| 180 | else | |
| 181 | { | |
| 182 | g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG %s :%s\n", who, message); | |
| 183 | } | |
| 184 | ||
| 185 | write(idata->fd, buf, strlen(buf)); | |
| 186 | ||
| 187 | g_free(buf); | |
| 188 | } | |
| 189 | ||
| 190 | int find_id_by_name(struct gaim_connection *gc, char *name) { | |
| 191 | gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN + 1); | |
| 192 | GList *templist; | |
| 193 | struct irc_channel *channel; | |
| 194 | ||
| 195 | templist = ((struct irc_data *)gc->proto_data)->channels; | |
| 196 | ||
| 197 | while (templist) { | |
| 198 | channel = (struct irc_channel *)templist->data; | |
| 199 | ||
| 200 | g_snprintf(temp, IRC_BUF_LEN, "#%s", channel->name); | |
| 201 | ||
| 202 | if (g_strcasecmp(temp, name) == 0) { | |
| 203 | g_free(temp); | |
| 204 | return channel->id; | |
| 205 | } | |
| 206 | ||
| 207 | templist = templist -> next; | |
| 208 | } | |
| 209 | ||
| 210 | g_free(temp); | |
| 211 | ||
| 212 | /* Return -1 if we have no ID */ | |
| 213 | return -1; | |
| 214 | } | |
| 215 | ||
| 216 | struct irc_channel * find_channel_by_name(struct gaim_connection *gc, char *name) { | |
| 217 | gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN + 1); | |
| 218 | GList *templist; | |
| 219 | struct irc_channel *channel; | |
| 220 | ||
| 221 | templist = ((struct irc_data *)gc->proto_data)->channels; | |
| 222 | ||
| 223 | while (templist) { | |
| 224 | channel = (struct irc_channel *)templist->data; | |
| 225 | ||
| 226 | g_snprintf(temp, IRC_BUF_LEN, "%s", channel->name); | |
| 227 | ||
| 228 | if (g_strcasecmp(temp, name) == 0) { | |
| 229 | g_free(temp); | |
| 230 | return channel; | |
| 231 | } | |
| 232 | ||
| 233 | templist = templist -> next; | |
| 234 | } | |
| 235 | ||
| 236 | g_free(temp); | |
| 237 | ||
| 238 | /* If we found nothing, return nothing :-) */ | |
| 239 | return NULL; | |
| 240 | } | |
| 241 | ||
| 242 | struct irc_channel * find_channel_by_id (struct gaim_connection *gc, int id) { | |
| 243 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 244 | struct irc_channel *channel; | |
| 245 | ||
| 246 | GList *temp; | |
| 247 | ||
| 248 | temp = idata->channels; | |
| 249 | ||
| 250 | while (temp) { | |
| 251 | channel = (struct irc_channel *)temp->data; | |
| 252 | ||
| 253 | if (channel->id == id) { | |
| 254 | /* We've found our man */ | |
| 255 | return channel; | |
| 256 | } | |
| 257 | ||
| 258 | temp = temp->next; | |
| 259 | } | |
| 260 | ||
| 261 | ||
| 262 | /* If we didnt find one, return NULL */ | |
| 263 | return NULL; | |
| 264 | } | |
| 265 | ||
| 266 | void irc_chat_send( struct gaim_connection *gc, int id, char *message) { | |
| 267 | ||
| 268 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 1021 | 269 | struct irc_channel *channel = NULL; |
| 1011 | 270 | gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN + 1); |
| 271 | ||
| 272 | /* First lets get our current channel */ | |
| 273 | channel = find_channel_by_id(gc, id); | |
| 274 | ||
| 275 | ||
| 276 | if (!channel) { | |
| 277 | /* If for some reason we've lost our channel, let's bolt */ | |
| 1021 | 278 | g_free(buf); |
| 1011 | 279 | return; |
| 280 | } | |
| 281 | ||
| 282 | ||
| 283 | /* Before we actually send this, we should check to see if they're trying | |
| 284 | * To issue a /me command and handle it properly. */ | |
| 285 | ||
| 286 | if ( (g_strncasecmp(message, "/me ", 4) == 0) && (strlen(message)>4)) { | |
| 287 | /* We have /me!! We have /me!! :-) */ | |
| 288 | ||
| 289 | gchar *temp = (gchar *)g_malloc(IRC_BUF_LEN+1); | |
| 290 | strcpy(temp, message+4); | |
| 291 | g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG #%s :%cACTION %s%c\n", channel->name, '\001', temp, '\001'); | |
| 292 | g_free(temp); | |
| 293 | } | |
| 294 | else | |
| 295 | { | |
| 296 | g_snprintf(buf, IRC_BUF_LEN, "PRIVMSG #%s :%s\n", channel->name, message); | |
| 297 | } | |
| 298 | ||
| 299 | write(idata->fd, buf, strlen(buf)); | |
| 300 | ||
| 301 | /* Since AIM expects us to receive the message we send, we gotta fake it */ | |
| 302 | serv_got_chat_in(gc, id, gc->username, 0, message); | |
| 303 | ||
| 304 | g_free(buf); | |
| 1008 | 305 | } |
| 306 | ||
| 1014 | 307 | struct conversation * find_conversation_by_id( struct gaim_connection * gc, int id) { |
| 308 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 309 | GSList *bc = gc->buddy_chats; | |
| 310 | struct conversation *b = NULL; | |
| 311 | ||
| 312 | while (bc) { | |
| 313 | b = (struct conversation *)bc->data; | |
| 314 | if (id == b->id) { | |
| 315 | break; | |
| 316 | } | |
| 317 | bc = bc->next; | |
| 318 | b = NULL; | |
| 319 | } | |
| 320 | ||
| 321 | if (!b) { | |
| 322 | return NULL; | |
| 323 | } | |
| 324 | ||
| 325 | return b; | |
| 326 | } | |
| 327 | ||
| 328 | struct conversation * find_conversation_by_name( struct gaim_connection * gc, char *name) { | |
| 329 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 330 | GSList *bc = gc->buddy_chats; | |
| 331 | struct conversation *b = NULL; | |
| 332 | ||
| 333 | while (bc) { | |
| 334 | b = (struct conversation *)bc->data; | |
| 335 | ||
| 336 | if (g_strcasecmp(name, b->name) == 0) { | |
| 337 | break; | |
| 338 | } | |
| 339 | bc = bc->next; | |
| 340 | b = NULL; | |
| 341 | } | |
| 342 | ||
| 343 | if (!b) { | |
| 344 | return NULL; | |
| 345 | } | |
| 346 | ||
| 347 | return b; | |
| 348 | } | |
| 349 | ||
| 350 | ||
| 351 | ||
| 1011 | 352 | void irc_callback ( struct gaim_connection * gc ) { |
| 353 | ||
| 354 | int i = 0; | |
| 355 | char c; | |
| 356 | gchar buf[4096]; | |
| 357 | gchar **buf2; | |
| 358 | int status; | |
| 359 | struct irc_data *idata; | |
| 360 | ||
| 361 | idata = (struct irc_data *)gc->proto_data; | |
| 362 | ||
| 363 | do { | |
| 364 | status = recv(idata->fd, &c, 1, 0); | |
| 365 | ||
| 366 | if (!status) | |
| 367 | { | |
| 368 | exit(1); | |
| 369 | } | |
| 370 | buf[i] = c; | |
| 371 | i++; | |
| 372 | } while (c != '\n'); | |
| 373 | ||
| 374 | buf[i] = '\0'; | |
| 375 | ||
| 376 | /* And remove that damned trailing \n */ | |
| 377 | g_strchomp(buf); | |
| 378 | ||
| 1014 | 379 | /* For now, lets display everything to the console too. Im such |
| 380 | * a bitch */ | |
| 1011 | 381 | printf("IRC:'%'s\n", buf); |
| 382 | ||
| 1014 | 383 | |
| 384 | /* Parse the list of names that we receive when we first sign on to | |
| 385 | * a channel */ | |
| 386 | ||
| 387 | if (((strstr(buf, " 353 ")) && (!strstr(buf, "PRIVMSG")) && | |
| 388 | (!strstr(buf, "NOTICE")))) { | |
| 389 | gchar u_host[255]; | |
| 390 | gchar u_command[32]; | |
| 391 | gchar u_channel[128]; | |
| 392 | gchar u_names[IRC_BUF_LEN + 1]; | |
| 393 | struct conversation *convo = NULL; | |
| 394 | int j; | |
| 395 | ||
| 396 | for (j = 0, i = 0; buf[i] != ' '; j++, i++) { | |
| 397 | u_host[j] = buf[i]; | |
| 398 | } | |
| 399 | ||
| 400 | u_host[j] = '\0'; i++; | |
| 401 | ||
| 402 | for (j = 0; buf[i] != ' '; j++, i++) { | |
| 403 | u_command[j] = buf[i]; | |
| 404 | } | |
| 405 | ||
| 406 | u_command[j] = '\0'; i++; | |
| 407 | ||
| 408 | for (j = 0; buf[i] != '#'; j++, i++) { | |
| 409 | } | |
| 410 | i++; | |
| 411 | ||
| 412 | for (j = 0; buf[i] != ':'; j++, i++) { | |
| 413 | u_channel[j] = buf[i]; | |
| 414 | } | |
| 415 | ||
| 416 | u_channel[j-1] = '\0'; i++; | |
| 417 | ||
| 418 | while ((buf[i] == ' ') || (buf[i] == ':')) { | |
| 419 | i++; | |
| 420 | } | |
| 421 | ||
| 422 | strcpy(u_names, buf + i); | |
| 423 | ||
| 424 | buf2 = g_strsplit(u_names, " ", 0); | |
| 425 | ||
| 426 | /* Let's get our conversation window */ | |
| 427 | convo = find_conversation_by_name(gc, u_channel); | |
| 428 | ||
| 429 | if (!convo) { | |
| 430 | return; | |
| 431 | } | |
| 432 | ||
| 433 | /* Now that we've parsed the hell out of this big | |
| 434 | * mess, let's try to split up the names properly */ | |
| 435 | ||
| 436 | for (i = 0; buf2[i] != NULL; i++) { | |
| 437 | /* We shouldnt play with ourselves */ | |
| 438 | if (g_strcasecmp(buf2[i], gc->username) != 0) { | |
| 439 | /* Add the person to the list */ | |
| 440 | add_chat_buddy(convo, buf2[i]); | |
| 441 | } | |
| 442 | } | |
| 1021 | 443 | |
| 444 | /* And free our pointers */ | |
| 445 | g_strfreev (buf2); | |
| 1014 | 446 | |
| 447 | return; | |
| 448 | ||
| 449 | } | |
| 450 | ||
| 1022 | 451 | /* Receive a list of users that are currently online */ |
| 452 | ||
| 453 | if (((strstr(buf, " 303 ")) && (!strstr(buf, "PRIVMSG")) && | |
| 454 | (!strstr(buf, "NOTICE")))) { | |
| 455 | gchar u_host[255]; | |
| 456 | gchar u_command[32]; | |
| 457 | gchar u_names[IRC_BUF_LEN + 1]; | |
| 458 | int j; | |
| 459 | ||
| 460 | for (j = 0, i = 0; buf[i] != ' '; j++, i++) { | |
| 461 | u_host[j] = buf[i]; | |
| 462 | } | |
| 463 | ||
| 464 | u_host[j] = '\0'; i++; | |
| 465 | ||
| 466 | for (j = 0; buf[i] != ' '; j++, i++) { | |
| 467 | u_command[j] = buf[i]; | |
| 468 | } | |
| 469 | ||
| 470 | u_command[j] = '\0'; i++; | |
| 471 | ||
| 472 | for (j = 0; buf[i] != ':'; j++, i++) { | |
| 473 | /* My Nick */ | |
| 474 | } | |
| 475 | i++; | |
| 476 | ||
| 477 | strcpy(u_names, buf + i); | |
| 478 | ||
| 479 | buf2 = g_strsplit(u_names, " ", 0); | |
| 480 | ||
| 481 | /* Now that we've parsed the hell out of this big | |
| 482 | * mess, let's try to split up the names properly */ | |
| 483 | ||
| 484 | for (i = 0; buf2[i] != NULL; i++) { | |
| 485 | /* If we have a name here then our buddy is online. We should | |
| 486 | * update our temporary gslist accordingly. When we achieve our maximum | |
| 487 | * list of names then we should force an update */ | |
| 488 | ||
| 489 | irc_update_user(gc, buf2[i], 1); | |
| 490 | } | |
| 491 | ||
| 492 | /* Increase our received blocks counter */ | |
| 493 | idata->recblocks++; | |
| 494 | ||
| 495 | /* If we have our total number of blocks */ | |
| 496 | if (idata->recblocks == idata->totalblocks) { | |
| 497 | GSList *temp; | |
| 498 | struct irc_channel *u; | |
| 499 | ||
| 500 | /* Let's grab our list of people and bring them all on or off line */ | |
| 501 | temp = idata->templist; | |
| 502 | ||
| 503 | /* Loop */ | |
| 504 | while (temp) { | |
| 505 | ||
| 506 | u = temp->data; | |
| 507 | ||
| 508 | /* Tell Gaim to bring the person on or off line */ | |
| 509 | serv_got_update(u->name, u->id, 0, 0, 0, 0, 0); | |
| 510 | ||
| 511 | /* Grab the next entry */ | |
| 512 | temp = g_slist_next(temp); | |
| 513 | } | |
| 514 | ||
| 515 | /* And now, let's delete all of our entries */ | |
| 516 | temp = idata->templist; | |
| 517 | while (temp) { | |
| 518 | u = temp->data; | |
| 519 | g_free(u->name); | |
| 520 | temp = g_slist_remove(temp, u); | |
| 521 | } | |
| 522 | ||
| 523 | /* Reset our list */ | |
| 524 | idata->totalblocks = 0; | |
| 525 | idata->recblocks = 0; | |
| 526 | ||
| 527 | idata->templist = NULL; | |
| 528 | ||
| 529 | return; | |
| 530 | } | |
| 531 | ||
| 532 | /* And free our pointers */ | |
| 533 | g_strfreev (buf2); | |
| 534 | ||
| 535 | return; | |
| 536 | ||
| 537 | } | |
| 538 | ||
| 1014 | 539 | |
| 1011 | 540 | if ( (strstr(buf, " JOIN ")) && (buf[0] == ':') && (!strstr(buf, " NOTICE "))) { |
| 541 | ||
| 542 | gchar u_channel[128]; | |
| 1012 | 543 | gchar u_nick[128]; |
| 544 | ||
| 1011 | 545 | struct irc_channel *channel; |
| 546 | int id; | |
| 547 | int j; | |
| 548 | ||
| 1012 | 549 | for (j = 0, i = 1; buf[i] != '!'; j++, i++) { |
| 550 | u_nick[j] = buf[i]; | |
| 551 | } | |
| 552 | ||
| 553 | u_nick[j] = '\0'; i++; | |
| 554 | ||
| 555 | for (j = 0; buf[i] != '#'; j++, i++) { | |
| 1011 | 556 | } |
| 557 | ||
| 558 | i++; | |
| 559 | ||
| 560 | strcpy(u_channel, buf+i); | |
| 561 | ||
| 1014 | 562 | /* Looks like we're going to join the channel for real |
| 563 | * now. Let's create a valid channel structure and add | |
| 564 | * it to our list. Let's make sure that | |
| 1011 | 565 | * we are not already in a channel first */ |
| 566 | ||
| 567 | channel = find_channel_by_name(gc, u_channel); | |
| 568 | ||
| 569 | if (!channel) { | |
| 570 | chat_id++; | |
| 571 | ||
| 572 | channel = g_new0(struct irc_channel, 1); | |
| 573 | ||
| 574 | channel->id = chat_id; | |
| 575 | channel->name = strdup(u_channel); | |
| 576 | ||
| 577 | idata->channels = g_list_append(idata->channels, channel); | |
| 578 | ||
| 579 | serv_got_joined_chat(gc, chat_id, u_channel); | |
| 580 | } else { | |
| 1014 | 581 | struct conversation *convo = NULL; |
| 582 | ||
| 583 | /* Someone else joined. Find their conversation | |
| 584 | * window */ | |
| 585 | convo = find_conversation_by_id(gc, channel->id); | |
| 586 | ||
| 587 | /* And add their name to it */ | |
| 588 | add_chat_buddy(convo, u_nick); | |
| 589 | ||
| 1011 | 590 | } |
| 591 | ||
| 592 | return; | |
| 593 | } | |
| 594 | ||
| 595 | if ( (strstr(buf, " PART ")) && (buf[0] == ':') && (!strstr(buf, " NOTICE "))) { | |
| 596 | ||
| 597 | gchar u_channel[128]; | |
| 598 | gchar u_nick[128]; | |
| 599 | ||
| 1021 | 600 | struct irc_channel *channel; |
| 1011 | 601 | int id; |
| 602 | int j; | |
| 603 | GList *test = NULL; | |
| 604 | ||
| 605 | for (j = 0, i = 1; buf[i] != '!'; j++, i++) { | |
| 606 | u_nick[j] = buf[i]; | |
| 607 | } | |
| 608 | u_nick[j] = '\0'; | |
| 609 | ||
| 610 | i++; | |
| 611 | ||
| 612 | for (j = 0; buf[i] != '#'; j++, i++) { | |
| 613 | } | |
| 614 | ||
| 615 | i++; | |
| 616 | ||
| 617 | strcpy(u_channel, buf+i); | |
| 618 | ||
| 619 | ||
| 1014 | 620 | /* Now, lets check to see if it was US that was leaving. |
| 621 | * If so, do the correct thing by closing up all of our | |
| 622 | * old channel stuff. Otherwise, | |
| 1011 | 623 | * we should just print that someone left */ |
| 624 | ||
| 1014 | 625 | channel = find_channel_by_name(gc, u_channel); |
| 626 | ||
| 627 | if (!channel) { | |
| 628 | return; | |
| 629 | } | |
| 630 | ||
| 1011 | 631 | if (g_strcasecmp(u_nick, gc->username) == 0) { |
| 632 | ||
| 1014 | 633 | /* Looks like we're going to leave the channel for |
| 634 | * real now. Let's create a valid channel structure | |
| 635 | * and add it to our list */ | |
| 1011 | 636 | |
| 637 | serv_got_chat_left(gc, channel->id); | |
| 638 | ||
| 639 | idata->channels = g_list_remove(idata->channels, channel); | |
| 1014 | 640 | } else { |
| 641 | struct conversation *convo = NULL; | |
| 642 | ||
| 643 | /* Find their conversation window */ | |
| 644 | convo = find_conversation_by_id(gc, channel->id); | |
| 645 | ||
| 646 | if (!convo) { | |
| 647 | /* Some how the window doesn't exist. | |
| 648 | * Let's get out of here */ | |
| 649 | return ; | |
| 650 | } | |
| 651 | ||
| 652 | /* And remove their name */ | |
| 653 | remove_chat_buddy(convo, u_nick); | |
| 654 | ||
| 1011 | 655 | } |
| 656 | ||
| 1014 | 657 | /* Go Home! */ |
| 1011 | 658 | return; |
| 659 | } | |
| 660 | ||
| 1012 | 661 | if ( (strstr(buf, " PRIVMSG ")) && (buf[0] == ':')) { |
| 1011 | 662 | gchar u_nick[128]; |
| 663 | gchar u_host[255]; | |
| 664 | gchar u_command[32]; | |
| 665 | gchar u_channel[128]; | |
| 666 | gchar u_message[IRC_BUF_LEN]; | |
| 667 | int j; | |
| 668 | int msgcode = 0; | |
| 669 | ||
| 670 | for (j = 0, i = 1; buf[i] != '!'; j++, i++) { | |
| 671 | u_nick[j] = buf[i]; | |
| 672 | } | |
| 673 | ||
| 674 | u_nick[j] = '\0'; i++; | |
| 675 | ||
| 676 | for (j = 0; buf[i] != ' '; j++, i++) { | |
| 677 | u_host[j] = buf[i]; | |
| 678 | } | |
| 679 | ||
| 680 | u_host[j] = '\0'; i++; | |
| 681 | ||
| 682 | for (j = 0; buf[i] != ' '; j++, i++) { | |
| 683 | u_command[j] = buf[i]; | |
| 684 | } | |
| 685 | ||
| 686 | u_command[j] = '\0'; i++; | |
| 687 | ||
| 688 | for (j = 0; buf[i] != ':'; j++, i++) { | |
| 689 | u_channel[j] = buf[i]; | |
| 690 | } | |
| 691 | ||
| 692 | u_channel[j-1] = '\0'; i++; | |
| 693 | ||
| 694 | ||
| 695 | /* Now that everything is parsed, the rest of this baby must be our message */ | |
| 696 | strncpy(u_message, buf + i, IRC_BUF_LEN); | |
| 697 | ||
| 698 | /* Now, lets check the message to see if there's anything special in it */ | |
| 699 | if (u_message[0] == '\001') { | |
| 1017 | 700 | if (g_strncasecmp(u_message, "\001VERSION", 8) == 0) { |
| 701 | /* Looks like we have a version request. Let | |
| 702 | * us handle it thusly */ | |
| 703 | ||
| 704 | g_snprintf(buf, IRC_BUF_LEN, "NOTICE %s :%cVERSION GAIM %s:The Pimpin Penguin AIM Clone:www.marko.net/gaim%c\n", u_nick, '\001', VERSION, '\001'); | |
| 705 | ||
| 706 | write(idata->fd, buf, strlen(buf)); | |
| 707 | ||
| 708 | /* And get the heck out of dodge */ | |
| 709 | return; | |
| 710 | } | |
| 711 | ||
| 712 | if ((g_strncasecmp(u_message, "\001PING ", 6) == 0) && (strlen(u_message) > 6)) { | |
| 713 | /* Someone's triyng to ping us. Let's respond */ | |
| 714 | gchar u_arg[24]; | |
| 715 | ||
| 716 | strcpy(u_arg, u_message + 6); | |
| 717 | u_arg[strlen(u_arg)-1] = '\0'; | |
| 718 | ||
| 719 | g_snprintf(buf, IRC_BUF_LEN, "NOTICE %s :%cPING %s%c\n", u_nick, '\001', u_arg, '\001'); | |
| 720 | ||
| 721 | write(idata->fd, buf, strlen(buf)); | |
| 722 | ||
| 723 | /* And get the heck out of dodge */ | |
| 724 | return; | |
| 725 | } | |
| 726 | ||
| 1011 | 727 | if (g_strncasecmp(u_message, "\001ACTION ", 8) == 0) { |
| 728 | /* Looks like we have an action. Let's parse it a little */ | |
| 729 | strcpy(buf, u_message); | |
| 730 | ||
| 731 | strcpy(u_message, "/me "); | |
| 732 | for (j = 4, i = 8; buf[i] != '\001'; i++, j++) { | |
| 733 | u_message[j] = buf[i]; | |
| 734 | } | |
| 735 | u_message[j] = '\0'; | |
| 736 | } | |
| 737 | } | |
| 738 | ||
| 739 | ||
| 740 | /* Let's check to see if we have a channel on our hands */ | |
| 741 | if (u_channel[0] == '#') { | |
| 742 | /* Yup. We have a channel */ | |
| 743 | int id; | |
| 744 | ||
| 745 | id = find_id_by_name(gc, u_channel); | |
| 746 | if (id != -1) { | |
| 747 | serv_got_chat_in(gc, id, u_nick, 0, u_message); | |
| 748 | } | |
| 749 | } | |
| 750 | else { | |
| 751 | /* Nope. Let's treat it as a private message */ | |
| 752 | serv_got_im(gc, u_nick, u_message, 0); | |
| 753 | } | |
| 754 | ||
| 755 | return; | |
| 756 | } | |
| 757 | ||
| 758 | /* Let's parse PING requests so that we wont get booted for inactivity */ | |
| 759 | ||
| 760 | if (strncmp(buf, "PING :", 6) == 0) { | |
| 761 | buf2 = g_strsplit(buf, ":", 1); | |
| 762 | ||
| 763 | /* Let's build a new response */ | |
| 764 | g_snprintf(buf, IRC_BUF_LEN, "PONG :%s\n", buf2[1]); | |
| 765 | write(idata->fd, buf, strlen(buf)); | |
| 766 | ||
| 767 | /* And clean up after ourselves */ | |
| 768 | g_strfreev(buf2); | |
| 769 | ||
| 770 | return; | |
| 771 | } | |
| 772 | ||
| 773 | } | |
| 774 | ||
| 775 | void irc_handler(gpointer data, gint source, GdkInputCondition condition) { | |
| 776 | irc_callback(data); | |
| 777 | } | |
| 778 | ||
| 779 | void irc_close(struct gaim_connection *gc) { | |
| 780 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 1021 | 781 | GList *chats = idata->channels; |
| 782 | struct irc_channel *cc; | |
| 783 | ||
| 1011 | 784 | gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN); |
| 785 | ||
| 1022 | 786 | gtk_timeout_remove(idata->timer); |
| 787 | ||
| 1021 | 788 | g_snprintf(buf, IRC_BUF_LEN, "QUIT :Download GAIM [www.marko.net/gaim]\n"); |
| 1011 | 789 | write(idata->fd, buf, strlen(buf)); |
| 790 | ||
| 791 | g_free(buf); | |
| 1021 | 792 | |
| 793 | while (chats) { | |
| 794 | cc = (struct irc_channel *)chats->data; | |
| 795 | g_free(cc->name); | |
| 796 | chats = g_list_remove(chats, cc); | |
| 797 | g_free(cc); | |
| 798 | } | |
| 799 | ||
| 800 | if (gc->inpa) | |
| 801 | gdk_input_remove(gc->inpa); | |
| 802 | ||
| 1011 | 803 | close(idata->fd); |
| 804 | g_free(gc->proto_data); | |
| 805 | } | |
| 806 | ||
| 807 | void irc_chat_leave(struct gaim_connection *gc, int id) { | |
| 808 | struct irc_data *idata = (struct irc_data *)gc->proto_data; | |
| 809 | struct irc_channel *channel; | |
| 810 | gchar *buf = (gchar *)g_malloc(IRC_BUF_LEN+1); | |
| 811 | ||
| 812 | channel = find_channel_by_id(gc, id); | |
| 813 | ||
| 814 | if (!channel) { | |
| 815 | return; | |
| 816 | } | |
| 817 | ||
| 818 | g_snprintf(buf, IRC_BUF_LEN, "PART #%s\n", channel->name); | |
| 819 | write(idata->fd, buf, strlen(buf)); | |
| 820 | ||
| 821 | g_free(buf); | |
| 822 | } | |
| 823 | ||
| 824 | void irc_login(struct aim_user *user) { | |
| 825 | int fd; | |
| 826 | struct hostent *host; | |
| 827 | struct sockaddr_in site; | |
| 828 | char buf[4096]; | |
| 829 | ||
| 830 | struct gaim_connection *gc = new_gaim_conn(PROTO_IRC, user->username, user->password); | |
| 831 | struct irc_data *idata = gc->proto_data = g_new0(struct irc_data, 1); | |
| 832 | char c; | |
| 833 | int i; | |
| 834 | int status; | |
| 835 | ||
| 836 | set_login_progress(gc, 1, buf); | |
| 837 | ||
| 838 | while (gtk_events_pending()) | |
| 839 | gtk_main_iteration(); | |
| 840 | ||
| 841 | host = gethostbyname(IRC_SERVER); | |
| 842 | if (!host) { | |
| 843 | hide_login_progress(gc, "Unable to resolve hostname"); | |
| 844 | destroy_gaim_conn(gc); | |
| 845 | return; | |
| 846 | } | |
| 847 | ||
| 848 | site.sin_family = AF_INET; | |
| 849 | site.sin_addr.s_addr = *(long *)(host->h_addr); | |
| 850 | site.sin_port = htons(IRC_PORT); | |
| 851 | ||
| 852 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
| 853 | if (fd < 0) { | |
| 854 | hide_login_progress(gc, "Unable to create socket"); | |
| 855 | destroy_gaim_conn(gc); | |
| 856 | return; | |
| 857 | } | |
| 858 | ||
| 859 | if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) { | |
| 860 | hide_login_progress(gc, "Unable to connect."); | |
| 861 | destroy_gaim_conn(gc); | |
| 862 | return; | |
| 863 | } | |
| 864 | ||
| 865 | idata->fd = fd; | |
| 866 | ||
| 867 | g_snprintf(buf, sizeof(buf), "Signon: %s", gc->username); | |
| 868 | set_login_progress(gc, 2, buf); | |
| 869 | ||
| 870 | /* This is where we will attempt to sign on */ | |
| 871 | ||
| 872 | /* FIXME: This should be their servername, not their username. im just lazy right now */ | |
| 873 | ||
| 874 | g_snprintf(buf, 4096, "NICK %s\nUSER %s localhost %s :GAIM (www.marko.net/gaim)\n", gc->username, gc->username, gc->username); | |
| 875 | write(idata->fd, buf, strlen(buf)); | |
| 876 | ||
| 877 | ||
| 878 | /* Now lets sign ourselves on */ | |
| 879 | account_online(gc); | |
| 880 | ||
| 881 | if (mainwindow) | |
| 882 | gtk_widget_hide(mainwindow); | |
| 883 | ||
| 884 | show_buddy_list(); | |
| 885 | refresh_buddy_window(); | |
| 886 | ||
| 887 | serv_finish_login(gc); | |
| 888 | gaim_setup(gc); | |
| 889 | ||
| 1022 | 890 | if (bud_list_cache_exists(gc)) |
| 891 | do_import(NULL, gc); | |
| 892 | ||
| 893 | ||
| 1011 | 894 | gc->inpa = gdk_input_add(idata->fd, GDK_INPUT_READ, irc_handler, gc); |
| 1022 | 895 | |
| 896 | /* We want to update our buddlist every 20 seconds */ | |
| 897 | idata->timer = gtk_timeout_add(20000, (GtkFunction)irc_request_buddy_update, gc); | |
| 898 | ||
| 899 | /* But first, let's go ahead and check our list */ | |
| 900 | irc_request_buddy_update(gc); | |
| 1011 | 901 | } |
| 1008 | 902 | |
| 987 | 903 | struct prpl *irc_init() { |
| 904 | struct prpl *ret = g_new0(struct prpl, 1); | |
| 905 | ||
| 1008 | 906 | ret->protocol = PROTO_IRC; |
| 907 | ret->name = irc_name; | |
| 908 | ret->login = irc_login; | |
| 1011 | 909 | ret->close = irc_close; |
| 910 | ret->send_im = irc_send_im; | |
| 987 | 911 | ret->set_info = NULL; |
| 912 | ret->get_info = NULL; | |
| 913 | ret->set_away = NULL; | |
| 914 | ret->get_away_msg = NULL; | |
| 915 | ret->set_dir = NULL; | |
| 916 | ret->get_dir = NULL; | |
| 917 | ret->dir_search = NULL; | |
| 918 | ret->set_idle = NULL; | |
| 919 | ret->change_passwd = NULL; | |
| 920 | ret->add_buddy = NULL; | |
| 921 | ret->add_buddies = NULL; | |
| 922 | ret->remove_buddy = NULL; | |
| 923 | ret->add_permit = NULL; | |
| 924 | ret->add_deny = NULL; | |
| 925 | ret->warn = NULL; | |
| 926 | ret->accept_chat = NULL; | |
| 1011 | 927 | ret->join_chat = irc_join_chat; |
| 987 | 928 | ret->chat_invite = NULL; |
| 1011 | 929 | ret->chat_leave = irc_chat_leave; |
| 987 | 930 | ret->chat_whisper = NULL; |
| 1011 | 931 | ret->chat_send = irc_chat_send; |
| 987 | 932 | ret->keepalive = NULL; |
| 933 | ||
| 934 | return ret; | |
| 935 | } | |
| 936 | ||
| 937 | int gaim_plugin_init(void *handle) { | |
| 938 | protocols = g_slist_append(protocols, irc_init()); | |
| 939 | return 0; | |
| 940 | } |