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