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