Mercurial > pidgin
annotate plugins/napster.c @ 1492:638b57b1801d
[gaim-migrate @ 1502]
bah.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 17 Feb 2001 00:08:45 +0000 |
parents | 55f67ef24683 |
children | 13bbf7b8f920 |
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); | |
425 gtk_progress_set_format_string(GTK_PROGRESS(req->progress), "%P %%"); | |
426 gtk_progress_set_show_text(GTK_PROGRESS(req->progress), TRUE); | |
427 gtk_box_pack_start(GTK_BOX(vbox), req->progress, FALSE, FALSE, 5); | |
428 | |
429 hbox = gtk_hbox_new(TRUE, 5); | |
430 | |
431 req->ok = gtk_button_new_with_label("Ok"); | |
432 req->cancel = gtk_button_new_with_label("Cancel"); | |
433 | |
434 gtk_box_pack_end(GTK_BOX(hbox), req->cancel, FALSE, FALSE, 5); | |
435 gtk_box_pack_end(GTK_BOX(hbox), req->ok, FALSE, FALSE, 5); | |
436 | |
437 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5); | |
438 | |
439 gtk_container_add(GTK_CONTAINER(req->window), vbox); | |
440 | |
441 gtk_widget_show_all(req->window); | |
442 | |
1422 | 443 free(buf); |
444 return; | |
445 } | |
446 | |
447 /* Looks like our status isn't 1. It's safe to assume we're downloadin' */ | |
448 i = recv(source, buf, NAP_BUF_LEN, 0); | |
449 | |
450 req->size += i; /* Lets add up the total */ | |
451 | |
1444 | 452 // printf("Downloaded %ld of %ld (%f)\n", req->size, req->total, (float)req->size/(float)req->total); |
453 | |
454 gtk_progress_bar_update(GTK_PROGRESS_BAR(req->progress), (float)req->size/(float)req->total); | |
455 | |
456 while (gtk_events_pending()) | |
457 gtk_main_iteration(); | |
1422 | 458 |
459 fwrite(buf, i, sizeof(char), req->mp3); | |
460 | |
461 free(buf); | |
462 | |
463 if (req->size >= req->total) { | |
464 printf("Download complete.\n"); | |
465 nap_write_packet(gc, 0xdb, "\n"); /* Tell the server we're finished */ | |
466 gdk_input_remove(req->inpa); | |
1444 | 467 |
1422 | 468 ndata->requests = g_slist_remove(ndata->requests, req); |
1444 | 469 |
470 if (req->name != NULL) | |
471 g_free(req->name); | |
472 | |
473 if (req->file != NULL) | |
474 g_free(req->file); | |
475 | |
1422 | 476 g_free(req); |
477 fclose(req->mp3); | |
478 close(source); | |
479 } | |
480 } | |
481 | |
482 void nap_get_file(struct gaim_connection *gc, gchar *user, gchar *file, unsigned long host, unsigned int port) | |
483 { | |
484 int fd; | |
485 struct sockaddr_in site; | |
486 char buf[NAP_BUF_LEN]; | |
487 int inpa; | |
488 struct nap_file_request *req = g_new0(struct nap_file_request, 1); | |
489 struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
490 | |
491 | |
492 site.sin_family = AF_INET; | |
493 site.sin_addr.s_addr = host; | |
494 site.sin_port = htons(port); | |
495 | |
496 fd = socket(AF_INET, SOCK_STREAM, 0); | |
497 | |
498 if (fd < 0) { | |
499 do_error_dialog("Error connecting to user", "Gaim: Napster error"); | |
500 return; | |
501 } | |
502 | |
503 /* Make a connection with the server */ | |
504 if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) { | |
505 do_error_dialog("Error connecting to user", "Gaim: Napster error"); | |
506 return; | |
507 } | |
508 | |
509 req->fd = fd; | |
510 req->name = g_strdup(user); | |
511 req->file = g_strdup(file); | |
512 req->size = 0; | |
513 req->status = 0; | |
514 req->total = 0; | |
515 | |
1444 | 516 send(fd, "GET", 3, 0); |
517 | |
1422 | 518 /* Send our request to the user */ |
1444 | 519 g_snprintf(buf, sizeof(buf), "%s \"%s\" 0",gc->username, file); |
520 | |
521 send(fd, buf, strlen(buf), 0); | |
1422 | 522 |
523 /* Add our request */ | |
524 ndata->requests = g_slist_append(ndata->requests, req); | |
525 | |
526 /* And start monitoring */ | |
527 req->inpa = gdk_input_add(fd, GDK_INPUT_READ, nap_ctc_callback, gc); | |
528 } | |
529 | |
1323 | 530 static void nap_callback(gpointer data, gint source, GdkInputCondition condition) |
531 { | |
532 struct gaim_connection *gc = data; | |
533 struct nap_data *ndata = gc->proto_data; | |
534 gchar *buf; | |
535 unsigned short header[2]; | |
536 int i = 0; | |
537 int len; | |
538 int command; | |
539 gchar **res; | |
540 | |
541 read(source, header, 4); | |
542 len = header[0]; | |
543 command = header[1]; | |
544 | |
545 buf = (gchar *)g_malloc(sizeof(gchar) * (len + 1)); | |
546 | |
547 read(source, buf, len); | |
548 | |
549 buf[len] = 0; | |
1427 | 550 |
551 printf("DEBUG: %s\n", buf); | |
1323 | 552 |
553 if (command == 0xd6) { | |
554 res = g_strsplit(buf, " ", 0); | |
555 /* Do we want to report what the users are doing? */ | |
556 printf("users: %s, files: %s, size: %sGB\n", res[0], res[1], res[2]); | |
557 g_strfreev(res); | |
558 free(buf); | |
559 return; | |
560 } | |
561 | |
562 if (command == 0x26d) { | |
563 /* Do we want to use the MOTD? */ | |
564 free(buf); | |
565 return; | |
566 } | |
567 | |
568 if (command == 0xCD) { | |
569 res = g_strsplit(buf, " ", 1); | |
570 serv_got_im(gc, res[0], res[1], 0); | |
571 g_strfreev(res); | |
572 free(buf); | |
573 return; | |
574 } | |
575 | |
576 if (command == 0x195) { | |
577 struct nap_channel *channel; | |
578 int id; | |
579 | |
580 channel = find_channel_by_name(gc, buf); | |
581 | |
582 if (!channel) { | |
583 chat_id++; | |
584 | |
585 channel = g_new0(struct nap_channel, 1); | |
586 | |
587 channel->id = chat_id; | |
588 channel->name = g_strdup(buf); | |
589 | |
590 ndata->channels = g_slist_append(ndata->channels, channel); | |
591 | |
592 serv_got_joined_chat(gc, chat_id, buf); | |
593 } | |
594 | |
595 free(buf); | |
596 return; | |
597 } | |
598 | |
599 if (command == 0x198 || command == 0x196) { | |
600 struct nap_channel *channel; | |
601 struct conversation *convo; | |
602 gchar **res; | |
603 | |
604 res = g_strsplit(buf, " ", 0); | |
605 | |
606 channel = find_channel_by_name(gc, res[0]); | |
607 convo = find_conversation_by_id(gc, channel->id); | |
608 | |
609 add_chat_buddy(convo, res[1]); | |
610 | |
611 g_strfreev(res); | |
612 | |
613 free(buf); | |
614 return; | |
615 } | |
616 | |
1325 | 617 if (command == 0x197) { |
618 struct nap_channel *channel; | |
619 struct conversation *convo; | |
620 gchar **res; | |
621 | |
622 res = g_strsplit(buf, " ", 0); | |
623 | |
624 channel = find_channel_by_name(gc, res[0]); | |
625 convo = find_conversation_by_id(gc, channel->id); | |
626 | |
627 remove_chat_buddy(convo, res[1]); | |
628 | |
629 g_strfreev(res); | |
630 free(buf); | |
631 return; | |
632 } | |
633 | |
1323 | 634 if (command == 0x193) { |
635 gchar **res; | |
636 struct nap_channel *channel; | |
637 | |
638 res = g_strsplit(buf, " ", 2); | |
639 | |
640 channel = find_channel_by_name(gc, res[0]); | |
641 | |
642 if (channel) | |
643 serv_got_chat_in(gc, channel->id, res[1], 0, res[2]); | |
644 | |
645 g_strfreev(res); | |
646 free(buf); | |
647 return; | |
648 } | |
649 | |
650 if (command == 0x194) { | |
651 do_error_dialog(buf, "Gaim: Napster Error"); | |
652 free(buf); | |
653 return; | |
654 } | |
655 | |
656 if (command == 0x12e) { | |
657 gchar buf2[NAP_BUF_LEN]; | |
658 | |
659 g_snprintf(buf2, NAP_BUF_LEN, "Unable to add '%s' to your hotlist", buf); | |
660 do_error_dialog(buf2, "Gaim: Napster Error"); | |
661 | |
662 free(buf); | |
663 return; | |
664 | |
665 } | |
666 | |
667 if (command == 0x191) { | |
668 struct nap_channel *channel; | |
669 | |
670 channel = find_channel_by_name(gc, buf); | |
671 | |
672 if (!channel) /* I'm not sure how this would happen =) */ | |
673 return; | |
674 | |
675 serv_got_chat_left(gc, channel->id); | |
676 ndata->channels = g_slist_remove(ndata->channels, channel); | |
677 | |
678 free(buf); | |
679 return; | |
680 | |
681 } | |
682 | |
683 if (command == 0xd1) { | |
684 gchar **res; | |
685 | |
686 res = g_strsplit(buf, " ", 0); | |
687 | |
688 serv_got_update(gc, res[0], 1, 0, time((time_t *)NULL), 0, 0, 0); | |
689 | |
690 g_strfreev(res); | |
691 free(buf); | |
692 return; | |
693 } | |
694 | |
695 if (command == 0xd2) { | |
696 serv_got_update(gc, buf, 0, 0, 0, 0, 0, 0); | |
697 free(buf); | |
698 return; | |
699 } | |
700 | |
1422 | 701 if (command == 0xd4) { |
702 /* Looks like we're getting a browse response */ | |
703 gchar user[64]; | |
704 gchar file[2048]; | |
705 struct browse_window *bw = NULL; | |
706 | |
707 int i,j; | |
708 | |
709 for (i = 0, j = 0; buf[i] != ' '; i++, j++) | |
710 { | |
711 user[j] = buf[i]; | |
712 } | |
713 user[j] = 0; i++; i++; | |
714 | |
715 for (j = 0; buf[i] != '\"'; i++, j++) | |
716 { | |
717 file[j] = buf[i]; | |
718 } | |
719 file[j] = 0; | |
720 | |
721 bw = find_browse_window_by_name(gc, user); | |
722 if (!bw) | |
723 { | |
724 /* If a browse window isn't found, let's create one */ | |
725 bw = browse_window_new(gc, user); | |
726 } | |
727 | |
728 browse_window_add_file(bw, file); | |
729 | |
730 free(buf); | |
731 return; | |
732 | |
733 } | |
734 | |
1427 | 735 if (command == 0xc9) { |
736 /* We've received a search response */ | |
737 gchar *file = (gchar *)g_malloc(sizeof(gchar) * (NAP_BUF_LEN+1)); | |
738 gchar *tmp; | |
739 gchar rest[NAP_BUF_LEN]; | |
740 gchar *data[5]; | |
741 gchar **parse_name; | |
742 | |
743 int i, j; | |
744 | |
745 for (i = 1, j = 0; buf[i] != '\"'; i++, j++) | |
746 { | |
747 file[j] = buf[i]; | |
748 } | |
749 | |
750 file[j] = 0; i++; | |
751 | |
752 tmp = (gchar *)g_malloc(sizeof(gchar) * (strlen(file) + 1)); | |
753 strcpy(tmp, rindex(file, '\\')+1); | |
754 | |
755 strcpy(rest, buf+i); | |
756 | |
757 parse_name = g_strsplit(rest, " ", 0); | |
758 | |
759 data[0] = g_strdup(tmp); | |
760 data[1] = g_strdup(parse_name[6]); | |
761 data[2] = g_strdup(parse_name[2]); | |
762 data[3] = g_strdup(parse_name[3]); | |
763 data[4] = g_strdup(parse_name[8]); | |
764 | |
765 // printf("File: %s, 1: %s, 2: %s, 3: %s\n", data[0], data[1], data[2], data[3]); | |
766 i = gtk_clist_append(GTK_CLIST(search_dialog->list), data); | |
767 | |
768 gtk_clist_set_row_data(GTK_CLIST(search_dialog->list), i, file); | |
769 g_strfreev(parse_name); | |
770 // g_free(file); | |
771 g_free(tmp); | |
772 g_free(buf); | |
773 return; | |
774 } | |
775 | |
776 if (command == 0xca) | |
777 { | |
778 /* End of search */ | |
779 g_free(buf); | |
780 return; | |
781 } | |
782 | |
1323 | 783 if (command == 0x12d) { |
784 /* Our buddy was added successfully */ | |
785 free(buf); | |
786 return; | |
787 } | |
788 | |
1329 | 789 if (command == 0x2ec) { |
790 /* Looks like someone logged in as us! =-O */ | |
791 free(buf); | |
792 | |
793 signoff(gc); | |
794 return; | |
795 } | |
796 | |
1422 | 797 if (command == 0xcc) { |
798 /* We received a Download ACK from a user. The way this is printed is kind of | |
799 * strange so we'll need to parse this one ourselves. */ | |
800 | |
801 gchar user[64]; | |
802 gchar file[2048]; | |
803 gchar hoststr[16]; | |
804 gchar portstr[16]; | |
805 int i,j; | |
806 | |
807 for (i = 0, j = 0; buf[i] != ' '; i++, j++) | |
808 { | |
809 user[j] = buf[i]; | |
810 } | |
811 user[j] = 0; i++; | |
812 | |
813 for (j = 0; buf[i] != ' '; i++, j++) | |
814 { | |
815 hoststr[j] = buf[i]; | |
816 } | |
817 hoststr[j] = 0; i++; | |
818 | |
819 for (j = 0; buf[i] != ' '; i++, j++) | |
820 { | |
821 portstr[j] = buf[i]; | |
822 } | |
823 portstr[j] = 0; i++; | |
824 | |
825 i++; /* We do this to ignore the first quotation mark */ | |
826 | |
827 for (j = 0; buf[i] != '\"'; i++, j++) | |
828 { | |
829 file[j] = buf[i]; | |
830 } | |
831 file[j] = 0; | |
832 | |
833 /* Aaight. We dont need nuttin' else. Let's download the file */ | |
834 nap_get_file(gc, user, file, atol(hoststr), atoi(portstr)); | |
835 | |
836 free(buf); | |
837 | |
838 return; | |
839 } | |
840 | |
1323 | 841 printf("NAP: [COMMAND: 0x%04x] %s\n", command, buf); |
1422 | 842 |
843 free(buf); | |
1323 | 844 } |
845 | |
846 | |
847 static void nap_login_callback(gpointer data, gint source, GdkInputCondition condition) | |
848 { | |
849 struct gaim_connection *gc = data; | |
850 struct nap_data *ndata = gc->proto_data; | |
851 gchar buf[NAP_BUF_LEN]; | |
852 unsigned short header[2]; | |
853 int i = 0; | |
854 int len; | |
855 int command; | |
856 | |
857 read(source, header, 4); | |
858 len = header[0]; | |
859 command = header[1]; | |
860 | |
861 read(source, buf, len); | |
862 buf[len] = 0; | |
863 | |
864 if (command == 0x03) { | |
865 printf("Registered with E-Mail address of: %s\n", buf); | |
866 ndata->email = g_strdup(buf); | |
867 | |
868 /* Remove old inpa, add new one */ | |
869 gdk_input_remove(ndata->inpa); | |
870 ndata->inpa = 0; | |
871 gc->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_callback, gc); | |
872 | |
873 /* Our signon is complete */ | |
874 account_online(gc); | |
875 serv_finish_login(gc); | |
876 | |
877 if (bud_list_cache_exists(gc)) | |
878 do_import(NULL, gc); | |
879 | |
880 return; | |
881 } | |
882 } | |
883 | |
884 | |
885 | |
886 static void nap_login(struct aim_user *user) | |
887 { | |
888 int fd; | |
889 struct hostent *host; | |
890 struct sockaddr_in site; | |
891 struct gaim_connection *gc = new_gaim_conn(user); | |
892 struct nap_data *ndata = gc->proto_data = g_new0(struct nap_data, 1); | |
893 char buf[NAP_BUF_LEN]; | |
894 char c; | |
895 char z[4]; | |
896 int i; | |
897 int status; | |
898 | |
1444 | 899 host = gethostbyname("64.124.41.175"); |
1323 | 900 |
1422 | 901 if (!host) { |
902 hide_login_progress(gc, "Unable to resolve hostname"); | |
903 signoff(gc); | |
904 return; | |
905 } | |
1323 | 906 |
1422 | 907 site.sin_family = AF_INET; |
908 site.sin_addr.s_addr = *(long *)(host->h_addr); | |
909 site.sin_port = htons(8888); | |
1323 | 910 |
1422 | 911 fd = socket(AF_INET, SOCK_STREAM, 0); |
912 if (fd < 0) { | |
913 hide_login_progress(gc, "Unable to create socket"); | |
914 signoff(gc); | |
915 return; | |
916 } | |
1323 | 917 |
918 /* Make a connection with the server */ | |
1422 | 919 if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) { |
920 hide_login_progress(gc, "Unable to connect."); | |
921 signoff(gc); | |
922 return; | |
923 } | |
1323 | 924 |
925 ndata->fd = fd; | |
926 | |
927 /* And write our signon data */ | |
1427 | 928 g_snprintf(buf, NAP_BUF_LEN, "%s %s 0 \"gaimster\" 0", gc->username, gc->password); |
1323 | 929 nap_write_packet(gc, 0x02, buf); |
930 | |
931 /* And set up the input watcher */ | |
932 ndata->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_login_callback, gc); | |
933 | |
1424 | 934 |
1323 | 935 } |
936 | |
937 static void nap_join_chat(struct gaim_connection *gc, int id, char *name) | |
938 { | |
939 struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
940 gchar buf[NAP_BUF_LEN]; | |
941 | |
942 /* Make sure the name has a # preceeding it */ | |
943 if (name[0] != '#') | |
944 g_snprintf(buf, NAP_BUF_LEN, "#%s", name); | |
945 else | |
946 g_snprintf(buf, NAP_BUF_LEN, "%s", name); | |
947 | |
948 nap_write_packet(gc, 0x190, buf); | |
949 } | |
950 | |
951 static void nap_chat_leave(struct gaim_connection *gc, int id) | |
952 { | |
953 struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
954 struct nap_channel *channel = NULL; | |
955 GSList *channels = ndata->channels; | |
956 | |
957 channel = find_channel_by_id(gc, id); | |
958 | |
959 if (!channel) /* Again, I'm not sure how this would happen */ | |
960 return; | |
961 | |
962 nap_write_packet(gc, 0x191, channel->name); | |
963 | |
1325 | 964 ndata->channels = g_slist_remove(ndata->channels, channel); |
1323 | 965 g_free(channel->name); |
966 g_free(channel); | |
967 | |
968 } | |
969 | |
970 static void nap_chat_send(struct gaim_connection *gc, int id, char *message) | |
971 { | |
972 struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
973 struct nap_channel *channel = NULL; | |
974 gchar buf[NAP_BUF_LEN]; | |
975 | |
976 channel = find_channel_by_id(gc, id); | |
977 | |
978 if (!channel) { | |
979 /* This shouldn't happen */ | |
980 return; | |
981 } | |
982 | |
983 g_snprintf(buf, NAP_BUF_LEN, "%s %s", channel->name, message); | |
984 nap_write_packet(gc, 0x192, buf); | |
985 | |
986 } | |
987 | |
988 static void nap_add_buddy(struct gaim_connection *gc, char *name) | |
989 { | |
990 nap_write_packet(gc, 0xCF, name); | |
991 } | |
992 | |
993 static void nap_remove_buddy(struct gaim_connection *gc, char *name) | |
994 { | |
995 nap_write_packet(gc, 0x12F, name); | |
996 } | |
997 | |
998 static void nap_close(struct gaim_connection *gc) | |
999 { | |
1000 struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
1001 gchar buf[NAP_BUF_LEN]; | |
1002 struct nap_channel *channel; | |
1422 | 1003 struct browse_window *browse; |
1004 struct nap_file_request *req; | |
1323 | 1005 GSList *channels = ndata->channels; |
1422 | 1006 GSList *requests = ndata->requests; |
1007 | |
1323 | 1008 if (gc->inpa) |
1009 gdk_input_remove(gc->inpa); | |
1010 | |
1325 | 1011 while (ndata->channels) { |
1012 channel = (struct nap_channel *)ndata->channels->data; | |
1323 | 1013 g_free(channel->name); |
1325 | 1014 ndata->channels = g_slist_remove(ndata->channels, channel); |
1323 | 1015 g_free(channel); |
1016 } | |
1325 | 1017 |
1422 | 1018 while (ndata->browses) { |
1019 browse = (struct browse_window *)ndata->browses->data; | |
1020 g_free(browse->name); | |
1021 gtk_widget_destroy(browse->window); | |
1022 ndata->browses = g_slist_remove(ndata->browses, browse); | |
1023 g_free(browse); | |
1024 } | |
1025 | |
1026 while (ndata->requests) { | |
1027 req = (struct nap_file_request *)ndata->requests->data; | |
1028 g_free(req->name); | |
1029 g_free(req->file); | |
1030 if (req->inpa) { | |
1031 gdk_input_remove(req->inpa); | |
1032 } | |
1033 ndata->requests = g_slist_remove(ndata->requests, req); | |
1034 g_free(req); | |
1035 | |
1036 } | |
1037 | |
1325 | 1038 free(gc->proto_data); |
1323 | 1039 } |
1040 | |
1041 static void nap_add_buddies(struct gaim_connection *gc, GList *buddies) | |
1042 { | |
1043 struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
1044 gchar buf[NAP_BUF_LEN]; | |
1045 int n = 0; | |
1046 | |
1047 while (buddies) { | |
1048 nap_write_packet(gc, 0xd0, (char *)buddies->data); | |
1049 buddies = buddies -> next; | |
1050 } | |
1051 } | |
1052 | |
1338 | 1053 static void nap_draw_new_user(GtkWidget *box) |
1054 { | |
1055 GtkWidget *label; | |
1056 | |
1057 label = gtk_label_new(_("Napster registration is currently under development")); | |
1058 | |
1059 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
1060 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 5); | |
1061 gtk_widget_show(label); | |
1062 } | |
1063 | |
1422 | 1064 |
1065 void nap_send_browse(GtkObject *w, char *who) | |
1066 { | |
1067 struct gaim_connection *gc = (struct gaim_connection *)gtk_object_get_user_data(w); | |
1068 struct nap_data *ndata = (struct nap_data *)gc->proto_data; | |
1069 gchar buf[NAP_BUF_LEN]; | |
1070 | |
1071 g_snprintf(buf, NAP_BUF_LEN, "%s", who); | |
1072 nap_write_packet(gc, 0xd3, buf); | |
1073 } | |
1074 | |
1427 | 1075 void nap_find_callback(GtkObject *w, GtkWidget *entry) |
1076 { | |
1077 struct gaim_connection *gc = (struct gaim_connection *)gtk_object_get_user_data(w); | |
1078 gchar *search; | |
1079 gchar buf[NAP_BUF_LEN]; | |
1080 | |
1081 search = gtk_entry_get_text(GTK_ENTRY(entry)); | |
1082 g_snprintf(buf, NAP_BUF_LEN, "FILENAME CONTAINS \"%s\" MAX_RESULTS 50", search); | |
1083 | |
1084 nap_write_packet(gc, 0xc8, buf); | |
1085 } | |
1086 | |
1087 void destroy_window(GtkObject *w, GtkWidget *win) | |
1088 { | |
1089 gtk_widget_destroy(win); | |
1090 } | |
1091 | |
1092 void nap_show_search(GtkObject *w, void *omit) | |
1093 { | |
1094 struct gaim_connection *gc = (struct gaim_connection *)gtk_object_get_user_data(w); | |
1095 | |
1096 if (!search_dialog) | |
1097 { | |
1098 GtkWidget *window; | |
1099 GtkWidget *sw; | |
1100 GtkWidget *vbox; | |
1101 GtkWidget *hbox; | |
1102 GtkWidget *label; | |
1103 GtkWidget *button; | |
1104 GtkWidget *entry; | |
1105 GtkWidget *list; | |
1106 gchar *titles[5] = {"Mp3 Name", "Nick", "Size", "Bitrate", "Connection"}; | |
1107 search_dialog = g_new0(struct search_window, 1); | |
1108 | |
1109 window = gtk_window_new(GTK_WINDOW_DIALOG); | |
1110 | |
1111 vbox = gtk_vbox_new(FALSE, 0); | |
1112 | |
1113 /* First Line */ | |
1114 hbox = gtk_hbox_new(FALSE, 5); | |
1115 label = gtk_label_new("Search for:"); | |
1116 gtk_widget_show(label); | |
1117 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5); | |
1118 | |
1119 entry = gtk_entry_new(); | |
1120 gtk_widget_show(entry); | |
1121 gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 5); | |
1122 | |
1123 button = gtk_button_new_with_label("Find"); | |
1124 gtk_widget_show(button); | |
1125 gtk_object_set_user_data(GTK_OBJECT(button), gc); | |
1126 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(nap_find_callback), entry); | |
1127 | |
1128 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5); | |
1129 | |
1130 button = gtk_button_new_with_label("Cancel"); | |
1131 gtk_widget_show(button); | |
1132 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5); | |
1133 | |
1134 | |
1135 gtk_widget_show(hbox); | |
1136 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5); | |
1137 /* End First List */ | |
1138 | |
1139 /* Second Line */ | |
1140 | |
1141 sw = gtk_scrolled_window_new(NULL, NULL); | |
1142 | |
1143 list = gtk_clist_new_with_titles(5, titles); | |
1144 | |
1145 gtk_clist_column_titles_show(GTK_CLIST(list)); | |
1146 | |
1147 gtk_signal_connect(GTK_OBJECT(list), "select-row", GTK_SIGNAL_FUNC(nap_handle_download_search), gc); | |
1148 | |
1149 gtk_container_add(GTK_CONTAINER(sw), list); | |
1150 gtk_widget_show(list); | |
1151 | |
1152 gtk_box_pack_start(GTK_BOX(vbox), sw, FALSE, FALSE, 5); | |
1153 | |
1154 gtk_widget_show(sw); | |
1155 | |
1156 /* End Second Line */ | |
1157 | |
1158 gtk_widget_show(vbox); | |
1159 | |
1160 gtk_container_add(GTK_CONTAINER(window), vbox); | |
1161 gtk_container_set_border_width(GTK_CONTAINER(window), 10); | |
1162 | |
1163 gtk_widget_set_usize(GTK_WIDGET(list), 500, 350); | |
1164 gtk_widget_set_usize(GTK_WIDGET(window), 500, 400); | |
1165 | |
1166 gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy_window), window); | |
1167 gtk_widget_show(window); | |
1168 | |
1169 search_dialog->window = window; | |
1170 search_dialog->list = list; | |
1171 } | |
1172 | |
1173 gtk_widget_show(search_dialog->window); | |
1174 } | |
1175 | |
1422 | 1176 static void nap_action_menu(GtkWidget *menu, struct gaim_connection *gc, char *who) |
1177 { | |
1178 GtkWidget *button; | |
1179 | |
1427 | 1180 button = gtk_menu_item_new_with_label("Browse Files"); |
1422 | 1181 gtk_signal_connect(GTK_OBJECT(button), "activate", GTK_SIGNAL_FUNC(nap_send_browse), who); |
1182 gtk_object_set_user_data(GTK_OBJECT(button), gc); | |
1183 gtk_menu_append(GTK_MENU(menu), button); | |
1184 gtk_widget_show(button); | |
1427 | 1185 |
1186 button = gtk_menu_item_new_with_label("Search Napster"); | |
1187 gtk_signal_connect(GTK_OBJECT(button), "activate", GTK_SIGNAL_FUNC(nap_show_search), NULL); | |
1188 gtk_object_set_user_data(GTK_OBJECT(button), gc); | |
1189 gtk_menu_append(GTK_MENU(menu), button); | |
1190 gtk_widget_show(button); | |
1422 | 1191 } |
1192 | |
1327 | 1193 static char** nap_list_icon(int uc) |
1194 { | |
1195 return napster_xpm; | |
1196 } | |
1197 | |
1323 | 1198 static struct prpl *my_protocol = NULL; |
1199 | |
1200 void nap_init(struct prpl *ret) | |
1201 { | |
1202 ret->protocol = PROTO_NAPSTER; | |
1203 ret->name = nap_name; | |
1327 | 1204 ret->list_icon = nap_list_icon; |
1422 | 1205 ret->action_menu = nap_action_menu; |
1323 | 1206 ret->user_opts = NULL; |
1207 ret->login = nap_login; | |
1208 ret->close = nap_close; | |
1209 ret->send_im = nap_send_im; | |
1210 ret->set_info = NULL; | |
1211 ret->get_info = NULL; | |
1212 ret->set_away = NULL; | |
1213 ret->get_away_msg = NULL; | |
1214 ret->set_dir = NULL; | |
1215 ret->get_dir = NULL; | |
1216 ret->dir_search = NULL; | |
1217 ret->set_idle = NULL; | |
1218 ret->change_passwd = NULL; | |
1219 ret->add_buddy = nap_add_buddy; | |
1220 ret->add_buddies = nap_add_buddies; | |
1221 ret->remove_buddy = nap_remove_buddy; | |
1222 ret->add_permit = NULL; | |
1223 ret->rem_permit = NULL; | |
1224 ret->add_deny = NULL; | |
1225 ret->rem_deny = NULL; | |
1226 ret->warn = NULL; | |
1227 ret->accept_chat = NULL; | |
1228 ret->join_chat = nap_join_chat; | |
1229 ret->chat_invite = NULL; | |
1230 ret->chat_leave = nap_chat_leave; | |
1231 ret->chat_whisper = NULL; | |
1232 ret->chat_send = nap_chat_send; | |
1233 ret->keepalive = NULL; | |
1338 | 1234 ret->draw_new_user = nap_draw_new_user; |
1323 | 1235 |
1236 my_protocol = ret; | |
1237 } | |
1238 | |
1239 char *gaim_plugin_init(GModule * handle) | |
1240 { | |
1443
336fc98b7f90
[gaim-migrate @ 1453]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1427
diff
changeset
|
1241 load_protocol(nap_init, sizeof(struct prpl)); |
1323 | 1242 return NULL; |
1243 } | |
1244 | |
1245 void gaim_plugin_remove() | |
1246 { | |
1247 struct prpl *p = find_prpl(PROTO_NAPSTER); | |
1248 if (p == my_protocol) | |
1249 unload_protocol(p); | |
1250 } |