1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 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., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 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 | |
---|
37 | irc_write(irc, ":%s %03d %s %s", irc->root->host, code, irc->user->nick ? : "*", text); |
---|
38 | } |
---|
39 | |
---|
40 | void irc_send_login(irc_t *irc) |
---|
41 | { |
---|
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); |
---|
52 | } |
---|
53 | |
---|
54 | void irc_send_motd(irc_t *irc) |
---|
55 | { |
---|
56 | char motd[2048]; |
---|
57 | ssize_t len; |
---|
58 | int fd; |
---|
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 { |
---|
64 | char linebuf[80]; |
---|
65 | char *add = "", max, *in; |
---|
66 | |
---|
67 | in = motd; |
---|
68 | motd[len] = '\0'; |
---|
69 | linebuf[79] = len = 0; |
---|
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) { |
---|
75 | linebuf[len] = 0; |
---|
76 | irc_send_num(irc, 372, ":- %s", linebuf); |
---|
77 | len = 0; |
---|
78 | } else if (linebuf[len] == '%') { |
---|
79 | linebuf[len] = *(in++); |
---|
80 | if (linebuf[len] == 'h') { |
---|
81 | add = irc->root->host; |
---|
82 | } else if (linebuf[len] == 'v') { |
---|
83 | add = BITLBEE_VERSION; |
---|
84 | } else if (linebuf[len] == 'n') { |
---|
85 | add = irc->user->nick; |
---|
86 | } else if (linebuf[len] == '\0') { |
---|
87 | in--; |
---|
88 | } else { |
---|
89 | add = "%"; |
---|
90 | } |
---|
91 | |
---|
92 | strncpy(linebuf + len, add, max - len); |
---|
93 | while (linebuf[++len]) { |
---|
94 | ; |
---|
95 | } |
---|
96 | } else if (len < max) { |
---|
97 | len++; |
---|
98 | } |
---|
99 | } |
---|
100 | irc_send_num(irc, 376, ":End of MOTD"); |
---|
101 | } |
---|
102 | |
---|
103 | if (fd != -1) { |
---|
104 | close(fd); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
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. */ |
---|
111 | const char *irc_user_msgdest(irc_user_t *iu) |
---|
112 | { |
---|
113 | irc_t *irc = iu->irc; |
---|
114 | irc_channel_t *ic = NULL; |
---|
115 | |
---|
116 | if (iu->last_channel) { |
---|
117 | if (iu->last_channel->flags & IRC_CHANNEL_JOINED) { |
---|
118 | ic = iu->last_channel; |
---|
119 | } else { |
---|
120 | ic = irc_channel_with_user(irc, iu); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | if (ic) { |
---|
125 | return ic->name; |
---|
126 | } else { |
---|
127 | return irc->user->nick; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /* cmd = "PRIVMSG" or "NOTICE" */ |
---|
132 | static void irc_usermsg_(const char *cmd, irc_user_t *iu, const char *format, va_list params) |
---|
133 | { |
---|
134 | char text[2048]; |
---|
135 | const char *dst; |
---|
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); |
---|
141 | } |
---|
142 | |
---|
143 | void irc_usermsg(irc_user_t *iu, char *format, ...) |
---|
144 | { |
---|
145 | va_list params; |
---|
146 | |
---|
147 | va_start(params, format); |
---|
148 | irc_usermsg_("PRIVMSG", iu, format, params); |
---|
149 | va_end(params); |
---|
150 | } |
---|
151 | |
---|
152 | void irc_usernotice(irc_user_t *iu, char *format, ...) |
---|
153 | { |
---|
154 | va_list params; |
---|
155 | |
---|
156 | va_start(params, format); |
---|
157 | irc_usermsg_("NOTICE", iu, format, params); |
---|
158 | va_end(params); |
---|
159 | } |
---|
160 | |
---|
161 | void irc_rootmsg(irc_t *irc, char *format, ...) |
---|
162 | { |
---|
163 | va_list params; |
---|
164 | |
---|
165 | va_start(params, format); |
---|
166 | irc_usermsg_("PRIVMSG", irc->root, format, params); |
---|
167 | va_end(params); |
---|
168 | } |
---|
169 | |
---|
170 | void irc_send_join(irc_channel_t *ic, irc_user_t *iu) |
---|
171 | { |
---|
172 | irc_t *irc = ic->irc; |
---|
173 | |
---|
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 | } |
---|
179 | |
---|
180 | if (iu == irc->user) { |
---|
181 | if (ic->topic && *ic->topic) { |
---|
182 | irc_send_topic(ic, FALSE); |
---|
183 | } |
---|
184 | irc_send_names(ic); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | void irc_send_part(irc_channel_t *ic, irc_user_t *iu, const char *reason) |
---|
189 | { |
---|
190 | irc_write(ic->irc, ":%s!%s@%s PART %s :%s", iu->nick, iu->user, iu->host, ic->name, reason ? : ""); |
---|
191 | } |
---|
192 | |
---|
193 | void irc_send_quit(irc_user_t *iu, const char *reason) |
---|
194 | { |
---|
195 | irc_write(iu->irc, ":%s!%s@%s QUIT :%s", iu->nick, iu->user, iu->host, reason ? : ""); |
---|
196 | } |
---|
197 | |
---|
198 | void irc_send_kick(irc_channel_t *ic, irc_user_t *iu, irc_user_t *kicker, const char *reason) |
---|
199 | { |
---|
200 | irc_write(ic->irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, |
---|
201 | kicker->host, ic->name, iu->nick, reason ? : ""); |
---|
202 | } |
---|
203 | |
---|
204 | void irc_send_names(irc_channel_t *ic) |
---|
205 | { |
---|
206 | GSList *l; |
---|
207 | char namelist[385] = ""; |
---|
208 | |
---|
209 | /* RFCs say there is no error reply allowed on NAMES, so when the |
---|
210 | channel is invalid, just give an empty reply. */ |
---|
211 | for (l = ic->users; l; l = l->next) { |
---|
212 | irc_channel_user_t *icu = l->data; |
---|
213 | irc_user_t *iu = icu->iu; |
---|
214 | |
---|
215 | if (strlen(namelist) + strlen(iu->nick) > sizeof(namelist) - 4) { |
---|
216 | irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist); |
---|
217 | *namelist = 0; |
---|
218 | } |
---|
219 | |
---|
220 | namelist[strlen(namelist) + 1] = '\0'; |
---|
221 | namelist[strlen(namelist)] = irc_channel_user_get_prefix(icu); |
---|
222 | |
---|
223 | strcat(namelist, iu->nick); |
---|
224 | strcat(namelist, " "); |
---|
225 | } |
---|
226 | |
---|
227 | if (*namelist) { |
---|
228 | irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist); |
---|
229 | } |
---|
230 | |
---|
231 | irc_send_num(ic->irc, 366, "%s :End of /NAMES list", ic->name); |
---|
232 | } |
---|
233 | |
---|
234 | void irc_send_topic(irc_channel_t *ic, gboolean topic_change) |
---|
235 | { |
---|
236 | if (topic_change && ic->topic_who) { |
---|
237 | irc_write(ic->irc, ":%s TOPIC %s :%s", ic->topic_who, |
---|
238 | ic->name, ic->topic && *ic->topic ? ic->topic : ""); |
---|
239 | } else if (ic->topic) { |
---|
240 | irc_send_num(ic->irc, 332, "%s :%s", ic->name, ic->topic); |
---|
241 | if (ic->topic_who) { |
---|
242 | irc_send_num(ic->irc, 333, "%s %s %d", |
---|
243 | ic->name, ic->topic_who, (int) ic->topic_time); |
---|
244 | } |
---|
245 | } else { |
---|
246 | irc_send_num(ic->irc, 331, "%s :No topic for this channel", ic->name); |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | void irc_send_whois(irc_user_t *iu) |
---|
251 | { |
---|
252 | irc_t *irc = iu->irc; |
---|
253 | |
---|
254 | irc_send_num(irc, 311, "%s %s %s * :%s", |
---|
255 | iu->nick, iu->user, iu->host, iu->fullname); |
---|
256 | |
---|
257 | if (iu->bu) { |
---|
258 | bee_user_t *bu = iu->bu; |
---|
259 | |
---|
260 | irc_send_num(irc, 312, "%s %s.%s :%s network", iu->nick, bu->ic->acc->user, |
---|
261 | bu->ic->acc->server && *bu->ic->acc->server ? bu->ic->acc->server : "", |
---|
262 | bu->ic->acc->prpl->name); |
---|
263 | |
---|
264 | if ((bu->status && *bu->status) || |
---|
265 | (bu->status_msg && *bu->status_msg)) { |
---|
266 | int num = bu->flags & BEE_USER_AWAY ? 301 : 320; |
---|
267 | |
---|
268 | if (bu->status && bu->status_msg) { |
---|
269 | irc_send_num(irc, num, "%s :%s (%s)", iu->nick, bu->status, bu->status_msg); |
---|
270 | } else { |
---|
271 | irc_send_num(irc, num, "%s :%s", iu->nick, bu->status ? : bu->status_msg); |
---|
272 | } |
---|
273 | } else if (!(bu->flags & BEE_USER_ONLINE)) { |
---|
274 | irc_send_num(irc, 301, "%s :%s", iu->nick, "User is offline"); |
---|
275 | } |
---|
276 | |
---|
277 | if (bu->idle_time || bu->login_time) { |
---|
278 | irc_send_num(irc, 317, "%s %d %d :seconds idle, signon time", |
---|
279 | iu->nick, |
---|
280 | bu->idle_time ? (int) (time(NULL) - bu->idle_time) : 0, |
---|
281 | (int) bu->login_time); |
---|
282 | } |
---|
283 | } else { |
---|
284 | irc_send_num(irc, 312, "%s %s :%s", iu->nick, irc->root->host, IRCD_INFO); |
---|
285 | } |
---|
286 | |
---|
287 | irc_send_num(irc, 318, "%s :End of /WHOIS list", iu->nick); |
---|
288 | } |
---|
289 | |
---|
290 | void irc_send_who(irc_t *irc, GSList *l, const char *channel) |
---|
291 | { |
---|
292 | gboolean is_channel = strchr(CTYPES, channel[0]) != NULL; |
---|
293 | |
---|
294 | while (l) { |
---|
295 | irc_user_t *iu; |
---|
296 | |
---|
297 | /* Null terminated string with three chars, respectively: |
---|
298 | * { <H|G>, <@|%|+|\0>, \0 } */ |
---|
299 | char status_prefix[3] = {0}; |
---|
300 | |
---|
301 | if (is_channel) { |
---|
302 | irc_channel_user_t *icu = l->data; |
---|
303 | status_prefix[1] = irc_channel_user_get_prefix(icu); |
---|
304 | iu = icu->iu; |
---|
305 | } else { |
---|
306 | iu = l->data; |
---|
307 | } |
---|
308 | |
---|
309 | /* rfc1459 doesn't mention this: G means gone, H means here */ |
---|
310 | status_prefix[0] = iu->flags & IRC_USER_AWAY ? 'G' : 'H'; |
---|
311 | |
---|
312 | irc_send_num(irc, 352, "%s %s %s %s %s %s :0 %s", |
---|
313 | is_channel ? channel : "*", iu->user, iu->host, irc->root->host, |
---|
314 | iu->nick, status_prefix, iu->fullname); |
---|
315 | l = l->next; |
---|
316 | } |
---|
317 | |
---|
318 | irc_send_num(irc, 315, "%s :End of /WHO list", channel); |
---|
319 | } |
---|
320 | |
---|
321 | void irc_send_msg(irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix) |
---|
322 | { |
---|
323 | char last = 0; |
---|
324 | const char *s = msg, *line = msg; |
---|
325 | char raw_msg[strlen(msg) + 1024]; |
---|
326 | |
---|
327 | while (!last) { |
---|
328 | if (*s == '\r' && *(s + 1) == '\n') { |
---|
329 | s++; |
---|
330 | } |
---|
331 | if (*s == '\n') { |
---|
332 | last = s[1] == 0; |
---|
333 | } else { |
---|
334 | last = s[0] == 0; |
---|
335 | } |
---|
336 | if (*s == 0 || *s == '\n') { |
---|
337 | if (g_strncasecmp(line, "/me ", 4) == 0 && (!prefix || !*prefix) && |
---|
338 | g_strcasecmp(type, "PRIVMSG") == 0) { |
---|
339 | strcpy(raw_msg, "\001ACTION "); |
---|
340 | strncat(raw_msg, line + 4, s - line - 4); |
---|
341 | strcat(raw_msg, "\001"); |
---|
342 | irc_send_msg_raw(iu, type, dst, raw_msg); |
---|
343 | } else { |
---|
344 | *raw_msg = '\0'; |
---|
345 | if (prefix && *prefix) { |
---|
346 | strcpy(raw_msg, prefix); |
---|
347 | } |
---|
348 | strncat(raw_msg, line, s - line); |
---|
349 | irc_send_msg_raw(iu, type, dst, raw_msg); |
---|
350 | } |
---|
351 | line = s + 1; |
---|
352 | } |
---|
353 | s++; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | void irc_send_msg_raw(irc_user_t *iu, const char *type, const char *dst, const char *msg) |
---|
358 | { |
---|
359 | irc_write(iu->irc, ":%s!%s@%s %s %s :%s", |
---|
360 | iu->nick, iu->user, iu->host, type, dst, msg && *msg ? msg : " "); |
---|
361 | } |
---|
362 | |
---|
363 | void irc_send_msg_f(irc_user_t *iu, const char *type, const char *dst, const char *format, ...) |
---|
364 | { |
---|
365 | char text[IRC_MAX_LINE]; |
---|
366 | va_list params; |
---|
367 | |
---|
368 | va_start(params, format); |
---|
369 | g_vsnprintf(text, IRC_MAX_LINE, format, params); |
---|
370 | va_end(params); |
---|
371 | |
---|
372 | irc_write(iu->irc, ":%s!%s@%s %s %s :%s", |
---|
373 | iu->nick, iu->user, iu->host, type, dst, text); |
---|
374 | } |
---|
375 | |
---|
376 | void irc_send_nick(irc_user_t *iu, const char *new) |
---|
377 | { |
---|
378 | irc_write(iu->irc, ":%s!%s@%s NICK %s", |
---|
379 | iu->nick, iu->user, iu->host, new); |
---|
380 | } |
---|
381 | |
---|
382 | /* Send an update of a user's mode inside a channel, compared to what it was. */ |
---|
383 | void irc_send_channel_user_mode_diff(irc_channel_t *ic, irc_user_t *iu, |
---|
384 | irc_channel_user_flags_t old, irc_channel_user_flags_t new) |
---|
385 | { |
---|
386 | char changes[3 * (5 + strlen(iu->nick))]; |
---|
387 | char from[strlen(ic->irc->root->nick) + strlen(ic->irc->root->user) + strlen(ic->irc->root->host) + 3]; |
---|
388 | int n; |
---|
389 | |
---|
390 | *changes = '\0'; n = 0; |
---|
391 | if ((old & IRC_CHANNEL_USER_OP) != (new & IRC_CHANNEL_USER_OP)) { |
---|
392 | n++; |
---|
393 | if (new & IRC_CHANNEL_USER_OP) { |
---|
394 | strcat(changes, "+o"); |
---|
395 | } else { |
---|
396 | strcat(changes, "-o"); |
---|
397 | } |
---|
398 | } |
---|
399 | if ((old & IRC_CHANNEL_USER_HALFOP) != (new & IRC_CHANNEL_USER_HALFOP)) { |
---|
400 | n++; |
---|
401 | if (new & IRC_CHANNEL_USER_HALFOP) { |
---|
402 | strcat(changes, "+h"); |
---|
403 | } else { |
---|
404 | strcat(changes, "-h"); |
---|
405 | } |
---|
406 | } |
---|
407 | if ((old & IRC_CHANNEL_USER_VOICE) != (new & IRC_CHANNEL_USER_VOICE)) { |
---|
408 | n++; |
---|
409 | if (new & IRC_CHANNEL_USER_VOICE) { |
---|
410 | strcat(changes, "+v"); |
---|
411 | } else { |
---|
412 | strcat(changes, "-v"); |
---|
413 | } |
---|
414 | } |
---|
415 | while (n) { |
---|
416 | strcat(changes, " "); |
---|
417 | strcat(changes, iu->nick); |
---|
418 | n--; |
---|
419 | } |
---|
420 | |
---|
421 | if (set_getbool(&ic->irc->b->set, "simulate_netsplit")) { |
---|
422 | g_snprintf(from, sizeof(from), "%s", ic->irc->root->host); |
---|
423 | } else { |
---|
424 | g_snprintf(from, sizeof(from), "%s!%s@%s", ic->irc->root->nick, |
---|
425 | ic->irc->root->user, ic->irc->root->host); |
---|
426 | } |
---|
427 | |
---|
428 | if (*changes) { |
---|
429 | irc_write(ic->irc, ":%s MODE %s %s", from, ic->name, changes); |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | void irc_send_invite(irc_user_t *iu, irc_channel_t *ic) |
---|
434 | { |
---|
435 | irc_t *irc = iu->irc; |
---|
436 | |
---|
437 | irc_write(iu->irc, ":%s!%s@%s INVITE %s :%s", |
---|
438 | iu->nick, iu->user, iu->host, irc->user->nick, ic->name); |
---|
439 | } |
---|
440 | |
---|
441 | void irc_send_cap(irc_t *irc, char *subcommand, char *body) |
---|
442 | { |
---|
443 | char *nick = irc->user->nick ? : "*"; |
---|
444 | |
---|
445 | irc_write(irc, ":%s CAP %s %s :%s", irc->root->host, nick, subcommand, body); |
---|
446 | } |
---|