[5ebff60] | 1 | /********************************************************************\ |
---|
[ebaebfe] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[ebaebfe] | 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; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[ebaebfe] | 24 | */ |
---|
| 25 | |
---|
| 26 | #include "bitlbee.h" |
---|
| 27 | |
---|
[5ebff60] | 28 | void irc_send_num(irc_t *irc, int code, char *format, ...) |
---|
[ebaebfe] | 29 | { |
---|
| 30 | char text[IRC_MAX_LINE]; |
---|
| 31 | va_list params; |
---|
[5ebff60] | 32 | |
---|
| 33 | va_start(params, format); |
---|
| 34 | g_vsnprintf(text, IRC_MAX_LINE, format, params); |
---|
| 35 | va_end(params); |
---|
| 36 | |
---|
| 37 | irc_write(irc, ":%s %03d %s %s", irc->root->host, code, irc->user->nick ? : "*", text); |
---|
[ebaebfe] | 38 | } |
---|
| 39 | |
---|
[5ebff60] | 40 | void irc_send_login(irc_t *irc) |
---|
[ebaebfe] | 41 | { |
---|
[5ebff60] | 42 | irc_send_num(irc, 1, ":Welcome to the %s gateway, %s", PACKAGE, irc->user->nick); |
---|
| 43 | irc_send_num(irc, 2, ":Host %s is running %s %s %s/%s.", irc->root->host, |
---|
| 44 | PACKAGE, BITLBEE_VERSION, ARCH, CPU); |
---|
| 45 | irc_send_num(irc, 3, ":%s", IRCD_INFO); |
---|
| 46 | irc_send_num(irc, 4, "%s %s %s %s", irc->root->host, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES); |
---|
| 47 | irc_send_num(irc, 5, "PREFIX=(ohv)@%%+ CHANTYPES=%s CHANMODES=,,,%s NICKLEN=%d CHANNELLEN=%d " |
---|
| 48 | "NETWORK=BitlBee SAFELIST CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 " |
---|
| 49 | "FLOOD=0/9999 :are supported by this server", |
---|
| 50 | CTYPES, CMODES, MAX_NICK_LENGTH - 1, MAX_NICK_LENGTH - 1); |
---|
| 51 | irc_send_motd(irc); |
---|
[ebaebfe] | 52 | } |
---|
| 53 | |
---|
[5ebff60] | 54 | void irc_send_motd(irc_t *irc) |
---|
[ebaebfe] | 55 | { |
---|
[fef7813] | 56 | char motd[2048]; |
---|
[570f183] | 57 | ssize_t len; |
---|
[ebaebfe] | 58 | int fd; |
---|
[5ebff60] | 59 | |
---|
| 60 | fd = open(global.conf->motdfile, O_RDONLY); |
---|
| 61 | if (fd == -1 || (len = read(fd, motd, sizeof(motd) - 1)) <= 0) { |
---|
| 62 | irc_send_num(irc, 422, ":We don't need MOTDs."); |
---|
| 63 | } else { |
---|
[fef7813] | 64 | char linebuf[80]; |
---|
[ed320e8] | 65 | char *add = "", max, *in; |
---|
[5ebff60] | 66 | |
---|
[fef7813] | 67 | in = motd; |
---|
| 68 | motd[len] = '\0'; |
---|
[ebaebfe] | 69 | linebuf[79] = len = 0; |
---|
[5ebff60] | 70 | max = sizeof(linebuf) - 1; |
---|
| 71 | |
---|
| 72 | irc_send_num(irc, 375, ":- %s Message Of The Day - ", irc->root->host); |
---|
| 73 | while ((linebuf[len] = *(in++))) { |
---|
| 74 | if (linebuf[len] == '\n' || len == max) { |
---|
[ebaebfe] | 75 | linebuf[len] = 0; |
---|
[5ebff60] | 76 | irc_send_num(irc, 372, ":- %s", linebuf); |
---|
[ebaebfe] | 77 | len = 0; |
---|
[5ebff60] | 78 | } else if (linebuf[len] == '%') { |
---|
[fef7813] | 79 | linebuf[len] = *(in++); |
---|
[5ebff60] | 80 | if (linebuf[len] == 'h') { |
---|
[ebaebfe] | 81 | add = irc->root->host; |
---|
[5ebff60] | 82 | } else if (linebuf[len] == 'v') { |
---|
[ebaebfe] | 83 | add = BITLBEE_VERSION; |
---|
[5ebff60] | 84 | } else if (linebuf[len] == 'n') { |
---|
[ebaebfe] | 85 | add = irc->user->nick; |
---|
[5ebff60] | 86 | } else if (linebuf[len] == '\0') { |
---|
| 87 | in--; |
---|
| 88 | } else { |
---|
[ebaebfe] | 89 | add = "%"; |
---|
[5ebff60] | 90 | } |
---|
| 91 | |
---|
| 92 | strncpy(linebuf + len, add, max - len); |
---|
| 93 | while (linebuf[++len]) { |
---|
| 94 | ; |
---|
| 95 | } |
---|
| 96 | } else if (len < max) { |
---|
| 97 | len++; |
---|
[ebaebfe] | 98 | } |
---|
| 99 | } |
---|
[5ebff60] | 100 | irc_send_num(irc, 376, ":End of MOTD"); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if (fd != -1) { |
---|
| 104 | close(fd); |
---|
[ebaebfe] | 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
[3864c08] | 108 | /* Used by some funcs that generate PRIVMSGs to figure out if we're talking to |
---|
| 109 | this person in /query or in a control channel. WARNING: callers rely on |
---|
| 110 | this returning a pointer at irc->user_nick, not a copy of it. */ |
---|
[5ebff60] | 111 | const char *irc_user_msgdest(irc_user_t *iu) |
---|
[ebaebfe] | 112 | { |
---|
[e67e513] | 113 | irc_t *irc = iu->irc; |
---|
[f7ca587] | 114 | irc_channel_t *ic = NULL; |
---|
[e67e513] | 115 | |
---|
[5ebff60] | 116 | if (iu->last_channel) { |
---|
| 117 | if (iu->last_channel->flags & IRC_CHANNEL_JOINED) { |
---|
[f7ca587] | 118 | ic = iu->last_channel; |
---|
[5ebff60] | 119 | } else { |
---|
| 120 | ic = irc_channel_with_user(irc, iu); |
---|
| 121 | } |
---|
[74f1cde] | 122 | } |
---|
[5ebff60] | 123 | |
---|
| 124 | if (ic) { |
---|
[e67e513] | 125 | return ic->name; |
---|
[5ebff60] | 126 | } else { |
---|
[e67e513] | 127 | return irc->user->nick; |
---|
[5ebff60] | 128 | } |
---|
[e67e513] | 129 | } |
---|
| 130 | |
---|
| 131 | /* cmd = "PRIVMSG" or "NOTICE" */ |
---|
[5ebff60] | 132 | static void irc_usermsg_(const char *cmd, irc_user_t *iu, const char *format, va_list params) |
---|
[e67e513] | 133 | { |
---|
| 134 | char text[2048]; |
---|
| 135 | const char *dst; |
---|
[5ebff60] | 136 | |
---|
| 137 | g_vsnprintf(text, sizeof(text), format, params); |
---|
| 138 | |
---|
| 139 | dst = irc_user_msgdest(iu); |
---|
| 140 | irc_send_msg(iu, cmd, dst, text, NULL); |
---|
[e67e513] | 141 | } |
---|
| 142 | |
---|
[5ebff60] | 143 | void irc_usermsg(irc_user_t *iu, char *format, ...) |
---|
[e67e513] | 144 | { |
---|
| 145 | va_list params; |
---|
[5ebff60] | 146 | |
---|
| 147 | va_start(params, format); |
---|
| 148 | irc_usermsg_("PRIVMSG", iu, format, params); |
---|
| 149 | va_end(params); |
---|
[e67e513] | 150 | } |
---|
| 151 | |
---|
[5ebff60] | 152 | void irc_usernotice(irc_user_t *iu, char *format, ...) |
---|
[e67e513] | 153 | { |
---|
| 154 | va_list params; |
---|
[5ebff60] | 155 | |
---|
| 156 | va_start(params, format); |
---|
| 157 | irc_usermsg_("NOTICE", iu, format, params); |
---|
| 158 | va_end(params); |
---|
[e67e513] | 159 | } |
---|
| 160 | |
---|
[5ebff60] | 161 | void irc_rootmsg(irc_t *irc, char *format, ...) |
---|
[e67e513] | 162 | { |
---|
| 163 | va_list params; |
---|
[5ebff60] | 164 | |
---|
| 165 | va_start(params, format); |
---|
| 166 | irc_usermsg_("PRIVMSG", irc->root, format, params); |
---|
| 167 | va_end(params); |
---|
[ebaebfe] | 168 | } |
---|
[4be8239] | 169 | |
---|
[5ebff60] | 170 | void irc_send_join(irc_channel_t *ic, irc_user_t *iu) |
---|
[4be8239] | 171 | { |
---|
| 172 | irc_t *irc = ic->irc; |
---|
[5ebff60] | 173 | |
---|
[d63f37c] | 174 | if (irc->caps & CAP_EXTENDED_JOIN) { |
---|
| 175 | irc_write(irc, ":%s!%s@%s JOIN %s * :%s", iu->nick, iu->user, iu->host, ic->name, iu->fullname); |
---|
| 176 | } else { |
---|
| 177 | irc_write(irc, ":%s!%s@%s JOIN :%s", iu->nick, iu->user, iu->host, ic->name); |
---|
| 178 | } |
---|
[5ebff60] | 179 | |
---|
| 180 | if (iu == irc->user) { |
---|
| 181 | if (ic->topic && *ic->topic) { |
---|
| 182 | irc_send_topic(ic, FALSE); |
---|
| 183 | } |
---|
[2d93f113] | 184 | irc_send_names(ic); |
---|
[4be8239] | 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
[5ebff60] | 188 | void irc_send_part(irc_channel_t *ic, irc_user_t *iu, const char *reason) |
---|
[4be8239] | 189 | { |
---|
[5ebff60] | 190 | irc_write(ic->irc, ":%s!%s@%s PART %s :%s", iu->nick, iu->user, iu->host, ic->name, reason ? : ""); |
---|
[4be8239] | 191 | } |
---|
| 192 | |
---|
[5ebff60] | 193 | void irc_send_quit(irc_user_t *iu, const char *reason) |
---|
[1f0224c] | 194 | { |
---|
[5ebff60] | 195 | irc_write(iu->irc, ":%s!%s@%s QUIT :%s", iu->nick, iu->user, iu->host, reason ? : ""); |
---|
[1f0224c] | 196 | } |
---|
| 197 | |
---|
[5ebff60] | 198 | void irc_send_kick(irc_channel_t *ic, irc_user_t *iu, irc_user_t *kicker, const char *reason) |
---|
[006a84f] | 199 | { |
---|
[5ebff60] | 200 | irc_write(ic->irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, |
---|
| 201 | kicker->host, ic->name, iu->nick, reason ? : ""); |
---|
[006a84f] | 202 | } |
---|
| 203 | |
---|
[f4396c4] | 204 | #define IRC_NAMES_LEN 385 |
---|
| 205 | |
---|
[5ebff60] | 206 | void irc_send_names(irc_channel_t *ic) |
---|
[4be8239] | 207 | { |
---|
| 208 | GSList *l; |
---|
[f4396c4] | 209 | GString *namelist = g_string_sized_new(IRC_NAMES_LEN); |
---|
[c54bb11] | 210 | gboolean uhnames = (ic->irc->caps & CAP_USERHOST_IN_NAMES); |
---|
[5ebff60] | 211 | |
---|
[4be8239] | 212 | /* RFCs say there is no error reply allowed on NAMES, so when the |
---|
| 213 | channel is invalid, just give an empty reply. */ |
---|
[5ebff60] | 214 | for (l = ic->users; l; l = l->next) { |
---|
[e54112f] | 215 | irc_channel_user_t *icu = l->data; |
---|
| 216 | irc_user_t *iu = icu->iu; |
---|
[f4396c4] | 217 | size_t extra_len = strlen(iu->nick); |
---|
| 218 | char prefix; |
---|
[5ebff60] | 219 | |
---|
[c54bb11] | 220 | if (uhnames) { |
---|
| 221 | extra_len += strlen(iu->user) + strlen(iu->host) + 2; |
---|
| 222 | } |
---|
| 223 | |
---|
[f4396c4] | 224 | if (namelist->len + extra_len > IRC_NAMES_LEN - 4) { |
---|
| 225 | irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist->str); |
---|
| 226 | g_string_truncate(namelist, 0); |
---|
[4be8239] | 227 | } |
---|
[5ebff60] | 228 | |
---|
[f4396c4] | 229 | if ((prefix = irc_channel_user_get_prefix(icu))) { |
---|
| 230 | g_string_append_c(namelist, prefix); |
---|
| 231 | } |
---|
[5ebff60] | 232 | |
---|
[c54bb11] | 233 | if (uhnames) { |
---|
| 234 | g_string_append_printf(namelist, "%s!%s@%s ", iu->nick, iu->user, iu->host); |
---|
| 235 | } else { |
---|
| 236 | g_string_append(namelist, iu->nick); |
---|
| 237 | g_string_append_c(namelist, ' '); |
---|
| 238 | } |
---|
[4be8239] | 239 | } |
---|
[5ebff60] | 240 | |
---|
[f4396c4] | 241 | if (namelist->len) { |
---|
| 242 | irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist->str); |
---|
[5ebff60] | 243 | } |
---|
| 244 | |
---|
| 245 | irc_send_num(ic->irc, 366, "%s :End of /NAMES list", ic->name); |
---|
[f4396c4] | 246 | |
---|
| 247 | g_string_free(namelist, TRUE); |
---|
[4be8239] | 248 | } |
---|
| 249 | |
---|
[5ebff60] | 250 | void irc_send_topic(irc_channel_t *ic, gboolean topic_change) |
---|
[4be8239] | 251 | { |
---|
[5ebff60] | 252 | if (topic_change && ic->topic_who) { |
---|
| 253 | irc_write(ic->irc, ":%s TOPIC %s :%s", ic->topic_who, |
---|
| 254 | ic->name, ic->topic && *ic->topic ? ic->topic : ""); |
---|
| 255 | } else if (ic->topic) { |
---|
| 256 | irc_send_num(ic->irc, 332, "%s :%s", ic->name, ic->topic); |
---|
| 257 | if (ic->topic_who) { |
---|
| 258 | irc_send_num(ic->irc, 333, "%s %s %d", |
---|
| 259 | ic->name, ic->topic_who, (int) ic->topic_time); |
---|
| 260 | } |
---|
| 261 | } else { |
---|
| 262 | irc_send_num(ic->irc, 331, "%s :No topic for this channel", ic->name); |
---|
[83e92bf] | 263 | } |
---|
[4be8239] | 264 | } |
---|
[b95932e] | 265 | |
---|
[0d8a9bb0] | 266 | /* msg1 and msg2 are output parameters. If msg2 is non-null, msg1 is guaranteed to be non-null too. |
---|
| 267 | The idea is to defer the formatting of "$msg1 ($msg2)" to later calls to avoid a g_strdup_printf() here. */ |
---|
| 268 | static void get_status_message(bee_user_t *bu, char **msg1, char **msg2) |
---|
| 269 | { |
---|
| 270 | *msg1 = NULL; |
---|
| 271 | *msg2 = NULL; |
---|
| 272 | |
---|
| 273 | if (!(bu->flags & BEE_USER_ONLINE)) { |
---|
| 274 | *msg1 = "User is offline"; |
---|
| 275 | |
---|
| 276 | } else if ((bu->status && *bu->status) || |
---|
| 277 | (bu->status_msg && *bu->status_msg)) { |
---|
| 278 | |
---|
| 279 | if (bu->status && bu->status_msg) { |
---|
| 280 | *msg1 = bu->status; |
---|
| 281 | *msg2 = bu->status_msg; |
---|
| 282 | } else { |
---|
| 283 | *msg1 = bu->status ? : bu->status_msg; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | if (*msg1 && !**msg1) { |
---|
| 288 | *msg1 = (bu->flags & BEE_USER_AWAY) ? "Away" : NULL; |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
[5ebff60] | 292 | void irc_send_whois(irc_user_t *iu) |
---|
[b95932e] | 293 | { |
---|
| 294 | irc_t *irc = iu->irc; |
---|
[5ebff60] | 295 | |
---|
| 296 | irc_send_num(irc, 311, "%s %s %s * :%s", |
---|
| 297 | iu->nick, iu->user, iu->host, iu->fullname); |
---|
| 298 | |
---|
| 299 | if (iu->bu) { |
---|
[1d39159] | 300 | bee_user_t *bu = iu->bu; |
---|
[0d8a9bb0] | 301 | char *msg1, *msg2; |
---|
| 302 | int num; |
---|
[5ebff60] | 303 | |
---|
| 304 | irc_send_num(irc, 312, "%s %s.%s :%s network", iu->nick, bu->ic->acc->user, |
---|
| 305 | bu->ic->acc->server && *bu->ic->acc->server ? bu->ic->acc->server : "", |
---|
| 306 | bu->ic->acc->prpl->name); |
---|
| 307 | |
---|
[0d8a9bb0] | 308 | num = (bu->flags & BEE_USER_AWAY || !(bu->flags & BEE_USER_ONLINE)) ? 301 : 320; |
---|
[5ebff60] | 309 | |
---|
[0d8a9bb0] | 310 | get_status_message(bu, &msg1, &msg2); |
---|
| 311 | |
---|
| 312 | if (msg1 && msg2) { |
---|
| 313 | irc_send_num(irc, num, "%s :%s (%s)", iu->nick, msg1, msg2); |
---|
| 314 | } else if (msg1) { |
---|
| 315 | irc_send_num(irc, num, "%s :%s", iu->nick, msg1); |
---|
[eb50495] | 316 | } |
---|
[5ebff60] | 317 | |
---|
| 318 | if (bu->idle_time || bu->login_time) { |
---|
| 319 | irc_send_num(irc, 317, "%s %d %d :seconds idle, signon time", |
---|
| 320 | iu->nick, |
---|
| 321 | bu->idle_time ? (int) (time(NULL) - bu->idle_time) : 0, |
---|
| 322 | (int) bu->login_time); |
---|
[56699f0] | 323 | } |
---|
[5ebff60] | 324 | } else { |
---|
| 325 | irc_send_num(irc, 312, "%s %s :%s", iu->nick, irc->root->host, IRCD_INFO); |
---|
[1d39159] | 326 | } |
---|
[5ebff60] | 327 | |
---|
| 328 | irc_send_num(irc, 318, "%s :End of /WHOIS list", iu->nick); |
---|
[b95932e] | 329 | } |
---|
[2f53ada] | 330 | |
---|
[5ebff60] | 331 | void irc_send_who(irc_t *irc, GSList *l, const char *channel) |
---|
[2f53ada] | 332 | { |
---|
[5ebff60] | 333 | gboolean is_channel = strchr(CTYPES, channel[0]) != NULL; |
---|
| 334 | |
---|
| 335 | while (l) { |
---|
[687ec88] | 336 | irc_user_t *iu; |
---|
| 337 | |
---|
| 338 | /* Null terminated string with three chars, respectively: |
---|
| 339 | * { <H|G>, <@|%|+|\0>, \0 } */ |
---|
| 340 | char status_prefix[3] = {0}; |
---|
| 341 | |
---|
[5ebff60] | 342 | if (is_channel) { |
---|
[687ec88] | 343 | irc_channel_user_t *icu = l->data; |
---|
| 344 | status_prefix[1] = irc_channel_user_get_prefix(icu); |
---|
| 345 | iu = icu->iu; |
---|
| 346 | } else { |
---|
| 347 | iu = l->data; |
---|
[5ebff60] | 348 | } |
---|
[687ec88] | 349 | |
---|
[2f73692] | 350 | /* rfc1459 doesn't mention this: G means gone, H means here */ |
---|
| 351 | status_prefix[0] = iu->flags & IRC_USER_AWAY ? 'G' : 'H'; |
---|
| 352 | |
---|
[687ec88] | 353 | irc_send_num(irc, 352, "%s %s %s %s %s %s :0 %s", |
---|
[5ebff60] | 354 | is_channel ? channel : "*", iu->user, iu->host, irc->root->host, |
---|
[687ec88] | 355 | iu->nick, status_prefix, iu->fullname); |
---|
[2f53ada] | 356 | l = l->next; |
---|
| 357 | } |
---|
[5ebff60] | 358 | |
---|
| 359 | irc_send_num(irc, 315, "%s :End of /WHO list", channel); |
---|
[2f53ada] | 360 | } |
---|
[280c56a] | 361 | |
---|
[5ebff60] | 362 | void irc_send_msg(irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix) |
---|
[6761a40] | 363 | { |
---|
| 364 | char last = 0; |
---|
| 365 | const char *s = msg, *line = msg; |
---|
[5ebff60] | 366 | char raw_msg[strlen(msg) + 1024]; |
---|
| 367 | |
---|
| 368 | while (!last) { |
---|
| 369 | if (*s == '\r' && *(s + 1) == '\n') { |
---|
[6761a40] | 370 | s++; |
---|
| 371 | } |
---|
[5ebff60] | 372 | if (*s == '\n') { |
---|
| 373 | last = s[1] == 0; |
---|
| 374 | } else { |
---|
[6761a40] | 375 | last = s[0] == 0; |
---|
| 376 | } |
---|
[5ebff60] | 377 | if (*s == 0 || *s == '\n') { |
---|
| 378 | if (g_strncasecmp(line, "/me ", 4) == 0 && (!prefix || !*prefix) && |
---|
| 379 | g_strcasecmp(type, "PRIVMSG") == 0) { |
---|
| 380 | strcpy(raw_msg, "\001ACTION "); |
---|
| 381 | strncat(raw_msg, line + 4, s - line - 4); |
---|
| 382 | strcat(raw_msg, "\001"); |
---|
| 383 | irc_send_msg_raw(iu, type, dst, raw_msg); |
---|
| 384 | } else { |
---|
[6761a40] | 385 | *raw_msg = '\0'; |
---|
[5ebff60] | 386 | if (prefix && *prefix) { |
---|
| 387 | strcpy(raw_msg, prefix); |
---|
| 388 | } |
---|
| 389 | strncat(raw_msg, line, s - line); |
---|
| 390 | irc_send_msg_raw(iu, type, dst, raw_msg); |
---|
[6761a40] | 391 | } |
---|
| 392 | line = s + 1; |
---|
| 393 | } |
---|
[5ebff60] | 394 | s++; |
---|
[6761a40] | 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
[5ebff60] | 398 | void irc_send_msg_raw(irc_user_t *iu, const char *type, const char *dst, const char *msg) |
---|
[280c56a] | 399 | { |
---|
[5ebff60] | 400 | irc_write(iu->irc, ":%s!%s@%s %s %s :%s", |
---|
| 401 | iu->nick, iu->user, iu->host, type, dst, msg && *msg ? msg : " "); |
---|
[280c56a] | 402 | } |
---|
[0b5cc72] | 403 | |
---|
[5ebff60] | 404 | void irc_send_msg_f(irc_user_t *iu, const char *type, const char *dst, const char *format, ...) |
---|
[7b59872] | 405 | { |
---|
| 406 | char text[IRC_MAX_LINE]; |
---|
| 407 | va_list params; |
---|
[5ebff60] | 408 | |
---|
| 409 | va_start(params, format); |
---|
| 410 | g_vsnprintf(text, IRC_MAX_LINE, format, params); |
---|
| 411 | va_end(params); |
---|
| 412 | |
---|
| 413 | irc_write(iu->irc, ":%s!%s@%s %s %s :%s", |
---|
| 414 | iu->nick, iu->user, iu->host, type, dst, text); |
---|
[7b59872] | 415 | } |
---|
| 416 | |
---|
[5ebff60] | 417 | void irc_send_nick(irc_user_t *iu, const char *new) |
---|
[0b5cc72] | 418 | { |
---|
[5ebff60] | 419 | irc_write(iu->irc, ":%s!%s@%s NICK %s", |
---|
| 420 | iu->nick, iu->user, iu->host, new); |
---|
[0b5cc72] | 421 | } |
---|
[6a9d068] | 422 | |
---|
| 423 | /* Send an update of a user's mode inside a channel, compared to what it was. */ |
---|
[5ebff60] | 424 | void irc_send_channel_user_mode_diff(irc_channel_t *ic, irc_user_t *iu, |
---|
| 425 | irc_channel_user_flags_t old, irc_channel_user_flags_t new) |
---|
[6a9d068] | 426 | { |
---|
[5ebff60] | 427 | char changes[3 * (5 + strlen(iu->nick))]; |
---|
| 428 | char from[strlen(ic->irc->root->nick) + strlen(ic->irc->root->user) + strlen(ic->irc->root->host) + 3]; |
---|
[6a9d068] | 429 | int n; |
---|
[5ebff60] | 430 | |
---|
[6a9d068] | 431 | *changes = '\0'; n = 0; |
---|
[5ebff60] | 432 | if ((old & IRC_CHANNEL_USER_OP) != (new & IRC_CHANNEL_USER_OP)) { |
---|
| 433 | n++; |
---|
| 434 | if (new & IRC_CHANNEL_USER_OP) { |
---|
| 435 | strcat(changes, "+o"); |
---|
| 436 | } else { |
---|
| 437 | strcat(changes, "-o"); |
---|
| 438 | } |
---|
[6a9d068] | 439 | } |
---|
[5ebff60] | 440 | if ((old & IRC_CHANNEL_USER_HALFOP) != (new & IRC_CHANNEL_USER_HALFOP)) { |
---|
| 441 | n++; |
---|
| 442 | if (new & IRC_CHANNEL_USER_HALFOP) { |
---|
| 443 | strcat(changes, "+h"); |
---|
| 444 | } else { |
---|
| 445 | strcat(changes, "-h"); |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | if ((old & IRC_CHANNEL_USER_VOICE) != (new & IRC_CHANNEL_USER_VOICE)) { |
---|
| 449 | n++; |
---|
| 450 | if (new & IRC_CHANNEL_USER_VOICE) { |
---|
| 451 | strcat(changes, "+v"); |
---|
| 452 | } else { |
---|
| 453 | strcat(changes, "-v"); |
---|
| 454 | } |
---|
[6a9d068] | 455 | } |
---|
[5ebff60] | 456 | while (n) { |
---|
| 457 | strcat(changes, " "); |
---|
| 458 | strcat(changes, iu->nick); |
---|
| 459 | n--; |
---|
[6a9d068] | 460 | } |
---|
[5ebff60] | 461 | |
---|
| 462 | if (set_getbool(&ic->irc->b->set, "simulate_netsplit")) { |
---|
| 463 | g_snprintf(from, sizeof(from), "%s", ic->irc->root->host); |
---|
| 464 | } else { |
---|
| 465 | g_snprintf(from, sizeof(from), "%s!%s@%s", ic->irc->root->nick, |
---|
| 466 | ic->irc->root->user, ic->irc->root->host); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | if (*changes) { |
---|
| 470 | irc_write(ic->irc, ":%s MODE %s %s", from, ic->name, changes); |
---|
[6a9d068] | 471 | } |
---|
| 472 | } |
---|
[1aa74f55] | 473 | |
---|
[5ebff60] | 474 | void irc_send_invite(irc_user_t *iu, irc_channel_t *ic) |
---|
[1aa74f55] | 475 | { |
---|
| 476 | irc_t *irc = iu->irc; |
---|
[5ebff60] | 477 | |
---|
| 478 | irc_write(iu->irc, ":%s!%s@%s INVITE %s :%s", |
---|
| 479 | iu->nick, iu->user, iu->host, irc->user->nick, ic->name); |
---|
[1aa74f55] | 480 | } |
---|
[0ef1c92] | 481 | |
---|
| 482 | void irc_send_cap(irc_t *irc, char *subcommand, char *body) |
---|
| 483 | { |
---|
| 484 | char *nick = irc->user->nick ? : "*"; |
---|
| 485 | |
---|
| 486 | irc_write(irc, ":%s CAP %s %s :%s", irc->root->host, nick, subcommand, body); |
---|
| 487 | } |
---|
[80c2f3c] | 488 | |
---|
| 489 | void irc_send_away_notify(irc_user_t *iu) |
---|
| 490 | { |
---|
| 491 | bee_user_t *bu = iu->bu; |
---|
| 492 | |
---|
| 493 | if (!bu) { |
---|
| 494 | return; |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | if (bu->flags & BEE_USER_AWAY || !(bu->flags & BEE_USER_ONLINE)) { |
---|
| 498 | char *msg1, *msg2; |
---|
| 499 | |
---|
| 500 | get_status_message(bu, &msg1, &msg2); |
---|
| 501 | |
---|
| 502 | if (msg2) { |
---|
| 503 | irc_write(iu->irc, ":%s!%s@%s AWAY :%s (%s)", iu->nick, iu->user, iu->host, msg1, msg2); |
---|
| 504 | } else { |
---|
| 505 | irc_write(iu->irc, ":%s!%s@%s AWAY :%s", iu->nick, iu->user, iu->host, msg1); |
---|
| 506 | } |
---|
| 507 | } else { |
---|
| 508 | irc_write(iu->irc, ":%s!%s@%s AWAY", iu->nick, iu->user, iu->host); |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | |
---|