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>
|
|
37 #include "multi.h"
|
|
38 #include "prpl.h"
|
|
39 #include "gaim.h"
|
1327
|
40 #include "pixmaps/napster.xpm"
|
1323
|
41
|
|
42 #define NAP_BUF_LEN 4096
|
|
43
|
|
44 GSList *nap_connections = NULL;
|
|
45
|
|
46 static unsigned int chat_id = 0;
|
|
47
|
|
48 struct nap_channel {
|
|
49 unsigned int id;
|
|
50 gchar *name;
|
|
51 };
|
|
52
|
|
53 struct nap_data {
|
|
54 int fd;
|
|
55 int inpa;
|
|
56
|
|
57 gchar *email;
|
|
58 GSList *channels;
|
|
59 };
|
|
60
|
|
61 static char *nap_name()
|
|
62 {
|
|
63 return "Napster";
|
|
64 }
|
|
65
|
|
66 char *name()
|
|
67 {
|
|
68 return "Napster";
|
|
69 }
|
|
70
|
|
71 char *description()
|
|
72 {
|
|
73 return "Allows gaim to use the Napster protocol. Yes, kids, drugs are bad.";
|
|
74 }
|
|
75
|
|
76
|
|
77 /* FIXME: Make this use va_arg stuff */
|
|
78 void nap_write_packet(struct gaim_connection *gc, unsigned short command, char *message)
|
|
79 {
|
|
80 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
81 unsigned short size;
|
|
82
|
|
83 size = strlen(message);
|
|
84
|
|
85 write(ndata->fd, &size, 2);
|
|
86 write(ndata->fd, &command, 2);
|
|
87 write(ndata->fd, message, size);
|
|
88 }
|
|
89
|
|
90 static void nap_send_im(struct gaim_connection *gc, char *who, char *message, int away)
|
|
91 {
|
|
92 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
93 gchar buf[NAP_BUF_LEN];
|
|
94
|
|
95 g_snprintf(buf, NAP_BUF_LEN, "%s %s", who, message);
|
|
96 nap_write_packet(gc, 0xCD, buf);
|
|
97 }
|
|
98
|
|
99 static struct nap_channel *find_channel_by_name(struct gaim_connection *gc, char *name)
|
|
100 {
|
|
101 struct nap_channel *channel;
|
|
102 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
103 GSList *channels;
|
|
104
|
|
105 channels = ndata->channels;
|
|
106
|
|
107 while (channels) {
|
|
108 channel = (struct nap_channel *)channels->data;
|
1325
|
109
|
|
110 if (channel) {
|
|
111 if (!g_strcasecmp(name, channel->name)) {
|
|
112 return channel;
|
|
113 }
|
1323
|
114 }
|
|
115 channels = g_slist_next(channels);
|
|
116 }
|
|
117
|
|
118 return NULL;
|
|
119 }
|
|
120
|
|
121 static struct nap_channel *find_channel_by_id(struct gaim_connection *gc, int id)
|
|
122 {
|
|
123 struct nap_channel *channel;
|
|
124 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
125 GSList *channels;
|
|
126
|
|
127 channels = ndata->channels;
|
|
128
|
|
129 while (channels) {
|
|
130 channel = (struct nap_channel *)channels->data;
|
|
131 if (id == channel->id) {
|
|
132 return channel;
|
|
133 }
|
|
134
|
|
135 channels = g_slist_next(channels);
|
|
136 }
|
|
137
|
|
138 return NULL;
|
|
139 }
|
|
140
|
|
141 static struct conversation *find_conversation_by_id(struct gaim_connection *gc, int id)
|
|
142 {
|
|
143 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
144 GSList *bc = gc->buddy_chats;
|
|
145 struct conversation *b = NULL;
|
|
146
|
|
147 while (bc) {
|
|
148 b = (struct conversation *)bc->data;
|
|
149 if (id == b->id) {
|
|
150 break;
|
|
151 }
|
|
152 bc = bc->next;
|
|
153 b = NULL;
|
|
154 }
|
|
155
|
|
156 if (!b) {
|
|
157 return NULL;
|
|
158 }
|
|
159
|
|
160 return b;
|
|
161 }
|
|
162
|
|
163 static void nap_callback(gpointer data, gint source, GdkInputCondition condition)
|
|
164 {
|
|
165 struct gaim_connection *gc = data;
|
|
166 struct nap_data *ndata = gc->proto_data;
|
|
167 gchar *buf;
|
|
168 unsigned short header[2];
|
|
169 int i = 0;
|
|
170 int len;
|
|
171 int command;
|
|
172 gchar **res;
|
|
173
|
|
174 read(source, header, 4);
|
|
175 len = header[0];
|
|
176 command = header[1];
|
|
177
|
|
178 buf = (gchar *)g_malloc(sizeof(gchar) * (len + 1));
|
|
179
|
|
180 read(source, buf, len);
|
|
181
|
|
182 buf[len] = 0;
|
|
183
|
|
184 if (command == 0xd6) {
|
|
185 res = g_strsplit(buf, " ", 0);
|
|
186 /* Do we want to report what the users are doing? */
|
|
187 printf("users: %s, files: %s, size: %sGB\n", res[0], res[1], res[2]);
|
|
188 g_strfreev(res);
|
|
189 free(buf);
|
|
190 return;
|
|
191 }
|
|
192
|
|
193 if (command == 0x26d) {
|
|
194 /* Do we want to use the MOTD? */
|
|
195 free(buf);
|
|
196 return;
|
|
197 }
|
|
198
|
|
199 if (command == 0xCD) {
|
|
200 res = g_strsplit(buf, " ", 1);
|
|
201 serv_got_im(gc, res[0], res[1], 0);
|
|
202 g_strfreev(res);
|
|
203 free(buf);
|
|
204 return;
|
|
205 }
|
|
206
|
|
207 if (command == 0x195) {
|
|
208 struct nap_channel *channel;
|
|
209 int id;
|
|
210
|
|
211 channel = find_channel_by_name(gc, buf);
|
|
212
|
|
213 if (!channel) {
|
|
214 chat_id++;
|
|
215
|
|
216 channel = g_new0(struct nap_channel, 1);
|
|
217
|
|
218 channel->id = chat_id;
|
|
219 channel->name = g_strdup(buf);
|
|
220
|
|
221 ndata->channels = g_slist_append(ndata->channels, channel);
|
|
222
|
|
223 serv_got_joined_chat(gc, chat_id, buf);
|
|
224 }
|
|
225
|
|
226 free(buf);
|
|
227 return;
|
|
228 }
|
|
229
|
|
230 if (command == 0x198 || command == 0x196) {
|
|
231 struct nap_channel *channel;
|
|
232 struct conversation *convo;
|
|
233 gchar **res;
|
|
234
|
|
235 res = g_strsplit(buf, " ", 0);
|
|
236
|
|
237 channel = find_channel_by_name(gc, res[0]);
|
|
238 convo = find_conversation_by_id(gc, channel->id);
|
|
239
|
|
240 add_chat_buddy(convo, res[1]);
|
|
241
|
|
242 g_strfreev(res);
|
|
243
|
|
244 free(buf);
|
|
245 return;
|
|
246 }
|
|
247
|
1325
|
248 if (command == 0x197) {
|
|
249 struct nap_channel *channel;
|
|
250 struct conversation *convo;
|
|
251 gchar **res;
|
|
252
|
|
253 res = g_strsplit(buf, " ", 0);
|
|
254
|
|
255 channel = find_channel_by_name(gc, res[0]);
|
|
256 convo = find_conversation_by_id(gc, channel->id);
|
|
257
|
|
258 remove_chat_buddy(convo, res[1]);
|
|
259
|
|
260 g_strfreev(res);
|
|
261 free(buf);
|
|
262 return;
|
|
263 }
|
|
264
|
1323
|
265 if (command == 0x193) {
|
|
266 gchar **res;
|
|
267 struct nap_channel *channel;
|
|
268
|
|
269 res = g_strsplit(buf, " ", 2);
|
|
270
|
|
271 channel = find_channel_by_name(gc, res[0]);
|
|
272
|
|
273 if (channel)
|
|
274 serv_got_chat_in(gc, channel->id, res[1], 0, res[2]);
|
|
275
|
|
276 g_strfreev(res);
|
|
277 free(buf);
|
|
278 return;
|
|
279 }
|
|
280
|
|
281 if (command == 0x194) {
|
|
282 do_error_dialog(buf, "Gaim: Napster Error");
|
|
283 free(buf);
|
|
284 return;
|
|
285 }
|
|
286
|
|
287 if (command == 0x12e) {
|
|
288 gchar buf2[NAP_BUF_LEN];
|
|
289
|
|
290 g_snprintf(buf2, NAP_BUF_LEN, "Unable to add '%s' to your hotlist", buf);
|
|
291 do_error_dialog(buf2, "Gaim: Napster Error");
|
|
292
|
|
293 free(buf);
|
|
294 return;
|
|
295
|
|
296 }
|
|
297
|
|
298 if (command == 0x191) {
|
|
299 struct nap_channel *channel;
|
|
300
|
|
301 channel = find_channel_by_name(gc, buf);
|
|
302
|
|
303 if (!channel) /* I'm not sure how this would happen =) */
|
|
304 return;
|
|
305
|
|
306 serv_got_chat_left(gc, channel->id);
|
|
307 ndata->channels = g_slist_remove(ndata->channels, channel);
|
|
308
|
|
309 free(buf);
|
|
310 return;
|
|
311
|
|
312 }
|
|
313
|
|
314 if (command == 0xd1) {
|
|
315 gchar **res;
|
|
316
|
|
317 res = g_strsplit(buf, " ", 0);
|
|
318
|
|
319 serv_got_update(gc, res[0], 1, 0, time((time_t *)NULL), 0, 0, 0);
|
|
320
|
|
321 g_strfreev(res);
|
|
322 free(buf);
|
|
323 return;
|
|
324 }
|
|
325
|
|
326 if (command == 0xd2) {
|
|
327 serv_got_update(gc, buf, 0, 0, 0, 0, 0, 0);
|
|
328 free(buf);
|
|
329 return;
|
|
330 }
|
|
331
|
|
332 if (command == 0x12d) {
|
|
333 /* Our buddy was added successfully */
|
|
334 free(buf);
|
|
335 return;
|
|
336 }
|
|
337
|
1329
|
338 if (command == 0x2ec) {
|
|
339 /* Looks like someone logged in as us! =-O */
|
|
340 free(buf);
|
|
341
|
|
342 signoff(gc);
|
|
343 return;
|
|
344 }
|
|
345
|
1323
|
346 printf("NAP: [COMMAND: 0x%04x] %s\n", command, buf);
|
|
347 }
|
|
348
|
|
349
|
|
350 static void nap_login_callback(gpointer data, gint source, GdkInputCondition condition)
|
|
351 {
|
|
352 struct gaim_connection *gc = data;
|
|
353 struct nap_data *ndata = gc->proto_data;
|
|
354 gchar buf[NAP_BUF_LEN];
|
|
355 unsigned short header[2];
|
|
356 int i = 0;
|
|
357 int len;
|
|
358 int command;
|
|
359
|
|
360 read(source, header, 4);
|
|
361 len = header[0];
|
|
362 command = header[1];
|
|
363
|
|
364 read(source, buf, len);
|
|
365 buf[len] = 0;
|
|
366
|
|
367 if (command == 0x03) {
|
|
368 printf("Registered with E-Mail address of: %s\n", buf);
|
|
369 ndata->email = g_strdup(buf);
|
|
370
|
|
371 /* Remove old inpa, add new one */
|
|
372 gdk_input_remove(ndata->inpa);
|
|
373 ndata->inpa = 0;
|
|
374 gc->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_callback, gc);
|
|
375
|
|
376 /* Our signon is complete */
|
|
377 account_online(gc);
|
|
378 serv_finish_login(gc);
|
|
379
|
|
380 if (bud_list_cache_exists(gc))
|
|
381 do_import(NULL, gc);
|
|
382
|
|
383 return;
|
|
384 }
|
|
385 }
|
|
386
|
|
387
|
|
388
|
|
389 static void nap_login(struct aim_user *user)
|
|
390 {
|
|
391 int fd;
|
|
392 struct hostent *host;
|
|
393 struct sockaddr_in site;
|
|
394 struct gaim_connection *gc = new_gaim_conn(user);
|
|
395 struct nap_data *ndata = gc->proto_data = g_new0(struct nap_data, 1);
|
|
396 char buf[NAP_BUF_LEN];
|
|
397 char c;
|
|
398 char z[4];
|
|
399 int i;
|
|
400 int status;
|
|
401
|
|
402 host = gethostbyname("64.124.41.184");
|
|
403
|
|
404 if (!host) {
|
|
405 hide_login_progress(gc, "Unable to resolve hostname");
|
|
406 signoff(gc);
|
|
407 return;
|
|
408 }
|
|
409
|
|
410 site.sin_family = AF_INET;
|
|
411 site.sin_addr.s_addr = *(long *)(host->h_addr);
|
|
412
|
|
413 site.sin_port = htons(8888);
|
|
414
|
|
415 fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
416 if (fd < 0) {
|
|
417 hide_login_progress(gc, "Unable to create socket");
|
|
418 signoff(gc);
|
|
419 return;
|
|
420 }
|
|
421
|
|
422 /* Make a connection with the server */
|
|
423 if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) {
|
|
424 hide_login_progress(gc, "Unable to connect.");
|
|
425 signoff(gc);
|
|
426 return;
|
|
427 }
|
|
428
|
|
429 ndata->fd = fd;
|
|
430
|
|
431 /* And write our signon data */
|
|
432 g_snprintf(buf, NAP_BUF_LEN, "%s %s 0 \"Gnapster 1.4.1a\" 0", gc->username, gc->password);
|
|
433 nap_write_packet(gc, 0x02, buf);
|
|
434
|
|
435 /* And set up the input watcher */
|
|
436 ndata->inpa = gdk_input_add(ndata->fd, GDK_INPUT_READ, nap_login_callback, gc);
|
|
437
|
|
438 }
|
|
439
|
|
440 static void nap_join_chat(struct gaim_connection *gc, int id, char *name)
|
|
441 {
|
|
442 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
443 gchar buf[NAP_BUF_LEN];
|
|
444
|
|
445 /* Make sure the name has a # preceeding it */
|
|
446 if (name[0] != '#')
|
|
447 g_snprintf(buf, NAP_BUF_LEN, "#%s", name);
|
|
448 else
|
|
449 g_snprintf(buf, NAP_BUF_LEN, "%s", name);
|
|
450
|
|
451 nap_write_packet(gc, 0x190, buf);
|
|
452 }
|
|
453
|
|
454 static void nap_chat_leave(struct gaim_connection *gc, int id)
|
|
455 {
|
|
456 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
457 struct nap_channel *channel = NULL;
|
|
458 GSList *channels = ndata->channels;
|
|
459
|
|
460 channel = find_channel_by_id(gc, id);
|
|
461
|
|
462 if (!channel) /* Again, I'm not sure how this would happen */
|
|
463 return;
|
|
464
|
|
465 nap_write_packet(gc, 0x191, channel->name);
|
|
466
|
1325
|
467 ndata->channels = g_slist_remove(ndata->channels, channel);
|
1323
|
468 g_free(channel->name);
|
|
469 g_free(channel);
|
|
470
|
|
471 }
|
|
472
|
|
473 static void nap_chat_send(struct gaim_connection *gc, int id, char *message)
|
|
474 {
|
|
475 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
476 struct nap_channel *channel = NULL;
|
|
477 gchar buf[NAP_BUF_LEN];
|
|
478
|
|
479 channel = find_channel_by_id(gc, id);
|
|
480
|
|
481 if (!channel) {
|
|
482 /* This shouldn't happen */
|
|
483 return;
|
|
484 }
|
|
485
|
|
486 g_snprintf(buf, NAP_BUF_LEN, "%s %s", channel->name, message);
|
|
487 nap_write_packet(gc, 0x192, buf);
|
|
488
|
|
489 }
|
|
490
|
|
491 static void nap_add_buddy(struct gaim_connection *gc, char *name)
|
|
492 {
|
|
493 nap_write_packet(gc, 0xCF, name);
|
|
494 }
|
|
495
|
|
496 static void nap_remove_buddy(struct gaim_connection *gc, char *name)
|
|
497 {
|
|
498 nap_write_packet(gc, 0x12F, name);
|
|
499 }
|
|
500
|
|
501 static void nap_close(struct gaim_connection *gc)
|
|
502 {
|
|
503 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
504 gchar buf[NAP_BUF_LEN];
|
|
505 struct nap_channel *channel;
|
|
506 GSList *channels = ndata->channels;
|
|
507
|
|
508 if (gc->inpa)
|
|
509 gdk_input_remove(gc->inpa);
|
|
510
|
1325
|
511 while (ndata->channels) {
|
|
512 channel = (struct nap_channel *)ndata->channels->data;
|
1323
|
513 g_free(channel->name);
|
1325
|
514 ndata->channels = g_slist_remove(ndata->channels, channel);
|
1323
|
515 g_free(channel);
|
|
516 }
|
1325
|
517
|
|
518 free(gc->proto_data);
|
1323
|
519 }
|
|
520
|
|
521 static void nap_add_buddies(struct gaim_connection *gc, GList *buddies)
|
|
522 {
|
|
523 struct nap_data *ndata = (struct nap_data *)gc->proto_data;
|
|
524 gchar buf[NAP_BUF_LEN];
|
|
525 int n = 0;
|
|
526
|
|
527 while (buddies) {
|
|
528 nap_write_packet(gc, 0xd0, (char *)buddies->data);
|
|
529 buddies = buddies -> next;
|
|
530 }
|
|
531 }
|
|
532
|
1327
|
533 static char** nap_list_icon(int uc)
|
|
534 {
|
|
535 return napster_xpm;
|
|
536 }
|
|
537
|
1323
|
538 static struct prpl *my_protocol = NULL;
|
|
539
|
|
540 void nap_init(struct prpl *ret)
|
|
541 {
|
|
542 ret->protocol = PROTO_NAPSTER;
|
|
543 ret->name = nap_name;
|
1327
|
544 ret->list_icon = nap_list_icon;
|
1323
|
545 ret->action_menu = NULL;
|
|
546 ret->user_opts = NULL;
|
|
547 ret->login = nap_login;
|
|
548 ret->close = nap_close;
|
|
549 ret->send_im = nap_send_im;
|
|
550 ret->set_info = NULL;
|
|
551 ret->get_info = NULL;
|
|
552 ret->set_away = NULL;
|
|
553 ret->get_away_msg = NULL;
|
|
554 ret->set_dir = NULL;
|
|
555 ret->get_dir = NULL;
|
|
556 ret->dir_search = NULL;
|
|
557 ret->set_idle = NULL;
|
|
558 ret->change_passwd = NULL;
|
|
559 ret->add_buddy = nap_add_buddy;
|
|
560 ret->add_buddies = nap_add_buddies;
|
|
561 ret->remove_buddy = nap_remove_buddy;
|
|
562 ret->add_permit = NULL;
|
|
563 ret->rem_permit = NULL;
|
|
564 ret->add_deny = NULL;
|
|
565 ret->rem_deny = NULL;
|
|
566 ret->warn = NULL;
|
|
567 ret->accept_chat = NULL;
|
|
568 ret->join_chat = nap_join_chat;
|
|
569 ret->chat_invite = NULL;
|
|
570 ret->chat_leave = nap_chat_leave;
|
|
571 ret->chat_whisper = NULL;
|
|
572 ret->chat_send = nap_chat_send;
|
|
573 ret->keepalive = NULL;
|
|
574
|
|
575 my_protocol = ret;
|
|
576 }
|
|
577
|
|
578 char *gaim_plugin_init(GModule * handle)
|
|
579 {
|
|
580 load_protocol(nap_init);
|
|
581 return NULL;
|
|
582 }
|
|
583
|
|
584 void gaim_plugin_remove()
|
|
585 {
|
|
586 struct prpl *p = find_prpl(PROTO_NAPSTER);
|
|
587 if (p == my_protocol)
|
|
588 unload_protocol(p);
|
|
589 }
|