[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 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" |
---|
[fb117aee] | 27 | #include "ipc.h" |
---|
[2ff2076] | 28 | #include "dcc.h" |
---|
[59e66ff] | 29 | #include "lib/ssl_client.h" |
---|
[b7d3cc34] | 30 | |
---|
[3ddb7477] | 31 | GSList *irc_connection_list; |
---|
[0c85c08] | 32 | GSList *irc_plugins; |
---|
[b7d3cc34] | 33 | |
---|
[3923003] | 34 | static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond ); |
---|
[3ddb7477] | 35 | static char *set_eval_charset( set_t *set, char *value ); |
---|
[0d9d53e] | 36 | static char *set_eval_password( set_t *set, char *value ); |
---|
[6d8cc05] | 37 | static char *set_eval_bw_compat( set_t *set, char *value ); |
---|
[c608891] | 38 | static char *set_eval_utf8_nicks( set_t *set, char *value ); |
---|
[58adb7e] | 39 | |
---|
[b7d3cc34] | 40 | irc_t *irc_new( int fd ) |
---|
| 41 | { |
---|
[e4d6271] | 42 | irc_t *irc; |
---|
[e9b755e] | 43 | struct sockaddr_storage sock; |
---|
[7435ccf] | 44 | socklen_t socklen = sizeof( sock ); |
---|
[3ddb7477] | 45 | char *host = NULL, *myhost = NULL; |
---|
| 46 | irc_user_t *iu; |
---|
[0c85c08] | 47 | GSList *l; |
---|
[7125cb3] | 48 | set_t *s; |
---|
[3ddb7477] | 49 | bee_t *b; |
---|
[e4d6271] | 50 | |
---|
| 51 | irc = g_new0( irc_t, 1 ); |
---|
[b7d3cc34] | 52 | |
---|
| 53 | irc->fd = fd; |
---|
[a0d04d6] | 54 | sock_make_nonblocking( irc->fd ); |
---|
| 55 | |
---|
[e046390] | 56 | irc->r_watch_source_id = b_input_add( irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc ); |
---|
[b7d3cc34] | 57 | |
---|
| 58 | irc->status = USTATUS_OFFLINE; |
---|
| 59 | irc->last_pong = gettime(); |
---|
| 60 | |
---|
[3ddb7477] | 61 | irc->nick_user_hash = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
[b7d3cc34] | 62 | irc->watches = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
| 63 | |
---|
[f9756bd] | 64 | irc->iconv = (GIConv) -1; |
---|
| 65 | irc->oconv = (GIConv) -1; |
---|
| 66 | |
---|
[b7d3cc34] | 67 | if( global.conf->hostname ) |
---|
| 68 | { |
---|
[3ddb7477] | 69 | myhost = g_strdup( global.conf->hostname ); |
---|
[b7d3cc34] | 70 | } |
---|
[7435ccf] | 71 | else if( getsockname( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 72 | { |
---|
[2231302] | 73 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 74 | |
---|
[2231302] | 75 | if( getnameinfo( (struct sockaddr *) &sock, socklen, buf, |
---|
[c84e31a] | 76 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
[2231302] | 77 | { |
---|
[3ddb7477] | 78 | myhost = g_strdup( ipv6_unwrap( buf ) ); |
---|
[2231302] | 79 | } |
---|
[b7d3cc34] | 80 | } |
---|
| 81 | |
---|
[7435ccf] | 82 | if( getpeername( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 83 | { |
---|
[2231302] | 84 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 85 | |
---|
[2231302] | 86 | if( getnameinfo( (struct sockaddr *)&sock, socklen, buf, |
---|
[c84e31a] | 87 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
[2231302] | 88 | { |
---|
[3ddb7477] | 89 | host = g_strdup( ipv6_unwrap( buf ) ); |
---|
[2231302] | 90 | } |
---|
[b7d3cc34] | 91 | } |
---|
| 92 | |
---|
[3ddb7477] | 93 | if( host == NULL ) |
---|
| 94 | host = g_strdup( "localhost.localdomain" ); |
---|
| 95 | if( myhost == NULL ) |
---|
| 96 | myhost = g_strdup( "localhost.localdomain" ); |
---|
[3e1e11af] | 97 | |
---|
[3923003] | 98 | if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 ) |
---|
| 99 | irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc ); |
---|
[b7d3cc34] | 100 | |
---|
| 101 | irc_connection_list = g_slist_append( irc_connection_list, irc ); |
---|
| 102 | |
---|
[3ddb7477] | 103 | b = irc->b = bee_new(); |
---|
[d860a8d] | 104 | b->ui_data = irc; |
---|
| 105 | b->ui = &irc_ui_funcs; |
---|
[3ddb7477] | 106 | |
---|
[af9f2ca] | 107 | s = set_add( &b->set, "allow_takeover", "true", set_eval_bool, irc ); |
---|
[6d8cc05] | 108 | s = set_add( &b->set, "away_devoice", "true", set_eval_bw_compat, irc ); |
---|
[a758ec1] | 109 | s->flags |= SET_HIDDEN; |
---|
[1c8e5f7] | 110 | s = set_add( &b->set, "away_reply_timeout", "3600", set_eval_int, irc ); |
---|
[3ddb7477] | 111 | s = set_add( &b->set, "charset", "utf-8", set_eval_charset, irc ); |
---|
| 112 | s = set_add( &b->set, "default_target", "root", NULL, irc ); |
---|
| 113 | s = set_add( &b->set, "display_namechanges", "false", set_eval_bool, irc ); |
---|
[21c87a7] | 114 | s = set_add( &b->set, "display_timestamps", "true", set_eval_bool, irc ); |
---|
[16834a5] | 115 | s = set_add( &b->set, "handle_unknown", "add_channel", NULL, irc ); |
---|
[8d93b4a] | 116 | s = set_add( &b->set, "last_version", "0", NULL, irc ); |
---|
[180ab31] | 117 | s->flags |= SET_HIDDEN; |
---|
[3ddb7477] | 118 | s = set_add( &b->set, "lcnicks", "true", set_eval_bool, irc ); |
---|
[09dfb68] | 119 | s = set_add( &b->set, "nick_format", "%-@nick", NULL, irc ); |
---|
[0bd948e] | 120 | s = set_add( &b->set, "offline_user_quits", "true", set_eval_bool, irc ); |
---|
[c5aefa4] | 121 | s = set_add( &b->set, "ops", "both", set_eval_irc_channel_ops, irc ); |
---|
[88eaf4b] | 122 | s = set_add( &b->set, "paste_buffer", "false", set_eval_bool, irc ); |
---|
| 123 | s->old_key = g_strdup( "buddy_sendbuffer" ); |
---|
| 124 | s = set_add( &b->set, "paste_buffer_delay", "200", set_eval_int, irc ); |
---|
| 125 | s->old_key = g_strdup( "buddy_sendbuffer_delay" ); |
---|
[0d9d53e] | 126 | s = set_add( &b->set, "password", NULL, set_eval_password, irc ); |
---|
[09d4922] | 127 | s->flags |= SET_NULL_OK | SET_PASSWORD; |
---|
[3ddb7477] | 128 | s = set_add( &b->set, "private", "true", set_eval_bool, irc ); |
---|
| 129 | s = set_add( &b->set, "query_order", "lifo", NULL, irc ); |
---|
[0a6e5d1] | 130 | s = set_add( &b->set, "root_nick", ROOT_NICK, set_eval_root_nick, irc ); |
---|
[180ab31] | 131 | s->flags |= SET_HIDDEN; |
---|
[6d8cc05] | 132 | s = set_add( &b->set, "show_offline", "false", set_eval_bw_compat, irc ); |
---|
[a758ec1] | 133 | s->flags |= SET_HIDDEN; |
---|
[3ddb7477] | 134 | s = set_add( &b->set, "simulate_netsplit", "true", set_eval_bool, irc ); |
---|
[21c87a7] | 135 | s = set_add( &b->set, "timezone", "local", set_eval_timezone, irc ); |
---|
[3ddb7477] | 136 | s = set_add( &b->set, "to_char", ": ", set_eval_to_char, irc ); |
---|
| 137 | s = set_add( &b->set, "typing_notice", "false", set_eval_bool, irc ); |
---|
[c608891] | 138 | s = set_add( &b->set, "utf8_nicks", "false", set_eval_utf8_nicks, irc ); |
---|
[3ddb7477] | 139 | |
---|
| 140 | irc->root = iu = irc_user_new( irc, ROOT_NICK ); |
---|
| 141 | iu->host = g_strdup( myhost ); |
---|
| 142 | iu->fullname = g_strdup( ROOT_FN ); |
---|
[280c56a] | 143 | iu->f = &irc_user_root_funcs; |
---|
[3ddb7477] | 144 | |
---|
| 145 | iu = irc_user_new( irc, NS_NICK ); |
---|
| 146 | iu->host = g_strdup( myhost ); |
---|
| 147 | iu->fullname = g_strdup( ROOT_FN ); |
---|
[280c56a] | 148 | iu->f = &irc_user_root_funcs; |
---|
[3ddb7477] | 149 | |
---|
| 150 | irc->user = g_new0( irc_user_t, 1 ); |
---|
| 151 | irc->user->host = g_strdup( host ); |
---|
| 152 | |
---|
[ebaebfe] | 153 | conf_loaddefaults( irc ); |
---|
[b7d3cc34] | 154 | |
---|
[f9756bd] | 155 | /* Evaluator sets the iconv/oconv structures. */ |
---|
[3ddb7477] | 156 | set_eval_charset( set_find( &b->set, "charset" ), set_getstr( &b->set, "charset" ) ); |
---|
[f9756bd] | 157 | |
---|
[3ddb7477] | 158 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, "BitlBee-IRCd initialized, please go on" ); |
---|
[b925666] | 159 | if( isatty( irc->fd ) ) |
---|
| 160 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, |
---|
| 161 | "If you read this, you most likely accidentally " |
---|
| 162 | "started BitlBee in inetd mode on the command line. " |
---|
| 163 | "You probably want to run it in (Fork)Daemon mode. " |
---|
| 164 | "See doc/README for more information." ); |
---|
[3ddb7477] | 165 | |
---|
[ebaebfe] | 166 | g_free( myhost ); |
---|
| 167 | g_free( host ); |
---|
| 168 | |
---|
[3fc6c32] | 169 | /* libpurple doesn't like fork()s after initializing itself, so this |
---|
| 170 | is the right moment to initialize it. */ |
---|
| 171 | #ifdef WITH_PURPLE |
---|
[5674207] | 172 | nogaim_init(); |
---|
[3fc6c32] | 173 | #endif |
---|
[59e66ff] | 174 | |
---|
| 175 | /* SSL library initialization also should be done after the fork, to |
---|
| 176 | avoid shared CSPRNG state. This is required by NSS, which refuses to |
---|
| 177 | work if a fork is detected */ |
---|
| 178 | ssl_init(); |
---|
[5674207] | 179 | |
---|
[0c85c08] | 180 | for( l = irc_plugins; l; l = l->next ) |
---|
| 181 | { |
---|
| 182 | irc_plugin_t *p = l->data; |
---|
| 183 | if( p->irc_new ) |
---|
| 184 | p->irc_new( irc ); |
---|
| 185 | } |
---|
| 186 | |
---|
[3ddb7477] | 187 | return irc; |
---|
[b7d3cc34] | 188 | } |
---|
| 189 | |
---|
[f73b969] | 190 | /* immed=1 makes this function pretty much equal to irc_free(), except that |
---|
| 191 | this one will "log". In case the connection is already broken and we |
---|
| 192 | shouldn't try to write to it. */ |
---|
[fc50d48] | 193 | void irc_abort( irc_t *irc, int immed, char *format, ... ) |
---|
[c1826c6] | 194 | { |
---|
[82ca986] | 195 | char *reason = NULL; |
---|
| 196 | |
---|
[fc50d48] | 197 | if( format != NULL ) |
---|
| 198 | { |
---|
[f73b969] | 199 | va_list params; |
---|
[fc50d48] | 200 | |
---|
| 201 | va_start( params, format ); |
---|
[f73b969] | 202 | reason = g_strdup_vprintf( format, params ); |
---|
[fc50d48] | 203 | va_end( params ); |
---|
| 204 | } |
---|
| 205 | |
---|
[e1aaea4] | 206 | if( reason ) |
---|
| 207 | irc_write( irc, "ERROR :Closing link: %s", reason ); |
---|
[82ca986] | 208 | |
---|
| 209 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
| 210 | irc->user->nick ? irc->user->nick : "(NONE)", |
---|
| 211 | irc->user->host, reason ? : "" ); |
---|
| 212 | |
---|
| 213 | g_free( reason ); |
---|
| 214 | |
---|
| 215 | irc_flush( irc ); |
---|
| 216 | if( immed ) |
---|
[c1826c6] | 217 | { |
---|
[82ca986] | 218 | irc_free( irc ); |
---|
[c1826c6] | 219 | } |
---|
| 220 | else |
---|
| 221 | { |
---|
[82ca986] | 222 | b_event_remove( irc->ping_source_id ); |
---|
| 223 | irc->ping_source_id = b_timeout_add( 1, (b_event_handler) irc_free, irc ); |
---|
[c1826c6] | 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
[3ddb7477] | 227 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ); |
---|
[b7d3cc34] | 228 | |
---|
[fa75134] | 229 | void irc_free( irc_t * irc ) |
---|
[b7d3cc34] | 230 | { |
---|
[0c85c08] | 231 | GSList *l; |
---|
| 232 | |
---|
[82ca986] | 233 | irc->status |= USTATUS_SHUTDOWN; |
---|
| 234 | |
---|
[b7d3cc34] | 235 | log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd ); |
---|
| 236 | |
---|
[51e9a1d] | 237 | if( irc->status & USTATUS_IDENTIFIED && set_getbool( &irc->b->set, "save_on_quit" ) ) |
---|
| 238 | if( storage_save( irc, NULL, TRUE ) != STORAGE_OK ) |
---|
| 239 | log_message( LOGLVL_WARNING, "Error while saving settings for user %s", irc->user->nick ); |
---|
| 240 | |
---|
[0c85c08] | 241 | for( l = irc_plugins; l; l = l->next ) |
---|
| 242 | { |
---|
| 243 | irc_plugin_t *p = l->data; |
---|
| 244 | if( p->irc_free ) |
---|
| 245 | p->irc_free( irc ); |
---|
| 246 | } |
---|
| 247 | |
---|
[b7d3cc34] | 248 | irc_connection_list = g_slist_remove( irc_connection_list, irc ); |
---|
| 249 | |
---|
[fa75134] | 250 | while( irc->queries != NULL ) |
---|
| 251 | query_del( irc, irc->queries ); |
---|
[d33679e] | 252 | |
---|
| 253 | /* This is a little bit messy: bee_free() frees all b->users which |
---|
| 254 | calls us back to free the corresponding irc->users. So do this |
---|
| 255 | before we clear the remaining ones ourselves. */ |
---|
| 256 | bee_free( irc->b ); |
---|
[5b52a48] | 257 | |
---|
[3ddb7477] | 258 | while( irc->users ) |
---|
[eabc9d2] | 259 | irc_user_free( irc, (irc_user_t *) irc->users->data ); |
---|
[b7d3cc34] | 260 | |
---|
[63a520b] | 261 | while( irc->channels ) |
---|
| 262 | irc_channel_free( irc->channels->data ); |
---|
| 263 | |
---|
[fa75134] | 264 | if( irc->ping_source_id > 0 ) |
---|
| 265 | b_event_remove( irc->ping_source_id ); |
---|
[883a398] | 266 | if( irc->r_watch_source_id > 0 ) |
---|
| 267 | b_event_remove( irc->r_watch_source_id ); |
---|
[fa75134] | 268 | if( irc->w_watch_source_id > 0 ) |
---|
| 269 | b_event_remove( irc->w_watch_source_id ); |
---|
| 270 | |
---|
| 271 | closesocket( irc->fd ); |
---|
| 272 | irc->fd = -1; |
---|
| 273 | |
---|
[3ddb7477] | 274 | g_hash_table_foreach_remove( irc->nick_user_hash, irc_free_hashkey, NULL ); |
---|
| 275 | g_hash_table_destroy( irc->nick_user_hash ); |
---|
[b7d3cc34] | 276 | |
---|
[fa75134] | 277 | g_hash_table_foreach_remove( irc->watches, irc_free_hashkey, NULL ); |
---|
| 278 | g_hash_table_destroy( irc->watches ); |
---|
[b7d3cc34] | 279 | |
---|
[f9756bd] | 280 | if( irc->iconv != (GIConv) -1 ) |
---|
| 281 | g_iconv_close( irc->iconv ); |
---|
| 282 | if( irc->oconv != (GIConv) -1 ) |
---|
| 283 | g_iconv_close( irc->oconv ); |
---|
| 284 | |
---|
[fa75134] | 285 | g_free( irc->sendbuffer ); |
---|
| 286 | g_free( irc->readbuffer ); |
---|
| 287 | g_free( irc->password ); |
---|
| 288 | |
---|
| 289 | g_free( irc ); |
---|
[b7d3cc34] | 290 | |
---|
[565a1ea] | 291 | if( global.conf->runmode == RUNMODE_INETD || |
---|
| 292 | global.conf->runmode == RUNMODE_FORKDAEMON || |
---|
| 293 | ( global.conf->runmode == RUNMODE_DAEMON && |
---|
| 294 | global.listen_socket == -1 && |
---|
| 295 | irc_connection_list == NULL ) ) |
---|
[ba9edaa] | 296 | b_main_quit(); |
---|
[b7d3cc34] | 297 | } |
---|
| 298 | |
---|
[3ddb7477] | 299 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ) |
---|
[7cad7b4] | 300 | { |
---|
[3ddb7477] | 301 | g_free( key ); |
---|
[7cad7b4] | 302 | |
---|
[3ddb7477] | 303 | return( TRUE ); |
---|
[7cad7b4] | 304 | } |
---|
| 305 | |
---|
[1f92a58] | 306 | /* USE WITH CAUTION! |
---|
| 307 | Sets pass without checking */ |
---|
| 308 | void irc_setpass (irc_t *irc, const char *pass) |
---|
| 309 | { |
---|
| 310 | g_free (irc->password); |
---|
| 311 | |
---|
| 312 | if (pass) { |
---|
| 313 | irc->password = g_strdup (pass); |
---|
| 314 | } else { |
---|
| 315 | irc->password = NULL; |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
[0d9d53e] | 319 | static char *set_eval_password( set_t *set, char *value ) |
---|
| 320 | { |
---|
| 321 | irc_t *irc = set->data; |
---|
| 322 | |
---|
| 323 | if( irc->status & USTATUS_IDENTIFIED && value ) |
---|
| 324 | { |
---|
| 325 | irc_setpass( irc, value ); |
---|
| 326 | return NULL; |
---|
| 327 | } |
---|
| 328 | else |
---|
| 329 | { |
---|
| 330 | return SET_INVALID; |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
[3ddb7477] | 334 | static char **irc_splitlines( char *buffer ); |
---|
| 335 | |
---|
[f73b969] | 336 | void irc_process( irc_t *irc ) |
---|
[b7d3cc34] | 337 | { |
---|
[f9756bd] | 338 | char **lines, *temp, **cmd; |
---|
[b7d3cc34] | 339 | int i; |
---|
| 340 | |
---|
[de3e100] | 341 | if( irc->readbuffer != NULL ) |
---|
| 342 | { |
---|
[3ddb7477] | 343 | lines = irc_splitlines( irc->readbuffer ); |
---|
[de3e100] | 344 | |
---|
| 345 | for( i = 0; *lines[i] != '\0'; i ++ ) |
---|
| 346 | { |
---|
[f9756bd] | 347 | char *conv = NULL; |
---|
[7d31002] | 348 | |
---|
[18ff38f] | 349 | /* [WvG] If the last line isn't empty, it's an incomplete line and we |
---|
| 350 | should wait for the rest to come in before processing it. */ |
---|
[de3e100] | 351 | if( lines[i+1] == NULL ) |
---|
| 352 | { |
---|
[b7d3cc34] | 353 | temp = g_strdup( lines[i] ); |
---|
| 354 | g_free( irc->readbuffer ); |
---|
| 355 | irc->readbuffer = temp; |
---|
[de3e100] | 356 | i ++; |
---|
[b7d3cc34] | 357 | break; |
---|
[e27661d] | 358 | } |
---|
| 359 | |
---|
[f9756bd] | 360 | if( irc->iconv != (GIConv) -1 ) |
---|
[e27661d] | 361 | { |
---|
[f9756bd] | 362 | gsize bytes_read, bytes_written; |
---|
| 363 | |
---|
| 364 | conv = g_convert_with_iconv( lines[i], -1, irc->iconv, |
---|
| 365 | &bytes_read, &bytes_written, NULL ); |
---|
| 366 | |
---|
| 367 | if( conv == NULL || bytes_read != strlen( lines[i] ) ) |
---|
[94d52d64] | 368 | { |
---|
[fc0cf92] | 369 | /* GLib can do strange things if things are not in the expected charset, |
---|
| 370 | so let's be a little bit paranoid here: */ |
---|
[94d52d64] | 371 | if( irc->status & USTATUS_LOGGED_IN ) |
---|
[fc0cf92] | 372 | { |
---|
[e67e513] | 373 | irc_rootmsg( irc, "Error: Charset mismatch detected. The charset " |
---|
[94d52d64] | 374 | "setting is currently set to %s, so please make " |
---|
| 375 | "sure your IRC client will send and accept text in " |
---|
| 376 | "that charset, or tell BitlBee which charset to " |
---|
| 377 | "expect by changing the charset setting. See " |
---|
| 378 | "`help set charset' for more information. Your " |
---|
[f9756bd] | 379 | "message was ignored.", |
---|
[3ddb7477] | 380 | set_getstr( &irc->b->set, "charset" ) ); |
---|
[f9756bd] | 381 | |
---|
| 382 | g_free( conv ); |
---|
| 383 | conv = NULL; |
---|
[fc0cf92] | 384 | } |
---|
| 385 | else |
---|
| 386 | { |
---|
[3ddb7477] | 387 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, |
---|
[a83442a] | 388 | "Warning: invalid characters received at login time." ); |
---|
[fc0cf92] | 389 | |
---|
[f9756bd] | 390 | conv = g_strdup( lines[i] ); |
---|
[fc0cf92] | 391 | for( temp = conv; *temp; temp ++ ) |
---|
| 392 | if( *temp & 0x80 ) |
---|
| 393 | *temp = '?'; |
---|
| 394 | } |
---|
[94d52d64] | 395 | } |
---|
| 396 | lines[i] = conv; |
---|
[e27661d] | 397 | } |
---|
[de3e100] | 398 | |
---|
[e1720ce] | 399 | if( lines[i] && ( cmd = irc_parse_line( lines[i] ) ) ) |
---|
[f9756bd] | 400 | { |
---|
| 401 | irc_exec( irc, cmd ); |
---|
| 402 | g_free( cmd ); |
---|
| 403 | } |
---|
[f73b969] | 404 | |
---|
[f9756bd] | 405 | g_free( conv ); |
---|
[f73b969] | 406 | |
---|
| 407 | /* Shouldn't really happen, but just in case... */ |
---|
| 408 | if( !g_slist_find( irc_connection_list, irc ) ) |
---|
[de3e100] | 409 | { |
---|
[b7d3cc34] | 410 | g_free( lines ); |
---|
[f73b969] | 411 | return; |
---|
[b7d3cc34] | 412 | } |
---|
| 413 | } |
---|
[de3e100] | 414 | |
---|
| 415 | if( lines[i] != NULL ) |
---|
| 416 | { |
---|
| 417 | g_free( irc->readbuffer ); |
---|
[0431ea1] | 418 | irc->readbuffer = NULL; |
---|
[b7d3cc34] | 419 | } |
---|
[de3e100] | 420 | |
---|
[b7d3cc34] | 421 | g_free( lines ); |
---|
| 422 | } |
---|
| 423 | } |
---|
| 424 | |
---|
[3ddb7477] | 425 | /* Splits a long string into separate lines. The array is NULL-terminated |
---|
| 426 | and, unless the string contains an incomplete line at the end, ends with |
---|
| 427 | an empty string. Could use g_strsplit() but this one does it in-place. |
---|
| 428 | (So yes, it's destructive.) */ |
---|
| 429 | static char **irc_splitlines( char *buffer ) |
---|
[b7d3cc34] | 430 | { |
---|
[18ff38f] | 431 | int i, j, n = 3; |
---|
[b7d3cc34] | 432 | char **lines; |
---|
| 433 | |
---|
[18ff38f] | 434 | /* Allocate n+1 elements. */ |
---|
| 435 | lines = g_new( char *, n + 1 ); |
---|
[b7d3cc34] | 436 | |
---|
[de3e100] | 437 | lines[0] = buffer; |
---|
[b7d3cc34] | 438 | |
---|
[18ff38f] | 439 | /* Split the buffer in several strings, and accept any kind of line endings, |
---|
| 440 | * knowing that ERC on Windows may send something interesting like \r\r\n, |
---|
| 441 | * and surely there must be clients that think just \n is enough... */ |
---|
| 442 | for( i = 0, j = 0; buffer[i] != '\0'; i ++ ) |
---|
[de3e100] | 443 | { |
---|
[18ff38f] | 444 | if( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
[de3e100] | 445 | { |
---|
[18ff38f] | 446 | while( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
| 447 | buffer[i++] = '\0'; |
---|
| 448 | |
---|
| 449 | lines[++j] = buffer + i; |
---|
[de3e100] | 450 | |
---|
[18ff38f] | 451 | if( j >= n ) |
---|
| 452 | { |
---|
| 453 | n *= 2; |
---|
| 454 | lines = g_renew( char *, lines, n + 1 ); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | if( buffer[i] == '\0' ) |
---|
| 458 | break; |
---|
[b7d3cc34] | 459 | } |
---|
| 460 | } |
---|
[de3e100] | 461 | |
---|
[18ff38f] | 462 | /* NULL terminate our list. */ |
---|
| 463 | lines[++j] = NULL; |
---|
| 464 | |
---|
| 465 | return lines; |
---|
[b7d3cc34] | 466 | } |
---|
| 467 | |
---|
[e27661d] | 468 | /* Split an IRC-style line into little parts/arguments. */ |
---|
[0431ea1] | 469 | char **irc_parse_line( char *line ) |
---|
[b7d3cc34] | 470 | { |
---|
| 471 | int i, j; |
---|
| 472 | char **cmd; |
---|
| 473 | |
---|
| 474 | /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ |
---|
[de3e100] | 475 | if( line[0] == ':' ) |
---|
| 476 | { |
---|
[e1720ce] | 477 | for( i = 0; line[i] && line[i] != ' '; i ++ ); |
---|
[de3e100] | 478 | line = line + i; |
---|
[b7d3cc34] | 479 | } |
---|
[de3e100] | 480 | for( i = 0; line[i] == ' '; i ++ ); |
---|
| 481 | line = line + i; |
---|
| 482 | |
---|
[b7d3cc34] | 483 | /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ |
---|
[de3e100] | 484 | if( line[0] == '\0') |
---|
| 485 | return NULL; |
---|
| 486 | |
---|
| 487 | /* Count the number of char **cmd elements we're going to need. */ |
---|
| 488 | j = 1; |
---|
| 489 | for( i = 0; line[i] != '\0'; i ++ ) |
---|
| 490 | { |
---|
| 491 | if( line[i] == ' ' ) |
---|
| 492 | { |
---|
| 493 | j ++; |
---|
[b7d3cc34] | 494 | |
---|
[de3e100] | 495 | if( line[i+1] == ':' ) |
---|
| 496 | break; |
---|
| 497 | } |
---|
[b7d3cc34] | 498 | } |
---|
| 499 | |
---|
| 500 | /* Allocate the space we need. */ |
---|
[de3e100] | 501 | cmd = g_new( char *, j + 1 ); |
---|
| 502 | cmd[j] = NULL; |
---|
[b7d3cc34] | 503 | |
---|
| 504 | /* Do the actual line splitting, format is: |
---|
| 505 | * Input: "PRIVMSG #bitlbee :foo bar" |
---|
| 506 | * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL |
---|
| 507 | */ |
---|
| 508 | |
---|
[de3e100] | 509 | cmd[0] = line; |
---|
| 510 | for( i = 0, j = 0; line[i] != '\0'; i ++ ) |
---|
[b7d3cc34] | 511 | { |
---|
[de3e100] | 512 | if( line[i] == ' ' ) |
---|
[b7d3cc34] | 513 | { |
---|
[de3e100] | 514 | line[i] = '\0'; |
---|
| 515 | cmd[++j] = line + i + 1; |
---|
[b7d3cc34] | 516 | |
---|
[de3e100] | 517 | if( line[i+1] == ':' ) |
---|
[b7d3cc34] | 518 | { |
---|
[de3e100] | 519 | cmd[j] ++; |
---|
[b7d3cc34] | 520 | break; |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | } |
---|
| 524 | |
---|
[de3e100] | 525 | return cmd; |
---|
[b7d3cc34] | 526 | } |
---|
| 527 | |
---|
[e27661d] | 528 | /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ |
---|
[74c119d] | 529 | char *irc_build_line( char **cmd ) |
---|
| 530 | { |
---|
| 531 | int i, len; |
---|
| 532 | char *s; |
---|
[b7d3cc34] | 533 | |
---|
[74c119d] | 534 | if( cmd[0] == NULL ) |
---|
| 535 | return NULL; |
---|
[b7d3cc34] | 536 | |
---|
[74c119d] | 537 | len = 1; |
---|
| 538 | for( i = 0; cmd[i]; i ++ ) |
---|
| 539 | len += strlen( cmd[i] ) + 1; |
---|
| 540 | |
---|
| 541 | if( strchr( cmd[i-1], ' ' ) != NULL ) |
---|
| 542 | len ++; |
---|
| 543 | |
---|
| 544 | s = g_new0( char, len + 1 ); |
---|
| 545 | for( i = 0; cmd[i]; i ++ ) |
---|
[b7d3cc34] | 546 | { |
---|
[74c119d] | 547 | if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) |
---|
| 548 | strcat( s, ":" ); |
---|
[b7d3cc34] | 549 | |
---|
[74c119d] | 550 | strcat( s, cmd[i] ); |
---|
[b7d3cc34] | 551 | |
---|
[74c119d] | 552 | if( cmd[i+1] ) |
---|
| 553 | strcat( s, " " ); |
---|
[b7d3cc34] | 554 | } |
---|
[74c119d] | 555 | strcat( s, "\r\n" ); |
---|
[b7d3cc34] | 556 | |
---|
[74c119d] | 557 | return s; |
---|
[b7d3cc34] | 558 | } |
---|
| 559 | |
---|
[3ddb7477] | 560 | void irc_write( irc_t *irc, char *format, ... ) |
---|
[b7d3cc34] | 561 | { |
---|
| 562 | va_list params; |
---|
[3ddb7477] | 563 | |
---|
[b7d3cc34] | 564 | va_start( params, format ); |
---|
[3ddb7477] | 565 | irc_vawrite( irc, format, params ); |
---|
[b7d3cc34] | 566 | va_end( params ); |
---|
[3ddb7477] | 567 | |
---|
[b7d3cc34] | 568 | return; |
---|
| 569 | } |
---|
| 570 | |
---|
[3ddb7477] | 571 | void irc_write_all( int now, char *format, ... ) |
---|
[b7d3cc34] | 572 | { |
---|
| 573 | va_list params; |
---|
[3ddb7477] | 574 | GSList *temp; |
---|
[b7d3cc34] | 575 | |
---|
| 576 | va_start( params, format ); |
---|
| 577 | |
---|
[3ddb7477] | 578 | temp = irc_connection_list; |
---|
| 579 | while( temp != NULL ) |
---|
| 580 | { |
---|
| 581 | irc_t *irc = temp->data; |
---|
| 582 | |
---|
| 583 | if( now ) |
---|
| 584 | { |
---|
| 585 | g_free( irc->sendbuffer ); |
---|
| 586 | irc->sendbuffer = g_strdup( "\r\n" ); |
---|
| 587 | } |
---|
| 588 | irc_vawrite( temp->data, format, params ); |
---|
| 589 | if( now ) |
---|
| 590 | { |
---|
[4aa0f6b] | 591 | bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ); |
---|
[3ddb7477] | 592 | } |
---|
| 593 | temp = temp->next; |
---|
| 594 | } |
---|
| 595 | |
---|
[b7d3cc34] | 596 | va_end( params ); |
---|
| 597 | return; |
---|
[3ddb7477] | 598 | } |
---|
[b7d3cc34] | 599 | |
---|
| 600 | void irc_vawrite( irc_t *irc, char *format, va_list params ) |
---|
| 601 | { |
---|
| 602 | int size; |
---|
[f9756bd] | 603 | char line[IRC_MAX_LINE+1]; |
---|
[d783e48] | 604 | |
---|
[0356ae3] | 605 | /* Don't try to write anything new anymore when shutting down. */ |
---|
[5898ef8] | 606 | if( irc->status & USTATUS_SHUTDOWN ) |
---|
[b7d3cc34] | 607 | return; |
---|
[d783e48] | 608 | |
---|
[f9756bd] | 609 | memset( line, 0, sizeof( line ) ); |
---|
[d783e48] | 610 | g_vsnprintf( line, IRC_MAX_LINE - 2, format, params ); |
---|
[b7d3cc34] | 611 | strip_newlines( line ); |
---|
[f9756bd] | 612 | |
---|
| 613 | if( irc->oconv != (GIConv) -1 ) |
---|
[d783e48] | 614 | { |
---|
[f9756bd] | 615 | gsize bytes_read, bytes_written; |
---|
| 616 | char *conv; |
---|
| 617 | |
---|
| 618 | conv = g_convert_with_iconv( line, -1, irc->oconv, |
---|
| 619 | &bytes_read, &bytes_written, NULL ); |
---|
| 620 | |
---|
| 621 | if( bytes_read == strlen( line ) ) |
---|
| 622 | strncpy( line, conv, IRC_MAX_LINE - 2 ); |
---|
[d783e48] | 623 | |
---|
[f9756bd] | 624 | g_free( conv ); |
---|
[d783e48] | 625 | } |
---|
[f9756bd] | 626 | g_strlcat( line, "\r\n", IRC_MAX_LINE + 1 ); |
---|
[d783e48] | 627 | |
---|
[a0d04d6] | 628 | if( irc->sendbuffer != NULL ) |
---|
| 629 | { |
---|
[b7d3cc34] | 630 | size = strlen( irc->sendbuffer ) + strlen( line ); |
---|
| 631 | irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 ); |
---|
| 632 | strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line ); |
---|
| 633 | } |
---|
[a0d04d6] | 634 | else |
---|
[b7d3cc34] | 635 | { |
---|
[a0d04d6] | 636 | irc->sendbuffer = g_strdup(line); |
---|
[b7d3cc34] | 637 | } |
---|
| 638 | |
---|
[a0d04d6] | 639 | if( irc->w_watch_source_id == 0 ) |
---|
[0356ae3] | 640 | { |
---|
| 641 | /* If the buffer is empty we can probably write, so call the write event handler |
---|
| 642 | immediately. If it returns TRUE, it should be called again, so add the event to |
---|
| 643 | the queue. If it's FALSE, we emptied the buffer and saved ourselves some work |
---|
| 644 | in the event queue. */ |
---|
[bbb6ffb] | 645 | /* Really can't be done as long as the code doesn't do error checking very well: |
---|
[e046390] | 646 | if( bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ) ) */ |
---|
[bbb6ffb] | 647 | |
---|
| 648 | /* So just always do it via the event handler. */ |
---|
[e046390] | 649 | irc->w_watch_source_id = b_input_add( irc->fd, B_EV_IO_WRITE, bitlbee_io_current_client_write, irc ); |
---|
[0356ae3] | 650 | } |
---|
[a0d04d6] | 651 | |
---|
[b7d3cc34] | 652 | return; |
---|
| 653 | } |
---|
| 654 | |
---|
[f1c2b21] | 655 | /* Flush sendbuffer if you can. If it fails, fail silently and let some |
---|
| 656 | I/O event handler clean up. */ |
---|
| 657 | void irc_flush( irc_t *irc ) |
---|
| 658 | { |
---|
| 659 | ssize_t n; |
---|
| 660 | size_t len; |
---|
| 661 | |
---|
| 662 | if( irc->sendbuffer == NULL ) |
---|
| 663 | return; |
---|
| 664 | |
---|
| 665 | len = strlen( irc->sendbuffer ); |
---|
| 666 | if( ( n = send( irc->fd, irc->sendbuffer, len, 0 ) ) == len ) |
---|
| 667 | { |
---|
| 668 | g_free( irc->sendbuffer ); |
---|
| 669 | irc->sendbuffer = NULL; |
---|
| 670 | |
---|
| 671 | b_event_remove( irc->w_watch_source_id ); |
---|
| 672 | irc->w_watch_source_id = 0; |
---|
| 673 | } |
---|
| 674 | else if( n > 0 ) |
---|
| 675 | { |
---|
| 676 | char *s = g_strdup( irc->sendbuffer + n ); |
---|
| 677 | g_free( irc->sendbuffer ); |
---|
| 678 | irc->sendbuffer = s; |
---|
| 679 | } |
---|
| 680 | /* Otherwise something went wrong and we don't currently care |
---|
| 681 | what the error was. We may or may not succeed later, we |
---|
| 682 | were just trying to flush the buffer immediately. */ |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | /* Meant for takeover functionality. Transfer an IRC connection to a different |
---|
| 686 | socket. */ |
---|
| 687 | void irc_switch_fd( irc_t *irc, int fd ) |
---|
| 688 | { |
---|
| 689 | irc_write( irc, "ERROR :Transferring session to a new connection" ); |
---|
| 690 | irc_flush( irc ); /* Write it now or forget about it forever. */ |
---|
| 691 | |
---|
| 692 | if( irc->sendbuffer ) |
---|
| 693 | { |
---|
| 694 | b_event_remove( irc->w_watch_source_id ); |
---|
[af9f2ca] | 695 | irc->w_watch_source_id = 0; |
---|
[f1c2b21] | 696 | g_free( irc->sendbuffer ); |
---|
[af9f2ca] | 697 | irc->sendbuffer = NULL; |
---|
[f1c2b21] | 698 | } |
---|
| 699 | |
---|
| 700 | b_event_remove( irc->r_watch_source_id ); |
---|
| 701 | closesocket( irc->fd ); |
---|
| 702 | irc->fd = fd; |
---|
| 703 | irc->r_watch_source_id = b_input_add( irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc ); |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | void irc_sync( irc_t *irc ) |
---|
| 707 | { |
---|
| 708 | GSList *l; |
---|
| 709 | |
---|
| 710 | irc_write( irc, ":%s!%s@%s MODE %s :+%s", irc->user->nick, |
---|
| 711 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 712 | irc->umode ); |
---|
| 713 | |
---|
| 714 | for( l = irc->channels; l; l = l->next ) |
---|
| 715 | { |
---|
| 716 | irc_channel_t *ic = l->data; |
---|
| 717 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 718 | irc_send_join( ic, irc->user ); |
---|
| 719 | } |
---|
[e1aaea4] | 720 | |
---|
| 721 | /* We may be waiting for a PONG from the previous client connection. */ |
---|
| 722 | irc->pinging = FALSE; |
---|
[f1c2b21] | 723 | } |
---|
| 724 | |
---|
| 725 | void irc_desync( irc_t *irc ) |
---|
| 726 | { |
---|
| 727 | GSList *l; |
---|
| 728 | |
---|
| 729 | for( l = irc->channels; l; l = l->next ) |
---|
| 730 | irc_channel_del_user( l->data, irc->user, IRC_CDU_KICK, |
---|
| 731 | "Switching to old session" ); |
---|
| 732 | |
---|
| 733 | irc_write( irc, ":%s!%s@%s MODE %s :-%s", irc->user->nick, |
---|
| 734 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 735 | irc->umode ); |
---|
| 736 | } |
---|
| 737 | |
---|
[edf9657] | 738 | int irc_check_login( irc_t *irc ) |
---|
[b7d3cc34] | 739 | { |
---|
[3ddb7477] | 740 | if( irc->user->user && irc->user->nick ) |
---|
[edf9657] | 741 | { |
---|
[3af70b0] | 742 | if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) ) |
---|
[b7d3cc34] | 743 | { |
---|
[3ddb7477] | 744 | irc_send_num( irc, 464, ":This server is password-protected." ); |
---|
[edf9657] | 745 | return 0; |
---|
[b7d3cc34] | 746 | } |
---|
[edf9657] | 747 | else |
---|
[b7d3cc34] | 748 | { |
---|
[4be8239] | 749 | irc_channel_t *ic; |
---|
| 750 | irc_user_t *iu = irc->user; |
---|
| 751 | |
---|
| 752 | irc->user = irc_user_new( irc, iu->nick ); |
---|
| 753 | irc->user->user = iu->user; |
---|
[b95932e] | 754 | irc->user->host = iu->host; |
---|
[4be8239] | 755 | irc->user->fullname = iu->fullname; |
---|
[280c56a] | 756 | irc->user->f = &irc_user_self_funcs; |
---|
[4be8239] | 757 | g_free( iu->nick ); |
---|
| 758 | g_free( iu ); |
---|
| 759 | |
---|
| 760 | if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 761 | ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->user->host, irc->user->nick, irc->user->fullname ); |
---|
| 762 | |
---|
| 763 | irc->status |= USTATUS_LOGGED_IN; |
---|
| 764 | |
---|
[3ddb7477] | 765 | irc_send_login( irc ); |
---|
[4be8239] | 766 | |
---|
[b919363] | 767 | irc->umode[0] = '\0'; |
---|
| 768 | irc_umode_set( irc, "+" UMODE, TRUE ); |
---|
| 769 | |
---|
[fd45e1d1] | 770 | ic = irc->default_channel = irc_channel_new( irc, ROOT_CHAN ); |
---|
[83e92bf] | 771 | irc_channel_set_topic( ic, CONTROL_TOPIC, irc->root ); |
---|
[c8eeadd] | 772 | set_setstr( &ic->set, "auto_join", "true" ); |
---|
| 773 | irc_channel_auto_joins( irc, NULL ); |
---|
[4be8239] | 774 | |
---|
[f7ca587] | 775 | irc->root->last_channel = irc->default_channel; |
---|
[74f1cde] | 776 | |
---|
[e67e513] | 777 | irc_rootmsg( irc, |
---|
[f7ca587] | 778 | "Welcome to the BitlBee gateway!\n\n" |
---|
| 779 | "If you've never used BitlBee before, please do read the help " |
---|
| 780 | "information using the \x02help\x02 command. Lots of FAQs are " |
---|
| 781 | "answered there.\n" |
---|
| 782 | "If you already have an account on this server, just use the " |
---|
| 783 | "\x02identify\x02 command to identify yourself." ); |
---|
[e21c0f8] | 784 | |
---|
[70f69ecc] | 785 | /* This is for bug #209 (use PASS to identify to NickServ). */ |
---|
| 786 | if( irc->password != NULL ) |
---|
| 787 | { |
---|
| 788 | char *send_cmd[] = { "identify", g_strdup( irc->password ), NULL }; |
---|
| 789 | |
---|
| 790 | irc_setpass( irc, NULL ); |
---|
| 791 | root_command( irc, send_cmd ); |
---|
| 792 | g_free( send_cmd[1] ); |
---|
| 793 | } |
---|
| 794 | |
---|
[edf9657] | 795 | return 1; |
---|
[b7d3cc34] | 796 | } |
---|
[edf9657] | 797 | } |
---|
| 798 | else |
---|
| 799 | { |
---|
| 800 | /* More information needed. */ |
---|
| 801 | return 0; |
---|
| 802 | } |
---|
[b7d3cc34] | 803 | } |
---|
| 804 | |
---|
[65016a6] | 805 | /* TODO: This is a mess, but this function is a bit too complicated to be |
---|
| 806 | converted to something more generic. */ |
---|
[b919363] | 807 | void irc_umode_set( irc_t *irc, const char *s, gboolean allow_priv ) |
---|
| 808 | { |
---|
| 809 | /* allow_priv: Set to 0 if s contains user input, 1 if you want |
---|
| 810 | to set a "privileged" mode (+o, +R, etc). */ |
---|
| 811 | char m[128], st = 1; |
---|
| 812 | const char *t; |
---|
| 813 | int i; |
---|
[00fd005] | 814 | char changes[512], st2 = 2; |
---|
[b919363] | 815 | char badflag = 0; |
---|
| 816 | |
---|
| 817 | memset( m, 0, sizeof( m ) ); |
---|
| 818 | |
---|
[00fd005] | 819 | /* Keep track of which modes are enabled in this array. */ |
---|
[b919363] | 820 | for( t = irc->umode; *t; t ++ ) |
---|
| 821 | if( *t < sizeof( m ) ) |
---|
| 822 | m[(int)*t] = 1; |
---|
| 823 | |
---|
[00fd005] | 824 | i = 0; |
---|
| 825 | for( t = s; *t && i < sizeof( changes ) - 3; t ++ ) |
---|
[b919363] | 826 | { |
---|
| 827 | if( *t == '+' || *t == '-' ) |
---|
| 828 | st = *t == '+'; |
---|
| 829 | else if( ( st == 0 && ( !strchr( UMODES_KEEP, *t ) || allow_priv ) ) || |
---|
| 830 | ( st == 1 && strchr( UMODES, *t ) ) || |
---|
| 831 | ( st == 1 && allow_priv && strchr( UMODES_PRIV, *t ) ) ) |
---|
| 832 | { |
---|
| 833 | if( m[(int)*t] != st) |
---|
| 834 | { |
---|
[00fd005] | 835 | /* If we're actually making a change, remember this |
---|
| 836 | for the response. */ |
---|
[b919363] | 837 | if( st != st2 ) |
---|
[00fd005] | 838 | st2 = st, changes[i++] = st ? '+' : '-'; |
---|
| 839 | changes[i++] = *t; |
---|
[b919363] | 840 | } |
---|
| 841 | m[(int)*t] = st; |
---|
| 842 | } |
---|
| 843 | else |
---|
| 844 | badflag = 1; |
---|
| 845 | } |
---|
[00fd005] | 846 | changes[i] = '\0'; |
---|
[b919363] | 847 | |
---|
[00fd005] | 848 | /* Convert the m array back into an umode string. */ |
---|
[b919363] | 849 | memset( irc->umode, 0, sizeof( irc->umode ) ); |
---|
| 850 | for( i = 'A'; i <= 'z' && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ ) |
---|
| 851 | if( m[i] ) |
---|
| 852 | irc->umode[strlen(irc->umode)] = i; |
---|
| 853 | |
---|
| 854 | if( badflag ) |
---|
| 855 | irc_send_num( irc, 501, ":Unknown MODE flag" ); |
---|
| 856 | if( *changes ) |
---|
| 857 | irc_write( irc, ":%s!%s@%s MODE %s :%s", irc->user->nick, |
---|
| 858 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 859 | changes ); |
---|
| 860 | } |
---|
| 861 | |
---|
[3923003] | 862 | /* Returns 0 if everything seems to be okay, a number >0 when there was a |
---|
| 863 | timeout. The number returned is the number of seconds we received no |
---|
| 864 | pongs from the user. When not connected yet, we don't ping but drop the |
---|
| 865 | connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ |
---|
| 866 | static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond ) |
---|
| 867 | { |
---|
[b958cb5] | 868 | double now = gettime(); |
---|
[3923003] | 869 | irc_t *irc = _irc; |
---|
[b958cb5] | 870 | int fail = 0; |
---|
[3923003] | 871 | |
---|
| 872 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
| 873 | { |
---|
[b958cb5] | 874 | if( now > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) ) |
---|
| 875 | fail = now - irc->last_pong; |
---|
[3923003] | 876 | } |
---|
| 877 | else |
---|
| 878 | { |
---|
[b958cb5] | 879 | if( now > ( irc->last_pong + global.conf->ping_timeout ) ) |
---|
[3923003] | 880 | { |
---|
[b958cb5] | 881 | fail = now - irc->last_pong; |
---|
[3923003] | 882 | } |
---|
[b958cb5] | 883 | else |
---|
[3923003] | 884 | { |
---|
[b958cb5] | 885 | irc_write( irc, "PING :%s", IRC_PING_STRING ); |
---|
[3923003] | 886 | } |
---|
| 887 | } |
---|
| 888 | |
---|
[b958cb5] | 889 | if( fail > 0 ) |
---|
[3923003] | 890 | { |
---|
[b958cb5] | 891 | irc_abort( irc, 0, "Ping Timeout: %d seconds", fail ); |
---|
[3923003] | 892 | return FALSE; |
---|
| 893 | } |
---|
| 894 | |
---|
| 895 | return TRUE; |
---|
| 896 | } |
---|
| 897 | |
---|
[3ddb7477] | 898 | static char *set_eval_charset( set_t *set, char *value ) |
---|
[b7d3cc34] | 899 | { |
---|
[21c87a7] | 900 | irc_t *irc = (irc_t*) set->data; |
---|
| 901 | char *test; |
---|
| 902 | gsize test_bytes = 0; |
---|
[3ddb7477] | 903 | GIConv ic, oc; |
---|
[b7d3cc34] | 904 | |
---|
[3ddb7477] | 905 | if( g_strcasecmp( value, "none" ) == 0 ) |
---|
| 906 | value = g_strdup( "utf-8" ); |
---|
[b7d3cc34] | 907 | |
---|
[21c87a7] | 908 | if( ( oc = g_iconv_open( value, "utf-8" ) ) == (GIConv) -1 ) |
---|
[b7d3cc34] | 909 | { |
---|
[3ddb7477] | 910 | return NULL; |
---|
[b7d3cc34] | 911 | } |
---|
[0e7ab64] | 912 | |
---|
[21c87a7] | 913 | /* Do a test iconv to see if the user picked an IRC-compatible |
---|
| 914 | charset (for example utf-16 goes *horribly* wrong). */ |
---|
| 915 | if( ( test = g_convert_with_iconv( " ", 1, oc, NULL, &test_bytes, NULL ) ) == NULL || |
---|
| 916 | test_bytes > 1 ) |
---|
[0e7ab64] | 917 | { |
---|
[21c87a7] | 918 | g_free( test ); |
---|
| 919 | g_iconv_close( oc ); |
---|
[e67e513] | 920 | irc_rootmsg( irc, "Unsupported character set: The IRC protocol " |
---|
[21c87a7] | 921 | "only supports 8-bit character sets." ); |
---|
| 922 | return NULL; |
---|
[0e7ab64] | 923 | } |
---|
[21c87a7] | 924 | g_free( test ); |
---|
[0e7ab64] | 925 | |
---|
[21c87a7] | 926 | if( ( ic = g_iconv_open( "utf-8", value ) ) == (GIConv) -1 ) |
---|
[b7d3cc34] | 927 | { |
---|
[21c87a7] | 928 | g_iconv_close( oc ); |
---|
[3ddb7477] | 929 | return NULL; |
---|
[b7d3cc34] | 930 | } |
---|
| 931 | |
---|
[3ddb7477] | 932 | if( irc->iconv != (GIConv) -1 ) |
---|
| 933 | g_iconv_close( irc->iconv ); |
---|
| 934 | if( irc->oconv != (GIConv) -1 ) |
---|
| 935 | g_iconv_close( irc->oconv ); |
---|
[b7d3cc34] | 936 | |
---|
[3ddb7477] | 937 | irc->iconv = ic; |
---|
| 938 | irc->oconv = oc; |
---|
[0e7ab64] | 939 | |
---|
[3ddb7477] | 940 | return value; |
---|
[0e7ab64] | 941 | } |
---|
[0e8b3e8] | 942 | |
---|
[6d8cc05] | 943 | /* Mostly meant for upgrades. If one of these is set to the non-default, |
---|
| 944 | set show_users of all channels to something with the same effect. */ |
---|
| 945 | static char *set_eval_bw_compat( set_t *set, char *value ) |
---|
[0e8b3e8] | 946 | { |
---|
| 947 | irc_t *irc = set->data; |
---|
[6d8cc05] | 948 | char *val; |
---|
| 949 | GSList *l; |
---|
[0e8b3e8] | 950 | |
---|
[e67e513] | 951 | irc_rootmsg( irc, "Setting `%s' is obsolete, use the `show_users' " |
---|
[6d8cc05] | 952 | "channel setting instead.", set->key ); |
---|
[0e8b3e8] | 953 | |
---|
[6d8cc05] | 954 | if( strcmp( set->key, "away_devoice" ) == 0 && !bool2int( value ) ) |
---|
| 955 | val = "online,away"; |
---|
| 956 | else if( strcmp( set->key, "show_offline" ) == 0 && bool2int( value ) ) |
---|
| 957 | val = "online@,away+,offline"; |
---|
| 958 | else |
---|
[a758ec1] | 959 | val = "online+,away"; |
---|
[0e8b3e8] | 960 | |
---|
[6d8cc05] | 961 | for( l = irc->channels; l; l = l->next ) |
---|
| 962 | { |
---|
| 963 | irc_channel_t *ic = l->data; |
---|
| 964 | /* No need to check channel type, if the setting doesn't exist it |
---|
| 965 | will just be ignored. */ |
---|
| 966 | set_setstr( &ic->set, "show_users", val ); |
---|
| 967 | } |
---|
[0e8b3e8] | 968 | |
---|
[6d8cc05] | 969 | return SET_INVALID; |
---|
[0e8b3e8] | 970 | } |
---|
[0c85c08] | 971 | |
---|
[c608891] | 972 | static char *set_eval_utf8_nicks( set_t *set, char *value ) |
---|
| 973 | { |
---|
| 974 | irc_t *irc = set->data; |
---|
| 975 | gboolean val = bool2int( value ); |
---|
| 976 | |
---|
| 977 | /* Do *NOT* unset this flag in the middle of a session. There will |
---|
| 978 | be UTF-8 nicks around already so if we suddenly disable support |
---|
| 979 | for them, various functions might behave strangely. */ |
---|
| 980 | if( val ) |
---|
| 981 | irc->status |= IRC_UTF8_NICKS; |
---|
| 982 | else if( irc->status & IRC_UTF8_NICKS ) |
---|
| 983 | irc_rootmsg( irc, "You need to reconnect to BitlBee for this " |
---|
| 984 | "change to take effect." ); |
---|
| 985 | |
---|
| 986 | return set_eval_bool( set, value ); |
---|
| 987 | } |
---|
| 988 | |
---|
[0c85c08] | 989 | void register_irc_plugin( const struct irc_plugin *p ) |
---|
| 990 | { |
---|
| 991 | irc_plugins = g_slist_prepend( irc_plugins, (gpointer) p ); |
---|
| 992 | } |
---|