source: irc_send.c

Last change on this file was 9767d03, checked in by dequis <dx@…>, at 2018-07-31T04:41:55Z

Modify server-time implementation to not change API

The new functions with the different paramters are flagged as internal,
so it doesn't even add new symbols.

abi-compliance checker says it's 100% compatible, yay.

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