source: protocols/msn/ns.c @ 913a663

Last change on this file since 913a663 was 913a663, checked in by dequis <dx@…>, at 2015-04-10T17:10:41Z

msn: implement the rest of the http gateway support, enable by default

  • Property mode set to 100644
File size: 16.8 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], "OUT") == 0) {
307                int allow_reconnect = TRUE;
308
309                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
310                        imcb_error(ic, "Someone else logged in with your account");
311                        allow_reconnect = FALSE;
312                } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) {
313                        imcb_error(ic, "Terminating session because of server shutdown");
314                } else {
315                        imcb_error(ic, "Session terminated by remote server (%s)",
316                                   cmd[1] ? cmd[1] : "reason unknown)");
317                }
318
319                imc_logout(ic, allow_reconnect);
320                return(0);
321        } else if (strcmp(cmd[0], "GCF") == 0) {
322                /* Coming up is cmd[2] bytes of stuff we're supposed to
323                   censore. Meh. */
324                handler->msglen = atoi(cmd[2]);
325        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
326                if (num_parts >= 3) {
327                        handler->msglen = atoi(cmd[2]);
328                }
329        } else if (strcmp(cmd[0], "QNG") == 0) {
330                ic->flags |= OPT_PONGED;
331        } else if (g_ascii_isdigit(cmd[0][0])) {
332                int num = atoi(cmd[0]);
333                const struct msn_status_code *err = msn_status_by_number(num);
334
335                imcb_error(ic, "Error reported by MSN server: %s", err->text);
336
337                if (err->flags & STATUS_FATAL) {
338                        imc_logout(ic, TRUE);
339                        return(0);
340                }
341
342                /* Oh yes, errors can have payloads too now. Discard them for now. */
343                if (num_parts >= 3) {
344                        handler->msglen = atoi(cmd[2]);
345                }
346        } else {
347                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
348        }
349
350        return(1);
351}
352
353int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
354{
355        struct im_connection *ic = handler->ic;
356        char *body;
357        int blen = 0;
358
359        if (!num_parts) {
360                return(1);
361        }
362
363        if ((body = strstr(msg, "\r\n\r\n"))) {
364                body += 4;
365                blen = msglen - (body - msg);
366        }
367
368        if (strcmp(cmd[0], "MSG") == 0) {
369                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
370                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
371
372                        if (!ct) {
373                                return(1);
374                        }
375
376                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
377                                char *mtype;
378                                char *arg1;
379
380                                if (!body) {
381                                        return(1);
382                                }
383
384                                mtype = get_rfc822_header(body, "Type:", blen);
385                                arg1 = get_rfc822_header(body, "Arg1:", blen);
386
387                                if (mtype && strcmp(mtype, "1") == 0) {
388                                        if (arg1) {
389                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
390                                                         arg1);
391                                        }
392                                }
393
394                                g_free(arg1);
395                                g_free(mtype);
396                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
397                                /* We don't care about this profile for now... */
398                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
399                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
400                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
401                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
402
403                                        if (inbox && folders) {
404                                                imcb_log(ic,
405                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
406                                                         folders);
407                                        }
408
409                                        g_free(inbox);
410                                        g_free(folders);
411                                }
412                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
413                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
414                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
415                                        char *fromname = get_rfc822_header(body, "From:", blen);
416
417                                        if (from && fromname) {
418                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
419                                                         from);
420                                        }
421
422                                        g_free(from);
423                                        g_free(fromname);
424                                }
425                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
426                                /* Notification that a message has been read... Ignore it */
427                        } else {
428                                debug("Can't handle %s packet from notification server", ct);
429                        }
430
431                        g_free(ct);
432                }
433        } else if (strcmp(cmd[0], "ADL") == 0) {
434                struct xt_node *adl, *d, *c;
435
436                if (!(adl = xt_from_string(msg, msglen))) {
437                        return 1;
438                }
439
440                for (d = adl->children; d; d = d->next) {
441                        char *dn;
442                        if (strcmp(d->name, "d") != 0 ||
443                            (dn = xt_find_attr(d, "n")) == NULL) {
444                                continue;
445                        }
446                        for (c = d->children; c; c = c->next) {
447                                bee_user_t *bu;
448                                struct msn_buddy_data *bd;
449                                char *cn, *handle, *f, *l;
450                                int flags;
451
452                                if (strcmp(c->name, "c") != 0 ||
453                                    (l = xt_find_attr(c, "l")) == NULL ||
454                                    (cn = xt_find_attr(c, "n")) == NULL) {
455                                        continue;
456                                }
457
458                                /* FIXME: Use "t" here, guess I should just add it
459                                   as a prefix like elsewhere in the protocol. */
460                                handle = g_strdup_printf("%s@%s", cn, dn);
461                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
462                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
463                                        g_free(handle);
464                                        continue;
465                                }
466                                g_free(handle);
467                                bd = bu->data;
468
469                                if ((f = xt_find_attr(c, "f"))) {
470                                        http_decode(f);
471                                        imcb_rename_buddy(ic, bu->handle, f);
472                                }
473
474                                flags = atoi(l) & 15;
475                                if (bd->flags != flags) {
476                                        bd->flags = flags;
477                                        msn_buddy_ask(bu);
478                                }
479                        }
480                }
481        } else if (strcmp(cmd[0], "SDG") == 0) {
482                char **parts = g_strsplit(msg, "\r\n\r\n", 4);
483                char *from = NULL;
484                char *mt = NULL;
485                char *who = NULL;
486                char *s = NULL;
487
488                if ((from = get_rfc822_header(parts[0], "From", 0)) &&
489                    (mt = get_rfc822_header(parts[2], "Message-Type", 0)) &&
490                    (s = strchr(from, ';'))) {
491
492                        who = g_strndup(from + 2, s - from - 2);
493
494                        if (strcmp(mt, "Control/Typing") == 0) {
495                                imcb_buddy_typing(ic, who, OPT_TYPING);
496                        } else if (strcmp(mt, "Text") == 0) {
497                                imcb_buddy_msg(ic, who, parts[3], 0, 0);
498                        }
499                }
500                g_free(from);
501                g_free(mt);
502                g_free(who);
503                return 1;
504        }
505
506        return 1;
507}
508
509void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
510{
511        struct msn_data *md;
512
513        /* Dead connection? */
514        if (g_slist_find(msn_connections, ic) == NULL) {
515                return;
516        }
517
518        md = ic->proto_data;
519
520        if (token) {
521                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
522        } else {
523                imcb_error(ic, "Error during Passport authentication: %s", error);
524                imc_logout(ic, TRUE);
525        }
526}
527
528void msn_auth_got_contact_list(struct im_connection *ic)
529{
530        /* Dead connection? */
531        if (g_slist_find(msn_connections, ic) == NULL) {
532                return;
533        }
534
535        msn_ns_send_adl_start(ic);
536        msn_ns_finish_login(ic);
537}
538
539static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
540{
541        struct xt_node *adl = data, *d, *c, *s;
542        struct bee_user *bu = value;
543        struct msn_buddy_data *bd = bu->data;
544        struct msn_data *md = bu->ic->proto_data;
545        char handle[strlen(bu->handle) + 1];
546        char *domain;
547        char l[4];
548
549        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
550                return FALSE;
551        }
552
553        strcpy(handle, bu->handle);
554        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
555                return FALSE;
556        }
557        *domain = '\0';
558        domain++;
559
560        if ((d = adl->children) == NULL ||
561            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
562                d = xt_new_node("d", NULL, NULL);
563                xt_add_attr(d, "n", domain);
564                xt_insert_child(adl, d);
565        }
566
567        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
568        c = xt_new_node("c", NULL, NULL);
569        xt_add_attr(c, "n", handle);
570        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
571        s = xt_new_node("s", NULL, NULL);
572        xt_add_attr(s, "n", "IM");
573        xt_add_attr(s, "l", l);
574        xt_insert_child(c, s);
575        xt_insert_child(d, c);
576
577        /* Do this in batches of 100. */
578        bd->flags |= MSN_BUDDY_ADL_SYNCED;
579        return (--md->adl_todo % 140) == 0;
580}
581
582static void msn_ns_send_adl(struct im_connection *ic)
583{
584        struct xt_node *adl;
585        struct msn_data *md = ic->proto_data;
586        char *adls;
587
588        adl = xt_new_node("ml", NULL, NULL);
589        xt_add_attr(adl, "l", "1");
590        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
591        if (adl->children == NULL) {
592                /* This tells the caller that we're done now. */
593                md->adl_todo = -1;
594                xt_free_node(adl);
595                return;
596        }
597
598        adls = xt_to_string(adl);
599        xt_free_node(adl);
600        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
601        g_free(adls);
602}
603
604static void msn_ns_send_adl_start(struct im_connection *ic)
605{
606        struct msn_data *md;
607        GSList *l;
608
609        /* Dead connection? */
610        if (g_slist_find(msn_connections, ic) == NULL) {
611                return;
612        }
613
614        md = ic->proto_data;
615        md->adl_todo = 0;
616        for (l = ic->bee->users; l; l = l->next) {
617                bee_user_t *bu = l->data;
618                struct msn_buddy_data *bd = bu->data;
619
620                if (bu->ic != ic || (bd->flags & 7) == 0) {
621                        continue;
622                }
623
624                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
625                md->adl_todo++;
626        }
627
628        msn_ns_send_adl(ic);
629}
630
631int msn_ns_finish_login(struct im_connection *ic)
632{
633        struct msn_data *md = ic->proto_data;
634
635        if (ic->flags & OPT_LOGGED_IN) {
636                return 1;
637        }
638
639        if (md->adl_todo < 0) {
640                md->flags |= MSN_DONE_ADL;
641        }
642
643        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
644                imcb_connected(ic);
645        }
646
647        return 1;
648}
649
650// TODO: typing notifications, nudges lol, etc
651int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
652{
653        struct msn_data *md = ic->proto_data;
654        int retval = 0;
655        char *buf;
656
657        if (strncmp(text, "\r\r\r", 3) == 0) {
658                /* Err. Shouldn't happen but I guess it can. Don't send others
659                   any of the "SHAKE THAT THING" messages. :-D */
660                return 1;
661        }
662
663        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text);
664        retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
665        g_free(buf);
666        return retval;
667}
Note: See TracBrowser for help on using the repository browser.