Changeset e27661d


Ignore:
Timestamp:
2006-03-31T17:55:47Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
7d31002
Parents:
d783e48
Message:

Finished the iconv() fix. Instead of doing it every time something goes from
or to the IM-modules, it's now just done with everything that goes between
BitlBee and the user. Incomparably more efficient/reliable. Plus some more
cleanups. It compiles, can't test it for real yet. ;-)

Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • bitlbee.h

    rd783e48 re27661d  
    111111#include "query.h"
    112112#include "sock.h"
     113#include "util.h"
    113114
    114115typedef struct global {
     
    133134void root_command( irc_t *irc, char *command[] );
    134135void bitlbee_shutdown( gpointer data );
    135 double gettime( void );
    136 G_MODULE_EXPORT void http_encode( char *s );
    137 G_MODULE_EXPORT void http_decode( char *s );
    138 G_MODULE_EXPORT char *strip_newlines(char *source);
    139136
    140137extern global_t global;
  • irc.c

    rd783e48 re27661d  
    346346void irc_process( irc_t *irc )
    347347{
    348         char **lines, *temp, **cmd;
     348        char **lines, *temp, **cmd, *cs;
    349349        int i;
    350350
     
    355355                for( i = 0; *lines[i] != '\0'; i ++ )
    356356                {
     357                        /* [WvG] Because irc_tokenize splits at every newline, the lines[] list
     358                            should end with an empty string. This is why this actually works.
     359                            Took me a while to figure out, Maurits. :-P */
    357360                        if( lines[i+1] == NULL )
    358361                        {
     
    362365                                i ++;
    363366                                break;
    364                         }                       
     367                        }
     368                       
     369                        if( ( cs = set_getstr( irc, "charset" ) ) )
     370                        {
     371                                char conv[IRC_MAX_LINE+1];
     372                               
     373                                conv[IRC_MAX_LINE] = 0;
     374                                if( do_iconv( cs, "UTF-8", lines[i], conv, 0, IRC_MAX_LINE - 2 ) != -1 )
     375                                        strcpy( lines[i], conv );
     376                        }
    365377                       
    366378                        if( ( cmd = irc_parse_line( lines[i] ) ) == NULL )
     
    388400}
    389401
     402/* Splits a long string into separate lines. The array is NULL-terminated and, unless the string
     403   contains an incomplete line at the end, ends with an empty string. */
    390404char **irc_tokenize( char *buffer )
    391405{
     
    428442}
    429443
     444/* Split an IRC-style line into little parts/arguments. */
    430445char **irc_parse_line( char *line )
    431446{
     
    487502}
    488503
     504/* Converts such an array back into a command string. Mainly used for the IPC code right now. */
    489505char *irc_build_line( char **cmd )
    490506{
  • protocols/nogaim.c

    rd783e48 re27661d  
    3737#include "nogaim.h"
    3838#include <ctype.h>
    39 #include <iconv.h>
    4039
    4140static char *proto_away_alias[8][5] =
     
    306305{
    307306        va_list params;
    308         char text[1024], buf[1024], *acc_id;
    309         char *msg;
     307        char *text;
    310308        account_t *a;
    311309       
    312310        va_start( params, format );
    313         g_vsnprintf( text, sizeof( text ), format, params );
     311        text = g_strdup_vprintf( format, params );
    314312        va_end( params );
    315313
    316         if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 &&
    317             do_iconv( "UTF8", set_getstr( gc->irc, "charset" ), text, buf, 0, 1024 ) != -1 )
    318                 msg = buf;
    319         else
    320                 msg = text;
    321        
    322314        if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) ||
    323315            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) )
    324                 strip_html( msg );
     316                strip_html( text );
    325317       
    326318        /* Try to find a different connection on the same protocol. */
     
    329321                        break;
    330322       
    331         /* If we found one, add the screenname to the acc_id. */
     323        /* If we found one, include the screenname in the message. */
    332324        if( a )
    333                 acc_id = g_strdup_printf( "%s(%s)", gc->prpl->name, gc->username );
     325                irc_usermsg( gc->irc, "%s(%s) - %s", gc->prpl->name, gc->username, text );
    334326        else
    335                 acc_id = g_strdup( gc->prpl->name );
    336        
    337         irc_usermsg( gc->irc, "%s - %s", acc_id, msg );
    338        
    339         g_free( acc_id );
     327                irc_usermsg( gc->irc, "%s - %s", gc->prpl->name, text );
     328       
     329        g_free( text );
    340330}
    341331
     
    559549{
    560550        user_t *u = user_findhandle( gc, handle );
    561         char *name, buf[1024];
    562551       
    563552        if( !u ) return;
    564553       
    565         /* Convert all UTF-8 */
    566         if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 &&
    567             do_iconv( "UTF-8", set_getstr( gc->irc, "charset" ), realname, buf, 0, sizeof( buf ) ) != -1 )
    568                 name = buf;
    569         else
    570                 name = realname;
    571        
    572         if( g_strcasecmp( u->realname, name ) != 0 )
     554        if( g_strcasecmp( u->realname, realname ) != 0 )
    573555        {
    574556                if( u->realname != u->nick ) g_free( u->realname );
    575557               
    576                 u->realname = g_strdup( name );
     558                u->realname = g_strdup( realname );
    577559               
    578560                if( ( gc->flags & OPT_LOGGED_IN ) && set_getint( gc->irc, "display_namechanges" ) )
     
    680662        irc_t *irc = gc->irc;
    681663        user_t *u;
    682         char buf[8192];
    683664       
    684665        u = user_findhandle( gc, handle );
     
    722703                strip_html( msg );
    723704
    724         if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 &&
    725             do_iconv( "UTF-8", set_getstr( irc, "charset" ), msg, buf, 0, 8192 ) != -1 )
    726                 msg = buf;
    727        
    728705        while( strlen( msg ) > 425 )
    729706        {
     
    822799        struct conversation *c;
    823800        user_t *u;
    824         char buf[8192];
    825801       
    826802        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
     
    834810            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) )
    835811                strip_html( msg );
    836        
    837         if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 &&
    838             do_iconv( "UTF-8", set_getstr( gc->irc, "charset" ), msg, buf, 0, 8192 ) != -1 )
    839                 msg = buf;
    840812       
    841813        if( c && u )
     
    10531025int serv_send_im( irc_t *irc, user_t *u, char *msg, int flags )
    10541026{
    1055         char buf[8192];
    1056        
    1057         if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 &&
    1058             do_iconv( set_getstr( irc, "charset" ), "UTF-8", msg, buf, 0, 8192 ) != -1 )
     1027        char *buf = NULL;
     1028        int st;
     1029       
     1030        if( ( u->gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
     1031        {
     1032                buf = escape_html( msg );
    10591033                msg = buf;
    1060 
    1061         if( ( u->gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
    1062         {
    1063                 char *html;
    1064                
    1065                 html = escape_html( msg );
    1066                 strncpy( buf, html, 8192 );
    1067                 g_free( html );
    1068                
     1034        }
     1035       
     1036        st = ((struct gaim_connection *)u->gc)->prpl->send_im( u->gc, u->handle, msg, strlen( msg ), flags );
     1037        g_free( buf );
     1038       
     1039        return st;
     1040}
     1041
     1042int serv_send_chat( irc_t *irc, struct gaim_connection *gc, int id, char *msg )
     1043{
     1044        char *buf = NULL;
     1045        int st;
     1046       
     1047        if( ( gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
     1048        {
     1049                buf = escape_html( msg );
    10691050                msg = buf;
    10701051        }
    10711052       
    1072         return( ((struct gaim_connection *)u->gc)->prpl->send_im( u->gc, u->handle, msg, strlen( msg ), flags ) );
    1073 }
    1074 
    1075 int serv_send_chat( irc_t *irc, struct gaim_connection *gc, int id, char *msg )
    1076 {
    1077         char buf[8192];
    1078        
    1079         if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 &&
    1080             do_iconv( set_getstr( irc, "charset" ), "UTF-8", msg, buf, 0, 8192 ) != -1 )
    1081                 msg = buf;
    1082 
    1083         if( gc->flags & OPT_CONN_HTML) {
    1084                 char * html = escape_html(msg);
    1085                 strncpy(buf, html, 8192);
    1086                 g_free(html);
    1087         }
    1088        
    1089         return( gc->prpl->chat_send( gc, id, msg ) );
    1090 }
    1091 
    1092 /* Convert from one charset to another.
    1093    
    1094    from_cs, to_cs: Source and destination charsets
    1095    src, dst: Source and destination strings
    1096    size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though.
    1097    maxbuf: Maximum number of bytes to write to dst
    1098    
    1099    Returns the number of bytes written to maxbuf or -1 on an error.
    1100 */
    1101 signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf )
    1102 {
    1103         iconv_t cd;
    1104         size_t res;
    1105         size_t inbytesleft, outbytesleft;
    1106         char *inbuf = src;
    1107         char *outbuf = dst;
    1108        
    1109         cd = iconv_open( to_cs, from_cs );
    1110         if( cd == (iconv_t) -1 )
    1111                 return( -1 );
    1112        
    1113         inbytesleft = size ? size : strlen( src );
    1114         outbytesleft = maxbuf - 1;
    1115         res = iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
    1116         *outbuf = '\0';
    1117         iconv_close( cd );
    1118        
    1119         if( res == (size_t) -1 )
    1120                 return( -1 );
    1121         else
    1122                 return( outbuf - dst );
    1123 }
    1124 
    1125 char *set_eval_charset( irc_t *irc, set_t *set, char *value )
    1126 {
    1127         iconv_t cd;
    1128 
    1129         if ( g_strncasecmp( value, "none", 4 ) == 0 )
    1130                 return( value );
    1131 
    1132         cd = iconv_open( "UTF-8", value );
    1133         if( cd == (iconv_t) -1 )
    1134                 return( NULL );
    1135 
    1136         iconv_close( cd );
    1137         return( value );
    1138 }
     1053        st = gc->prpl->chat_send( gc, id, msg );
     1054        g_free( buf );
     1055       
     1056        return st;
     1057}
  • protocols/nogaim.h

    rd783e48 re27661d  
    207207int serv_send_chat(irc_t *irc, struct gaim_connection *gc, int id, char *msg );
    208208
    209 G_MODULE_EXPORT signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf );
    210 char *set_eval_charset( irc_t *irc, set_t *set, char *value );
    211 
    212209void nogaim_init();
    213210int proto_away( struct gaim_connection *gc, char *away );
     
    258255G_MODULE_EXPORT void serv_got_chat_left( struct gaim_connection *gc, int id );
    259256
    260 /* util.c */
    261 G_MODULE_EXPORT void strip_linefeed( gchar *text );
    262 G_MODULE_EXPORT char *add_cr( char *text );
    263 G_MODULE_EXPORT char *tobase64( const char *text );
    264 G_MODULE_EXPORT char *normalize( const char *s );
    265 G_MODULE_EXPORT time_t get_time( int year, int month, int day, int hour, int min, int sec );
    266 G_MODULE_EXPORT void strip_html( char *msg );
    267 G_MODULE_EXPORT char *escape_html( const char *html );
    268 G_MODULE_EXPORT void info_string_append(GString *str, char *newline, char *name, char *value);
    269 G_MODULE_EXPORT char *ipv6_wrap( char *src );
    270 G_MODULE_EXPORT char *ipv6_unwrap( char *src );
    271 
    272257/* prefs.c */
    273258G_MODULE_EXPORT void build_block_list();
  • util.c

    rd783e48 re27661d  
    3939#include <glib.h>
    4040#include <time.h>
     41#include <iconv.h>
    4142
    4243void strip_linefeed(gchar *text)
     
    445446}
    446447#endif
     448
     449/* Convert from one charset to another.
     450   
     451   from_cs, to_cs: Source and destination charsets
     452   src, dst: Source and destination strings
     453   size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though.
     454   maxbuf: Maximum number of bytes to write to dst
     455   
     456   Returns the number of bytes written to maxbuf or -1 on an error.
     457*/
     458signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf )
     459{
     460        iconv_t cd;
     461        size_t res;
     462        size_t inbytesleft, outbytesleft;
     463        char *inbuf = src;
     464        char *outbuf = dst;
     465       
     466        cd = iconv_open( to_cs, from_cs );
     467        if( cd == (iconv_t) -1 )
     468                return( -1 );
     469       
     470        inbytesleft = size ? size : strlen( src );
     471        outbytesleft = maxbuf - 1;
     472        res = iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
     473        *outbuf = '\0';
     474        iconv_close( cd );
     475       
     476        if( res == (size_t) -1 )
     477                return( -1 );
     478        else
     479                return( outbuf - dst );
     480}
     481
     482char *set_eval_charset( irc_t *irc, set_t *set, char *value )
     483{
     484        iconv_t cd;
     485
     486        if ( g_strncasecmp( value, "none", 4 ) == 0 )
     487                return( value );
     488
     489        cd = iconv_open( "UTF-8", value );
     490        if( cd == (iconv_t) -1 )
     491                return( NULL );
     492
     493        iconv_close( cd );
     494        return( value );
     495}
Note: See TracChangeset for help on using the changeset viewer.