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 - 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., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 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 |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
42 | |
---|
43 | set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); |
---|
44 | set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc ); |
---|
45 | |
---|
46 | acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE; |
---|
47 | } |
---|
48 | |
---|
49 | static void msn_login( account_t *acc ) |
---|
50 | { |
---|
51 | struct im_connection *ic = imcb_new( acc ); |
---|
52 | struct msn_data *md = g_new0( struct msn_data, 1 ); |
---|
53 | |
---|
54 | ic->proto_data = md; |
---|
55 | md->fd = -1; |
---|
56 | |
---|
57 | if( strchr( acc->user, '@' ) == NULL ) |
---|
58 | { |
---|
59 | imcb_error( ic, "Invalid account name" ); |
---|
60 | imc_logout( ic, FALSE ); |
---|
61 | return; |
---|
62 | } |
---|
63 | |
---|
64 | imcb_log( ic, "Connecting" ); |
---|
65 | |
---|
66 | md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic ); |
---|
67 | if( md->fd < 0 ) |
---|
68 | { |
---|
69 | imcb_error( ic, "Could not connect to server" ); |
---|
70 | imc_logout( ic, TRUE ); |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | md->ic = ic; |
---|
75 | md->away_state = msn_away_state_list; |
---|
76 | md->domaintree = g_tree_new( msn_domaintree_cmp ); |
---|
77 | |
---|
78 | msn_connections = g_slist_append( msn_connections, ic ); |
---|
79 | } |
---|
80 | |
---|
81 | static void msn_logout( struct im_connection *ic ) |
---|
82 | { |
---|
83 | struct msn_data *md = ic->proto_data; |
---|
84 | GSList *l; |
---|
85 | int i; |
---|
86 | |
---|
87 | if( md ) |
---|
88 | { |
---|
89 | /** Disabling MSN ft support for now. |
---|
90 | while( md->filetransfers ) { |
---|
91 | imcb_file_canceled( md->filetransfers->data, "Closing connection" ); |
---|
92 | } |
---|
93 | */ |
---|
94 | |
---|
95 | if( md->fd >= 0 ) |
---|
96 | closesocket( md->fd ); |
---|
97 | |
---|
98 | if( md->handler ) |
---|
99 | { |
---|
100 | if( md->handler->rxq ) g_free( md->handler->rxq ); |
---|
101 | if( md->handler->cmd_text ) g_free( md->handler->cmd_text ); |
---|
102 | g_free( md->handler ); |
---|
103 | } |
---|
104 | |
---|
105 | while( md->switchboards ) |
---|
106 | msn_sb_destroy( md->switchboards->data ); |
---|
107 | |
---|
108 | msn_msgq_purge( ic, &md->msgq ); |
---|
109 | |
---|
110 | for( i = 0; i < sizeof( md->tokens ) / sizeof( md->tokens[0] ); i ++ ) |
---|
111 | g_free( md->tokens[i] ); |
---|
112 | g_free( md->lock_key ); |
---|
113 | |
---|
114 | while( md->groups ) |
---|
115 | { |
---|
116 | struct msn_group *mg = md->groups->data; |
---|
117 | g_free( mg->id ); |
---|
118 | g_free( mg->name ); |
---|
119 | g_free( mg ); |
---|
120 | md->groups = g_slist_remove( md->groups, mg ); |
---|
121 | } |
---|
122 | |
---|
123 | g_tree_destroy( md->domaintree ); |
---|
124 | md->domaintree = NULL; |
---|
125 | |
---|
126 | while( md->grpq ) |
---|
127 | { |
---|
128 | struct msn_groupadd *ga = md->grpq->data; |
---|
129 | g_free( ga->group ); |
---|
130 | g_free( ga->who ); |
---|
131 | g_free( ga ); |
---|
132 | md->grpq = g_slist_remove( md->grpq, ga ); |
---|
133 | } |
---|
134 | |
---|
135 | g_free( md ); |
---|
136 | } |
---|
137 | |
---|
138 | for( l = ic->permit; l; l = l->next ) |
---|
139 | g_free( l->data ); |
---|
140 | g_slist_free( ic->permit ); |
---|
141 | |
---|
142 | for( l = ic->deny; l; l = l->next ) |
---|
143 | g_free( l->data ); |
---|
144 | g_slist_free( ic->deny ); |
---|
145 | |
---|
146 | msn_connections = g_slist_remove( msn_connections, ic ); |
---|
147 | } |
---|
148 | |
---|
149 | static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
150 | { |
---|
151 | struct msn_switchboard *sb; |
---|
152 | |
---|
153 | #ifdef DEBUG |
---|
154 | if( strcmp( who, "raw" ) == 0 ) |
---|
155 | { |
---|
156 | msn_write( ic, message, strlen( message ) ); |
---|
157 | msn_write( ic, "\r\n", 2 ); |
---|
158 | } |
---|
159 | else |
---|
160 | #endif |
---|
161 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
162 | { |
---|
163 | return( msn_sb_sendmessage( sb, message ) ); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | struct msn_message *m; |
---|
168 | |
---|
169 | /* Create a message. We have to arrange a usable switchboard, and send the message later. */ |
---|
170 | m = g_new0( struct msn_message, 1 ); |
---|
171 | m->who = g_strdup( who ); |
---|
172 | m->text = g_strdup( message ); |
---|
173 | |
---|
174 | return msn_sb_write_msg( ic, m ); |
---|
175 | } |
---|
176 | |
---|
177 | return( 0 ); |
---|
178 | } |
---|
179 | |
---|
180 | static GList *msn_away_states( struct im_connection *ic ) |
---|
181 | { |
---|
182 | static GList *l = NULL; |
---|
183 | int i; |
---|
184 | |
---|
185 | if( l == NULL ) |
---|
186 | for( i = 0; *msn_away_state_list[i].code; i ++ ) |
---|
187 | if( *msn_away_state_list[i].name ) |
---|
188 | l = g_list_append( l, (void*) msn_away_state_list[i].name ); |
---|
189 | |
---|
190 | return l; |
---|
191 | } |
---|
192 | |
---|
193 | static void msn_set_away( struct im_connection *ic, char *state, char *message ) |
---|
194 | { |
---|
195 | char buf[1024]; |
---|
196 | char *uux; |
---|
197 | struct msn_data *md = ic->proto_data; |
---|
198 | |
---|
199 | if( state == NULL ) |
---|
200 | md->away_state = msn_away_state_list; |
---|
201 | else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL ) |
---|
202 | md->away_state = msn_away_state_list + 1; |
---|
203 | |
---|
204 | g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, md->away_state->code ); |
---|
205 | if( !msn_write( ic, buf, strlen( buf ) ) ) |
---|
206 | return; |
---|
207 | |
---|
208 | uux = g_markup_printf_escaped( "<Data><PSM>%s</PSM><CurrentMedia></CurrentMedia>" |
---|
209 | "</Data>", message ? message : "" ); |
---|
210 | g_snprintf( buf, sizeof( buf ), "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux ); |
---|
211 | if( !msn_write( ic, buf, strlen( buf ) ) ) |
---|
212 | return; |
---|
213 | } |
---|
214 | |
---|
215 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
216 | { |
---|
217 | /* Just make an URL and let the user fetch the info */ |
---|
218 | imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who ); |
---|
219 | } |
---|
220 | |
---|
221 | static void msn_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
222 | { |
---|
223 | struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who ); |
---|
224 | |
---|
225 | msn_buddy_list_add( ic, MSN_BUDDY_FL, who, who, group ); |
---|
226 | if( bu && bu->group ) |
---|
227 | msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, bu->group->name ); |
---|
228 | } |
---|
229 | |
---|
230 | static void msn_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
231 | { |
---|
232 | msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, NULL ); |
---|
233 | } |
---|
234 | |
---|
235 | static void msn_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
236 | { |
---|
237 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
238 | |
---|
239 | if( sb ) |
---|
240 | msn_sb_sendmessage( sb, message ); |
---|
241 | /* FIXME: Error handling (although this can't happen unless something's |
---|
242 | already severely broken) disappeared here! */ |
---|
243 | } |
---|
244 | |
---|
245 | static void msn_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
246 | { |
---|
247 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
248 | char buf[1024]; |
---|
249 | |
---|
250 | if( sb ) |
---|
251 | { |
---|
252 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
253 | msn_sb_write( sb, buf, strlen( buf ) ); |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | static void msn_chat_leave( struct groupchat *c ) |
---|
258 | { |
---|
259 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
260 | |
---|
261 | if( sb ) |
---|
262 | msn_sb_write( sb, "OUT\r\n", 5 ); |
---|
263 | } |
---|
264 | |
---|
265 | static struct groupchat *msn_chat_with( struct im_connection *ic, char *who ) |
---|
266 | { |
---|
267 | struct msn_switchboard *sb; |
---|
268 | struct groupchat *c = imcb_chat_new( ic, who ); |
---|
269 | |
---|
270 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
271 | { |
---|
272 | debug( "Converting existing switchboard to %s to a groupchat", who ); |
---|
273 | return msn_sb_to_chat( sb ); |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | struct msn_message *m; |
---|
278 | |
---|
279 | /* Create a magic message. This is quite hackish, but who cares? :-P */ |
---|
280 | m = g_new0( struct msn_message, 1 ); |
---|
281 | m->who = g_strdup( who ); |
---|
282 | m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE ); |
---|
283 | |
---|
284 | msn_sb_write_msg( ic, m ); |
---|
285 | |
---|
286 | return c; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | static void msn_keepalive( struct im_connection *ic ) |
---|
291 | { |
---|
292 | msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) ); |
---|
293 | } |
---|
294 | |
---|
295 | static void msn_add_permit( struct im_connection *ic, char *who ) |
---|
296 | { |
---|
297 | msn_buddy_list_add( ic, MSN_BUDDY_AL, who, who, NULL ); |
---|
298 | } |
---|
299 | |
---|
300 | static void msn_rem_permit( struct im_connection *ic, char *who ) |
---|
301 | { |
---|
302 | msn_buddy_list_remove( ic, MSN_BUDDY_AL, who, NULL ); |
---|
303 | } |
---|
304 | |
---|
305 | static void msn_add_deny( struct im_connection *ic, char *who ) |
---|
306 | { |
---|
307 | struct msn_switchboard *sb; |
---|
308 | |
---|
309 | msn_buddy_list_add( ic, MSN_BUDDY_BL, who, who, NULL ); |
---|
310 | |
---|
311 | /* If there's still a conversation with this person, close it. */ |
---|
312 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
313 | { |
---|
314 | msn_sb_destroy( sb ); |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | static void msn_rem_deny( struct im_connection *ic, char *who ) |
---|
319 | { |
---|
320 | msn_buddy_list_remove( ic, MSN_BUDDY_BL, who, NULL ); |
---|
321 | } |
---|
322 | |
---|
323 | static int msn_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
324 | { |
---|
325 | struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who ); |
---|
326 | |
---|
327 | if( !( bu->flags & BEE_USER_ONLINE ) ) |
---|
328 | return 0; |
---|
329 | else if( typing & OPT_TYPING ) |
---|
330 | return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) ); |
---|
331 | else |
---|
332 | return 1; |
---|
333 | } |
---|
334 | |
---|
335 | static char *set_eval_display_name( set_t *set, char *value ) |
---|
336 | { |
---|
337 | account_t *acc = set->data; |
---|
338 | struct im_connection *ic = acc->ic; |
---|
339 | struct msn_data *md = ic->proto_data; |
---|
340 | |
---|
341 | if( strlen( value ) > 129 ) |
---|
342 | { |
---|
343 | imcb_log( ic, "Maximum name length exceeded" ); |
---|
344 | return NULL; |
---|
345 | } |
---|
346 | |
---|
347 | if( md->flags & MSN_GOT_PROFILE_DN ) |
---|
348 | imcb_log( ic, "Warning: Persistent name changes for this account have to be done " |
---|
349 | "in the profile. BitlBee doesn't currently support this." ); |
---|
350 | |
---|
351 | msn_soap_addressbook_set_display_name( ic, value ); |
---|
352 | return msn_ns_set_display_name( ic, value ) ? value : NULL; |
---|
353 | } |
---|
354 | |
---|
355 | static void msn_buddy_data_add( bee_user_t *bu ) |
---|
356 | { |
---|
357 | struct msn_data *md = bu->ic->proto_data; |
---|
358 | bu->data = g_new0( struct msn_buddy_data, 1 ); |
---|
359 | g_tree_insert( md->domaintree, bu->handle, bu ); |
---|
360 | } |
---|
361 | |
---|
362 | static void msn_buddy_data_free( bee_user_t *bu ) |
---|
363 | { |
---|
364 | struct msn_data *md = bu->ic->proto_data; |
---|
365 | g_tree_remove( md->domaintree, bu->handle ); |
---|
366 | g_free( bu->data ); |
---|
367 | } |
---|
368 | |
---|
369 | void msn_initmodule() |
---|
370 | { |
---|
371 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
372 | |
---|
373 | ret->name = "msn"; |
---|
374 | ret->login = msn_login; |
---|
375 | ret->init = msn_init; |
---|
376 | ret->logout = msn_logout; |
---|
377 | ret->buddy_msg = msn_buddy_msg; |
---|
378 | ret->away_states = msn_away_states; |
---|
379 | ret->set_away = msn_set_away; |
---|
380 | ret->get_info = msn_get_info; |
---|
381 | ret->add_buddy = msn_add_buddy; |
---|
382 | ret->remove_buddy = msn_remove_buddy; |
---|
383 | ret->chat_msg = msn_chat_msg; |
---|
384 | ret->chat_invite = msn_chat_invite; |
---|
385 | ret->chat_leave = msn_chat_leave; |
---|
386 | ret->chat_with = msn_chat_with; |
---|
387 | ret->keepalive = msn_keepalive; |
---|
388 | ret->add_permit = msn_add_permit; |
---|
389 | ret->rem_permit = msn_rem_permit; |
---|
390 | ret->add_deny = msn_add_deny; |
---|
391 | ret->rem_deny = msn_rem_deny; |
---|
392 | ret->send_typing = msn_send_typing; |
---|
393 | ret->handle_cmp = g_strcasecmp; |
---|
394 | ret->buddy_data_add = msn_buddy_data_add; |
---|
395 | ret->buddy_data_free = msn_buddy_data_free; |
---|
396 | |
---|
397 | //ret->transfer_request = msn_ftp_transfer_request; |
---|
398 | |
---|
399 | register_protocol(ret); |
---|
400 | } |
---|