1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2013 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* MSN module - Main file; functions to be called from BitlBee */ |
---|
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 "nogaim.h" |
---|
27 | #include "soap.h" |
---|
28 | #include "msn.h" |
---|
29 | |
---|
30 | int msn_chat_id; |
---|
31 | GSList *msn_connections; |
---|
32 | GSList *msn_switchboards; |
---|
33 | |
---|
34 | static char *set_eval_display_name( set_t *set, char *value ); |
---|
35 | |
---|
36 | static void msn_init( account_t *acc ) |
---|
37 | { |
---|
38 | set_t *s; |
---|
39 | |
---|
40 | s = set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc ); |
---|
41 | s->flags |= SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
42 | |
---|
43 | s = set_add( &acc->set, "server", MSN_NS_HOST, set_eval_account, acc ); |
---|
44 | s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
45 | |
---|
46 | s = set_add( &acc->set, "port", MSN_NS_PORT, set_eval_int, acc ); |
---|
47 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
48 | |
---|
49 | set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); |
---|
50 | set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc ); |
---|
51 | |
---|
52 | acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE | |
---|
53 | ACC_FLAG_HANDLE_DOMAINS; |
---|
54 | } |
---|
55 | |
---|
56 | static void msn_login( account_t *acc ) |
---|
57 | { |
---|
58 | struct im_connection *ic = imcb_new( acc ); |
---|
59 | struct msn_data *md = g_new0( struct msn_data, 1 ); |
---|
60 | |
---|
61 | ic->proto_data = md; |
---|
62 | ic->flags |= OPT_PONGS | OPT_PONGED; |
---|
63 | |
---|
64 | if( strchr( acc->user, '@' ) == NULL ) |
---|
65 | { |
---|
66 | imcb_error( ic, "Invalid account name" ); |
---|
67 | imc_logout( ic, FALSE ); |
---|
68 | return; |
---|
69 | } |
---|
70 | |
---|
71 | md->ic = ic; |
---|
72 | md->away_state = msn_away_state_list; |
---|
73 | md->domaintree = g_tree_new( msn_domaintree_cmp ); |
---|
74 | md->ns->fd = -1; |
---|
75 | |
---|
76 | msn_connections = g_slist_prepend( msn_connections, ic ); |
---|
77 | |
---|
78 | imcb_log( ic, "Connecting" ); |
---|
79 | msn_ns_connect( ic, md->ns, |
---|
80 | set_getstr( &ic->acc->set, "server" ), |
---|
81 | set_getint( &ic->acc->set, "port" ) ); |
---|
82 | } |
---|
83 | |
---|
84 | static void msn_logout( struct im_connection *ic ) |
---|
85 | { |
---|
86 | struct msn_data *md = ic->proto_data; |
---|
87 | GSList *l; |
---|
88 | int i; |
---|
89 | |
---|
90 | if( md ) |
---|
91 | { |
---|
92 | /** Disabling MSN ft support for now. |
---|
93 | while( md->filetransfers ) { |
---|
94 | imcb_file_canceled( md->filetransfers->data, "Closing connection" ); |
---|
95 | } |
---|
96 | */ |
---|
97 | |
---|
98 | msn_ns_close( md->ns ); |
---|
99 | |
---|
100 | while( md->switchboards ) |
---|
101 | msn_sb_destroy( md->switchboards->data ); |
---|
102 | |
---|
103 | msn_msgq_purge( ic, &md->msgq ); |
---|
104 | msn_soapq_flush( ic, FALSE ); |
---|
105 | |
---|
106 | for( i = 0; i < sizeof( md->tokens ) / sizeof( md->tokens[0] ); i ++ ) |
---|
107 | g_free( md->tokens[i] ); |
---|
108 | g_free( md->lock_key ); |
---|
109 | g_free( md->pp_policy ); |
---|
110 | g_free( md->uuid ); |
---|
111 | |
---|
112 | while( md->groups ) |
---|
113 | { |
---|
114 | struct msn_group *mg = md->groups->data; |
---|
115 | g_free( mg->id ); |
---|
116 | g_free( mg->name ); |
---|
117 | g_free( mg ); |
---|
118 | md->groups = g_slist_remove( md->groups, mg ); |
---|
119 | } |
---|
120 | |
---|
121 | g_free( md->profile_rid ); |
---|
122 | |
---|
123 | if( md->domaintree ) |
---|
124 | g_tree_destroy( md->domaintree ); |
---|
125 | md->domaintree = NULL; |
---|
126 | |
---|
127 | while( md->grpq ) |
---|
128 | { |
---|
129 | struct msn_groupadd *ga = md->grpq->data; |
---|
130 | g_free( ga->group ); |
---|
131 | g_free( ga->who ); |
---|
132 | g_free( ga ); |
---|
133 | md->grpq = g_slist_remove( md->grpq, ga ); |
---|
134 | } |
---|
135 | |
---|
136 | g_free( md ); |
---|
137 | } |
---|
138 | |
---|
139 | for( l = ic->permit; l; l = l->next ) |
---|
140 | g_free( l->data ); |
---|
141 | g_slist_free( ic->permit ); |
---|
142 | |
---|
143 | for( l = ic->deny; l; l = l->next ) |
---|
144 | g_free( l->data ); |
---|
145 | g_slist_free( ic->deny ); |
---|
146 | |
---|
147 | msn_connections = g_slist_remove( msn_connections, ic ); |
---|
148 | } |
---|
149 | |
---|
150 | static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
151 | { |
---|
152 | struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who ); |
---|
153 | struct msn_buddy_data *bd = bu ? bu->data : NULL; |
---|
154 | struct msn_switchboard *sb; |
---|
155 | |
---|
156 | #ifdef DEBUG |
---|
157 | if( strcmp( who, "raw" ) == 0 ) |
---|
158 | { |
---|
159 | msn_ns_write( ic, -1, "%s\r\n", message ); |
---|
160 | } |
---|
161 | else |
---|
162 | #endif |
---|
163 | if( bd && bd->flags & MSN_BUDDY_FED ) |
---|
164 | { |
---|
165 | msn_ns_sendmessage( ic, bu, message ); |
---|
166 | } |
---|
167 | else if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
168 | { |
---|
169 | return( msn_sb_sendmessage( sb, message ) ); |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | struct msn_message *m; |
---|
174 | |
---|
175 | /* Create a message. We have to arrange a usable switchboard, and send the message later. */ |
---|
176 | m = g_new0( struct msn_message, 1 ); |
---|
177 | m->who = g_strdup( who ); |
---|
178 | m->text = g_strdup( message ); |
---|
179 | |
---|
180 | return msn_sb_write_msg( ic, m ); |
---|
181 | } |
---|
182 | |
---|
183 | return( 0 ); |
---|
184 | } |
---|
185 | |
---|
186 | static GList *msn_away_states( struct im_connection *ic ) |
---|
187 | { |
---|
188 | static GList *l = NULL; |
---|
189 | int i; |
---|
190 | |
---|
191 | if( l == NULL ) |
---|
192 | for( i = 0; *msn_away_state_list[i].code; i ++ ) |
---|
193 | if( *msn_away_state_list[i].name ) |
---|
194 | l = g_list_append( l, (void*) msn_away_state_list[i].name ); |
---|
195 | |
---|
196 | return l; |
---|
197 | } |
---|
198 | |
---|
199 | static void msn_set_away( struct im_connection *ic, char *state, char *message ) |
---|
200 | { |
---|
201 | char *uux; |
---|
202 | struct msn_data *md = ic->proto_data; |
---|
203 | |
---|
204 | if( state == NULL ) |
---|
205 | md->away_state = msn_away_state_list; |
---|
206 | else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL ) |
---|
207 | md->away_state = msn_away_state_list + 1; |
---|
208 | |
---|
209 | if( !msn_ns_write( ic, -1, "CHG %d %s %d:%02d\r\n", ++md->trId, md->away_state->code, MSN_CAP1, MSN_CAP2 ) ) |
---|
210 | return; |
---|
211 | |
---|
212 | uux = g_markup_printf_escaped( "<EndpointData><Capabilities>%d:%02d" |
---|
213 | "</Capabilities></EndpointData>", |
---|
214 | MSN_CAP1, MSN_CAP2 ); |
---|
215 | msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux ); |
---|
216 | g_free( uux ); |
---|
217 | |
---|
218 | uux = g_markup_printf_escaped( "<PrivateEndpointData><EpName>%s</EpName>" |
---|
219 | "<Idle>%s</Idle><ClientType>%d</ClientType>" |
---|
220 | "<State>%s</State></PrivateEndpointData>", |
---|
221 | md->uuid, |
---|
222 | strcmp( md->away_state->code, "IDL" ) ? "false" : "true", |
---|
223 | 1, /* ? */ |
---|
224 | md->away_state->code ); |
---|
225 | msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux ); |
---|
226 | g_free( uux ); |
---|
227 | |
---|
228 | uux = g_markup_printf_escaped( "<Data><DDP></DDP><PSM>%s</PSM>" |
---|
229 | "<CurrentMedia></CurrentMedia>" |
---|
230 | "<MachineGuid>%s</MachineGuid></Data>", |
---|
231 | message ? message : "", md->uuid ); |
---|
232 | msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux ); |
---|
233 | g_free( uux ); |
---|
234 | } |
---|
235 | |
---|
236 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
237 | { |
---|
238 | /* Just make an URL and let the user fetch the info */ |
---|
239 | imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who ); |
---|
240 | } |
---|
241 | |
---|
242 | static void msn_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
243 | { |
---|
244 | struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who ); |
---|
245 | |
---|
246 | msn_buddy_list_add( ic, MSN_BUDDY_FL, who, who, group ); |
---|
247 | if( bu && bu->group ) |
---|
248 | msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, bu->group->name ); |
---|
249 | } |
---|
250 | |
---|
251 | static void msn_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
252 | { |
---|
253 | msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, NULL ); |
---|
254 | } |
---|
255 | |
---|
256 | static void msn_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
257 | { |
---|
258 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
259 | |
---|
260 | if( sb ) |
---|
261 | msn_sb_sendmessage( sb, message ); |
---|
262 | /* FIXME: Error handling (although this can't happen unless something's |
---|
263 | already severely broken) disappeared here! */ |
---|
264 | } |
---|
265 | |
---|
266 | static void msn_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
267 | { |
---|
268 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
269 | |
---|
270 | if( sb ) |
---|
271 | msn_sb_write( sb, "CAL %d %s\r\n", ++sb->trId, who ); |
---|
272 | } |
---|
273 | |
---|
274 | static void msn_chat_leave( struct groupchat *c ) |
---|
275 | { |
---|
276 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
277 | |
---|
278 | if( sb ) |
---|
279 | msn_sb_write( sb, "OUT\r\n" ); |
---|
280 | } |
---|
281 | |
---|
282 | static struct groupchat *msn_chat_with( struct im_connection *ic, char *who ) |
---|
283 | { |
---|
284 | struct msn_switchboard *sb; |
---|
285 | struct groupchat *c = imcb_chat_new( ic, who ); |
---|
286 | |
---|
287 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
288 | { |
---|
289 | debug( "Converting existing switchboard to %s to a groupchat", who ); |
---|
290 | return msn_sb_to_chat( sb ); |
---|
291 | } |
---|
292 | else |
---|
293 | { |
---|
294 | struct msn_message *m; |
---|
295 | |
---|
296 | /* Create a magic message. This is quite hackish, but who cares? :-P */ |
---|
297 | m = g_new0( struct msn_message, 1 ); |
---|
298 | m->who = g_strdup( who ); |
---|
299 | m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE ); |
---|
300 | |
---|
301 | msn_sb_write_msg( ic, m ); |
---|
302 | |
---|
303 | return c; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | static void msn_keepalive( struct im_connection *ic ) |
---|
308 | { |
---|
309 | msn_ns_write( ic, -1, "PNG\r\n" ); |
---|
310 | } |
---|
311 | |
---|
312 | static void msn_add_permit( struct im_connection *ic, char *who ) |
---|
313 | { |
---|
314 | msn_buddy_list_add( ic, MSN_BUDDY_AL, who, who, NULL ); |
---|
315 | } |
---|
316 | |
---|
317 | static void msn_rem_permit( struct im_connection *ic, char *who ) |
---|
318 | { |
---|
319 | msn_buddy_list_remove( ic, MSN_BUDDY_AL, who, NULL ); |
---|
320 | } |
---|
321 | |
---|
322 | static void msn_add_deny( struct im_connection *ic, char *who ) |
---|
323 | { |
---|
324 | struct msn_switchboard *sb; |
---|
325 | |
---|
326 | msn_buddy_list_add( ic, MSN_BUDDY_BL, who, who, NULL ); |
---|
327 | |
---|
328 | /* If there's still a conversation with this person, close it. */ |
---|
329 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
330 | { |
---|
331 | msn_sb_destroy( sb ); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | static void msn_rem_deny( struct im_connection *ic, char *who ) |
---|
336 | { |
---|
337 | msn_buddy_list_remove( ic, MSN_BUDDY_BL, who, NULL ); |
---|
338 | } |
---|
339 | |
---|
340 | static int msn_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
341 | { |
---|
342 | struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who ); |
---|
343 | |
---|
344 | if( !( bu->flags & BEE_USER_ONLINE ) ) |
---|
345 | return 0; |
---|
346 | else if( typing & OPT_TYPING ) |
---|
347 | return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) ); |
---|
348 | else |
---|
349 | return 1; |
---|
350 | } |
---|
351 | |
---|
352 | static char *set_eval_display_name( set_t *set, char *value ) |
---|
353 | { |
---|
354 | account_t *acc = set->data; |
---|
355 | struct im_connection *ic = acc->ic; |
---|
356 | struct msn_data *md = ic->proto_data; |
---|
357 | |
---|
358 | if( md->flags & MSN_EMAIL_UNVERIFIED ) |
---|
359 | imcb_log( ic, "Warning: Your e-mail address is unverified. MSN doesn't allow " |
---|
360 | "changing your display name until your e-mail address is verified." ); |
---|
361 | |
---|
362 | if( md->flags & MSN_GOT_PROFILE_DN ) |
---|
363 | msn_soap_profile_set_dn( ic, value ); |
---|
364 | else |
---|
365 | msn_soap_addressbook_set_display_name( ic, value ); |
---|
366 | |
---|
367 | return msn_ns_set_display_name( ic, value ) ? value : NULL; |
---|
368 | } |
---|
369 | |
---|
370 | static void msn_buddy_data_add( bee_user_t *bu ) |
---|
371 | { |
---|
372 | struct msn_data *md = bu->ic->proto_data; |
---|
373 | struct msn_buddy_data *bd; |
---|
374 | char *handle; |
---|
375 | |
---|
376 | bd = bu->data = g_new0( struct msn_buddy_data, 1 ); |
---|
377 | g_tree_insert( md->domaintree, bu->handle, bu ); |
---|
378 | |
---|
379 | for( handle = bu->handle; isdigit( *handle ); handle ++ ); |
---|
380 | if( *handle == ':' ) |
---|
381 | { |
---|
382 | /* Pass a nick hint so hopefully the stupid numeric prefix |
---|
383 | won't show up to the user. */ |
---|
384 | char *s = strchr( ++handle, '@' ); |
---|
385 | if( s ) |
---|
386 | { |
---|
387 | handle = g_strndup( handle, s - handle ); |
---|
388 | imcb_buddy_nick_hint( bu->ic, bu->handle, handle ); |
---|
389 | g_free( handle ); |
---|
390 | } |
---|
391 | |
---|
392 | bd->flags |= MSN_BUDDY_FED; |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | static void msn_buddy_data_free( bee_user_t *bu ) |
---|
397 | { |
---|
398 | struct msn_data *md = bu->ic->proto_data; |
---|
399 | struct msn_buddy_data *bd = bu->data; |
---|
400 | |
---|
401 | g_free( bd->cid ); |
---|
402 | g_free( bd ); |
---|
403 | |
---|
404 | g_tree_remove( md->domaintree, bu->handle ); |
---|
405 | } |
---|
406 | |
---|
407 | GList *msn_buddy_action_list( bee_user_t *bu ) |
---|
408 | { |
---|
409 | static GList *ret = NULL; |
---|
410 | |
---|
411 | if( ret == NULL ) |
---|
412 | { |
---|
413 | static const struct buddy_action ba[2] = { |
---|
414 | { "NUDGE", "Draw attention" }, |
---|
415 | }; |
---|
416 | |
---|
417 | ret = g_list_prepend( ret, (void*) ba + 0 ); |
---|
418 | } |
---|
419 | |
---|
420 | return ret; |
---|
421 | } |
---|
422 | |
---|
423 | void *msn_buddy_action( struct bee_user *bu, const char *action, char * const args[], void *data ) |
---|
424 | { |
---|
425 | if( g_strcasecmp( action, "NUDGE" ) == 0 ) |
---|
426 | msn_buddy_msg( bu->ic, bu->handle, NUDGE_MESSAGE, 0 ); |
---|
427 | |
---|
428 | return NULL; |
---|
429 | } |
---|
430 | |
---|
431 | void msn_initmodule() |
---|
432 | { |
---|
433 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
434 | |
---|
435 | ret->name = "msn"; |
---|
436 | ret->mms = 1409; /* this guess taken from libotr UPGRADING file */ |
---|
437 | ret->login = msn_login; |
---|
438 | ret->init = msn_init; |
---|
439 | ret->logout = msn_logout; |
---|
440 | ret->buddy_msg = msn_buddy_msg; |
---|
441 | ret->away_states = msn_away_states; |
---|
442 | ret->set_away = msn_set_away; |
---|
443 | ret->get_info = msn_get_info; |
---|
444 | ret->add_buddy = msn_add_buddy; |
---|
445 | ret->remove_buddy = msn_remove_buddy; |
---|
446 | ret->chat_msg = msn_chat_msg; |
---|
447 | ret->chat_invite = msn_chat_invite; |
---|
448 | ret->chat_leave = msn_chat_leave; |
---|
449 | ret->chat_with = msn_chat_with; |
---|
450 | ret->keepalive = msn_keepalive; |
---|
451 | ret->add_permit = msn_add_permit; |
---|
452 | ret->rem_permit = msn_rem_permit; |
---|
453 | ret->add_deny = msn_add_deny; |
---|
454 | ret->rem_deny = msn_rem_deny; |
---|
455 | ret->send_typing = msn_send_typing; |
---|
456 | ret->handle_cmp = g_strcasecmp; |
---|
457 | ret->buddy_data_add = msn_buddy_data_add; |
---|
458 | ret->buddy_data_free = msn_buddy_data_free; |
---|
459 | ret->buddy_action_list = msn_buddy_action_list; |
---|
460 | ret->buddy_action = msn_buddy_action; |
---|
461 | |
---|
462 | //ret->transfer_request = msn_ftp_transfer_request; |
---|
463 | |
---|
464 | register_protocol(ret); |
---|
465 | } |
---|