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