Wed, 24 Jan 2001 12:00:32 +0000
[gaim-migrate @ 1432]
Yeah, it sucks so far and its only 'browse' support. Searching will be easy to add, thankfully. Also,
download progress is printed on the console.
Files are saved in $HOME/
Progress bar will be implemented.
The clist currently uses select-row. That sucks. Ill change that too.
im sleepy.
| 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> | |
| 1422 | 37 | #include <sys/types.h> |
| 38 | #include <fcntl.h> | |
| 39 | #include <ctype.h> | |
| 1323 | 40 | #include "multi.h" |
| 41 | #include "prpl.h" | |
| 42 | #include "gaim.h" | |
| 1327 | 43 | #include "pixmaps/napster.xpm" |
| 1323 | 44 | |
| 45 | #define NAP_BUF_LEN 4096 | |
| 46 | ||
| 47 | GSList *nap_connections = NULL; | |
| 48 | ||
| 49 | static unsigned int chat_id = 0; | |
| 50 | ||
| 1422 | 51 | struct browse_window { |
| 52 | GtkWidget *window; | |
| 53 | GtkWidget *list; | |
| 54 | struct gaim_connection *gc; | |
| 55 | char *name; | |
| 56 | }; | |
| 57 | ||
| 58 | struct nap_download_box { | |
| 59 | GtkWidget *window; | |
| 60 | GtkWidget *ok; | |
| 61 | GtkWidget *entry; | |
| 62 | gchar *who; | |
| 63 | }; | |
| 64 | ||
| 1323 | 65 | struct nap_channel { |
| 66 | unsigned int id; | |
| 67 | gchar *name; | |
| 68 | }; | |
| 69 | ||
| 1422 | 70 | struct nap_file_request { |
| 71 | gchar *name; | |
| 72 | gchar *file; | |
| 73 | int fd; | |
| 74 | long size; | |
| 75 | long total; | |
| 76 | int status; | |
| 77 | int inpa; | |
| 78 | FILE *mp3; | |
| 79 | }; | |
| 80 | ||
| 1323 | 81 | struct nap_data { |
| 82 | int fd; | |
| 83 | int inpa; | |
| 84 | ||
| 85 | gchar *email; | |
| 86 | GSList *channels; | |
| 1422 | 87 | GSList *requests; |
| 88 | GSList *browses; | |
| 1323 | 89 | }; |
| 90 | ||
| 91 | static char *nap_name() | |
| 92 | { | |
| 93 | return "Napster"; | |
| 94 | } | |
| 95 | ||
| 96 | char *name() | |
| 97 | { | |
| 98 | return "Napster"; | |
| 99 | } | |
| 100 | ||
| 101 | char *description() | |
| 102 | { | |
| 103 | return "Allows gaim to use the Napster protocol. Yes, kids, drugs are bad."; | |
| 104 | } | |
| 105 | ||
| 106 | ||
| 107 | /* FIXME: Make this use va_arg stuff */ | |
| 108 | void nap_write_packet(struct gaim_connection *gc, unsigned short command, char *message) | |
| 109 | { | |
| 110 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 111 | unsigned short size; | |
| 112 | ||
| 113 | size = strlen(message); | |
| 114 | ||
| 115 | write(ndata->fd, &size, 2); | |
| 116 | write(ndata->fd, &command, 2); | |
| 117 | write(ndata->fd, message, size); | |
| 118 | } | |
| 119 | ||
| 1422 | 120 | void nap_send_download_req(struct gaim_connection *gc, char *who, char *file) |
| 121 | { | |
| 122 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 123 | gchar buf[NAP_BUF_LEN]; | |
| 124 | ||
| 125 | g_snprintf(buf, NAP_BUF_LEN, "%s \"%s\"", who, file); | |
| 126 | ||
| 127 | nap_write_packet(gc, 0xCB, buf); | |
| 128 | } | |
| 129 | ||
| 130 | void nap_handle_download(GtkCList *clist, gint row, gint col, GdkEventButton *event, gpointer user_data) | |
| 131 | { | |
| 132 | gchar **results; | |
| 133 | struct browse_window *bw = (struct browse_window *)user_data; | |
| 134 | ||
| 135 | gtk_clist_get_text(GTK_CLIST(bw->list), row, 0, results); | |
| 136 | ||
| 137 | nap_send_download_req(bw->gc, bw->name, results[0]); | |
| 138 | ||
| 139 | } | |
| 140 | ||
| 141 | struct browse_window *browse_window_new(struct gaim_connection *gc, char *name) | |
| 142 | { | |
| 143 | struct browse_window *browse = g_new0(struct browse_window, 1); | |
| 144 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 145 | ||
| 146 | browse->window = gtk_window_new(GTK_WINDOW_DIALOG); | |
| 147 | browse->name = g_strdup(name); | |
| 148 | browse->list = gtk_clist_new(1); | |
| 149 | browse->gc = gc; | |
| 150 | ||
| 151 | gtk_widget_show(browse->list); | |
| 152 | gtk_container_add(GTK_CONTAINER(browse->window), browse->list); | |
| 153 | ||
| 154 | gtk_widget_set_usize(GTK_WIDGET(browse->window), 300, 250); | |
| 155 | gtk_widget_show(browse->window); | |
| 156 | ||
| 157 | /*FIXME: I dont like using select-row. Im lazy. Ill fix it later */ | |
| 158 | gtk_signal_connect(GTK_OBJECT(browse->list), "select-row", GTK_SIGNAL_FUNC(nap_handle_download), browse); | |
| 159 | ||
| 160 | ndata->browses = g_slist_append(ndata->browses, browse); | |
| 161 | } | |
| 162 | ||
| 163 | void browse_window_add_file(struct browse_window *bw, char *name) | |
| 164 | { | |
| 165 | char *fn[1]; | |
| 166 | fn[0] = strdup(name); | |
| 167 | printf("User '%s' has file '%s'\n", bw->name, name); | |
| 168 | gtk_clist_append(GTK_CLIST(bw->list), fn); | |
| 169 | ||
| 170 | free(fn[0]); | |
| 171 | } | |
| 172 | ||
| 173 | static struct browse_window *find_browse_window_by_name(struct gaim_connection *gc, char *name) | |
| 174 | { | |
| 175 | struct browse_window *browse; | |
| 176 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 177 | GSList *browses; | |
| 178 | ||
| 179 | browses = ndata->browses; | |
| 180 | ||
| 181 | while (browses) { | |
| 182 | browse = (struct browse_window *)browses->data; | |
| 183 | ||
| 184 | if (browse) { | |
| 185 | if (!g_strcasecmp(name, browse->name)) { | |
| 186 | return browse; | |
| 187 | } | |
| 188 | } | |
| 189 | browses = g_slist_next(browses); | |
| 190 | } | |
| 191 | ||
| 192 | return NULL; | |
| 193 | } | |
| 194 | ||
| 1323 | 195 | static void nap_send_im(struct gaim_connection *gc, char *who, char *message, int away) |
| 196 | { | |
| 197 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 198 | gchar buf[NAP_BUF_LEN]; | |
| 199 | ||
| 200 | g_snprintf(buf, NAP_BUF_LEN, "%s %s", who, message); | |
| 201 | nap_write_packet(gc, 0xCD, buf); | |
| 202 | } | |
| 203 | ||
| 204 | static struct nap_channel *find_channel_by_name(struct gaim_connection *gc, char *name) | |
| 205 | { | |
| 206 | struct nap_channel *channel; | |
| 207 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 208 | GSList *channels; | |
| 209 | ||
| 210 | channels = ndata->channels; | |
| 211 | ||
| 212 | while (channels) { | |
| 213 | channel = (struct nap_channel *)channels->data; | |
| 1325 | 214 | |
| 215 | if (channel) { | |
| 216 | if (!g_strcasecmp(name, channel->name)) { | |
| 217 | return channel; | |
| 218 | } | |
| 1323 | 219 | } |
| 220 | channels = g_slist_next(channels); | |
| 221 | } | |
| 222 | ||
| 223 | return NULL; | |
| 224 | } | |
| 225 | ||
| 226 | static struct nap_channel *find_channel_by_id(struct gaim_connection *gc, int id) | |
| 227 | { | |
| 228 | struct nap_channel *channel; | |
| 229 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 230 | GSList *channels; | |
| 231 | ||
| 232 | channels = ndata->channels; | |
| 233 | ||
| 234 | while (channels) { | |
| 235 | channel = (struct nap_channel *)channels->data; | |
| 236 | if (id == channel->id) { | |
| 237 | return channel; | |
| 238 | } | |
| 239 | ||
| 240 | channels = g_slist_next(channels); | |
| 241 | } | |
| 242 | ||
| 243 | return NULL; | |
| 244 | } | |
| 245 | ||
| 246 | static struct conversation *find_conversation_by_id(struct gaim_connection *gc, int id) | |
| 247 | { | |
| 248 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 249 | GSList *bc = gc->buddy_chats; | |
| 250 | struct conversation *b = NULL; | |
| 251 | ||
| 252 | while (bc) { | |
| 253 | b = (struct conversation *)bc->data; | |
| 254 | if (id == b->id) { | |
| 255 | break; | |
| 256 | } | |
| 257 | bc = bc->next; | |
| 258 | b = NULL; | |
| 259 | } | |
| 260 | ||
| 261 | if (!b) { | |
| 262 | return NULL; | |
| 263 | } | |
| 264 | ||
| 265 | return b; | |
| 266 | } | |
| 267 | ||
| 1422 | 268 | /* This is a strange function. I smoke too many bad bad things :-) */ |
| 269 | struct nap_file_request * find_request_by_fd(struct gaim_connection *gc, int fd) | |
| 270 | { | |
| 271 | struct nap_file_request *req; | |
| 272 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 273 | GSList *requests; | |
| 274 | ||
| 275 | requests = ndata->requests; | |
| 276 | ||
| 277 | while (requests) { | |
| 278 | req = (struct nap_file_request *)requests->data; | |
| 279 | ||
| 280 | if (req) { | |
| 281 | if (req->fd == fd) | |
| 282 | return req; | |
| 283 | } | |
| 284 | requests = g_slist_next(requests); | |
| 285 | } | |
| 286 | ||
| 287 | return NULL; | |
| 288 | } | |
| 289 | ||
| 290 | static void nap_ctc_callback(gpointer data, gint source, GdkInputCondition condition) | |
| 291 | { | |
| 292 | struct gaim_connection *gc = (struct gaim_connection *)data; | |
| 293 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 294 | struct nap_file_request *req; | |
| 295 | unsigned char *buf; | |
| 296 | int i = 0; | |
| 297 | gchar *c; | |
| 298 | int len; | |
| 299 | ||
| 300 | req = find_request_by_fd(gc, source); | |
| 301 | if (!req) /* Something bad happened */ | |
| 302 | return; | |
| 303 | ||
| 304 | buf = (char *)malloc(sizeof(char) * (NAP_BUF_LEN + 1)); | |
| 305 | ||
| 306 | if (req->status == 0) | |
| 307 | { | |
| 308 | int j; | |
| 309 | gchar tmp[32]; | |
| 310 | long filesize; | |
| 311 | gchar **parse_name; | |
| 312 | gchar path[2048]; | |
| 313 | ||
| 314 | ||
| 315 | recv(source, buf, 1, 0); | |
| 316 | ||
| 317 | /* We should receive a '1' upon connection */ | |
| 318 | if (buf[0] != '1') | |
| 319 | { | |
| 320 | do_error_dialog("Uh Oh", "Uh Oh"); | |
| 321 | gdk_input_remove(req->inpa); | |
| 322 | ndata->requests = g_slist_remove(ndata->requests, req); | |
| 323 | g_free(req->name); | |
| 324 | g_free(req->file); | |
| 325 | close(source); | |
| 326 | g_free(req); | |
| 327 | free(buf); | |
| 328 | return; | |
| 329 | } | |
| 330 | ||
| 331 | /* Lets take a peek at the awaiting data */ | |
| 332 | i = recv(source, buf, NAP_BUF_LEN, MSG_PEEK); | |
| 333 | buf[i] = 0; /* Make sure that we terminate our string */ | |
| 334 | ||
| 335 | /* Looks like the uploader sent the proper data. Let's see how big the | |
| 336 | * file is */ | |
| 337 | ||
| 338 | for (j = 0, i = 0; isdigit(buf[i]); i++, j++) | |
| 339 | { | |
| 340 | tmp[j] = buf[i]; | |
| 341 | } | |
| 342 | tmp[j] = 0; | |
| 343 | filesize = atol(tmp); | |
| 344 | ||
| 345 | /* Save the size of the file */ | |
| 346 | req->total = filesize; | |
| 347 | ||
| 348 | /* If we have a zero file size then something bad happened */ | |
| 349 | if (filesize == 0) { | |
| 350 | gdk_input_remove(req->inpa); | |
| 351 | ndata->requests = g_slist_remove(ndata->requests, req); | |
| 352 | g_free(req->name); | |
| 353 | g_free(req->file); | |
| 354 | g_free(req); | |
| 355 | free(buf); | |
| 356 | close(source); | |
| 357 | return; | |
| 358 | } | |
| 359 | ||
| 360 | /* Now that we've done that, let's go ahead and read that | |
| 361 | * data to get it out of the way */ | |
| 362 | recv(source, buf, strlen(tmp), 0); | |
| 363 | ||
| 364 | /* Now, we should tell the server that we're download something */ | |
| 365 | nap_write_packet(gc, 0xda, "\n"); | |
| 366 | ||
| 367 | req->status = 1; | |
| 368 | ||
| 369 | /* FIXME: We dont want to force the file name. I'll parse this | |
| 370 | * later */ | |
| 371 | ||
| 372 | parse_name = g_strsplit(req->file, "\\", 0); | |
| 373 | g_snprintf(path, sizeof(path), "%s/%s", getenv("HOME"), parse_name[sizeof(parse_name)]); | |
| 374 | printf("Gonna try to save to: %s\n", path); | |
| 375 | g_strfreev(parse_name); | |
| 376 | ||
| 377 | req->mp3 = fopen(path, "w"); | |
| 378 | free(buf); | |
| 379 | return; | |
| 380 | } | |
| 381 | ||
| 382 | /* Looks like our status isn't 1. It's safe to assume we're downloadin' */ | |
| 383 | i = recv(source, buf, NAP_BUF_LEN, 0); | |
| 384 | ||
| 385 | req->size += i; /* Lets add up the total */ | |
| 386 | ||
| 387 | printf("Downloaded %ld of %ld\n", req->size, req->total); | |
| 388 | ||
| 389 | fwrite(buf, i, sizeof(char), req->mp3); | |
| 390 | ||
| 391 | free(buf); | |
| 392 | ||
| 393 | if (req->size >= req->total) { | |
| 394 | printf("Download complete.\n"); | |
| 395 | nap_write_packet(gc, 0xdb, "\n"); /* Tell the server we're finished */ | |
| 396 | gdk_input_remove(req->inpa); | |
| 397 | ndata->requests = g_slist_remove(ndata->requests, req); | |
| 398 | g_free(req->name); | |
| 399 | g_free(req->file); | |
| 400 | g_free(req); | |
| 401 | fclose(req->mp3); | |
| 402 | close(source); | |
| 403 | } | |
| 404 | } | |
| 405 | ||
| 406 | void nap_get_file(struct gaim_connection *gc, gchar *user, gchar *file, unsigned long host, unsigned int port) | |
| 407 | { | |
| 408 | int fd; | |
| 409 | struct sockaddr_in site; | |
| 410 | char buf[NAP_BUF_LEN]; | |
| 411 | int inpa; | |
| 412 | struct nap_file_request *req = g_new0(struct nap_file_request, 1); | |
| 413 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 414 | ||
| 415 | ||
| 416 | site.sin_family = AF_INET; | |
| 417 | site.sin_addr.s_addr = host; | |
| 418 | site.sin_port = htons(port); | |
| 419 | ||
| 420 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
| 421 | ||
| 422 | if (fd < 0) { | |
| 423 | do_error_dialog("Error connecting to user", "Gaim: Napster error"); | |
| 424 | return; | |
| 425 | } | |
| 426 | ||
| 427 | /* Make a connection with the server */ | |
| 428 | if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) { | |
| 429 | do_error_dialog("Error connecting to user", "Gaim: Napster error"); | |
| 430 | return; | |
| 431 | } | |
| 432 | ||
| 433 | req->fd = fd; | |
| 434 | req->name = g_strdup(user); | |
| 435 | req->file = g_strdup(file); | |
| 436 | req->size = 0; | |
| 437 | req->status = 0; | |
| 438 | req->total = 0; | |
| 439 | ||
| 440 | /* Send our request to the user */ | |
| 441 | g_snprintf(buf, sizeof(buf), "GET%s \"%s\" 0\n", user, file); | |
| 442 | write(fd, buf, strlen(buf)); | |
| 443 | ||
| 444 | /* Add our request */ | |
| 445 | ndata->requests = g_slist_append(ndata->requests, req); | |
| 446 | ||
| 447 | /* And start monitoring */ | |
| 448 | req->inpa = gdk_input_add(fd, GDK_INPUT_READ, nap_ctc_callback, gc); | |
| 449 | } | |
| 450 | ||
| 1323 | 451 | static void nap_callback(gpointer data, gint source, GdkInputCondition condition) |
| 452 | { | |
| 453 | struct gaim_connection *gc = data; | |
| 454 | struct nap_data *ndata = gc->proto_data; | |
| 455 | gchar *buf; | |
| 456 | unsigned short header[2]; | |
| 457 | int i = 0; | |
| 458 | int len; | |
| 459 | int command; | |
| 460 | gchar **res; | |
| 461 | ||
| 462 | read(source, header, 4); | |
| 463 | len = header[0]; | |
| 464 | command = header[1]; | |
| 465 | ||
| 466 | buf = (gchar *)g_malloc(sizeof(gchar) * (len + 1)); | |
| 467 | ||
| 468 | read(source, buf, len); | |
| 469 | ||
| 470 | buf[len] = 0; | |
| 471 | ||
| 472 | if (command == 0xd6) { | |
| 473 | res = g_strsplit(buf, " ", 0); | |
| 474 | /* Do we want to report what the users are doing? */ | |
| 475 | printf("users: %s, files: %s, size: %sGB\n", res[0], res[1], res[2]); | |
| 476 | g_strfreev(res); | |
| 477 | free(buf); | |
| 478 | return; | |
| 479 | } | |
| 480 | ||
| 481 | if (command == 0x26d) { | |
| 482 | /* Do we want to use the MOTD? */ | |
| 483 | free(buf); | |
| 484 | return; | |
| 485 | } | |
| 486 | ||
| 487 | if (command == 0xCD) { | |
| 488 | res = g_strsplit(buf, " ", 1); | |
| 489 | serv_got_im(gc, res[0], res[1], 0); | |
| 490 | g_strfreev(res); | |
| 491 | free(buf); | |
| 492 | return; | |
| 493 | } | |
| 494 | ||
| 495 | if (command == 0x195) { | |
| 496 | struct nap_channel *channel; | |
| 497 | int id; | |
| 498 | ||
| 499 | channel = find_channel_by_name(gc, buf); | |
| 500 | ||
| 501 | if (!channel) { | |
| 502 | chat_id++; | |
| 503 | ||
| 504 | channel = g_new0(struct nap_channel, 1); | |
| 505 | ||
| 506 | channel->id = chat_id; | |
| 507 | channel->name = g_strdup(buf); | |
| 508 | ||
| 509 | ndata->channels = g_slist_append(ndata->channels, channel); | |
| 510 | ||
| 511 | serv_got_joined_chat(gc, chat_id, buf); | |
| 512 | } | |
| 513 | ||
| 514 | free(buf); | |
| 515 | return; | |
| 516 | } | |
| 517 | ||
| 518 | if (command == 0x198 || command == 0x196) { | |
| 519 | struct nap_channel *channel; | |
| 520 | struct conversation *convo; | |
| 521 | gchar **res; | |
| 522 | ||
| 523 | res = g_strsplit(buf, " ", 0); | |
| 524 | ||
| 525 | channel = find_channel_by_name(gc, res[0]); | |
| 526 | convo = find_conversation_by_id(gc, channel->id); | |
| 527 | ||
| 528 | add_chat_buddy(convo, res[1]); | |
| 529 | ||
| 530 | g_strfreev(res); | |
| 531 | ||
| 532 | free(buf); | |
| 533 | return; | |
| 534 | } | |
| 535 | ||
| 1325 | 536 | if (command == 0x197) { |
| 537 | struct nap_channel *channel; | |
| 538 | struct conversation *convo; | |
| 539 | gchar **res; | |
| 540 | ||
| 541 | res = g_strsplit(buf, " ", 0); | |
| 542 | ||
| 543 | channel = find_channel_by_name(gc, res[0]); | |
| 544 | convo = find_conversation_by_id(gc, channel->id); | |
| 545 | ||
| 546 | remove_chat_buddy(convo, res[1]); | |
| 547 | ||
| 548 | g_strfreev(res); | |
| 549 | free(buf); | |
| 550 | return; | |
| 551 | } | |
| 552 | ||
| 1323 | 553 | if (command == 0x193) { |
| 554 | gchar **res; | |
| 555 | struct nap_channel *channel; | |
| 556 | ||
| 557 | res = g_strsplit(buf, " ", 2); | |
| 558 | ||
| 559 | channel = find_channel_by_name(gc, res[0]); | |
| 560 | ||
| 561 | if (channel) | |
| 562 | serv_got_chat_in(gc, channel->id, res[1], 0, res[2]); | |
| 563 | ||
| 564 | g_strfreev(res); | |
| 565 | free(buf); | |
| 566 | return; | |
| 567 | } | |
| 568 | ||
| 569 | if (command == 0x194) { | |
| 570 | do_error_dialog(buf, "Gaim: Napster Error"); | |
| 571 | free(buf); | |
| 572 | return; | |
| 573 | } | |
| 574 | ||
| 575 | if (command == 0x12e) { | |
| 576 | gchar buf2[NAP_BUF_LEN]; | |
| 577 | ||
| 578 | g_snprintf(buf2, NAP_BUF_LEN, "Unable to add '%s' to your hotlist", buf); | |
| 579 | do_error_dialog(buf2, "Gaim: Napster Error"); | |
| 580 | ||
| 581 | free(buf); | |
| 582 | return; | |
| 583 | ||
| 584 | } | |
| 585 | ||
| 586 | if (command == 0x191) { | |
| 587 | struct nap_channel *channel; | |
| 588 | ||
| 589 | channel = find_channel_by_name(gc, buf); | |
| 590 | ||
| 591 | if (!channel) /* I'm not sure how this would happen =) */ | |
| 592 | return; | |
| 593 | ||
| 594 | serv_got_chat_left(gc, channel->id); | |
| 595 | ndata->channels = g_slist_remove(ndata->channels, channel); | |
| 596 | ||
| 597 | free(buf); | |
| 598 | return; | |
| 599 | ||
| 600 | } | |
| 601 | ||
| 602 | if (command == 0xd1) { | |
| 603 | gchar **res; | |
| 604 | ||
| 605 | res = g_strsplit(buf, " ", 0); | |
| 606 | ||
| 607 | serv_got_update(gc, res[0], 1, 0, time((time_t *)NULL), 0, 0, 0); | |
| 608 | ||
| 609 | g_strfreev(res); | |
| 610 | free(buf); | |
| 611 | return; | |
| 612 | } | |
| 613 | ||
| 614 | if (command == 0xd2) { | |
| 615 | serv_got_update(gc, buf, 0, 0, 0, 0, 0, 0); | |
| 616 | free(buf); | |
| 617 | return; | |
| 618 | } | |
| 619 | ||
| 1422 | 620 | if (command == 0xd4) { |
| 621 | /* Looks like we're getting a browse response */ | |
| 622 | gchar user[64]; | |
| 623 | gchar file[2048]; | |
| 624 | struct browse_window *bw = NULL; | |
| 625 | ||
| 626 | int i,j; | |
| 627 | ||
| 628 | for (i = 0, j = 0; buf[i] != ' '; i++, j++) | |
| 629 | { | |
| 630 | user[j] = buf[i]; | |
| 631 | } | |
| 632 | user[j] = 0; i++; i++; | |
| 633 | ||
| 634 | for (j = 0; buf[i] != '\"'; i++, j++) | |
| 635 | { | |
| 636 | file[j] = buf[i]; | |
| 637 | } | |
| 638 | file[j] = 0; | |
| 639 | ||
| 640 | bw = find_browse_window_by_name(gc, user); | |
| 641 | if (!bw) | |
| 642 | { | |
| 643 | /* If a browse window isn't found, let's create one */ | |
| 644 | bw = browse_window_new(gc, user); | |
| 645 | } | |
| 646 | ||
| 647 | browse_window_add_file(bw, file); | |
| 648 | ||
| 649 | free(buf); | |
| 650 | return; | |
| 651 | ||
| 652 | } | |
| 653 | ||
| 1323 | 654 | if (command == 0x12d) { |
| 655 | /* Our buddy was added successfully */ | |
| 656 | free(buf); | |
| 657 | return; | |
| 658 | } | |
| 659 | ||
| 1329 | 660 | if (command == 0x2ec) { |
| 661 | /* Looks like someone logged in as us! =-O */ | |
| 662 | free(buf); | |
| 663 | ||
| 664 | signoff(gc); | |
| 665 | return; | |
| 666 | } | |
| 667 | ||
| 1422 | 668 | if (command == 0xcc) { |
| 669 | /* We received a Download ACK from a user. The way this is printed is kind of | |
| 670 | * strange so we'll need to parse this one ourselves. */ | |
| 671 | ||
| 672 | gchar user[64]; | |
| 673 | gchar file[2048]; | |
| 674 | gchar hoststr[16]; | |
| 675 | gchar portstr[16]; | |
| 676 | int i,j; | |
| 677 | ||
| 678 | for (i = 0, j = 0; buf[i] != ' '; i++, j++) | |
| 679 | { | |
| 680 | user[j] = buf[i]; | |
| 681 | } | |
| 682 | user[j] = 0; i++; | |
| 683 | ||
| 684 | for (j = 0; buf[i] != ' '; i++, j++) | |
| 685 | { | |
| 686 | hoststr[j] = buf[i]; | |
| 687 | } | |
| 688 | hoststr[j] = 0; i++; | |
| 689 | ||
| 690 | for (j = 0; buf[i] != ' '; i++, j++) | |
| 691 | { | |
| 692 | portstr[j] = buf[i]; | |
| 693 | } | |
| 694 | portstr[j] = 0; i++; | |
| 695 | ||
| 696 | i++; /* We do this to ignore the first quotation mark */ | |
| 697 | ||
| 698 | for (j = 0; buf[i] != '\"'; i++, j++) | |
| 699 | { | |
| 700 | file[j] = buf[i]; | |
| 701 | } | |
| 702 | file[j] = 0; | |
| 703 | ||
| 704 | /* Aaight. We dont need nuttin' else. Let's download the file */ | |
| 705 | nap_get_file(gc, user, file, atol(hoststr), atoi(portstr)); | |
| 706 | ||
| 707 | free(buf); | |
| 708 | ||
| 709 | return; | |
| 710 | } | |
| 711 | ||
| 1323 | 712 | printf("NAP: [COMMAND: 0x%04x] %s\n", command, buf); |
| 1422 | 713 | |
| 714 | free(buf); | |
| 1323 | 715 | } |
| 716 | ||
| 717 | ||
| 718 | static void nap_login_callback(gpointer data, gint source, GdkInputCondition condition) | |
| 719 | { | |
| 720 | struct gaim_connection *gc = data; | |
| 721 | struct nap_data *ndata = gc->proto_data; | |
| 722 | gchar buf[NAP_BUF_LEN]; | |
| 723 | unsigned short header[2]; | |
| 724 | int i = 0; | |
| 725 | int len; | |
| 726 | int command; | |
| 727 | ||
| 728 | read(source, header, 4); | |
| 729 | len = header[0]; | |
| 730 | command = header[1]; | |
| 731 | ||
| 732 | read(source, buf, len); | |
| 733 | buf[len] = 0; | |
| 734 | ||
| 735 | if (command == 0x03) { | |
| 736 | printf("Registered with E-Mail address of: %s\n", buf); | |
| 737 | ndata->email = g_strdup(buf); | |
| 738 | ||
| 739 | /* Remove old inpa, add new one */ | |
| 740 | gdk_input_remove(ndata->inpa); | |
| 741 | ndata->inpa = 0; | |
| 742 | gc->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_callback, gc); | |
| 743 | ||
| 744 | /* Our signon is complete */ | |
| 745 | account_online(gc); | |
| 746 | serv_finish_login(gc); | |
| 747 | ||
| 748 | if (bud_list_cache_exists(gc)) | |
| 749 | do_import(NULL, gc); | |
| 750 | ||
| 751 | return; | |
| 752 | } | |
| 753 | } | |
| 754 | ||
| 755 | ||
| 756 | ||
| 757 | static void nap_login(struct aim_user *user) | |
| 758 | { | |
| 759 | int fd; | |
| 760 | struct hostent *host; | |
| 761 | struct sockaddr_in site; | |
| 762 | struct gaim_connection *gc = new_gaim_conn(user); | |
| 763 | struct nap_data *ndata = gc->proto_data = g_new0(struct nap_data, 1); | |
| 764 | char buf[NAP_BUF_LEN]; | |
| 765 | char c; | |
| 766 | char z[4]; | |
| 767 | int i; | |
| 768 | int status; | |
| 769 | ||
| 1422 | 770 | // host = gethostbyname("208.184.216.24"); |
| 771 | host = gethostbyname("213.254.1.82"); | |
| 1323 | 772 | |
| 1422 | 773 | if (!host) { |
| 774 | hide_login_progress(gc, "Unable to resolve hostname"); | |
| 775 | signoff(gc); | |
| 776 | return; | |
| 777 | } | |
| 1323 | 778 | |
| 1422 | 779 | site.sin_family = AF_INET; |
| 780 | site.sin_addr.s_addr = *(long *)(host->h_addr); | |
| 781 | site.sin_port = htons(8888); | |
| 1323 | 782 | |
| 1422 | 783 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 784 | if (fd < 0) { | |
| 785 | hide_login_progress(gc, "Unable to create socket"); | |
| 786 | signoff(gc); | |
| 787 | return; | |
| 788 | } | |
| 1323 | 789 | |
| 790 | /* Make a connection with the server */ | |
| 1422 | 791 | if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) { |
| 792 | hide_login_progress(gc, "Unable to connect."); | |
| 793 | signoff(gc); | |
| 794 | return; | |
| 795 | } | |
| 1323 | 796 | |
| 797 | ndata->fd = fd; | |
| 798 | ||
| 799 | /* And write our signon data */ | |
| 1422 | 800 | g_snprintf(buf, NAP_BUF_LEN, "%s %s 0 \"Gaim - Napster Plugin\" 0", gc->username, gc->password); |
| 1323 | 801 | nap_write_packet(gc, 0x02, buf); |
| 802 | ||
| 803 | /* And set up the input watcher */ | |
| 804 | ndata->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_login_callback, gc); | |
| 805 | ||
| 806 | } | |
| 807 | ||
| 808 | static void nap_join_chat(struct gaim_connection *gc, int id, char *name) | |
| 809 | { | |
| 810 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 811 | gchar buf[NAP_BUF_LEN]; | |
| 812 | ||
| 813 | /* Make sure the name has a # preceeding it */ | |
| 814 | if (name[0] != '#') | |
| 815 | g_snprintf(buf, NAP_BUF_LEN, "#%s", name); | |
| 816 | else | |
| 817 | g_snprintf(buf, NAP_BUF_LEN, "%s", name); | |
| 818 | ||
| 819 | nap_write_packet(gc, 0x190, buf); | |
| 820 | } | |
| 821 | ||
| 822 | static void nap_chat_leave(struct gaim_connection *gc, int id) | |
| 823 | { | |
| 824 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 825 | struct nap_channel *channel = NULL; | |
| 826 | GSList *channels = ndata->channels; | |
| 827 | ||
| 828 | channel = find_channel_by_id(gc, id); | |
| 829 | ||
| 830 | if (!channel) /* Again, I'm not sure how this would happen */ | |
| 831 | return; | |
| 832 | ||
| 833 | nap_write_packet(gc, 0x191, channel->name); | |
| 834 | ||
| 1325 | 835 | ndata->channels = g_slist_remove(ndata->channels, channel); |
| 1323 | 836 | g_free(channel->name); |
| 837 | g_free(channel); | |
| 838 | ||
| 839 | } | |
| 840 | ||
| 841 | static void nap_chat_send(struct gaim_connection *gc, int id, char *message) | |
| 842 | { | |
| 843 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 844 | struct nap_channel *channel = NULL; | |
| 845 | gchar buf[NAP_BUF_LEN]; | |
| 846 | ||
| 847 | channel = find_channel_by_id(gc, id); | |
| 848 | ||
| 849 | if (!channel) { | |
| 850 | /* This shouldn't happen */ | |
| 851 | return; | |
| 852 | } | |
| 853 | ||
| 854 | g_snprintf(buf, NAP_BUF_LEN, "%s %s", channel->name, message); | |
| 855 | nap_write_packet(gc, 0x192, buf); | |
| 856 | ||
| 857 | } | |
| 858 | ||
| 859 | static void nap_add_buddy(struct gaim_connection *gc, char *name) | |
| 860 | { | |
| 861 | nap_write_packet(gc, 0xCF, name); | |
| 862 | } | |
| 863 | ||
| 864 | static void nap_remove_buddy(struct gaim_connection *gc, char *name) | |
| 865 | { | |
| 866 | nap_write_packet(gc, 0x12F, name); | |
| 867 | } | |
| 868 | ||
| 869 | static void nap_close(struct gaim_connection *gc) | |
| 870 | { | |
| 871 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 872 | gchar buf[NAP_BUF_LEN]; | |
| 873 | struct nap_channel *channel; | |
| 1422 | 874 | struct browse_window *browse; |
| 875 | struct nap_file_request *req; | |
| 1323 | 876 | GSList *channels = ndata->channels; |
| 1422 | 877 | GSList *requests = ndata->requests; |
| 878 | ||
| 1323 | 879 | if (gc->inpa) |
| 880 | gdk_input_remove(gc->inpa); | |
| 881 | ||
| 1325 | 882 | while (ndata->channels) { |
| 883 | channel = (struct nap_channel *)ndata->channels->data; | |
| 1323 | 884 | g_free(channel->name); |
| 1325 | 885 | ndata->channels = g_slist_remove(ndata->channels, channel); |
| 1323 | 886 | g_free(channel); |
| 887 | } | |
| 1325 | 888 | |
| 1422 | 889 | while (ndata->browses) { |
| 890 | browse = (struct browse_window *)ndata->browses->data; | |
| 891 | g_free(browse->name); | |
| 892 | gtk_widget_destroy(browse->window); | |
| 893 | ndata->browses = g_slist_remove(ndata->browses, browse); | |
| 894 | g_free(browse); | |
| 895 | } | |
| 896 | ||
| 897 | while (ndata->requests) { | |
| 898 | req = (struct nap_file_request *)ndata->requests->data; | |
| 899 | g_free(req->name); | |
| 900 | g_free(req->file); | |
| 901 | if (req->inpa) { | |
| 902 | gdk_input_remove(req->inpa); | |
| 903 | } | |
| 904 | ndata->requests = g_slist_remove(ndata->requests, req); | |
| 905 | g_free(req); | |
| 906 | ||
| 907 | } | |
| 908 | ||
| 1325 | 909 | free(gc->proto_data); |
| 1323 | 910 | } |
| 911 | ||
| 912 | static void nap_add_buddies(struct gaim_connection *gc, GList *buddies) | |
| 913 | { | |
| 914 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 915 | gchar buf[NAP_BUF_LEN]; | |
| 916 | int n = 0; | |
| 917 | ||
| 918 | while (buddies) { | |
| 919 | nap_write_packet(gc, 0xd0, (char *)buddies->data); | |
| 920 | buddies = buddies -> next; | |
| 921 | } | |
| 922 | } | |
| 923 | ||
| 1338 | 924 | static void nap_draw_new_user(GtkWidget *box) |
| 925 | { | |
| 926 | GtkWidget *label; | |
| 927 | ||
| 928 | label = gtk_label_new(_("Napster registration is currently under development")); | |
| 929 | ||
| 930 | gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
| 931 | gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 5); | |
| 932 | gtk_widget_show(label); | |
| 933 | } | |
| 934 | ||
| 1422 | 935 | |
| 936 | void nap_send_browse(GtkObject *w, char *who) | |
| 937 | { | |
| 938 | struct gaim_connection *gc = (struct gaim_connection *)gtk_object_get_user_data(w); | |
| 939 | struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
| 940 | gchar buf[NAP_BUF_LEN]; | |
| 941 | ||
| 942 | g_snprintf(buf, NAP_BUF_LEN, "%s", who); | |
| 943 | nap_write_packet(gc, 0xd3, buf); | |
| 944 | } | |
| 945 | ||
| 946 | static void nap_action_menu(GtkWidget *menu, struct gaim_connection *gc, char *who) | |
| 947 | { | |
| 948 | GtkWidget *button; | |
| 949 | ||
| 950 | button = gtk_menu_item_new_with_label("List Files"); | |
| 951 | gtk_signal_connect(GTK_OBJECT(button), "activate", GTK_SIGNAL_FUNC(nap_send_browse), who); | |
| 952 | gtk_object_set_user_data(GTK_OBJECT(button), gc); | |
| 953 | gtk_menu_append(GTK_MENU(menu), button); | |
| 954 | gtk_widget_show(button); | |
| 955 | } | |
| 956 | ||
| 1327 | 957 | static char** nap_list_icon(int uc) |
| 958 | { | |
| 959 | return napster_xpm; | |
| 960 | } | |
| 961 | ||
| 1323 | 962 | static struct prpl *my_protocol = NULL; |
| 963 | ||
| 964 | void nap_init(struct prpl *ret) | |
| 965 | { | |
| 966 | ret->protocol = PROTO_NAPSTER; | |
| 967 | ret->name = nap_name; | |
| 1327 | 968 | ret->list_icon = nap_list_icon; |
| 1422 | 969 | ret->action_menu = nap_action_menu; |
| 1323 | 970 | ret->user_opts = NULL; |
| 971 | ret->login = nap_login; | |
| 972 | ret->close = nap_close; | |
| 973 | ret->send_im = nap_send_im; | |
| 974 | ret->set_info = NULL; | |
| 975 | ret->get_info = NULL; | |
| 976 | ret->set_away = NULL; | |
| 977 | ret->get_away_msg = NULL; | |
| 978 | ret->set_dir = NULL; | |
| 979 | ret->get_dir = NULL; | |
| 980 | ret->dir_search = NULL; | |
| 981 | ret->set_idle = NULL; | |
| 982 | ret->change_passwd = NULL; | |
| 983 | ret->add_buddy = nap_add_buddy; | |
| 984 | ret->add_buddies = nap_add_buddies; | |
| 985 | ret->remove_buddy = nap_remove_buddy; | |
| 986 | ret->add_permit = NULL; | |
| 987 | ret->rem_permit = NULL; | |
| 988 | ret->add_deny = NULL; | |
| 989 | ret->rem_deny = NULL; | |
| 990 | ret->warn = NULL; | |
| 991 | ret->accept_chat = NULL; | |
| 992 | ret->join_chat = nap_join_chat; | |
| 993 | ret->chat_invite = NULL; | |
| 994 | ret->chat_leave = nap_chat_leave; | |
| 995 | ret->chat_whisper = NULL; | |
| 996 | ret->chat_send = nap_chat_send; | |
| 997 | ret->keepalive = NULL; | |
| 1338 | 998 | ret->draw_new_user = nap_draw_new_user; |
| 1323 | 999 | |
| 1000 | my_protocol = ret; | |
| 1001 | } | |
| 1002 | ||
| 1003 | char *gaim_plugin_init(GModule * handle) | |
| 1004 | { | |
| 1005 | load_protocol(nap_init); | |
| 1006 | return NULL; | |
| 1007 | } | |
| 1008 | ||
| 1009 | void gaim_plugin_remove() | |
| 1010 | { | |
| 1011 | struct prpl *p = find_prpl(PROTO_NAPSTER); | |
| 1012 | if (p == my_protocol) | |
| 1013 | unload_protocol(p); | |
| 1014 | } |