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