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 - 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 "msn.h" |
---|
28 | |
---|
29 | static char *msn_set_display_name( set_t *set, char *value ); |
---|
30 | |
---|
31 | static void msn_init( account_t *acc ) |
---|
32 | { |
---|
33 | set_t *s; |
---|
34 | |
---|
35 | s = set_add( &acc->set, "display_name", NULL, msn_set_display_name, acc ); |
---|
36 | s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
37 | |
---|
38 | s = set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); |
---|
39 | } |
---|
40 | |
---|
41 | static void msn_login( account_t *acc ) |
---|
42 | { |
---|
43 | struct im_connection *ic = imcb_new( acc ); |
---|
44 | struct msn_data *md = g_new0( struct msn_data, 1 ); |
---|
45 | |
---|
46 | ic->proto_data = md; |
---|
47 | md->fd = -1; |
---|
48 | |
---|
49 | if( strchr( acc->user, '@' ) == NULL ) |
---|
50 | { |
---|
51 | imcb_error( ic, "Invalid account name" ); |
---|
52 | imc_logout( ic, FALSE ); |
---|
53 | return; |
---|
54 | } |
---|
55 | |
---|
56 | imcb_log( ic, "Connecting" ); |
---|
57 | |
---|
58 | md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic ); |
---|
59 | if( md->fd < 0 ) |
---|
60 | { |
---|
61 | imcb_error( ic, "Could not connect to server" ); |
---|
62 | imc_logout( ic, TRUE ); |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | md->ic = ic; |
---|
67 | md->away_state = msn_away_state_list; |
---|
68 | |
---|
69 | msn_connections = g_slist_append( msn_connections, ic ); |
---|
70 | } |
---|
71 | |
---|
72 | static void msn_logout( struct im_connection *ic ) |
---|
73 | { |
---|
74 | struct msn_data *md = ic->proto_data; |
---|
75 | GSList *l; |
---|
76 | |
---|
77 | if( md ) |
---|
78 | { |
---|
79 | if( md->fd >= 0 ) |
---|
80 | closesocket( md->fd ); |
---|
81 | |
---|
82 | if( md->handler ) |
---|
83 | { |
---|
84 | if( md->handler->rxq ) g_free( md->handler->rxq ); |
---|
85 | if( md->handler->cmd_text ) g_free( md->handler->cmd_text ); |
---|
86 | g_free( md->handler ); |
---|
87 | } |
---|
88 | |
---|
89 | while( md->switchboards ) |
---|
90 | msn_sb_destroy( md->switchboards->data ); |
---|
91 | |
---|
92 | msn_msgq_purge( ic, &md->msgq ); |
---|
93 | |
---|
94 | while( md->groupcount > 0 ) |
---|
95 | g_free( md->grouplist[--md->groupcount] ); |
---|
96 | g_free( md->grouplist ); |
---|
97 | |
---|
98 | g_free( md ); |
---|
99 | } |
---|
100 | |
---|
101 | for( l = ic->permit; l; l = l->next ) |
---|
102 | g_free( l->data ); |
---|
103 | g_slist_free( ic->permit ); |
---|
104 | |
---|
105 | for( l = ic->deny; l; l = l->next ) |
---|
106 | g_free( l->data ); |
---|
107 | g_slist_free( ic->deny ); |
---|
108 | |
---|
109 | msn_connections = g_slist_remove( msn_connections, ic ); |
---|
110 | } |
---|
111 | |
---|
112 | static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
113 | { |
---|
114 | struct msn_switchboard *sb; |
---|
115 | struct msn_data *md = ic->proto_data; |
---|
116 | |
---|
117 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
118 | { |
---|
119 | return( msn_sb_sendmessage( sb, message ) ); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | struct msn_message *m; |
---|
124 | char buf[1024]; |
---|
125 | |
---|
126 | /* Create a message. We have to arrange a usable switchboard, and send the message later. */ |
---|
127 | m = g_new0( struct msn_message, 1 ); |
---|
128 | m->who = g_strdup( who ); |
---|
129 | m->text = g_strdup( message ); |
---|
130 | |
---|
131 | /* FIXME: *CHECK* the reliability of using spare sb's! */ |
---|
132 | if( ( sb = msn_sb_spare( ic ) ) ) |
---|
133 | { |
---|
134 | debug( "Trying to use a spare switchboard to message %s", who ); |
---|
135 | |
---|
136 | sb->who = g_strdup( who ); |
---|
137 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
138 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
139 | { |
---|
140 | /* He/She should join the switchboard soon, let's queue the message. */ |
---|
141 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
142 | return( 1 ); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | debug( "Creating a new switchboard to message %s", who ); |
---|
147 | |
---|
148 | /* If we reach this line, there was no spare switchboard, so let's make one. */ |
---|
149 | g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); |
---|
150 | if( !msn_write( ic, buf, strlen( buf ) ) ) |
---|
151 | { |
---|
152 | g_free( m->who ); |
---|
153 | g_free( m->text ); |
---|
154 | g_free( m ); |
---|
155 | |
---|
156 | return( 0 ); |
---|
157 | } |
---|
158 | |
---|
159 | /* And queue the message to md. We'll pick it up when the switchboard comes up. */ |
---|
160 | md->msgq = g_slist_append( md->msgq, m ); |
---|
161 | |
---|
162 | /* FIXME: If the switchboard creation fails, the message will not be sent. */ |
---|
163 | |
---|
164 | return( 1 ); |
---|
165 | } |
---|
166 | |
---|
167 | return( 0 ); |
---|
168 | } |
---|
169 | |
---|
170 | static GList *msn_away_states( struct im_connection *ic ) |
---|
171 | { |
---|
172 | static GList *l = NULL; |
---|
173 | int i; |
---|
174 | |
---|
175 | if( l == NULL ) |
---|
176 | for( i = 0; msn_away_state_list[i].number > -1; i ++ ) |
---|
177 | l = g_list_append( l, (void*) msn_away_state_list[i].name ); |
---|
178 | |
---|
179 | return l; |
---|
180 | } |
---|
181 | |
---|
182 | static void msn_set_away( struct im_connection *ic, char *state, char *message ) |
---|
183 | { |
---|
184 | char buf[1024]; |
---|
185 | struct msn_data *md = ic->proto_data; |
---|
186 | const struct msn_away_state *st; |
---|
187 | |
---|
188 | if( strcmp( state, GAIM_AWAY_CUSTOM ) == 0 ) |
---|
189 | st = msn_away_state_by_name( "Away" ); |
---|
190 | else |
---|
191 | st = msn_away_state_by_name( state ); |
---|
192 | |
---|
193 | if( !st ) st = msn_away_state_list; |
---|
194 | md->away_state = st; |
---|
195 | |
---|
196 | g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, st->code ); |
---|
197 | msn_write( ic, buf, strlen( buf ) ); |
---|
198 | } |
---|
199 | |
---|
200 | static void msn_set_my_name( struct im_connection *ic, char *info ) |
---|
201 | { |
---|
202 | msn_set_display_name( set_find( &ic->acc->set, "display_name" ), info ); |
---|
203 | } |
---|
204 | |
---|
205 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
206 | { |
---|
207 | /* Just make an URL and let the user fetch the info */ |
---|
208 | imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who ); |
---|
209 | } |
---|
210 | |
---|
211 | static void msn_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
212 | { |
---|
213 | msn_buddy_list_add( ic, "FL", who, who ); |
---|
214 | } |
---|
215 | |
---|
216 | static void msn_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
217 | { |
---|
218 | msn_buddy_list_remove( ic, "FL", who ); |
---|
219 | } |
---|
220 | |
---|
221 | static void msn_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
222 | { |
---|
223 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
224 | |
---|
225 | if( sb ) |
---|
226 | msn_sb_sendmessage( sb, message ); |
---|
227 | /* FIXME: Error handling (although this can't happen unless something's |
---|
228 | already severely broken) disappeared here! */ |
---|
229 | } |
---|
230 | |
---|
231 | static void msn_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
232 | { |
---|
233 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
234 | char buf[1024]; |
---|
235 | |
---|
236 | if( sb ) |
---|
237 | { |
---|
238 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
239 | msn_sb_write( sb, buf, strlen( buf ) ); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | static void msn_chat_leave( struct groupchat *c ) |
---|
244 | { |
---|
245 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
246 | |
---|
247 | if( sb ) |
---|
248 | msn_sb_write( sb, "OUT\r\n", 5 ); |
---|
249 | } |
---|
250 | |
---|
251 | static struct groupchat *msn_chat_with( struct im_connection *ic, char *who ) |
---|
252 | { |
---|
253 | struct msn_switchboard *sb; |
---|
254 | struct msn_data *md = ic->proto_data; |
---|
255 | char buf[1024]; |
---|
256 | |
---|
257 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
258 | { |
---|
259 | debug( "Converting existing switchboard to %s to a groupchat", who ); |
---|
260 | return msn_sb_to_chat( sb ); |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | struct msn_message *m; |
---|
265 | |
---|
266 | if( ( sb = msn_sb_spare( ic ) ) ) |
---|
267 | { |
---|
268 | debug( "Trying to reuse an existing switchboard as a groupchat with %s", who ); |
---|
269 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
270 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
271 | return msn_sb_to_chat( sb ); |
---|
272 | } |
---|
273 | |
---|
274 | /* If the stuff above failed for some reason: */ |
---|
275 | debug( "Creating a new switchboard to groupchat with %s", who ); |
---|
276 | |
---|
277 | /* Request a new switchboard. */ |
---|
278 | g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); |
---|
279 | if( !msn_write( ic, buf, strlen( buf ) ) ) |
---|
280 | return( 0 ); |
---|
281 | |
---|
282 | /* Create a magic message. This is quite hackish, but who cares? :-P */ |
---|
283 | m = g_new0( struct msn_message, 1 ); |
---|
284 | m->who = g_strdup( who ); |
---|
285 | m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE ); |
---|
286 | |
---|
287 | /* Queue the magic message and cross your fingers. */ |
---|
288 | md->msgq = g_slist_append( md->msgq, m ); |
---|
289 | |
---|
290 | /* FIXME: Can I try to return something here already? */ |
---|
291 | return NULL; |
---|
292 | } |
---|
293 | |
---|
294 | return NULL; |
---|
295 | } |
---|
296 | |
---|
297 | static void msn_keepalive( struct im_connection *ic ) |
---|
298 | { |
---|
299 | msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) ); |
---|
300 | } |
---|
301 | |
---|
302 | static void msn_add_permit( struct im_connection *ic, char *who ) |
---|
303 | { |
---|
304 | msn_buddy_list_add( ic, "AL", who, who ); |
---|
305 | } |
---|
306 | |
---|
307 | static void msn_rem_permit( struct im_connection *ic, char *who ) |
---|
308 | { |
---|
309 | msn_buddy_list_remove( ic, "AL", who ); |
---|
310 | } |
---|
311 | |
---|
312 | static void msn_add_deny( struct im_connection *ic, char *who ) |
---|
313 | { |
---|
314 | struct msn_switchboard *sb; |
---|
315 | |
---|
316 | msn_buddy_list_add( ic, "BL", who, who ); |
---|
317 | |
---|
318 | /* If there's still a conversation with this person, close it. */ |
---|
319 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
320 | { |
---|
321 | msn_sb_destroy( sb ); |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | static void msn_rem_deny( struct im_connection *ic, char *who ) |
---|
326 | { |
---|
327 | msn_buddy_list_remove( ic, "BL", who ); |
---|
328 | } |
---|
329 | |
---|
330 | static int msn_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
331 | { |
---|
332 | if( typing & OPT_TYPING ) |
---|
333 | return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) ); |
---|
334 | else |
---|
335 | return( 1 ); |
---|
336 | } |
---|
337 | |
---|
338 | static char *msn_set_display_name( set_t *set, char *value ) |
---|
339 | { |
---|
340 | account_t *acc = set->data; |
---|
341 | struct im_connection *ic = acc->ic; |
---|
342 | struct msn_data *md; |
---|
343 | char buf[1024], *fn; |
---|
344 | |
---|
345 | /* Double-check. */ |
---|
346 | if( ic == NULL ) |
---|
347 | return NULL; |
---|
348 | |
---|
349 | md = ic->proto_data; |
---|
350 | |
---|
351 | if( strlen( value ) > 129 ) |
---|
352 | { |
---|
353 | imcb_log( ic, "Maximum name length exceeded" ); |
---|
354 | return NULL; |
---|
355 | } |
---|
356 | |
---|
357 | fn = msn_http_encode( value ); |
---|
358 | |
---|
359 | g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, ic->acc->user, fn ); |
---|
360 | msn_write( ic, buf, strlen( buf ) ); |
---|
361 | g_free( fn ); |
---|
362 | |
---|
363 | /* Returning NULL would be better, because the server still has to |
---|
364 | confirm the name change. However, it looks a bit confusing to the |
---|
365 | user. */ |
---|
366 | return value; |
---|
367 | } |
---|
368 | |
---|
369 | void msn_initmodule() |
---|
370 | { |
---|
371 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
372 | |
---|
373 | ret->name = "msn"; |
---|
374 | ret->mms = 1409; /* this guess taken from libotr UPGRADING file */ |
---|
375 | ret->login = msn_login; |
---|
376 | ret->init = msn_init; |
---|
377 | ret->logout = msn_logout; |
---|
378 | ret->buddy_msg = msn_buddy_msg; |
---|
379 | ret->away_states = msn_away_states; |
---|
380 | ret->set_away = msn_set_away; |
---|
381 | ret->get_info = msn_get_info; |
---|
382 | ret->set_my_name = msn_set_my_name; |
---|
383 | ret->add_buddy = msn_add_buddy; |
---|
384 | ret->remove_buddy = msn_remove_buddy; |
---|
385 | ret->chat_msg = msn_chat_msg; |
---|
386 | ret->chat_invite = msn_chat_invite; |
---|
387 | ret->chat_leave = msn_chat_leave; |
---|
388 | ret->chat_with = msn_chat_with; |
---|
389 | ret->keepalive = msn_keepalive; |
---|
390 | ret->add_permit = msn_add_permit; |
---|
391 | ret->rem_permit = msn_rem_permit; |
---|
392 | ret->add_deny = msn_add_deny; |
---|
393 | ret->rem_deny = msn_rem_deny; |
---|
394 | ret->send_typing = msn_send_typing; |
---|
395 | ret->handle_cmp = g_strcasecmp; |
---|
396 | |
---|
397 | register_protocol(ret); |
---|
398 | } |
---|