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