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