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