source: protocols/msn/msn.c @ d93c0eb9

Last change on this file since d93c0eb9 was 12767e3, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-14T11:30:40Z

Status/Away messages.

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