source: protocols/msn/ns.c @ d80822c

Last change on this file since d80822c was 11e42dc, checked in by dequis <dx@…>, at 2015-04-10T17:10:40Z

msn: removed switchboards, implemented SDG message

  • Property mode set to 100644
File size: 21.6 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        st = write(fd, out, len);
63        g_free(out);
64        if (st != len) {
65                imcb_error(ic, "Short write() to main server");
66                imc_logout(ic, TRUE);
67                return 0;
68        }
69
70        return 1;
71}
72
73gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
74{
75        struct msn_data *handler = ic->proto_data;
76
77        if (handler->fd >= 0) {
78                closesocket(handler->fd);
79        }
80
81        handler->fd = proxy_connect(host, port, msn_ns_connected, handler);
82        if (handler->fd < 0) {
83                imcb_error(ic, "Could not connect to server");
84                imc_logout(ic, TRUE);
85                return FALSE;
86        }
87
88        return TRUE;
89}
90
91static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
92{
93        struct msn_data *md = data;
94        struct msn_data *handler = md;
95        struct im_connection *ic = md->ic;
96
97        if (source == -1) {
98                imcb_error(ic, "Could not connect to server");
99                imc_logout(ic, TRUE);
100                return FALSE;
101        }
102
103        g_free(handler->rxq);
104        handler->rxlen = 0;
105        handler->rxq = g_new0(char, 1);
106
107        if (md->uuid == NULL) {
108                struct utsname name;
109                sha1_state_t sha[1];
110
111                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
112                sha1_init(sha);
113                sha1_append(sha, (void *) "BitlBee", 7);
114                if (uname(&name) == 0) {
115                        sha1_append(sha, (void *) name.nodename, strlen(name.nodename));
116                }
117                sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user));
118                md->uuid = sha1_random_uuid(sha);
119                memcpy(md->uuid, "b171be3e", 8);   /* :-P */
120        }
121
122        if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
123                handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler);
124                imcb_log(ic, "Connected to server, waiting for reply");
125        }
126
127        return FALSE;
128}
129
130void msn_ns_close(struct msn_data *handler)
131{
132        if (handler->fd >= 0) {
133                closesocket(handler->fd);
134                b_event_remove(handler->inpa);
135        }
136
137        handler->fd = handler->inpa = -1;
138        g_free(handler->rxq);
139        g_free(handler->cmd_text);
140
141        handler->rxlen = 0;
142        handler->rxq = NULL;
143        handler->cmd_text = NULL;
144}
145
146static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
147{
148        struct msn_data *handler = data;
149        struct im_connection *ic = handler->ic;
150
151        if (msn_handler(handler) == -1) {  /* Don't do this on ret == 0, it's already done then. */
152                imcb_error(ic, "Error while reading from server");
153                imc_logout(ic, TRUE);
154
155                return FALSE;
156        } else {
157                return TRUE;
158        }
159}
160
161int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts)
162{
163        struct im_connection *ic = handler->ic;
164        struct msn_data *md = handler;
165
166        if (num_parts == 0) {
167                /* Hrrm... Empty command...? Ignore? */
168                return(1);
169        }
170
171        if (strcmp(cmd[0], "VER") == 0) {
172                if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) {
173                        imcb_error(ic, "Unsupported protocol");
174                        imc_logout(ic, FALSE);
175                        return(0);
176                }
177
178                return(msn_ns_write(ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s VmVyc2lvbjogMQ0KWGZyQ291bnQ6IDINClhmclNlbnRVVENUaW1lOiA2MzU2MTQ3OTU5NzgzOTAwMDANCklzR2VvWGZyOiB0cnVlDQo=\r\n",
179                                    ++md->trId, ic->acc->user));
180        } else if (strcmp(cmd[0], "CVR") == 0) {
181                /* We don't give a damn about the information we just received */
182                return msn_ns_write(ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user);
183        } else if (strcmp(cmd[0], "XFR") == 0) {
184                char *server;
185                int port;
186
187                if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) {
188                        b_event_remove(handler->inpa);
189                        handler->inpa = -1;
190
191                        server = strchr(cmd[3], ':');
192                        if (!server) {
193                                imcb_error(ic, "Syntax error");
194                                imc_logout(ic, TRUE);
195                                return(0);
196                        }
197                        *server = 0;
198                        port = atoi(server + 1);
199                        server = cmd[3];
200
201                        imcb_log(ic, "Transferring to other server");
202                        return msn_ns_connect(ic, server, port);
203                } else {
204                        imcb_error(ic, "Syntax error");
205                        imc_logout(ic, TRUE);
206                        return(0);
207                }
208        } else if (strcmp(cmd[0], "USR") == 0) {
209                if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 &&
210                    strcmp(cmd[3], "S") == 0) {
211                        g_free(md->pp_policy);
212                        md->pp_policy = g_strdup(cmd[4]);
213                        msn_soap_passport_sso_request(ic, cmd[5]);
214                } else if (strcmp(cmd[2], "OK") == 0) {
215                        /* If the number after the handle is 0, the e-mail
216                           address is unverified, which means we can't change
217                           the display name. */
218                        if (cmd[4][0] == '0') {
219                                md->flags |= MSN_EMAIL_UNVERIFIED;
220                        }
221
222                        imcb_log(ic, "Authenticated, getting buddy list");
223                        msn_soap_memlist_request(ic);
224                } else {
225                        imcb_error(ic, "Unknown authentication type");
226                        imc_logout(ic, FALSE);
227                        return(0);
228                }
229        } else if (strcmp(cmd[0], "MSG") == 0) {
230                if (num_parts < 4) {
231                        imcb_error(ic, "Syntax error");
232                        imc_logout(ic, TRUE);
233                        return(0);
234                }
235
236                handler->msglen = atoi(cmd[3]);
237
238                if (handler->msglen <= 0) {
239                        imcb_error(ic, "Syntax error");
240                        imc_logout(ic, TRUE);
241                        return(0);
242                }
243        } else if (strcmp(cmd[0], "ADL") == 0) {
244                if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) {
245                        msn_ns_send_adl(ic);
246                        return msn_ns_finish_login(ic);
247                } else if (num_parts >= 3) {
248                        handler->msglen = atoi(cmd[2]);
249                }
250        } else if (strcmp(cmd[0], "PRP") == 0) {
251                imcb_connected(ic);
252        } else if (strcmp(cmd[0], "CHL") == 0) {
253                char *resp;
254                int st;
255
256                if (num_parts < 3) {
257                        imcb_error(ic, "Syntax error");
258                        imc_logout(ic, TRUE);
259                        return(0);
260                }
261
262                resp = msn_p11_challenge(cmd[2]);
263
264                st =  msn_ns_write(ic, -1, "QRY %d %s %zd\r\n%s",
265                                   ++md->trId, MSNP11_PROD_ID,
266                                   strlen(resp), resp);
267                g_free(resp);
268                return st;
269        } else if (strcmp(cmd[0], "ILN") == 0 || strcmp(cmd[0], "NLN") == 0) {
270                const struct msn_away_state *st;
271                const char *handle;
272                int cap = 0;
273
274                if (num_parts < 6) {
275                        imcb_error(ic, "Syntax error");
276                        imc_logout(ic, TRUE);
277                        return(0);
278                }
279                /* ILN and NLN are more or less the same, except ILN has a trId
280                   at the start, and NLN has a capability field at the end.
281                   Does ILN still exist BTW? */
282                if (cmd[0][1] == 'I') {
283                        cmd++;
284                } else {
285                        cap = atoi(cmd[4]);
286                }
287
288                handle = msn_normalize_handle(cmd[2]);
289                if (strcmp(handle, ic->acc->user) == 0) {
290                        return 1; /* That's me! */
291
292                }
293                http_decode(cmd[3]);
294                imcb_rename_buddy(ic, handle, cmd[3]);
295
296                st = msn_away_state_by_code(cmd[1]);
297                if (!st) {
298                        /* FIXME: Warn/Bomb about unknown away state? */
299                        st = msn_away_state_list + 1;
300                }
301
302                imcb_buddy_status(ic, handle, OPT_LOGGED_IN |
303                                  (st != msn_away_state_list ? OPT_AWAY : 0) |
304                                  (cap & 1 ? OPT_MOBILE : 0),
305                                  st->name, NULL);
306
307        } else if (strcmp(cmd[0], "FLN") == 0) {
308                const char *handle;
309
310                if (cmd[1] == NULL) {
311                        return 1;
312                }
313
314                handle = msn_normalize_handle(cmd[1]);
315                imcb_buddy_status(ic, handle, 0, NULL, NULL);
316        } else if (strcmp(cmd[0], "OUT") == 0) {
317                int allow_reconnect = TRUE;
318
319                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
320                        imcb_error(ic, "Someone else logged in with your account");
321                        allow_reconnect = FALSE;
322                } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) {
323                        imcb_error(ic, "Terminating session because of server shutdown");
324                } else {
325                        imcb_error(ic, "Session terminated by remote server (%s)",
326                                   cmd[1] ? cmd[1] : "reason unknown)");
327                }
328
329                imc_logout(ic, allow_reconnect);
330                return(0);
331        } else if (strcmp(cmd[0], "IPG") == 0) {
332                imcb_error(ic, "Received IPG command, we don't handle them yet.");
333
334                handler->msglen = atoi(cmd[1]);
335
336                if (handler->msglen <= 0) {
337                        imcb_error(ic, "Syntax error");
338                        imc_logout(ic, TRUE);
339                        return(0);
340                }
341        }
342#if 0
343        else if (strcmp(cmd[0], "ADG") == 0) {
344                char *group = g_strdup(cmd[3]);
345                int groupnum, i;
346                GSList *l, *next;
347
348                http_decode(group);
349                if (sscanf(cmd[4], "%d", &groupnum) == 1) {
350                        if (groupnum >= md->groupcount) {
351                                md->grouplist = g_renew(char *, md->grouplist, groupnum + 1);
352                                for (i = md->groupcount; i <= groupnum; i++) {
353                                        md->grouplist[i] = NULL;
354                                }
355                                md->groupcount = groupnum + 1;
356                        }
357                        g_free(md->grouplist[groupnum]);
358                        md->grouplist[groupnum] = group;
359                } else {
360                        /* Shouldn't happen, but if it does, give up on the group. */
361                        g_free(group);
362                        imcb_error(ic, "Syntax error");
363                        imc_logout(ic, TRUE);
364                        return 0;
365                }
366
367                for (l = md->grpq; l; l = next) {
368                        struct msn_groupadd *ga = l->data;
369                        next = l->next;
370                        if (g_strcasecmp(ga->group, group) == 0) {
371                                if (!msn_buddy_list_add(ic, "FL", ga->who, ga->who, group)) {
372                                        return 0;
373                                }
374
375                                g_free(ga->group);
376                                g_free(ga->who);
377                                g_free(ga);
378                                md->grpq = g_slist_remove(md->grpq, ga);
379                        }
380                }
381        }
382#endif
383        else if (strcmp(cmd[0], "GCF") == 0) {
384                /* Coming up is cmd[2] bytes of stuff we're supposed to
385                   censore. Meh. */
386                handler->msglen = atoi(cmd[2]);
387        } else if (strcmp(cmd[0], "UBX") == 0) {
388                /* Status message. */
389                if (num_parts >= 3) {
390                        handler->msglen = atoi(cmd[2]);
391                }
392        } else if (strcmp(cmd[0], "NOT") == 0) {
393                /* Some kind of notification, poorly documented but
394                   apparently used to announce address book changes. */
395                if (num_parts >= 2) {
396                        handler->msglen = atoi(cmd[1]);
397                }
398        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
399                if (num_parts >= 3) {
400                        handler->msglen = atoi(cmd[2]);
401                }
402        } else if (strcmp(cmd[0], "UBM") == 0) {
403                if (num_parts >= 7) {
404                        handler->msglen = atoi(cmd[6]);
405                }
406        } else if (strcmp(cmd[0], "QNG") == 0) {
407                ic->flags |= OPT_PONGED;
408        } else if (g_ascii_isdigit(cmd[0][0])) {
409                int num = atoi(cmd[0]);
410                const struct msn_status_code *err = msn_status_by_number(num);
411
412                imcb_error(ic, "Error reported by MSN server: %s", err->text);
413
414                if (err->flags & STATUS_FATAL) {
415                        imc_logout(ic, TRUE);
416                        return(0);
417                }
418
419                /* Oh yes, errors can have payloads too now. Discard them for now. */
420                if (num_parts >= 3) {
421                        handler->msglen = atoi(cmd[2]);
422                }
423        } else {
424                /* debug( "Received unknown command from main server: %s", cmd[0] ); */
425        }
426
427        return(1);
428}
429
430int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
431{
432        struct im_connection *ic = handler->ic;
433        char *body;
434        int blen = 0;
435
436        if (!num_parts) {
437                return(1);
438        }
439
440        if ((body = strstr(msg, "\r\n\r\n"))) {
441                body += 4;
442                blen = msglen - (body - msg);
443        }
444
445        if (strcmp(cmd[0], "MSG") == 0) {
446                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
447                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
448
449                        if (!ct) {
450                                return(1);
451                        }
452
453                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
454                                char *mtype;
455                                char *arg1;
456
457                                if (!body) {
458                                        return(1);
459                                }
460
461                                mtype = get_rfc822_header(body, "Type:", blen);
462                                arg1 = get_rfc822_header(body, "Arg1:", blen);
463
464                                if (mtype && strcmp(mtype, "1") == 0) {
465                                        if (arg1) {
466                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
467                                                         arg1);
468                                        }
469                                }
470
471                                g_free(arg1);
472                                g_free(mtype);
473                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
474                                /* We don't care about this profile for now... */
475                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
476                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
477                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
478                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
479
480                                        if (inbox && folders) {
481                                                imcb_log(ic,
482                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
483                                                         folders);
484                                        }
485
486                                        g_free(inbox);
487                                        g_free(folders);
488                                }
489                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
490                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
491                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
492                                        char *fromname = get_rfc822_header(body, "From:", blen);
493
494                                        if (from && fromname) {
495                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
496                                                         from);
497                                        }
498
499                                        g_free(from);
500                                        g_free(fromname);
501                                }
502                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
503                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialmdatanotification", 37) == 0 ||
504                                   g_strncasecmp(ct, "text/x-msmsgsoimnotification", 28) == 0) {
505                                /* We received an offline message. Or at least notification
506                                   that there is one waiting for us. Fetching the message(s)
507                                   and purging them from the server is a lot of SOAPy work
508                                   not worth doing IMHO. Also I thought it was possible to
509                                   have the notification server send them directly, I was
510                                   pretty sure I saw Pidgin do it..
511
512                                   At least give a notification for now, seems like a
513                                   reasonable thing to do. Only problem is, they'll keep
514                                   coming back at login time until you read them using a
515                                   different client. :-( */
516
517                                char *xml = get_rfc822_header(body, "Mail-Data:", blen);
518                                struct xt_node *md, *m;
519
520                                if (!xml) {
521                                        return 1;
522                                }
523                                md = xt_from_string(xml, 0);
524                                if (!md) {
525                                        return 1;
526                                }
527
528                                for (m = md->children; (m = xt_find_node(m, "M")); m = m->next) {
529                                        struct xt_node *e = xt_find_node(m->children, "E");
530                                        struct xt_node *rt = xt_find_node(m->children, "RT");
531                                        struct tm tp;
532                                        time_t msgtime = 0;
533
534                                        if (!e || !e->text) {
535                                                continue;
536                                        }
537
538                                        memset(&tp, 0, sizeof(tp));
539                                        if (rt && rt->text &&
540                                            sscanf(rt->text, "%4d-%2d-%2dT%2d:%2d:%2d.",
541                                                   &tp.tm_year, &tp.tm_mon, &tp.tm_mday,
542                                                   &tp.tm_hour, &tp.tm_min, &tp.tm_sec) == 6) {
543                                                tp.tm_year -= 1900;
544                                                tp.tm_mon--;
545                                                msgtime = mktime_utc(&tp);
546
547                                        }
548                                        imcb_buddy_msg(ic, e->text,
549                                                       "<< \002BitlBee\002 - Received offline message. BitlBee can't show these. >>", 0,
550                                                       msgtime);
551                                }
552
553                                g_free(xml);
554                                xt_free_node(md);
555                        } else {
556                                debug("Can't handle %s packet from notification server", ct);
557                        }
558
559                        g_free(ct);
560                }
561        } else if (strcmp(cmd[0], "UBX") == 0) {
562                struct xt_node *ubx, *psm;
563                char *psm_text = NULL;
564
565                ubx = xt_from_string(msg, msglen);
566                if (ubx && strcmp(ubx->name, "Data") == 0 &&
567                    (psm = xt_find_node(ubx->children, "PSM"))) {
568                        psm_text = psm->text;
569                }
570
571                imcb_buddy_status_msg(ic, msn_normalize_handle(cmd[1]), psm_text);
572                xt_free_node(ubx);
573        } else if (strcmp(cmd[0], "ADL") == 0) {
574                struct xt_node *adl, *d, *c;
575
576                if (!(adl = xt_from_string(msg, msglen))) {
577                        return 1;
578                }
579
580                for (d = adl->children; d; d = d->next) {
581                        char *dn;
582                        if (strcmp(d->name, "d") != 0 ||
583                            (dn = xt_find_attr(d, "n")) == NULL) {
584                                continue;
585                        }
586                        for (c = d->children; c; c = c->next) {
587                                bee_user_t *bu;
588                                struct msn_buddy_data *bd;
589                                char *cn, *handle, *f, *l;
590                                int flags;
591
592                                if (strcmp(c->name, "c") != 0 ||
593                                    (l = xt_find_attr(c, "l")) == NULL ||
594                                    (cn = xt_find_attr(c, "n")) == NULL) {
595                                        continue;
596                                }
597
598                                /* FIXME: Use "t" here, guess I should just add it
599                                   as a prefix like elsewhere in the protocol. */
600                                handle = g_strdup_printf("%s@%s", cn, dn);
601                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
602                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
603                                        g_free(handle);
604                                        continue;
605                                }
606                                g_free(handle);
607                                bd = bu->data;
608
609                                if ((f = xt_find_attr(c, "f"))) {
610                                        http_decode(f);
611                                        imcb_rename_buddy(ic, bu->handle, f);
612                                }
613
614                                flags = atoi(l) & 15;
615                                if (bd->flags != flags) {
616                                        bd->flags = flags;
617                                        msn_buddy_ask(bu);
618                                }
619                        }
620                }
621        } else if (strcmp(cmd[0], "SDG") == 0) {
622                char **parts = g_strsplit(msg, "\r\n\r\n", 4);
623                char *from = NULL;
624                char *mt = NULL;
625                char *who = NULL;
626                char *s = NULL;
627
628                if ((from = get_rfc822_header(parts[0], "From", 0)) &&
629                    (mt = get_rfc822_header(parts[2], "Message-Type", 0)) &&
630                    (s = strchr(from, ';'))) {
631
632                        who = g_strndup(from + 2, s - from - 2);
633
634                        if (strcmp(mt, "Control/Typing") == 0) {
635                                imcb_buddy_typing(ic, who, OPT_TYPING);
636                        } else if (strcmp(mt, "Text") == 0) {
637                                imcb_buddy_msg(ic, who, parts[3], 0, 0);
638                        }
639                }
640                g_free(from);
641                g_free(mt);
642                g_free(who);
643                return 1;
644        }
645
646        return 1;
647}
648
649void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
650{
651        struct msn_data *md;
652
653        /* Dead connection? */
654        if (g_slist_find(msn_connections, ic) == NULL) {
655                return;
656        }
657
658        md = ic->proto_data;
659
660        if (token) {
661                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
662        } else {
663                imcb_error(ic, "Error during Passport authentication: %s", error);
664                imc_logout(ic, TRUE);
665        }
666}
667
668void msn_auth_got_contact_list(struct im_connection *ic)
669{
670        /* Dead connection? */
671        if (g_slist_find(msn_connections, ic) == NULL) {
672                return;
673        }
674
675        msn_ns_send_adl_start(ic);
676        msn_ns_finish_login(ic);
677}
678
679static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
680{
681        struct xt_node *adl = data, *d, *c, *s;
682        struct bee_user *bu = value;
683        struct msn_buddy_data *bd = bu->data;
684        struct msn_data *md = bu->ic->proto_data;
685        char handle[strlen(bu->handle) + 1];
686        char *domain;
687        char l[4];
688
689        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
690                return FALSE;
691        }
692
693        strcpy(handle, bu->handle);
694        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
695                return FALSE;
696        }
697        *domain = '\0';
698        domain++;
699
700        if ((d = adl->children) == NULL ||
701            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
702                d = xt_new_node("d", NULL, NULL);
703                xt_add_attr(d, "n", domain);
704                xt_insert_child(adl, d);
705        }
706
707        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
708        c = xt_new_node("c", NULL, NULL);
709        xt_add_attr(c, "n", handle);
710        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
711        s = xt_new_node("s", NULL, NULL);
712        xt_add_attr(s, "n", "IM");
713        xt_add_attr(s, "l", l);
714        xt_insert_child(c, s);
715        xt_insert_child(d, c);
716
717        /* Do this in batches of 100. */
718        bd->flags |= MSN_BUDDY_ADL_SYNCED;
719        return (--md->adl_todo % 140) == 0;
720}
721
722static void msn_ns_send_adl(struct im_connection *ic)
723{
724        struct xt_node *adl;
725        struct msn_data *md = ic->proto_data;
726        char *adls;
727
728        adl = xt_new_node("ml", NULL, NULL);
729        xt_add_attr(adl, "l", "1");
730        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
731        if (adl->children == NULL) {
732                /* This tells the caller that we're done now. */
733                md->adl_todo = -1;
734                xt_free_node(adl);
735                return;
736        }
737
738        adls = xt_to_string(adl);
739        xt_free_node(adl);
740        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
741        g_free(adls);
742}
743
744static void msn_ns_send_adl_start(struct im_connection *ic)
745{
746        struct msn_data *md;
747        GSList *l;
748
749        /* Dead connection? */
750        if (g_slist_find(msn_connections, ic) == NULL) {
751                return;
752        }
753
754        md = ic->proto_data;
755        md->adl_todo = 0;
756        for (l = ic->bee->users; l; l = l->next) {
757                bee_user_t *bu = l->data;
758                struct msn_buddy_data *bd = bu->data;
759
760                if (bu->ic != ic || (bd->flags & 7) == 0) {
761                        continue;
762                }
763
764                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
765                md->adl_todo++;
766        }
767
768        msn_ns_send_adl(ic);
769}
770
771int msn_ns_finish_login(struct im_connection *ic)
772{
773        struct msn_data *md = ic->proto_data;
774
775        if (ic->flags & OPT_LOGGED_IN) {
776                return 1;
777        }
778
779        if (md->adl_todo < 0) {
780                md->flags |= MSN_DONE_ADL;
781        }
782
783        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
784                if (md->flags & MSN_EMAIL_UNVERIFIED) {
785                        imcb_connected(ic);
786                } else {
787                        return msn_ns_set_display_name(ic, set_getstr(&ic->acc->set, "display_name"));
788                }
789        }
790
791        return 1;
792}
793
794// TODO: typing notifications, nudges lol, etc
795int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
796{
797        struct msn_data *md = ic->proto_data;
798        int retval = 0;
799        char *buf;
800
801        if (strncmp(text, "\r\r\r", 3) == 0) {
802                /* Err. Shouldn't happen but I guess it can. Don't send others
803                   any of the "SHAKE THAT THING" messages. :-D */
804                return 1;
805        }
806
807        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text);
808        retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
809        g_free(buf);
810        return retval;
811}
812
813void msn_ns_oim_send_queue(struct im_connection *ic, GSList **msgq)
814{
815        GSList *l;
816
817        for (l = *msgq; l; l = l->next) {
818                struct msn_message *m = l->data;
819                bee_user_t *bu = bee_user_by_handle(ic->bee, ic, m->who);
820
821                if (bu) {
822                        if (!msn_ns_sendmessage(ic, bu, m->text)) {
823                                return;
824                        }
825                }
826        }
827
828        while (*msgq != NULL) {
829                struct msn_message *m = (*msgq)->data;
830
831                *msgq = g_slist_remove(*msgq, m);
832                g_free(m->who);
833                g_free(m->text);
834                g_free(m);
835        }
836}
Note: See TracBrowser for help on using the repository browser.