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