[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
[3ddb7477] | 7 | /* The IRC-based UI (for now the only one) */ |
---|
[b7d3cc34] | 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 "bitlbee.h" |
---|
| 27 | |
---|
[3ddb7477] | 28 | GSList *irc_connection_list; |
---|
[b7d3cc34] | 29 | |
---|
[3ddb7477] | 30 | static char *set_eval_charset( set_t *set, char *value ); |
---|
[58adb7e] | 31 | |
---|
[b7d3cc34] | 32 | irc_t *irc_new( int fd ) |
---|
| 33 | { |
---|
[e4d6271] | 34 | irc_t *irc; |
---|
[e9b755e] | 35 | struct sockaddr_storage sock; |
---|
[7435ccf] | 36 | socklen_t socklen = sizeof( sock ); |
---|
[3ddb7477] | 37 | char *host = NULL, *myhost = NULL; |
---|
| 38 | irc_user_t *iu; |
---|
[7125cb3] | 39 | set_t *s; |
---|
[3ddb7477] | 40 | bee_t *b; |
---|
[e4d6271] | 41 | |
---|
| 42 | irc = g_new0( irc_t, 1 ); |
---|
[b7d3cc34] | 43 | |
---|
| 44 | irc->fd = fd; |
---|
[a0d04d6] | 45 | sock_make_nonblocking( irc->fd ); |
---|
| 46 | |
---|
[ba9edaa] | 47 | irc->r_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_READ, bitlbee_io_current_client_read, irc ); |
---|
[b7d3cc34] | 48 | |
---|
| 49 | irc->status = USTATUS_OFFLINE; |
---|
| 50 | irc->last_pong = gettime(); |
---|
| 51 | |
---|
[3ddb7477] | 52 | irc->nick_user_hash = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
[b7d3cc34] | 53 | irc->watches = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
| 54 | |
---|
[f9756bd] | 55 | irc->iconv = (GIConv) -1; |
---|
| 56 | irc->oconv = (GIConv) -1; |
---|
| 57 | |
---|
[b7d3cc34] | 58 | if( global.conf->hostname ) |
---|
| 59 | { |
---|
[3ddb7477] | 60 | myhost = g_strdup( global.conf->hostname ); |
---|
[b7d3cc34] | 61 | } |
---|
[7435ccf] | 62 | else if( getsockname( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 63 | { |
---|
[2231302] | 64 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 65 | |
---|
[2231302] | 66 | if( getnameinfo( (struct sockaddr *) &sock, socklen, buf, |
---|
[c84e31a] | 67 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
[2231302] | 68 | { |
---|
[3ddb7477] | 69 | myhost = g_strdup( ipv6_unwrap( buf ) ); |
---|
[2231302] | 70 | } |
---|
[b7d3cc34] | 71 | } |
---|
| 72 | |
---|
[7435ccf] | 73 | if( getpeername( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 74 | { |
---|
[2231302] | 75 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 76 | |
---|
[2231302] | 77 | if( getnameinfo( (struct sockaddr *)&sock, socklen, buf, |
---|
[c84e31a] | 78 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
[2231302] | 79 | { |
---|
[3ddb7477] | 80 | host = g_strdup( ipv6_unwrap( buf ) ); |
---|
[2231302] | 81 | } |
---|
[b7d3cc34] | 82 | } |
---|
| 83 | |
---|
[3ddb7477] | 84 | if( host == NULL ) |
---|
| 85 | host = g_strdup( "localhost.localdomain" ); |
---|
| 86 | if( myhost == NULL ) |
---|
| 87 | myhost = g_strdup( "localhost.localdomain" ); |
---|
[3e1e11af] | 88 | |
---|
[3ddb7477] | 89 | //if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 ) |
---|
| 90 | // irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc ); |
---|
[b7d3cc34] | 91 | |
---|
| 92 | irc_connection_list = g_slist_append( irc_connection_list, irc ); |
---|
| 93 | |
---|
[3ddb7477] | 94 | b = irc->b = bee_new(); |
---|
| 95 | |
---|
| 96 | s = set_add( &b->set, "away_devoice", "true", NULL/*set_eval_away_devoice*/, irc ); |
---|
| 97 | s = set_add( &b->set, "buddy_sendbuffer", "false", set_eval_bool, irc ); |
---|
| 98 | s = set_add( &b->set, "buddy_sendbuffer_delay", "200", set_eval_int, irc ); |
---|
| 99 | s = set_add( &b->set, "charset", "utf-8", set_eval_charset, irc ); |
---|
| 100 | //s = set_add( &b->set, "control_channel", irc->channel, NULL/*set_eval_control_channel*/, irc ); |
---|
| 101 | s = set_add( &b->set, "default_target", "root", NULL, irc ); |
---|
| 102 | s = set_add( &b->set, "display_namechanges", "false", set_eval_bool, irc ); |
---|
| 103 | s = set_add( &b->set, "handle_unknown", "root", NULL, irc ); |
---|
| 104 | s = set_add( &b->set, "lcnicks", "true", set_eval_bool, irc ); |
---|
| 105 | s = set_add( &b->set, "ops", "both", NULL/*set_eval_ops*/, irc ); |
---|
| 106 | s = set_add( &b->set, "private", "true", set_eval_bool, irc ); |
---|
| 107 | s = set_add( &b->set, "query_order", "lifo", NULL, irc ); |
---|
| 108 | s = set_add( &b->set, "root_nick", ROOT_NICK, NULL/*set_eval_root_nick*/, irc ); |
---|
| 109 | s = set_add( &b->set, "simulate_netsplit", "true", set_eval_bool, irc ); |
---|
| 110 | s = set_add( &b->set, "to_char", ": ", set_eval_to_char, irc ); |
---|
| 111 | s = set_add( &b->set, "typing_notice", "false", set_eval_bool, irc ); |
---|
| 112 | |
---|
| 113 | irc->root = iu = irc_user_new( irc, ROOT_NICK ); |
---|
| 114 | iu->host = g_strdup( myhost ); |
---|
| 115 | iu->fullname = g_strdup( ROOT_FN ); |
---|
[280c56a] | 116 | iu->f = &irc_user_root_funcs; |
---|
[3ddb7477] | 117 | |
---|
| 118 | iu = irc_user_new( irc, NS_NICK ); |
---|
| 119 | iu->host = g_strdup( myhost ); |
---|
| 120 | iu->fullname = g_strdup( ROOT_FN ); |
---|
[280c56a] | 121 | iu->f = &irc_user_root_funcs; |
---|
[3ddb7477] | 122 | |
---|
| 123 | irc->user = g_new0( irc_user_t, 1 ); |
---|
| 124 | irc->user->host = g_strdup( host ); |
---|
| 125 | |
---|
[ebaebfe] | 126 | conf_loaddefaults( irc ); |
---|
[b7d3cc34] | 127 | |
---|
[f9756bd] | 128 | /* Evaluator sets the iconv/oconv structures. */ |
---|
[3ddb7477] | 129 | set_eval_charset( set_find( &b->set, "charset" ), set_getstr( &b->set, "charset" ) ); |
---|
[f9756bd] | 130 | |
---|
[3ddb7477] | 131 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, "BitlBee-IRCd initialized, please go on" ); |
---|
| 132 | |
---|
[ebaebfe] | 133 | g_free( myhost ); |
---|
| 134 | g_free( host ); |
---|
| 135 | |
---|
[3ddb7477] | 136 | return irc; |
---|
[b7d3cc34] | 137 | } |
---|
| 138 | |
---|
[f73b969] | 139 | /* immed=1 makes this function pretty much equal to irc_free(), except that |
---|
| 140 | this one will "log". In case the connection is already broken and we |
---|
| 141 | shouldn't try to write to it. */ |
---|
[fc50d48] | 142 | void irc_abort( irc_t *irc, int immed, char *format, ... ) |
---|
[c1826c6] | 143 | { |
---|
[fc50d48] | 144 | if( format != NULL ) |
---|
| 145 | { |
---|
[f73b969] | 146 | va_list params; |
---|
[fc50d48] | 147 | char *reason; |
---|
| 148 | |
---|
| 149 | va_start( params, format ); |
---|
[f73b969] | 150 | reason = g_strdup_vprintf( format, params ); |
---|
[fc50d48] | 151 | va_end( params ); |
---|
| 152 | |
---|
| 153 | if( !immed ) |
---|
| 154 | irc_write( irc, "ERROR :Closing link: %s", reason ); |
---|
| 155 | |
---|
| 156 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
[3ddb7477] | 157 | irc->user->nick ? irc->user->nick : "(NONE)", irc->root->host, reason ); |
---|
[fc50d48] | 158 | |
---|
| 159 | g_free( reason ); |
---|
| 160 | } |
---|
| 161 | else |
---|
| 162 | { |
---|
| 163 | if( !immed ) |
---|
| 164 | irc_write( irc, "ERROR :Closing link" ); |
---|
| 165 | |
---|
| 166 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
[3ddb7477] | 167 | irc->user->nick ? irc->user->nick : "(NONE)", irc->root->host, "No reason given" ); |
---|
[fc50d48] | 168 | } |
---|
| 169 | |
---|
[79e826a] | 170 | irc->status |= USTATUS_SHUTDOWN; |
---|
[fc50d48] | 171 | if( irc->sendbuffer && !immed ) |
---|
[c1826c6] | 172 | { |
---|
[883a398] | 173 | /* Set up a timeout event that should shut down the connection |
---|
| 174 | in a second, just in case ..._write doesn't do it first. */ |
---|
[fc50d48] | 175 | |
---|
[ba9edaa] | 176 | b_event_remove( irc->r_watch_source_id ); |
---|
[883a398] | 177 | irc->r_watch_source_id = 0; |
---|
| 178 | |
---|
| 179 | b_event_remove( irc->ping_source_id ); |
---|
| 180 | irc->ping_source_id = b_timeout_add( 1000, (b_event_handler) irc_free, irc ); |
---|
[c1826c6] | 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | irc_free( irc ); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
[3ddb7477] | 188 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ); |
---|
[b7d3cc34] | 189 | |
---|
[fa75134] | 190 | void irc_free( irc_t * irc ) |
---|
[b7d3cc34] | 191 | { |
---|
| 192 | log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd ); |
---|
| 193 | |
---|
[3ddb7477] | 194 | /* |
---|
| 195 | if( irc->status & USTATUS_IDENTIFIED && set_getbool( &irc->b->set, "save_on_quit" ) ) |
---|
[3183c21] | 196 | if( storage_save( irc, NULL, TRUE ) != STORAGE_OK ) |
---|
[b7d3cc34] | 197 | irc_usermsg( irc, "Error while saving settings!" ); |
---|
[3ddb7477] | 198 | */ |
---|
[b7d3cc34] | 199 | |
---|
| 200 | irc_connection_list = g_slist_remove( irc_connection_list, irc ); |
---|
| 201 | |
---|
[3ddb7477] | 202 | /* |
---|
[fa75134] | 203 | while( irc->queries != NULL ) |
---|
| 204 | query_del( irc, irc->queries ); |
---|
[3ddb7477] | 205 | */ |
---|
[5b52a48] | 206 | |
---|
[3ddb7477] | 207 | while( irc->users ) |
---|
[ebaebfe] | 208 | { |
---|
| 209 | irc_user_t *iu = irc->users->data; |
---|
| 210 | irc_user_free( irc, iu->nick ); |
---|
| 211 | } |
---|
[b7d3cc34] | 212 | |
---|
[63a520b] | 213 | while( irc->channels ) |
---|
| 214 | irc_channel_free( irc->channels->data ); |
---|
| 215 | |
---|
[fa75134] | 216 | if( irc->ping_source_id > 0 ) |
---|
| 217 | b_event_remove( irc->ping_source_id ); |
---|
[883a398] | 218 | if( irc->r_watch_source_id > 0 ) |
---|
| 219 | b_event_remove( irc->r_watch_source_id ); |
---|
[fa75134] | 220 | if( irc->w_watch_source_id > 0 ) |
---|
| 221 | b_event_remove( irc->w_watch_source_id ); |
---|
| 222 | |
---|
| 223 | closesocket( irc->fd ); |
---|
| 224 | irc->fd = -1; |
---|
| 225 | |
---|
[3ddb7477] | 226 | g_hash_table_foreach_remove( irc->nick_user_hash, irc_free_hashkey, NULL ); |
---|
| 227 | g_hash_table_destroy( irc->nick_user_hash ); |
---|
[b7d3cc34] | 228 | |
---|
[fa75134] | 229 | g_hash_table_foreach_remove( irc->watches, irc_free_hashkey, NULL ); |
---|
| 230 | g_hash_table_destroy( irc->watches ); |
---|
[b7d3cc34] | 231 | |
---|
[f9756bd] | 232 | if( irc->iconv != (GIConv) -1 ) |
---|
| 233 | g_iconv_close( irc->iconv ); |
---|
| 234 | if( irc->oconv != (GIConv) -1 ) |
---|
| 235 | g_iconv_close( irc->oconv ); |
---|
| 236 | |
---|
[fa75134] | 237 | g_free( irc->sendbuffer ); |
---|
| 238 | g_free( irc->readbuffer ); |
---|
| 239 | |
---|
| 240 | g_free( irc->password ); |
---|
| 241 | |
---|
| 242 | g_free( irc ); |
---|
[b7d3cc34] | 243 | |
---|
[565a1ea] | 244 | if( global.conf->runmode == RUNMODE_INETD || |
---|
| 245 | global.conf->runmode == RUNMODE_FORKDAEMON || |
---|
| 246 | ( global.conf->runmode == RUNMODE_DAEMON && |
---|
| 247 | global.listen_socket == -1 && |
---|
| 248 | irc_connection_list == NULL ) ) |
---|
[ba9edaa] | 249 | b_main_quit(); |
---|
[b7d3cc34] | 250 | } |
---|
| 251 | |
---|
[3ddb7477] | 252 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ) |
---|
[7cad7b4] | 253 | { |
---|
[3ddb7477] | 254 | g_free( key ); |
---|
[7cad7b4] | 255 | |
---|
[3ddb7477] | 256 | return( TRUE ); |
---|
[7cad7b4] | 257 | } |
---|
| 258 | |
---|
[3ddb7477] | 259 | static char **irc_splitlines( char *buffer ); |
---|
| 260 | |
---|
[f73b969] | 261 | void irc_process( irc_t *irc ) |
---|
[b7d3cc34] | 262 | { |
---|
[f9756bd] | 263 | char **lines, *temp, **cmd; |
---|
[b7d3cc34] | 264 | int i; |
---|
| 265 | |
---|
[de3e100] | 266 | if( irc->readbuffer != NULL ) |
---|
| 267 | { |
---|
[3ddb7477] | 268 | lines = irc_splitlines( irc->readbuffer ); |
---|
[de3e100] | 269 | |
---|
| 270 | for( i = 0; *lines[i] != '\0'; i ++ ) |
---|
| 271 | { |
---|
[f9756bd] | 272 | char *conv = NULL; |
---|
[7d31002] | 273 | |
---|
[18ff38f] | 274 | /* [WvG] If the last line isn't empty, it's an incomplete line and we |
---|
| 275 | should wait for the rest to come in before processing it. */ |
---|
[de3e100] | 276 | if( lines[i+1] == NULL ) |
---|
| 277 | { |
---|
[b7d3cc34] | 278 | temp = g_strdup( lines[i] ); |
---|
| 279 | g_free( irc->readbuffer ); |
---|
| 280 | irc->readbuffer = temp; |
---|
[de3e100] | 281 | i ++; |
---|
[b7d3cc34] | 282 | break; |
---|
[e27661d] | 283 | } |
---|
| 284 | |
---|
[f9756bd] | 285 | if( irc->iconv != (GIConv) -1 ) |
---|
[e27661d] | 286 | { |
---|
[f9756bd] | 287 | gsize bytes_read, bytes_written; |
---|
| 288 | |
---|
| 289 | conv = g_convert_with_iconv( lines[i], -1, irc->iconv, |
---|
| 290 | &bytes_read, &bytes_written, NULL ); |
---|
| 291 | |
---|
| 292 | if( conv == NULL || bytes_read != strlen( lines[i] ) ) |
---|
[94d52d64] | 293 | { |
---|
[fc0cf92] | 294 | /* GLib can do strange things if things are not in the expected charset, |
---|
| 295 | so let's be a little bit paranoid here: */ |
---|
[94d52d64] | 296 | if( irc->status & USTATUS_LOGGED_IN ) |
---|
[fc0cf92] | 297 | { |
---|
[43462708] | 298 | irc_usermsg( irc, "Error: Charset mismatch detected. The charset " |
---|
[94d52d64] | 299 | "setting is currently set to %s, so please make " |
---|
| 300 | "sure your IRC client will send and accept text in " |
---|
| 301 | "that charset, or tell BitlBee which charset to " |
---|
| 302 | "expect by changing the charset setting. See " |
---|
| 303 | "`help set charset' for more information. Your " |
---|
[f9756bd] | 304 | "message was ignored.", |
---|
[3ddb7477] | 305 | set_getstr( &irc->b->set, "charset" ) ); |
---|
[f9756bd] | 306 | |
---|
| 307 | g_free( conv ); |
---|
| 308 | conv = NULL; |
---|
[fc0cf92] | 309 | } |
---|
| 310 | else |
---|
| 311 | { |
---|
[3ddb7477] | 312 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, |
---|
[a83442a] | 313 | "Warning: invalid characters received at login time." ); |
---|
[fc0cf92] | 314 | |
---|
[f9756bd] | 315 | conv = g_strdup( lines[i] ); |
---|
[fc0cf92] | 316 | for( temp = conv; *temp; temp ++ ) |
---|
| 317 | if( *temp & 0x80 ) |
---|
| 318 | *temp = '?'; |
---|
| 319 | } |
---|
[94d52d64] | 320 | } |
---|
| 321 | lines[i] = conv; |
---|
[e27661d] | 322 | } |
---|
[de3e100] | 323 | |
---|
[e1720ce] | 324 | if( lines[i] && ( cmd = irc_parse_line( lines[i] ) ) ) |
---|
[f9756bd] | 325 | { |
---|
| 326 | irc_exec( irc, cmd ); |
---|
| 327 | g_free( cmd ); |
---|
| 328 | } |
---|
[f73b969] | 329 | |
---|
[f9756bd] | 330 | g_free( conv ); |
---|
[f73b969] | 331 | |
---|
| 332 | /* Shouldn't really happen, but just in case... */ |
---|
| 333 | if( !g_slist_find( irc_connection_list, irc ) ) |
---|
[de3e100] | 334 | { |
---|
[b7d3cc34] | 335 | g_free( lines ); |
---|
[f73b969] | 336 | return; |
---|
[b7d3cc34] | 337 | } |
---|
| 338 | } |
---|
[de3e100] | 339 | |
---|
| 340 | if( lines[i] != NULL ) |
---|
| 341 | { |
---|
| 342 | g_free( irc->readbuffer ); |
---|
[0431ea1] | 343 | irc->readbuffer = NULL; |
---|
[b7d3cc34] | 344 | } |
---|
[de3e100] | 345 | |
---|
[b7d3cc34] | 346 | g_free( lines ); |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
[3ddb7477] | 350 | /* Splits a long string into separate lines. The array is NULL-terminated |
---|
| 351 | and, unless the string contains an incomplete line at the end, ends with |
---|
| 352 | an empty string. Could use g_strsplit() but this one does it in-place. |
---|
| 353 | (So yes, it's destructive.) */ |
---|
| 354 | static char **irc_splitlines( char *buffer ) |
---|
[b7d3cc34] | 355 | { |
---|
[18ff38f] | 356 | int i, j, n = 3; |
---|
[b7d3cc34] | 357 | char **lines; |
---|
| 358 | |
---|
[18ff38f] | 359 | /* Allocate n+1 elements. */ |
---|
| 360 | lines = g_new( char *, n + 1 ); |
---|
[b7d3cc34] | 361 | |
---|
[de3e100] | 362 | lines[0] = buffer; |
---|
[b7d3cc34] | 363 | |
---|
[18ff38f] | 364 | /* Split the buffer in several strings, and accept any kind of line endings, |
---|
| 365 | * knowing that ERC on Windows may send something interesting like \r\r\n, |
---|
| 366 | * and surely there must be clients that think just \n is enough... */ |
---|
| 367 | for( i = 0, j = 0; buffer[i] != '\0'; i ++ ) |
---|
[de3e100] | 368 | { |
---|
[18ff38f] | 369 | if( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
[de3e100] | 370 | { |
---|
[18ff38f] | 371 | while( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
| 372 | buffer[i++] = '\0'; |
---|
| 373 | |
---|
| 374 | lines[++j] = buffer + i; |
---|
[de3e100] | 375 | |
---|
[18ff38f] | 376 | if( j >= n ) |
---|
| 377 | { |
---|
| 378 | n *= 2; |
---|
| 379 | lines = g_renew( char *, lines, n + 1 ); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | if( buffer[i] == '\0' ) |
---|
| 383 | break; |
---|
[b7d3cc34] | 384 | } |
---|
| 385 | } |
---|
[de3e100] | 386 | |
---|
[18ff38f] | 387 | /* NULL terminate our list. */ |
---|
| 388 | lines[++j] = NULL; |
---|
| 389 | |
---|
| 390 | return lines; |
---|
[b7d3cc34] | 391 | } |
---|
| 392 | |
---|
[e27661d] | 393 | /* Split an IRC-style line into little parts/arguments. */ |
---|
[0431ea1] | 394 | char **irc_parse_line( char *line ) |
---|
[b7d3cc34] | 395 | { |
---|
| 396 | int i, j; |
---|
| 397 | char **cmd; |
---|
| 398 | |
---|
| 399 | /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ |
---|
[de3e100] | 400 | if( line[0] == ':' ) |
---|
| 401 | { |
---|
[e1720ce] | 402 | for( i = 0; line[i] && line[i] != ' '; i ++ ); |
---|
[de3e100] | 403 | line = line + i; |
---|
[b7d3cc34] | 404 | } |
---|
[de3e100] | 405 | for( i = 0; line[i] == ' '; i ++ ); |
---|
| 406 | line = line + i; |
---|
| 407 | |
---|
[b7d3cc34] | 408 | /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ |
---|
[de3e100] | 409 | if( line[0] == '\0') |
---|
| 410 | return NULL; |
---|
| 411 | |
---|
| 412 | /* Count the number of char **cmd elements we're going to need. */ |
---|
| 413 | j = 1; |
---|
| 414 | for( i = 0; line[i] != '\0'; i ++ ) |
---|
| 415 | { |
---|
| 416 | if( line[i] == ' ' ) |
---|
| 417 | { |
---|
| 418 | j ++; |
---|
[b7d3cc34] | 419 | |
---|
[de3e100] | 420 | if( line[i+1] == ':' ) |
---|
| 421 | break; |
---|
| 422 | } |
---|
[b7d3cc34] | 423 | } |
---|
| 424 | |
---|
| 425 | /* Allocate the space we need. */ |
---|
[de3e100] | 426 | cmd = g_new( char *, j + 1 ); |
---|
| 427 | cmd[j] = NULL; |
---|
[b7d3cc34] | 428 | |
---|
| 429 | /* Do the actual line splitting, format is: |
---|
| 430 | * Input: "PRIVMSG #bitlbee :foo bar" |
---|
| 431 | * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL |
---|
| 432 | */ |
---|
| 433 | |
---|
[de3e100] | 434 | cmd[0] = line; |
---|
| 435 | for( i = 0, j = 0; line[i] != '\0'; i ++ ) |
---|
[b7d3cc34] | 436 | { |
---|
[de3e100] | 437 | if( line[i] == ' ' ) |
---|
[b7d3cc34] | 438 | { |
---|
[de3e100] | 439 | line[i] = '\0'; |
---|
| 440 | cmd[++j] = line + i + 1; |
---|
[b7d3cc34] | 441 | |
---|
[de3e100] | 442 | if( line[i+1] == ':' ) |
---|
[b7d3cc34] | 443 | { |
---|
[de3e100] | 444 | cmd[j] ++; |
---|
[b7d3cc34] | 445 | break; |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | |
---|
[de3e100] | 450 | return cmd; |
---|
[b7d3cc34] | 451 | } |
---|
| 452 | |
---|
[e27661d] | 453 | /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ |
---|
[74c119d] | 454 | char *irc_build_line( char **cmd ) |
---|
| 455 | { |
---|
| 456 | int i, len; |
---|
| 457 | char *s; |
---|
[b7d3cc34] | 458 | |
---|
[74c119d] | 459 | if( cmd[0] == NULL ) |
---|
| 460 | return NULL; |
---|
[b7d3cc34] | 461 | |
---|
[74c119d] | 462 | len = 1; |
---|
| 463 | for( i = 0; cmd[i]; i ++ ) |
---|
| 464 | len += strlen( cmd[i] ) + 1; |
---|
| 465 | |
---|
| 466 | if( strchr( cmd[i-1], ' ' ) != NULL ) |
---|
| 467 | len ++; |
---|
| 468 | |
---|
| 469 | s = g_new0( char, len + 1 ); |
---|
| 470 | for( i = 0; cmd[i]; i ++ ) |
---|
[b7d3cc34] | 471 | { |
---|
[74c119d] | 472 | if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) |
---|
| 473 | strcat( s, ":" ); |
---|
[b7d3cc34] | 474 | |
---|
[74c119d] | 475 | strcat( s, cmd[i] ); |
---|
[b7d3cc34] | 476 | |
---|
[74c119d] | 477 | if( cmd[i+1] ) |
---|
| 478 | strcat( s, " " ); |
---|
[b7d3cc34] | 479 | } |
---|
[74c119d] | 480 | strcat( s, "\r\n" ); |
---|
[b7d3cc34] | 481 | |
---|
[74c119d] | 482 | return s; |
---|
[b7d3cc34] | 483 | } |
---|
| 484 | |
---|
[3ddb7477] | 485 | void irc_write( irc_t *irc, char *format, ... ) |
---|
[b7d3cc34] | 486 | { |
---|
| 487 | va_list params; |
---|
[3ddb7477] | 488 | |
---|
[b7d3cc34] | 489 | va_start( params, format ); |
---|
[3ddb7477] | 490 | irc_vawrite( irc, format, params ); |
---|
[b7d3cc34] | 491 | va_end( params ); |
---|
[3ddb7477] | 492 | |
---|
[b7d3cc34] | 493 | return; |
---|
| 494 | } |
---|
| 495 | |
---|
[3ddb7477] | 496 | void irc_write_all( int now, char *format, ... ) |
---|
[b7d3cc34] | 497 | { |
---|
| 498 | va_list params; |
---|
[3ddb7477] | 499 | GSList *temp; |
---|
[b7d3cc34] | 500 | |
---|
| 501 | va_start( params, format ); |
---|
| 502 | |
---|
[3ddb7477] | 503 | temp = irc_connection_list; |
---|
| 504 | while( temp != NULL ) |
---|
| 505 | { |
---|
| 506 | irc_t *irc = temp->data; |
---|
| 507 | |
---|
| 508 | if( now ) |
---|
| 509 | { |
---|
| 510 | g_free( irc->sendbuffer ); |
---|
| 511 | irc->sendbuffer = g_strdup( "\r\n" ); |
---|
| 512 | } |
---|
| 513 | irc_vawrite( temp->data, format, params ); |
---|
| 514 | if( now ) |
---|
| 515 | { |
---|
| 516 | bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ); |
---|
| 517 | } |
---|
| 518 | temp = temp->next; |
---|
| 519 | } |
---|
| 520 | |
---|
[b7d3cc34] | 521 | va_end( params ); |
---|
| 522 | return; |
---|
[3ddb7477] | 523 | } |
---|
[b7d3cc34] | 524 | |
---|
| 525 | void irc_vawrite( irc_t *irc, char *format, va_list params ) |
---|
| 526 | { |
---|
| 527 | int size; |
---|
[f9756bd] | 528 | char line[IRC_MAX_LINE+1]; |
---|
[d783e48] | 529 | |
---|
[0356ae3] | 530 | /* Don't try to write anything new anymore when shutting down. */ |
---|
[5898ef8] | 531 | if( irc->status & USTATUS_SHUTDOWN ) |
---|
[b7d3cc34] | 532 | return; |
---|
[d783e48] | 533 | |
---|
[f9756bd] | 534 | memset( line, 0, sizeof( line ) ); |
---|
[d783e48] | 535 | g_vsnprintf( line, IRC_MAX_LINE - 2, format, params ); |
---|
[b7d3cc34] | 536 | strip_newlines( line ); |
---|
[f9756bd] | 537 | |
---|
| 538 | if( irc->oconv != (GIConv) -1 ) |
---|
[d783e48] | 539 | { |
---|
[f9756bd] | 540 | gsize bytes_read, bytes_written; |
---|
| 541 | char *conv; |
---|
| 542 | |
---|
| 543 | conv = g_convert_with_iconv( line, -1, irc->oconv, |
---|
| 544 | &bytes_read, &bytes_written, NULL ); |
---|
| 545 | |
---|
| 546 | if( bytes_read == strlen( line ) ) |
---|
| 547 | strncpy( line, conv, IRC_MAX_LINE - 2 ); |
---|
[d783e48] | 548 | |
---|
[f9756bd] | 549 | g_free( conv ); |
---|
[d783e48] | 550 | } |
---|
[f9756bd] | 551 | g_strlcat( line, "\r\n", IRC_MAX_LINE + 1 ); |
---|
[d783e48] | 552 | |
---|
[a0d04d6] | 553 | if( irc->sendbuffer != NULL ) |
---|
| 554 | { |
---|
[b7d3cc34] | 555 | size = strlen( irc->sendbuffer ) + strlen( line ); |
---|
| 556 | irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 ); |
---|
| 557 | strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line ); |
---|
| 558 | } |
---|
[a0d04d6] | 559 | else |
---|
[b7d3cc34] | 560 | { |
---|
[a0d04d6] | 561 | irc->sendbuffer = g_strdup(line); |
---|
[b7d3cc34] | 562 | } |
---|
| 563 | |
---|
[a0d04d6] | 564 | if( irc->w_watch_source_id == 0 ) |
---|
[0356ae3] | 565 | { |
---|
| 566 | /* If the buffer is empty we can probably write, so call the write event handler |
---|
| 567 | immediately. If it returns TRUE, it should be called again, so add the event to |
---|
| 568 | the queue. If it's FALSE, we emptied the buffer and saved ourselves some work |
---|
| 569 | in the event queue. */ |
---|
[bbb6ffb] | 570 | /* Really can't be done as long as the code doesn't do error checking very well: |
---|
| 571 | if( bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ) ) */ |
---|
| 572 | |
---|
| 573 | /* So just always do it via the event handler. */ |
---|
| 574 | irc->w_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_WRITE, bitlbee_io_current_client_write, irc ); |
---|
[0356ae3] | 575 | } |
---|
[a0d04d6] | 576 | |
---|
[b7d3cc34] | 577 | return; |
---|
| 578 | } |
---|
| 579 | |
---|
[edf9657] | 580 | int irc_check_login( irc_t *irc ) |
---|
[b7d3cc34] | 581 | { |
---|
[3ddb7477] | 582 | if( irc->user->user && irc->user->nick ) |
---|
[edf9657] | 583 | { |
---|
[3af70b0] | 584 | if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) ) |
---|
[b7d3cc34] | 585 | { |
---|
[3ddb7477] | 586 | irc_send_num( irc, 464, ":This server is password-protected." ); |
---|
[edf9657] | 587 | return 0; |
---|
[b7d3cc34] | 588 | } |
---|
[edf9657] | 589 | else |
---|
[b7d3cc34] | 590 | { |
---|
[4be8239] | 591 | irc_channel_t *ic; |
---|
| 592 | irc_user_t *iu = irc->user; |
---|
| 593 | |
---|
| 594 | irc->user = irc_user_new( irc, iu->nick ); |
---|
| 595 | irc->user->user = iu->user; |
---|
[b95932e] | 596 | irc->user->host = iu->host; |
---|
[4be8239] | 597 | irc->user->fullname = iu->fullname; |
---|
[280c56a] | 598 | irc->user->f = &irc_user_self_funcs; |
---|
[4be8239] | 599 | g_free( iu->nick ); |
---|
| 600 | g_free( iu ); |
---|
| 601 | |
---|
| 602 | if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 603 | ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->user->host, irc->user->nick, irc->user->fullname ); |
---|
| 604 | |
---|
| 605 | irc->status |= USTATUS_LOGGED_IN; |
---|
| 606 | |
---|
| 607 | /* This is for bug #209 (use PASS to identify to NickServ). */ |
---|
| 608 | if( irc->password != NULL ) |
---|
| 609 | { |
---|
| 610 | char *send_cmd[] = { "identify", g_strdup( irc->password ), NULL }; |
---|
| 611 | |
---|
| 612 | /*irc_setpass( irc, NULL );*/ |
---|
| 613 | /*root_command( irc, send_cmd );*/ |
---|
| 614 | g_free( send_cmd[1] ); |
---|
| 615 | } |
---|
| 616 | |
---|
[3ddb7477] | 617 | irc_send_login( irc ); |
---|
[4be8239] | 618 | |
---|
[b919363] | 619 | irc->umode[0] = '\0'; |
---|
| 620 | irc_umode_set( irc, "+" UMODE, TRUE ); |
---|
| 621 | |
---|
[4be8239] | 622 | ic = irc_channel_new( irc, ROOT_CHAN ); |
---|
[83e92bf] | 623 | irc_channel_set_topic( ic, CONTROL_TOPIC, irc->root ); |
---|
[4be8239] | 624 | irc_channel_add_user( ic, irc->user ); |
---|
| 625 | |
---|
[74f1cde] | 626 | irc->last_root_cmd = g_strdup( ROOT_CHAN ); |
---|
| 627 | |
---|
[edf9657] | 628 | return 1; |
---|
[b7d3cc34] | 629 | } |
---|
[edf9657] | 630 | } |
---|
| 631 | else |
---|
| 632 | { |
---|
| 633 | /* More information needed. */ |
---|
| 634 | return 0; |
---|
| 635 | } |
---|
[b7d3cc34] | 636 | } |
---|
| 637 | |
---|
[b919363] | 638 | void irc_umode_set( irc_t *irc, const char *s, gboolean allow_priv ) |
---|
| 639 | { |
---|
| 640 | /* allow_priv: Set to 0 if s contains user input, 1 if you want |
---|
| 641 | to set a "privileged" mode (+o, +R, etc). */ |
---|
| 642 | char m[128], st = 1; |
---|
| 643 | const char *t; |
---|
| 644 | int i; |
---|
| 645 | char changes[512], *p, st2 = 2; |
---|
| 646 | char badflag = 0; |
---|
| 647 | |
---|
| 648 | memset( m, 0, sizeof( m ) ); |
---|
| 649 | |
---|
| 650 | for( t = irc->umode; *t; t ++ ) |
---|
| 651 | if( *t < sizeof( m ) ) |
---|
| 652 | m[(int)*t] = 1; |
---|
| 653 | |
---|
| 654 | p = changes; |
---|
| 655 | for( t = s; *t; t ++ ) |
---|
| 656 | { |
---|
| 657 | if( *t == '+' || *t == '-' ) |
---|
| 658 | st = *t == '+'; |
---|
| 659 | else if( ( st == 0 && ( !strchr( UMODES_KEEP, *t ) || allow_priv ) ) || |
---|
| 660 | ( st == 1 && strchr( UMODES, *t ) ) || |
---|
| 661 | ( st == 1 && allow_priv && strchr( UMODES_PRIV, *t ) ) ) |
---|
| 662 | { |
---|
| 663 | if( m[(int)*t] != st) |
---|
| 664 | { |
---|
| 665 | if( st != st2 ) |
---|
| 666 | st2 = st, *p++ = st ? '+' : '-'; |
---|
| 667 | *p++ = *t; |
---|
| 668 | } |
---|
| 669 | m[(int)*t] = st; |
---|
| 670 | } |
---|
| 671 | else |
---|
| 672 | badflag = 1; |
---|
| 673 | } |
---|
| 674 | *p = '\0'; |
---|
| 675 | |
---|
| 676 | memset( irc->umode, 0, sizeof( irc->umode ) ); |
---|
| 677 | |
---|
| 678 | for( i = 'A'; i <= 'z' && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ ) |
---|
| 679 | if( m[i] ) |
---|
| 680 | irc->umode[strlen(irc->umode)] = i; |
---|
| 681 | |
---|
| 682 | if( badflag ) |
---|
| 683 | irc_send_num( irc, 501, ":Unknown MODE flag" ); |
---|
| 684 | if( *changes ) |
---|
| 685 | irc_write( irc, ":%s!%s@%s MODE %s :%s", irc->user->nick, |
---|
| 686 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 687 | changes ); |
---|
| 688 | } |
---|
| 689 | |
---|
[b7d3cc34] | 690 | |
---|
| 691 | |
---|
[3ddb7477] | 692 | static char *set_eval_charset( set_t *set, char *value ) |
---|
[b7d3cc34] | 693 | { |
---|
[3ddb7477] | 694 | irc_t *irc = set->data; |
---|
| 695 | GIConv ic, oc; |
---|
[b7d3cc34] | 696 | |
---|
[3ddb7477] | 697 | if( g_strcasecmp( value, "none" ) == 0 ) |
---|
| 698 | value = g_strdup( "utf-8" ); |
---|
[b7d3cc34] | 699 | |
---|
[3ddb7477] | 700 | if( ( ic = g_iconv_open( "utf-8", value ) ) == (GIConv) -1 ) |
---|
[b7d3cc34] | 701 | { |
---|
[3ddb7477] | 702 | return NULL; |
---|
[b7d3cc34] | 703 | } |
---|
[3ddb7477] | 704 | if( ( oc = g_iconv_open( value, "utf-8" ) ) == (GIConv) -1 ) |
---|
[b7d3cc34] | 705 | { |
---|
[3ddb7477] | 706 | g_iconv_close( ic ); |
---|
| 707 | return NULL; |
---|
[b7d3cc34] | 708 | } |
---|
| 709 | |
---|
[3ddb7477] | 710 | if( irc->iconv != (GIConv) -1 ) |
---|
| 711 | g_iconv_close( irc->iconv ); |
---|
| 712 | if( irc->oconv != (GIConv) -1 ) |
---|
| 713 | g_iconv_close( irc->oconv ); |
---|
[b7d3cc34] | 714 | |
---|
[3ddb7477] | 715 | irc->iconv = ic; |
---|
| 716 | irc->oconv = oc; |
---|
[0e7ab64] | 717 | |
---|
[3ddb7477] | 718 | return value; |
---|
[0e7ab64] | 719 | } |
---|