source: protocols/msn/ns.c @ 89e0c94

Last change on this file since 89e0c94 was 89e0c94, checked in by dequis <dx@…>, at 2015-03-19T10:25:33Z

get_rfc822_header: allow headers at the end of a string

  • Property mode set to 100644
File size: 17.0 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* MSN module - Notification server callbacks                           */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#include <ctype.h>
27#include <sys/utsname.h>
28#include "nogaim.h"
29#include "msn.h"
30#include "md5.h"
31#include "sha1.h"
32#include "soap.h"
33#include "xmltree.h"
34
35static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond);
36static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond);
37
38static void msn_ns_send_adl_start(struct im_connection *ic);
39static void msn_ns_send_adl(struct im_connection *ic);
40
41int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...)
42{
43        struct msn_data *md = ic->proto_data;
44        va_list params;
45        char *out;
46        size_t len;
47        int st;
48
49        va_start(params, fmt);
50        out = g_strdup_vprintf(fmt, params);
51        va_end(params);
52
53        if (fd < 0) {
54                fd = md->fd;
55        }
56
57        if (getenv("BITLBEE_DEBUG")) {
58                fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, out);
59        }
60
61        len = strlen(out);
62
63        if (md->is_http) {
64                st = len;
65                msn_gw_write(md->gw, out, len);
66        } else {
67                st = write(fd, out, len);
68        }
69
70        g_free(out);
71        if (st != len) {
72                imcb_error(ic, "Short write() to main server");
73                imc_logout(ic, TRUE);
74                return 0;
75        }
76
77        return 1;
78}
79
80gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
81{
82        struct msn_data *handler = ic->proto_data;
83
84        if (handler->fd >= 0) {
85                closesocket(handler->fd);
86        }
87
88        if (handler->is_http) {
89                handler->gw = msn_gw_new(handler);
90                handler->gw->callback = msn_ns_callback;
91                msn_ns_connected(handler, -1, B_EV_IO_READ);
92        } else {
93                handler->fd = proxy_connect(host, port, msn_ns_connected, handler);
94                if (handler->fd < 0) {
95                        imcb_error(ic, "Could not connect to server");
96                        imc_logout(ic, TRUE);
97                        return FALSE;
98                }
99        }
100
101        return TRUE;
102}
103
104static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
105{
106        struct msn_data *md = data;
107        struct msn_data *handler = md;
108        struct im_connection *ic = md->ic;
109
110        if (source == -1 && !md->is_http) {
111                imcb_error(ic, "Could not connect to server");
112                imc_logout(ic, TRUE);
113                return FALSE;
114        }
115
116        g_free(handler->rxq);
117        handler->rxlen = 0;
118        handler->rxq = g_new0(char, 1);
119
120        if (md->uuid == NULL) {
121                struct utsname name;
122                sha1_state_t sha[1];
123
124                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
125                sha1_init(sha);
126                sha1_append(sha, (void *) "BitlBee", 7);
127                if (uname(&name) == 0) {
128                        sha1_append(sha, (void *) name.nodename, strlen(name.nodename));
129                }
130                sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user));
131                md->uuid = sha1_random_uuid(sha);
132                memcpy(md->uuid, "b171be3e", 8);   /* :-P */
133        }
134
135        if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
136                if (!handler->is_http) {
137                        handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler);
138                }
139                imcb_log(ic, "Connected to server, waiting for reply");
140        }
141
142        return FALSE;
143}
144
145void msn_ns_close(struct msn_data *handler)
146{
147        if (handler->gw) {
148                if (handler->gw->waiting) {
149                        /* mark it as closed, let the request callback clean it */
150                        handler->gw->open = FALSE;
151                } else {
152                        msn_gw_free(handler->gw);
153                }
154        }
155        if (handler->fd >= 0) {
156                closesocket(handler->fd);
157                b_event_remove(handler->inpa);
158        }
159
160        handler->fd = handler->inpa = -1;
161        g_free(handler->rxq);
162        g_free(handler->cmd_text);
163
164        handler->rxlen = 0;
165        handler->rxq = NULL;
166        handler->cmd_text = NULL;
167}
168
169static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
170{
171        struct msn_data *handler = data;
172        struct im_connection *ic = handler->ic;
173        char *bytes;
174        int st;
175
176        if (handler->is_http) {
177                st = msn_gw_read(handler->gw, &bytes);
178        } else {
179                bytes = g_malloc(1024);
180                st = read(handler->fd, bytes, 1024);
181        }
182
183        if (st <= 0) {
184                imcb_error(ic, "Error while reading from server");
185                imc_logout(ic, TRUE);
186                return FALSE;
187        }
188
189        msn_queue_feed(handler, bytes, st);
190
191        g_free(bytes);
192
193        /* Ignore ret == 0, it's already disconnected then. */
194        msn_handler(handler);
195
196        return TRUE;
197       
198}
199
200int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts)
201{
202        struct im_connection *ic = handler->ic;
203        struct msn_data *md = handler;
204
205        if (num_parts == 0) {
206                /* Hrrm... Empty command...? Ignore? */
207                return(1);
208        }
209
210        if (strcmp(cmd[0], "VER") == 0) {
211                if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) {
212                        imcb_error(ic, "Unsupported protocol");
213                        imc_logout(ic, FALSE);
214                        return(0);
215                }
216
217                return(msn_ns_write(ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s VmVyc2lvbjogMQ0KWGZyQ291bnQ6IDINClhmclNlbnRVVENUaW1lOiA2MzU2MTQ3OTU5NzgzOTAwMDANCklzR2VvWGZyOiB0cnVlDQo=\r\n",
218                                    ++md->trId, ic->acc->user));
219        } else if (strcmp(cmd[0], "CVR") == 0) {
220                /* We don't give a damn about the information we just received */
221                return msn_ns_write(ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user);
222        } else if (strcmp(cmd[0], "XFR") == 0) {
223                char *server;
224                int port;
225
226                if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) {
227                        b_event_remove(handler->inpa);
228                        handler->inpa = -1;
229
230                        server = strchr(cmd[3], ':');
231                        if (!server) {
232                                imcb_error(ic, "Syntax error");
233                                imc_logout(ic, TRUE);
234                                return(0);
235                        }
236                        *server = 0;
237                        port = atoi(server + 1);
238                        server = cmd[3];
239
240                        imcb_log(ic, "Transferring to other server");
241                        return msn_ns_connect(ic, server, port);
242                } else {
243                        imcb_error(ic, "Syntax error");
244                        imc_logout(ic, TRUE);
245                        return(0);
246                }
247        } else if (strcmp(cmd[0], "USR") == 0) {
248                if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 &&
249                    strcmp(cmd[3], "S") == 0) {
250                        g_free(md->pp_policy);
251                        md->pp_policy = g_strdup(cmd[4]);
252                        msn_soap_passport_sso_request(ic, cmd[5]);
253                } else if (strcmp(cmd[2], "OK") == 0) {
254                        /* If the number after the handle is 0, the e-mail
255                           address is unverified, which means we can't change
256                           the display name. */
257                        if (cmd[4][0] == '0') {
258                                md->flags |= MSN_EMAIL_UNVERIFIED;
259                        }
260
261                        imcb_log(ic, "Authenticated, getting buddy list");
262                        msn_soap_memlist_request(ic);
263                } else {
264                        imcb_error(ic, "Unknown authentication type");
265                        imc_logout(ic, FALSE);
266                        return(0);
267                }
268        } else if (strcmp(cmd[0], "MSG") == 0) {
269                if (num_parts < 4) {
270                        imcb_error(ic, "Syntax error");
271                        imc_logout(ic, TRUE);
272                        return(0);
273                }
274
275                handler->msglen = atoi(cmd[3]);
276
277                if (handler->msglen <= 0) {
278                        imcb_error(ic, "Syntax error");
279                        imc_logout(ic, TRUE);
280                        return(0);
281                }
282        } else if (strcmp(cmd[0], "ADL") == 0) {
283                if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) {
284                        msn_ns_send_adl(ic);
285                        return msn_ns_finish_login(ic);
286                } else if (num_parts >= 3) {
287                        handler->msglen = atoi(cmd[2]);
288                }
289        } else if (strcmp(cmd[0], "CHL") == 0) {
290                char *resp;
291                int st;
292
293                if (num_parts < 3) {
294                        imcb_error(ic, "Syntax error");
295                        imc_logout(ic, TRUE);
296                        return(0);
297                }
298
299                resp = msn_p11_challenge(cmd[2]);
300
301                st =  msn_ns_write(ic, -1, "QRY %d %s %zd\r\n%s",
302                                   ++md->trId, MSNP11_PROD_ID,
303                                   strlen(resp), resp);
304                g_free(resp);
305                return st;
306        } else if (strcmp(cmd[0], "QRY") == 0) {
307                /* CONGRATULATIONS */
308        } else if (strcmp(cmd[0], "OUT") == 0) {
309                int allow_reconnect = TRUE;
310
311                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
312                        imcb_error(ic, "Someone else logged in with your account");
313                        allow_reconnect = FALSE;
314                } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) {
315                        imcb_error(ic, "Terminating session because of server shutdown");
316                } else {
317                        imcb_error(ic, "Session terminated by remote server (%s)",
318                                   cmd[1] ? cmd[1] : "reason unknown)");
319                }
320
321                imc_logout(ic, allow_reconnect);
322                return(0);
323        } else if (strcmp(cmd[0], "GCF") == 0) {
324                /* Coming up is cmd[2] bytes of stuff we're supposed to
325                   censore. Meh. */
326                handler->msglen = atoi(cmd[2]);
327        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
328                if (num_parts >= 3) {
329                        handler->msglen = atoi(cmd[2]);
330                }
331        } else if (strcmp(cmd[0], "PUT") == 0) {
332                if (num_parts >= 4) {
333                        handler->msglen = atoi(cmd[3]);
334                }
335        } else if (strcmp(cmd[0], "QNG") == 0) {
336                ic->flags |= OPT_PONGED;
337        } else if (g_ascii_isdigit(cmd[0][0])) {
338                int num = atoi(cmd[0]);
339                const struct msn_status_code *err = msn_status_by_number(num);
340
341                imcb_error(ic, "Error reported by MSN server: %s", err->text);
342
343                if (err->flags & STATUS_FATAL) {
344                        imc_logout(ic, TRUE);
345                        return(0);
346                }
347
348                /* Oh yes, errors can have payloads too now. Discard them for now. */
349                if (num_parts >= 3) {
350                        handler->msglen = atoi(cmd[2]);
351                }
352        } else {
353                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
354        }
355
356        return(1);
357}
358
359int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
360{
361        struct im_connection *ic = handler->ic;
362        char *body;
363        int blen = 0;
364
365        if (!num_parts) {
366                return(1);
367        }
368
369        if ((body = strstr(msg, "\r\n\r\n"))) {
370                body += 4;
371                blen = msglen - (body - msg);
372        }
373
374        if (strcmp(cmd[0], "MSG") == 0) {
375                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
376                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
377
378                        if (!ct) {
379                                return(1);
380                        }
381
382                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
383                                char *mtype;
384                                char *arg1;
385
386                                if (!body) {
387                                        return(1);
388                                }
389
390                                mtype = get_rfc822_header(body, "Type:", blen);
391                                arg1 = get_rfc822_header(body, "Arg1:", blen);
392
393                                if (mtype && strcmp(mtype, "1") == 0) {
394                                        if (arg1) {
395                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
396                                                         arg1);
397                                        }
398                                }
399
400                                g_free(arg1);
401                                g_free(mtype);
402                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
403                                /* We don't care about this profile for now... */
404                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
405                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
406                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
407                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
408
409                                        if (inbox && folders) {
410                                                imcb_log(ic,
411                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
412                                                         folders);
413                                        }
414
415                                        g_free(inbox);
416                                        g_free(folders);
417                                }
418                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
419                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
420                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
421                                        char *fromname = get_rfc822_header(body, "From:", blen);
422
423                                        if (from && fromname) {
424                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
425                                                         from);
426                                        }
427
428                                        g_free(from);
429                                        g_free(fromname);
430                                }
431                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
432                                /* Notification that a message has been read... Ignore it */
433                        } else {
434                                debug("Can't handle %s packet from notification server", ct);
435                        }
436
437                        g_free(ct);
438                }
439        } else if (strcmp(cmd[0], "ADL") == 0) {
440                struct xt_node *adl, *d, *c;
441
442                if (!(adl = xt_from_string(msg, msglen))) {
443                        return 1;
444                }
445
446                for (d = adl->children; d; d = d->next) {
447                        char *dn;
448                        if (strcmp(d->name, "d") != 0 ||
449                            (dn = xt_find_attr(d, "n")) == NULL) {
450                                continue;
451                        }
452                        for (c = d->children; c; c = c->next) {
453                                bee_user_t *bu;
454                                struct msn_buddy_data *bd;
455                                char *cn, *handle, *f, *l;
456                                int flags;
457
458                                if (strcmp(c->name, "c") != 0 ||
459                                    (l = xt_find_attr(c, "l")) == NULL ||
460                                    (cn = xt_find_attr(c, "n")) == NULL) {
461                                        continue;
462                                }
463
464                                /* FIXME: Use "t" here, guess I should just add it
465                                   as a prefix like elsewhere in the protocol. */
466                                handle = g_strdup_printf("%s@%s", cn, dn);
467                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
468                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
469                                        g_free(handle);
470                                        continue;
471                                }
472                                g_free(handle);
473                                bd = bu->data;
474
475                                if ((f = xt_find_attr(c, "f"))) {
476                                        http_decode(f);
477                                        imcb_rename_buddy(ic, bu->handle, f);
478                                }
479
480                                flags = atoi(l) & 15;
481                                if (bd->flags != flags) {
482                                        bd->flags = flags;
483                                        msn_buddy_ask(bu);
484                                }
485                        }
486                }
487        } else if (strcmp(cmd[0], "SDG") == 0) {
488                char **parts = g_strsplit(msg, "\r\n\r\n", 4);
489                char *from = NULL;
490                char *mt = NULL;
491                char *who = NULL;
492                char *s = NULL;
493
494                if ((from = get_rfc822_header(parts[0], "From", 0)) &&
495                    (mt = get_rfc822_header(parts[2], "Message-Type", 0)) &&
496                    (s = strchr(from, ';'))) {
497
498                        who = g_strndup(from + 2, s - from - 2);
499
500                        if (strcmp(mt, "Control/Typing") == 0) {
501                                imcb_buddy_typing(ic, who, OPT_TYPING);
502                        } else if (strcmp(mt, "Text") == 0) {
503                                imcb_buddy_msg(ic, who, parts[3], 0, 0);
504                        }
505                }
506                g_free(from);
507                g_free(mt);
508                g_free(who);
509                return 1;
510        }
511
512        return 1;
513}
514
515void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
516{
517        struct msn_data *md;
518
519        /* Dead connection? */
520        if (g_slist_find(msn_connections, ic) == NULL) {
521                return;
522        }
523
524        md = ic->proto_data;
525
526        if (token) {
527                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
528        } else {
529                imcb_error(ic, "Error during Passport authentication: %s", error);
530                imc_logout(ic, TRUE);
531        }
532}
533
534void msn_auth_got_contact_list(struct im_connection *ic)
535{
536        /* Dead connection? */
537        if (g_slist_find(msn_connections, ic) == NULL) {
538                return;
539        }
540
541        msn_ns_send_adl_start(ic);
542        msn_ns_finish_login(ic);
543}
544
545static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
546{
547        struct xt_node *adl = data, *d, *c, *s;
548        struct bee_user *bu = value;
549        struct msn_buddy_data *bd = bu->data;
550        struct msn_data *md = bu->ic->proto_data;
551        char handle[strlen(bu->handle) + 1];
552        char *domain;
553        char l[4];
554
555        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
556                return FALSE;
557        }
558
559        strcpy(handle, bu->handle);
560        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
561                return FALSE;
562        }
563        *domain = '\0';
564        domain++;
565
566        if ((d = adl->children) == NULL ||
567            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
568                d = xt_new_node("d", NULL, NULL);
569                xt_add_attr(d, "n", domain);
570                xt_insert_child(adl, d);
571        }
572
573        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
574        c = xt_new_node("c", NULL, NULL);
575        xt_add_attr(c, "n", handle);
576        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
577        s = xt_new_node("s", NULL, NULL);
578        xt_add_attr(s, "n", "IM");
579        xt_add_attr(s, "l", l);
580        xt_insert_child(c, s);
581        xt_insert_child(d, c);
582
583        /* Do this in batches of 100. */
584        bd->flags |= MSN_BUDDY_ADL_SYNCED;
585        return (--md->adl_todo % 140) == 0;
586}
587
588static void msn_ns_send_adl(struct im_connection *ic)
589{
590        struct xt_node *adl;
591        struct msn_data *md = ic->proto_data;
592        char *adls;
593
594        adl = xt_new_node("ml", NULL, NULL);
595        xt_add_attr(adl, "l", "1");
596        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
597        if (adl->children == NULL) {
598                /* This tells the caller that we're done now. */
599                md->adl_todo = -1;
600                xt_free_node(adl);
601                return;
602        }
603
604        adls = xt_to_string(adl);
605        xt_free_node(adl);
606        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
607        g_free(adls);
608}
609
610static void msn_ns_send_adl_start(struct im_connection *ic)
611{
612        struct msn_data *md;
613        GSList *l;
614
615        /* Dead connection? */
616        if (g_slist_find(msn_connections, ic) == NULL) {
617                return;
618        }
619
620        md = ic->proto_data;
621        md->adl_todo = 0;
622        for (l = ic->bee->users; l; l = l->next) {
623                bee_user_t *bu = l->data;
624                struct msn_buddy_data *bd = bu->data;
625
626                if (bu->ic != ic || (bd->flags & 7) == 0) {
627                        continue;
628                }
629
630                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
631                md->adl_todo++;
632        }
633
634        msn_ns_send_adl(ic);
635}
636
637int msn_ns_finish_login(struct im_connection *ic)
638{
639        struct msn_data *md = ic->proto_data;
640
641        if (ic->flags & OPT_LOGGED_IN) {
642                return 1;
643        }
644
645        if (md->adl_todo < 0) {
646                md->flags |= MSN_DONE_ADL;
647        }
648
649        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
650                imcb_connected(ic);
651        }
652
653        return 1;
654}
655
656// TODO: typing notifications, nudges lol, etc
657int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
658{
659        struct msn_data *md = ic->proto_data;
660        int retval = 0;
661        char *buf;
662
663        if (strncmp(text, "\r\r\r", 3) == 0) {
664                /* Err. Shouldn't happen but I guess it can. Don't send others
665                   any of the "SHAKE THAT THING" messages. :-D */
666                return 1;
667        }
668
669        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text);
670        retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
671        g_free(buf);
672        return retval;
673}
Note: See TracBrowser for help on using the repository browser.