1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 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., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include <ctype.h> |
---|
27 | #include "nogaim.h" |
---|
28 | #include "msn.h" |
---|
29 | #include "md5.h" |
---|
30 | #include "soap.h" |
---|
31 | #include "xmltree.h" |
---|
32 | |
---|
33 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ); |
---|
34 | static int msn_ns_command( gpointer data, char **cmd, int num_parts ); |
---|
35 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ); |
---|
36 | |
---|
37 | static void msn_ns_send_adl_start( struct im_connection *ic ); |
---|
38 | static void msn_ns_send_adl( struct im_connection *ic ); |
---|
39 | |
---|
40 | int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... ) |
---|
41 | { |
---|
42 | struct msn_data *md = ic->proto_data; |
---|
43 | va_list params; |
---|
44 | char *out; |
---|
45 | size_t len; |
---|
46 | int st; |
---|
47 | |
---|
48 | va_start( params, fmt ); |
---|
49 | out = g_strdup_vprintf( fmt, params ); |
---|
50 | va_end( params ); |
---|
51 | |
---|
52 | if( fd < 0 ) |
---|
53 | fd = md->fd; |
---|
54 | |
---|
55 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
56 | fprintf( stderr, "->NS%d:%s", fd, out ); |
---|
57 | |
---|
58 | len = strlen( out ); |
---|
59 | st = write( fd, out, len ); |
---|
60 | g_free( out ); |
---|
61 | if( st != len ) |
---|
62 | { |
---|
63 | imcb_error( ic, "Short write() to main server" ); |
---|
64 | imc_logout( ic, TRUE ); |
---|
65 | return 0; |
---|
66 | } |
---|
67 | |
---|
68 | return 1; |
---|
69 | } |
---|
70 | |
---|
71 | gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) |
---|
72 | { |
---|
73 | struct im_connection *ic = data; |
---|
74 | struct msn_data *md; |
---|
75 | |
---|
76 | if( !g_slist_find( msn_connections, ic ) ) |
---|
77 | return FALSE; |
---|
78 | |
---|
79 | if( source == -1 ) |
---|
80 | { |
---|
81 | imcb_error( ic, "Could not connect to server" ); |
---|
82 | imc_logout( ic, TRUE ); |
---|
83 | return FALSE; |
---|
84 | } |
---|
85 | |
---|
86 | md = ic->proto_data; |
---|
87 | |
---|
88 | if( !md->handler ) |
---|
89 | { |
---|
90 | md->handler = g_new0( struct msn_handler_data, 1 ); |
---|
91 | md->handler->data = ic; |
---|
92 | md->handler->exec_command = msn_ns_command; |
---|
93 | md->handler->exec_message = msn_ns_message; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | if( md->handler->rxq ) |
---|
98 | g_free( md->handler->rxq ); |
---|
99 | |
---|
100 | md->handler->rxlen = 0; |
---|
101 | } |
---|
102 | |
---|
103 | md->handler->fd = md->fd; |
---|
104 | md->handler->rxq = g_new0( char, 1 ); |
---|
105 | |
---|
106 | if( msn_ns_write( ic, -1, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER ) ) |
---|
107 | { |
---|
108 | ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic ); |
---|
109 | imcb_log( ic, "Connected to server, waiting for reply" ); |
---|
110 | } |
---|
111 | |
---|
112 | return FALSE; |
---|
113 | } |
---|
114 | |
---|
115 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ) |
---|
116 | { |
---|
117 | struct im_connection *ic = data; |
---|
118 | struct msn_data *md = ic->proto_data; |
---|
119 | |
---|
120 | if( msn_handler( md->handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */ |
---|
121 | { |
---|
122 | imcb_error( ic, "Error while reading from server" ); |
---|
123 | imc_logout( ic, TRUE ); |
---|
124 | |
---|
125 | return FALSE; |
---|
126 | } |
---|
127 | else |
---|
128 | return TRUE; |
---|
129 | } |
---|
130 | |
---|
131 | static int msn_ns_command( gpointer data, char **cmd, int num_parts ) |
---|
132 | { |
---|
133 | struct im_connection *ic = data; |
---|
134 | struct msn_data *md = ic->proto_data; |
---|
135 | |
---|
136 | if( num_parts == 0 ) |
---|
137 | { |
---|
138 | /* Hrrm... Empty command...? Ignore? */ |
---|
139 | return( 1 ); |
---|
140 | } |
---|
141 | |
---|
142 | if( strcmp( cmd[0], "VER" ) == 0 ) |
---|
143 | { |
---|
144 | if( cmd[2] && strncmp( cmd[2], MSNP_VER, 5 ) != 0 ) |
---|
145 | { |
---|
146 | imcb_error( ic, "Unsupported protocol" ); |
---|
147 | imc_logout( ic, FALSE ); |
---|
148 | return( 0 ); |
---|
149 | } |
---|
150 | |
---|
151 | return( msn_ns_write( ic, -1, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n", |
---|
152 | ++md->trId, ic->acc->user ) ); |
---|
153 | } |
---|
154 | else if( strcmp( cmd[0], "CVR" ) == 0 ) |
---|
155 | { |
---|
156 | /* We don't give a damn about the information we just received */ |
---|
157 | return msn_ns_write( ic, -1, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user ); |
---|
158 | } |
---|
159 | else if( strcmp( cmd[0], "XFR" ) == 0 ) |
---|
160 | { |
---|
161 | char *server; |
---|
162 | int port; |
---|
163 | |
---|
164 | if( num_parts >= 6 && strcmp( cmd[2], "NS" ) == 0 ) |
---|
165 | { |
---|
166 | b_event_remove( ic->inpa ); |
---|
167 | ic->inpa = 0; |
---|
168 | closesocket( md->fd ); |
---|
169 | |
---|
170 | server = strchr( cmd[3], ':' ); |
---|
171 | if( !server ) |
---|
172 | { |
---|
173 | imcb_error( ic, "Syntax error" ); |
---|
174 | imc_logout( ic, TRUE ); |
---|
175 | return( 0 ); |
---|
176 | } |
---|
177 | *server = 0; |
---|
178 | port = atoi( server + 1 ); |
---|
179 | server = cmd[3]; |
---|
180 | |
---|
181 | imcb_log( ic, "Transferring to other server" ); |
---|
182 | |
---|
183 | md->fd = proxy_connect( server, port, msn_ns_connected, ic ); |
---|
184 | } |
---|
185 | else if( num_parts >= 6 && strcmp( cmd[2], "SB" ) == 0 ) |
---|
186 | { |
---|
187 | struct msn_switchboard *sb; |
---|
188 | |
---|
189 | server = strchr( cmd[3], ':' ); |
---|
190 | if( !server ) |
---|
191 | { |
---|
192 | imcb_error( ic, "Syntax error" ); |
---|
193 | imc_logout( ic, TRUE ); |
---|
194 | return( 0 ); |
---|
195 | } |
---|
196 | *server = 0; |
---|
197 | port = atoi( server + 1 ); |
---|
198 | server = cmd[3]; |
---|
199 | |
---|
200 | if( strcmp( cmd[4], "CKI" ) != 0 ) |
---|
201 | { |
---|
202 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
203 | imc_logout( ic, TRUE ); |
---|
204 | return( 0 ); |
---|
205 | } |
---|
206 | |
---|
207 | debug( "Connecting to a new switchboard with key %s", cmd[5] ); |
---|
208 | |
---|
209 | if( ( sb = msn_sb_create( ic, server, port, cmd[5], MSN_SB_NEW ) ) == NULL ) |
---|
210 | { |
---|
211 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
212 | definitely something serious (we ran out of file descriptors?). */ |
---|
213 | imcb_error( ic, "Could not create new switchboard" ); |
---|
214 | imc_logout( ic, TRUE ); |
---|
215 | return( 0 ); |
---|
216 | } |
---|
217 | |
---|
218 | if( md->msgq ) |
---|
219 | { |
---|
220 | struct msn_message *m = md->msgq->data; |
---|
221 | GSList *l; |
---|
222 | |
---|
223 | sb->who = g_strdup( m->who ); |
---|
224 | |
---|
225 | /* Move all the messages to the first user in the message |
---|
226 | queue to the switchboard message queue. */ |
---|
227 | l = md->msgq; |
---|
228 | while( l ) |
---|
229 | { |
---|
230 | m = l->data; |
---|
231 | l = l->next; |
---|
232 | if( strcmp( m->who, sb->who ) == 0 ) |
---|
233 | { |
---|
234 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
235 | md->msgq = g_slist_remove( md->msgq, m ); |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | else |
---|
241 | { |
---|
242 | imcb_error( ic, "Syntax error" ); |
---|
243 | imc_logout( ic, TRUE ); |
---|
244 | return( 0 ); |
---|
245 | } |
---|
246 | } |
---|
247 | else if( strcmp( cmd[0], "USR" ) == 0 ) |
---|
248 | { |
---|
249 | if( num_parts >= 6 && strcmp( cmd[2], "SSO" ) == 0 && |
---|
250 | strcmp( cmd[3], "S" ) == 0 ) |
---|
251 | { |
---|
252 | msn_soap_passport_sso_request( ic, cmd[4], cmd[5] ); |
---|
253 | } |
---|
254 | else if( strcmp( cmd[2], "OK" ) == 0 ) |
---|
255 | { |
---|
256 | imcb_log( ic, "Authenticated, getting buddy list" ); |
---|
257 | msn_soap_memlist_request( ic ); |
---|
258 | } |
---|
259 | else |
---|
260 | { |
---|
261 | imcb_error( ic, "Unknown authentication type" ); |
---|
262 | imc_logout( ic, FALSE ); |
---|
263 | return( 0 ); |
---|
264 | } |
---|
265 | } |
---|
266 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
267 | { |
---|
268 | if( num_parts < 4 ) |
---|
269 | { |
---|
270 | imcb_error( ic, "Syntax error" ); |
---|
271 | imc_logout( ic, TRUE ); |
---|
272 | return( 0 ); |
---|
273 | } |
---|
274 | |
---|
275 | md->handler->msglen = atoi( cmd[3] ); |
---|
276 | |
---|
277 | if( md->handler->msglen <= 0 ) |
---|
278 | { |
---|
279 | imcb_error( ic, "Syntax error" ); |
---|
280 | imc_logout( ic, TRUE ); |
---|
281 | return( 0 ); |
---|
282 | } |
---|
283 | } |
---|
284 | else if( strcmp( cmd[0], "BLP" ) == 0 ) |
---|
285 | { |
---|
286 | msn_ns_send_adl_start( ic ); |
---|
287 | return msn_ns_finish_login( ic ); |
---|
288 | } |
---|
289 | else if( strcmp( cmd[0], "ADL" ) == 0 ) |
---|
290 | { |
---|
291 | if( num_parts >= 3 && strcmp( cmd[2], "OK" ) == 0 ) |
---|
292 | { |
---|
293 | msn_ns_send_adl( ic ); |
---|
294 | return msn_ns_finish_login( ic ); |
---|
295 | } |
---|
296 | else if( num_parts >= 3 ) |
---|
297 | { |
---|
298 | md->handler->msglen = atoi( cmd[2] ); |
---|
299 | } |
---|
300 | } |
---|
301 | else if( strcmp( cmd[0], "PRP" ) == 0 ) |
---|
302 | { |
---|
303 | imcb_connected( ic ); |
---|
304 | } |
---|
305 | else if( strcmp( cmd[0], "CHL" ) == 0 ) |
---|
306 | { |
---|
307 | char *resp; |
---|
308 | int st; |
---|
309 | |
---|
310 | if( num_parts < 3 ) |
---|
311 | { |
---|
312 | imcb_error( ic, "Syntax error" ); |
---|
313 | imc_logout( ic, TRUE ); |
---|
314 | return( 0 ); |
---|
315 | } |
---|
316 | |
---|
317 | resp = msn_p11_challenge( cmd[2] ); |
---|
318 | |
---|
319 | st = msn_ns_write( ic, -1, "QRY %d %s %zd\r\n%s", |
---|
320 | ++md->trId, MSNP11_PROD_ID, |
---|
321 | strlen( resp ), resp ); |
---|
322 | g_free( resp ); |
---|
323 | return st; |
---|
324 | } |
---|
325 | else if( strcmp( cmd[0], "ILN" ) == 0 ) |
---|
326 | { |
---|
327 | const struct msn_away_state *st; |
---|
328 | |
---|
329 | if( num_parts < 6 ) |
---|
330 | { |
---|
331 | imcb_error( ic, "Syntax error" ); |
---|
332 | imc_logout( ic, TRUE ); |
---|
333 | return( 0 ); |
---|
334 | } |
---|
335 | |
---|
336 | http_decode( cmd[5] ); |
---|
337 | imcb_rename_buddy( ic, cmd[3], cmd[5] ); |
---|
338 | |
---|
339 | st = msn_away_state_by_code( cmd[2] ); |
---|
340 | if( !st ) |
---|
341 | { |
---|
342 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
343 | st = msn_away_state_list + 1; |
---|
344 | } |
---|
345 | |
---|
346 | imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | |
---|
347 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
348 | st->name, NULL ); |
---|
349 | } |
---|
350 | else if( strcmp( cmd[0], "FLN" ) == 0 ) |
---|
351 | { |
---|
352 | if( cmd[1] == NULL ) |
---|
353 | return 1; |
---|
354 | |
---|
355 | imcb_buddy_status( ic, cmd[1], 0, NULL, NULL ); |
---|
356 | |
---|
357 | msn_sb_start_keepalives( msn_sb_by_handle( ic, cmd[1] ), TRUE ); |
---|
358 | } |
---|
359 | else if( strcmp( cmd[0], "NLN" ) == 0 ) |
---|
360 | { |
---|
361 | const struct msn_away_state *st; |
---|
362 | int cap; |
---|
363 | |
---|
364 | if( num_parts < 6 ) |
---|
365 | { |
---|
366 | imcb_error( ic, "Syntax error" ); |
---|
367 | imc_logout( ic, TRUE ); |
---|
368 | return( 0 ); |
---|
369 | } |
---|
370 | |
---|
371 | http_decode( cmd[4] ); |
---|
372 | cap = atoi( cmd[5] ); |
---|
373 | imcb_rename_buddy( ic, cmd[2], cmd[4] ); |
---|
374 | |
---|
375 | st = msn_away_state_by_code( cmd[1] ); |
---|
376 | if( !st ) |
---|
377 | { |
---|
378 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
379 | st = msn_away_state_list + 1; |
---|
380 | } |
---|
381 | |
---|
382 | imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | |
---|
383 | ( st != msn_away_state_list ? OPT_AWAY : 0 ) | |
---|
384 | ( cap & 1 ? OPT_MOBILE : 0 ), |
---|
385 | st->name, NULL ); |
---|
386 | |
---|
387 | msn_sb_stop_keepalives( msn_sb_by_handle( ic, cmd[2] ) ); |
---|
388 | } |
---|
389 | else if( strcmp( cmd[0], "RNG" ) == 0 ) |
---|
390 | { |
---|
391 | struct msn_switchboard *sb; |
---|
392 | char *server; |
---|
393 | int session, port; |
---|
394 | |
---|
395 | if( num_parts < 7 ) |
---|
396 | { |
---|
397 | imcb_error( ic, "Syntax error" ); |
---|
398 | imc_logout( ic, TRUE ); |
---|
399 | return( 0 ); |
---|
400 | } |
---|
401 | |
---|
402 | session = atoi( cmd[1] ); |
---|
403 | |
---|
404 | server = strchr( cmd[2], ':' ); |
---|
405 | if( !server ) |
---|
406 | { |
---|
407 | imcb_error( ic, "Syntax error" ); |
---|
408 | imc_logout( ic, TRUE ); |
---|
409 | return( 0 ); |
---|
410 | } |
---|
411 | *server = 0; |
---|
412 | port = atoi( server + 1 ); |
---|
413 | server = cmd[2]; |
---|
414 | |
---|
415 | if( strcmp( cmd[3], "CKI" ) != 0 ) |
---|
416 | { |
---|
417 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
418 | imc_logout( ic, TRUE ); |
---|
419 | return( 0 ); |
---|
420 | } |
---|
421 | |
---|
422 | debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] ); |
---|
423 | |
---|
424 | if( ( sb = msn_sb_create( ic, server, port, cmd[4], session ) ) == NULL ) |
---|
425 | { |
---|
426 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
427 | definitely something serious (we ran out of file descriptors?). */ |
---|
428 | imcb_error( ic, "Could not create new switchboard" ); |
---|
429 | imc_logout( ic, TRUE ); |
---|
430 | return( 0 ); |
---|
431 | } |
---|
432 | else |
---|
433 | { |
---|
434 | sb->who = g_strdup( cmd[5] ); |
---|
435 | } |
---|
436 | } |
---|
437 | else if( strcmp( cmd[0], "OUT" ) == 0 ) |
---|
438 | { |
---|
439 | int allow_reconnect = TRUE; |
---|
440 | |
---|
441 | if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 ) |
---|
442 | { |
---|
443 | imcb_error( ic, "Someone else logged in with your account" ); |
---|
444 | allow_reconnect = FALSE; |
---|
445 | } |
---|
446 | else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 ) |
---|
447 | { |
---|
448 | imcb_error( ic, "Terminating session because of server shutdown" ); |
---|
449 | } |
---|
450 | else |
---|
451 | { |
---|
452 | imcb_error( ic, "Session terminated by remote server (reason unknown)" ); |
---|
453 | } |
---|
454 | |
---|
455 | imc_logout( ic, allow_reconnect ); |
---|
456 | return( 0 ); |
---|
457 | } |
---|
458 | else if( strcmp( cmd[0], "IPG" ) == 0 ) |
---|
459 | { |
---|
460 | imcb_error( ic, "Received IPG command, we don't handle them yet." ); |
---|
461 | |
---|
462 | md->handler->msglen = atoi( cmd[1] ); |
---|
463 | |
---|
464 | if( md->handler->msglen <= 0 ) |
---|
465 | { |
---|
466 | imcb_error( ic, "Syntax error" ); |
---|
467 | imc_logout( ic, TRUE ); |
---|
468 | return( 0 ); |
---|
469 | } |
---|
470 | } |
---|
471 | #if 0 |
---|
472 | else if( strcmp( cmd[0], "ADG" ) == 0 ) |
---|
473 | { |
---|
474 | char *group = g_strdup( cmd[3] ); |
---|
475 | int groupnum, i; |
---|
476 | GSList *l, *next; |
---|
477 | |
---|
478 | http_decode( group ); |
---|
479 | if( sscanf( cmd[4], "%d", &groupnum ) == 1 ) |
---|
480 | { |
---|
481 | if( groupnum >= md->groupcount ) |
---|
482 | { |
---|
483 | md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 ); |
---|
484 | for( i = md->groupcount; i <= groupnum; i ++ ) |
---|
485 | md->grouplist[i] = NULL; |
---|
486 | md->groupcount = groupnum + 1; |
---|
487 | } |
---|
488 | g_free( md->grouplist[groupnum] ); |
---|
489 | md->grouplist[groupnum] = group; |
---|
490 | } |
---|
491 | else |
---|
492 | { |
---|
493 | /* Shouldn't happen, but if it does, give up on the group. */ |
---|
494 | g_free( group ); |
---|
495 | imcb_error( ic, "Syntax error" ); |
---|
496 | imc_logout( ic, TRUE ); |
---|
497 | return 0; |
---|
498 | } |
---|
499 | |
---|
500 | for( l = md->grpq; l; l = next ) |
---|
501 | { |
---|
502 | struct msn_groupadd *ga = l->data; |
---|
503 | next = l->next; |
---|
504 | if( g_strcasecmp( ga->group, group ) == 0 ) |
---|
505 | { |
---|
506 | if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) ) |
---|
507 | return 0; |
---|
508 | |
---|
509 | g_free( ga->group ); |
---|
510 | g_free( ga->who ); |
---|
511 | g_free( ga ); |
---|
512 | md->grpq = g_slist_remove( md->grpq, ga ); |
---|
513 | } |
---|
514 | } |
---|
515 | } |
---|
516 | #endif |
---|
517 | else if( strcmp( cmd[0], "GCF" ) == 0 ) |
---|
518 | { |
---|
519 | /* Coming up is cmd[2] bytes of stuff we're supposed to |
---|
520 | censore. Meh. */ |
---|
521 | md->handler->msglen = atoi( cmd[2] ); |
---|
522 | } |
---|
523 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
524 | { |
---|
525 | /* Status message. */ |
---|
526 | if( num_parts >= 4 ) |
---|
527 | md->handler->msglen = atoi( cmd[3] ); |
---|
528 | } |
---|
529 | else if( strcmp( cmd[0], "NOT" ) == 0 ) |
---|
530 | { |
---|
531 | /* Some kind of notification, poorly documented but |
---|
532 | apparently used to announce address book changes. */ |
---|
533 | if( num_parts >= 2 ) |
---|
534 | md->handler->msglen = atoi( cmd[1] ); |
---|
535 | } |
---|
536 | else if( isdigit( cmd[0][0] ) ) |
---|
537 | { |
---|
538 | int num = atoi( cmd[0] ); |
---|
539 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
540 | |
---|
541 | imcb_error( ic, "Error reported by MSN server: %s", err->text ); |
---|
542 | |
---|
543 | if( err->flags & STATUS_FATAL ) |
---|
544 | { |
---|
545 | imc_logout( ic, TRUE ); |
---|
546 | return( 0 ); |
---|
547 | } |
---|
548 | |
---|
549 | /* Oh yes, errors can have payloads too now. Discard them for now. */ |
---|
550 | if( num_parts >= 3 ) |
---|
551 | md->handler->msglen = atoi( cmd[2] ); |
---|
552 | } |
---|
553 | else |
---|
554 | { |
---|
555 | /* debug( "Received unknown command from main server: %s", cmd[0] ); */ |
---|
556 | } |
---|
557 | |
---|
558 | return( 1 ); |
---|
559 | } |
---|
560 | |
---|
561 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
562 | { |
---|
563 | struct im_connection *ic = data; |
---|
564 | char *body; |
---|
565 | int blen = 0; |
---|
566 | |
---|
567 | if( !num_parts ) |
---|
568 | return( 1 ); |
---|
569 | |
---|
570 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
571 | { |
---|
572 | body += 4; |
---|
573 | blen = msglen - ( body - msg ); |
---|
574 | } |
---|
575 | |
---|
576 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
577 | { |
---|
578 | if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 ) |
---|
579 | { |
---|
580 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
581 | |
---|
582 | if( !ct ) |
---|
583 | return( 1 ); |
---|
584 | |
---|
585 | if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 ) |
---|
586 | { |
---|
587 | char *mtype; |
---|
588 | char *arg1; |
---|
589 | |
---|
590 | if( !body ) |
---|
591 | return( 1 ); |
---|
592 | |
---|
593 | mtype = msn_findheader( body, "Type:", blen ); |
---|
594 | arg1 = msn_findheader( body, "Arg1:", blen ); |
---|
595 | |
---|
596 | if( mtype && strcmp( mtype, "1" ) == 0 ) |
---|
597 | { |
---|
598 | if( arg1 ) |
---|
599 | imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 ); |
---|
600 | } |
---|
601 | |
---|
602 | g_free( arg1 ); |
---|
603 | g_free( mtype ); |
---|
604 | } |
---|
605 | else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 ) |
---|
606 | { |
---|
607 | /* We don't care about this profile for now... */ |
---|
608 | } |
---|
609 | else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 ) |
---|
610 | { |
---|
611 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
612 | { |
---|
613 | char *inbox = msn_findheader( body, "Inbox-Unread:", blen ); |
---|
614 | char *folders = msn_findheader( body, "Folders-Unread:", blen ); |
---|
615 | |
---|
616 | if( inbox && folders ) |
---|
617 | imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders ); |
---|
618 | |
---|
619 | g_free( inbox ); |
---|
620 | g_free( folders ); |
---|
621 | } |
---|
622 | } |
---|
623 | else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 ) |
---|
624 | { |
---|
625 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
626 | { |
---|
627 | char *from = msn_findheader( body, "From-Addr:", blen ); |
---|
628 | char *fromname = msn_findheader( body, "From:", blen ); |
---|
629 | |
---|
630 | if( from && fromname ) |
---|
631 | imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from ); |
---|
632 | |
---|
633 | g_free( from ); |
---|
634 | g_free( fromname ); |
---|
635 | } |
---|
636 | } |
---|
637 | else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 ) |
---|
638 | { |
---|
639 | /* Sorry, but this one really is *USELESS* */ |
---|
640 | } |
---|
641 | else |
---|
642 | { |
---|
643 | debug( "Can't handle %s packet from notification server", ct ); |
---|
644 | } |
---|
645 | |
---|
646 | g_free( ct ); |
---|
647 | } |
---|
648 | } |
---|
649 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
650 | { |
---|
651 | struct xt_node *psm; |
---|
652 | char *psm_text = NULL; |
---|
653 | |
---|
654 | psm = xt_from_string( msg ); |
---|
655 | if( psm && strcmp( psm->name, "Data" ) == 0 && |
---|
656 | ( psm = xt_find_node( psm->children, "PSM" ) ) ) |
---|
657 | psm_text = psm->text; |
---|
658 | |
---|
659 | imcb_buddy_status_msg( ic, cmd[1], psm_text ); |
---|
660 | xt_free_node( psm ); |
---|
661 | } |
---|
662 | else if( strcmp( cmd[0], "ADL" ) == 0 ) |
---|
663 | { |
---|
664 | struct xt_node *adl, *d, *c; |
---|
665 | |
---|
666 | if( !( adl = xt_from_string( msg ) ) ) |
---|
667 | return 1; |
---|
668 | |
---|
669 | for( d = adl->children; d; d = d->next ) |
---|
670 | { |
---|
671 | char *dn; |
---|
672 | if( strcmp( d->name, "d" ) != 0 || |
---|
673 | ( dn = xt_find_attr( d, "n" ) ) == NULL ) |
---|
674 | continue; |
---|
675 | for( c = d->children; c; c = c->next ) |
---|
676 | { |
---|
677 | bee_user_t *bu; |
---|
678 | struct msn_buddy_data *bd; |
---|
679 | char *cn, *handle, *f, *l; |
---|
680 | int flags; |
---|
681 | |
---|
682 | if( strcmp( c->name, "c" ) != 0 || |
---|
683 | ( l = xt_find_attr( c, "l" ) ) == NULL || |
---|
684 | ( cn = xt_find_attr( c, "n" ) ) == NULL ) |
---|
685 | continue; |
---|
686 | |
---|
687 | handle = g_strdup_printf( "%s@%s", cn, dn ); |
---|
688 | if( !( ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) || |
---|
689 | ( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) ) ) |
---|
690 | { |
---|
691 | g_free( handle ); |
---|
692 | continue; |
---|
693 | } |
---|
694 | g_free( handle ); |
---|
695 | bd = bu->data; |
---|
696 | |
---|
697 | if( ( f = xt_find_attr( c, "f" ) ) ) |
---|
698 | { |
---|
699 | http_decode( f ); |
---|
700 | imcb_rename_buddy( ic, bu->handle, f ); |
---|
701 | } |
---|
702 | |
---|
703 | flags = atoi( l ) & 15; |
---|
704 | if( bd->flags != flags ) |
---|
705 | { |
---|
706 | bd->flags = flags; |
---|
707 | msn_buddy_ask( bu ); |
---|
708 | } |
---|
709 | } |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | return( 1 ); |
---|
714 | } |
---|
715 | |
---|
716 | void msn_auth_got_passport_token( struct im_connection *ic, const char *token, const char *error ) |
---|
717 | { |
---|
718 | struct msn_data *md; |
---|
719 | |
---|
720 | /* Dead connection? */ |
---|
721 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
722 | return; |
---|
723 | |
---|
724 | md = ic->proto_data; |
---|
725 | |
---|
726 | if( token ) |
---|
727 | { |
---|
728 | msn_ns_write( ic, -1, "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token ); |
---|
729 | } |
---|
730 | else |
---|
731 | { |
---|
732 | imcb_error( ic, "Error during Passport authentication: %s", error ); |
---|
733 | imc_logout( ic, TRUE ); |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | void msn_auth_got_contact_list( struct im_connection *ic ) |
---|
738 | { |
---|
739 | struct msn_data *md; |
---|
740 | |
---|
741 | /* Dead connection? */ |
---|
742 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
743 | return; |
---|
744 | |
---|
745 | md = ic->proto_data; |
---|
746 | msn_ns_write( ic, -1, "BLP %d %s\r\n", ++md->trId, "BL" ); |
---|
747 | } |
---|
748 | |
---|
749 | static gboolean msn_ns_send_adl_1( gpointer key, gpointer value, gpointer data ) |
---|
750 | { |
---|
751 | struct xt_node *adl = data, *d, *c; |
---|
752 | struct bee_user *bu = value; |
---|
753 | struct msn_buddy_data *bd = bu->data; |
---|
754 | struct msn_data *md = bu->ic->proto_data; |
---|
755 | char handle[strlen(bu->handle)]; |
---|
756 | char *domain; |
---|
757 | char l[4]; |
---|
758 | |
---|
759 | if( ( bd->flags & 7 ) == 0 || ( bd->flags & MSN_BUDDY_ADL_SYNCED ) ) |
---|
760 | return FALSE; |
---|
761 | |
---|
762 | strcpy( handle, bu->handle ); |
---|
763 | if( ( domain = strchr( handle, '@' ) ) == NULL ) /* WTF */ |
---|
764 | return FALSE; |
---|
765 | *domain = '\0'; |
---|
766 | domain ++; |
---|
767 | |
---|
768 | if( ( d = adl->children ) == NULL || |
---|
769 | g_strcasecmp( xt_find_attr( d, "n" ), domain ) != 0 ) |
---|
770 | { |
---|
771 | d = xt_new_node( "d", NULL, NULL ); |
---|
772 | xt_add_attr( d, "n", domain ); |
---|
773 | xt_insert_child( adl, d ); |
---|
774 | } |
---|
775 | |
---|
776 | g_snprintf( l, sizeof( l ), "%d", bd->flags & 7 ); |
---|
777 | c = xt_new_node( "c", NULL, NULL ); |
---|
778 | xt_add_attr( c, "n", handle ); |
---|
779 | xt_add_attr( c, "l", l ); |
---|
780 | xt_add_attr( c, "t", "1" ); /* 1 means normal, 4 means mobile? */ |
---|
781 | xt_insert_child( d, c ); |
---|
782 | |
---|
783 | /* Do this in batches of 100. */ |
---|
784 | bd->flags |= MSN_BUDDY_ADL_SYNCED; |
---|
785 | return (--md->adl_todo % 140) == 0; |
---|
786 | } |
---|
787 | |
---|
788 | static void msn_ns_send_adl( struct im_connection *ic ) |
---|
789 | { |
---|
790 | struct xt_node *adl; |
---|
791 | struct msn_data *md = ic->proto_data; |
---|
792 | char *adls; |
---|
793 | |
---|
794 | adl = xt_new_node( "ml", NULL, NULL ); |
---|
795 | xt_add_attr( adl, "l", "1" ); |
---|
796 | g_tree_foreach( md->domaintree, msn_ns_send_adl_1, adl ); |
---|
797 | if( adl->children == NULL ) |
---|
798 | { |
---|
799 | /* This tells the caller that we're done now. */ |
---|
800 | md->adl_todo = -1; |
---|
801 | xt_free_node( adl ); |
---|
802 | return; |
---|
803 | } |
---|
804 | |
---|
805 | adls = xt_to_string( adl ); |
---|
806 | msn_ns_write( ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen( adls ), adls ); |
---|
807 | g_free( adls ); |
---|
808 | } |
---|
809 | |
---|
810 | static void msn_ns_send_adl_start( struct im_connection *ic ) |
---|
811 | { |
---|
812 | struct msn_data *md; |
---|
813 | GSList *l; |
---|
814 | |
---|
815 | /* Dead connection? */ |
---|
816 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
817 | return; |
---|
818 | |
---|
819 | md = ic->proto_data; |
---|
820 | md->adl_todo = 0; |
---|
821 | for( l = ic->bee->users; l; l = l->next ) |
---|
822 | { |
---|
823 | bee_user_t *bu = l->data; |
---|
824 | struct msn_buddy_data *bd = bu->data; |
---|
825 | |
---|
826 | if( bu->ic != ic || ( bd->flags & 7 ) == 0 ) |
---|
827 | continue; |
---|
828 | |
---|
829 | bd->flags &= ~MSN_BUDDY_ADL_SYNCED; |
---|
830 | md->adl_todo++; |
---|
831 | } |
---|
832 | |
---|
833 | msn_ns_send_adl( ic ); |
---|
834 | } |
---|
835 | |
---|
836 | int msn_ns_finish_login( struct im_connection *ic ) |
---|
837 | { |
---|
838 | struct msn_data *md = ic->proto_data; |
---|
839 | |
---|
840 | if( ic->flags & OPT_LOGGED_IN ) |
---|
841 | return 1; |
---|
842 | |
---|
843 | if( md->adl_todo < 0 ) |
---|
844 | md->flags |= MSN_DONE_ADL; |
---|
845 | |
---|
846 | if( ( md->flags & MSN_DONE_ADL ) && ( md->flags & MSN_GOT_PROFILE ) ) |
---|
847 | return msn_ns_set_display_name( ic, set_getstr( &ic->acc->set, "display_name" ) ); |
---|
848 | else |
---|
849 | return 1; |
---|
850 | } |
---|