comparison plugins/msn/msn.c @ 1259:7db9121aac80

[gaim-migrate @ 1269] MSN plugin committer: Tailor Script <tailor@pidgin.im>
author Rob Flynn <gaim@robflynn.com>
date Thu, 14 Dec 2000 08:33:41 +0000
parents
children b0bd82cce5e1
comparison
equal deleted inserted replaced
1258:385c1a1d96aa 1259:7db9121aac80
1 /*
2 * gaim - MSN 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 <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <time.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include "multi.h"
37 #include "prpl.h"
38 #include "gaim.h"
39 #include "gnome_applet_mgr.h"
40 #include "md5.h"
41
42 #define MSN_BUF_LEN 4096
43
44 #define MIME_HEADER "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nX-MMS-IM-Format: FN=MS%20Sans%20Serif; EF=; CO=0; CS=0; PF=0\r\n\r\n"
45
46 #define MSN_ONLINE 1
47 #define MSN_BUSY 2
48 #define MSN_IDLE 3
49 #define MSN_BRB 4
50 #define MSN_AWAY 5
51 #define MSN_PHONE 6
52 #define MSN_LUNCH 7
53 #define MSN_OFFLINE 8
54 #define MSN_HIDDEN 9
55
56
57 struct msn_ask_add_permit {
58 struct gaim_connection *gc;
59 char *user;
60 char *friendly;
61 };
62
63 struct msn_data {
64 int fd;
65
66 char protocol[6];
67 char *friendly;
68 gchar *policy;
69 };
70
71 struct msn_conn {
72 gchar *user;
73 int inpa;
74 int fd;
75 };
76
77 void msn_handler(gpointer data, gint source, GdkInputCondition condition);
78
79 GSList * msn_connections = NULL;
80
81 static char *msn_name() {
82 return "MSN";
83 }
84
85 char *name() {
86 return "MSN";
87 }
88
89 char *description() {
90 return "Allows gaim to use the MSN protocol. For some reason, this frightens me.";
91 }
92
93 struct msn_conn *find_msn_conn_by_user(gchar *user) {
94 struct msn_conn *mc;
95 GSList *conns = msn_connections;
96
97 while (conns) {
98 mc = (struct msn_conn *)conns->data;
99
100 if (mc != NULL) {
101 if (strcasecmp(mc->user, user) == 0) {
102 return mc;
103 }
104 }
105
106 conns = g_slist_next(conns);
107 }
108
109 return NULL;
110 }
111
112 void msn_read_line(char *buf, int fd) {
113
114 int status;
115 char c;
116 int i = 0;
117
118 do {
119 status = recv(fd, &c, 1, 0);
120
121 if (!status)
122 return;
123
124 buf[i] = c;
125 i++;
126 } while (c != '\n');
127
128 buf[i] = '\0';
129 g_strchomp(buf);
130
131 /* I'm a bastard again :-) */
132 printf("MSN: %s\n", buf);
133 }
134
135 int msn_connect(char *server, int port) {
136 int fd;
137 struct hostent *host;
138 struct sockaddr_in site;
139
140 printf("Connecting to '%s' on '%d'\n", server, port);
141 host = gethostbyname(server);
142 if (!host) {
143 return -1;
144 }
145
146 site.sin_family = AF_INET;
147 site.sin_addr.s_addr = *(long *)(host->h_addr);
148 site.sin_port = htons(port);
149
150 fd = socket(AF_INET, SOCK_STREAM, 0);
151 if (fd < 0) {
152 return -1;
153 }
154
155 if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) {
156 return -1;
157 }
158
159 return fd;
160 }
161
162 static void msn_add_buddy(struct gaim_connection *gc, char *who) {
163 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
164 time_t trId = time((time_t *)NULL);
165 gchar buf[4096];
166
167 g_snprintf(buf, 4096, "ADD %d FL %s %s\n", trId, who, who);
168 write(mdata->fd, buf, strlen(buf));
169 }
170
171 static void msn_rem_permit(struct gaim_connection *gc, char *who) {
172 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
173 time_t trId = time((time_t *)NULL);
174 gchar buf[4096];
175
176 g_snprintf(buf, 4096, "REM %d AL %s %s\n", trId, who, who);
177 write(mdata->fd, buf, strlen(buf));
178 }
179
180 static void msn_add_permit(struct gaim_connection *gc, char *who) {
181 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
182 time_t trId = time((time_t *)NULL);
183 gchar buf[4096];
184
185 g_snprintf(buf, 4096, "ADD %d AL %s %s\n", trId, who, who);
186 write(mdata->fd, buf, strlen(buf));
187 }
188
189 static void msn_remove_buddy(struct gaim_connection *gc, char *who) {
190 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
191 time_t trId = time((time_t *)NULL);
192 gchar buf[4096];
193
194 g_snprintf(buf, 4096, "REM %d FL %s\n", trId, who);
195 write(mdata->fd, buf, strlen(buf));
196 }
197
198 void msn_accept_add_permit (gpointer w, struct msn_ask_add_permit *ap ) {
199 gchar buf[4096];
200
201 msn_add_permit(ap->gc, ap->user);
202 }
203
204 void msn_cancel_add_permit (gpointer w, struct msn_ask_add_permit *ap ) {
205
206 g_free(ap->user);
207 g_free(ap->friendly);
208 g_free(ap);
209 }
210
211 void msn_callback (struct gaim_connection * gc, gint fd) {
212 struct msn_data *mdata;
213 char c;
214 int i = 0;
215 int status;
216 gchar buf[4096];
217 gchar **resps;
218
219 mdata = (struct msn_data *)gc->proto_data;
220
221 do {
222 /* Read data from whatever connection our inpa
223 * refered us from */
224 status = recv(fd, &c, 1,0);
225
226 if (!status)
227 return;
228
229 buf[i] = c;
230 i++;
231 } while (c != '\n');
232
233 buf[i] = '\0';
234
235 g_strchomp(buf);
236
237 printf("MSN: %s\n", buf);
238
239 if (strlen(buf) == 0) { return; }
240
241 resps = g_strsplit(buf, " ", 0);
242
243 /* See if someone is bumping us */
244 if (strcasecmp(resps[0], "BYE") == 0) {
245 struct msn_conn *mc;
246 GSList * conns = msn_connections;
247
248 /* Yup. Let's find their convo and kill it */
249
250 mc = find_msn_conn_by_user(resps[1]);
251
252 /* If we have the convo, remove it */
253 if (mc != NULL) {
254 /* and remove it */
255 conns = g_slist_remove(conns, mc);
256
257 g_free(mc->user);
258 gdk_input_remove(mc->inpa);
259 close(mc->fd);
260
261
262 g_free(mc);
263 }
264
265 g_strfreev(resps);
266 return;
267 }
268
269 if (strcasecmp(resps[0], "ADD") == 0) {
270
271 if (strcasecmp(resps[2], "RL") == 0) {
272 gchar buf[4096];
273 struct msn_ask_add_permit *ap = g_new0(struct msn_ask_add_permit, 1);
274
275 g_snprintf(buf, 4096, "The user %s (%s) wants to add you to their buddylist.", resps[4], resps[5]);
276
277 ap->user = g_strdup(resps[4]);
278 ap->friendly = g_strdup(resps[5]);
279 ap->gc = gc;
280
281 do_ask_dialog(buf, ap, (GtkFunction)msn_accept_add_permit, (GtkFunction)msn_cancel_add_permit);
282 }
283
284 g_strfreev(resps);
285 return;
286 }
287
288 if (strcasecmp(resps[0], "REM") == 0) {
289
290 if (strcasecmp(resps[2], "RL") == 0) {
291 msn_rem_permit(gc, resps[4]);
292 }
293
294 g_strfreev(resps);
295 return;
296 }
297
298 if (strcasecmp(resps[0], "FLN") == 0) {
299 serv_got_update(gc, resps[1], 0, 0, 0, 0, MSN_OFFLINE, 0);
300 }
301
302 if (strcasecmp(resps[0], "ILN") == 0) {
303 int status;
304
305 if (!strcasecmp(resps[2], "NLN"))
306 status = MSN_ONLINE;
307 else if (!strcasecmp(resps[2], "BSY"))
308 status = MSN_BUSY;
309 else if (!strcasecmp(resps[2], "IDL"))
310 status = MSN_IDLE;
311 else if (!strcasecmp(resps[2], "BRB"))
312 status = MSN_BRB;
313 else if (!strcasecmp(resps[2], "AWY"))
314 status = MSN_AWAY;
315 else if (!strcasecmp(resps[2], "PHN"))
316 status = MSN_PHONE;
317 else if (!strcasecmp(resps[2], "LUN"))
318 status = MSN_LUNCH;
319 else
320 status = MSN_ONLINE;
321
322 serv_got_update(gc, resps[3], 1, 0, 0, 0, status, 0);
323
324 g_strfreev(resps);
325 return;
326
327 }
328
329 /* Check buddy update status */
330 if (strcasecmp(resps[0], "NLN") == 0) {
331 /* FIXME: We currently dont care if they are busy,
332 * idle, brb, away, phone, our out to lunch. This will
333 * be supported eventually (BSY,IDL,BRB,AWY,PHN,LUN)
334 * respectively */
335
336 serv_got_update(gc, resps[2], 1, 0, 0, 0, MSN_ONLINE, 0);
337 }
338
339 /* Check to see if we have an incoming buddylist */
340 if (strcasecmp(resps[0], "LST") == 0) {
341 /* Check to see if there are any buddies in the list */
342 if (atoi(resps[5]) == 0) {
343 /* No buddies */
344 g_strfreev(resps);
345 return;
346 }
347
348 /* FIXME: We should support the permit and deny
349 * lists as well */
350
351 if (strcasecmp(resps[2], "FL") == 0) {
352 struct buddy *b;
353
354 b = find_buddy(gc, resps[6]);
355
356 if (!b)
357 add_buddy(gc, "Buddies", resps[6], resps[6]);
358 }
359
360 g_strfreev(resps);
361 return;
362 }
363
364 /* Check to see if we got a message request */
365 if (strcasecmp(resps[0], "MSG") == 0) {
366 gchar *message;
367 gchar *buf2;
368 int size;
369 int status;
370
371 /* Determine our message size */
372 size = atoi(resps[3]);
373
374 buf2 = (gchar *)g_malloc(sizeof(gchar) * (size+1));
375 status = recv(fd, buf2, size, 0);
376 buf2[size] = 0;
377
378 /* Looks like we got the message. If it's blank, let's bail */
379 if (strcasecmp(strstr(buf2, "\r\n\r\n")+4, "\r\n") == 0) {
380 g_free(buf2);
381 g_strfreev(resps);
382 return;
383 }
384
385 serv_got_im(gc, resps[1], strstr(buf2, "\r\n\r\n")+4, 0);
386
387 g_free(buf2);
388 g_strfreev(resps);
389 return;
390 }
391
392
393 /* Check to see if we got a ring request */
394 if (strcasecmp(resps[0], "RNG") == 0) {
395 gchar **address;
396 struct msn_conn *mc = g_new0(struct msn_conn, 1);
397
398 address = g_strsplit(resps[2], ":", 0);
399
400 if (!(mc->fd = msn_connect(address[0], atoi(address[1])))) {
401 do_error_dialog(resps[5], "Msg Err from");
402 g_strfreev(address);
403 g_strfreev(resps);
404 g_free(mc);
405 return;
406 }
407
408 mc->user = g_strdup(resps[5]);
409
410 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_READ, msn_handler, gc);
411
412 g_snprintf(buf, 4096, "ANS 1 %s %s %s\n", gc->username, resps[4], resps[1]);
413 write(mc->fd, buf, strlen(buf));
414
415 msn_connections = g_slist_append(msn_connections, mc);
416
417 g_strfreev(address);
418 g_strfreev(resps);
419 return;
420 }
421
422 g_strfreev(resps);
423
424 }
425
426
427 void msn_handler(gpointer data, gint source, GdkInputCondition condition) {
428 msn_callback(data, source);
429 }
430
431 void msn_login(struct aim_user *user) {
432 time_t trId = time((time_t *)NULL);
433 char buf[4096];
434 char buf2[4096];
435
436 struct gaim_connection *gc = new_gaim_conn(user);
437 struct msn_data *mdata = gc->proto_data = g_new0(struct msn_data, 1);
438 char c;
439 int i;
440 int status;
441
442 md5_state_t st;
443 md5_byte_t di[16];
444 int x;
445
446 gchar **results;
447
448 g_snprintf(mdata->protocol, strlen("MSNP2")+1, "MSNP2");
449
450 set_login_progress(gc, 1,"Connecting");
451
452 while (gtk_events_pending())
453 gtk_main_iteration();
454 if (!g_slist_find(connections, gc))
455 return;
456
457 if (!(mdata->fd = msn_connect("messenger.hotmail.com", 1863))) {
458 hide_login_progress(gc, "Error connection to server");
459 signoff(gc);
460 return;
461 }
462
463 g_snprintf(buf, sizeof(buf), "Signon: %s", gc->username);
464 set_login_progress(gc, 2, buf);
465
466 /* This is where we will attempt to sign on */
467 g_snprintf(buf, 4096, "VER %d %s\n", trId, mdata->protocol);
468 write(mdata->fd, buf, strlen(buf));
469
470 msn_read_line(&buf2, mdata->fd);
471
472 buf[strlen(buf)-1] = '\0';
473 if (strcmp(buf, buf2) != 0) {
474 hide_login_progress(gc, buf2);
475 signoff(gc);
476 return;
477 }
478
479 /* Looks like our versions matched up. Let's find out
480 * which policy we should use */
481
482 g_snprintf(buf, 4096, "INF %d\n", trId);
483 write(mdata->fd, buf, strlen(buf));
484
485 msn_read_line(&buf2, mdata->fd);
486 results = g_strsplit(buf2, " ", 0);
487 mdata->policy = g_strdup(results[2]);
488 g_strfreev(results);
489
490 /* We've set our policy. Now, lets attempt a sign on */
491 g_snprintf(buf, 4096, "USR %d %s I %s\n", trId, mdata->policy, gc->username);
492 write(mdata->fd, buf, strlen(buf));
493
494 msn_read_line(&buf2, mdata->fd);
495
496 /* This is where things get kinky */
497 results = g_strsplit(buf2, " ", 0);
498
499 /* Are we being transfered to another server ? */
500 if (strcasecmp(results[0], "XFR") == 0) {
501 /* Yup. We should connect to the _new_ server */
502 strcpy(buf, results[3]);
503 g_strfreev(results);
504
505 results = g_strsplit(buf, ":", 0);
506
507 /* Connect to the new server */
508 if (!(mdata->fd = msn_connect(results[0], atoi(results[1])))) {
509 hide_login_progress(gc, "Error connecting to server");
510 signoff(gc);
511 g_strfreev(results);
512 return;
513 }
514
515
516 g_strfreev(results);
517
518 /* We're now connected to the new server. Send signon
519 * information again */
520 g_snprintf(buf, 4096, "USR %d %s I %s\n", trId, mdata->policy, gc->username);
521 write(mdata->fd, buf, strlen(buf));
522
523 msn_read_line(&buf, mdata->fd);
524 results = g_strsplit(buf, " ", 0);
525
526 }
527
528 /* Otherwise, if we have a USR response, let's handle it */
529 if (strcasecmp("USR", results[0]) == 0) {
530 /* Looks like we got a response. Let's get our challenge
531 * string */
532 strcpy(buf, results[4]);
533
534 }
535 else {
536 g_strfreev(results);
537 hide_login_progress(gc, "Error signing on");
538 signoff(gc);
539 return;
540 }
541 g_strfreev(results);
542
543 /* Build our response string */
544 snprintf(buf2, 4096, "%s%s", buf, gc->password);
545
546 /* Use the MD5 Hashing */
547 md5_init(&st);
548 md5_append(&st, (const md5_byte_t *)buf2, strlen(buf2));
549 md5_finish(&st, di);
550
551 /* And now encode it in hex */
552 sprintf(buf, "%02x", di[0]);
553 for (x = 1; x < 16; x++) {
554 sprintf(buf, "%s%02x", buf, di[x]);
555 }
556
557 /* And now we should fire back a response */
558 g_snprintf(buf2, 4096, "USR %d %s S %s\n", trId, mdata->policy, buf);
559 write(mdata->fd, buf2, strlen(buf2));
560
561
562 msn_read_line(&buf, mdata->fd);
563
564 results = g_strsplit(buf, " ", 0);
565
566 if ((strcasecmp("USR", results[0]) == 0) && (strcasecmp("OK", results[2]) == 0)) {
567 mdata->friendly = g_strdup(results[4]);
568 g_strfreev(results);
569 }
570 else {
571 g_strfreev(results);
572 hide_login_progress(gc, "Error signing on!");
573 signoff(gc);
574 return;
575
576 }
577 set_login_progress(gc, 3, "Getting Config");
578 g_snprintf(buf, 4096, "SYN %d 0\n", trId);
579 write(mdata->fd, buf, strlen(buf));
580
581 /* Go online */
582 g_snprintf(buf, 4096, "CHG %d NLN\n", trId);
583 write(mdata->fd, buf, strlen(buf));
584
585 account_online(gc);
586 serv_finish_login(gc);
587
588 if (bud_list_cache_exists(gc))
589 do_import(NULL, gc);
590
591 /* We want to do this so that we can read what's going on */
592 gc->inpa = gdk_input_add(mdata->fd, GDK_INPUT_READ, msn_handler, gc);
593 }
594
595 void msn_send_im(struct gaim_connection *gc, char *who, char *message, int away) {
596 struct msn_conn *mc;
597 struct msn_data *mdata;
598 time_t trId = time((time_t *)NULL);
599 char *buf;
600
601 mdata = (struct msn_data *)gc->proto_data;
602 mc = find_msn_conn_by_user(who);
603
604 if (mc == NULL)
605 {
606 gchar buf2[4096];
607 gchar *address;
608 gchar *auth;
609 gchar **resps;
610
611 /* Request a new switchboard connection */
612 g_snprintf(buf2, 4096, "XFR %d SB\n", trId);
613 write(mdata->fd, buf2, strlen(buf2));
614
615 /* Read the results */
616 msn_read_line(&buf2, mdata->fd);
617
618 resps = g_strsplit(buf2, " ", 0);
619
620 address = g_strdup(resps[3]);
621 auth = g_strdup(resps[5]);
622 g_strfreev(resps);
623
624 resps = g_strsplit(address, ":", 0);
625
626 mc = g_new0(struct msn_conn, 1);
627
628 if (!(mc->fd = msn_connect(resps[0], atoi(resps[1])))) {
629 g_strfreev(resps);
630 g_free(address);
631 g_free(auth);
632 g_free(mc);
633 return;
634 }
635
636 /* Looks like we got connected ok. Now, let's verify */
637 g_snprintf(buf2, 4096, "USR %d %s %s\n", trId, gc->username, auth);
638 write(mc->fd, buf2, strlen(buf2));
639
640 /* Read the results */
641 msn_read_line(&buf2, mc->fd);
642 g_strfreev(resps);
643
644 resps = g_strsplit(buf2, " ", 0);
645
646 if (!(strcasecmp("OK", resps[2]) == 0)) {
647 g_free(auth);
648 g_free(address);
649 g_strfreev(resps);
650 g_free(mc);
651 return;
652 }
653
654 mc->user = g_strdup(who);
655 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_READ, msn_handler, gc);
656
657 msn_connections = g_slist_append(msn_connections, mc);
658
659 /* Now we must invite our new user to the switchboard session */
660 g_snprintf(buf2, 4096, "CAL %d %s\n", trId, who);
661 write(mc->fd, buf2, strlen(buf2));
662
663 /* FIXME: This causes a delay. I will make some sort of queing feature to prevent
664 * this from being needed */
665
666 while (!strstr(buf2, "JOI")) {
667 msn_read_line(&buf2, mc->fd);
668 }
669
670 g_free(auth);
671 g_free(address);
672 g_strfreev(resps);
673
674 }
675
676 /* Always practice safe sets :-) */
677 buf = (gchar *)g_malloc(sizeof(gchar) * (strlen(message) + strlen(MIME_HEADER) + 64));
678
679 g_snprintf(buf, strlen(message) + strlen(MIME_HEADER) + 64, "MSG %d N %d\r\n%s%s", trId, strlen(message)+strlen(MIME_HEADER), MIME_HEADER, message);
680
681 write(mc->fd, buf, strlen(buf));
682
683 g_free(buf);
684 }
685
686 static struct prpl *my_protocol = NULL;
687
688 void msn_init(struct prpl *ret) {
689 ret->protocol = PROTO_MSN;
690 ret->name = msn_name;
691 ret->list_icon = NULL;
692 ret->action_menu = NULL;
693 ret->user_opts = NULL;
694 ret->login = msn_login;
695 ret->close = NULL;
696 ret->send_im = msn_send_im;
697 ret->set_info = NULL;
698 ret->get_info = NULL;
699 ret->set_away = NULL;
700 ret->get_away_msg = NULL;
701 ret->set_dir = NULL;
702 ret->get_dir = NULL;
703 ret->dir_search = NULL;
704 ret->set_idle = NULL;
705 ret->change_passwd = NULL;
706 ret->add_buddy = msn_add_buddy;
707 ret->add_buddies = NULL;
708 ret->remove_buddy = msn_remove_buddy;
709 ret->add_permit = msn_add_permit;
710 ret->rem_permit = msn_rem_permit;
711 ret->add_deny = NULL;
712 ret->warn = NULL;
713 ret->accept_chat = NULL;
714 ret->join_chat = NULL;
715 ret->chat_invite = NULL;
716 ret->chat_leave = NULL;
717 ret->chat_whisper = NULL;
718 ret->chat_send = NULL;
719 ret->keepalive = NULL;
720
721 my_protocol = ret;
722 }
723
724 char *gaim_plugin_init(GModule *handle) {
725 load_protocol(msn_init);
726 return NULL;
727 }
728
729 void gaim_plugin_remove() {
730 struct prpl *p = find_prpl(PROTO_MSN);
731 if (p == my_protocol)
732 unload_protocol(p);
733 }