source: protocols/msn/ns.c @ 002fede

Last change on this file since 002fede was 002fede, checked in by dequis <dx@…>, at 2015-04-11T18:20:31Z

msn: handle NOT command payload length

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