diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2010-09-03 22:24:58 +0100 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2010-09-03 22:24:58 +0100 |
commit | bae06178bbe3863b39ec307c34d2781a53472272 (patch) | |
tree | 53f8cd8b8d2bc0b2da26356fa604a37b4368971e | |
parent | 64768d4ec0c3ad473573c3f3c34871e0081b4e59 (diff) |
Rearrange things a bit to support multiple NS connections. This is apparently
needed for refreshing auth. tokens.
-rw-r--r-- | protocols/msn/msn.c | 27 | ||||
-rw-r--r-- | protocols/msn/msn.h | 41 | ||||
-rw-r--r-- | protocols/msn/msn_util.c | 4 | ||||
-rw-r--r-- | protocols/msn/ns.c | 114 | ||||
-rw-r--r-- | protocols/msn/sb.c | 12 |
5 files changed, 105 insertions, 93 deletions
diff --git a/protocols/msn/msn.c b/protocols/msn/msn.c index f37b802e..c5c05087 100644 --- a/protocols/msn/msn.c +++ b/protocols/msn/msn.c @@ -52,7 +52,6 @@ static void msn_login( account_t *acc ) struct msn_data *md = g_new0( struct msn_data, 1 ); ic->proto_data = md; - md->fd = -1; if( strchr( acc->user, '@' ) == NULL ) { @@ -61,21 +60,14 @@ static void msn_login( account_t *acc ) return; } - imcb_log( ic, "Connecting" ); - - md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic ); - if( md->fd < 0 ) - { - imcb_error( ic, "Could not connect to server" ); - imc_logout( ic, TRUE ); - return; - } - md->ic = ic; md->away_state = msn_away_state_list; md->domaintree = g_tree_new( msn_domaintree_cmp ); - msn_connections = g_slist_append( msn_connections, ic ); + msn_connections = g_slist_prepend( msn_connections, ic ); + + imcb_log( ic, "Connecting" ); + msn_ns_connect( ic, md->ns, MSN_NS_HOST, MSN_NS_PORT ); } static void msn_logout( struct im_connection *ic ) @@ -92,15 +84,8 @@ static void msn_logout( struct im_connection *ic ) } */ - if( md->fd >= 0 ) - closesocket( md->fd ); - - if( md->handler ) - { - if( md->handler->rxq ) g_free( md->handler->rxq ); - if( md->handler->cmd_text ) g_free( md->handler->cmd_text ); - g_free( md->handler ); - } + msn_ns_close( md->ns ); + msn_ns_close( md->auth ); while( md->switchboards ) msn_sb_destroy( md->switchboards->data ); diff --git a/protocols/msn/msn.h b/protocols/msn/msn.h index dae115ef..5a195348 100644 --- a/protocols/msn/msn.h +++ b/protocols/msn/msn.h @@ -43,6 +43,9 @@ #define MSNP11_PROD_ID "PROD0101{0RM?UBW" */ +#define MSN_NS_HOST "messenger.hotmail.com" +#define MSN_NS_PORT 1863 + /* Some other version. #define MSNP11_PROD_KEY "O4BG@C7BWLYQX?5G" #define MSNP11_PROD_ID "PROD01065C%ZFN6F" @@ -79,12 +82,27 @@ typedef enum MSN_DONE_ADL = 4, } msn_flags_t; +struct msn_handler_data +{ + int fd, inpa; + int rxlen; + char *rxq; + + int msglen; + char *cmd_text; + + /* Either ic or sb */ + gpointer data; + + int (*exec_command) ( struct msn_handler_data *handler, char **cmd, int count ); + int (*exec_message) ( struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int count ); +}; + struct msn_data { struct im_connection *ic; - int fd; - struct msn_handler_data *handler; + struct msn_handler_data ns[1], auth[1]; msn_flags_t flags; int trId; @@ -110,6 +128,7 @@ struct msn_switchboard { struct im_connection *ic; + /* The following two are also in the handler. TODO: Clean up. */ int fd; gint inp; struct msn_handler_data *handler; @@ -151,21 +170,6 @@ struct msn_groupadd char *group; }; -struct msn_handler_data -{ - int fd; - int rxlen; - char *rxq; - - int msglen; - char *cmd_text; - - gpointer data; - - int (*exec_command) ( gpointer data, char **cmd, int count ); - int (*exec_message) ( gpointer data, char *msg, int msglen, char **cmd, int count ); -}; - typedef enum { MSN_BUDDY_FL = 1, /* Warning: FL,AL,BL *must* be 1,2,4. */ @@ -208,7 +212,8 @@ extern GSList *msn_switchboards; /* ns.c */ int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... ); -gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ); +gboolean msn_ns_connect( struct im_connection *ic, struct msn_handler_data *handler, const char *host, int port ); +void msn_ns_close( struct msn_handler_data *handler ); void msn_auth_got_passport_token( struct im_connection *ic, const char *token, const char *error ); void msn_auth_got_contact_list( struct im_connection *ic ); int msn_ns_finish_login( struct im_connection *ic ); diff --git a/protocols/msn/msn_util.c b/protocols/msn/msn_util.c index 6a16ba17..7fa68915 100644 --- a/protocols/msn/msn_util.c +++ b/protocols/msn/msn_util.c @@ -350,7 +350,7 @@ int msn_handler( struct msn_handler_data *h ) cmd_text = g_strndup( h->rxq, i ); cmd = msn_linesplit( cmd_text ); for( count = 0; cmd[count]; count ++ ); - st = h->exec_command( h->data, cmd, count ); + st = h->exec_command( h, cmd, count ); g_free( cmd_text ); /* If the connection broke, don't continue. We don't even exist anymore. */ @@ -385,7 +385,7 @@ int msn_handler( struct msn_handler_data *h ) cmd = msn_linesplit( h->cmd_text ); for( count = 0; cmd[count]; count ++ ); - st = h->exec_message( h->data, msg, h->msglen, cmd, count ); + st = h->exec_message( h, msg, h->msglen, cmd, count ); g_free( msg ); g_free( h->cmd_text ); h->cmd_text = NULL; diff --git a/protocols/msn/ns.c b/protocols/msn/ns.c index 23a1951d..6260e300 100644 --- a/protocols/msn/ns.c +++ b/protocols/msn/ns.c @@ -30,9 +30,10 @@ #include "soap.h" #include "xmltree.h" +static gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ); static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ); -static int msn_ns_command( gpointer data, char **cmd, int num_parts ); -static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ); +static int msn_ns_command( struct msn_handler_data *handler, char **cmd, int num_parts ); +static int msn_ns_message( struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts ); static void msn_ns_send_adl_start( struct im_connection *ic ); static void msn_ns_send_adl( struct im_connection *ic ); @@ -50,7 +51,7 @@ int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... ) va_end( params ); if( fd < 0 ) - fd = md->fd; + fd = md->ns->fd; if( getenv( "BITLBEE_DEBUG" ) ) fprintf( stderr, "->NS%d:%s", fd, out ); @@ -68,56 +69,79 @@ int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... ) return 1; } -gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) +gboolean msn_ns_connect( struct im_connection *ic, struct msn_handler_data *handler, const char *host, int port ) { - struct im_connection *ic = data; - struct msn_data *md; - - if( !g_slist_find( msn_connections, ic ) ) - return FALSE; + if( handler->fd >= 0 ) + closesocket( handler->fd ); - if( source == -1 ) + handler->exec_command = msn_ns_command; + handler->exec_message = msn_ns_message; + handler->data = ic; + handler->fd = proxy_connect( host, port, msn_ns_connected, handler ); + if( handler->fd < 0 ) { imcb_error( ic, "Could not connect to server" ); imc_logout( ic, TRUE ); return FALSE; } + return TRUE; +} + +static gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) +{ + struct msn_handler_data *handler = data; + struct im_connection *ic = handler->data; + struct msn_data *md; + + if( !g_slist_find( msn_connections, ic ) ) + return FALSE; + md = ic->proto_data; - if( !md->handler ) - { - md->handler = g_new0( struct msn_handler_data, 1 ); - md->handler->data = ic; - md->handler->exec_command = msn_ns_command; - md->handler->exec_message = msn_ns_message; - } - else + if( source == -1 ) { - if( md->handler->rxq ) - g_free( md->handler->rxq ); - - md->handler->rxlen = 0; + imcb_error( ic, "Could not connect to server" ); + imc_logout( ic, TRUE ); + return FALSE; } - md->handler->fd = md->fd; - md->handler->rxq = g_new0( char, 1 ); + g_free( handler->rxq ); + handler->rxlen = 0; + handler->rxq = g_new0( char, 1 ); if( msn_ns_write( ic, -1, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER ) ) { - ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic ); + handler->inpa = b_input_add( handler->fd, B_EV_IO_READ, msn_ns_callback, handler ); imcb_log( ic, "Connected to server, waiting for reply" ); } return FALSE; } +void msn_ns_close( struct msn_handler_data *handler ) +{ + if( handler->fd >= 0 ) + { + closesocket( handler->fd ); + b_event_remove( handler->inpa ); + } + + handler->fd = handler->inpa = -1; + g_free( handler->rxq ); + g_free( handler->cmd_text ); + + handler->rxlen = 0; + handler->rxq = NULL; + handler->cmd_text = NULL; +} + static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ) { - struct im_connection *ic = data; - struct msn_data *md = ic->proto_data; + struct msn_handler_data *handler = data; + struct im_connection *ic = handler->data; - if( msn_handler( md->handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */ + if( msn_handler( handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */ { imcb_error( ic, "Error while reading from server" ); imc_logout( ic, TRUE ); @@ -128,9 +152,9 @@ static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition c return TRUE; } -static int msn_ns_command( gpointer data, char **cmd, int num_parts ) +static int msn_ns_command( struct msn_handler_data *handler, char **cmd, int num_parts ) { - struct im_connection *ic = data; + struct im_connection *ic = handler->data; struct msn_data *md = ic->proto_data; if( num_parts == 0 ) @@ -163,9 +187,8 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) if( num_parts >= 6 && strcmp( cmd[2], "NS" ) == 0 ) { - b_event_remove( ic->inpa ); - ic->inpa = 0; - closesocket( md->fd ); + b_event_remove( handler->inpa ); + handler->inpa = -1; server = strchr( cmd[3], ':' ); if( !server ) @@ -179,8 +202,7 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) server = cmd[3]; imcb_log( ic, "Transferring to other server" ); - - md->fd = proxy_connect( server, port, msn_ns_connected, ic ); + return msn_ns_connect( ic, handler, server, port ); } else if( num_parts >= 6 && strcmp( cmd[2], "SB" ) == 0 ) { @@ -272,9 +294,9 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) return( 0 ); } - md->handler->msglen = atoi( cmd[3] ); + handler->msglen = atoi( cmd[3] ); - if( md->handler->msglen <= 0 ) + if( handler->msglen <= 0 ) { imcb_error( ic, "Syntax error" ); imc_logout( ic, TRUE ); @@ -295,7 +317,7 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) } else if( num_parts >= 3 ) { - md->handler->msglen = atoi( cmd[2] ); + handler->msglen = atoi( cmd[2] ); } } else if( strcmp( cmd[0], "PRP" ) == 0 ) @@ -459,9 +481,9 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) { imcb_error( ic, "Received IPG command, we don't handle them yet." ); - md->handler->msglen = atoi( cmd[1] ); + handler->msglen = atoi( cmd[1] ); - if( md->handler->msglen <= 0 ) + if( handler->msglen <= 0 ) { imcb_error( ic, "Syntax error" ); imc_logout( ic, TRUE ); @@ -518,20 +540,20 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) { /* Coming up is cmd[2] bytes of stuff we're supposed to censore. Meh. */ - md->handler->msglen = atoi( cmd[2] ); + handler->msglen = atoi( cmd[2] ); } else if( strcmp( cmd[0], "UBX" ) == 0 ) { /* Status message. */ if( num_parts >= 4 ) - md->handler->msglen = atoi( cmd[3] ); + handler->msglen = atoi( cmd[3] ); } else if( strcmp( cmd[0], "NOT" ) == 0 ) { /* Some kind of notification, poorly documented but apparently used to announce address book changes. */ if( num_parts >= 2 ) - md->handler->msglen = atoi( cmd[1] ); + handler->msglen = atoi( cmd[1] ); } else if( isdigit( cmd[0][0] ) ) { @@ -548,7 +570,7 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) /* Oh yes, errors can have payloads too now. Discard them for now. */ if( num_parts >= 3 ) - md->handler->msglen = atoi( cmd[2] ); + handler->msglen = atoi( cmd[2] ); } else { @@ -558,9 +580,9 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) return( 1 ); } -static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) +static int msn_ns_message( struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts ) { - struct im_connection *ic = data; + struct im_connection *ic = handler->data; char *body; int blen = 0; diff --git a/protocols/msn/sb.c b/protocols/msn/sb.c index d10d7c19..898fb34f 100644 --- a/protocols/msn/sb.c +++ b/protocols/msn/sb.c @@ -31,8 +31,8 @@ #include "invitation.h" static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond ); -static int msn_sb_command( gpointer data, char **cmd, int num_parts ); -static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ); +static int msn_sb_command( struct msn_handler_data *handler, char **cmd, int num_parts ); +static int msn_sb_message( struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts ); int msn_sb_write( struct msn_switchboard *sb, const char *fmt, ... ) { @@ -391,9 +391,9 @@ static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition c return FALSE; } -static int msn_sb_command( gpointer data, char **cmd, int num_parts ) +static int msn_sb_command( struct msn_handler_data *handler, char **cmd, int num_parts ) { - struct msn_switchboard *sb = data; + struct msn_switchboard *sb = handler->data; struct im_connection *ic = sb->ic; if( !num_parts ) @@ -664,9 +664,9 @@ static int msn_sb_command( gpointer data, char **cmd, int num_parts ) return( 1 ); } -static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) +static int msn_sb_message( struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts ) { - struct msn_switchboard *sb = data; + struct msn_switchboard *sb = handler->data; struct im_connection *ic = sb->ic; char *body; int blen = 0; |