source: irc_send.c @ 0ef1c92

Last change on this file since 0ef1c92 was 0ef1c92, checked in by dequis <dx@…>, at 2015-09-11T02:31:10Z

Initial implementation of ircv3 capability negotiation

Mostly no-op for now. Puts registration on hold, supports the basic
commands, and NAKs everything

  • Property mode set to 100644
File size: 12.0 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);
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]54void 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]111const 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]132static 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]143void 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]152void 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]161void 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]170void irc_send_join(irc_channel_t *ic, irc_user_t *iu)
[4be8239]171{
172        irc_t *irc = ic->irc;
[5ebff60]173
174        irc_write(irc, ":%s!%s@%s JOIN :%s", iu->nick, iu->user, iu->host, ic->name);
175
176        if (iu == irc->user) {
177                if (ic->topic && *ic->topic) {
178                        irc_send_topic(ic, FALSE);
179                }
[2d93f113]180                irc_send_names(ic);
[4be8239]181        }
182}
183
[5ebff60]184void irc_send_part(irc_channel_t *ic, irc_user_t *iu, const char *reason)
[4be8239]185{
[5ebff60]186        irc_write(ic->irc, ":%s!%s@%s PART %s :%s", iu->nick, iu->user, iu->host, ic->name, reason ? : "");
[4be8239]187}
188
[5ebff60]189void irc_send_quit(irc_user_t *iu, const char *reason)
[1f0224c]190{
[5ebff60]191        irc_write(iu->irc, ":%s!%s@%s QUIT :%s", iu->nick, iu->user, iu->host, reason ? : "");
[1f0224c]192}
193
[5ebff60]194void irc_send_kick(irc_channel_t *ic, irc_user_t *iu, irc_user_t *kicker, const char *reason)
[006a84f]195{
[5ebff60]196        irc_write(ic->irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user,
197                  kicker->host, ic->name, iu->nick, reason ? : "");
[006a84f]198}
199
[5ebff60]200void irc_send_names(irc_channel_t *ic)
[4be8239]201{
202        GSList *l;
203        char namelist[385] = "";
[5ebff60]204
[4be8239]205        /* RFCs say there is no error reply allowed on NAMES, so when the
206           channel is invalid, just give an empty reply. */
[5ebff60]207        for (l = ic->users; l; l = l->next) {
[e54112f]208                irc_channel_user_t *icu = l->data;
209                irc_user_t *iu = icu->iu;
[5ebff60]210
211                if (strlen(namelist) + strlen(iu->nick) > sizeof(namelist) - 4) {
212                        irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist);
[4be8239]213                        *namelist = 0;
214                }
[5ebff60]215
216                if (icu->flags & IRC_CHANNEL_USER_OP) {
217                        strcat(namelist, "@");
218                } else if (icu->flags & IRC_CHANNEL_USER_HALFOP) {
219                        strcat(namelist, "%");
220                } else if (icu->flags & IRC_CHANNEL_USER_VOICE) {
221                        strcat(namelist, "+");
222                }
223
224                strcat(namelist, iu->nick);
225                strcat(namelist, " ");
[4be8239]226        }
[5ebff60]227
228        if (*namelist) {
229                irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist);
230        }
231
232        irc_send_num(ic->irc, 366, "%s :End of /NAMES list", ic->name);
[4be8239]233}
234
[5ebff60]235void irc_send_topic(irc_channel_t *ic, gboolean topic_change)
[4be8239]236{
[5ebff60]237        if (topic_change && ic->topic_who) {
238                irc_write(ic->irc, ":%s TOPIC %s :%s", ic->topic_who,
239                          ic->name, ic->topic && *ic->topic ? ic->topic : "");
240        } else if (ic->topic) {
241                irc_send_num(ic->irc, 332, "%s :%s", ic->name, ic->topic);
242                if (ic->topic_who) {
243                        irc_send_num(ic->irc, 333, "%s %s %d",
244                                     ic->name, ic->topic_who, (int) ic->topic_time);
245                }
246        } else {
247                irc_send_num(ic->irc, 331, "%s :No topic for this channel", ic->name);
[83e92bf]248        }
[4be8239]249}
[b95932e]250
[5ebff60]251void irc_send_whois(irc_user_t *iu)
[b95932e]252{
253        irc_t *irc = iu->irc;
[5ebff60]254
255        irc_send_num(irc, 311, "%s %s %s * :%s",
256                     iu->nick, iu->user, iu->host, iu->fullname);
257
258        if (iu->bu) {
[1d39159]259                bee_user_t *bu = iu->bu;
[5ebff60]260
261                irc_send_num(irc, 312, "%s %s.%s :%s network", iu->nick, bu->ic->acc->user,
262                             bu->ic->acc->server && *bu->ic->acc->server ? bu->ic->acc->server : "",
263                             bu->ic->acc->prpl->name);
264
265                if ((bu->status && *bu->status) ||
266                    (bu->status_msg && *bu->status_msg)) {
[d986463]267                        int num = bu->flags & BEE_USER_AWAY ? 301 : 320;
[5ebff60]268
269                        if (bu->status && bu->status_msg) {
270                                irc_send_num(irc, num, "%s :%s (%s)", iu->nick, bu->status, bu->status_msg);
271                        } else {
272                                irc_send_num(irc, num, "%s :%s", iu->nick, bu->status ? : bu->status_msg);
273                        }
274                } else if (!(bu->flags & BEE_USER_ONLINE)) {
275                        irc_send_num(irc, 301, "%s :%s", iu->nick, "User is offline");
[eb50495]276                }
[5ebff60]277
278                if (bu->idle_time || bu->login_time) {
279                        irc_send_num(irc, 317, "%s %d %d :seconds idle, signon time",
280                                     iu->nick,
281                                     bu->idle_time ? (int) (time(NULL) - bu->idle_time) : 0,
282                                     (int) bu->login_time);
[56699f0]283                }
[5ebff60]284        } else {
285                irc_send_num(irc, 312, "%s %s :%s", iu->nick, irc->root->host, IRCD_INFO);
[1d39159]286        }
[5ebff60]287
288        irc_send_num(irc, 318, "%s :End of /WHOIS list", iu->nick);
[b95932e]289}
[2f53ada]290
[5ebff60]291void irc_send_who(irc_t *irc, GSList *l, const char *channel)
[2f53ada]292{
[5ebff60]293        gboolean is_channel = strchr(CTYPES, channel[0]) != NULL;
294
295        while (l) {
[2f53ada]296                irc_user_t *iu = l->data;
[5ebff60]297                if (is_channel) {
298                        iu = ((irc_channel_user_t *) iu)->iu;
299                }
[2f53ada]300                /* TODO(wilmer): Restore away/channel information here */
[5ebff60]301                irc_send_num(irc, 352, "%s %s %s %s %s %c :0 %s",
302                             is_channel ? channel : "*", iu->user, iu->host, irc->root->host,
303                             iu->nick, iu->flags & IRC_USER_AWAY ? 'G' : 'H',
304                             iu->fullname);
[2f53ada]305                l = l->next;
306        }
[5ebff60]307
308        irc_send_num(irc, 315, "%s :End of /WHO list", channel);
[2f53ada]309}
[280c56a]310
[5ebff60]311void irc_send_msg(irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix)
[6761a40]312{
313        char last = 0;
314        const char *s = msg, *line = msg;
[5ebff60]315        char raw_msg[strlen(msg) + 1024];
316
317        while (!last) {
318                if (*s == '\r' && *(s + 1) == '\n') {
[6761a40]319                        s++;
320                }
[5ebff60]321                if (*s == '\n') {
322                        last = s[1] == 0;
323                } else {
[6761a40]324                        last = s[0] == 0;
325                }
[5ebff60]326                if (*s == 0 || *s == '\n') {
327                        if (g_strncasecmp(line, "/me ", 4) == 0 && (!prefix || !*prefix) &&
328                            g_strcasecmp(type, "PRIVMSG") == 0) {
329                                strcpy(raw_msg, "\001ACTION ");
330                                strncat(raw_msg, line + 4, s - line - 4);
331                                strcat(raw_msg, "\001");
332                                irc_send_msg_raw(iu, type, dst, raw_msg);
333                        } else {
[6761a40]334                                *raw_msg = '\0';
[5ebff60]335                                if (prefix && *prefix) {
336                                        strcpy(raw_msg, prefix);
337                                }
338                                strncat(raw_msg, line, s - line);
339                                irc_send_msg_raw(iu, type, dst, raw_msg);
[6761a40]340                        }
341                        line = s + 1;
342                }
[5ebff60]343                s++;
[6761a40]344        }
345}
346
[5ebff60]347void irc_send_msg_raw(irc_user_t *iu, const char *type, const char *dst, const char *msg)
[280c56a]348{
[5ebff60]349        irc_write(iu->irc, ":%s!%s@%s %s %s :%s",
350                  iu->nick, iu->user, iu->host, type, dst, msg && *msg ? msg : " ");
[280c56a]351}
[0b5cc72]352
[5ebff60]353void irc_send_msg_f(irc_user_t *iu, const char *type, const char *dst, const char *format, ...)
[7b59872]354{
355        char text[IRC_MAX_LINE];
356        va_list params;
[5ebff60]357
358        va_start(params, format);
359        g_vsnprintf(text, IRC_MAX_LINE, format, params);
360        va_end(params);
361
362        irc_write(iu->irc, ":%s!%s@%s %s %s :%s",
363                  iu->nick, iu->user, iu->host, type, dst, text);
[7b59872]364}
365
[5ebff60]366void irc_send_nick(irc_user_t *iu, const char *new)
[0b5cc72]367{
[5ebff60]368        irc_write(iu->irc, ":%s!%s@%s NICK %s",
369                  iu->nick, iu->user, iu->host, new);
[0b5cc72]370}
[6a9d068]371
372/* Send an update of a user's mode inside a channel, compared to what it was. */
[5ebff60]373void irc_send_channel_user_mode_diff(irc_channel_t *ic, irc_user_t *iu,
374                                     irc_channel_user_flags_t old, irc_channel_user_flags_t new)
[6a9d068]375{
[5ebff60]376        char changes[3 * (5 + strlen(iu->nick))];
377        char from[strlen(ic->irc->root->nick) + strlen(ic->irc->root->user) + strlen(ic->irc->root->host) + 3];
[6a9d068]378        int n;
[5ebff60]379
[6a9d068]380        *changes = '\0'; n = 0;
[5ebff60]381        if ((old & IRC_CHANNEL_USER_OP) != (new & IRC_CHANNEL_USER_OP)) {
382                n++;
383                if (new & IRC_CHANNEL_USER_OP) {
384                        strcat(changes, "+o");
385                } else {
386                        strcat(changes, "-o");
387                }
[6a9d068]388        }
[5ebff60]389        if ((old & IRC_CHANNEL_USER_HALFOP) != (new & IRC_CHANNEL_USER_HALFOP)) {
390                n++;
391                if (new & IRC_CHANNEL_USER_HALFOP) {
392                        strcat(changes, "+h");
393                } else {
394                        strcat(changes, "-h");
395                }
396        }
397        if ((old & IRC_CHANNEL_USER_VOICE) != (new & IRC_CHANNEL_USER_VOICE)) {
398                n++;
399                if (new & IRC_CHANNEL_USER_VOICE) {
400                        strcat(changes, "+v");
401                } else {
402                        strcat(changes, "-v");
403                }
[6a9d068]404        }
[5ebff60]405        while (n) {
406                strcat(changes, " ");
407                strcat(changes, iu->nick);
408                n--;
[6a9d068]409        }
[5ebff60]410
411        if (set_getbool(&ic->irc->b->set, "simulate_netsplit")) {
412                g_snprintf(from, sizeof(from), "%s", ic->irc->root->host);
413        } else {
414                g_snprintf(from, sizeof(from), "%s!%s@%s", ic->irc->root->nick,
415                           ic->irc->root->user, ic->irc->root->host);
416        }
417
418        if (*changes) {
419                irc_write(ic->irc, ":%s MODE %s %s", from, ic->name, changes);
[6a9d068]420        }
421}
[1aa74f55]422
[5ebff60]423void irc_send_invite(irc_user_t *iu, irc_channel_t *ic)
[1aa74f55]424{
425        irc_t *irc = iu->irc;
[5ebff60]426
427        irc_write(iu->irc, ":%s!%s@%s INVITE %s :%s",
428                  iu->nick, iu->user, iu->host, irc->user->nick, ic->name);
[1aa74f55]429}
[0ef1c92]430
431void irc_send_cap(irc_t *irc, char *subcommand, char *body)
432{
433        char *nick = irc->user->nick ? : "*";
434
435        irc_write(irc, ":%s CAP %s %s :%s", irc->root->host, nick, subcommand, body);
436}
Note: See TracBrowser for help on using the repository browser.