[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* The big hairy IRCd part of the project */ |
---|
| 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 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
[7d3ef7b] | 28 | #include "sock.h" |
---|
[b7d3cc34] | 29 | #include "crypting.h" |
---|
[2face62] | 30 | #include "ipc.h" |
---|
[b7d3cc34] | 31 | |
---|
[ba9edaa] | 32 | static gboolean irc_userping( gpointer _irc, int fd, b_input_condition cond ); |
---|
[b7d3cc34] | 33 | |
---|
| 34 | GSList *irc_connection_list = NULL; |
---|
| 35 | |
---|
[f3579fd] | 36 | static char *set_eval_password( set_t *set, char *value ) |
---|
[c2295f7] | 37 | { |
---|
[0383943] | 38 | irc_t *irc = set->data; |
---|
| 39 | |
---|
[3183c21] | 40 | if( irc->status & USTATUS_IDENTIFIED && value ) |
---|
[f3579fd] | 41 | { |
---|
| 42 | irc_setpass( irc, value ); |
---|
| 43 | return NULL; |
---|
| 44 | } |
---|
| 45 | else |
---|
| 46 | { |
---|
| 47 | return SET_INVALID; |
---|
| 48 | } |
---|
[c2295f7] | 49 | } |
---|
| 50 | |
---|
[f9756bd] | 51 | static char *set_eval_charset( set_t *set, char *value ) |
---|
| 52 | { |
---|
| 53 | irc_t *irc = set->data; |
---|
| 54 | GIConv ic, oc; |
---|
| 55 | |
---|
| 56 | if( g_strcasecmp( value, "none" ) == 0 ) |
---|
| 57 | value = g_strdup( "utf-8" ); |
---|
| 58 | |
---|
| 59 | if( ( ic = g_iconv_open( "utf-8", value ) ) == (GIConv) -1 ) |
---|
| 60 | { |
---|
| 61 | return NULL; |
---|
| 62 | } |
---|
| 63 | if( ( oc = g_iconv_open( value, "utf-8" ) ) == (GIConv) -1 ) |
---|
| 64 | { |
---|
| 65 | g_iconv_close( ic ); |
---|
| 66 | return NULL; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | if( irc->iconv != (GIConv) -1 ) |
---|
| 70 | g_iconv_close( irc->iconv ); |
---|
| 71 | if( irc->oconv != (GIConv) -1 ) |
---|
| 72 | g_iconv_close( irc->oconv ); |
---|
| 73 | |
---|
| 74 | irc->iconv = ic; |
---|
| 75 | irc->oconv = oc; |
---|
| 76 | |
---|
| 77 | return value; |
---|
| 78 | } |
---|
| 79 | |
---|
[b7d3cc34] | 80 | irc_t *irc_new( int fd ) |
---|
| 81 | { |
---|
[e4d6271] | 82 | irc_t *irc; |
---|
[e9b755e] | 83 | struct sockaddr_storage sock; |
---|
[7435ccf] | 84 | socklen_t socklen = sizeof( sock ); |
---|
[7125cb3] | 85 | set_t *s; |
---|
[e4d6271] | 86 | |
---|
| 87 | irc = g_new0( irc_t, 1 ); |
---|
[b7d3cc34] | 88 | |
---|
| 89 | irc->fd = fd; |
---|
[a0d04d6] | 90 | sock_make_nonblocking( irc->fd ); |
---|
| 91 | |
---|
[ba9edaa] | 92 | irc->r_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_READ, bitlbee_io_current_client_read, irc ); |
---|
[b7d3cc34] | 93 | |
---|
| 94 | irc->status = USTATUS_OFFLINE; |
---|
| 95 | irc->last_pong = gettime(); |
---|
| 96 | |
---|
| 97 | irc->userhash = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
| 98 | irc->watches = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
| 99 | |
---|
| 100 | strcpy( irc->umode, UMODE ); |
---|
| 101 | irc->mynick = g_strdup( ROOT_NICK ); |
---|
| 102 | irc->channel = g_strdup( ROOT_CHAN ); |
---|
| 103 | |
---|
[f9756bd] | 104 | irc->iconv = (GIConv) -1; |
---|
| 105 | irc->oconv = (GIConv) -1; |
---|
| 106 | |
---|
[b7d3cc34] | 107 | if( global.conf->hostname ) |
---|
| 108 | { |
---|
| 109 | irc->myhost = g_strdup( global.conf->hostname ); |
---|
| 110 | } |
---|
[7435ccf] | 111 | else if( getsockname( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 112 | { |
---|
[2231302] | 113 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 114 | |
---|
[2231302] | 115 | if( getnameinfo( (struct sockaddr *) &sock, socklen, buf, |
---|
[c84e31a] | 116 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
[2231302] | 117 | { |
---|
| 118 | irc->myhost = g_strdup( ipv6_unwrap( buf ) ); |
---|
| 119 | } |
---|
[b7d3cc34] | 120 | } |
---|
| 121 | |
---|
[7435ccf] | 122 | if( getpeername( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
[b7d3cc34] | 123 | { |
---|
[2231302] | 124 | char buf[NI_MAXHOST+1]; |
---|
[e9b755e] | 125 | |
---|
[2231302] | 126 | if( getnameinfo( (struct sockaddr *)&sock, socklen, buf, |
---|
[c84e31a] | 127 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
[2231302] | 128 | { |
---|
[2a6ca4f] | 129 | irc->host = g_strdup( ipv6_unwrap( buf ) ); |
---|
[2231302] | 130 | } |
---|
[b7d3cc34] | 131 | } |
---|
| 132 | |
---|
[3e1e11af] | 133 | if( irc->host == NULL ) |
---|
| 134 | irc->host = g_strdup( "localhost.localdomain" ); |
---|
| 135 | if( irc->myhost == NULL ) |
---|
| 136 | irc->myhost = g_strdup( "localhost.localdomain" ); |
---|
| 137 | |
---|
[b7d3cc34] | 138 | if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 ) |
---|
[ba9edaa] | 139 | irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc ); |
---|
[b7d3cc34] | 140 | |
---|
| 141 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->myhost, "BitlBee-IRCd initialized, please go on" ); |
---|
| 142 | |
---|
| 143 | irc_connection_list = g_slist_append( irc_connection_list, irc ); |
---|
| 144 | |
---|
[7125cb3] | 145 | s = set_add( &irc->set, "away_devoice", "true", set_eval_away_devoice, irc ); |
---|
| 146 | s = set_add( &irc->set, "auto_connect", "true", set_eval_bool, irc ); |
---|
| 147 | s = set_add( &irc->set, "auto_reconnect", "false", set_eval_bool, irc ); |
---|
| 148 | s = set_add( &irc->set, "auto_reconnect_delay", "5*3<900", set_eval_account_reconnect_delay, irc ); |
---|
| 149 | s = set_add( &irc->set, "buddy_sendbuffer", "false", set_eval_bool, irc ); |
---|
| 150 | s = set_add( &irc->set, "buddy_sendbuffer_delay", "200", set_eval_int, irc ); |
---|
| 151 | s = set_add( &irc->set, "charset", "utf-8", set_eval_charset, irc ); |
---|
| 152 | s = set_add( &irc->set, "debug", "false", set_eval_bool, irc ); |
---|
| 153 | s = set_add( &irc->set, "default_target", "root", NULL, irc ); |
---|
| 154 | s = set_add( &irc->set, "display_namechanges", "false", set_eval_bool, irc ); |
---|
| 155 | s = set_add( &irc->set, "handle_unknown", "root", NULL, irc ); |
---|
| 156 | s = set_add( &irc->set, "lcnicks", "true", set_eval_bool, irc ); |
---|
| 157 | s = set_add( &irc->set, "ops", "both", set_eval_ops, irc ); |
---|
[f3579fd] | 158 | s = set_add( &irc->set, "password", NULL, set_eval_password, irc ); |
---|
[7125cb3] | 159 | s->flags |= SET_NULL_OK; |
---|
| 160 | s = set_add( &irc->set, "private", "true", set_eval_bool, irc ); |
---|
| 161 | s = set_add( &irc->set, "query_order", "lifo", NULL, irc ); |
---|
| 162 | s = set_add( &irc->set, "root_nick", irc->mynick, set_eval_root_nick, irc ); |
---|
| 163 | s = set_add( &irc->set, "save_on_quit", "true", set_eval_bool, irc ); |
---|
| 164 | s = set_add( &irc->set, "simulate_netsplit", "true", set_eval_bool, irc ); |
---|
| 165 | s = set_add( &irc->set, "strip_html", "true", NULL, irc ); |
---|
| 166 | s = set_add( &irc->set, "to_char", ": ", set_eval_to_char, irc ); |
---|
| 167 | s = set_add( &irc->set, "typing_notice", "false", set_eval_bool, irc ); |
---|
[b7d3cc34] | 168 | |
---|
| 169 | conf_loaddefaults( irc ); |
---|
| 170 | |
---|
[f9756bd] | 171 | /* Evaluator sets the iconv/oconv structures. */ |
---|
| 172 | set_eval_charset( set_find( &irc->set, "charset" ), set_getstr( &irc->set, "charset" ) ); |
---|
| 173 | |
---|
[b7d3cc34] | 174 | return( irc ); |
---|
| 175 | } |
---|
| 176 | |
---|
[f73b969] | 177 | /* immed=1 makes this function pretty much equal to irc_free(), except that |
---|
| 178 | this one will "log". In case the connection is already broken and we |
---|
| 179 | shouldn't try to write to it. */ |
---|
[fc50d48] | 180 | void irc_abort( irc_t *irc, int immed, char *format, ... ) |
---|
[c1826c6] | 181 | { |
---|
[fc50d48] | 182 | if( format != NULL ) |
---|
| 183 | { |
---|
[f73b969] | 184 | va_list params; |
---|
[fc50d48] | 185 | char *reason; |
---|
| 186 | |
---|
| 187 | va_start( params, format ); |
---|
[f73b969] | 188 | reason = g_strdup_vprintf( format, params ); |
---|
[fc50d48] | 189 | va_end( params ); |
---|
| 190 | |
---|
| 191 | if( !immed ) |
---|
| 192 | irc_write( irc, "ERROR :Closing link: %s", reason ); |
---|
| 193 | |
---|
| 194 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
[f73b969] | 195 | irc->nick ? irc->nick : "(NONE)", irc->host, reason ); |
---|
[fc50d48] | 196 | |
---|
| 197 | g_free( reason ); |
---|
| 198 | } |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | if( !immed ) |
---|
| 202 | irc_write( irc, "ERROR :Closing link" ); |
---|
| 203 | |
---|
| 204 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
[f73b969] | 205 | irc->nick ? irc->nick : "(NONE)", irc->host, "No reason given" ); |
---|
[fc50d48] | 206 | } |
---|
| 207 | |
---|
[79e826a] | 208 | irc->status |= USTATUS_SHUTDOWN; |
---|
[fc50d48] | 209 | if( irc->sendbuffer && !immed ) |
---|
[c1826c6] | 210 | { |
---|
[883a398] | 211 | /* Set up a timeout event that should shut down the connection |
---|
| 212 | in a second, just in case ..._write doesn't do it first. */ |
---|
[fc50d48] | 213 | |
---|
[ba9edaa] | 214 | b_event_remove( irc->r_watch_source_id ); |
---|
[883a398] | 215 | irc->r_watch_source_id = 0; |
---|
| 216 | |
---|
| 217 | b_event_remove( irc->ping_source_id ); |
---|
| 218 | irc->ping_source_id = b_timeout_add( 1000, (b_event_handler) irc_free, irc ); |
---|
[c1826c6] | 219 | } |
---|
| 220 | else |
---|
| 221 | { |
---|
| 222 | irc_free( irc ); |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
[36fa9bd] | 226 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ) |
---|
[b7d3cc34] | 227 | { |
---|
| 228 | g_free( key ); |
---|
| 229 | |
---|
| 230 | return( TRUE ); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /* Because we have no garbage collection, this is quite annoying */ |
---|
[fa75134] | 234 | void irc_free( irc_t * irc ) |
---|
[b7d3cc34] | 235 | { |
---|
| 236 | user_t *user, *usertmp; |
---|
| 237 | |
---|
| 238 | log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd ); |
---|
| 239 | |
---|
[d5ccd83] | 240 | if( irc->status & USTATUS_IDENTIFIED && set_getbool( &irc->set, "save_on_quit" ) ) |
---|
[3183c21] | 241 | if( storage_save( irc, NULL, TRUE ) != STORAGE_OK ) |
---|
[b7d3cc34] | 242 | irc_usermsg( irc, "Error while saving settings!" ); |
---|
| 243 | |
---|
| 244 | irc_connection_list = g_slist_remove( irc_connection_list, irc ); |
---|
| 245 | |
---|
[fa75134] | 246 | while( irc->accounts ) |
---|
| 247 | { |
---|
| 248 | if( irc->accounts->ic ) |
---|
| 249 | imc_logout( irc->accounts->ic, FALSE ); |
---|
| 250 | else if( irc->accounts->reconnect ) |
---|
| 251 | cancel_auto_reconnect( irc->accounts ); |
---|
| 252 | |
---|
| 253 | if( irc->accounts->ic == NULL ) |
---|
| 254 | account_del( irc, irc->accounts ); |
---|
[f959495] | 255 | else |
---|
| 256 | /* Nasty hack, but account_del() doesn't work in this |
---|
| 257 | case and we don't want infinite loops, do we? ;-) */ |
---|
| 258 | irc->accounts = irc->accounts->next; |
---|
[fa75134] | 259 | } |
---|
| 260 | |
---|
| 261 | while( irc->queries != NULL ) |
---|
| 262 | query_del( irc, irc->queries ); |
---|
[5b52a48] | 263 | |
---|
[fa75134] | 264 | while( irc->set ) |
---|
| 265 | set_del( &irc->set, irc->set->key ); |
---|
[b7d3cc34] | 266 | |
---|
[fa75134] | 267 | if (irc->users != NULL) |
---|
| 268 | { |
---|
[b7d3cc34] | 269 | user = irc->users; |
---|
[fa75134] | 270 | while( user != NULL ) |
---|
| 271 | { |
---|
| 272 | g_free( user->nick ); |
---|
| 273 | g_free( user->away ); |
---|
| 274 | g_free( user->handle ); |
---|
| 275 | if( user->user != user->nick ) g_free( user->user ); |
---|
| 276 | if( user->host != user->nick ) g_free( user->host ); |
---|
| 277 | if( user->realname != user->nick ) g_free( user->realname ); |
---|
| 278 | b_event_remove( user->sendbuf_timer ); |
---|
[b7d3cc34] | 279 | |
---|
| 280 | usertmp = user; |
---|
| 281 | user = user->next; |
---|
[fa75134] | 282 | g_free( usertmp ); |
---|
[b7d3cc34] | 283 | } |
---|
| 284 | } |
---|
| 285 | |
---|
[fa75134] | 286 | if( irc->ping_source_id > 0 ) |
---|
| 287 | b_event_remove( irc->ping_source_id ); |
---|
[883a398] | 288 | if( irc->r_watch_source_id > 0 ) |
---|
| 289 | b_event_remove( irc->r_watch_source_id ); |
---|
[fa75134] | 290 | if( irc->w_watch_source_id > 0 ) |
---|
| 291 | b_event_remove( irc->w_watch_source_id ); |
---|
| 292 | |
---|
| 293 | closesocket( irc->fd ); |
---|
| 294 | irc->fd = -1; |
---|
| 295 | |
---|
| 296 | g_hash_table_foreach_remove( irc->userhash, irc_free_hashkey, NULL ); |
---|
| 297 | g_hash_table_destroy( irc->userhash ); |
---|
[b7d3cc34] | 298 | |
---|
[fa75134] | 299 | g_hash_table_foreach_remove( irc->watches, irc_free_hashkey, NULL ); |
---|
| 300 | g_hash_table_destroy( irc->watches ); |
---|
[b7d3cc34] | 301 | |
---|
[f9756bd] | 302 | if( irc->iconv != (GIConv) -1 ) |
---|
| 303 | g_iconv_close( irc->iconv ); |
---|
| 304 | if( irc->oconv != (GIConv) -1 ) |
---|
| 305 | g_iconv_close( irc->oconv ); |
---|
| 306 | |
---|
[fa75134] | 307 | g_free( irc->sendbuffer ); |
---|
| 308 | g_free( irc->readbuffer ); |
---|
| 309 | |
---|
| 310 | g_free( irc->nick ); |
---|
| 311 | g_free( irc->user ); |
---|
| 312 | g_free( irc->host ); |
---|
| 313 | g_free( irc->realname ); |
---|
| 314 | g_free( irc->password ); |
---|
| 315 | |
---|
| 316 | g_free( irc->myhost ); |
---|
| 317 | g_free( irc->mynick ); |
---|
| 318 | |
---|
| 319 | g_free( irc->channel ); |
---|
| 320 | |
---|
[f9756bd] | 321 | g_free( irc->last_target ); |
---|
| 322 | |
---|
[fa75134] | 323 | g_free( irc ); |
---|
[b7d3cc34] | 324 | |
---|
[565a1ea] | 325 | if( global.conf->runmode == RUNMODE_INETD || |
---|
| 326 | global.conf->runmode == RUNMODE_FORKDAEMON || |
---|
| 327 | ( global.conf->runmode == RUNMODE_DAEMON && |
---|
| 328 | global.listen_socket == -1 && |
---|
| 329 | irc_connection_list == NULL ) ) |
---|
[ba9edaa] | 330 | b_main_quit(); |
---|
[b7d3cc34] | 331 | } |
---|
| 332 | |
---|
[7cad7b4] | 333 | /* USE WITH CAUTION! |
---|
| 334 | Sets pass without checking */ |
---|
| 335 | void irc_setpass (irc_t *irc, const char *pass) |
---|
| 336 | { |
---|
[6e1fed7] | 337 | g_free (irc->password); |
---|
[7cad7b4] | 338 | |
---|
| 339 | if (pass) { |
---|
| 340 | irc->password = g_strdup (pass); |
---|
| 341 | } else { |
---|
| 342 | irc->password = NULL; |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | |
---|
[f73b969] | 346 | void irc_process( irc_t *irc ) |
---|
[b7d3cc34] | 347 | { |
---|
[f9756bd] | 348 | char **lines, *temp, **cmd; |
---|
[b7d3cc34] | 349 | int i; |
---|
| 350 | |
---|
[de3e100] | 351 | if( irc->readbuffer != NULL ) |
---|
| 352 | { |
---|
| 353 | lines = irc_tokenize( irc->readbuffer ); |
---|
| 354 | |
---|
| 355 | for( i = 0; *lines[i] != '\0'; i ++ ) |
---|
| 356 | { |
---|
[f9756bd] | 357 | char *conv = NULL; |
---|
[7d31002] | 358 | |
---|
[18ff38f] | 359 | /* [WvG] If the last line isn't empty, it's an incomplete line and we |
---|
| 360 | should wait for the rest to come in before processing it. */ |
---|
[de3e100] | 361 | if( lines[i+1] == NULL ) |
---|
| 362 | { |
---|
[b7d3cc34] | 363 | temp = g_strdup( lines[i] ); |
---|
| 364 | g_free( irc->readbuffer ); |
---|
| 365 | irc->readbuffer = temp; |
---|
[de3e100] | 366 | i ++; |
---|
[b7d3cc34] | 367 | break; |
---|
[e27661d] | 368 | } |
---|
| 369 | |
---|
[f9756bd] | 370 | if( irc->iconv != (GIConv) -1 ) |
---|
[e27661d] | 371 | { |
---|
[f9756bd] | 372 | gsize bytes_read, bytes_written; |
---|
| 373 | |
---|
| 374 | conv = g_convert_with_iconv( lines[i], -1, irc->iconv, |
---|
| 375 | &bytes_read, &bytes_written, NULL ); |
---|
| 376 | |
---|
| 377 | if( conv == NULL || bytes_read != strlen( lines[i] ) ) |
---|
[94d52d64] | 378 | { |
---|
[fc0cf92] | 379 | /* GLib can do strange things if things are not in the expected charset, |
---|
| 380 | so let's be a little bit paranoid here: */ |
---|
[94d52d64] | 381 | if( irc->status & USTATUS_LOGGED_IN ) |
---|
[fc0cf92] | 382 | { |
---|
[43462708] | 383 | irc_usermsg( irc, "Error: Charset mismatch detected. The charset " |
---|
[94d52d64] | 384 | "setting is currently set to %s, so please make " |
---|
| 385 | "sure your IRC client will send and accept text in " |
---|
| 386 | "that charset, or tell BitlBee which charset to " |
---|
| 387 | "expect by changing the charset setting. See " |
---|
| 388 | "`help set charset' for more information. Your " |
---|
[f9756bd] | 389 | "message was ignored.", |
---|
| 390 | set_getstr( &irc->set, "charset" ) ); |
---|
| 391 | |
---|
| 392 | g_free( conv ); |
---|
| 393 | conv = NULL; |
---|
[fc0cf92] | 394 | } |
---|
| 395 | else |
---|
| 396 | { |
---|
| 397 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->myhost, |
---|
[a83442a] | 398 | "Warning: invalid characters received at login time." ); |
---|
[fc0cf92] | 399 | |
---|
[f9756bd] | 400 | conv = g_strdup( lines[i] ); |
---|
[fc0cf92] | 401 | for( temp = conv; *temp; temp ++ ) |
---|
| 402 | if( *temp & 0x80 ) |
---|
| 403 | *temp = '?'; |
---|
| 404 | } |
---|
[94d52d64] | 405 | } |
---|
| 406 | lines[i] = conv; |
---|
[e27661d] | 407 | } |
---|
[de3e100] | 408 | |
---|
[f9756bd] | 409 | if( lines[i] ) |
---|
| 410 | { |
---|
| 411 | if( ( cmd = irc_parse_line( lines[i] ) ) == NULL ) |
---|
| 412 | continue; |
---|
| 413 | irc_exec( irc, cmd ); |
---|
| 414 | g_free( cmd ); |
---|
| 415 | } |
---|
[f73b969] | 416 | |
---|
[f9756bd] | 417 | g_free( conv ); |
---|
[f73b969] | 418 | |
---|
| 419 | /* Shouldn't really happen, but just in case... */ |
---|
| 420 | if( !g_slist_find( irc_connection_list, irc ) ) |
---|
[de3e100] | 421 | { |
---|
[b7d3cc34] | 422 | g_free( lines ); |
---|
[f73b969] | 423 | return; |
---|
[b7d3cc34] | 424 | } |
---|
| 425 | } |
---|
[de3e100] | 426 | |
---|
| 427 | if( lines[i] != NULL ) |
---|
| 428 | { |
---|
| 429 | g_free( irc->readbuffer ); |
---|
[0431ea1] | 430 | irc->readbuffer = NULL; |
---|
[b7d3cc34] | 431 | } |
---|
[de3e100] | 432 | |
---|
[b7d3cc34] | 433 | g_free( lines ); |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | |
---|
[e27661d] | 437 | /* Splits a long string into separate lines. The array is NULL-terminated and, unless the string |
---|
| 438 | contains an incomplete line at the end, ends with an empty string. */ |
---|
[b7d3cc34] | 439 | char **irc_tokenize( char *buffer ) |
---|
| 440 | { |
---|
[18ff38f] | 441 | int i, j, n = 3; |
---|
[b7d3cc34] | 442 | char **lines; |
---|
| 443 | |
---|
[18ff38f] | 444 | /* Allocate n+1 elements. */ |
---|
| 445 | lines = g_new( char *, n + 1 ); |
---|
[b7d3cc34] | 446 | |
---|
[de3e100] | 447 | lines[0] = buffer; |
---|
[b7d3cc34] | 448 | |
---|
[18ff38f] | 449 | /* Split the buffer in several strings, and accept any kind of line endings, |
---|
| 450 | * knowing that ERC on Windows may send something interesting like \r\r\n, |
---|
| 451 | * and surely there must be clients that think just \n is enough... */ |
---|
| 452 | for( i = 0, j = 0; buffer[i] != '\0'; i ++ ) |
---|
[de3e100] | 453 | { |
---|
[18ff38f] | 454 | if( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
[de3e100] | 455 | { |
---|
[18ff38f] | 456 | while( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
| 457 | buffer[i++] = '\0'; |
---|
| 458 | |
---|
| 459 | lines[++j] = buffer + i; |
---|
[de3e100] | 460 | |
---|
[18ff38f] | 461 | if( j >= n ) |
---|
| 462 | { |
---|
| 463 | n *= 2; |
---|
| 464 | lines = g_renew( char *, lines, n + 1 ); |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | if( buffer[i] == '\0' ) |
---|
| 468 | break; |
---|
[b7d3cc34] | 469 | } |
---|
| 470 | } |
---|
[de3e100] | 471 | |
---|
[18ff38f] | 472 | /* NULL terminate our list. */ |
---|
| 473 | lines[++j] = NULL; |
---|
| 474 | |
---|
| 475 | return lines; |
---|
[b7d3cc34] | 476 | } |
---|
| 477 | |
---|
[e27661d] | 478 | /* Split an IRC-style line into little parts/arguments. */ |
---|
[0431ea1] | 479 | char **irc_parse_line( char *line ) |
---|
[b7d3cc34] | 480 | { |
---|
| 481 | int i, j; |
---|
| 482 | char **cmd; |
---|
| 483 | |
---|
| 484 | /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ |
---|
[de3e100] | 485 | if( line[0] == ':' ) |
---|
| 486 | { |
---|
| 487 | for( i = 0; line[i] != ' '; i ++ ); |
---|
| 488 | line = line + i; |
---|
[b7d3cc34] | 489 | } |
---|
[de3e100] | 490 | for( i = 0; line[i] == ' '; i ++ ); |
---|
| 491 | line = line + i; |
---|
| 492 | |
---|
[b7d3cc34] | 493 | /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ |
---|
[de3e100] | 494 | if( line[0] == '\0') |
---|
| 495 | return NULL; |
---|
| 496 | |
---|
| 497 | /* Count the number of char **cmd elements we're going to need. */ |
---|
| 498 | j = 1; |
---|
| 499 | for( i = 0; line[i] != '\0'; i ++ ) |
---|
| 500 | { |
---|
| 501 | if( line[i] == ' ' ) |
---|
| 502 | { |
---|
| 503 | j ++; |
---|
[b7d3cc34] | 504 | |
---|
[de3e100] | 505 | if( line[i+1] == ':' ) |
---|
| 506 | break; |
---|
| 507 | } |
---|
[b7d3cc34] | 508 | } |
---|
| 509 | |
---|
| 510 | /* Allocate the space we need. */ |
---|
[de3e100] | 511 | cmd = g_new( char *, j + 1 ); |
---|
| 512 | cmd[j] = NULL; |
---|
[b7d3cc34] | 513 | |
---|
| 514 | /* Do the actual line splitting, format is: |
---|
| 515 | * Input: "PRIVMSG #bitlbee :foo bar" |
---|
| 516 | * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL |
---|
| 517 | */ |
---|
| 518 | |
---|
[de3e100] | 519 | cmd[0] = line; |
---|
| 520 | for( i = 0, j = 0; line[i] != '\0'; i ++ ) |
---|
[b7d3cc34] | 521 | { |
---|
[de3e100] | 522 | if( line[i] == ' ' ) |
---|
[b7d3cc34] | 523 | { |
---|
[de3e100] | 524 | line[i] = '\0'; |
---|
| 525 | cmd[++j] = line + i + 1; |
---|
[b7d3cc34] | 526 | |
---|
[de3e100] | 527 | if( line[i+1] == ':' ) |
---|
[b7d3cc34] | 528 | { |
---|
[de3e100] | 529 | cmd[j] ++; |
---|
[b7d3cc34] | 530 | break; |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | |
---|
[de3e100] | 535 | return cmd; |
---|
[b7d3cc34] | 536 | } |
---|
| 537 | |
---|
[e27661d] | 538 | /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ |
---|
[74c119d] | 539 | char *irc_build_line( char **cmd ) |
---|
| 540 | { |
---|
| 541 | int i, len; |
---|
| 542 | char *s; |
---|
[b7d3cc34] | 543 | |
---|
[74c119d] | 544 | if( cmd[0] == NULL ) |
---|
| 545 | return NULL; |
---|
[b7d3cc34] | 546 | |
---|
[74c119d] | 547 | len = 1; |
---|
| 548 | for( i = 0; cmd[i]; i ++ ) |
---|
| 549 | len += strlen( cmd[i] ) + 1; |
---|
| 550 | |
---|
| 551 | if( strchr( cmd[i-1], ' ' ) != NULL ) |
---|
| 552 | len ++; |
---|
| 553 | |
---|
| 554 | s = g_new0( char, len + 1 ); |
---|
| 555 | for( i = 0; cmd[i]; i ++ ) |
---|
[b7d3cc34] | 556 | { |
---|
[74c119d] | 557 | if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) |
---|
| 558 | strcat( s, ":" ); |
---|
[b7d3cc34] | 559 | |
---|
[74c119d] | 560 | strcat( s, cmd[i] ); |
---|
[b7d3cc34] | 561 | |
---|
[74c119d] | 562 | if( cmd[i+1] ) |
---|
| 563 | strcat( s, " " ); |
---|
[b7d3cc34] | 564 | } |
---|
[74c119d] | 565 | strcat( s, "\r\n" ); |
---|
[b7d3cc34] | 566 | |
---|
[74c119d] | 567 | return s; |
---|
[b7d3cc34] | 568 | } |
---|
| 569 | |
---|
| 570 | void irc_reply( irc_t *irc, int code, char *format, ... ) |
---|
| 571 | { |
---|
| 572 | char text[IRC_MAX_LINE]; |
---|
| 573 | va_list params; |
---|
| 574 | |
---|
| 575 | va_start( params, format ); |
---|
| 576 | g_vsnprintf( text, IRC_MAX_LINE, format, params ); |
---|
| 577 | va_end( params ); |
---|
| 578 | irc_write( irc, ":%s %03d %s %s", irc->myhost, code, irc->nick?irc->nick:"*", text ); |
---|
| 579 | |
---|
| 580 | return; |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | int irc_usermsg( irc_t *irc, char *format, ... ) |
---|
| 584 | { |
---|
| 585 | char text[1024]; |
---|
| 586 | va_list params; |
---|
| 587 | char is_private = 0; |
---|
| 588 | user_t *u; |
---|
| 589 | |
---|
| 590 | u = user_find( irc, irc->mynick ); |
---|
[dd89a55] | 591 | is_private = u->is_private; |
---|
[b7d3cc34] | 592 | |
---|
| 593 | va_start( params, format ); |
---|
| 594 | g_vsnprintf( text, sizeof( text ), format, params ); |
---|
| 595 | va_end( params ); |
---|
| 596 | |
---|
| 597 | return( irc_msgfrom( irc, u->nick, text ) ); |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | void irc_write( irc_t *irc, char *format, ... ) |
---|
| 601 | { |
---|
| 602 | va_list params; |
---|
| 603 | |
---|
| 604 | va_start( params, format ); |
---|
| 605 | irc_vawrite( irc, format, params ); |
---|
| 606 | va_end( params ); |
---|
| 607 | |
---|
| 608 | return; |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | void irc_vawrite( irc_t *irc, char *format, va_list params ) |
---|
| 612 | { |
---|
| 613 | int size; |
---|
[f9756bd] | 614 | char line[IRC_MAX_LINE+1]; |
---|
[d783e48] | 615 | |
---|
[0356ae3] | 616 | /* Don't try to write anything new anymore when shutting down. */ |
---|
[5898ef8] | 617 | if( irc->status & USTATUS_SHUTDOWN ) |
---|
[b7d3cc34] | 618 | return; |
---|
[d783e48] | 619 | |
---|
[f9756bd] | 620 | memset( line, 0, sizeof( line ) ); |
---|
[d783e48] | 621 | g_vsnprintf( line, IRC_MAX_LINE - 2, format, params ); |
---|
[b7d3cc34] | 622 | strip_newlines( line ); |
---|
[f9756bd] | 623 | |
---|
| 624 | if( irc->oconv != (GIConv) -1 ) |
---|
[d783e48] | 625 | { |
---|
[f9756bd] | 626 | gsize bytes_read, bytes_written; |
---|
| 627 | char *conv; |
---|
| 628 | |
---|
| 629 | conv = g_convert_with_iconv( line, -1, irc->oconv, |
---|
| 630 | &bytes_read, &bytes_written, NULL ); |
---|
| 631 | |
---|
| 632 | if( bytes_read == strlen( line ) ) |
---|
| 633 | strncpy( line, conv, IRC_MAX_LINE - 2 ); |
---|
[d783e48] | 634 | |
---|
[f9756bd] | 635 | g_free( conv ); |
---|
[d783e48] | 636 | } |
---|
[f9756bd] | 637 | g_strlcat( line, "\r\n", IRC_MAX_LINE + 1 ); |
---|
[d783e48] | 638 | |
---|
[a0d04d6] | 639 | if( irc->sendbuffer != NULL ) |
---|
| 640 | { |
---|
[b7d3cc34] | 641 | size = strlen( irc->sendbuffer ) + strlen( line ); |
---|
| 642 | irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 ); |
---|
| 643 | strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line ); |
---|
| 644 | } |
---|
[a0d04d6] | 645 | else |
---|
[b7d3cc34] | 646 | { |
---|
[a0d04d6] | 647 | irc->sendbuffer = g_strdup(line); |
---|
[b7d3cc34] | 648 | } |
---|
| 649 | |
---|
[a0d04d6] | 650 | if( irc->w_watch_source_id == 0 ) |
---|
[0356ae3] | 651 | { |
---|
| 652 | /* If the buffer is empty we can probably write, so call the write event handler |
---|
| 653 | immediately. If it returns TRUE, it should be called again, so add the event to |
---|
| 654 | the queue. If it's FALSE, we emptied the buffer and saved ourselves some work |
---|
| 655 | in the event queue. */ |
---|
[bbb6ffb] | 656 | /* Really can't be done as long as the code doesn't do error checking very well: |
---|
| 657 | if( bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ) ) */ |
---|
| 658 | |
---|
| 659 | /* So just always do it via the event handler. */ |
---|
| 660 | irc->w_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_WRITE, bitlbee_io_current_client_write, irc ); |
---|
[0356ae3] | 661 | } |
---|
[a0d04d6] | 662 | |
---|
[b7d3cc34] | 663 | return; |
---|
| 664 | } |
---|
| 665 | |
---|
[22d41a2] | 666 | void irc_write_all( int now, char *format, ... ) |
---|
[b7d3cc34] | 667 | { |
---|
| 668 | va_list params; |
---|
| 669 | GSList *temp; |
---|
[22d41a2] | 670 | |
---|
[b7d3cc34] | 671 | va_start( params, format ); |
---|
[22d41a2] | 672 | |
---|
[b7d3cc34] | 673 | temp = irc_connection_list; |
---|
[22d41a2] | 674 | while( temp != NULL ) |
---|
| 675 | { |
---|
| 676 | irc_t *irc = temp->data; |
---|
| 677 | |
---|
| 678 | if( now ) |
---|
| 679 | { |
---|
| 680 | g_free( irc->sendbuffer ); |
---|
| 681 | irc->sendbuffer = g_strdup( "\r\n" ); |
---|
| 682 | } |
---|
[b7d3cc34] | 683 | irc_vawrite( temp->data, format, params ); |
---|
[22d41a2] | 684 | if( now ) |
---|
| 685 | { |
---|
[a0d04d6] | 686 | bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ); |
---|
[22d41a2] | 687 | } |
---|
[b7d3cc34] | 688 | temp = temp->next; |
---|
| 689 | } |
---|
[22d41a2] | 690 | |
---|
[b7d3cc34] | 691 | va_end( params ); |
---|
| 692 | return; |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | void irc_names( irc_t *irc, char *channel ) |
---|
| 696 | { |
---|
[3f9440d] | 697 | user_t *u; |
---|
| 698 | char namelist[385] = ""; |
---|
[0da65d5] | 699 | struct groupchat *c = NULL; |
---|
[04026d4] | 700 | char *ops = set_getstr( &irc->set, "ops" ); |
---|
[b7d3cc34] | 701 | |
---|
[2f13222] | 702 | /* RFCs say there is no error reply allowed on NAMES, so when the |
---|
[b7d3cc34] | 703 | channel is invalid, just give an empty reply. */ |
---|
| 704 | |
---|
[3f9440d] | 705 | if( g_strcasecmp( channel, irc->channel ) == 0 ) |
---|
[b7d3cc34] | 706 | { |
---|
[3f9440d] | 707 | for( u = irc->users; u; u = u->next ) if( u->online ) |
---|
[b7d3cc34] | 708 | { |
---|
[3f9440d] | 709 | if( strlen( namelist ) + strlen( u->nick ) > sizeof( namelist ) - 4 ) |
---|
[b7d3cc34] | 710 | { |
---|
[3f9440d] | 711 | irc_reply( irc, 353, "= %s :%s", channel, namelist ); |
---|
| 712 | *namelist = 0; |
---|
[b7d3cc34] | 713 | } |
---|
[3f9440d] | 714 | |
---|
[0da65d5] | 715 | if( u->ic && !u->away && set_getbool( &irc->set, "away_devoice" ) ) |
---|
[3f9440d] | 716 | strcat( namelist, "+" ); |
---|
[a93e3c8] | 717 | else if( ( strcmp( u->nick, irc->mynick ) == 0 && ( strcmp( ops, "root" ) == 0 || strcmp( ops, "both" ) == 0 ) ) || |
---|
| 718 | ( strcmp( u->nick, irc->nick ) == 0 && ( strcmp( ops, "user" ) == 0 || strcmp( ops, "both" ) == 0 ) ) ) |
---|
| 719 | strcat( namelist, "@" ); |
---|
[3f9440d] | 720 | |
---|
| 721 | strcat( namelist, u->nick ); |
---|
| 722 | strcat( namelist, " " ); |
---|
[b7d3cc34] | 723 | } |
---|
| 724 | } |
---|
[0e7ab64] | 725 | else if( ( c = irc_chat_by_channel( irc, channel ) ) ) |
---|
[b7d3cc34] | 726 | { |
---|
| 727 | GList *l; |
---|
[3f9440d] | 728 | |
---|
| 729 | /* root and the user aren't in the channel userlist but should |
---|
| 730 | show up in /NAMES, so list them first: */ |
---|
| 731 | sprintf( namelist, "%s%s %s%s ", strcmp( ops, "root" ) == 0 || strcmp( ops, "both" ) ? "@" : "", irc->mynick, |
---|
| 732 | strcmp( ops, "user" ) == 0 || strcmp( ops, "both" ) ? "@" : "", irc->nick ); |
---|
[b7d3cc34] | 733 | |
---|
[0da65d5] | 734 | for( l = c->in_room; l; l = l->next ) if( ( u = user_findhandle( c->ic, l->data ) ) ) |
---|
[3f9440d] | 735 | { |
---|
| 736 | if( strlen( namelist ) + strlen( u->nick ) > sizeof( namelist ) - 4 ) |
---|
| 737 | { |
---|
| 738 | irc_reply( irc, 353, "= %s :%s", channel, namelist ); |
---|
| 739 | *namelist = 0; |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | strcat( namelist, u->nick ); |
---|
| 743 | strcat( namelist, " " ); |
---|
| 744 | } |
---|
[b7d3cc34] | 745 | } |
---|
| 746 | |
---|
[3f9440d] | 747 | if( *namelist ) |
---|
| 748 | irc_reply( irc, 353, "= %s :%s", channel, namelist ); |
---|
| 749 | |
---|
[b7d3cc34] | 750 | irc_reply( irc, 366, "%s :End of /NAMES list", channel ); |
---|
| 751 | } |
---|
| 752 | |
---|
[edf9657] | 753 | int irc_check_login( irc_t *irc ) |
---|
[b7d3cc34] | 754 | { |
---|
[edf9657] | 755 | if( irc->user && irc->nick ) |
---|
| 756 | { |
---|
[3af70b0] | 757 | if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) ) |
---|
[b7d3cc34] | 758 | { |
---|
[edf9657] | 759 | irc_reply( irc, 464, ":This server is password-protected." ); |
---|
| 760 | return 0; |
---|
[b7d3cc34] | 761 | } |
---|
[edf9657] | 762 | else |
---|
[b7d3cc34] | 763 | { |
---|
[edf9657] | 764 | irc_login( irc ); |
---|
| 765 | return 1; |
---|
[b7d3cc34] | 766 | } |
---|
[edf9657] | 767 | } |
---|
| 768 | else |
---|
| 769 | { |
---|
| 770 | /* More information needed. */ |
---|
| 771 | return 0; |
---|
| 772 | } |
---|
[b7d3cc34] | 773 | } |
---|
| 774 | |
---|
| 775 | void irc_login( irc_t *irc ) |
---|
| 776 | { |
---|
| 777 | user_t *u; |
---|
| 778 | |
---|
| 779 | irc_reply( irc, 1, ":Welcome to the BitlBee gateway, %s", irc->nick ); |
---|
| 780 | irc_reply( irc, 2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->myhost ); |
---|
| 781 | irc_reply( irc, 3, ":%s", IRCD_INFO ); |
---|
[238f828] | 782 | irc_reply( irc, 4, "%s %s %s %s", irc->myhost, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES ); |
---|
[578d627] | 783 | irc_reply( irc, 5, "PREFIX=(ov)@+ CHANTYPES=#& CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server", CMODES, MAX_NICK_LENGTH - 1 ); |
---|
[b7d3cc34] | 784 | irc_motd( irc ); |
---|
[2f13222] | 785 | irc->umode[0] = '\0'; |
---|
[238f828] | 786 | irc_umode_set( irc, "+" UMODE, 1 ); |
---|
[b7d3cc34] | 787 | |
---|
| 788 | u = user_add( irc, irc->mynick ); |
---|
| 789 | u->host = g_strdup( irc->myhost ); |
---|
| 790 | u->realname = g_strdup( ROOT_FN ); |
---|
| 791 | u->online = 1; |
---|
| 792 | u->send_handler = root_command_string; |
---|
| 793 | u->is_private = 0; /* [SH] The channel is root's personal playground. */ |
---|
| 794 | irc_spawn( irc, u ); |
---|
| 795 | |
---|
| 796 | u = user_add( irc, NS_NICK ); |
---|
| 797 | u->host = g_strdup( irc->myhost ); |
---|
| 798 | u->realname = g_strdup( ROOT_FN ); |
---|
| 799 | u->online = 0; |
---|
| 800 | u->send_handler = root_command_string; |
---|
| 801 | u->is_private = 1; /* [SH] NickServ is not in the channel, so should always /query. */ |
---|
| 802 | |
---|
| 803 | u = user_add( irc, irc->nick ); |
---|
| 804 | u->user = g_strdup( irc->user ); |
---|
| 805 | u->host = g_strdup( irc->host ); |
---|
| 806 | u->realname = g_strdup( irc->realname ); |
---|
| 807 | u->online = 1; |
---|
| 808 | irc_spawn( irc, u ); |
---|
| 809 | |
---|
[a199d33] | 810 | irc_usermsg( irc, "Welcome to the BitlBee gateway!\n\n" |
---|
| 811 | "If you've never used BitlBee before, please do read the help " |
---|
| 812 | "information using the \x02help\x02 command. Lots of FAQs are " |
---|
| 813 | "answered there.\n" |
---|
| 814 | "If you already have an account on this server, just use the " |
---|
| 815 | "\x02identify\x02 command to identify yourself." ); |
---|
[b7d3cc34] | 816 | |
---|
[bd9b00f] | 817 | if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON ) |
---|
[2face62] | 818 | ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->host, irc->nick, irc->realname ); |
---|
| 819 | |
---|
[79e826a] | 820 | irc->status |= USTATUS_LOGGED_IN; |
---|
[a199d33] | 821 | |
---|
| 822 | /* This is for bug #209 (use PASS to identify to NickServ). */ |
---|
| 823 | if( irc->password != NULL ) |
---|
| 824 | { |
---|
| 825 | char *send_cmd[] = { "identify", g_strdup( irc->password ), NULL }; |
---|
| 826 | |
---|
| 827 | irc_setpass( irc, NULL ); |
---|
| 828 | root_command( irc, send_cmd ); |
---|
| 829 | g_free( send_cmd[1] ); |
---|
| 830 | } |
---|
[b7d3cc34] | 831 | } |
---|
| 832 | |
---|
| 833 | void irc_motd( irc_t *irc ) |
---|
| 834 | { |
---|
| 835 | int fd; |
---|
| 836 | |
---|
| 837 | fd = open( global.conf->motdfile, O_RDONLY ); |
---|
| 838 | if( fd == -1 ) |
---|
| 839 | { |
---|
| 840 | irc_reply( irc, 422, ":We don't need MOTDs." ); |
---|
| 841 | } |
---|
| 842 | else |
---|
| 843 | { |
---|
| 844 | char linebuf[80]; /* Max. line length for MOTD's is 79 chars. It's what most IRC networks seem to do. */ |
---|
| 845 | char *add, max; |
---|
| 846 | int len; |
---|
| 847 | |
---|
| 848 | linebuf[79] = len = 0; |
---|
| 849 | max = sizeof( linebuf ) - 1; |
---|
| 850 | |
---|
| 851 | irc_reply( irc, 375, ":- %s Message Of The Day - ", irc->myhost ); |
---|
| 852 | while( read( fd, linebuf + len, 1 ) == 1 ) |
---|
| 853 | { |
---|
| 854 | if( linebuf[len] == '\n' || len == max ) |
---|
| 855 | { |
---|
| 856 | linebuf[len] = 0; |
---|
| 857 | irc_reply( irc, 372, ":- %s", linebuf ); |
---|
| 858 | len = 0; |
---|
| 859 | } |
---|
| 860 | else if( linebuf[len] == '%' ) |
---|
| 861 | { |
---|
| 862 | read( fd, linebuf + len, 1 ); |
---|
| 863 | if( linebuf[len] == 'h' ) |
---|
| 864 | add = irc->myhost; |
---|
| 865 | else if( linebuf[len] == 'v' ) |
---|
| 866 | add = BITLBEE_VERSION; |
---|
| 867 | else if( linebuf[len] == 'n' ) |
---|
| 868 | add = irc->nick; |
---|
| 869 | else |
---|
| 870 | add = "%"; |
---|
| 871 | |
---|
| 872 | strncpy( linebuf + len, add, max - len ); |
---|
| 873 | while( linebuf[++len] ); |
---|
| 874 | } |
---|
| 875 | else if( len < max ) |
---|
| 876 | { |
---|
| 877 | len ++; |
---|
| 878 | } |
---|
| 879 | } |
---|
| 880 | irc_reply( irc, 376, ":End of MOTD" ); |
---|
[d990997] | 881 | close( fd ); |
---|
[b7d3cc34] | 882 | } |
---|
| 883 | } |
---|
| 884 | |
---|
| 885 | void irc_topic( irc_t *irc, char *channel ) |
---|
| 886 | { |
---|
[50e1776] | 887 | struct groupchat *c = irc_chat_by_channel( irc, channel ); |
---|
| 888 | |
---|
| 889 | if( c && c->topic ) |
---|
| 890 | irc_reply( irc, 332, "%s :%s", channel, c->topic ); |
---|
| 891 | else if( g_strcasecmp( channel, irc->channel ) == 0 ) |
---|
[b7d3cc34] | 892 | irc_reply( irc, 332, "%s :%s", channel, CONTROL_TOPIC ); |
---|
| 893 | else |
---|
[50e1776] | 894 | irc_reply( irc, 331, "%s :No topic for this channel", channel ); |
---|
[b7d3cc34] | 895 | } |
---|
| 896 | |
---|
[238f828] | 897 | void irc_umode_set( irc_t *irc, char *s, int allow_priv ) |
---|
[b7d3cc34] | 898 | { |
---|
[238f828] | 899 | /* allow_priv: Set to 0 if s contains user input, 1 if you want |
---|
| 900 | to set a "privileged" mode (+o, +R, etc). */ |
---|
[b7d3cc34] | 901 | char m[256], st = 1, *t; |
---|
| 902 | int i; |
---|
[2f13222] | 903 | char changes[512], *p, st2 = 2; |
---|
| 904 | char badflag = 0; |
---|
[b7d3cc34] | 905 | |
---|
| 906 | memset( m, 0, sizeof( m ) ); |
---|
| 907 | |
---|
| 908 | for( t = irc->umode; *t; t ++ ) |
---|
| 909 | m[(int)*t] = 1; |
---|
[2f13222] | 910 | |
---|
| 911 | p = changes; |
---|
[b7d3cc34] | 912 | for( t = s; *t; t ++ ) |
---|
| 913 | { |
---|
| 914 | if( *t == '+' || *t == '-' ) |
---|
| 915 | st = *t == '+'; |
---|
[238f828] | 916 | else if( st == 0 || ( strchr( UMODES, *t ) || ( allow_priv && strchr( UMODES_PRIV, *t ) ) ) ) |
---|
[2f13222] | 917 | { |
---|
| 918 | if( m[(int)*t] != st) |
---|
| 919 | { |
---|
| 920 | if( st != st2 ) |
---|
| 921 | st2 = st, *p++ = st ? '+' : '-'; |
---|
| 922 | *p++ = *t; |
---|
| 923 | } |
---|
[b7d3cc34] | 924 | m[(int)*t] = st; |
---|
[2f13222] | 925 | } |
---|
| 926 | else |
---|
| 927 | badflag = 1; |
---|
[b7d3cc34] | 928 | } |
---|
[2f13222] | 929 | *p = '\0'; |
---|
[b7d3cc34] | 930 | |
---|
| 931 | memset( irc->umode, 0, sizeof( irc->umode ) ); |
---|
| 932 | |
---|
| 933 | for( i = 0; i < 256 && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ ) |
---|
[238f828] | 934 | if( m[i] ) |
---|
[b7d3cc34] | 935 | irc->umode[strlen(irc->umode)] = i; |
---|
| 936 | |
---|
[2f13222] | 937 | if( badflag ) |
---|
| 938 | irc_reply( irc, 501, ":Unknown MODE flag" ); |
---|
| 939 | /* Deliberately no !user@host on the prefix here */ |
---|
| 940 | if( *changes ) |
---|
| 941 | irc_write( irc, ":%s MODE %s %s", irc->nick, irc->nick, changes ); |
---|
[b7d3cc34] | 942 | } |
---|
| 943 | |
---|
| 944 | void irc_spawn( irc_t *irc, user_t *u ) |
---|
| 945 | { |
---|
| 946 | irc_join( irc, u, irc->channel ); |
---|
| 947 | } |
---|
| 948 | |
---|
| 949 | void irc_join( irc_t *irc, user_t *u, char *channel ) |
---|
| 950 | { |
---|
| 951 | char *nick; |
---|
| 952 | |
---|
| 953 | if( ( g_strcasecmp( channel, irc->channel ) != 0 ) || user_find( irc, irc->nick ) ) |
---|
| 954 | irc_write( irc, ":%s!%s@%s JOIN :%s", u->nick, u->user, u->host, channel ); |
---|
| 955 | |
---|
| 956 | if( nick_cmp( u->nick, irc->nick ) == 0 ) |
---|
| 957 | { |
---|
| 958 | irc_write( irc, ":%s MODE %s +%s", irc->myhost, channel, CMODE ); |
---|
| 959 | irc_names( irc, channel ); |
---|
| 960 | irc_topic( irc, channel ); |
---|
| 961 | } |
---|
| 962 | |
---|
| 963 | nick = g_strdup( u->nick ); |
---|
| 964 | nick_lc( nick ); |
---|
| 965 | if( g_hash_table_lookup( irc->watches, nick ) ) |
---|
| 966 | { |
---|
[fc630f9] | 967 | irc_reply( irc, 600, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged online" ); |
---|
[b7d3cc34] | 968 | } |
---|
| 969 | g_free( nick ); |
---|
| 970 | } |
---|
| 971 | |
---|
| 972 | void irc_part( irc_t *irc, user_t *u, char *channel ) |
---|
| 973 | { |
---|
| 974 | irc_write( irc, ":%s!%s@%s PART %s :%s", u->nick, u->user, u->host, channel, "" ); |
---|
| 975 | } |
---|
| 976 | |
---|
| 977 | void irc_kick( irc_t *irc, user_t *u, char *channel, user_t *kicker ) |
---|
| 978 | { |
---|
| 979 | irc_write( irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, kicker->host, channel, u->nick, "" ); |
---|
| 980 | } |
---|
| 981 | |
---|
| 982 | void irc_kill( irc_t *irc, user_t *u ) |
---|
| 983 | { |
---|
[fb62f81f] | 984 | char *nick, *s; |
---|
[0a3c243] | 985 | char reason[128]; |
---|
[fb62f81f] | 986 | |
---|
[1186382] | 987 | if( u->ic && u->ic->flags & OPT_LOGGING_OUT && set_getbool( &irc->set, "simulate_netsplit" ) ) |
---|
[fb62f81f] | 988 | { |
---|
[0da65d5] | 989 | if( u->ic->acc->server ) |
---|
[fb62f81f] | 990 | g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost, |
---|
[0da65d5] | 991 | u->ic->acc->server ); |
---|
[c2fb3809] | 992 | else if( ( s = strchr( u->ic->acc->user, '@' ) ) ) |
---|
[fb62f81f] | 993 | g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost, |
---|
| 994 | s + 1 ); |
---|
| 995 | else |
---|
| 996 | g_snprintf( reason, sizeof( reason ), "%s %s.%s", irc->myhost, |
---|
[0da65d5] | 997 | u->ic->acc->prpl->name, irc->myhost ); |
---|
[fb62f81f] | 998 | |
---|
| 999 | /* proto_opt might contain garbage after the : */ |
---|
| 1000 | if( ( s = strchr( reason, ':' ) ) ) |
---|
| 1001 | *s = 0; |
---|
| 1002 | } |
---|
| 1003 | else |
---|
| 1004 | { |
---|
| 1005 | strcpy( reason, "Leaving..." ); |
---|
| 1006 | } |
---|
[b7d3cc34] | 1007 | |
---|
[fb62f81f] | 1008 | irc_write( irc, ":%s!%s@%s QUIT :%s", u->nick, u->user, u->host, reason ); |
---|
[b7d3cc34] | 1009 | |
---|
| 1010 | nick = g_strdup( u->nick ); |
---|
| 1011 | nick_lc( nick ); |
---|
| 1012 | if( g_hash_table_lookup( irc->watches, nick ) ) |
---|
| 1013 | { |
---|
[fc630f9] | 1014 | irc_reply( irc, 601, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged offline" ); |
---|
[b7d3cc34] | 1015 | } |
---|
| 1016 | g_free( nick ); |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | int irc_send( irc_t *irc, char *nick, char *s, int flags ) |
---|
| 1020 | { |
---|
[0da65d5] | 1021 | struct groupchat *c = NULL; |
---|
[b7d3cc34] | 1022 | user_t *u = NULL; |
---|
| 1023 | |
---|
[94281ef] | 1024 | if( *nick == '#' || *nick == '&' ) |
---|
[b7d3cc34] | 1025 | { |
---|
[0e7ab64] | 1026 | if( !( c = irc_chat_by_channel( irc, nick ) ) ) |
---|
[b7d3cc34] | 1027 | { |
---|
| 1028 | irc_reply( irc, 403, "%s :Channel does not exist", nick ); |
---|
| 1029 | return( 0 ); |
---|
| 1030 | } |
---|
| 1031 | } |
---|
| 1032 | else |
---|
| 1033 | { |
---|
| 1034 | u = user_find( irc, nick ); |
---|
| 1035 | |
---|
| 1036 | if( !u ) |
---|
| 1037 | { |
---|
| 1038 | if( irc->is_private ) |
---|
| 1039 | irc_reply( irc, 401, "%s :Nick does not exist", nick ); |
---|
| 1040 | else |
---|
| 1041 | irc_usermsg( irc, "Nick `%s' does not exist!", nick ); |
---|
| 1042 | return( 0 ); |
---|
| 1043 | } |
---|
| 1044 | } |
---|
| 1045 | |
---|
| 1046 | if( *s == 1 && s[strlen(s)-1] == 1 ) |
---|
| 1047 | { |
---|
| 1048 | if( g_strncasecmp( s + 1, "ACTION", 6 ) == 0 ) |
---|
| 1049 | { |
---|
| 1050 | if( s[7] == ' ' ) s ++; |
---|
| 1051 | s += 3; |
---|
| 1052 | *(s++) = '/'; |
---|
| 1053 | *(s++) = 'm'; |
---|
| 1054 | *(s++) = 'e'; |
---|
| 1055 | *(s++) = ' '; |
---|
| 1056 | s -= 4; |
---|
| 1057 | s[strlen(s)-1] = 0; |
---|
| 1058 | } |
---|
| 1059 | else if( g_strncasecmp( s + 1, "VERSION", 7 ) == 0 ) |
---|
| 1060 | { |
---|
| 1061 | u = user_find( irc, irc->mynick ); |
---|
| 1062 | irc_privmsg( irc, u, "NOTICE", irc->nick, "", "\001VERSION BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\001" ); |
---|
| 1063 | return( 1 ); |
---|
| 1064 | } |
---|
| 1065 | else if( g_strncasecmp( s + 1, "PING", 4 ) == 0 ) |
---|
| 1066 | { |
---|
| 1067 | u = user_find( irc, irc->mynick ); |
---|
| 1068 | irc_privmsg( irc, u, "NOTICE", irc->nick, "", s ); |
---|
| 1069 | return( 1 ); |
---|
| 1070 | } |
---|
| 1071 | else if( g_strncasecmp( s + 1, "TYPING", 6 ) == 0 ) |
---|
| 1072 | { |
---|
[0da65d5] | 1073 | if( u && u->ic && u->ic->acc->prpl->send_typing && strlen( s ) >= 10 ) |
---|
[b7d3cc34] | 1074 | { |
---|
| 1075 | time_t current_typing_notice = time( NULL ); |
---|
| 1076 | |
---|
| 1077 | if( current_typing_notice - u->last_typing_notice >= 5 ) |
---|
| 1078 | { |
---|
[df1fb67] | 1079 | u->ic->acc->prpl->send_typing( u->ic, u->handle, ( s[8] - '0' ) << 8 ); |
---|
[b7d3cc34] | 1080 | u->last_typing_notice = current_typing_notice; |
---|
| 1081 | } |
---|
| 1082 | } |
---|
| 1083 | return( 1 ); |
---|
| 1084 | } |
---|
| 1085 | else |
---|
| 1086 | { |
---|
| 1087 | irc_usermsg( irc, "Non-ACTION CTCP's aren't supported" ); |
---|
| 1088 | return( 0 ); |
---|
| 1089 | } |
---|
| 1090 | } |
---|
| 1091 | |
---|
| 1092 | if( u ) |
---|
| 1093 | { |
---|
| 1094 | /* For the next message, we probably do have to send new notices... */ |
---|
| 1095 | u->last_typing_notice = 0; |
---|
| 1096 | u->is_private = irc->is_private; |
---|
| 1097 | |
---|
| 1098 | if( u->is_private ) |
---|
| 1099 | { |
---|
| 1100 | if( !u->online ) |
---|
| 1101 | irc_reply( irc, 301, "%s :%s", u->nick, "User is offline" ); |
---|
| 1102 | else if( u->away ) |
---|
| 1103 | irc_reply( irc, 301, "%s :%s", u->nick, u->away ); |
---|
| 1104 | } |
---|
| 1105 | |
---|
| 1106 | if( u->send_handler ) |
---|
[f73b969] | 1107 | { |
---|
| 1108 | u->send_handler( irc, u, s, flags ); |
---|
| 1109 | return 1; |
---|
| 1110 | } |
---|
[b7d3cc34] | 1111 | } |
---|
[0da65d5] | 1112 | else if( c && c->ic && c->ic->acc && c->ic->acc->prpl ) |
---|
[b7d3cc34] | 1113 | { |
---|
[84b045d] | 1114 | return( imc_chat_msg( c, s, 0 ) ); |
---|
[b7d3cc34] | 1115 | } |
---|
| 1116 | |
---|
| 1117 | return( 0 ); |
---|
| 1118 | } |
---|
| 1119 | |
---|
[ba9edaa] | 1120 | static gboolean buddy_send_handler_delayed( gpointer data, gint fd, b_input_condition cond ) |
---|
[b7d3cc34] | 1121 | { |
---|
| 1122 | user_t *u = data; |
---|
| 1123 | |
---|
[226fce1] | 1124 | /* Shouldn't happen, but just to be sure. */ |
---|
| 1125 | if( u->sendbuf_len < 2 ) |
---|
| 1126 | return FALSE; |
---|
| 1127 | |
---|
[b7d3cc34] | 1128 | u->sendbuf[u->sendbuf_len-2] = 0; /* Cut off the last newline */ |
---|
[84b045d] | 1129 | imc_buddy_msg( u->ic, u->handle, u->sendbuf, u->sendbuf_flags ); |
---|
[b7d3cc34] | 1130 | |
---|
| 1131 | g_free( u->sendbuf ); |
---|
| 1132 | u->sendbuf = NULL; |
---|
| 1133 | u->sendbuf_len = 0; |
---|
| 1134 | u->sendbuf_timer = 0; |
---|
| 1135 | u->sendbuf_flags = 0; |
---|
| 1136 | |
---|
[ba9edaa] | 1137 | return FALSE; |
---|
[b7d3cc34] | 1138 | } |
---|
| 1139 | |
---|
[f73b969] | 1140 | void buddy_send_handler( irc_t *irc, user_t *u, char *msg, int flags ) |
---|
[b7d3cc34] | 1141 | { |
---|
[0da65d5] | 1142 | if( !u || !u->ic ) return; |
---|
[b7d3cc34] | 1143 | |
---|
[d5ccd83] | 1144 | if( set_getbool( &irc->set, "buddy_sendbuffer" ) && set_getint( &irc->set, "buddy_sendbuffer_delay" ) > 0 ) |
---|
[b7d3cc34] | 1145 | { |
---|
[834ff44] | 1146 | int delay; |
---|
| 1147 | |
---|
[b7d3cc34] | 1148 | if( u->sendbuf_len > 0 && u->sendbuf_flags != flags) |
---|
| 1149 | { |
---|
[226fce1] | 1150 | /* Flush the buffer */ |
---|
[ba9edaa] | 1151 | b_event_remove( u->sendbuf_timer ); |
---|
| 1152 | buddy_send_handler_delayed( u, -1, 0 ); |
---|
[b7d3cc34] | 1153 | } |
---|
| 1154 | |
---|
| 1155 | if( u->sendbuf_len == 0 ) |
---|
| 1156 | { |
---|
| 1157 | u->sendbuf_len = strlen( msg ) + 2; |
---|
[226fce1] | 1158 | u->sendbuf = g_new( char, u->sendbuf_len ); |
---|
[b7d3cc34] | 1159 | u->sendbuf[0] = 0; |
---|
| 1160 | u->sendbuf_flags = flags; |
---|
| 1161 | } |
---|
| 1162 | else |
---|
| 1163 | { |
---|
| 1164 | u->sendbuf_len += strlen( msg ) + 1; |
---|
[226fce1] | 1165 | u->sendbuf = g_renew( char, u->sendbuf, u->sendbuf_len ); |
---|
[b7d3cc34] | 1166 | } |
---|
| 1167 | |
---|
| 1168 | strcat( u->sendbuf, msg ); |
---|
| 1169 | strcat( u->sendbuf, "\n" ); |
---|
| 1170 | |
---|
[5c9512f] | 1171 | delay = set_getint( &irc->set, "buddy_sendbuffer_delay" ); |
---|
[834ff44] | 1172 | if( delay <= 5 ) |
---|
| 1173 | delay *= 1000; |
---|
| 1174 | |
---|
[b7d3cc34] | 1175 | if( u->sendbuf_timer > 0 ) |
---|
[ba9edaa] | 1176 | b_event_remove( u->sendbuf_timer ); |
---|
| 1177 | u->sendbuf_timer = b_timeout_add( delay, buddy_send_handler_delayed, u ); |
---|
[b7d3cc34] | 1178 | } |
---|
| 1179 | else |
---|
| 1180 | { |
---|
[84b045d] | 1181 | imc_buddy_msg( u->ic, u->handle, msg, flags ); |
---|
[b7d3cc34] | 1182 | } |
---|
| 1183 | } |
---|
| 1184 | |
---|
| 1185 | int irc_privmsg( irc_t *irc, user_t *u, char *type, char *to, char *prefix, char *msg ) |
---|
| 1186 | { |
---|
| 1187 | char last = 0; |
---|
| 1188 | char *s = msg, *line = msg; |
---|
| 1189 | |
---|
| 1190 | /* The almighty linesplitter .. woohoo!! */ |
---|
| 1191 | while( !last ) |
---|
| 1192 | { |
---|
| 1193 | if( *s == '\r' && *(s+1) == '\n' ) |
---|
| 1194 | *(s++) = 0; |
---|
| 1195 | if( *s == '\n' ) |
---|
| 1196 | { |
---|
| 1197 | last = s[1] == 0; |
---|
| 1198 | *s = 0; |
---|
| 1199 | } |
---|
| 1200 | else |
---|
| 1201 | { |
---|
| 1202 | last = s[0] == 0; |
---|
| 1203 | } |
---|
| 1204 | if( *s == 0 ) |
---|
| 1205 | { |
---|
| 1206 | if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) && g_strcasecmp( type, "PRIVMSG" ) == 0 ) |
---|
| 1207 | { |
---|
| 1208 | irc_write( irc, ":%s!%s@%s %s %s :\001ACTION %s\001", u->nick, u->user, u->host, |
---|
| 1209 | type, to, line + 4 ); |
---|
| 1210 | } |
---|
| 1211 | else |
---|
| 1212 | { |
---|
| 1213 | irc_write( irc, ":%s!%s@%s %s %s :%s%s", u->nick, u->user, u->host, |
---|
[25d1be7] | 1214 | type, to, prefix ? prefix : "", line ); |
---|
[b7d3cc34] | 1215 | } |
---|
| 1216 | line = s + 1; |
---|
| 1217 | } |
---|
| 1218 | s ++; |
---|
| 1219 | } |
---|
| 1220 | |
---|
| 1221 | return( 1 ); |
---|
| 1222 | } |
---|
| 1223 | |
---|
| 1224 | int irc_msgfrom( irc_t *irc, char *nick, char *msg ) |
---|
| 1225 | { |
---|
| 1226 | user_t *u = user_find( irc, nick ); |
---|
| 1227 | static char *prefix = NULL; |
---|
| 1228 | |
---|
| 1229 | if( !u ) return( 0 ); |
---|
| 1230 | if( prefix && *prefix ) g_free( prefix ); |
---|
| 1231 | |
---|
| 1232 | if( !u->is_private && nick_cmp( u->nick, irc->mynick ) != 0 ) |
---|
| 1233 | { |
---|
| 1234 | int len = strlen( irc->nick) + 3; |
---|
| 1235 | prefix = g_new (char, len ); |
---|
[5c9512f] | 1236 | g_snprintf( prefix, len, "%s%s", irc->nick, set_getstr( &irc->set, "to_char" ) ); |
---|
[b7d3cc34] | 1237 | prefix[len-1] = 0; |
---|
| 1238 | } |
---|
| 1239 | else |
---|
| 1240 | { |
---|
| 1241 | prefix = ""; |
---|
| 1242 | } |
---|
| 1243 | |
---|
| 1244 | return( irc_privmsg( irc, u, "PRIVMSG", u->is_private ? irc->nick : irc->channel, prefix, msg ) ); |
---|
| 1245 | } |
---|
| 1246 | |
---|
| 1247 | int irc_noticefrom( irc_t *irc, char *nick, char *msg ) |
---|
| 1248 | { |
---|
| 1249 | user_t *u = user_find( irc, nick ); |
---|
| 1250 | |
---|
| 1251 | if( u ) |
---|
| 1252 | return( irc_privmsg( irc, u, "NOTICE", irc->nick, "", msg ) ); |
---|
| 1253 | else |
---|
| 1254 | return( 0 ); |
---|
| 1255 | } |
---|
| 1256 | |
---|
| 1257 | /* Returns 0 if everything seems to be okay, a number >0 when there was a |
---|
| 1258 | timeout. The number returned is the number of seconds we received no |
---|
| 1259 | pongs from the user. When not connected yet, we don't ping but drop the |
---|
| 1260 | connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ |
---|
[ba9edaa] | 1261 | static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond ) |
---|
[b7d3cc34] | 1262 | { |
---|
| 1263 | irc_t *irc = _irc; |
---|
| 1264 | int rv = 0; |
---|
| 1265 | |
---|
[3af70b0] | 1266 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
[b7d3cc34] | 1267 | { |
---|
| 1268 | if( gettime() > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) ) |
---|
| 1269 | rv = gettime() - irc->last_pong; |
---|
| 1270 | } |
---|
| 1271 | else |
---|
| 1272 | { |
---|
| 1273 | if( ( gettime() > ( irc->last_pong + global.conf->ping_interval ) ) && !irc->pinging ) |
---|
| 1274 | { |
---|
| 1275 | irc_write( irc, "PING :%s", IRC_PING_STRING ); |
---|
| 1276 | irc->pinging = 1; |
---|
| 1277 | } |
---|
| 1278 | else if( gettime() > ( irc->last_pong + global.conf->ping_timeout ) ) |
---|
| 1279 | { |
---|
| 1280 | rv = gettime() - irc->last_pong; |
---|
| 1281 | } |
---|
| 1282 | } |
---|
| 1283 | |
---|
| 1284 | if( rv > 0 ) |
---|
| 1285 | { |
---|
[f73b969] | 1286 | irc_abort( irc, 0, "Ping Timeout: %d seconds", rv ); |
---|
[b7d3cc34] | 1287 | return FALSE; |
---|
| 1288 | } |
---|
| 1289 | |
---|
| 1290 | return TRUE; |
---|
| 1291 | } |
---|
[0e7ab64] | 1292 | |
---|
| 1293 | struct groupchat *irc_chat_by_channel( irc_t *irc, char *channel ) |
---|
| 1294 | { |
---|
| 1295 | struct groupchat *c; |
---|
| 1296 | account_t *a; |
---|
| 1297 | |
---|
| 1298 | /* This finds the connection which has a conversation which belongs to this channel */ |
---|
| 1299 | for( a = irc->accounts; a; a = a->next ) |
---|
| 1300 | { |
---|
[bdda9e9] | 1301 | if( a->ic == NULL ) |
---|
| 1302 | continue; |
---|
| 1303 | |
---|
| 1304 | c = a->ic->groupchats; |
---|
| 1305 | while( c ) |
---|
| 1306 | { |
---|
| 1307 | if( c->channel && g_strcasecmp( c->channel, channel ) == 0 ) |
---|
| 1308 | return c; |
---|
| 1309 | |
---|
| 1310 | c = c->next; |
---|
| 1311 | } |
---|
[0e7ab64] | 1312 | } |
---|
| 1313 | |
---|
| 1314 | return NULL; |
---|
| 1315 | } |
---|