[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
[3ddb7477] | 7 | /* The IRC-based UI (for now the only one) */ |
---|
[b7d3cc34] | 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; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 24 | */ |
---|
| 25 | |
---|
| 26 | #include "bitlbee.h" |
---|
[fb117aee] | 27 | #include "ipc.h" |
---|
[2ff2076] | 28 | #include "dcc.h" |
---|
[59e66ff] | 29 | #include "lib/ssl_client.h" |
---|
[b7d3cc34] | 30 | |
---|
[3ddb7477] | 31 | GSList *irc_connection_list; |
---|
[0c85c08] | 32 | GSList *irc_plugins; |
---|
[b7d3cc34] | 33 | |
---|
[5ebff60] | 34 | static gboolean irc_userping(gpointer _irc, gint fd, b_input_condition cond); |
---|
| 35 | static char *set_eval_charset(set_t *set, char *value); |
---|
| 36 | static char *set_eval_password(set_t *set, char *value); |
---|
| 37 | static char *set_eval_bw_compat(set_t *set, char *value); |
---|
| 38 | static char *set_eval_utf8_nicks(set_t *set, char *value); |
---|
[58adb7e] | 39 | |
---|
[5ebff60] | 40 | irc_t *irc_new(int fd) |
---|
[b7d3cc34] | 41 | { |
---|
[e4d6271] | 42 | irc_t *irc; |
---|
[3ddb7477] | 43 | irc_user_t *iu; |
---|
[0c85c08] | 44 | GSList *l; |
---|
[7125cb3] | 45 | set_t *s; |
---|
[3ddb7477] | 46 | bee_t *b; |
---|
[5ebff60] | 47 | |
---|
| 48 | irc = g_new0(irc_t, 1); |
---|
| 49 | |
---|
[b7d3cc34] | 50 | irc->fd = fd; |
---|
[5ebff60] | 51 | sock_make_nonblocking(irc->fd); |
---|
| 52 | |
---|
| 53 | irc->r_watch_source_id = b_input_add(irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc); |
---|
| 54 | |
---|
[b7d3cc34] | 55 | irc->status = USTATUS_OFFLINE; |
---|
| 56 | irc->last_pong = gettime(); |
---|
[5ebff60] | 57 | |
---|
| 58 | irc->nick_user_hash = g_hash_table_new(g_str_hash, g_str_equal); |
---|
| 59 | irc->watches = g_hash_table_new(g_str_hash, g_str_equal); |
---|
| 60 | |
---|
| 61 | irc->iconv = (GIConv) - 1; |
---|
| 62 | irc->oconv = (GIConv) - 1; |
---|
| 63 | |
---|
| 64 | if (global.conf->ping_interval > 0 && global.conf->ping_timeout > 0) { |
---|
| 65 | irc->ping_source_id = b_timeout_add(global.conf->ping_interval * 1000, irc_userping, irc); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | irc_connection_list = g_slist_append(irc_connection_list, irc); |
---|
| 69 | |
---|
[3ddb7477] | 70 | b = irc->b = bee_new(); |
---|
[d860a8d] | 71 | b->ui_data = irc; |
---|
| 72 | b->ui = &irc_ui_funcs; |
---|
[5ebff60] | 73 | |
---|
| 74 | s = set_add(&b->set, "allow_takeover", "true", set_eval_bool, irc); |
---|
| 75 | s = set_add(&b->set, "away_devoice", "true", set_eval_bw_compat, irc); |
---|
[a758ec1] | 76 | s->flags |= SET_HIDDEN; |
---|
[5ebff60] | 77 | s = set_add(&b->set, "away_reply_timeout", "3600", set_eval_int, irc); |
---|
| 78 | s = set_add(&b->set, "charset", "utf-8", set_eval_charset, irc); |
---|
| 79 | s = set_add(&b->set, "default_target", "root", NULL, irc); |
---|
| 80 | s = set_add(&b->set, "display_namechanges", "false", set_eval_bool, irc); |
---|
| 81 | s = set_add(&b->set, "display_timestamps", "true", set_eval_bool, irc); |
---|
| 82 | s = set_add(&b->set, "handle_unknown", "add_channel", NULL, irc); |
---|
| 83 | s = set_add(&b->set, "last_version", "0", NULL, irc); |
---|
[180ab31] | 84 | s->flags |= SET_HIDDEN; |
---|
[5ebff60] | 85 | s = set_add(&b->set, "nick_format", "%-@nick", NULL, irc); |
---|
[90254d0] | 86 | s = set_add(&b->set, "nick_lowercase", "false", set_eval_bool, irc); |
---|
| 87 | s = set_add(&b->set, "nick_underscores", "false", set_eval_bool, irc); |
---|
[5ebff60] | 88 | s = set_add(&b->set, "offline_user_quits", "true", set_eval_bool, irc); |
---|
| 89 | s = set_add(&b->set, "ops", "both", set_eval_irc_channel_ops, irc); |
---|
| 90 | s = set_add(&b->set, "paste_buffer", "false", set_eval_bool, irc); |
---|
| 91 | s->old_key = g_strdup("buddy_sendbuffer"); |
---|
| 92 | s = set_add(&b->set, "paste_buffer_delay", "200", set_eval_int, irc); |
---|
| 93 | s->old_key = g_strdup("buddy_sendbuffer_delay"); |
---|
| 94 | s = set_add(&b->set, "password", NULL, set_eval_password, irc); |
---|
[09d4922] | 95 | s->flags |= SET_NULL_OK | SET_PASSWORD; |
---|
[5ebff60] | 96 | s = set_add(&b->set, "private", "true", set_eval_bool, irc); |
---|
| 97 | s = set_add(&b->set, "query_order", "lifo", NULL, irc); |
---|
| 98 | s = set_add(&b->set, "root_nick", ROOT_NICK, set_eval_root_nick, irc); |
---|
[180ab31] | 99 | s->flags |= SET_HIDDEN; |
---|
[5ebff60] | 100 | s = set_add(&b->set, "show_offline", "false", set_eval_bw_compat, irc); |
---|
[a758ec1] | 101 | s->flags |= SET_HIDDEN; |
---|
[345577b] | 102 | s = set_add(&b->set, "self_messages", "true", set_eval_self_messages, irc); |
---|
[5ebff60] | 103 | s = set_add(&b->set, "simulate_netsplit", "true", set_eval_bool, irc); |
---|
| 104 | s = set_add(&b->set, "timezone", "local", set_eval_timezone, irc); |
---|
| 105 | s = set_add(&b->set, "to_char", ": ", set_eval_to_char, irc); |
---|
| 106 | s = set_add(&b->set, "typing_notice", "false", set_eval_bool, irc); |
---|
| 107 | s = set_add(&b->set, "utf8_nicks", "false", set_eval_utf8_nicks, irc); |
---|
| 108 | |
---|
| 109 | irc->root = iu = irc_user_new(irc, ROOT_NICK); |
---|
| 110 | iu->fullname = g_strdup(ROOT_FN); |
---|
[280c56a] | 111 | iu->f = &irc_user_root_funcs; |
---|
[5ebff60] | 112 | |
---|
| 113 | iu = irc_user_new(irc, NS_NICK); |
---|
| 114 | iu->fullname = g_strdup(ROOT_FN); |
---|
[280c56a] | 115 | iu->f = &irc_user_root_funcs; |
---|
[5ebff60] | 116 | |
---|
| 117 | irc->user = g_new0(irc_user_t, 1); |
---|
[df67b48] | 118 | |
---|
| 119 | irc_set_hosts(irc, NULL, 0); |
---|
[5ebff60] | 120 | |
---|
| 121 | conf_loaddefaults(irc); |
---|
| 122 | |
---|
[f9756bd] | 123 | /* Evaluator sets the iconv/oconv structures. */ |
---|
[5ebff60] | 124 | set_eval_charset(set_find(&b->set, "charset"), set_getstr(&b->set, "charset")); |
---|
| 125 | |
---|
[b39859e] | 126 | irc_write(irc, ":%s NOTICE * :%s", irc->root->host, "BitlBee-IRCd initialized, please go on"); |
---|
[5ebff60] | 127 | if (isatty(irc->fd)) { |
---|
[b39859e] | 128 | irc_write(irc, ":%s NOTICE * :%s", irc->root->host, |
---|
[5ebff60] | 129 | "If you read this, you most likely accidentally " |
---|
| 130 | "started BitlBee in inetd mode on the command line. " |
---|
| 131 | "You probably want to run it in (Fork)Daemon mode. " |
---|
| 132 | "See doc/README for more information."); |
---|
| 133 | } |
---|
| 134 | |
---|
[3fc6c32] | 135 | /* libpurple doesn't like fork()s after initializing itself, so this |
---|
| 136 | is the right moment to initialize it. */ |
---|
| 137 | #ifdef WITH_PURPLE |
---|
[5674207] | 138 | nogaim_init(); |
---|
[3fc6c32] | 139 | #endif |
---|
[59e66ff] | 140 | |
---|
| 141 | /* SSL library initialization also should be done after the fork, to |
---|
| 142 | avoid shared CSPRNG state. This is required by NSS, which refuses to |
---|
| 143 | work if a fork is detected */ |
---|
| 144 | ssl_init(); |
---|
[5ebff60] | 145 | |
---|
| 146 | for (l = irc_plugins; l; l = l->next) { |
---|
[0c85c08] | 147 | irc_plugin_t *p = l->data; |
---|
[5ebff60] | 148 | if (p->irc_new) { |
---|
| 149 | p->irc_new(irc); |
---|
| 150 | } |
---|
[0c85c08] | 151 | } |
---|
[5ebff60] | 152 | |
---|
[3ddb7477] | 153 | return irc; |
---|
[b7d3cc34] | 154 | } |
---|
| 155 | |
---|
[df67b48] | 156 | void irc_set_hosts(irc_t *irc, const struct sockaddr *remote_addr, const socklen_t remote_addrlen) |
---|
| 157 | { |
---|
| 158 | struct sockaddr_storage sock; |
---|
| 159 | socklen_t socklen = sizeof(sock); |
---|
| 160 | char *host = NULL, *myhost = NULL; |
---|
| 161 | struct irc_user *iu; |
---|
| 162 | |
---|
| 163 | if (global.conf->hostname) { |
---|
| 164 | myhost = g_strdup(global.conf->hostname); |
---|
| 165 | } else if (getsockname(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { |
---|
| 166 | myhost = reverse_lookup((struct sockaddr*) &sock, socklen); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | if (remote_addrlen > 0) { |
---|
| 170 | host = reverse_lookup(remote_addr, remote_addrlen); |
---|
| 171 | } else if (getpeername(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { |
---|
| 172 | host = reverse_lookup((struct sockaddr*) &sock, socklen); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | if (myhost == NULL) { |
---|
| 176 | myhost = g_strdup("localhost.localdomain"); |
---|
| 177 | } |
---|
| 178 | if (host == NULL) { |
---|
| 179 | host = g_strdup("localhost.localdomain"); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if (irc->root->host != irc->root->nick) { |
---|
| 183 | g_free(irc->root->host); |
---|
| 184 | } |
---|
| 185 | irc->root->host = g_strdup(myhost); |
---|
| 186 | if ((iu = irc_user_by_name(irc, NS_NICK))) { |
---|
| 187 | if (iu->host != iu->nick) { |
---|
| 188 | g_free(iu->host); |
---|
| 189 | } |
---|
| 190 | iu->host = g_strdup(myhost); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | if (irc->user->host != irc->user->nick) { |
---|
| 194 | g_free(irc->user->host); |
---|
| 195 | } |
---|
| 196 | irc->user->host = g_strdup(host); |
---|
| 197 | |
---|
| 198 | g_free(myhost); |
---|
| 199 | g_free(host); |
---|
| 200 | } |
---|
| 201 | |
---|
[f73b969] | 202 | /* immed=1 makes this function pretty much equal to irc_free(), except that |
---|
| 203 | this one will "log". In case the connection is already broken and we |
---|
| 204 | shouldn't try to write to it. */ |
---|
[5ebff60] | 205 | void irc_abort(irc_t *irc, int immed, char *format, ...) |
---|
[c1826c6] | 206 | { |
---|
[82ca986] | 207 | char *reason = NULL; |
---|
[5ebff60] | 208 | |
---|
| 209 | if (format != NULL) { |
---|
[f73b969] | 210 | va_list params; |
---|
[5ebff60] | 211 | |
---|
| 212 | va_start(params, format); |
---|
| 213 | reason = g_strdup_vprintf(format, params); |
---|
| 214 | va_end(params); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | if (reason) { |
---|
| 218 | irc_write(irc, "ERROR :Closing link: %s", reason); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | ipc_to_master_str("OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
| 222 | irc->user->nick ? irc->user->nick : "(NONE)", |
---|
| 223 | irc->user->host, reason ? : ""); |
---|
| 224 | |
---|
| 225 | g_free(reason); |
---|
| 226 | |
---|
| 227 | irc_flush(irc); |
---|
| 228 | if (immed) { |
---|
| 229 | irc_free(irc); |
---|
| 230 | } else { |
---|
| 231 | b_event_remove(irc->ping_source_id); |
---|
| 232 | irc->ping_source_id = b_timeout_add(1, (b_event_handler) irc_free, irc); |
---|
[c1826c6] | 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
[5ebff60] | 236 | static gboolean irc_free_hashkey(gpointer key, gpointer value, gpointer data); |
---|
[b7d3cc34] | 237 | |
---|
[5ebff60] | 238 | void irc_free(irc_t * irc) |
---|
[b7d3cc34] | 239 | { |
---|
[0c85c08] | 240 | GSList *l; |
---|
[5ebff60] | 241 | |
---|
[82ca986] | 242 | irc->status |= USTATUS_SHUTDOWN; |
---|
[5ebff60] | 243 | |
---|
| 244 | log_message(LOGLVL_INFO, "Destroying connection with fd %d", irc->fd); |
---|
| 245 | |
---|
| 246 | if (irc->status & USTATUS_IDENTIFIED && set_getbool(&irc->b->set, "save_on_quit")) { |
---|
| 247 | if (storage_save(irc, NULL, TRUE) != STORAGE_OK) { |
---|
| 248 | log_message(LOGLVL_WARNING, "Error while saving settings for user %s", irc->user->nick); |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | for (l = irc_plugins; l; l = l->next) { |
---|
[0c85c08] | 253 | irc_plugin_t *p = l->data; |
---|
[5ebff60] | 254 | if (p->irc_free) { |
---|
| 255 | p->irc_free(irc); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | irc_connection_list = g_slist_remove(irc_connection_list, irc); |
---|
| 260 | |
---|
| 261 | while (irc->queries != NULL) { |
---|
| 262 | query_del(irc, irc->queries); |
---|
| 263 | } |
---|
| 264 | |
---|
[d33679e] | 265 | /* This is a little bit messy: bee_free() frees all b->users which |
---|
| 266 | calls us back to free the corresponding irc->users. So do this |
---|
| 267 | before we clear the remaining ones ourselves. */ |
---|
[5ebff60] | 268 | bee_free(irc->b); |
---|
| 269 | |
---|
| 270 | while (irc->users) { |
---|
| 271 | irc_user_free(irc, (irc_user_t *) irc->users->data); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | while (irc->channels) { |
---|
| 275 | irc_channel_free(irc->channels->data); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | if (irc->ping_source_id > 0) { |
---|
| 279 | b_event_remove(irc->ping_source_id); |
---|
| 280 | } |
---|
| 281 | if (irc->r_watch_source_id > 0) { |
---|
| 282 | b_event_remove(irc->r_watch_source_id); |
---|
| 283 | } |
---|
| 284 | if (irc->w_watch_source_id > 0) { |
---|
| 285 | b_event_remove(irc->w_watch_source_id); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | closesocket(irc->fd); |
---|
[fa75134] | 289 | irc->fd = -1; |
---|
[5ebff60] | 290 | |
---|
| 291 | g_hash_table_foreach_remove(irc->nick_user_hash, irc_free_hashkey, NULL); |
---|
| 292 | g_hash_table_destroy(irc->nick_user_hash); |
---|
| 293 | |
---|
| 294 | g_hash_table_foreach_remove(irc->watches, irc_free_hashkey, NULL); |
---|
| 295 | g_hash_table_destroy(irc->watches); |
---|
| 296 | |
---|
| 297 | if (irc->iconv != (GIConv) - 1) { |
---|
| 298 | g_iconv_close(irc->iconv); |
---|
| 299 | } |
---|
| 300 | if (irc->oconv != (GIConv) - 1) { |
---|
| 301 | g_iconv_close(irc->oconv); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | g_free(irc->sendbuffer); |
---|
| 305 | g_free(irc->readbuffer); |
---|
| 306 | g_free(irc->password); |
---|
| 307 | |
---|
| 308 | g_free(irc); |
---|
| 309 | |
---|
| 310 | if (global.conf->runmode == RUNMODE_INETD || |
---|
[565a1ea] | 311 | global.conf->runmode == RUNMODE_FORKDAEMON || |
---|
[5ebff60] | 312 | (global.conf->runmode == RUNMODE_DAEMON && |
---|
| 313 | global.listen_socket == -1 && |
---|
| 314 | irc_connection_list == NULL)) { |
---|
[ba9edaa] | 315 | b_main_quit(); |
---|
[5ebff60] | 316 | } |
---|
[b7d3cc34] | 317 | } |
---|
| 318 | |
---|
[5ebff60] | 319 | static gboolean irc_free_hashkey(gpointer key, gpointer value, gpointer data) |
---|
[7cad7b4] | 320 | { |
---|
[5ebff60] | 321 | g_free(key); |
---|
| 322 | |
---|
| 323 | return(TRUE); |
---|
[7cad7b4] | 324 | } |
---|
| 325 | |
---|
[1f92a58] | 326 | /* USE WITH CAUTION! |
---|
| 327 | Sets pass without checking */ |
---|
[5ebff60] | 328 | void irc_setpass(irc_t *irc, const char *pass) |
---|
[1f92a58] | 329 | { |
---|
[5ebff60] | 330 | g_free(irc->password); |
---|
| 331 | |
---|
[1f92a58] | 332 | if (pass) { |
---|
[5ebff60] | 333 | irc->password = g_strdup(pass); |
---|
[1f92a58] | 334 | } else { |
---|
| 335 | irc->password = NULL; |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | |
---|
[5ebff60] | 339 | static char *set_eval_password(set_t *set, char *value) |
---|
[0d9d53e] | 340 | { |
---|
| 341 | irc_t *irc = set->data; |
---|
[5ebff60] | 342 | |
---|
| 343 | if (irc->status & USTATUS_IDENTIFIED && value) { |
---|
| 344 | irc_setpass(irc, value); |
---|
[0d9d53e] | 345 | return NULL; |
---|
[5ebff60] | 346 | } else { |
---|
[0d9d53e] | 347 | return SET_INVALID; |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | |
---|
[5ebff60] | 351 | static char **irc_splitlines(char *buffer); |
---|
[3ddb7477] | 352 | |
---|
[5ebff60] | 353 | void irc_process(irc_t *irc) |
---|
[b7d3cc34] | 354 | { |
---|
[f9756bd] | 355 | char **lines, *temp, **cmd; |
---|
[b7d3cc34] | 356 | int i; |
---|
| 357 | |
---|
[5ebff60] | 358 | if (irc->readbuffer != NULL) { |
---|
| 359 | lines = irc_splitlines(irc->readbuffer); |
---|
| 360 | |
---|
| 361 | for (i = 0; *lines[i] != '\0'; i++) { |
---|
[f9756bd] | 362 | char *conv = NULL; |
---|
[5ebff60] | 363 | |
---|
[18ff38f] | 364 | /* [WvG] If the last line isn't empty, it's an incomplete line and we |
---|
| 365 | should wait for the rest to come in before processing it. */ |
---|
[5ebff60] | 366 | if (lines[i + 1] == NULL) { |
---|
| 367 | temp = g_strdup(lines[i]); |
---|
| 368 | g_free(irc->readbuffer); |
---|
[b7d3cc34] | 369 | irc->readbuffer = temp; |
---|
[5ebff60] | 370 | i++; |
---|
[b7d3cc34] | 371 | break; |
---|
[e27661d] | 372 | } |
---|
[5ebff60] | 373 | |
---|
| 374 | if (irc->iconv != (GIConv) - 1) { |
---|
[f9756bd] | 375 | gsize bytes_read, bytes_written; |
---|
[5ebff60] | 376 | |
---|
| 377 | conv = g_convert_with_iconv(lines[i], -1, irc->iconv, |
---|
| 378 | &bytes_read, &bytes_written, NULL); |
---|
| 379 | |
---|
| 380 | if (conv == NULL || bytes_read != strlen(lines[i])) { |
---|
[fc0cf92] | 381 | /* GLib can do strange things if things are not in the expected charset, |
---|
| 382 | so let's be a little bit paranoid here: */ |
---|
[5ebff60] | 383 | if (irc->status & USTATUS_LOGGED_IN) { |
---|
| 384 | irc_rootmsg(irc, "Error: Charset mismatch detected. The charset " |
---|
| 385 | "setting is currently set to %s, so please make " |
---|
| 386 | "sure your IRC client will send and accept text in " |
---|
| 387 | "that charset, or tell BitlBee which charset to " |
---|
| 388 | "expect by changing the charset setting. See " |
---|
| 389 | "`help set charset' for more information. Your " |
---|
| 390 | "message was ignored.", |
---|
| 391 | set_getstr(&irc->b->set, "charset")); |
---|
| 392 | |
---|
| 393 | g_free(conv); |
---|
[f9756bd] | 394 | conv = NULL; |
---|
[5ebff60] | 395 | } else { |
---|
[b39859e] | 396 | irc_write(irc, ":%s NOTICE * :%s", irc->root->host, |
---|
[5ebff60] | 397 | "Warning: invalid characters received at login time."); |
---|
| 398 | |
---|
| 399 | conv = g_strdup(lines[i]); |
---|
| 400 | for (temp = conv; *temp; temp++) { |
---|
| 401 | if (*temp & 0x80) { |
---|
[fc0cf92] | 402 | *temp = '?'; |
---|
[5ebff60] | 403 | } |
---|
| 404 | } |
---|
[fc0cf92] | 405 | } |
---|
[94d52d64] | 406 | } |
---|
| 407 | lines[i] = conv; |
---|
[e27661d] | 408 | } |
---|
[5ebff60] | 409 | |
---|
| 410 | if (lines[i] && (cmd = irc_parse_line(lines[i]))) { |
---|
| 411 | irc_exec(irc, cmd); |
---|
| 412 | g_free(cmd); |
---|
[f9756bd] | 413 | } |
---|
[5ebff60] | 414 | |
---|
| 415 | g_free(conv); |
---|
| 416 | |
---|
[f73b969] | 417 | /* Shouldn't really happen, but just in case... */ |
---|
[5ebff60] | 418 | if (!g_slist_find(irc_connection_list, irc)) { |
---|
| 419 | g_free(lines); |
---|
[f73b969] | 420 | return; |
---|
[b7d3cc34] | 421 | } |
---|
| 422 | } |
---|
[5ebff60] | 423 | |
---|
| 424 | if (lines[i] != NULL) { |
---|
| 425 | g_free(irc->readbuffer); |
---|
[0431ea1] | 426 | irc->readbuffer = NULL; |
---|
[b7d3cc34] | 427 | } |
---|
[5ebff60] | 428 | |
---|
| 429 | g_free(lines); |
---|
[b7d3cc34] | 430 | } |
---|
| 431 | } |
---|
| 432 | |
---|
[3ddb7477] | 433 | /* Splits a long string into separate lines. The array is NULL-terminated |
---|
| 434 | and, unless the string contains an incomplete line at the end, ends with |
---|
| 435 | an empty string. Could use g_strsplit() but this one does it in-place. |
---|
| 436 | (So yes, it's destructive.) */ |
---|
[5ebff60] | 437 | static char **irc_splitlines(char *buffer) |
---|
[b7d3cc34] | 438 | { |
---|
[18ff38f] | 439 | int i, j, n = 3; |
---|
[b7d3cc34] | 440 | char **lines; |
---|
| 441 | |
---|
[18ff38f] | 442 | /* Allocate n+1 elements. */ |
---|
[5ebff60] | 443 | lines = g_new(char *, n + 1); |
---|
| 444 | |
---|
[de3e100] | 445 | lines[0] = buffer; |
---|
[5ebff60] | 446 | |
---|
[18ff38f] | 447 | /* Split the buffer in several strings, and accept any kind of line endings, |
---|
| 448 | * knowing that ERC on Windows may send something interesting like \r\r\n, |
---|
| 449 | * and surely there must be clients that think just \n is enough... */ |
---|
[5ebff60] | 450 | for (i = 0, j = 0; buffer[i] != '\0'; i++) { |
---|
| 451 | if (buffer[i] == '\r' || buffer[i] == '\n') { |
---|
| 452 | while (buffer[i] == '\r' || buffer[i] == '\n') { |
---|
[18ff38f] | 453 | buffer[i++] = '\0'; |
---|
[5ebff60] | 454 | } |
---|
| 455 | |
---|
[18ff38f] | 456 | lines[++j] = buffer + i; |
---|
[5ebff60] | 457 | |
---|
| 458 | if (j >= n) { |
---|
[18ff38f] | 459 | n *= 2; |
---|
[5ebff60] | 460 | lines = g_renew(char *, lines, n + 1); |
---|
[18ff38f] | 461 | } |
---|
| 462 | |
---|
[5ebff60] | 463 | if (buffer[i] == '\0') { |
---|
[18ff38f] | 464 | break; |
---|
[5ebff60] | 465 | } |
---|
[b7d3cc34] | 466 | } |
---|
| 467 | } |
---|
[5ebff60] | 468 | |
---|
| 469 | /* NULL terminate our list. */ |
---|
[18ff38f] | 470 | lines[++j] = NULL; |
---|
[5ebff60] | 471 | |
---|
[18ff38f] | 472 | return lines; |
---|
[b7d3cc34] | 473 | } |
---|
| 474 | |
---|
[e27661d] | 475 | /* Split an IRC-style line into little parts/arguments. */ |
---|
[5ebff60] | 476 | char **irc_parse_line(char *line) |
---|
[b7d3cc34] | 477 | { |
---|
| 478 | int i, j; |
---|
| 479 | char **cmd; |
---|
[5ebff60] | 480 | |
---|
[b7d3cc34] | 481 | /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ |
---|
[5ebff60] | 482 | if (line[0] == ':') { |
---|
| 483 | for (i = 0; line[i] && line[i] != ' '; i++) { |
---|
| 484 | ; |
---|
| 485 | } |
---|
[de3e100] | 486 | line = line + i; |
---|
[b7d3cc34] | 487 | } |
---|
[5ebff60] | 488 | for (i = 0; line[i] == ' '; i++) { |
---|
| 489 | ; |
---|
| 490 | } |
---|
[de3e100] | 491 | line = line + i; |
---|
[5ebff60] | 492 | |
---|
[b7d3cc34] | 493 | /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ |
---|
[5ebff60] | 494 | if (line[0] == '\0') { |
---|
[de3e100] | 495 | return NULL; |
---|
[5ebff60] | 496 | } |
---|
| 497 | |
---|
[de3e100] | 498 | /* Count the number of char **cmd elements we're going to need. */ |
---|
| 499 | j = 1; |
---|
[5ebff60] | 500 | for (i = 0; line[i] != '\0'; i++) { |
---|
| 501 | if (line[i] == ' ') { |
---|
| 502 | j++; |
---|
| 503 | |
---|
| 504 | if (line[i + 1] == ':') { |
---|
[de3e100] | 505 | break; |
---|
[5ebff60] | 506 | } |
---|
[de3e100] | 507 | } |
---|
[5ebff60] | 508 | } |
---|
[b7d3cc34] | 509 | |
---|
| 510 | /* Allocate the space we need. */ |
---|
[5ebff60] | 511 | cmd = g_new(char *, j + 1); |
---|
[de3e100] | 512 | cmd[j] = NULL; |
---|
[5ebff60] | 513 | |
---|
[b7d3cc34] | 514 | /* Do the actual line splitting, format is: |
---|
| 515 | * Input: "PRIVMSG #bitlbee :foo bar" |
---|
| 516 | * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL |
---|
| 517 | */ |
---|
| 518 | |
---|
[de3e100] | 519 | cmd[0] = line; |
---|
[5ebff60] | 520 | for (i = 0, j = 0; line[i] != '\0'; i++) { |
---|
| 521 | if (line[i] == ' ') { |
---|
[de3e100] | 522 | line[i] = '\0'; |
---|
| 523 | cmd[++j] = line + i + 1; |
---|
[5ebff60] | 524 | |
---|
| 525 | if (line[i + 1] == ':') { |
---|
| 526 | cmd[j]++; |
---|
[b7d3cc34] | 527 | break; |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | } |
---|
[5ebff60] | 531 | |
---|
[de3e100] | 532 | return cmd; |
---|
[b7d3cc34] | 533 | } |
---|
| 534 | |
---|
[e27661d] | 535 | /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ |
---|
[5ebff60] | 536 | char *irc_build_line(char **cmd) |
---|
[74c119d] | 537 | { |
---|
| 538 | int i, len; |
---|
| 539 | char *s; |
---|
[5ebff60] | 540 | |
---|
| 541 | if (cmd[0] == NULL) { |
---|
[74c119d] | 542 | return NULL; |
---|
[5ebff60] | 543 | } |
---|
| 544 | |
---|
[74c119d] | 545 | len = 1; |
---|
[5ebff60] | 546 | for (i = 0; cmd[i]; i++) { |
---|
| 547 | len += strlen(cmd[i]) + 1; |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | if (strchr(cmd[i - 1], ' ') != NULL) { |
---|
| 551 | len++; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | s = g_new0(char, len + 1); |
---|
| 555 | for (i = 0; cmd[i]; i++) { |
---|
| 556 | if (cmd[i + 1] == NULL && strchr(cmd[i], ' ') != NULL) { |
---|
| 557 | strcat(s, ":"); |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | strcat(s, cmd[i]); |
---|
| 561 | |
---|
| 562 | if (cmd[i + 1]) { |
---|
| 563 | strcat(s, " "); |
---|
| 564 | } |
---|
| 565 | } |
---|
| 566 | strcat(s, "\r\n"); |
---|
| 567 | |
---|
[74c119d] | 568 | return s; |
---|
[b7d3cc34] | 569 | } |
---|
| 570 | |
---|
[5ebff60] | 571 | void irc_write(irc_t *irc, char *format, ...) |
---|
[b7d3cc34] | 572 | { |
---|
| 573 | va_list params; |
---|
[3ddb7477] | 574 | |
---|
[5ebff60] | 575 | va_start(params, format); |
---|
| 576 | irc_vawrite(irc, format, params); |
---|
| 577 | va_end(params); |
---|
[3ddb7477] | 578 | |
---|
[b7d3cc34] | 579 | return; |
---|
| 580 | } |
---|
| 581 | |
---|
[5ebff60] | 582 | void irc_write_all(int now, char *format, ...) |
---|
[b7d3cc34] | 583 | { |
---|
| 584 | va_list params; |
---|
[5ebff60] | 585 | GSList *temp; |
---|
| 586 | |
---|
| 587 | va_start(params, format); |
---|
| 588 | |
---|
[3ddb7477] | 589 | temp = irc_connection_list; |
---|
[5ebff60] | 590 | while (temp != NULL) { |
---|
[3ddb7477] | 591 | irc_t *irc = temp->data; |
---|
[5ebff60] | 592 | |
---|
| 593 | if (now) { |
---|
| 594 | g_free(irc->sendbuffer); |
---|
| 595 | irc->sendbuffer = g_strdup("\r\n"); |
---|
[3ddb7477] | 596 | } |
---|
[5ebff60] | 597 | irc_vawrite(temp->data, format, params); |
---|
| 598 | if (now) { |
---|
| 599 | bitlbee_io_current_client_write(irc, irc->fd, B_EV_IO_WRITE); |
---|
[3ddb7477] | 600 | } |
---|
| 601 | temp = temp->next; |
---|
| 602 | } |
---|
[5ebff60] | 603 | |
---|
| 604 | va_end(params); |
---|
[b7d3cc34] | 605 | return; |
---|
[5ebff60] | 606 | } |
---|
[b7d3cc34] | 607 | |
---|
[5ebff60] | 608 | void irc_vawrite(irc_t *irc, char *format, va_list params) |
---|
[b7d3cc34] | 609 | { |
---|
| 610 | int size; |
---|
[5ebff60] | 611 | char line[IRC_MAX_LINE + 1]; |
---|
| 612 | |
---|
[0356ae3] | 613 | /* Don't try to write anything new anymore when shutting down. */ |
---|
[5ebff60] | 614 | if (irc->status & USTATUS_SHUTDOWN) { |
---|
[b7d3cc34] | 615 | return; |
---|
[5ebff60] | 616 | } |
---|
| 617 | |
---|
| 618 | memset(line, 0, sizeof(line)); |
---|
| 619 | g_vsnprintf(line, IRC_MAX_LINE - 2, format, params); |
---|
| 620 | strip_newlines(line); |
---|
| 621 | |
---|
| 622 | if (irc->oconv != (GIConv) - 1) { |
---|
[f9756bd] | 623 | gsize bytes_read, bytes_written; |
---|
| 624 | char *conv; |
---|
[5ebff60] | 625 | |
---|
| 626 | conv = g_convert_with_iconv(line, -1, irc->oconv, |
---|
| 627 | &bytes_read, &bytes_written, NULL); |
---|
| 628 | |
---|
| 629 | if (bytes_read == strlen(line)) { |
---|
| 630 | strncpy(line, conv, IRC_MAX_LINE - 2); |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | g_free(conv); |
---|
| 634 | } |
---|
| 635 | g_strlcat(line, "\r\n", IRC_MAX_LINE + 1); |
---|
| 636 | |
---|
| 637 | if (irc->sendbuffer != NULL) { |
---|
| 638 | size = strlen(irc->sendbuffer) + strlen(line); |
---|
| 639 | irc->sendbuffer = g_renew(char, irc->sendbuffer, size + 1); |
---|
| 640 | strcpy((irc->sendbuffer + strlen(irc->sendbuffer)), line); |
---|
| 641 | } else { |
---|
[a0d04d6] | 642 | irc->sendbuffer = g_strdup(line); |
---|
[b7d3cc34] | 643 | } |
---|
[5ebff60] | 644 | |
---|
| 645 | if (irc->w_watch_source_id == 0) { |
---|
[0356ae3] | 646 | /* If the buffer is empty we can probably write, so call the write event handler |
---|
| 647 | immediately. If it returns TRUE, it should be called again, so add the event to |
---|
| 648 | the queue. If it's FALSE, we emptied the buffer and saved ourselves some work |
---|
| 649 | in the event queue. */ |
---|
[bbb6ffb] | 650 | /* Really can't be done as long as the code doesn't do error checking very well: |
---|
[e046390] | 651 | if( bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ) ) */ |
---|
[5ebff60] | 652 | |
---|
[bbb6ffb] | 653 | /* So just always do it via the event handler. */ |
---|
[5ebff60] | 654 | irc->w_watch_source_id = b_input_add(irc->fd, B_EV_IO_WRITE, bitlbee_io_current_client_write, irc); |
---|
[0356ae3] | 655 | } |
---|
[5ebff60] | 656 | |
---|
[b7d3cc34] | 657 | return; |
---|
| 658 | } |
---|
| 659 | |
---|
[f1c2b21] | 660 | /* Flush sendbuffer if you can. If it fails, fail silently and let some |
---|
| 661 | I/O event handler clean up. */ |
---|
[5ebff60] | 662 | void irc_flush(irc_t *irc) |
---|
[f1c2b21] | 663 | { |
---|
| 664 | ssize_t n; |
---|
| 665 | size_t len; |
---|
[5ebff60] | 666 | |
---|
| 667 | if (irc->sendbuffer == NULL) { |
---|
[f1c2b21] | 668 | return; |
---|
[5ebff60] | 669 | } |
---|
| 670 | |
---|
| 671 | len = strlen(irc->sendbuffer); |
---|
| 672 | if ((n = send(irc->fd, irc->sendbuffer, len, 0)) == len) { |
---|
| 673 | g_free(irc->sendbuffer); |
---|
[f1c2b21] | 674 | irc->sendbuffer = NULL; |
---|
[5ebff60] | 675 | |
---|
| 676 | b_event_remove(irc->w_watch_source_id); |
---|
[f1c2b21] | 677 | irc->w_watch_source_id = 0; |
---|
[5ebff60] | 678 | } else if (n > 0) { |
---|
| 679 | char *s = g_strdup(irc->sendbuffer + n); |
---|
| 680 | g_free(irc->sendbuffer); |
---|
[f1c2b21] | 681 | irc->sendbuffer = s; |
---|
| 682 | } |
---|
| 683 | /* Otherwise something went wrong and we don't currently care |
---|
| 684 | what the error was. We may or may not succeed later, we |
---|
| 685 | were just trying to flush the buffer immediately. */ |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | /* Meant for takeover functionality. Transfer an IRC connection to a different |
---|
| 689 | socket. */ |
---|
[5ebff60] | 690 | void irc_switch_fd(irc_t *irc, int fd) |
---|
[f1c2b21] | 691 | { |
---|
[5ebff60] | 692 | irc_write(irc, "ERROR :Transferring session to a new connection"); |
---|
| 693 | irc_flush(irc); /* Write it now or forget about it forever. */ |
---|
| 694 | |
---|
| 695 | if (irc->sendbuffer) { |
---|
| 696 | b_event_remove(irc->w_watch_source_id); |
---|
[af9f2ca] | 697 | irc->w_watch_source_id = 0; |
---|
[5ebff60] | 698 | g_free(irc->sendbuffer); |
---|
[af9f2ca] | 699 | irc->sendbuffer = NULL; |
---|
[f1c2b21] | 700 | } |
---|
[5ebff60] | 701 | |
---|
| 702 | b_event_remove(irc->r_watch_source_id); |
---|
| 703 | closesocket(irc->fd); |
---|
[f1c2b21] | 704 | irc->fd = fd; |
---|
[5ebff60] | 705 | irc->r_watch_source_id = b_input_add(irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc); |
---|
[f1c2b21] | 706 | } |
---|
| 707 | |
---|
[5ebff60] | 708 | void irc_sync(irc_t *irc) |
---|
[f1c2b21] | 709 | { |
---|
| 710 | GSList *l; |
---|
[5ebff60] | 711 | |
---|
| 712 | irc_write(irc, ":%s!%s@%s MODE %s :+%s", irc->user->nick, |
---|
| 713 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 714 | irc->umode); |
---|
| 715 | |
---|
| 716 | for (l = irc->channels; l; l = l->next) { |
---|
[f1c2b21] | 717 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 718 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
| 719 | irc_send_join(ic, irc->user); |
---|
| 720 | } |
---|
[f1c2b21] | 721 | } |
---|
[5ebff60] | 722 | |
---|
[e1aaea4] | 723 | /* We may be waiting for a PONG from the previous client connection. */ |
---|
| 724 | irc->pinging = FALSE; |
---|
[f1c2b21] | 725 | } |
---|
| 726 | |
---|
[5ebff60] | 727 | void irc_desync(irc_t *irc) |
---|
[f1c2b21] | 728 | { |
---|
| 729 | GSList *l; |
---|
[5ebff60] | 730 | |
---|
| 731 | for (l = irc->channels; l; l = l->next) { |
---|
| 732 | irc_channel_del_user(l->data, irc->user, IRC_CDU_KICK, |
---|
| 733 | "Switching to old session"); |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | irc_write(irc, ":%s!%s@%s MODE %s :-%s", irc->user->nick, |
---|
| 737 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 738 | irc->umode); |
---|
[f1c2b21] | 739 | } |
---|
| 740 | |
---|
[5ebff60] | 741 | int irc_check_login(irc_t *irc) |
---|
[b7d3cc34] | 742 | { |
---|
[0ef1c92] | 743 | if (irc->user->user && irc->user->nick && !(irc->status & USTATUS_CAP_PENDING)) { |
---|
[5ebff60] | 744 | if (global.conf->authmode == AUTHMODE_CLOSED && !(irc->status & USTATUS_AUTHORIZED)) { |
---|
| 745 | irc_send_num(irc, 464, ":This server is password-protected."); |
---|
[edf9657] | 746 | return 0; |
---|
[5ebff60] | 747 | } else { |
---|
[4be8239] | 748 | irc_channel_t *ic; |
---|
| 749 | irc_user_t *iu = irc->user; |
---|
[5ebff60] | 750 | |
---|
| 751 | irc->user = irc_user_new(irc, iu->nick); |
---|
[4be8239] | 752 | irc->user->user = iu->user; |
---|
[b95932e] | 753 | irc->user->host = iu->host; |
---|
[4be8239] | 754 | irc->user->fullname = iu->fullname; |
---|
[280c56a] | 755 | irc->user->f = &irc_user_self_funcs; |
---|
[5ebff60] | 756 | g_free(iu->nick); |
---|
| 757 | g_free(iu); |
---|
| 758 | |
---|
| 759 | if (global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON) { |
---|
| 760 | ipc_to_master_str("CLIENT %s %s :%s\r\n", irc->user->host, irc->user->nick, |
---|
| 761 | irc->user->fullname); |
---|
| 762 | } |
---|
| 763 | |
---|
[4be8239] | 764 | irc->status |= USTATUS_LOGGED_IN; |
---|
[5ebff60] | 765 | |
---|
| 766 | irc_send_login(irc); |
---|
| 767 | |
---|
[b919363] | 768 | irc->umode[0] = '\0'; |
---|
[5ebff60] | 769 | irc_umode_set(irc, "+" UMODE, TRUE); |
---|
| 770 | |
---|
| 771 | ic = irc->default_channel = irc_channel_new(irc, ROOT_CHAN); |
---|
| 772 | irc_channel_set_topic(ic, CONTROL_TOPIC, irc->root); |
---|
| 773 | set_setstr(&ic->set, "auto_join", "true"); |
---|
| 774 | irc_channel_auto_joins(irc, NULL); |
---|
| 775 | |
---|
[f7ca587] | 776 | irc->root->last_channel = irc->default_channel; |
---|
[5ebff60] | 777 | |
---|
| 778 | irc_rootmsg(irc, |
---|
| 779 | "Welcome to the BitlBee gateway!\n\n" |
---|
[7320610] | 780 | "Running %s %s\n\n" |
---|
[5ebff60] | 781 | "If you've never used BitlBee before, please do read the help " |
---|
| 782 | "information using the \x02help\x02 command. Lots of FAQs are " |
---|
| 783 | "answered there.\n" |
---|
| 784 | "If you already have an account on this server, just use the " |
---|
[7320610] | 785 | "\x02identify\x02 command to identify yourself.", |
---|
| 786 | PACKAGE, BITLBEE_VERSION); |
---|
[5ebff60] | 787 | |
---|
[70f69ecc] | 788 | /* This is for bug #209 (use PASS to identify to NickServ). */ |
---|
[5ebff60] | 789 | if (irc->password != NULL) { |
---|
| 790 | char *send_cmd[] = { "identify", g_strdup(irc->password), NULL }; |
---|
| 791 | |
---|
| 792 | irc_setpass(irc, NULL); |
---|
| 793 | root_command(irc, send_cmd); |
---|
| 794 | g_free(send_cmd[1]); |
---|
[70f69ecc] | 795 | } |
---|
[5ebff60] | 796 | |
---|
[edf9657] | 797 | return 1; |
---|
[b7d3cc34] | 798 | } |
---|
[5ebff60] | 799 | } else { |
---|
[edf9657] | 800 | /* More information needed. */ |
---|
| 801 | return 0; |
---|
| 802 | } |
---|
[b7d3cc34] | 803 | } |
---|
| 804 | |
---|
[65016a6] | 805 | /* TODO: This is a mess, but this function is a bit too complicated to be |
---|
| 806 | converted to something more generic. */ |
---|
[5ebff60] | 807 | void irc_umode_set(irc_t *irc, const char *s, gboolean allow_priv) |
---|
[b919363] | 808 | { |
---|
| 809 | /* allow_priv: Set to 0 if s contains user input, 1 if you want |
---|
| 810 | to set a "privileged" mode (+o, +R, etc). */ |
---|
| 811 | char m[128], st = 1; |
---|
| 812 | const char *t; |
---|
| 813 | int i; |
---|
[00fd005] | 814 | char changes[512], st2 = 2; |
---|
[b919363] | 815 | char badflag = 0; |
---|
[5ebff60] | 816 | |
---|
| 817 | memset(m, 0, sizeof(m)); |
---|
| 818 | |
---|
[00fd005] | 819 | /* Keep track of which modes are enabled in this array. */ |
---|
[5ebff60] | 820 | for (t = irc->umode; *t; t++) { |
---|
| 821 | if (*t < sizeof(m)) { |
---|
| 822 | m[(int) *t] = 1; |
---|
| 823 | } |
---|
| 824 | } |
---|
| 825 | |
---|
[00fd005] | 826 | i = 0; |
---|
[5ebff60] | 827 | for (t = s; *t && i < sizeof(changes) - 3; t++) { |
---|
| 828 | if (*t == '+' || *t == '-') { |
---|
[b919363] | 829 | st = *t == '+'; |
---|
[5ebff60] | 830 | } else if ((st == 0 && (!strchr(UMODES_KEEP, *t) || allow_priv)) || |
---|
| 831 | (st == 1 && strchr(UMODES, *t)) || |
---|
| 832 | (st == 1 && allow_priv && strchr(UMODES_PRIV, *t))) { |
---|
| 833 | if (m[(int) *t] != st) { |
---|
[00fd005] | 834 | /* If we're actually making a change, remember this |
---|
| 835 | for the response. */ |
---|
[5ebff60] | 836 | if (st != st2) { |
---|
[00fd005] | 837 | st2 = st, changes[i++] = st ? '+' : '-'; |
---|
[5ebff60] | 838 | } |
---|
[00fd005] | 839 | changes[i++] = *t; |
---|
[b919363] | 840 | } |
---|
[5ebff60] | 841 | m[(int) *t] = st; |
---|
| 842 | } else { |
---|
[b919363] | 843 | badflag = 1; |
---|
[5ebff60] | 844 | } |
---|
[b919363] | 845 | } |
---|
[00fd005] | 846 | changes[i] = '\0'; |
---|
[5ebff60] | 847 | |
---|
[00fd005] | 848 | /* Convert the m array back into an umode string. */ |
---|
[5ebff60] | 849 | memset(irc->umode, 0, sizeof(irc->umode)); |
---|
| 850 | for (i = 'A'; i <= 'z' && strlen(irc->umode) < (sizeof(irc->umode) - 1); i++) { |
---|
| 851 | if (m[i]) { |
---|
[b919363] | 852 | irc->umode[strlen(irc->umode)] = i; |
---|
[5ebff60] | 853 | } |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | if (badflag) { |
---|
| 857 | irc_send_num(irc, 501, ":Unknown MODE flag"); |
---|
| 858 | } |
---|
| 859 | if (*changes) { |
---|
| 860 | irc_write(irc, ":%s!%s@%s MODE %s :%s", irc->user->nick, |
---|
| 861 | irc->user->user, irc->user->host, irc->user->nick, |
---|
| 862 | changes); |
---|
| 863 | } |
---|
[b919363] | 864 | } |
---|
| 865 | |
---|
[3923003] | 866 | /* Returns 0 if everything seems to be okay, a number >0 when there was a |
---|
| 867 | timeout. The number returned is the number of seconds we received no |
---|
| 868 | pongs from the user. When not connected yet, we don't ping but drop the |
---|
| 869 | connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ |
---|
[5ebff60] | 870 | static gboolean irc_userping(gpointer _irc, gint fd, b_input_condition cond) |
---|
[3923003] | 871 | { |
---|
[b958cb5] | 872 | double now = gettime(); |
---|
[3923003] | 873 | irc_t *irc = _irc; |
---|
[b958cb5] | 874 | int fail = 0; |
---|
[5ebff60] | 875 | |
---|
| 876 | if (!(irc->status & USTATUS_LOGGED_IN)) { |
---|
| 877 | if (now > (irc->last_pong + IRC_LOGIN_TIMEOUT)) { |
---|
[b958cb5] | 878 | fail = now - irc->last_pong; |
---|
[3923003] | 879 | } |
---|
[5ebff60] | 880 | } else { |
---|
| 881 | if (now > (irc->last_pong + global.conf->ping_timeout)) { |
---|
| 882 | fail = now - irc->last_pong; |
---|
| 883 | } else { |
---|
| 884 | irc_write(irc, "PING :%s", IRC_PING_STRING); |
---|
[3923003] | 885 | } |
---|
| 886 | } |
---|
[5ebff60] | 887 | |
---|
| 888 | if (fail > 0) { |
---|
| 889 | irc_abort(irc, 0, "Ping Timeout: %d seconds", fail); |
---|
[3923003] | 890 | return FALSE; |
---|
| 891 | } |
---|
[5ebff60] | 892 | |
---|
[3923003] | 893 | return TRUE; |
---|
| 894 | } |
---|
| 895 | |
---|
[5ebff60] | 896 | static char *set_eval_charset(set_t *set, char *value) |
---|
[b7d3cc34] | 897 | { |
---|
[5ebff60] | 898 | irc_t *irc = (irc_t *) set->data; |
---|
[21c87a7] | 899 | char *test; |
---|
| 900 | gsize test_bytes = 0; |
---|
[3ddb7477] | 901 | GIConv ic, oc; |
---|
[b7d3cc34] | 902 | |
---|
[5ebff60] | 903 | if (g_strcasecmp(value, "none") == 0) { |
---|
| 904 | value = g_strdup("utf-8"); |
---|
| 905 | } |
---|
[b7d3cc34] | 906 | |
---|
[5ebff60] | 907 | if ((oc = g_iconv_open(value, "utf-8")) == (GIConv) - 1) { |
---|
[3ddb7477] | 908 | return NULL; |
---|
[b7d3cc34] | 909 | } |
---|
[5ebff60] | 910 | |
---|
[21c87a7] | 911 | /* Do a test iconv to see if the user picked an IRC-compatible |
---|
| 912 | charset (for example utf-16 goes *horribly* wrong). */ |
---|
[5ebff60] | 913 | if ((test = g_convert_with_iconv(" ", 1, oc, NULL, &test_bytes, NULL)) == NULL || |
---|
| 914 | test_bytes > 1) { |
---|
| 915 | g_free(test); |
---|
| 916 | g_iconv_close(oc); |
---|
| 917 | irc_rootmsg(irc, "Unsupported character set: The IRC protocol " |
---|
| 918 | "only supports 8-bit character sets."); |
---|
[21c87a7] | 919 | return NULL; |
---|
[0e7ab64] | 920 | } |
---|
[5ebff60] | 921 | g_free(test); |
---|
| 922 | |
---|
| 923 | if ((ic = g_iconv_open("utf-8", value)) == (GIConv) - 1) { |
---|
| 924 | g_iconv_close(oc); |
---|
[3ddb7477] | 925 | return NULL; |
---|
[b7d3cc34] | 926 | } |
---|
[5ebff60] | 927 | |
---|
| 928 | if (irc->iconv != (GIConv) - 1) { |
---|
| 929 | g_iconv_close(irc->iconv); |
---|
| 930 | } |
---|
| 931 | if (irc->oconv != (GIConv) - 1) { |
---|
| 932 | g_iconv_close(irc->oconv); |
---|
| 933 | } |
---|
| 934 | |
---|
[3ddb7477] | 935 | irc->iconv = ic; |
---|
| 936 | irc->oconv = oc; |
---|
[0e7ab64] | 937 | |
---|
[3ddb7477] | 938 | return value; |
---|
[0e7ab64] | 939 | } |
---|
[0e8b3e8] | 940 | |
---|
[6d8cc05] | 941 | /* Mostly meant for upgrades. If one of these is set to the non-default, |
---|
| 942 | set show_users of all channels to something with the same effect. */ |
---|
[5ebff60] | 943 | static char *set_eval_bw_compat(set_t *set, char *value) |
---|
[0e8b3e8] | 944 | { |
---|
| 945 | irc_t *irc = set->data; |
---|
[6d8cc05] | 946 | char *val; |
---|
| 947 | GSList *l; |
---|
[5ebff60] | 948 | |
---|
| 949 | irc_rootmsg(irc, "Setting `%s' is obsolete, use the `show_users' " |
---|
| 950 | "channel setting instead.", set->key); |
---|
| 951 | |
---|
| 952 | if (strcmp(set->key, "away_devoice") == 0 && !bool2int(value)) { |
---|
[7b8238d] | 953 | val = "online,special%,away"; |
---|
[5ebff60] | 954 | } else if (strcmp(set->key, "show_offline") == 0 && bool2int(value)) { |
---|
[7b8238d] | 955 | val = "online@,special%,away+,offline"; |
---|
[5ebff60] | 956 | } else { |
---|
[7b8238d] | 957 | val = "online+,special%,away"; |
---|
[5ebff60] | 958 | } |
---|
| 959 | |
---|
| 960 | for (l = irc->channels; l; l = l->next) { |
---|
[6d8cc05] | 961 | irc_channel_t *ic = l->data; |
---|
| 962 | /* No need to check channel type, if the setting doesn't exist it |
---|
| 963 | will just be ignored. */ |
---|
[5ebff60] | 964 | set_setstr(&ic->set, "show_users", val); |
---|
[6d8cc05] | 965 | } |
---|
[5ebff60] | 966 | |
---|
[6d8cc05] | 967 | return SET_INVALID; |
---|
[0e8b3e8] | 968 | } |
---|
[0c85c08] | 969 | |
---|
[5ebff60] | 970 | static char *set_eval_utf8_nicks(set_t *set, char *value) |
---|
[c608891] | 971 | { |
---|
| 972 | irc_t *irc = set->data; |
---|
[5ebff60] | 973 | gboolean val = bool2int(value); |
---|
| 974 | |
---|
[c608891] | 975 | /* Do *NOT* unset this flag in the middle of a session. There will |
---|
| 976 | be UTF-8 nicks around already so if we suddenly disable support |
---|
| 977 | for them, various functions might behave strangely. */ |
---|
[5ebff60] | 978 | if (val) { |
---|
[c608891] | 979 | irc->status |= IRC_UTF8_NICKS; |
---|
[5ebff60] | 980 | } else if (irc->status & IRC_UTF8_NICKS) { |
---|
| 981 | irc_rootmsg(irc, "You need to reconnect to BitlBee for this " |
---|
| 982 | "change to take effect."); |
---|
| 983 | } |
---|
| 984 | |
---|
| 985 | return set_eval_bool(set, value); |
---|
[c608891] | 986 | } |
---|
| 987 | |
---|
[5ebff60] | 988 | void register_irc_plugin(const struct irc_plugin *p) |
---|
[0c85c08] | 989 | { |
---|
[5ebff60] | 990 | irc_plugins = g_slist_prepend(irc_plugins, (gpointer) p); |
---|
[0c85c08] | 991 | } |
---|