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