Wed, 20 Dec 2000 05:28:35 +0000
[gaim-migrate @ 1337]
hehe
| 1323 | 1 | /* |
| 2 | * gaim - Napster Protocol Plugin | |
| 3 | * | |
| 4 | * Copyright (C) 2000, Rob Flynn <rob@tgflinux.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | ||
| 22 | #include "../config.h" | |
| 23 | ||
| 24 | #include <netdb.h> | |
| 25 | #include <gtk/gtk.h> | |
| 26 | #include <unistd.h> | |
| 27 | #include <errno.h> | |
| 28 | #include <netinet/in.h> | |
| 29 | #include <arpa/inet.h> | |
| 30 | #include <time.h> | |
| 31 | #include <string.h> | |
| 32 | #include <stdlib.h> | |
| 33 | #include <stdio.h> | |
| 34 | #include <time.h> | |
| 35 | #include <sys/socket.h> | |
| 36 | #include <sys/stat.h> | |
| 37 | #include "multi.h" | |
| 38 | #include "prpl.h" | |
| 39 | #include "gaim.h" | |
| 1327 | 40 | #include "pixmaps/napster.xpm" |
| 1323 | 41 | |
| 42 | #define NAP_BUF_LEN 4096 | |
| 43 | ||
| 44 | GSList *nap_connections = NULL; | |
| 45 | ||
| 46 | static unsigned int chat_id = 0; | |
| 47 | ||
| 48 | struct nap_channel { | |
| 49 | unsigned int id; | |
| 50 | gchar *name; | |
| 51 | }; | |
| 52 | ||
| 53 | struct nap_data { | |
| 54 | int fd; | |
| 55 | int inpa; | |
| 56 | ||
| 57 | gchar *email; | |
| 58 | GSList *channels; | |
| 59 | }; | |
| 60 | ||
| 61 | static char *nap_name() | |
| 62 | { | |
| 63 | return "Napster"; | |
| 64 | } | |
| 65 | ||
| 66 | char *name() | |
| 67 | { | |
| 68 | return "Napster"; | |
| 69 | } | |
| 70 | ||
| 71 | char *description() | |
| 72 | { | |
| 73 | return "Allows gaim to use the Napster protocol. Yes, kids, drugs are bad."; | |
| 74 | } | |
| 75 | ||
| 76 | ||
| 77 | /* FIXME: Make this use va_arg stuff */ | |
| 78 | void nap_write_packet(struct gaim_connection *gc, unsigned short command, char *message) | |
| 79 | { | |
| 80 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 81 | unsigned short size; | |
| 82 | ||
| 83 | size = strlen(message); | |
| 84 | ||
| 85 | write(ndata->fd, &size, 2); | |
| 86 | write(ndata->fd, &command, 2); | |
| 87 | write(ndata->fd, message, size); | |
| 88 | } | |
| 89 | ||
| 90 | static void nap_send_im(struct gaim_connection *gc, char *who, char *message, int away) | |
| 91 | { | |
| 92 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 93 | gchar buf[NAP_BUF_LEN]; | |
| 94 | ||
| 95 | g_snprintf(buf, NAP_BUF_LEN, "%s %s", who, message); | |
| 96 | nap_write_packet(gc, 0xCD, buf); | |
| 97 | } | |
| 98 | ||
| 99 | static struct nap_channel *find_channel_by_name(struct gaim_connection *gc, char *name) | |
| 100 | { | |
| 101 | struct nap_channel *channel; | |
| 102 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 103 | GSList *channels; | |
| 104 | ||
| 105 | channels = ndata->channels; | |
| 106 | ||
| 107 | while (channels) { | |
| 108 | channel = (struct nap_channel *)channels->data; | |
| 1325 | 109 | |
| 110 | if (channel) { | |
| 111 | if (!g_strcasecmp(name, channel->name)) { | |
| 112 | return channel; | |
| 113 | } | |
| 1323 | 114 | } |
| 115 | channels = g_slist_next(channels); | |
| 116 | } | |
| 117 | ||
| 118 | return NULL; | |
| 119 | } | |
| 120 | ||
| 121 | static struct nap_channel *find_channel_by_id(struct gaim_connection *gc, int id) | |
| 122 | { | |
| 123 | struct nap_channel *channel; | |
| 124 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 125 | GSList *channels; | |
| 126 | ||
| 127 | channels = ndata->channels; | |
| 128 | ||
| 129 | while (channels) { | |
| 130 | channel = (struct nap_channel *)channels->data; | |
| 131 | if (id == channel->id) { | |
| 132 | return channel; | |
| 133 | } | |
| 134 | ||
| 135 | channels = g_slist_next(channels); | |
| 136 | } | |
| 137 | ||
| 138 | return NULL; | |
| 139 | } | |
| 140 | ||
| 141 | static struct conversation *find_conversation_by_id(struct gaim_connection *gc, int id) | |
| 142 | { | |
| 143 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 144 | GSList *bc = gc->buddy_chats; | |
| 145 | struct conversation *b = NULL; | |
| 146 | ||
| 147 | while (bc) { | |
| 148 | b = (struct conversation *)bc->data; | |
| 149 | if (id == b->id) { | |
| 150 | break; | |
| 151 | } | |
| 152 | bc = bc->next; | |
| 153 | b = NULL; | |
| 154 | } | |
| 155 | ||
| 156 | if (!b) { | |
| 157 | return NULL; | |
| 158 | } | |
| 159 | ||
| 160 | return b; | |
| 161 | } | |
| 162 | ||
| 163 | static void nap_callback(gpointer data, gint source, GdkInputCondition condition) | |
| 164 | { | |
| 165 | struct gaim_connection *gc = data; | |
| 166 | struct nap_data *ndata = gc->proto_data; | |
| 167 | gchar *buf; | |
| 168 | unsigned short header[2]; | |
| 169 | int i = 0; | |
| 170 | int len; | |
| 171 | int command; | |
| 172 | gchar **res; | |
| 173 | ||
| 174 | read(source, header, 4); | |
| 175 | len = header[0]; | |
| 176 | command = header[1]; | |
| 177 | ||
| 178 | buf = (gchar *)g_malloc(sizeof(gchar) * (len + 1)); | |
| 179 | ||
| 180 | read(source, buf, len); | |
| 181 | ||
| 182 | buf[len] = 0; | |
| 183 | ||
| 184 | if (command == 0xd6) { | |
| 185 | res = g_strsplit(buf, " ", 0); | |
| 186 | /* Do we want to report what the users are doing? */ | |
| 187 | printf("users: %s, files: %s, size: %sGB\n", res[0], res[1], res[2]); | |
| 188 | g_strfreev(res); | |
| 189 | free(buf); | |
| 190 | return; | |
| 191 | } | |
| 192 | ||
| 193 | if (command == 0x26d) { | |
| 194 | /* Do we want to use the MOTD? */ | |
| 195 | free(buf); | |
| 196 | return; | |
| 197 | } | |
| 198 | ||
| 199 | if (command == 0xCD) { | |
| 200 | res = g_strsplit(buf, " ", 1); | |
| 201 | serv_got_im(gc, res[0], res[1], 0); | |
| 202 | g_strfreev(res); | |
| 203 | free(buf); | |
| 204 | return; | |
| 205 | } | |
| 206 | ||
| 207 | if (command == 0x195) { | |
| 208 | struct nap_channel *channel; | |
| 209 | int id; | |
| 210 | ||
| 211 | channel = find_channel_by_name(gc, buf); | |
| 212 | ||
| 213 | if (!channel) { | |
| 214 | chat_id++; | |
| 215 | ||
| 216 | channel = g_new0(struct nap_channel, 1); | |
| 217 | ||
| 218 | channel->id = chat_id; | |
| 219 | channel->name = g_strdup(buf); | |
| 220 | ||
| 221 | ndata->channels = g_slist_append(ndata->channels, channel); | |
| 222 | ||
| 223 | serv_got_joined_chat(gc, chat_id, buf); | |
| 224 | } | |
| 225 | ||
| 226 | free(buf); | |
| 227 | return; | |
| 228 | } | |
| 229 | ||
| 230 | if (command == 0x198 || command == 0x196) { | |
| 231 | struct nap_channel *channel; | |
| 232 | struct conversation *convo; | |
| 233 | gchar **res; | |
| 234 | ||
| 235 | res = g_strsplit(buf, " ", 0); | |
| 236 | ||
| 237 | channel = find_channel_by_name(gc, res[0]); | |
| 238 | convo = find_conversation_by_id(gc, channel->id); | |
| 239 | ||
| 240 | add_chat_buddy(convo, res[1]); | |
| 241 | ||
| 242 | g_strfreev(res); | |
| 243 | ||
| 244 | free(buf); | |
| 245 | return; | |
| 246 | } | |
| 247 | ||
| 1325 | 248 | if (command == 0x197) { |
| 249 | struct nap_channel *channel; | |
| 250 | struct conversation *convo; | |
| 251 | gchar **res; | |
| 252 | ||
| 253 | res = g_strsplit(buf, " ", 0); | |
| 254 | ||
| 255 | channel = find_channel_by_name(gc, res[0]); | |
| 256 | convo = find_conversation_by_id(gc, channel->id); | |
| 257 | ||
| 258 | remove_chat_buddy(convo, res[1]); | |
| 259 | ||
| 260 | g_strfreev(res); | |
| 261 | free(buf); | |
| 262 | return; | |
| 263 | } | |
| 264 | ||
| 1323 | 265 | if (command == 0x193) { |
| 266 | gchar **res; | |
| 267 | struct nap_channel *channel; | |
| 268 | ||
| 269 | res = g_strsplit(buf, " ", 2); | |
| 270 | ||
| 271 | channel = find_channel_by_name(gc, res[0]); | |
| 272 | ||
| 273 | if (channel) | |
| 274 | serv_got_chat_in(gc, channel->id, res[1], 0, res[2]); | |
| 275 | ||
| 276 | g_strfreev(res); | |
| 277 | free(buf); | |
| 278 | return; | |
| 279 | } | |
| 280 | ||
| 281 | if (command == 0x194) { | |
| 282 | do_error_dialog(buf, "Gaim: Napster Error"); | |
| 283 | free(buf); | |
| 284 | return; | |
| 285 | } | |
| 286 | ||
| 287 | if (command == 0x12e) { | |
| 288 | gchar buf2[NAP_BUF_LEN]; | |
| 289 | ||
| 290 | g_snprintf(buf2, NAP_BUF_LEN, "Unable to add '%s' to your hotlist", buf); | |
| 291 | do_error_dialog(buf2, "Gaim: Napster Error"); | |
| 292 | ||
| 293 | free(buf); | |
| 294 | return; | |
| 295 | ||
| 296 | } | |
| 297 | ||
| 298 | if (command == 0x191) { | |
| 299 | struct nap_channel *channel; | |
| 300 | ||
| 301 | channel = find_channel_by_name(gc, buf); | |
| 302 | ||
| 303 | if (!channel) /* I'm not sure how this would happen =) */ | |
| 304 | return; | |
| 305 | ||
| 306 | serv_got_chat_left(gc, channel->id); | |
| 307 | ndata->channels = g_slist_remove(ndata->channels, channel); | |
| 308 | ||
| 309 | free(buf); | |
| 310 | return; | |
| 311 | ||
| 312 | } | |
| 313 | ||
| 314 | if (command == 0xd1) { | |
| 315 | gchar **res; | |
| 316 | ||
| 317 | res = g_strsplit(buf, " ", 0); | |
| 318 | ||
| 319 | serv_got_update(gc, res[0], 1, 0, time((time_t *)NULL), 0, 0, 0); | |
| 320 | ||
| 321 | g_strfreev(res); | |
| 322 | free(buf); | |
| 323 | return; | |
| 324 | } | |
| 325 | ||
| 326 | if (command == 0xd2) { | |
| 327 | serv_got_update(gc, buf, 0, 0, 0, 0, 0, 0); | |
| 328 | free(buf); | |
| 329 | return; | |
| 330 | } | |
| 331 | ||
| 332 | if (command == 0x12d) { | |
| 333 | /* Our buddy was added successfully */ | |
| 334 | free(buf); | |
| 335 | return; | |
| 336 | } | |
| 337 | ||
| 338 | printf("NAP: [COMMAND: 0x%04x] %s\n", command, buf); | |
| 339 | } | |
| 340 | ||
| 341 | ||
| 342 | static void nap_login_callback(gpointer data, gint source, GdkInputCondition condition) | |
| 343 | { | |
| 344 | struct gaim_connection *gc = data; | |
| 345 | struct nap_data *ndata = gc->proto_data; | |
| 346 | gchar buf[NAP_BUF_LEN]; | |
| 347 | unsigned short header[2]; | |
| 348 | int i = 0; | |
| 349 | int len; | |
| 350 | int command; | |
| 351 | ||
| 352 | read(source, header, 4); | |
| 353 | len = header[0]; | |
| 354 | command = header[1]; | |
| 355 | ||
| 356 | read(source, buf, len); | |
| 357 | buf[len] = 0; | |
| 358 | ||
| 359 | if (command == 0x03) { | |
| 360 | printf("Registered with E-Mail address of: %s\n", buf); | |
| 361 | ndata->email = g_strdup(buf); | |
| 362 | ||
| 363 | /* Remove old inpa, add new one */ | |
| 364 | gdk_input_remove(ndata->inpa); | |
| 365 | ndata->inpa = 0; | |
| 366 | gc->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_callback, gc); | |
| 367 | ||
| 368 | /* Our signon is complete */ | |
| 369 | account_online(gc); | |
| 370 | serv_finish_login(gc); | |
| 371 | ||
| 372 | if (bud_list_cache_exists(gc)) | |
| 373 | do_import(NULL, gc); | |
| 374 | ||
| 375 | return; | |
| 376 | } | |
| 377 | } | |
| 378 | ||
| 379 | ||
| 380 | ||
| 381 | static void nap_login(struct aim_user *user) | |
| 382 | { | |
| 383 | int fd; | |
| 384 | struct hostent *host; | |
| 385 | struct sockaddr_in site; | |
| 386 | struct gaim_connection *gc = new_gaim_conn(user); | |
| 387 | struct nap_data *ndata = gc->proto_data = g_new0(struct nap_data, 1); | |
| 388 | char buf[NAP_BUF_LEN]; | |
| 389 | char c; | |
| 390 | char z[4]; | |
| 391 | int i; | |
| 392 | int status; | |
| 393 | ||
| 394 | host = gethostbyname("64.124.41.184"); | |
| 395 | ||
| 396 | if (!host) { | |
| 397 | hide_login_progress(gc, "Unable to resolve hostname"); | |
| 398 | signoff(gc); | |
| 399 | return; | |
| 400 | } | |
| 401 | ||
| 402 | site.sin_family = AF_INET; | |
| 403 | site.sin_addr.s_addr = *(long *)(host->h_addr); | |
| 404 | ||
| 405 | site.sin_port = htons(8888); | |
| 406 | ||
| 407 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
| 408 | if (fd < 0) { | |
| 409 | hide_login_progress(gc, "Unable to create socket"); | |
| 410 | signoff(gc); | |
| 411 | return; | |
| 412 | } | |
| 413 | ||
| 414 | /* Make a connection with the server */ | |
| 415 | if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) { | |
| 416 | hide_login_progress(gc, "Unable to connect."); | |
| 417 | signoff(gc); | |
| 418 | return; | |
| 419 | } | |
| 420 | ||
| 421 | ndata->fd = fd; | |
| 422 | ||
| 423 | /* And write our signon data */ | |
| 424 | g_snprintf(buf, NAP_BUF_LEN, "%s %s 0 \"Gnapster 1.4.1a\" 0", gc->username, gc->password); | |
| 425 | nap_write_packet(gc, 0x02, buf); | |
| 426 | ||
| 427 | /* And set up the input watcher */ | |
| 428 | ndata->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_login_callback, gc); | |
| 429 | ||
| 430 | } | |
| 431 | ||
| 432 | static void nap_join_chat(struct gaim_connection *gc, int id, char *name) | |
| 433 | { | |
| 434 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 435 | gchar buf[NAP_BUF_LEN]; | |
| 436 | ||
| 437 | /* Make sure the name has a # preceeding it */ | |
| 438 | if (name[0] != '#') | |
| 439 | g_snprintf(buf, NAP_BUF_LEN, "#%s", name); | |
| 440 | else | |
| 441 | g_snprintf(buf, NAP_BUF_LEN, "%s", name); | |
| 442 | ||
| 443 | nap_write_packet(gc, 0x190, buf); | |
| 444 | } | |
| 445 | ||
| 446 | static void nap_chat_leave(struct gaim_connection *gc, int id) | |
| 447 | { | |
| 448 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 449 | struct nap_channel *channel = NULL; | |
| 450 | GSList *channels = ndata->channels; | |
| 451 | ||
| 452 | channel = find_channel_by_id(gc, id); | |
| 453 | ||
| 454 | if (!channel) /* Again, I'm not sure how this would happen */ | |
| 455 | return; | |
| 456 | ||
| 457 | nap_write_packet(gc, 0x191, channel->name); | |
| 458 | ||
| 1325 | 459 | ndata->channels = g_slist_remove(ndata->channels, channel); |
| 1323 | 460 | g_free(channel->name); |
| 461 | g_free(channel); | |
| 462 | ||
| 463 | } | |
| 464 | ||
| 465 | static void nap_chat_send(struct gaim_connection *gc, int id, char *message) | |
| 466 | { | |
| 467 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 468 | struct nap_channel *channel = NULL; | |
| 469 | gchar buf[NAP_BUF_LEN]; | |
| 470 | ||
| 471 | channel = find_channel_by_id(gc, id); | |
| 472 | ||
| 473 | if (!channel) { | |
| 474 | /* This shouldn't happen */ | |
| 475 | return; | |
| 476 | } | |
| 477 | ||
| 478 | g_snprintf(buf, NAP_BUF_LEN, "%s %s", channel->name, message); | |
| 479 | nap_write_packet(gc, 0x192, buf); | |
| 480 | ||
| 481 | } | |
| 482 | ||
| 483 | static void nap_add_buddy(struct gaim_connection *gc, char *name) | |
| 484 | { | |
| 485 | nap_write_packet(gc, 0xCF, name); | |
| 486 | } | |
| 487 | ||
| 488 | static void nap_remove_buddy(struct gaim_connection *gc, char *name) | |
| 489 | { | |
| 490 | nap_write_packet(gc, 0x12F, name); | |
| 491 | } | |
| 492 | ||
| 493 | static void nap_close(struct gaim_connection *gc) | |
| 494 | { | |
| 495 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 496 | gchar buf[NAP_BUF_LEN]; | |
| 497 | struct nap_channel *channel; | |
| 498 | GSList *channels = ndata->channels; | |
| 499 | ||
| 500 | if (gc->inpa) | |
| 501 | gdk_input_remove(gc->inpa); | |
| 502 | ||
| 1325 | 503 | while (ndata->channels) { |
| 504 | channel = (struct nap_channel *)ndata->channels->data; | |
| 1323 | 505 | g_free(channel->name); |
| 1325 | 506 | ndata->channels = g_slist_remove(ndata->channels, channel); |
| 1323 | 507 | g_free(channel); |
| 508 | } | |
| 1325 | 509 | |
| 510 | free(gc->proto_data); | |
| 1323 | 511 | } |
| 512 | ||
| 513 | static void nap_add_buddies(struct gaim_connection *gc, GList *buddies) | |
| 514 | { | |
| 515 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 516 | gchar buf[NAP_BUF_LEN]; | |
| 517 | int n = 0; | |
| 518 | ||
| 519 | while (buddies) { | |
| 520 | nap_write_packet(gc, 0xd0, (char *)buddies->data); | |
| 521 | buddies = buddies -> next; | |
| 522 | } | |
| 523 | } | |
| 524 | ||
| 1327 | 525 | static char** nap_list_icon(int uc) |
| 526 | { | |
| 527 | return napster_xpm; | |
| 528 | } | |
| 529 | ||
| 1323 | 530 | static struct prpl *my_protocol = NULL; |
| 531 | ||
| 532 | void nap_init(struct prpl *ret) | |
| 533 | { | |
| 534 | ret->protocol = PROTO_NAPSTER; | |
| 535 | ret->name = nap_name; | |
| 1327 | 536 | ret->list_icon = nap_list_icon; |
| 1323 | 537 | ret->action_menu = NULL; |
| 538 | ret->user_opts = NULL; | |
| 539 | ret->login = nap_login; | |
| 540 | ret->close = nap_close; | |
| 541 | ret->send_im = nap_send_im; | |
| 542 | ret->set_info = NULL; | |
| 543 | ret->get_info = NULL; | |
| 544 | ret->set_away = NULL; | |
| 545 | ret->get_away_msg = NULL; | |
| 546 | ret->set_dir = NULL; | |
| 547 | ret->get_dir = NULL; | |
| 548 | ret->dir_search = NULL; | |
| 549 | ret->set_idle = NULL; | |
| 550 | ret->change_passwd = NULL; | |
| 551 | ret->add_buddy = nap_add_buddy; | |
| 552 | ret->add_buddies = nap_add_buddies; | |
| 553 | ret->remove_buddy = nap_remove_buddy; | |
| 554 | ret->add_permit = NULL; | |
| 555 | ret->rem_permit = NULL; | |
| 556 | ret->add_deny = NULL; | |
| 557 | ret->rem_deny = NULL; | |
| 558 | ret->warn = NULL; | |
| 559 | ret->accept_chat = NULL; | |
| 560 | ret->join_chat = nap_join_chat; | |
| 561 | ret->chat_invite = NULL; | |
| 562 | ret->chat_leave = nap_chat_leave; | |
| 563 | ret->chat_whisper = NULL; | |
| 564 | ret->chat_send = nap_chat_send; | |
| 565 | ret->keepalive = NULL; | |
| 566 | ||
| 567 | my_protocol = ret; | |
| 568 | } | |
| 569 | ||
| 570 | char *gaim_plugin_init(GModule * handle) | |
| 571 | { | |
| 572 | load_protocol(nap_init); | |
| 573 | return NULL; | |
| 574 | } | |
| 575 | ||
| 576 | void gaim_plugin_remove() | |
| 577 | { | |
| 578 | struct prpl *p = find_prpl(PROTO_NAPSTER); | |
| 579 | if (p == my_protocol) | |
| 580 | unload_protocol(p); | |
| 581 | } |