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