source: protocols/msn/msn.c @ 12767e3

Last change on this file since 12767e3 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
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[4e4af1b]4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
[b7d3cc34]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
[c6ca3ee]29int msn_chat_id;
30GSList *msn_connections;
31GSList *msn_switchboards;
32
[e3413cc]33static char *set_eval_display_name( set_t *set, char *value );
[911f2eb]34
[0da65d5]35static void msn_init( account_t *acc )
[911f2eb]36{
[e3413cc]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 );
[efbc154]40        set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc );
[12767e3]41       
42        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE;
[911f2eb]43}
44
[0a3c243]45static void msn_login( account_t *acc )
[b7d3cc34]46{
[84b045d]47        struct im_connection *ic = imcb_new( acc );
[b7d3cc34]48        struct msn_data *md = g_new0( struct msn_data, 1 );
49       
[0da65d5]50        ic->proto_data = md;
[b7d3cc34]51        md->fd = -1;
52       
[0a3c243]53        if( strchr( acc->user, '@' ) == NULL )
[b7d3cc34]54        {
[84b045d]55                imcb_error( ic, "Invalid account name" );
[c2fb3809]56                imc_logout( ic, FALSE );
[b7d3cc34]57                return;
58        }
59       
[84b045d]60        imcb_log( ic, "Connecting" );
[2a29eac]61       
[0da65d5]62        md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic );
[b7d3cc34]63        if( md->fd < 0 )
64        {
[84b045d]65                imcb_error( ic, "Could not connect to server" );
[c2fb3809]66                imc_logout( ic, TRUE );
[2a29eac]67                return;
[b7d3cc34]68        }
[2a29eac]69       
[0da65d5]70        md->ic = ic;
[2a29eac]71        md->away_state = msn_away_state_list;
[ca7de3a]72        md->domaintree = g_tree_new( msn_domaintree_cmp );
[2a29eac]73       
[0da65d5]74        msn_connections = g_slist_append( msn_connections, ic );
[b7d3cc34]75}
76
[0da65d5]77static void msn_logout( struct im_connection *ic )
[b7d3cc34]78{
[0da65d5]79        struct msn_data *md = ic->proto_data;
[b7d3cc34]80        GSList *l;
81       
[5a5c926]82        if( md )
[b7d3cc34]83        {
[17a6ee9]84                /** Disabling MSN ft support for now.
[a2b99ec]85                while( md->filetransfers ) {
[1c3008a]86                        imcb_file_canceled( md->filetransfers->data, "Closing connection" );
[a2b99ec]87                }
[17a6ee9]88                */
[a2b99ec]89               
[5a5c926]90                if( md->fd >= 0 )
91                        closesocket( md->fd );
[b7d3cc34]92               
[5a5c926]93                if( md->handler )
[b7d3cc34]94                {
[5a5c926]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 );
[1053836]102               
[46dca11]103                msn_msgq_purge( ic, &md->msgq );
[5a5c926]104               
[bf25fa3]105                while( md->groupcount > 0 )
106                        g_free( md->grouplist[--md->groupcount] );
[59f5c42a]107                g_free( md->grouplist );
[523fb23]108                g_free( md->tokens[0] );
109                g_free( md->tokens[1] );
[91d6e91]110                g_free( md->tokens[2] );
[ffb6dea]111                g_free( md->lock_key );
[59f5c42a]112               
[ca7de3a]113                g_tree_destroy( md->domaintree );
114                md->domaintree = NULL;
115               
[70ac477]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               
[5a5c926]125                g_free( md );
[b7d3cc34]126        }
127       
[0da65d5]128        for( l = ic->permit; l; l = l->next )
[b7d3cc34]129                g_free( l->data );
[0da65d5]130        g_slist_free( ic->permit );
[b7d3cc34]131       
[0da65d5]132        for( l = ic->deny; l; l = l->next )
[b7d3cc34]133                g_free( l->data );
[0da65d5]134        g_slist_free( ic->deny );
[b7d3cc34]135       
[0da65d5]136        msn_connections = g_slist_remove( msn_connections, ic );
[b7d3cc34]137}
138
[f6c963b]139static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
[b7d3cc34]140{
141        struct msn_switchboard *sb;
142       
[70ac477]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
[0da65d5]151        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]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               
[a830512]164                return msn_sb_write_msg( ic, m );
[b7d3cc34]165        }
166       
167        return( 0 );
168}
169
[0da65d5]170static GList *msn_away_states( struct im_connection *ic )
[b7d3cc34]171{
[5e202b0]172        static GList *l = NULL;
[b7d3cc34]173        int i;
174       
[5e202b0]175        if( l == NULL )
[b051d39]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 );
[b7d3cc34]179       
[5e202b0]180        return l;
[b7d3cc34]181}
182
[0da65d5]183static void msn_set_away( struct im_connection *ic, char *state, char *message )
[b7d3cc34]184{
185        char buf[1024];
[12767e3]186        char *uux;
[0da65d5]187        struct msn_data *md = ic->proto_data;
[b7d3cc34]188       
[daae10f]189        if( state == NULL )
[b051d39]190                md->away_state = msn_away_state_list;
[daae10f]191        else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL )
192                md->away_state = msn_away_state_list + 1;
[b7d3cc34]193       
[b051d39]194        g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, md->away_state->code );
[12767e3]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;
[b7d3cc34]203}
204
[0da65d5]205static void msn_set_my_name( struct im_connection *ic, char *info )
[b7d3cc34]206{
[e3413cc]207        msn_set_display_name( ic, info );
[b7d3cc34]208}
209
[0da65d5]210static void msn_get_info(struct im_connection *ic, char *who) 
[b7d3cc34]211{
212        /* Just make an URL and let the user fetch the info */
[84b045d]213        imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who );
[b7d3cc34]214}
215
[0da65d5]216static void msn_add_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]217{
[8b01217]218        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
219       
[6acc033]220        msn_buddy_list_add( ic, "FL", who, who, group );
[8b01217]221        if( bu && bu->group )
222                msn_buddy_list_remove( ic, "FL", who, bu->group->name );
[b7d3cc34]223}
224
[0da65d5]225static void msn_remove_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]226{
[8b01217]227        msn_buddy_list_remove( ic, "FL", who, NULL );
[b7d3cc34]228}
229
[f6c963b]230static void msn_chat_msg( struct groupchat *c, char *message, int flags )
[b7d3cc34]231{
[fa29d093]232        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]233       
234        if( sb )
[0da65d5]235                msn_sb_sendmessage( sb, message );
236        /* FIXME: Error handling (although this can't happen unless something's
237           already severely broken) disappeared here! */
[b7d3cc34]238}
239
[c058ff9]240static void msn_chat_invite( struct groupchat *c, char *who, char *message )
[b7d3cc34]241{
[fa29d093]242        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]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
[0da65d5]252static void msn_chat_leave( struct groupchat *c )
[b7d3cc34]253{
[fa29d093]254        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]255       
256        if( sb )
257                msn_sb_write( sb, "OUT\r\n", 5 );
258}
259
[0da65d5]260static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )
[b7d3cc34]261{
262        struct msn_switchboard *sb;
[36577aa]263        struct groupchat *c = imcb_chat_new( ic, who );
[b7d3cc34]264       
[0da65d5]265        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]266        {
267                debug( "Converting existing switchboard to %s to a groupchat", who );
[fa29d093]268                return msn_sb_to_chat( sb );
[b7d3cc34]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               
[a830512]279                msn_sb_write_msg( ic, m );
280
[36577aa]281                return c;
[b7d3cc34]282        }
283}
284
[0da65d5]285static void msn_keepalive( struct im_connection *ic )
[b7d3cc34]286{
[0da65d5]287        msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) );
[b7d3cc34]288}
289
[0da65d5]290static void msn_add_permit( struct im_connection *ic, char *who )
[b7d3cc34]291{
[6acc033]292        msn_buddy_list_add( ic, "AL", who, who, NULL );
[b7d3cc34]293}
294
[0da65d5]295static void msn_rem_permit( struct im_connection *ic, char *who )
[b7d3cc34]296{
[8b01217]297        msn_buddy_list_remove( ic, "AL", who, NULL );
[b7d3cc34]298}
299
[0da65d5]300static void msn_add_deny( struct im_connection *ic, char *who )
[b7d3cc34]301{
302        struct msn_switchboard *sb;
303       
[6acc033]304        msn_buddy_list_add( ic, "BL", who, who, NULL );
[b7d3cc34]305       
306        /* If there's still a conversation with this person, close it. */
[0da65d5]307        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]308        {
309                msn_sb_destroy( sb );
310        }
311}
312
[0da65d5]313static void msn_rem_deny( struct im_connection *ic, char *who )
[b7d3cc34]314{
[8b01217]315        msn_buddy_list_remove( ic, "BL", who, NULL );
[b7d3cc34]316}
317
[0da65d5]318static int msn_send_typing( struct im_connection *ic, char *who, int typing )
[b7d3cc34]319{
[4255320]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 )
[f6c963b]325                return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) );
[b7d3cc34]326        else
[4255320]327                return 1;
[b7d3cc34]328}
329
[e3413cc]330static char *set_eval_display_name( set_t *set, char *value )
[911f2eb]331{
332        account_t *acc = set->data;
[0da65d5]333        struct im_connection *ic = acc->ic;
[911f2eb]334       
[e3413cc]335        /* Allow any name if we're offline. */
[0da65d5]336        if( ic == NULL )
[e3413cc]337                return value;
[911f2eb]338       
339        if( strlen( value ) > 129 )
340        {
[84b045d]341                imcb_log( ic, "Maximum name length exceeded" );
[911f2eb]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. */
[e3413cc]348        return msn_set_display_name( ic, value ) ? value : NULL;
[911f2eb]349}
350
[7f34ce2]351static void msn_buddy_data_add( bee_user_t *bu )
352{
[ca7de3a]353        struct msn_data *md = bu->ic->proto_data;
[7f34ce2]354        bu->data = g_new0( struct msn_buddy_data, 1 );
[ca7de3a]355        g_tree_insert( md->domaintree, bu->handle, bu );
[7f34ce2]356}
357
358static void msn_buddy_data_free( bee_user_t *bu )
359{
[ca7de3a]360        struct msn_data *md = bu->ic->proto_data;
361        g_tree_remove( md->domaintree, bu->handle );
[7f34ce2]362        g_free( bu->data );
363}
364
[0da65d5]365void msn_initmodule()
[b7d3cc34]366{
[7b23afd]367        struct prpl *ret = g_new0(struct prpl, 1);
[911f2eb]368       
[7b23afd]369        ret->name = "msn";
[b7d3cc34]370        ret->login = msn_login;
[0da65d5]371        ret->init = msn_init;
372        ret->logout = msn_logout;
[f6c963b]373        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]374        ret->away_states = msn_away_states;
375        ret->set_away = msn_set_away;
376        ret->get_info = msn_get_info;
[0da65d5]377        ret->set_my_name = msn_set_my_name;
[b7d3cc34]378        ret->add_buddy = msn_add_buddy;
379        ret->remove_buddy = msn_remove_buddy;
[f6c963b]380        ret->chat_msg = msn_chat_msg;
[b7d3cc34]381        ret->chat_invite = msn_chat_invite;
382        ret->chat_leave = msn_chat_leave;
[0da65d5]383        ret->chat_with = msn_chat_with;
[b7d3cc34]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;
[5b52a48]390        ret->handle_cmp = g_strcasecmp;
[7f34ce2]391        ret->buddy_data_add = msn_buddy_data_add;
392        ret->buddy_data_free = msn_buddy_data_free;
393       
[17a6ee9]394        //ret->transfer_request = msn_ftp_transfer_request;
[b7d3cc34]395
[7b23afd]396        register_protocol(ret);
[b7d3cc34]397}
Note: See TracBrowser for help on using the repository browser.