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