[ebaebfe] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* The IRC-based UI - Sending responses to commands/etc. */ |
---|
| 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 | #include "bitlbee.h" |
---|
| 27 | |
---|
| 28 | void irc_send_num( irc_t *irc, int code, char *format, ... ) |
---|
| 29 | { |
---|
| 30 | char text[IRC_MAX_LINE]; |
---|
| 31 | va_list params; |
---|
| 32 | |
---|
| 33 | va_start( params, format ); |
---|
| 34 | g_vsnprintf( text, IRC_MAX_LINE, format, params ); |
---|
| 35 | va_end( params ); |
---|
| 36 | |
---|
[4be8239] | 37 | irc_write( irc, ":%s %03d %s %s", irc->root->host, code, irc->user->nick ? : "*", text ); |
---|
[ebaebfe] | 38 | } |
---|
| 39 | |
---|
| 40 | void irc_send_login( irc_t *irc ) |
---|
| 41 | { |
---|
| 42 | irc_send_num( irc, 1, ":Welcome to the BitlBee gateway, %s", irc->user->nick ); |
---|
| 43 | irc_send_num( irc, 2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->root->host ); |
---|
| 44 | irc_send_num( irc, 3, ":%s", IRCD_INFO ); |
---|
| 45 | irc_send_num( irc, 4, "%s %s %s %s", irc->root->host, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES ); |
---|
| 46 | irc_send_num( irc, 5, "PREFIX=(ov)@+ CHANTYPES=%s CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee " |
---|
| 47 | "CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server", |
---|
| 48 | CTYPES, CMODES, MAX_NICK_LENGTH - 1 ); |
---|
| 49 | irc_send_motd( irc ); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | void irc_send_motd( irc_t *irc ) |
---|
| 53 | { |
---|
| 54 | int fd; |
---|
| 55 | |
---|
| 56 | fd = open( global.conf->motdfile, O_RDONLY ); |
---|
| 57 | if( fd == -1 ) |
---|
| 58 | { |
---|
| 59 | irc_send_num( irc, 422, ":We don't need MOTDs." ); |
---|
| 60 | } |
---|
| 61 | else |
---|
| 62 | { |
---|
| 63 | char linebuf[80]; /* Max. line length for MOTD's is 79 chars. It's what most IRC networks seem to do. */ |
---|
| 64 | char *add, max; |
---|
| 65 | int len; |
---|
| 66 | |
---|
| 67 | linebuf[79] = len = 0; |
---|
| 68 | max = sizeof( linebuf ) - 1; |
---|
| 69 | |
---|
| 70 | irc_send_num( irc, 375, ":- %s Message Of The Day - ", irc->root->host ); |
---|
| 71 | while( read( fd, linebuf + len, 1 ) == 1 ) |
---|
| 72 | { |
---|
| 73 | if( linebuf[len] == '\n' || len == max ) |
---|
| 74 | { |
---|
| 75 | linebuf[len] = 0; |
---|
| 76 | irc_send_num( irc, 372, ":- %s", linebuf ); |
---|
| 77 | len = 0; |
---|
| 78 | } |
---|
| 79 | else if( linebuf[len] == '%' ) |
---|
| 80 | { |
---|
| 81 | read( fd, linebuf + len, 1 ); |
---|
| 82 | if( linebuf[len] == 'h' ) |
---|
| 83 | add = irc->root->host; |
---|
| 84 | else if( linebuf[len] == 'v' ) |
---|
| 85 | add = BITLBEE_VERSION; |
---|
| 86 | else if( linebuf[len] == 'n' ) |
---|
| 87 | add = irc->user->nick; |
---|
| 88 | else |
---|
| 89 | add = "%"; |
---|
| 90 | |
---|
| 91 | strncpy( linebuf + len, add, max - len ); |
---|
| 92 | while( linebuf[++len] ); |
---|
| 93 | } |
---|
| 94 | else if( len < max ) |
---|
| 95 | { |
---|
| 96 | len ++; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | irc_send_num( irc, 376, ":End of MOTD" ); |
---|
| 100 | close( fd ); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
[4be8239] | 104 | void irc_usermsg( irc_t *irc, char *format, ... ) |
---|
[ebaebfe] | 105 | { |
---|
[74f1cde] | 106 | irc_channel_t *ic; |
---|
| 107 | irc_user_t *iu; |
---|
[ebaebfe] | 108 | char text[1024]; |
---|
| 109 | va_list params; |
---|
| 110 | |
---|
| 111 | va_start( params, format ); |
---|
| 112 | g_vsnprintf( text, sizeof( text ), format, params ); |
---|
| 113 | va_end( params ); |
---|
| 114 | |
---|
[74f1cde] | 115 | if( irc->last_root_cmd && |
---|
| 116 | irc_channel_name_ok( irc->last_root_cmd ) && |
---|
| 117 | ( ic = irc_channel_by_name( irc, irc->last_root_cmd ) ) && |
---|
| 118 | ic->flags & IRC_CHANNEL_JOINED ) |
---|
[6761a40] | 119 | irc_send_msg( irc->root, "PRIVMSG", irc->last_root_cmd, text, NULL ); |
---|
[74f1cde] | 120 | else if( irc->last_root_cmd && |
---|
| 121 | ( iu = irc_user_by_name( irc, irc->last_root_cmd ) ) && |
---|
| 122 | iu->f == &irc_user_root_funcs ) |
---|
[6761a40] | 123 | irc_send_msg( iu, "PRIVMSG", irc->user->nick, text, NULL ); |
---|
[74f1cde] | 124 | else |
---|
| 125 | { |
---|
| 126 | g_free( irc->last_root_cmd ); |
---|
| 127 | irc->last_root_cmd = NULL; |
---|
| 128 | |
---|
[6761a40] | 129 | irc_send_msg( irc->root, "PRIVMSG", irc->user->nick, text, NULL ); |
---|
[74f1cde] | 130 | } |
---|
[ebaebfe] | 131 | |
---|
| 132 | /*return( irc_msgfrom( irc, u->nick, text ) );*/ |
---|
| 133 | } |
---|
[4be8239] | 134 | |
---|
| 135 | void irc_send_join( irc_channel_t *ic, irc_user_t *iu ) |
---|
| 136 | { |
---|
| 137 | irc_t *irc = ic->irc; |
---|
| 138 | |
---|
| 139 | irc_write( irc, ":%s!%s@%s JOIN :%s", iu->nick, iu->user, iu->host, ic->name ); |
---|
| 140 | |
---|
| 141 | if( iu == irc->user ) |
---|
| 142 | { |
---|
| 143 | irc_write( irc, ":%s MODE %s +%s", irc->root->host, ic->name, ic->mode ); |
---|
| 144 | irc_send_names( ic ); |
---|
[8240840] | 145 | if( ic->topic && *ic->topic ) |
---|
| 146 | irc_send_topic( ic, FALSE ); |
---|
[4be8239] | 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | void irc_send_part( irc_channel_t *ic, irc_user_t *iu, const char *reason ) |
---|
| 151 | { |
---|
[18da20b] | 152 | irc_write( ic->irc, ":%s!%s@%s PART %s :%s", iu->nick, iu->user, iu->host, ic->name, reason ? : "" ); |
---|
[4be8239] | 153 | } |
---|
| 154 | |
---|
[1f0224c] | 155 | void irc_send_quit( irc_user_t *iu, const char *reason ) |
---|
| 156 | { |
---|
| 157 | irc_write( iu->irc, ":%s!%s@%s QUIT :%s", iu->nick, iu->user, iu->host, reason ? : "" ); |
---|
| 158 | } |
---|
| 159 | |
---|
[4be8239] | 160 | void irc_send_names( irc_channel_t *ic ) |
---|
| 161 | { |
---|
| 162 | GSList *l; |
---|
| 163 | char namelist[385] = ""; |
---|
| 164 | |
---|
| 165 | /* RFCs say there is no error reply allowed on NAMES, so when the |
---|
| 166 | channel is invalid, just give an empty reply. */ |
---|
| 167 | for( l = ic->users; l; l = l->next ) |
---|
| 168 | { |
---|
[e54112f] | 169 | irc_channel_user_t *icu = l->data; |
---|
| 170 | irc_user_t *iu = icu->iu; |
---|
[4be8239] | 171 | |
---|
| 172 | if( strlen( namelist ) + strlen( iu->nick ) > sizeof( namelist ) - 4 ) |
---|
| 173 | { |
---|
| 174 | irc_send_num( ic->irc, 353, "= %s :%s", ic->name, namelist ); |
---|
| 175 | *namelist = 0; |
---|
| 176 | } |
---|
| 177 | |
---|
[6a9d068] | 178 | if( icu->flags & IRC_CHANNEL_USER_OP ) |
---|
[4be8239] | 179 | strcat( namelist, "@" ); |
---|
[6a9d068] | 180 | else if( icu->flags & IRC_CHANNEL_USER_HALFOP ) |
---|
| 181 | strcat( namelist, "%" ); |
---|
| 182 | else if( icu->flags & IRC_CHANNEL_USER_VOICE ) |
---|
| 183 | strcat( namelist, "+" ); |
---|
[4be8239] | 184 | |
---|
| 185 | strcat( namelist, iu->nick ); |
---|
| 186 | strcat( namelist, " " ); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if( *namelist ) |
---|
| 190 | irc_send_num( ic->irc, 353, "= %s :%s", ic->name, namelist ); |
---|
| 191 | |
---|
| 192 | irc_send_num( ic->irc, 366, "%s :End of /NAMES list", ic->name ); |
---|
| 193 | } |
---|
| 194 | |
---|
[83e92bf] | 195 | void irc_send_topic( irc_channel_t *ic, gboolean topic_change ) |
---|
[4be8239] | 196 | { |
---|
[83e92bf] | 197 | if( topic_change && ic->topic_who ) |
---|
| 198 | { |
---|
| 199 | irc_write( ic->irc, ":%s TOPIC %s :%s", ic->topic_who, |
---|
| 200 | ic->name, ic->topic && *ic->topic ? ic->topic : "" ); |
---|
| 201 | } |
---|
| 202 | else if( ic->topic ) |
---|
| 203 | { |
---|
[4be8239] | 204 | irc_send_num( ic->irc, 332, "%s :%s", ic->name, ic->topic ); |
---|
[83e92bf] | 205 | if( ic->topic_who ) |
---|
| 206 | irc_send_num( ic->irc, 333, "%s %s %d", |
---|
| 207 | ic->name, ic->topic_who, (int) ic->topic_time ); |
---|
| 208 | } |
---|
[4be8239] | 209 | else |
---|
| 210 | irc_send_num( ic->irc, 331, "%s :No topic for this channel", ic->name ); |
---|
| 211 | } |
---|
[b95932e] | 212 | |
---|
| 213 | void irc_send_whois( irc_user_t *iu ) |
---|
| 214 | { |
---|
| 215 | irc_t *irc = iu->irc; |
---|
| 216 | |
---|
| 217 | irc_send_num( irc, 311, "%s %s %s * :%s", |
---|
| 218 | iu->nick, iu->user, iu->host, iu->fullname ); |
---|
| 219 | |
---|
[1d39159] | 220 | if( iu->bu ) |
---|
| 221 | { |
---|
| 222 | bee_user_t *bu = iu->bu; |
---|
| 223 | |
---|
| 224 | irc_send_num( irc, 312, "%s %s.%s :%s network", iu->nick, bu->ic->acc->user, |
---|
| 225 | bu->ic->acc->server && *bu->ic->acc->server ? bu->ic->acc->server : "", |
---|
| 226 | bu->ic->acc->prpl->name ); |
---|
| 227 | |
---|
[e7edbb7] | 228 | if( ( bu->status && *bu->status ) || |
---|
| 229 | ( bu->status_msg && *bu->status_msg ) ) |
---|
[1d39159] | 230 | { |
---|
[d986463] | 231 | int num = bu->flags & BEE_USER_AWAY ? 301 : 320; |
---|
| 232 | |
---|
| 233 | if( bu->status && bu->status_msg ) |
---|
| 234 | irc_send_num( irc, num, "%s :%s (%s)", iu->nick, bu->status, bu->status_msg ); |
---|
[1d39159] | 235 | else |
---|
[d986463] | 236 | irc_send_num( irc, num, "%s :%s", iu->nick, bu->status ? : bu->status_msg ); |
---|
[1d39159] | 237 | } |
---|
[eb50495] | 238 | else if( !( bu->flags & BEE_USER_ONLINE ) ) |
---|
| 239 | { |
---|
| 240 | irc_send_num( irc, 301, "%s :%s", iu->nick, "User is offline" ); |
---|
| 241 | } |
---|
[56699f0] | 242 | |
---|
| 243 | if( bu->idle_time || bu->login_time ) |
---|
| 244 | { |
---|
| 245 | irc_send_num( irc, 317, "%s %d %d :seconds idle, signon time", |
---|
| 246 | iu->nick, |
---|
| 247 | bu->idle_time ? (int) ( time( NULL ) - bu->idle_time ) : 0, |
---|
| 248 | (int) bu->login_time ); |
---|
| 249 | } |
---|
[1d39159] | 250 | } |
---|
[b95932e] | 251 | else |
---|
[1d39159] | 252 | { |
---|
[e7edbb7] | 253 | irc_send_num( irc, 312, "%s %s :%s", iu->nick, irc->root->host, IRCD_INFO ); |
---|
[1d39159] | 254 | } |
---|
[b95932e] | 255 | |
---|
| 256 | irc_send_num( irc, 318, "%s :End of /WHOIS list", iu->nick ); |
---|
| 257 | } |
---|
[2f53ada] | 258 | |
---|
| 259 | void irc_send_who( irc_t *irc, GSList *l, const char *channel ) |
---|
| 260 | { |
---|
[e54112f] | 261 | gboolean is_channel = strcmp( channel, "**" ) != 0; |
---|
| 262 | |
---|
[2f53ada] | 263 | while( l ) |
---|
| 264 | { |
---|
| 265 | irc_user_t *iu = l->data; |
---|
[e54112f] | 266 | if( is_channel ) |
---|
| 267 | iu = ((irc_channel_user_t*)iu)->iu; |
---|
[2f53ada] | 268 | /* TODO(wilmer): Restore away/channel information here */ |
---|
| 269 | irc_send_num( irc, 352, "%s %s %s %s %s %c :0 %s", |
---|
[410bf6d] | 270 | channel ? : "*", iu->user, iu->host, irc->root->host, |
---|
[eb50495] | 271 | iu->nick, iu->flags & IRC_USER_AWAY ? 'G' : 'H', |
---|
| 272 | iu->fullname ); |
---|
[2f53ada] | 273 | l = l->next; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | irc_send_num( irc, 315, "%s :End of /WHO list", channel ); |
---|
| 277 | } |
---|
[280c56a] | 278 | |
---|
[6761a40] | 279 | void irc_send_msg( irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix ) |
---|
| 280 | { |
---|
| 281 | char last = 0; |
---|
| 282 | const char *s = msg, *line = msg; |
---|
| 283 | char raw_msg[strlen(msg)+1024]; |
---|
| 284 | |
---|
| 285 | while( !last ) |
---|
| 286 | { |
---|
| 287 | if( *s == '\r' && *(s+1) == '\n' ) |
---|
| 288 | s++; |
---|
| 289 | if( *s == '\n' ) |
---|
| 290 | { |
---|
| 291 | last = s[1] == 0; |
---|
| 292 | } |
---|
| 293 | else |
---|
| 294 | { |
---|
| 295 | last = s[0] == 0; |
---|
| 296 | } |
---|
| 297 | if( *s == 0 || *s == '\n' ) |
---|
| 298 | { |
---|
| 299 | if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) && |
---|
| 300 | g_strcasecmp( type, "PRIVMSG" ) == 0 ) |
---|
| 301 | { |
---|
| 302 | strcpy( raw_msg, "\001ACTION " ); |
---|
| 303 | strncat( raw_msg, line + 4, s - line - 4 ); |
---|
| 304 | strcat( raw_msg, "\001" ); |
---|
| 305 | irc_send_msg_raw( iu, type, dst, raw_msg ); |
---|
| 306 | } |
---|
| 307 | else |
---|
| 308 | { |
---|
| 309 | *raw_msg = '\0'; |
---|
| 310 | if( prefix && *prefix ) |
---|
| 311 | strcpy( raw_msg, prefix ); |
---|
| 312 | strncat( raw_msg, line, s - line ); |
---|
| 313 | irc_send_msg_raw( iu, type, dst, raw_msg ); |
---|
| 314 | } |
---|
| 315 | line = s + 1; |
---|
| 316 | } |
---|
| 317 | s ++; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | void irc_send_msg_raw( irc_user_t *iu, const char *type, const char *dst, const char *msg ) |
---|
[280c56a] | 322 | { |
---|
| 323 | irc_write( iu->irc, ":%s!%s@%s %s %s :%s", |
---|
| 324 | iu->nick, iu->user, iu->host, type, dst, msg ); |
---|
| 325 | } |
---|
[0b5cc72] | 326 | |
---|
[7b59872] | 327 | void irc_send_msg_f( irc_user_t *iu, const char *type, const char *dst, const char *format, ... ) |
---|
| 328 | { |
---|
| 329 | char text[IRC_MAX_LINE]; |
---|
| 330 | va_list params; |
---|
| 331 | |
---|
| 332 | va_start( params, format ); |
---|
| 333 | g_vsnprintf( text, IRC_MAX_LINE, format, params ); |
---|
| 334 | va_end( params ); |
---|
| 335 | |
---|
| 336 | irc_write( iu->irc, ":%s!%s@%s %s %s :%s", |
---|
| 337 | iu->nick, iu->user, iu->host, type, dst, text ); |
---|
| 338 | } |
---|
| 339 | |
---|
[0b5cc72] | 340 | void irc_send_nick( irc_user_t *iu, const char *new ) |
---|
| 341 | { |
---|
| 342 | irc_write( iu->irc, ":%s!%s@%s NICK %s", |
---|
| 343 | iu->nick, iu->user, iu->host, new ); |
---|
| 344 | } |
---|
[6a9d068] | 345 | |
---|
| 346 | /* Send an update of a user's mode inside a channel, compared to what it was. */ |
---|
| 347 | void irc_send_channel_user_mode_diff( irc_channel_t *ic, irc_user_t *iu, |
---|
| 348 | irc_channel_user_flags_t old, irc_channel_user_flags_t new ) |
---|
| 349 | { |
---|
| 350 | char changes[3*(5+strlen(iu->nick))]; |
---|
| 351 | char from[strlen(ic->irc->root->nick)+strlen(ic->irc->root->user)+strlen(ic->irc->root->host)+3]; |
---|
| 352 | int n; |
---|
| 353 | |
---|
| 354 | *changes = '\0'; n = 0; |
---|
| 355 | if( ( old & IRC_CHANNEL_USER_OP ) != ( new & IRC_CHANNEL_USER_OP ) ) |
---|
| 356 | { |
---|
| 357 | n ++; |
---|
| 358 | if( new & IRC_CHANNEL_USER_OP ) |
---|
| 359 | strcat( changes, "+o" ); |
---|
| 360 | else |
---|
| 361 | strcat( changes, "-o" ); |
---|
| 362 | } |
---|
| 363 | if( ( old & IRC_CHANNEL_USER_HALFOP ) != ( new & IRC_CHANNEL_USER_HALFOP ) ) |
---|
| 364 | { |
---|
| 365 | n ++; |
---|
| 366 | if( new & IRC_CHANNEL_USER_HALFOP ) |
---|
| 367 | strcat( changes, "+h" ); |
---|
| 368 | else |
---|
| 369 | strcat( changes, "-h" ); |
---|
| 370 | } |
---|
| 371 | if( ( old & IRC_CHANNEL_USER_VOICE ) != ( new & IRC_CHANNEL_USER_VOICE ) ) |
---|
| 372 | { |
---|
| 373 | n ++; |
---|
| 374 | if( new & IRC_CHANNEL_USER_VOICE ) |
---|
| 375 | strcat( changes, "+v" ); |
---|
| 376 | else |
---|
| 377 | strcat( changes, "-v" ); |
---|
| 378 | } |
---|
| 379 | while( n ) |
---|
| 380 | { |
---|
| 381 | strcat( changes, " " ); |
---|
| 382 | strcat( changes, iu->nick ); |
---|
| 383 | n --; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | if( set_getbool( &ic->irc->b->set, "simulate_netsplit" ) ) |
---|
| 387 | g_snprintf( from, sizeof( from ), "%s", ic->irc->root->host ); |
---|
| 388 | else |
---|
| 389 | g_snprintf( from, sizeof( from ), "%s!%s@%s", ic->irc->root->nick, |
---|
| 390 | ic->irc->root->user, ic->irc->root->host ); |
---|
| 391 | |
---|
| 392 | irc_write( ic->irc, ":%s MODE %s %s", from, ic->name, changes ); |
---|
| 393 | } |
---|