Anope IRC Services  Version 1.8
send.c
Go to the documentation of this file.
1 /* Routines for sending stuff to the network.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 
14 #include "services.h"
15 
16 /*************************************************************************/
17 
26 void send_cmd(const char *source, const char *fmt, ...)
27 {
28  va_list args;
29 
30  if (fmt) {
31  va_start(args, fmt);
32  vsend_cmd(source, fmt, args);
33  va_end(args);
34  }
35 }
36 
37 /*************************************************************************/
38 
46 void vsend_cmd(const char *source, const char *fmt, va_list args)
47 {
48  char buf[BUFSIZE];
49  *buf = '\0';
50 
51  if (fmt) {
52  vsnprintf(buf, BUFSIZE - 1, fmt, args);
53 
54  if (source) {
55  sockprintf(servsock, ":%s %s\r\n", source, buf);
56  eventprintf(":%s %s", source, buf);
57  if (debug) {
58  alog("debug: Sent: :%s %s", source, buf);
59  }
60  } else {
61  sockprintf(servsock, "%s\r\n", buf);
62  eventprintf("%s", buf);
63  if (debug) {
64  alog("debug: Sent: %s", buf);
65  }
66  }
67  }
68 }
69 
70 /*************************************************************************/
71 
80 void notice_server(char *source, Server * s, char *fmt, ...)
81 {
82  va_list args;
83  char buf[BUFSIZE];
84  *buf = '\0';
85 
86  if (fmt) {
87  va_start(args, fmt);
88  vsnprintf(buf, BUFSIZE - 1, fmt, args);
89 
90  if (NSDefFlags & NI_MSG) {
91  anope_cmd_serv_privmsg(source, s->name, buf);
92  } else {
93  anope_cmd_serv_notice(source, s->name, buf);
94  }
95  va_end(args);
96  }
97 }
98 
99 /*************************************************************************/
100 
109 void notice_user(char *source, User * u, const char *fmt, ...)
110 {
111  va_list args;
112  char buf[BUFSIZE];
113  *buf = '\0';
114 
115  if (fmt) {
116  va_start(args, fmt);
117  vsnprintf(buf, BUFSIZE - 1, fmt, args);
118 
119  /* Send privmsg instead of notice if:
120  * - UsePrivmsg is enabled
121  * - The user is not registered and NSDefMsg is enabled
122  * - The user is registered and has set /ns set msg on
123  */
124  if (UsePrivmsg && ((!u->na && (NSDefFlags & NI_MSG))
125  || (u->na && (u->na->nc->flags & NI_MSG)))) {
126  anope_cmd_privmsg2(source, u->nick, buf);
127  } else {
128  anope_cmd_notice2(source, u->nick, buf);
129  }
130  va_end(args);
131  }
132 }
133 
134 /*************************************************************************/
135 
143 void notice_list(char *source, char *dest, char **text)
144 {
145  while (*text) {
146  /* Have to kludge around an ircII bug here: if a notice includes
147  * no text, it is ignored, so we replace blank lines by lines
148  * with a single space.
149  */
150  if (**text) {
151  anope_cmd_notice2(source, dest, *text);
152  } else {
153  anope_cmd_notice2(source, dest, " ");
154  }
155  text++;
156  }
157 }
158 
159 /*************************************************************************/
160 
169 void notice_lang(char *source, User * dest, int message, ...)
170 {
171  va_list args;
172  char buf[4096]; /* because messages can be really big */
173  char *s, *t;
174  const char *fmt;
175 
176  if (!dest || !message) {
177  return;
178  }
179  va_start(args, message);
180  fmt = getstring(dest->na, message);
181 
182  if (!fmt)
183  return;
184  memset(buf, 0, 4096);
185  vsnprintf(buf, sizeof(buf), fmt, args);
186  s = buf;
187  while (*s) {
188  t = s;
189  s += strcspn(s, "\n");
190  if (*s)
191  *s++ = 0;
192  /* Send privmsg instead of notice if:
193  * - UsePrivmsg is enabled
194  * - The user is not registered and NSDefMsg is enabled
195  * - The user is registered and has set /ns set msg on
196  */
197  if (UsePrivmsg && ((!dest->na && (NSDefFlags & NI_MSG))
198  || (dest->na
199  && (dest->na->nc->flags & NI_MSG)))) {
200  anope_cmd_privmsg2(source, dest->nick, *t ? t : " ");
201  } else {
202  anope_cmd_notice2(source, dest->nick, *t ? t : " ");
203  }
204  }
205  va_end(args);
206 }
207 
208 /*************************************************************************/
209 
220 void notice_help(char *source, User * dest, int message, ...)
221 {
222  va_list args;
223  char buf[4096], buf2[4096], outbuf[BUFSIZE];
224  char *s, *t;
225  const char *fmt;
226 
227  if (!dest || !message) {
228  return;
229  }
230  va_start(args, message);
231  fmt = getstring(dest->na, message);
232  if (!fmt)
233  return;
234  /* Some sprintf()'s eat %S or turn it into just S, so change all %S's
235  * into \1\1... we assume this doesn't occur anywhere else in the
236  * string. */
237  strscpy(buf2, fmt, sizeof(buf2));
238  strnrepl(buf2, sizeof(buf2), "%S", "\1\1");
239  vsnprintf(buf, sizeof(buf), buf2, args);
240  s = buf;
241  while (*s) {
242  t = s;
243  s += strcspn(s, "\n");
244  if (*s)
245  *s++ = 0;
246  strscpy(outbuf, t, sizeof(outbuf));
247  strnrepl(outbuf, sizeof(outbuf), "\1\1", source);
248  /* Send privmsg instead of notice if:
249  * - UsePrivmsg is enabled
250  * - The user is not registered and NSDefMsg is enabled
251  * - The user is registered and has set /ns set msg on
252  */
253  if (UsePrivmsg && ((!dest->na && (NSDefFlags & NI_MSG))
254  || (dest->na
255  && (dest->na->nc->flags & NI_MSG)))) {
256  anope_cmd_privmsg2(source, dest->nick, *outbuf ? outbuf : " ");
257  } else {
258  anope_cmd_notice2(source, dest->nick, *outbuf ? outbuf : " ");
259  }
260  }
261  va_end(args);
262 }
263 
264 /*************************************************************************/
265 
274 void notice(char *source, char *dest, const char *fmt, ...)
275 {
276  va_list args;
277  char buf[BUFSIZE];
278  *buf = '\0';
279 
280  if (fmt) {
281  va_start(args, fmt);
282  vsnprintf(buf, BUFSIZE - 1, fmt, args);
283 
284  if (NSDefFlags & NI_MSG) {
285  anope_cmd_privmsg2(source, dest, buf);
286  } else {
287  anope_cmd_notice2(source, dest, buf);
288  }
289  va_end(args);
290  }
291 }
292 
293 /*************************************************************************/
294 
303 void privmsg(char *source, char *dest, const char *fmt, ...)
304 {
305  va_list args;
306  char buf[BUFSIZE];
307  *buf = '\0';
308 
309  if (fmt) {
310  va_start(args, fmt);
311  vsnprintf(buf, BUFSIZE - 1, fmt, args);
312  va_end(args);
313  }
314 
315  anope_cmd_privmsg2(source, dest, buf);
316 }
317 
318 /*************************************************************************/
319 
327 void wallops(char *source, const char *fmt, ...)
328 {
329  va_list args;
330  char buf[BUFSIZE];
331  *buf = '\0';
332 
333  if (fmt) {
334  va_start(args, fmt);
335  vsnprintf(buf, BUFSIZE - 1, fmt, args);
336  va_end(args);
337  }
338 
339  anope_cmd_global_legacy(source, buf);
340 }
void notice_help(char *source, User *dest, int message,...)
Definition: send.c:220
void notice_lang(char *source, User *dest, int message,...)
Definition: send.c:169
E void eventprintf(char *fmt,...)
Definition: events.c:66
char nick[NICKMAX]
Definition: services.h:875
void notice_user(char *source, User *u, const char *fmt,...)
Definition: send.c:109
E void anope_cmd_global_legacy(char *source, char *fmt)
Definition: ircd.c:519
E int sockprintf(ano_socket_t s, char *fmt,...)
Definition: sockutil.c:502
E void anope_cmd_notice2(char *source, char *dest, char *msg)
Definition: ircd.c:270
void privmsg(char *source, char *dest, const char *fmt,...)
Definition: send.c:303
#define getstring(na, index)
Definition: extern.h:731
E char * strscpy(char *d, const char *s, size_t len)
Definition: db-merger.c:1886
void notice(char *source, char *dest, const char *fmt,...)
Definition: send.c:274
NickCore * nc
Definition: services.h:533
E void anope_cmd_privmsg2(char *source, char *dest, char *msg)
Definition: ircd.c:309
void notice_list(char *source, char *dest, char **text)
Definition: send.c:143
E void anope_cmd_serv_notice(char *source, char *dest, char *msg)
Definition: ircd.c:314
void notice_server(char *source, Server *s, char *fmt,...)
Definition: send.c:80
E int servsock
Definition: extern.h:794
void wallops(char *source, const char *fmt,...)
Definition: send.c:327
char * name
Definition: services.h:854
E void alog(const char *fmt,...) FORMAT(printf
#define NI_MSG
Definition: services.h:1292
E int debug
Definition: extern.h:775
E int NSDefFlags
Definition: extern.h:377
E int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
E void anope_cmd_serv_privmsg(char *source, char *dest, char *msg)
Definition: ircd.c:319
uint32 flags
Definition: services.h:548
void send_cmd(const char *source, const char *fmt,...)
Definition: send.c:26
void vsend_cmd(const char *source, const char *fmt, va_list args)
Definition: send.c:46
E int UsePrivmsg
Definition: extern.h:352
#define BUFSIZE
Definition: config.h:47
E char * strnrepl(char *s, int32 size, const char *old, const char *new)
Definition: misc.c:144
NickAlias * na
Definition: services.h:892