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