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