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
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[f9258ae]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[b7d3cc34]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;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#include <ctype.h>
[f9258ae]27#include <sys/utsname.h>
[b7d3cc34]28#include "nogaim.h"
29#include "msn.h"
30#include "md5.h"
[f9258ae]31#include "sha1.h"
[7db65b7]32#include "soap.h"
[ca7de3a]33#include "xmltree.h"
[b7d3cc34]34
[5ebff60]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);
[b7d3cc34]37
[5ebff60]38static void msn_ns_send_adl_start(struct im_connection *ic);
39static void msn_ns_send_adl(struct im_connection *ic);
[cd5fdcf]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);
[b7d3cc34]43
[5ebff60]44int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...)
[64768d4]45{
46        struct msn_data *md = ic->proto_data;
47        va_list params;
48        char *out;
49        size_t len;
50        int st;
[5ebff60]51
52        va_start(params, fmt);
53        out = g_strdup_vprintf(fmt, params);
54        va_end(params);
55
56        if (fd < 0) {
[11e42dc]57                fd = md->fd;
[5ebff60]58        }
59
60        if (getenv("BITLBEE_DEBUG")) {
[693aca0]61                fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, out);
[5ebff60]62        }
63
64        len = strlen(out);
[913a663]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
[5ebff60]73        g_free(out);
74        if (st != len) {
75                imcb_error(ic, "Short write() to main server");
76                imc_logout(ic, TRUE);
[64768d4]77                return 0;
78        }
[5ebff60]79
[64768d4]80        return 1;
81}
82
[11e42dc]83gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
[b7d3cc34]84{
[11e42dc]85        struct msn_data *handler = ic->proto_data;
86
[5ebff60]87        if (handler->fd >= 0) {
88                closesocket(handler->fd);
89        }
90
[913a663]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                }
[b7d3cc34]102        }
[5ebff60]103
[bae0617]104        return TRUE;
105}
106
[5ebff60]107static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
[bae0617]108{
[11e42dc]109        struct msn_data *md = data;
110        struct msn_data *handler = md;
111        struct im_connection *ic = md->ic;
[5ebff60]112
[913a663]113        if (source == -1 && !md->is_http) {
[5ebff60]114                imcb_error(ic, "Could not connect to server");
115                imc_logout(ic, TRUE);
[bae0617]116                return FALSE;
[b7d3cc34]117        }
[5ebff60]118
119        g_free(handler->rxq);
[bae0617]120        handler->rxlen = 0;
[5ebff60]121        handler->rxq = g_new0(char, 1);
122
123        if (md->uuid == NULL) {
[f9258ae]124                struct utsname name;
125                sha1_state_t sha[1];
[5ebff60]126
[f9258ae]127                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
[5ebff60]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));
[f9258ae]132                }
[5ebff60]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 */
[f9258ae]136        }
[5ebff60]137
138        if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
[913a663]139                if (!handler->is_http) {
140                        handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler);
141                }
[5ebff60]142                imcb_log(ic, "Connected to server, waiting for reply");
[b7d3cc34]143        }
[5ebff60]144
[ba9edaa]145        return FALSE;
[b7d3cc34]146}
147
[11e42dc]148void msn_ns_close(struct msn_data *handler)
[bae0617]149{
[913a663]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        }
[5ebff60]158        if (handler->fd >= 0) {
159                closesocket(handler->fd);
160                b_event_remove(handler->inpa);
[bae0617]161        }
[5ebff60]162
[bae0617]163        handler->fd = handler->inpa = -1;
[5ebff60]164        g_free(handler->rxq);
165        g_free(handler->cmd_text);
166
[bae0617]167        handler->rxlen = 0;
168        handler->rxq = NULL;
169        handler->cmd_text = NULL;
170}
171
[5ebff60]172static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]173{
[11e42dc]174        struct msn_data *handler = data;
175        struct im_connection *ic = handler->ic;
[913a663]176        char *bytes;
[a4be2f6]177        int st;
[5ebff60]178
[913a663]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
[a4be2f6]186        if (st <= 0) {
[5ebff60]187                imcb_error(ic, "Error while reading from server");
188                imc_logout(ic, TRUE);
[ba9edaa]189                return FALSE;
[5ebff60]190        }
[a4be2f6]191
192        msn_queue_feed(handler, bytes, st);
193
[913a663]194        g_free(bytes);
195
[a4be2f6]196        /* Ignore ret == 0, it's already disconnected then. */
197        msn_handler(handler);
198
199        return TRUE;
200       
[b7d3cc34]201}
202
[11e42dc]203int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts)
[b7d3cc34]204{
[11e42dc]205        struct im_connection *ic = handler->ic;
206        struct msn_data *md = handler;
[5ebff60]207
208        if (num_parts == 0) {
[b7d3cc34]209                /* Hrrm... Empty command...? Ignore? */
[5ebff60]210                return(1);
[b7d3cc34]211        }
[5ebff60]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);
[b7d3cc34]218                }
[5ebff60]219
[254a4da]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",
[5ebff60]221                                    ++md->trId, ic->acc->user));
222        } else if (strcmp(cmd[0], "CVR") == 0) {
[b7d3cc34]223                /* We don't give a damn about the information we just received */
[5ebff60]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) {
[b7d3cc34]226                char *server;
227                int port;
[5ebff60]228
229                if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) {
230                        b_event_remove(handler->inpa);
[bae0617]231                        handler->inpa = -1;
[5ebff60]232
233                        server = strchr(cmd[3], ':');
234                        if (!server) {
235                                imcb_error(ic, "Syntax error");
236                                imc_logout(ic, TRUE);
237                                return(0);
[b7d3cc34]238                        }
239                        *server = 0;
[5ebff60]240                        port = atoi(server + 1);
[b7d3cc34]241                        server = cmd[3];
[5ebff60]242
243                        imcb_log(ic, "Transferring to other server");
[11e42dc]244                        return msn_ns_connect(ic, server, port);
[5ebff60]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) {
[ed0589c]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. */
[5ebff60]260                        if (cmd[4][0] == '0') {
[ed0589c]261                                md->flags |= MSN_EMAIL_UNVERIFIED;
[5ebff60]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);
[e5854a8]276                }
[5ebff60]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) {
[be7a180]293                char *resp;
[64768d4]294                int st;
[5ebff60]295
296                if (num_parts < 3) {
297                        imcb_error(ic, "Syntax error");
298                        imc_logout(ic, TRUE);
299                        return(0);
[b7d3cc34]300                }
[5ebff60]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);
[64768d4]308                return st;
[73b1a8e]309        } else if (strcmp(cmd[0], "QRY") == 0) {
310                /* CONGRATULATIONS */
[5ebff60]311        } else if (strcmp(cmd[0], "OUT") == 0) {
[c2fb3809]312                int allow_reconnect = TRUE;
[5ebff60]313
314                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
315                        imcb_error(ic, "Someone else logged in with your account");
[c2fb3809]316                        allow_reconnect = FALSE;
[5ebff60]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)");
[b7d3cc34]322                }
[5ebff60]323
324                imc_logout(ic, allow_reconnect);
325                return(0);
[d550358]326        } else if (strcmp(cmd[0], "GCF") == 0) {
[5fecede]327                /* Coming up is cmd[2] bytes of stuff we're supposed to
328                   censore. Meh. */
[5ebff60]329                handler->msglen = atoi(cmd[2]);
[11e42dc]330        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
[254a4da]331                if (num_parts >= 3) {
332                        handler->msglen = atoi(cmd[2]);
333                }
[73b1a8e]334        } else if (strcmp(cmd[0], "PUT") == 0) {
335                if (num_parts >= 4) {
336                        handler->msglen = atoi(cmd[3]);
337                }
[002fede]338        } else if (strcmp(cmd[0], "NOT") == 0) {
339                if (num_parts >= 2) {
340                        handler->msglen = atoi(cmd[1]);
341                }
[5ebff60]342        } else if (strcmp(cmd[0], "QNG") == 0) {
[e132b60]343                ic->flags |= OPT_PONGED;
[5ebff60]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);
[b7d3cc34]353                }
[5ebff60]354
[02bb9db]355                /* Oh yes, errors can have payloads too now. Discard them for now. */
[5ebff60]356                if (num_parts >= 3) {
357                        handler->msglen = atoi(cmd[2]);
358                }
359        } else {
[d550358]360                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
[b7d3cc34]361        }
[5ebff60]362
363        return(1);
[b7d3cc34]364}
365
[11e42dc]366int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
[b7d3cc34]367{
[11e42dc]368        struct im_connection *ic = handler->ic;
[b7d3cc34]369        char *body;
370        int blen = 0;
[5ebff60]371
372        if (!num_parts) {
373                return(1);
374        }
375
376        if ((body = strstr(msg, "\r\n\r\n"))) {
[b7d3cc34]377                body += 4;
[5ebff60]378                blen = msglen - (body - msg);
[b7d3cc34]379        }
[5ebff60]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) {
[b7d3cc34]390                                char *mtype;
391                                char *arg1;
[5ebff60]392
393                                if (!body) {
394                                        return(1);
[b7d3cc34]395                                }
[5ebff60]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) {
[b7d3cc34]410                                /* We don't care about this profile for now... */
[5ebff60]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);
[b7d3cc34]424                                }
[5ebff60]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);
[b7d3cc34]437                                }
[5ebff60]438                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
[d550358]439                                /* Notification that a message has been read... Ignore it */
[5ebff60]440                        } else {
441                                debug("Can't handle %s packet from notification server", ct);
[b7d3cc34]442                        }
[5ebff60]443
444                        g_free(ct);
[b7d3cc34]445                }
[5ebff60]446        } else if (strcmp(cmd[0], "ADL") == 0) {
[e5854a8]447                struct xt_node *adl, *d, *c;
[5ebff60]448
449                if (!(adl = xt_from_string(msg, msglen))) {
[e5854a8]450                        return 1;
[5ebff60]451                }
452
453                for (d = adl->children; d; d = d->next) {
[e5854a8]454                        char *dn;
[5ebff60]455                        if (strcmp(d->name, "d") != 0 ||
456                            (dn = xt_find_attr(d, "n")) == NULL) {
[e5854a8]457                                continue;
[5ebff60]458                        }
459                        for (c = d->children; c; c = c->next) {
[e5854a8]460                                bee_user_t *bu;
461                                struct msn_buddy_data *bd;
462                                char *cn, *handle, *f, *l;
463                                int flags;
[5ebff60]464
465                                if (strcmp(c->name, "c") != 0 ||
466                                    (l = xt_find_attr(c, "l")) == NULL ||
467                                    (cn = xt_find_attr(c, "n")) == NULL) {
[e5854a8]468                                        continue;
[5ebff60]469                                }
470
[3901b5d]471                                /* FIXME: Use "t" here, guess I should just add it
472                                   as a prefix like elsewhere in the protocol. */
[5ebff60]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);
[e5854a8]477                                        continue;
478                                }
[5ebff60]479                                g_free(handle);
[e5854a8]480                                bd = bu->data;
[5ebff60]481
482                                if ((f = xt_find_attr(c, "f"))) {
483                                        http_decode(f);
484                                        imcb_rename_buddy(ic, bu->handle, f);
[e5854a8]485                                }
[5ebff60]486
487                                flags = atoi(l) & 15;
488                                if (bd->flags != flags) {
[e5854a8]489                                        bd->flags = flags;
[5ebff60]490                                        msn_buddy_ask(bu);
[e5854a8]491                                }
492                        }
493                }
[cd5fdcf]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;
[11e42dc]591                        }
592                }
[cd5fdcf]593                s = s->next;
[3901b5d]594        }
[5ebff60]595
[cd5fdcf]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);
[b7d3cc34]604}
605
[5ebff60]606void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
[b7d3cc34]607{
[d84e2a9]608        struct msn_data *md;
[5ebff60]609
[d84e2a9]610        /* Dead connection? */
[5ebff60]611        if (g_slist_find(msn_connections, ic) == NULL) {
[d84e2a9]612                return;
[b7d3cc34]613        }
[5ebff60]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);
[660cb00]622        }
[b7d3cc34]623}
[e3413cc]624
[5ebff60]625void msn_auth_got_contact_list(struct im_connection *ic)
[ca7de3a]626{
627        /* Dead connection? */
[5ebff60]628        if (g_slist_find(msn_connections, ic) == NULL) {
[ca7de3a]629                return;
[5ebff60]630        }
631
[254a4da]632        msn_ns_send_adl_start(ic);
633        msn_ns_finish_login(ic);
[ca7de3a]634}
635
[5ebff60]636static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
[ca7de3a]637{
[254a4da]638        struct xt_node *adl = data, *d, *c, *s;
[ca7de3a]639        struct bee_user *bu = value;
640        struct msn_buddy_data *bd = bu->data;
[5a7af1b]641        struct msn_data *md = bu->ic->proto_data;
[4f161e3]642        char handle[strlen(bu->handle) + 1];
[ca7de3a]643        char *domain;
644        char l[4];
[5ebff60]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 */
[e5854a8]652                return FALSE;
[5ebff60]653        }
[ca7de3a]654        *domain = '\0';
[5ebff60]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);
[ca7de3a]662        }
[5ebff60]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 */
[254a4da]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);
[5ebff60]672        xt_insert_child(d, c);
673
[5a7af1b]674        /* Do this in batches of 100. */
675        bd->flags |= MSN_BUDDY_ADL_SYNCED;
676        return (--md->adl_todo % 140) == 0;
[ca7de3a]677}
678
[5ebff60]679static void msn_ns_send_adl(struct im_connection *ic)
[ca7de3a]680{
681        struct xt_node *adl;
[5a7af1b]682        struct msn_data *md = ic->proto_data;
[64768d4]683        char *adls;
[5ebff60]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) {
[5a7af1b]689                /* This tells the caller that we're done now. */
690                md->adl_todo = -1;
[5ebff60]691                xt_free_node(adl);
[5a7af1b]692                return;
693        }
[5ebff60]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);
[5a7af1b]699}
700
[5ebff60]701static void msn_ns_send_adl_start(struct im_connection *ic)
[5a7af1b]702{
703        struct msn_data *md;
704        GSList *l;
[5ebff60]705
[5a7af1b]706        /* Dead connection? */
[5ebff60]707        if (g_slist_find(msn_connections, ic) == NULL) {
[5a7af1b]708                return;
[5ebff60]709        }
710
[5a7af1b]711        md = ic->proto_data;
712        md->adl_todo = 0;
[5ebff60]713        for (l = ic->bee->users; l; l = l->next) {
[5a7af1b]714                bee_user_t *bu = l->data;
715                struct msn_buddy_data *bd = bu->data;
[5ebff60]716
717                if (bu->ic != ic || (bd->flags & 7) == 0) {
[5a7af1b]718                        continue;
[5ebff60]719                }
720
[5a7af1b]721                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
722                md->adl_todo++;
723        }
[5ebff60]724
725        msn_ns_send_adl(ic);
[ca7de3a]726}
[80175a1]727
[5ebff60]728int msn_ns_finish_login(struct im_connection *ic)
[80175a1]729{
730        struct msn_data *md = ic->proto_data;
[5ebff60]731
732        if (ic->flags & OPT_LOGGED_IN) {
[80175a1]733                return 1;
[5ebff60]734        }
735
736        if (md->adl_todo < 0) {
[80175a1]737                md->flags |= MSN_DONE_ADL;
[ed0589c]738        }
[5ebff60]739
740        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
[d550358]741                imcb_connected(ic);
[5ebff60]742        }
743
[ed0589c]744        return 1;
[80175a1]745}
[bc676ac]746
[11e42dc]747// TODO: typing notifications, nudges lol, etc
[5ebff60]748int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
[bc676ac]749{
750        struct msn_data *md = ic->proto_data;
[11e42dc]751        int retval = 0;
752        char *buf;
[5ebff60]753
754        if (strncmp(text, "\r\r\r", 3) == 0) {
[bc676ac]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;
[5ebff60]758        }
759
[11e42dc]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;
[bc676ac]764}
Note: See TracBrowser for help on using the repository browser.