source: protocols/msn/ns.c @ cd5fdcf

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

msn: handle NFY PUT (presence notifications), refactor a bit

  • Property mode set to 100644
File size: 19.4 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                }
[5ebff60]338        } else if (strcmp(cmd[0], "QNG") == 0) {
[e132b60]339                ic->flags |= OPT_PONGED;
[5ebff60]340        } else if (g_ascii_isdigit(cmd[0][0])) {
341                int num = atoi(cmd[0]);
342                const struct msn_status_code *err = msn_status_by_number(num);
343
344                imcb_error(ic, "Error reported by MSN server: %s", err->text);
345
346                if (err->flags & STATUS_FATAL) {
347                        imc_logout(ic, TRUE);
348                        return(0);
[b7d3cc34]349                }
[5ebff60]350
[02bb9db]351                /* Oh yes, errors can have payloads too now. Discard them for now. */
[5ebff60]352                if (num_parts >= 3) {
353                        handler->msglen = atoi(cmd[2]);
354                }
355        } else {
[d550358]356                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
[b7d3cc34]357        }
[5ebff60]358
359        return(1);
[b7d3cc34]360}
361
[11e42dc]362int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
[b7d3cc34]363{
[11e42dc]364        struct im_connection *ic = handler->ic;
[b7d3cc34]365        char *body;
366        int blen = 0;
[5ebff60]367
368        if (!num_parts) {
369                return(1);
370        }
371
372        if ((body = strstr(msg, "\r\n\r\n"))) {
[b7d3cc34]373                body += 4;
[5ebff60]374                blen = msglen - (body - msg);
[b7d3cc34]375        }
[5ebff60]376
377        if (strcmp(cmd[0], "MSG") == 0) {
378                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
379                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
380
381                        if (!ct) {
382                                return(1);
383                        }
384
385                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
[b7d3cc34]386                                char *mtype;
387                                char *arg1;
[5ebff60]388
389                                if (!body) {
390                                        return(1);
[b7d3cc34]391                                }
[5ebff60]392
393                                mtype = get_rfc822_header(body, "Type:", blen);
394                                arg1 = get_rfc822_header(body, "Arg1:", blen);
395
396                                if (mtype && strcmp(mtype, "1") == 0) {
397                                        if (arg1) {
398                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
399                                                         arg1);
400                                        }
401                                }
402
403                                g_free(arg1);
404                                g_free(mtype);
405                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
[b7d3cc34]406                                /* We don't care about this profile for now... */
[5ebff60]407                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
408                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
409                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
410                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
411
412                                        if (inbox && folders) {
413                                                imcb_log(ic,
414                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
415                                                         folders);
416                                        }
417
418                                        g_free(inbox);
419                                        g_free(folders);
[b7d3cc34]420                                }
[5ebff60]421                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
422                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
423                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
424                                        char *fromname = get_rfc822_header(body, "From:", blen);
425
426                                        if (from && fromname) {
427                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
428                                                         from);
429                                        }
430
431                                        g_free(from);
432                                        g_free(fromname);
[b7d3cc34]433                                }
[5ebff60]434                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
[d550358]435                                /* Notification that a message has been read... Ignore it */
[5ebff60]436                        } else {
437                                debug("Can't handle %s packet from notification server", ct);
[b7d3cc34]438                        }
[5ebff60]439
440                        g_free(ct);
[b7d3cc34]441                }
[5ebff60]442        } else if (strcmp(cmd[0], "ADL") == 0) {
[e5854a8]443                struct xt_node *adl, *d, *c;
[5ebff60]444
445                if (!(adl = xt_from_string(msg, msglen))) {
[e5854a8]446                        return 1;
[5ebff60]447                }
448
449                for (d = adl->children; d; d = d->next) {
[e5854a8]450                        char *dn;
[5ebff60]451                        if (strcmp(d->name, "d") != 0 ||
452                            (dn = xt_find_attr(d, "n")) == NULL) {
[e5854a8]453                                continue;
[5ebff60]454                        }
455                        for (c = d->children; c; c = c->next) {
[e5854a8]456                                bee_user_t *bu;
457                                struct msn_buddy_data *bd;
458                                char *cn, *handle, *f, *l;
459                                int flags;
[5ebff60]460
461                                if (strcmp(c->name, "c") != 0 ||
462                                    (l = xt_find_attr(c, "l")) == NULL ||
463                                    (cn = xt_find_attr(c, "n")) == NULL) {
[e5854a8]464                                        continue;
[5ebff60]465                                }
466
[3901b5d]467                                /* FIXME: Use "t" here, guess I should just add it
468                                   as a prefix like elsewhere in the protocol. */
[5ebff60]469                                handle = g_strdup_printf("%s@%s", cn, dn);
470                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
471                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
472                                        g_free(handle);
[e5854a8]473                                        continue;
474                                }
[5ebff60]475                                g_free(handle);
[e5854a8]476                                bd = bu->data;
[5ebff60]477
478                                if ((f = xt_find_attr(c, "f"))) {
479                                        http_decode(f);
480                                        imcb_rename_buddy(ic, bu->handle, f);
[e5854a8]481                                }
[5ebff60]482
483                                flags = atoi(l) & 15;
484                                if (bd->flags != flags) {
[e5854a8]485                                        bd->flags = flags;
[5ebff60]486                                        msn_buddy_ask(bu);
[e5854a8]487                                }
488                        }
489                }
[cd5fdcf]490        } else if ((strcmp(cmd[0], "SDG") == 0) || (strcmp(cmd[0], "NFY") == 0)) {
491                msn_ns_structured_message(handler, msg, msglen, cmd);
492        }
493
494        return 1;
495}
496
497static void msn_ns_structured_message(struct msn_data *handler, char *msg, int msglen, char **cmd)
498{
499        char **parts = NULL;
500        char *semicolon = NULL;
501        char *action = NULL;
502        char *from = NULL;
503        char *who = NULL;
504
505        parts = g_strsplit(msg, "\r\n\r\n", 4);
506
507        if (!(from = get_rfc822_header(parts[0], "From", 0))) {
508                goto cleanup;
509        }
510
511        /* either the semicolon or the end of the string */
512        semicolon = strchr(from, ';') ? : (from + strlen(from));
513
514        who = g_strndup(from + 2, semicolon - from - 2);
515
516        if ((strcmp(cmd[0], "SDG") == 0) && (action = get_rfc822_header(parts[2], "Message-Type", 0))) {
517                msn_ns_sdg(handler, who, parts, action);
518
519        } else if ((strcmp(cmd[0], "NFY") == 0) && (action = get_rfc822_header(parts[2], "Uri", 0))) {
520                gboolean is_put = (strcmp(cmd[1], "PUT") == 0);
521                msn_ns_nfy(handler, who, parts, action, is_put);
522        }
523
524cleanup:
525        g_strfreev(parts);
526        g_free(action);
527        g_free(from);
528        g_free(who);
529}
530
531static void msn_ns_sdg(struct msn_data *handler, char *who, char **parts, char *action)
532{
533        struct im_connection *ic = handler->ic;
534
535        if (strcmp(action, "Control/Typing") == 0) {
536                imcb_buddy_typing(ic, who, OPT_TYPING);
537        } else if (strcmp(action, "Text") == 0) {
538                imcb_buddy_msg(ic, who, parts[3], 0, 0);
539        }
540}
541
542static void msn_ns_nfy(struct msn_data *handler, char *who, char **parts, char *action, gboolean is_put)
543{
544        struct im_connection *ic = handler->ic;
545        struct xt_node *body = NULL;
546        struct xt_node *s = NULL;
547        const char *state = NULL;
548        char *nick = NULL;
549        char *psm = NULL;
550        int flags = OPT_LOGGED_IN;
551
552        if (strcmp(action, "/user") != 0) {
553                return;
554        }
555
556        if (!(body = xt_from_string(parts[3], 0))) {
557                goto cleanup;
558        }
559
560        s = body->children;
561        while ((s = xt_find_node(s, "s"))) {
562                struct xt_node *s2;
563                char *n = xt_find_attr(s, "n");  /* service name: IM, PE, etc */
564
565                if (strcmp(n, "IM") == 0) {
566                        /* IM has basic presence information */
567                        if (!is_put) {
568                                /* NFY DEL with a <s> usually means log out from the last endpoint */
569                                flags &= ~OPT_LOGGED_IN;
570                                break;
571                        }
572
573                        s2 = xt_find_node(s->children, "Status");
574                        if (s2 && s2->text_len) {
575                                const struct msn_away_state *msn_state = msn_away_state_by_code(s2->text);
576                                state = msn_state->name;
577                                if (msn_state != msn_away_state_list) {
578                                        flags |= OPT_AWAY;
579                                }
580                        }
581                } else if (strcmp(n, "PE") == 0) {
582                        if ((s2 = xt_find_node(s->children, "PSM")) && s2->text_len) {
583                                psm = s2->text;
584                        }
585                        if ((s2 = xt_find_node(s->children, "FriendlyName")) && s2->text_len) {
586                                nick = s2->text;
[11e42dc]587                        }
588                }
[cd5fdcf]589                s = s->next;
[3901b5d]590        }
[5ebff60]591
[cd5fdcf]592        imcb_buddy_status(ic, who, flags, state, psm);
593
594        if (nick) {
595                imcb_rename_buddy(ic, who, nick);
596        }
597
598cleanup:
599        xt_free_node(body);
[b7d3cc34]600}
601
[5ebff60]602void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
[b7d3cc34]603{
[d84e2a9]604        struct msn_data *md;
[5ebff60]605
[d84e2a9]606        /* Dead connection? */
[5ebff60]607        if (g_slist_find(msn_connections, ic) == NULL) {
[d84e2a9]608                return;
[b7d3cc34]609        }
[5ebff60]610
611        md = ic->proto_data;
612
613        if (token) {
614                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
615        } else {
616                imcb_error(ic, "Error during Passport authentication: %s", error);
617                imc_logout(ic, TRUE);
[660cb00]618        }
[b7d3cc34]619}
[e3413cc]620
[5ebff60]621void msn_auth_got_contact_list(struct im_connection *ic)
[ca7de3a]622{
623        /* Dead connection? */
[5ebff60]624        if (g_slist_find(msn_connections, ic) == NULL) {
[ca7de3a]625                return;
[5ebff60]626        }
627
[254a4da]628        msn_ns_send_adl_start(ic);
629        msn_ns_finish_login(ic);
[ca7de3a]630}
631
[5ebff60]632static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
[ca7de3a]633{
[254a4da]634        struct xt_node *adl = data, *d, *c, *s;
[ca7de3a]635        struct bee_user *bu = value;
636        struct msn_buddy_data *bd = bu->data;
[5a7af1b]637        struct msn_data *md = bu->ic->proto_data;
[4f161e3]638        char handle[strlen(bu->handle) + 1];
[ca7de3a]639        char *domain;
640        char l[4];
[5ebff60]641
642        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
643                return FALSE;
644        }
645
646        strcpy(handle, bu->handle);
647        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
[e5854a8]648                return FALSE;
[5ebff60]649        }
[ca7de3a]650        *domain = '\0';
[5ebff60]651        domain++;
652
653        if ((d = adl->children) == NULL ||
654            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
655                d = xt_new_node("d", NULL, NULL);
656                xt_add_attr(d, "n", domain);
657                xt_insert_child(adl, d);
[ca7de3a]658        }
[5ebff60]659
660        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
661        c = xt_new_node("c", NULL, NULL);
662        xt_add_attr(c, "n", handle);
663        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
[254a4da]664        s = xt_new_node("s", NULL, NULL);
665        xt_add_attr(s, "n", "IM");
666        xt_add_attr(s, "l", l);
667        xt_insert_child(c, s);
[5ebff60]668        xt_insert_child(d, c);
669
[5a7af1b]670        /* Do this in batches of 100. */
671        bd->flags |= MSN_BUDDY_ADL_SYNCED;
672        return (--md->adl_todo % 140) == 0;
[ca7de3a]673}
674
[5ebff60]675static void msn_ns_send_adl(struct im_connection *ic)
[ca7de3a]676{
677        struct xt_node *adl;
[5a7af1b]678        struct msn_data *md = ic->proto_data;
[64768d4]679        char *adls;
[5ebff60]680
681        adl = xt_new_node("ml", NULL, NULL);
682        xt_add_attr(adl, "l", "1");
683        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
684        if (adl->children == NULL) {
[5a7af1b]685                /* This tells the caller that we're done now. */
686                md->adl_todo = -1;
[5ebff60]687                xt_free_node(adl);
[5a7af1b]688                return;
689        }
[5ebff60]690
691        adls = xt_to_string(adl);
692        xt_free_node(adl);
693        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
694        g_free(adls);
[5a7af1b]695}
696
[5ebff60]697static void msn_ns_send_adl_start(struct im_connection *ic)
[5a7af1b]698{
699        struct msn_data *md;
700        GSList *l;
[5ebff60]701
[5a7af1b]702        /* Dead connection? */
[5ebff60]703        if (g_slist_find(msn_connections, ic) == NULL) {
[5a7af1b]704                return;
[5ebff60]705        }
706
[5a7af1b]707        md = ic->proto_data;
708        md->adl_todo = 0;
[5ebff60]709        for (l = ic->bee->users; l; l = l->next) {
[5a7af1b]710                bee_user_t *bu = l->data;
711                struct msn_buddy_data *bd = bu->data;
[5ebff60]712
713                if (bu->ic != ic || (bd->flags & 7) == 0) {
[5a7af1b]714                        continue;
[5ebff60]715                }
716
[5a7af1b]717                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
718                md->adl_todo++;
719        }
[5ebff60]720
721        msn_ns_send_adl(ic);
[ca7de3a]722}
[80175a1]723
[5ebff60]724int msn_ns_finish_login(struct im_connection *ic)
[80175a1]725{
726        struct msn_data *md = ic->proto_data;
[5ebff60]727
728        if (ic->flags & OPT_LOGGED_IN) {
[80175a1]729                return 1;
[5ebff60]730        }
731
732        if (md->adl_todo < 0) {
[80175a1]733                md->flags |= MSN_DONE_ADL;
[ed0589c]734        }
[5ebff60]735
736        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
[d550358]737                imcb_connected(ic);
[5ebff60]738        }
739
[ed0589c]740        return 1;
[80175a1]741}
[bc676ac]742
[11e42dc]743// TODO: typing notifications, nudges lol, etc
[5ebff60]744int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
[bc676ac]745{
746        struct msn_data *md = ic->proto_data;
[11e42dc]747        int retval = 0;
748        char *buf;
[5ebff60]749
750        if (strncmp(text, "\r\r\r", 3) == 0) {
[bc676ac]751                /* Err. Shouldn't happen but I guess it can. Don't send others
752                   any of the "SHAKE THAT THING" messages. :-D */
753                return 1;
[5ebff60]754        }
755
[11e42dc]756        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text);
757        retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
758        g_free(buf);
759        return retval;
[bc676ac]760}
Note: See TracBrowser for help on using the repository browser.