diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2010-09-02 10:15:44 +0100 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2010-09-02 10:15:44 +0100 |
commit | 64768d4ec0c3ad473573c3f3c34871e0081b4e59 (patch) | |
tree | 5cde705bcd282cec322da9e82c9b0e26f16387dc | |
parent | 02bb9db2edd535036e030e004a58ed1459c15bb8 (diff) |
Replace msn*write functions with saner versions that accept format strings.
Also preparing for additional temporary NS connections (auth token renewal).
-rw-r--r-- | protocols/msn/msn.c | 14 | ||||
-rw-r--r-- | protocols/msn/msn.h | 4 | ||||
-rw-r--r-- | protocols/msn/msn_util.c | 43 | ||||
-rw-r--r-- | protocols/msn/ns.c | 75 | ||||
-rw-r--r-- | protocols/msn/sb.c | 57 |
5 files changed, 87 insertions, 106 deletions
diff --git a/protocols/msn/msn.c b/protocols/msn/msn.c index 4b79cc26..f37b802e 100644 --- a/protocols/msn/msn.c +++ b/protocols/msn/msn.c @@ -153,8 +153,7 @@ static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, in #ifdef DEBUG if( strcmp( who, "raw" ) == 0 ) { - msn_write( ic, message, strlen( message ) ); - msn_write( ic, "\r\n", 2 ); + msn_ns_write( ic, -1, "%s\r\n", message ); } else #endif @@ -192,7 +191,6 @@ static GList *msn_away_states( struct im_connection *ic ) static void msn_set_away( struct im_connection *ic, char *state, char *message ) { - char buf[1024]; char *uux; struct msn_data *md = ic->proto_data; @@ -201,15 +199,13 @@ static void msn_set_away( struct im_connection *ic, char *state, char *message ) else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL ) md->away_state = msn_away_state_list + 1; - g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, md->away_state->code ); - if( !msn_write( ic, buf, strlen( buf ) ) ) + if( !msn_ns_write( ic, -1, "CHG %d %s\r\n", ++md->trId, md->away_state->code ) ) return; uux = g_markup_printf_escaped( "<Data><PSM>%s</PSM><CurrentMedia></CurrentMedia>" "</Data>", message ? message : "" ); - g_snprintf( buf, sizeof( buf ), "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux ); - if( !msn_write( ic, buf, strlen( buf ) ) ) - return; + msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux ); + g_free( uux ); } static void msn_get_info(struct im_connection *ic, char *who) @@ -289,7 +285,7 @@ static struct groupchat *msn_chat_with( struct im_connection *ic, char *who ) static void msn_keepalive( struct im_connection *ic ) { - msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) ); + msn_ns_write( ic, -1, "PNG\r\n" ); } static void msn_add_permit( struct im_connection *ic, char *who ) diff --git a/protocols/msn/msn.h b/protocols/msn/msn.h index 7cb3241c..dae115ef 100644 --- a/protocols/msn/msn.h +++ b/protocols/msn/msn.h @@ -207,13 +207,13 @@ extern GSList *msn_connections; 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 ); 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 ); /* msn_util.c */ -int msn_write( struct im_connection *ic, char *s, int len ); int msn_logged_in( struct im_connection *ic ); int msn_buddy_list_add( struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *realname_, const char *group ); int msn_buddy_list_remove( struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *group ); @@ -235,7 +235,7 @@ const struct msn_away_state *msn_away_state_by_name( char *name ); const struct msn_status_code *msn_status_by_number( int number ); /* sb.c */ -int msn_sb_write( struct msn_switchboard *sb, char *s, int len ); +int msn_sb_write( struct msn_switchboard *sb, const char *fmt, ... ); struct msn_switchboard *msn_sb_create( struct im_connection *ic, char *host, int port, char *key, int session ); struct msn_switchboard *msn_sb_by_handle( struct im_connection *ic, char *handle ); struct msn_switchboard *msn_sb_by_chat( struct groupchat *c ); diff --git a/protocols/msn/msn_util.c b/protocols/msn/msn_util.c index 78f81a41..6a16ba17 100644 --- a/protocols/msn/msn_util.c +++ b/protocols/msn/msn_util.c @@ -29,28 +29,6 @@ #include "soap.h" #include <ctype.h> -int msn_write( struct im_connection *ic, char *s, int len ) -{ - struct msn_data *md = ic->proto_data; - int st; - - if( getenv( "BITLBEE_DEBUG" ) ) - { - write( 2, "->NS:", 5 ); - write( 2, s, len ); - } - - st = write( md->fd, s, len ); - if( st != len ) - { - imcb_error( ic, "Short write() to main server" ); - imc_logout( ic, TRUE ); - return 0; - } - - return 1; -} - int msn_logged_in( struct im_connection *ic ) { imcb_connected( ic ); @@ -75,7 +53,7 @@ static char *adlrml_entry( const char *handle_, msn_buddy_flags_t list ) int msn_buddy_list_add( struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *realname, const char *group ) { struct msn_data *md = ic->proto_data; - char buf[1024], groupid[8]; + char groupid[8]; bee_user_t *bu; struct msn_buddy_data *bd; char *adl; @@ -143,11 +121,11 @@ int msn_buddy_list_add( struct im_connection *ic, msn_buddy_flags_t list, const if( ( adl = adlrml_entry( who, list ) ) ) { - g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n%s", - ++md->trId, strlen( adl ), adl ); + int st = msn_ns_write( ic, -1, "ADL %d %zd\r\n%s", + ++md->trId, strlen( adl ), adl ); g_free( adl ); - return msn_write( ic, buf, strlen( buf ) ); + return st; } return 1; @@ -156,7 +134,7 @@ int msn_buddy_list_add( struct im_connection *ic, msn_buddy_flags_t list, const int msn_buddy_list_remove( struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *group ) { struct msn_data *md = ic->proto_data; - char buf[1024], groupid[8]; + char groupid[8]; bee_user_t *bu; struct msn_buddy_data *bd; char *adl; @@ -188,11 +166,11 @@ int msn_buddy_list_remove( struct im_connection *ic, msn_buddy_flags_t list, con if( ( adl = adlrml_entry( who, list ) ) ) { - g_snprintf( buf, sizeof( buf ), "RML %d %zd\r\n%s", - ++md->trId, strlen( adl ), adl ); + int st = msn_ns_write( ic, -1, "RML %d %zd\r\n%s", + ++md->trId, strlen( adl ), adl ); g_free( adl ); - return msn_write( ic, buf, strlen( buf ) ); + return st; } return 1; @@ -602,14 +580,11 @@ int msn_ns_set_display_name( struct im_connection *ic, const char *value ) { struct msn_data *md = ic->proto_data; char fn[strlen(value)*3+1]; - char buf[512]; strcpy( fn, value ); http_encode( fn ); - g_snprintf( buf, sizeof( buf ), "PRP %d MFN %s\r\n", - ++md->trId, fn ); /* Note: We don't actually know if the server accepted the new name, and won't give proper feedback yet if it doesn't. */ - return msn_write( ic, buf, strlen( buf ) ); + return msn_ns_write( ic, -1, "PRP %d MFN %s\r\n", ++md->trId, fn ); } diff --git a/protocols/msn/ns.c b/protocols/msn/ns.c index fa445aa7..23a1951d 100644 --- a/protocols/msn/ns.c +++ b/protocols/msn/ns.c @@ -37,11 +37,41 @@ static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int static void msn_ns_send_adl_start( struct im_connection *ic ); static void msn_ns_send_adl( struct im_connection *ic ); +int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... ) +{ + struct msn_data *md = ic->proto_data; + va_list params; + char *out; + size_t len; + int st; + + va_start( params, fmt ); + out = g_strdup_vprintf( fmt, params ); + va_end( params ); + + if( fd < 0 ) + fd = md->fd; + + if( getenv( "BITLBEE_DEBUG" ) ) + fprintf( stderr, "->NS%d:%s", fd, out ); + + len = strlen( out ); + st = write( fd, out, len ); + g_free( out ); + if( st != len ) + { + imcb_error( ic, "Short write() to main server" ); + imc_logout( ic, TRUE ); + return 0; + } + + return 1; +} + gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) { struct im_connection *ic = data; struct msn_data *md; - char s[1024]; if( !g_slist_find( msn_connections, ic ) ) return FALSE; @@ -73,8 +103,7 @@ gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) md->handler->fd = md->fd; md->handler->rxq = g_new0( char, 1 ); - g_snprintf( s, sizeof( s ), "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER ); - if( msn_write( ic, s, strlen( s ) ) ) + 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 ); imcb_log( ic, "Connected to server, waiting for reply" ); @@ -103,7 +132,6 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) { struct im_connection *ic = data; struct msn_data *md = ic->proto_data; - char buf[1024]; if( num_parts == 0 ) { @@ -120,15 +148,13 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) return( 0 ); } - g_snprintf( buf, sizeof( buf ), "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n", - ++md->trId, ic->acc->user ); - return( msn_write( ic, buf, strlen( buf ) ) ); + return( msn_ns_write( ic, -1, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n", + ++md->trId, ic->acc->user ) ); } else if( strcmp( cmd[0], "CVR" ) == 0 ) { /* We don't give a damn about the information we just received */ - g_snprintf( buf, sizeof( buf ), "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user ); - return( msn_write( ic, buf, strlen( buf ) ) ); + return msn_ns_write( ic, -1, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user ); } else if( strcmp( cmd[0], "XFR" ) == 0 ) { @@ -279,6 +305,7 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) else if( strcmp( cmd[0], "CHL" ) == 0 ) { char *resp; + int st; if( num_parts < 3 ) { @@ -288,12 +315,12 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts ) } resp = msn_p11_challenge( cmd[2] ); - g_snprintf( buf, sizeof( buf ), "QRY %d %s %zd\r\n%s", - ++md->trId, MSNP11_PROD_ID, - strlen( resp ), resp ); - g_free( resp ); - return( msn_write( ic, buf, strlen( buf ) ) ); + st = msn_ns_write( ic, -1, "QRY %d %s %zd\r\n%s", + ++md->trId, MSNP11_PROD_ID, + strlen( resp ), resp ); + g_free( resp ); + return st; } else if( strcmp( cmd[0], "ILN" ) == 0 ) { @@ -698,10 +725,7 @@ void msn_auth_got_passport_token( struct im_connection *ic, const char *token, c if( token ) { - char buf[1536]; - - g_snprintf( buf, sizeof( buf ), "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token ); - msn_write( ic, buf, strlen( buf ) ); + msn_ns_write( ic, -1, "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token ); } else { @@ -712,7 +736,6 @@ void msn_auth_got_passport_token( struct im_connection *ic, const char *token, c void msn_auth_got_contact_list( struct im_connection *ic ) { - char buf[64]; struct msn_data *md; /* Dead connection? */ @@ -720,10 +743,7 @@ void msn_auth_got_contact_list( struct im_connection *ic ) return; md = ic->proto_data; - - - g_snprintf( buf, sizeof( buf ), "BLP %d %s\r\n", ++md->trId, "BL" ); - msn_write( ic, buf, strlen( buf ) ); + msn_ns_write( ic, -1, "BLP %d %s\r\n", ++md->trId, "BL" ); } static gboolean msn_ns_send_adl_1( gpointer key, gpointer value, gpointer data ) @@ -769,7 +789,7 @@ static void msn_ns_send_adl( struct im_connection *ic ) { struct xt_node *adl; struct msn_data *md = ic->proto_data; - char *adls, buf[64]; + char *adls; adl = xt_new_node( "ml", NULL, NULL ); xt_add_attr( adl, "l", "1" ); @@ -781,12 +801,9 @@ static void msn_ns_send_adl( struct im_connection *ic ) xt_free_node( adl ); return; } - adls = xt_to_string( adl ); - - g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n", ++md->trId, strlen( adls ) ); - if( msn_write( ic, buf, strlen( buf ) ) ) - msn_write( ic, adls, strlen( adls ) ); + adls = xt_to_string( adl ); + msn_ns_write( ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen( adls ), adls ); g_free( adls ); } diff --git a/protocols/msn/sb.c b/protocols/msn/sb.c index 75417fae..d10d7c19 100644 --- a/protocols/msn/sb.c +++ b/protocols/msn/sb.c @@ -34,31 +34,36 @@ static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition c 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 ); -int msn_sb_write( struct msn_switchboard *sb, char *s, int len ) +int msn_sb_write( struct msn_switchboard *sb, const char *fmt, ... ) { + va_list params; + char *out; + size_t len; int st; + va_start( params, fmt ); + out = g_strdup_vprintf( fmt, params ); + va_end( params ); + if( getenv( "BITLBEE_DEBUG" ) ) - { - write( 2, "->SB:", 5 ); - write( 2, s, len ); - } + fprintf( stderr, "->SB%d:%s", sb->fd, out ); - st = write( sb->fd, s, len ); + len = strlen( out ); + st = write( sb->fd, out, len ); + g_free( out ); if( st != len ) { msn_sb_destroy( sb ); - return( 0 ); + return 0; } - return( 1 ); + return 1; } int msn_sb_write_msg( struct im_connection *ic, struct msn_message *m ) { struct msn_data *md = ic->proto_data; struct msn_switchboard *sb; - char buf[1024]; /* FIXME: *CHECK* the reliability of using spare sb's! */ if( ( sb = msn_sb_spare( ic ) ) ) @@ -66,8 +71,7 @@ int msn_sb_write_msg( struct im_connection *ic, struct msn_message *m ) debug( "Trying to use a spare switchboard to message %s", m->who ); sb->who = g_strdup( m->who ); - g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, m->who ); - if( msn_sb_write( sb, buf, strlen( buf ) ) ) + if( msn_sb_write( sb, "CAL %d %s\r\n", ++sb->trId, m->who ) ) { /* He/She should join the switchboard soon, let's queue the message. */ sb->msgq = g_slist_append( sb->msgq, m ); @@ -78,8 +82,7 @@ int msn_sb_write_msg( struct im_connection *ic, struct msn_message *m ) debug( "Creating a new switchboard to message %s", m->who ); /* If we reach this line, there was no spare switchboard, so let's make one. */ - g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); - if( !msn_write( ic, buf, strlen( buf ) ) ) + if( !msn_ns_write( ic, -1, "XFR %d SB\r\n", ++md->trId ) ) { g_free( m->who ); g_free( m->text ); @@ -170,7 +173,7 @@ int msn_sb_sendmessage( struct msn_switchboard *sb, char *text ) { if( sb->ready ) { - char *packet, *buf; + char *buf; int i, j; /* Build the message. Convert LF to CR-LF for normal messages. */ @@ -206,17 +209,15 @@ int msn_sb_sendmessage( struct msn_switchboard *sb, char *text ) } /* Build the final packet (MSG command + the message). */ - packet = g_strdup_printf( "MSG %d N %d\r\n%s", ++sb->trId, i, buf ); - g_free( buf ); - if( msn_sb_write( sb, packet, strlen( packet ) ) ) + if( msn_sb_write( sb, "MSG %d N %d\r\n%s", ++sb->trId, i, buf ) ) { - g_free( packet ); - return( 1 ); + g_free( buf ); + return 1; } else { - g_free( packet ); - return( 0 ); + g_free( buf ); + return 0; } } else if( sb->who ) @@ -331,7 +332,7 @@ gboolean msn_sb_connected( gpointer data, gint source, b_input_condition cond ) else g_snprintf( buf, sizeof( buf ), "ANS %d %s %s %d\r\n", ++sb->trId, ic->acc->user, sb->key, sb->session ); - if( msn_sb_write( sb, buf, strlen( buf ) ) ) + if( msn_sb_write( sb, "%s", buf ) ) sb->inp = b_input_add( sb->fd, B_EV_IO_READ, msn_sb_callback, sb ); else debug( "Error %d while connecting to switchboard server", 2 ); @@ -351,7 +352,6 @@ static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition c if( sb->msgq != NULL ) { time_t now = time( NULL ); - char buf[1024]; if( now - md->first_sb_failure > 600 ) { @@ -383,8 +383,7 @@ static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition c debug( "Moved queued messages back to the main queue, " "creating a new switchboard to retry." ); - g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); - if( !msn_write( ic, buf, strlen( buf ) ) ) + if( !msn_ns_write( ic, -1, "XFR %d SB\r\n", ++md->trId ) ) return FALSE; } @@ -396,7 +395,6 @@ static int msn_sb_command( gpointer data, char **cmd, int num_parts ) { struct msn_switchboard *sb = data; struct im_connection *ic = sb->ic; - char buf[1024]; if( !num_parts ) { @@ -425,14 +423,9 @@ static int msn_sb_command( gpointer data, char **cmd, int num_parts ) } if( sb->who ) - { - g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, sb->who ); - return( msn_sb_write( sb, buf, strlen( buf ) ) ); - } + return msn_sb_write( sb, "CAL %d %s\r\n", ++sb->trId, sb->who ); else - { debug( "Just created a switchboard, but I don't know what to do with it." ); - } } else if( strcmp( cmd[0], "IRO" ) == 0 ) { |