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