comparison src/protocols/napster/napster.c @ 2086:424a40f12a6c

[gaim-migrate @ 2096] moving protocols from plugins/ to src/protocols. making it so that you can select which protocols are compiled statically. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Tue, 31 Jul 2001 01:00:39 +0000
parents
children b66aca8e8dce
comparison
equal deleted inserted replaced
2085:7ebb4322f89b 2086:424a40f12a6c
1 /*
2 * gaim - Napster Protocol Plugin
3 *
4 * Copyright (C) 2000-2001, 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 <sys/types.h>
38 #include <fcntl.h>
39 #include <ctype.h>
40 #include "multi.h"
41 #include "prpl.h"
42 #include "gaim.h"
43 #include "proxy.h"
44 #include "pixmaps/napster.xpm"
45
46 #define NAP_BUF_LEN 4096
47
48 GSList *nap_connections = NULL;
49
50 static unsigned int chat_id = 0;
51
52 struct search_window {
53 GtkWidget *window;
54 GtkWidget *list;
55 };
56
57 struct browse_window {
58 GtkWidget *window;
59 GtkWidget *list;
60 struct gaim_connection *gc;
61 char *name;
62 };
63
64 struct nap_download_box {
65 GtkWidget *window;
66 GtkWidget *ok;
67 GtkWidget *entry;
68 gchar *who;
69 };
70
71 struct nap_channel {
72 unsigned int id;
73 gchar *name;
74 };
75
76 struct nap_file_request {
77 gchar *name;
78 gchar *file;
79 int fd;
80 long size;
81 long total;
82 int status;
83 int inpa;
84 FILE *mp3;
85 GtkWidget *window;
86 GtkWidget *progress;
87 GtkWidget *ok;
88 GtkWidget *cancel;
89 struct gaim_connection *gc;
90 };
91
92 struct nap_data {
93 int fd;
94 int inpa;
95
96 gchar *email;
97 GSList *channels;
98 GSList *requests;
99 GSList *browses;
100 };
101
102 static struct search_window *search_dialog = NULL;
103
104 static char *nap_name()
105 {
106 return "Napster";
107 }
108
109
110 /* FIXME: Make this use va_arg stuff */
111 static void nap_write_packet(struct gaim_connection *gc, unsigned short command, char *message)
112 {
113 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
114 unsigned short size;
115
116 size = strlen(message);
117 write(ndata->fd, &size, 2);
118 write(ndata->fd, &command, 2);
119 write(ndata->fd, message, size);
120
121 }
122
123 static void nap_send_download_req(struct gaim_connection *gc, char *who, char *file)
124 {
125 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
126 gchar buf[NAP_BUF_LEN];
127
128 g_snprintf(buf, NAP_BUF_LEN, "%s \"%s\"", who, file);
129
130 printf("%s\n", buf);
131 printf("%d\n", ndata->fd);
132 nap_write_packet(gc, 0xCB, buf);
133 }
134
135 // FIXME: These next two windows should really be together
136 // and should use the same clist style look too.
137
138 static void nap_handle_download(GtkCList *clist, gint row, gint col, GdkEventButton *event, gpointer user_data)
139 {
140 gchar *results;
141 struct browse_window *bw = (struct browse_window *)user_data;
142
143 gtk_clist_get_text(GTK_CLIST(clist), row, 0, &results);
144
145 nap_send_download_req(bw->gc, bw->name, results);
146
147 }
148
149 static void nap_handle_download_search(GtkCList *clist, gint row, gint col, GdkEventButton *event, gpointer user_data)
150 {
151 gchar *filename;
152 gchar *nick;
153
154 struct gaim_connection *gc = (struct gaim_connection *)user_data;
155
156 filename = (gchar *)gtk_clist_get_row_data(GTK_CLIST(clist), row);
157
158 gtk_clist_get_text(GTK_CLIST(clist), row, 1, &nick);
159
160 printf("Trying to download: %s from %s\n", filename, nick);
161 nap_send_download_req(gc, nick, filename);
162 }
163
164 static struct browse_window *browse_window_new(struct gaim_connection *gc, char *name)
165 {
166 struct browse_window *browse = g_new0(struct browse_window, 1);
167 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
168
169 browse->window = gtk_window_new(GTK_WINDOW_DIALOG);
170 browse->name = g_strdup(name);
171 browse->list = gtk_clist_new(1);
172 browse->gc = gc;
173
174 gtk_widget_show(browse->list);
175 gtk_container_add(GTK_CONTAINER(browse->window), browse->list);
176
177 gtk_widget_set_usize(GTK_WIDGET(browse->window), 300, 250);
178 gtk_widget_show(browse->window);
179
180 /*FIXME: I dont like using select-row. Im lazy. Ill fix it later */
181 gtk_signal_connect(GTK_OBJECT(browse->list), "select-row", GTK_SIGNAL_FUNC(nap_handle_download), browse);
182
183 ndata->browses = g_slist_append(ndata->browses, browse);
184
185 return browse;
186 }
187
188 static void browse_window_add_file(struct browse_window *bw, char *name)
189 {
190 char *fn[1];
191 fn[0] = strdup(name);
192 printf("User '%s' has file '%s'\n", bw->name, name);
193 gtk_clist_append(GTK_CLIST(bw->list), fn);
194
195 free(fn[0]);
196 }
197
198 static struct browse_window *find_browse_window_by_name(struct gaim_connection *gc, char *name)
199 {
200 struct browse_window *browse;
201 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
202 GSList *browses;
203
204 browses = ndata->browses;
205
206 while (browses) {
207 browse = (struct browse_window *)browses->data;
208
209 if (browse) {
210 if (!g_strcasecmp(name, browse->name)) {
211 return browse;
212 }
213 }
214 browses = g_slist_next(browses);
215 }
216
217 return NULL;
218 }
219
220 static void nap_send_im(struct gaim_connection *gc, char *who, char *message, int away)
221 {
222 gchar buf[NAP_BUF_LEN];
223
224 g_snprintf(buf, NAP_BUF_LEN, "%s %s", who, message);
225 nap_write_packet(gc, 0xCD, buf);
226 }
227
228 static struct nap_channel *find_channel_by_name(struct gaim_connection *gc, char *name)
229 {
230 struct nap_channel *channel;
231 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
232 GSList *channels;
233
234 channels = ndata->channels;
235
236 while (channels) {
237 channel = (struct nap_channel *)channels->data;
238
239 if (channel) {
240 if (!g_strcasecmp(name, channel->name)) {
241 return channel;
242 }
243 }
244 channels = g_slist_next(channels);
245 }
246
247 return NULL;
248 }
249
250 static struct nap_channel *find_channel_by_id(struct gaim_connection *gc, int id)
251 {
252 struct nap_channel *channel;
253 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
254 GSList *channels;
255
256 channels = ndata->channels;
257
258 while (channels) {
259 channel = (struct nap_channel *)channels->data;
260 if (id == channel->id) {
261 return channel;
262 }
263
264 channels = g_slist_next(channels);
265 }
266
267 return NULL;
268 }
269
270 static struct conversation *find_conversation_by_id(struct gaim_connection *gc, int id)
271 {
272 GSList *bc = gc->buddy_chats;
273 struct conversation *b = NULL;
274
275 while (bc) {
276 b = (struct conversation *)bc->data;
277 if (id == b->id) {
278 break;
279 }
280 bc = bc->next;
281 b = NULL;
282 }
283
284 return b;
285 }
286
287 /* This is a strange function. I smoke too many bad bad things :-) */
288 static struct nap_file_request * find_request_by_fd(struct gaim_connection *gc, int fd)
289 {
290 struct nap_file_request *req;
291 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
292 GSList *requests;
293
294 requests = ndata->requests;
295
296 while (requests) {
297 req = (struct nap_file_request *)requests->data;
298
299 if (req) {
300 if (req->fd == fd)
301 return req;
302 }
303 requests = g_slist_next(requests);
304 }
305
306 return NULL;
307 }
308
309 static void nap_ctc_callback(gpointer data, gint source, GdkInputCondition condition)
310 {
311 struct gaim_connection *gc = (struct gaim_connection *)data;
312 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
313 struct nap_file_request *req;
314 unsigned char *buf;
315 int i = 0;
316
317 req = find_request_by_fd(gc, source);
318 if (!req) /* Something bad happened */
319 return;
320
321 buf = (char *)malloc(sizeof(char) * (NAP_BUF_LEN + 1));
322
323 if (req->status == 0)
324 {
325 int j;
326 gchar tmp[32];
327 long filesize;
328 gchar **parse_name;
329 gchar path[2048];
330 GtkWidget *hbox;
331 GtkWidget *vbox;
332 GtkWidget *label;
333 gchar *buf2;
334
335 recv(source, buf, 1, 0);
336
337 /* We should receive a '1' upon connection */
338 if (buf[0] != '1')
339 {
340 do_error_dialog("Uh Oh", "Uh Oh");
341 gdk_input_remove(req->inpa);
342 ndata->requests = g_slist_remove(ndata->requests, req);
343 g_free(req->name);
344 g_free(req->file);
345 close(source);
346 g_free(req);
347 free(buf);
348 return;
349 }
350
351 /* Lets take a peek at the awaiting data */
352 i = recv(source, buf, NAP_BUF_LEN, MSG_PEEK);
353 buf[i] = 0; /* Make sure that we terminate our string */
354
355 /* Looks like the uploader sent the proper data. Let's see how big the
356 * file is */
357
358 for (j = 0, i = 0; isdigit(buf[i]); i++, j++)
359 {
360 tmp[j] = buf[i];
361 }
362 tmp[j] = 0;
363 filesize = atol(tmp);
364
365 /* Save the size of the file */
366 req->total = filesize;
367
368 /* If we have a zero file size then something bad happened */
369 if (filesize == 0) {
370 gdk_input_remove(req->inpa);
371 ndata->requests = g_slist_remove(ndata->requests, req);
372 g_free(req->name);
373 g_free(req->file);
374 g_free(req);
375 free(buf);
376 close(source);
377 return;
378 }
379
380 /* Now that we've done that, let's go ahead and read that
381 * data to get it out of the way */
382 recv(source, buf, strlen(tmp), 0);
383
384 /* Now, we should tell the server that we're download something */
385 nap_write_packet(gc, 0xda, "\n");
386
387 req->status = 1;
388
389 /* FIXME: We dont want to force the file name. I'll parse this
390 * later */
391
392 parse_name = g_strsplit(req->file, "\\", 0);
393 g_snprintf(path, sizeof(path), "%s/%s", getenv("HOME"), parse_name[sizeof(parse_name)]);
394 printf("Gonna try to save to: %s\n", path);
395 g_strfreev(parse_name);
396
397 req->mp3 = fopen(path, "w");
398
399 req->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
400
401 vbox = gtk_vbox_new(FALSE, 5);
402
403 buf2 = (gchar *)g_malloc(sizeof(gchar) * (strlen(req->file) + 33));
404 g_snprintf(buf2, strlen(req->file) + 32, "Downloading File: %s", req->file);
405 label = gtk_label_new(buf2);
406 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
407
408 req->progress = gtk_progress_bar_new();
409 gtk_progress_bar_update(GTK_PROGRESS_BAR(req->progress), 0);
410 gtk_progress_configure(GTK_PROGRESS(req->progress), 0, 0, (float)req->total/(float)1024);
411 gtk_progress_set_format_string(GTK_PROGRESS(req->progress), "%P%% (%VKB / %UKB)");
412 gtk_progress_set_show_text(GTK_PROGRESS(req->progress), TRUE);
413 gtk_box_pack_start(GTK_BOX(vbox), req->progress, FALSE, FALSE, 5);
414
415 hbox = gtk_hbox_new(TRUE, 5);
416
417 req->ok = gtk_button_new_with_label("Ok");
418 req->cancel = gtk_button_new_with_label("Cancel");
419
420 gtk_box_pack_end(GTK_BOX(hbox), req->cancel, FALSE, FALSE, 5);
421 gtk_box_pack_end(GTK_BOX(hbox), req->ok, FALSE, FALSE, 5);
422
423 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
424
425 gtk_container_add(GTK_CONTAINER(req->window), vbox);
426
427 gtk_widget_show_all(req->window);
428
429 free(buf);
430 return;
431 }
432
433 /* Looks like our status isn't 1. It's safe to assume we're downloadin' */
434 i = recv(source, buf, NAP_BUF_LEN, 0);
435
436 req->size += i; /* Lets add up the total */
437
438 // printf("Downloaded %ld of %ld (%f)\n", req->size, req->total, (float)req->size/(float)req->total);
439
440 gtk_progress_bar_update(GTK_PROGRESS_BAR(req->progress), (float)req->size/(float)req->total);
441
442 while (gtk_events_pending())
443 gtk_main_iteration();
444
445 fwrite(buf, i, sizeof(char), req->mp3);
446
447 free(buf);
448
449 if (req->size >= req->total) {
450 printf("Download complete.\n");
451 nap_write_packet(gc, 0xdb, "\n"); /* Tell the server we're finished */
452 gdk_input_remove(req->inpa);
453
454 ndata->requests = g_slist_remove(ndata->requests, req);
455
456 if (req->name != NULL)
457 g_free(req->name);
458
459 if (req->file != NULL)
460 g_free(req->file);
461
462 g_free(req);
463 fclose(req->mp3);
464 close(source);
465 }
466 }
467
468 static void nap_get_file_connect(gpointer data, gint source, GdkInputCondition cond)
469 {
470 char buf[NAP_BUF_LEN];
471 struct nap_file_request *req = data;
472 struct gaim_connection *gc = req->gc;
473 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
474
475 if (source < 0) {
476 do_error_dialog("Error connecting to user", "Gaim: Napster error");
477 g_free(req->name);
478 g_free(req->file);
479 g_free(req);
480 return;
481 }
482
483 if (req->fd != source)
484 req->fd = source;
485
486 send(req->fd, "GET", 3, 0);
487
488 /* Send our request to the user */
489 g_snprintf(buf, sizeof(buf), "%s \"%s\" 0", gc->username, req->file);
490
491 send(req->fd, buf, strlen(buf), 0);
492
493 /* Add our request */
494 ndata->requests = g_slist_append(ndata->requests, req);
495
496 /* And start monitoring */
497 req->inpa = gdk_input_add(req->fd, GDK_INPUT_READ, nap_ctc_callback, gc);
498 }
499
500 static void nap_get_file(struct gaim_connection *gc, gchar *user, gchar *file, gchar *host, unsigned int port)
501 {
502 struct nap_file_request *req = g_new0(struct nap_file_request, 1);
503
504 req->name = g_strdup(user);
505 req->file = g_strdup(file);
506 req->size = 0;
507 req->status = 0;
508 req->total = 0;
509 req->gc = gc;
510
511 /* Make a connection with the server */
512 req->fd = proxy_connect(host, port, nap_get_file_connect, req);
513 if (req->fd < 0) {
514 do_error_dialog("Error connecting to user", "Gaim: Napster error");
515 g_free(req->name);
516 g_free(req->file);
517 g_free(req);
518 }
519 }
520
521 static void nap_callback(gpointer data, gint source, GdkInputCondition condition)
522 {
523 struct gaim_connection *gc = data;
524 struct nap_data *ndata = gc->proto_data;
525 gchar *buf;
526 unsigned short header[2];
527 int len;
528 int command;
529 gchar **res;
530
531 recv(source, header, 4, 0);
532 // read(source, header, 4);
533 len = header[0];
534 command = header[1];
535
536 buf = (gchar *)g_malloc(sizeof(gchar) * (len + 1));
537
538 // read(source, buf, len);
539 recv(source, buf, len, 0);
540
541 buf[len] = 0;
542
543 printf("DEBUG: %s\n", buf);
544
545 if (command == 0xd6) {
546 res = g_strsplit(buf, " ", 0);
547 /* Do we want to report what the users are doing? */
548 printf("users: %s, files: %s, size: %sGB\n", res[0], res[1], res[2]);
549 g_strfreev(res);
550 free(buf);
551 return;
552 }
553
554 if (command == 0x26d) {
555 /* Do we want to use the MOTD? */
556 free(buf);
557 return;
558 }
559
560 if (command == 0xCD) {
561 res = g_strsplit(buf, " ", 1);
562 serv_got_im(gc, res[0], res[1], 0, time((time_t)NULL));
563 g_strfreev(res);
564 free(buf);
565 return;
566 }
567
568 if (command == 0x195) {
569 struct nap_channel *channel;
570
571 channel = find_channel_by_name(gc, buf);
572
573 if (!channel) {
574 chat_id++;
575
576 channel = g_new0(struct nap_channel, 1);
577
578 channel->id = chat_id;
579 channel->name = g_strdup(buf);
580
581 ndata->channels = g_slist_append(ndata->channels, channel);
582
583 serv_got_joined_chat(gc, chat_id, buf);
584 }
585
586 free(buf);
587 return;
588 }
589
590 if (command == 0x198 || command == 0x196) {
591 struct nap_channel *channel;
592 struct conversation *convo;
593 gchar **res;
594
595 res = g_strsplit(buf, " ", 0);
596
597 channel = find_channel_by_name(gc, res[0]);
598 convo = find_conversation_by_id(gc, channel->id);
599
600 add_chat_buddy(convo, res[1]);
601
602 g_strfreev(res);
603
604 free(buf);
605 return;
606 }
607
608 if (command == 0x197) {
609 struct nap_channel *channel;
610 struct conversation *convo;
611 gchar **res;
612
613 res = g_strsplit(buf, " ", 0);
614
615 channel = find_channel_by_name(gc, res[0]);
616 convo = find_conversation_by_id(gc, channel->id);
617
618 remove_chat_buddy(convo, res[1]);
619
620 g_strfreev(res);
621 free(buf);
622 return;
623 }
624
625 if (command == 0x193) {
626 gchar **res;
627 struct nap_channel *channel;
628
629 res = g_strsplit(buf, " ", 2);
630
631 channel = find_channel_by_name(gc, res[0]);
632
633 if (channel)
634 serv_got_chat_in(gc, channel->id, res[1], 0, res[2], time((time_t)NULL));
635
636 g_strfreev(res);
637 free(buf);
638 return;
639 }
640
641 if (command == 0x194) {
642 do_error_dialog(buf, "Gaim: Napster Error");
643 free(buf);
644 return;
645 }
646
647 if (command == 0x12e) {
648 gchar buf2[NAP_BUF_LEN];
649
650 g_snprintf(buf2, NAP_BUF_LEN, "Unable to add '%s' to your hotlist", buf);
651 do_error_dialog(buf2, "Gaim: Napster Error");
652
653 free(buf);
654 return;
655
656 }
657
658 if (command == 0x191) {
659 struct nap_channel *channel;
660
661 channel = find_channel_by_name(gc, buf);
662
663 if (!channel) /* I'm not sure how this would happen =) */
664 return;
665
666 serv_got_chat_left(gc, channel->id);
667 ndata->channels = g_slist_remove(ndata->channels, channel);
668
669 free(buf);
670 return;
671
672 }
673
674 if (command == 0xd1) {
675 gchar **res;
676
677 res = g_strsplit(buf, " ", 0);
678
679 serv_got_update(gc, res[0], 1, 0, 0, 0, 0, 0);
680
681 g_strfreev(res);
682 free(buf);
683 return;
684 }
685
686 if (command == 0xd2) {
687 serv_got_update(gc, buf, 0, 0, 0, 0, 0, 0);
688 free(buf);
689 return;
690 }
691
692 if (command == 0xd4) {
693 /* Looks like we're getting a browse response */
694 gchar user[64];
695 gchar file[2048];
696 struct browse_window *bw = NULL;
697
698 int i,j;
699
700 for (i = 0, j = 0; buf[i] != ' '; i++, j++)
701 {
702 user[j] = buf[i];
703 }
704 user[j] = 0; i++; i++;
705
706 for (j = 0; buf[i] != '\"'; i++, j++)
707 {
708 file[j] = buf[i];
709 }
710 file[j] = 0;
711
712 bw = find_browse_window_by_name(gc, user);
713 if (!bw)
714 {
715 /* If a browse window isn't found, let's create one */
716 bw = browse_window_new(gc, user);
717 }
718
719 browse_window_add_file(bw, file);
720
721 free(buf);
722 return;
723
724 }
725
726 if (command == 0xc9) {
727 /* We've received a search response */
728 gchar *file = (gchar *)g_malloc(sizeof(gchar) * (NAP_BUF_LEN+1));
729 gchar *tmp;
730 gchar rest[NAP_BUF_LEN];
731 gchar *data[5];
732 gchar **parse_name;
733
734 int i, j;
735
736 for (i = 1, j = 0; ((buf[i] != '\"') && (buf[i] != 0)); i++, j++)
737 {
738 file[j] = buf[i];
739 }
740
741 file[j] = 0; i++;
742
743 tmp = (gchar *)g_malloc(sizeof(gchar) * (strlen(file) + 1));
744 strcpy(tmp, rindex(file, '\\')+1);
745
746 strcpy(rest, buf+i);
747
748 parse_name = g_strsplit(rest, " ", 0);
749
750 data[0] = g_strdup(tmp);
751 data[1] = g_strdup(parse_name[6]);
752 data[2] = g_strdup(parse_name[2]);
753 data[3] = g_strdup(parse_name[3]);
754 data[4] = g_strdup(parse_name[8]);
755
756 // printf("File: %s, 1: %s, 2: %s, 3: %s\n", data[0], data[1], data[2], data[3]);
757 i = gtk_clist_append(GTK_CLIST(search_dialog->list), data);
758
759 gtk_clist_set_row_data(GTK_CLIST(search_dialog->list), i, file);
760 g_strfreev(parse_name);
761 // g_free(file);
762 g_free(tmp);
763 g_free(buf);
764 return;
765 }
766
767 if (command == 0xca)
768 {
769 /* End of search */
770 g_free(buf);
771 return;
772 }
773
774 if (command == 0x12d) {
775 /* Our buddy was added successfully */
776 free(buf);
777 return;
778 }
779
780 if (command == 0x2ec) {
781 /* Looks like someone logged in as us! =-O */
782 free(buf);
783
784 signoff(gc);
785 return;
786 }
787
788 if (command == 0xcc) {
789 /* We received a Download ACK from a user. The way this is printed is kind of
790 * strange so we'll need to parse this one ourselves. */
791
792 gchar user[64];
793 gchar file[2048];
794 gchar hoststr[16];
795 gchar portstr[16];
796 int i,j;
797
798 for (i = 0, j = 0; buf[i] != ' '; i++, j++)
799 {
800 user[j] = buf[i];
801 }
802 user[j] = 0; i++;
803
804 for (j = 0; buf[i] != ' '; i++, j++)
805 {
806 hoststr[j] = buf[i];
807 }
808 hoststr[j] = 0; i++;
809
810 for (j = 0; buf[i] != ' '; i++, j++)
811 {
812 portstr[j] = buf[i];
813 }
814 portstr[j] = 0; i++;
815
816 i++; /* We do this to ignore the first quotation mark */
817
818 for (j = 0; buf[i] != '\"'; i++, j++)
819 {
820 file[j] = buf[i];
821 }
822 file[j] = 0;
823
824 /* Aaight. We dont need nuttin' else. Let's download the file */
825 nap_get_file(gc, user, file, hoststr, atoi(portstr));
826
827 free(buf);
828
829 return;
830 }
831
832 printf("NAP: [COMMAND: 0x%04x] %s\n", command, buf);
833
834 free(buf);
835 }
836
837
838 static void nap_login_callback(gpointer data, gint source, GdkInputCondition condition)
839 {
840 struct gaim_connection *gc = data;
841 struct nap_data *ndata = gc->proto_data;
842 gchar buf[NAP_BUF_LEN];
843 unsigned short header[2];
844 int len;
845 int command;
846
847 read(source, header, 4);
848 len = header[0];
849 command = header[1];
850
851 read(source, buf, len);
852 buf[len] = 0;
853
854 /* If we have some kind of error, get outta here */
855 if (command == 0x00)
856 {
857 do_error_dialog(buf, "Gaim: Napster Error");
858 gdk_input_remove(ndata->inpa);
859 ndata->inpa = 0;
860 close(source);
861 signoff(gc);
862 return;
863 }
864
865 if (command == 0x03) {
866 printf("Registered with E-Mail address of: %s\n", buf);
867 ndata->email = g_strdup(buf);
868
869 /* Remove old inpa, add new one */
870 gdk_input_remove(ndata->inpa);
871 ndata->inpa = 0;
872 gc->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_callback, gc);
873
874 /* Our signon is complete */
875 account_online(gc);
876 serv_finish_login(gc);
877
878 if (bud_list_cache_exists(gc))
879 do_import(NULL, gc);
880
881 return;
882 }
883 }
884
885
886 static void nap_login_connect(gpointer data, gint source, GdkInputCondition cond)
887 {
888 struct gaim_connection *gc = data;
889 struct nap_data *ndata = gc->proto_data;
890 char buf[NAP_BUF_LEN];
891
892 if (source < 0) {
893 hide_login_progress(gc, "Unable to connect");
894 signoff(gc);
895 return;
896 }
897
898 if (ndata->fd != source)
899 ndata->fd = source;
900
901 /* And write our signon data */
902 g_snprintf(buf, NAP_BUF_LEN, "%s %s 0 \"gaimster\" 0", gc->username, gc->password);
903 nap_write_packet(gc, 0x02, buf);
904
905 /* And set up the input watcher */
906 ndata->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_login_callback, gc);
907 }
908
909
910 static void nap_login(struct aim_user *user)
911 {
912 struct gaim_connection *gc = new_gaim_conn(user);
913 struct nap_data *ndata = gc->proto_data = g_new0(struct nap_data, 1);
914
915 ndata->fd = proxy_connect("64.124.41.187", 8888, nap_login_connect, gc);
916 if (ndata->fd < 0) {
917 hide_login_progress(gc, "Unable to connect");
918 signoff(gc);
919 }
920 }
921
922 static void nap_join_chat(struct gaim_connection *gc, int id, char *name)
923 {
924 gchar buf[NAP_BUF_LEN];
925
926 /* Make sure the name has a # preceeding it */
927 if (name[0] != '#')
928 g_snprintf(buf, NAP_BUF_LEN, "#%s", name);
929 else
930 g_snprintf(buf, NAP_BUF_LEN, "%s", name);
931
932 nap_write_packet(gc, 0x190, buf);
933 }
934
935 static void nap_chat_leave(struct gaim_connection *gc, int id)
936 {
937 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
938 struct nap_channel *channel = NULL;
939
940 channel = find_channel_by_id(gc, id);
941
942 if (!channel) /* Again, I'm not sure how this would happen */
943 return;
944
945 nap_write_packet(gc, 0x191, channel->name);
946
947 ndata->channels = g_slist_remove(ndata->channels, channel);
948 g_free(channel->name);
949 g_free(channel);
950
951 }
952
953 static void nap_chat_send(struct gaim_connection *gc, int id, char *message)
954 {
955 struct nap_channel *channel = NULL;
956 gchar buf[NAP_BUF_LEN];
957
958 channel = find_channel_by_id(gc, id);
959
960 if (!channel) {
961 /* This shouldn't happen */
962 return;
963 }
964
965 g_snprintf(buf, NAP_BUF_LEN, "%s %s", channel->name, message);
966 nap_write_packet(gc, 0x192, buf);
967
968 }
969
970 static void nap_add_buddy(struct gaim_connection *gc, char *name)
971 {
972 nap_write_packet(gc, 0xCF, name);
973 }
974
975 static void nap_remove_buddy(struct gaim_connection *gc, char *name)
976 {
977 nap_write_packet(gc, 0x12F, name);
978 }
979
980 static void nap_close(struct gaim_connection *gc)
981 {
982 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
983 struct nap_channel *channel;
984 struct browse_window *browse;
985 struct nap_file_request *req;
986
987 if (gc->inpa)
988 gdk_input_remove(gc->inpa);
989
990 while (ndata->channels) {
991 channel = (struct nap_channel *)ndata->channels->data;
992 g_free(channel->name);
993 ndata->channels = g_slist_remove(ndata->channels, channel);
994 g_free(channel);
995 }
996
997 while (ndata->browses) {
998 browse = (struct browse_window *)ndata->browses->data;
999 g_free(browse->name);
1000 gtk_widget_destroy(browse->window);
1001 ndata->browses = g_slist_remove(ndata->browses, browse);
1002 g_free(browse);
1003 }
1004
1005 while (ndata->requests) {
1006 req = (struct nap_file_request *)ndata->requests->data;
1007 g_free(req->name);
1008 g_free(req->file);
1009 if (req->inpa) {
1010 gdk_input_remove(req->inpa);
1011 }
1012 ndata->requests = g_slist_remove(ndata->requests, req);
1013 g_free(req);
1014
1015 }
1016
1017 free(gc->proto_data);
1018 }
1019
1020 static void nap_add_buddies(struct gaim_connection *gc, GList *buddies)
1021 {
1022 while (buddies) {
1023 nap_write_packet(gc, 0xd0, (char *)buddies->data);
1024 buddies = buddies -> next;
1025 }
1026 }
1027
1028 static void nap_draw_new_user(GtkWidget *box)
1029 {
1030 GtkWidget *label;
1031
1032 label = gtk_label_new(_("Napster registration is currently under development"));
1033
1034 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
1035 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 5);
1036 gtk_widget_show(label);
1037 }
1038
1039
1040 static void nap_send_browse(GtkObject *w, char *who)
1041 {
1042 struct gaim_connection *gc = (struct gaim_connection *)gtk_object_get_user_data(w);
1043 gchar buf[NAP_BUF_LEN];
1044
1045 g_snprintf(buf, NAP_BUF_LEN, "%s", who);
1046 nap_write_packet(gc, 0xd3, buf);
1047 }
1048
1049 static void nap_find_callback(GtkObject *w, GtkWidget *entry)
1050 {
1051 struct gaim_connection *gc = (struct gaim_connection *)gtk_object_get_user_data(w);
1052 const gchar *search;
1053 gchar buf[NAP_BUF_LEN];
1054
1055 search = gtk_entry_get_text(GTK_ENTRY(entry));
1056 g_snprintf(buf, NAP_BUF_LEN, "FILENAME CONTAINS \"%s\" MAX_RESULTS 50", search);
1057
1058 nap_write_packet(gc, 0xc8, buf);
1059 }
1060
1061 static void destroy_window(GtkObject *w, GtkWidget *win)
1062 {
1063 gtk_widget_destroy(win);
1064 }
1065
1066 static void nap_show_search(GtkObject *w, void *omit)
1067 {
1068 struct gaim_connection *gc = (struct gaim_connection *)gtk_object_get_user_data(w);
1069
1070 if (!search_dialog)
1071 {
1072 GtkWidget *window;
1073 GtkWidget *sw;
1074 GtkWidget *vbox;
1075 GtkWidget *hbox;
1076 GtkWidget *label;
1077 GtkWidget *button;
1078 GtkWidget *entry;
1079 GtkWidget *list;
1080 gchar *titles[5] = {"Mp3 Name", "Nick", "Size", "Bitrate", "Connection"};
1081 search_dialog = g_new0(struct search_window, 1);
1082
1083 window = gtk_window_new(GTK_WINDOW_DIALOG);
1084
1085 vbox = gtk_vbox_new(FALSE, 0);
1086
1087 /* First Line */
1088 hbox = gtk_hbox_new(FALSE, 5);
1089 label = gtk_label_new("Search for:");
1090 gtk_widget_show(label);
1091 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
1092
1093 entry = gtk_entry_new();
1094 gtk_widget_show(entry);
1095 gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 5);
1096
1097 button = gtk_button_new_with_label("Find");
1098 gtk_widget_show(button);
1099 gtk_object_set_user_data(GTK_OBJECT(button), gc);
1100 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(nap_find_callback), entry);
1101
1102 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5);
1103
1104 button = gtk_button_new_with_label("Cancel");
1105 gtk_widget_show(button);
1106 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5);
1107
1108
1109 gtk_widget_show(hbox);
1110 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
1111 /* End First List */
1112
1113 /* Second Line */
1114
1115 sw = gtk_scrolled_window_new(NULL, NULL);
1116
1117 list = gtk_clist_new_with_titles(5, titles);
1118
1119 gtk_clist_column_titles_show(GTK_CLIST(list));
1120
1121 gtk_signal_connect(GTK_OBJECT(list), "select-row", GTK_SIGNAL_FUNC(nap_handle_download_search), gc);
1122
1123 gtk_container_add(GTK_CONTAINER(sw), list);
1124 gtk_widget_show(list);
1125
1126 gtk_box_pack_start(GTK_BOX(vbox), sw, FALSE, FALSE, 5);
1127
1128 gtk_widget_show(sw);
1129
1130 /* End Second Line */
1131
1132 gtk_widget_show(vbox);
1133
1134 gtk_container_add(GTK_CONTAINER(window), vbox);
1135 gtk_container_set_border_width(GTK_CONTAINER(window), 10);
1136
1137 gtk_widget_set_usize(GTK_WIDGET(list), 500, 350);
1138 gtk_widget_set_usize(GTK_WIDGET(window), 500, 400);
1139
1140 gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy_window), window);
1141 gtk_widget_show(window);
1142
1143 search_dialog->window = window;
1144 search_dialog->list = list;
1145 }
1146
1147 gtk_widget_show(search_dialog->window);
1148 }
1149
1150 static void nap_buddy_menu(GtkWidget *menu, struct gaim_connection *gc, char *who)
1151 {
1152 GtkWidget *button;
1153
1154 button = gtk_menu_item_new_with_label("Browse Files");
1155 gtk_signal_connect(GTK_OBJECT(button), "activate", GTK_SIGNAL_FUNC(nap_send_browse), who);
1156 gtk_object_set_user_data(GTK_OBJECT(button), gc);
1157 gtk_menu_append(GTK_MENU(menu), button);
1158 gtk_widget_show(button);
1159
1160 button = gtk_menu_item_new_with_label("Search Napster");
1161 gtk_signal_connect(GTK_OBJECT(button), "activate", GTK_SIGNAL_FUNC(nap_show_search), NULL);
1162 gtk_object_set_user_data(GTK_OBJECT(button), gc);
1163 gtk_menu_append(GTK_MENU(menu), button);
1164 gtk_widget_show(button);
1165 }
1166
1167 static char** nap_list_icon(int uc)
1168 {
1169 return napster_xpm;
1170 }
1171
1172 static struct prpl *my_protocol = NULL;
1173
1174 void napster_init(struct prpl *ret)
1175 {
1176 ret->protocol = PROTO_NAPSTER;
1177 ret->name = nap_name;
1178 ret->list_icon = nap_list_icon;
1179 ret->buddy_menu = nap_buddy_menu;
1180 ret->user_opts = NULL;
1181 ret->login = nap_login;
1182 ret->close = nap_close;
1183 ret->send_im = nap_send_im;
1184 ret->set_info = NULL;
1185 ret->get_info = NULL;
1186 ret->set_away = NULL;
1187 ret->get_away_msg = NULL;
1188 ret->set_dir = NULL;
1189 ret->get_dir = NULL;
1190 ret->dir_search = NULL;
1191 ret->set_idle = NULL;
1192 ret->change_passwd = NULL;
1193 ret->add_buddy = nap_add_buddy;
1194 ret->add_buddies = nap_add_buddies;
1195 ret->remove_buddy = nap_remove_buddy;
1196 ret->add_permit = NULL;
1197 ret->rem_permit = NULL;
1198 ret->add_deny = NULL;
1199 ret->rem_deny = NULL;
1200 ret->warn = NULL;
1201 ret->accept_chat = NULL;
1202 ret->join_chat = nap_join_chat;
1203 ret->chat_invite = NULL;
1204 ret->chat_leave = nap_chat_leave;
1205 ret->chat_whisper = NULL;
1206 ret->chat_send = nap_chat_send;
1207 ret->keepalive = NULL;
1208 ret->draw_new_user = nap_draw_new_user;
1209
1210 my_protocol = ret;
1211 }
1212
1213 #ifndef STATIC
1214
1215 char *gaim_plugin_init(GModule * handle)
1216 {
1217 load_protocol(napster_init, sizeof(struct prpl));
1218 return NULL;
1219 }
1220
1221 void gaim_plugin_remove()
1222 {
1223 struct prpl *p = find_prpl(PROTO_NAPSTER);
1224 if (p == my_protocol)
1225 unload_protocol(p);
1226 }
1227
1228 char *name()
1229 {
1230 return "Napster";
1231 }
1232
1233 char *description()
1234 {
1235 return "Allows gaim to use the Napster protocol. Yes, kids, drugs are bad.";
1236 }
1237
1238 #endif