send.c

Go to the documentation of this file.
00001 /* Routines for sending stuff to the network.
00002  *
00003  * (C) 2003-2007 Anope Team
00004  * Contact us at info@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church. 
00010  * 
00011  * $Id: send.c 1282 2007-08-28 14:20:44Z geniusdex $ 
00012  *
00013  */
00014 
00015 #include "services.h"
00016 
00017 /*************************************************************************/
00018 
00027 void send_cmd(const char *source, const char *fmt, ...)
00028 {
00029     va_list args;
00030 
00031     if (fmt) {
00032         va_start(args, fmt);
00033         vsend_cmd(source, fmt, args);
00034         va_end(args);
00035     }
00036 }
00037 
00038 /*************************************************************************/
00039 
00047 void vsend_cmd(const char *source, const char *fmt, va_list args)
00048 {
00049     char buf[BUFSIZE];
00050     *buf = '\0';
00051 
00052     if (fmt) {
00053         vsnprintf(buf, BUFSIZE - 1, fmt, args);
00054 
00055         if (!buf) {
00056             return;
00057         }
00058 
00059         if (source) {
00060             sockprintf(servsock, ":%s %s\r\n", source, buf);
00061             eventprintf(":%s %s", source, buf);
00062             if (debug) {
00063                 alog("debug: Sent: :%s %s", source, buf);
00064             }
00065         } else {
00066             sockprintf(servsock, "%s\r\n", buf);
00067             eventprintf("%s", buf);
00068             if (debug) {
00069                 alog("debug: Sent: %s", buf);
00070             }
00071         }
00072     }
00073 }
00074 
00075 /*************************************************************************/
00076 
00085 void notice_server(char *source, Server * s, char *fmt, ...)
00086 {
00087     va_list args;
00088     char buf[BUFSIZE];
00089     *buf = '\0';
00090 
00091     if (fmt) {
00092         va_start(args, fmt);
00093         vsnprintf(buf, BUFSIZE - 1, fmt, args);
00094 
00095         if (!buf) {
00096             va_end(args);
00097             return;
00098         }
00099 
00100         if (NSDefFlags & NI_MSG) {
00101             anope_cmd_serv_privmsg(source, s->name, buf);
00102         } else {
00103             anope_cmd_serv_notice(source, s->name, buf);
00104         }
00105         va_end(args);
00106     }
00107 }
00108 
00109 /*************************************************************************/
00110 
00119 void notice_user(char *source, User * u, const char *fmt, ...)
00120 {
00121     va_list args;
00122     char buf[BUFSIZE];
00123     *buf = '\0';
00124 
00125     if (fmt) {
00126         va_start(args, fmt);
00127         vsnprintf(buf, BUFSIZE - 1, fmt, args);
00128 
00129         if (!buf) {
00130             va_end(args);
00131             return;
00132         }
00133 
00134         /* Send privmsg instead of notice if:
00135          * - UsePrivmsg is enabled
00136          * - The user is not registered and NSDefMsg is enabled
00137          * - The user is registered and has set /ns set msg on
00138          */
00139         if (UsePrivmsg && ((!u->na && (NSDefFlags & NI_MSG))
00140                            || (u->na && (u->na->nc->flags & NI_MSG)))) {
00141             anope_cmd_privmsg2(source, u->nick, buf);
00142         } else {
00143             anope_cmd_notice2(source, u->nick, buf);
00144         }
00145         va_end(args);
00146     }
00147 }
00148 
00149 /*************************************************************************/
00150 
00158 void notice_list(char *source, char *dest, char **text)
00159 {
00160     while (*text) {
00161         /* Have to kludge around an ircII bug here: if a notice includes
00162          * no text, it is ignored, so we replace blank lines by lines
00163          * with a single space.
00164          */
00165         if (**text) {
00166             anope_cmd_notice2(source, dest, *text);
00167         } else {
00168             anope_cmd_notice2(source, dest, " ");
00169         }
00170         text++;
00171     }
00172 }
00173 
00174 /*************************************************************************/
00175 
00184 void notice_lang(char *source, User * dest, int message, ...)
00185 {
00186     va_list args;
00187     char buf[4096];             /* because messages can be really big */
00188     char *s, *t;
00189     const char *fmt;
00190 
00191     if (!dest || !message) {
00192         return;
00193     }
00194     va_start(args, message);
00195     fmt = getstring(dest->na, message);
00196 
00197     if (!fmt)
00198         return;
00199     memset(buf, 0, 4096);
00200     vsnprintf(buf, sizeof(buf), fmt, args);
00201     s = buf;
00202     while (*s) {
00203         t = s;
00204         s += strcspn(s, "\n");
00205         if (*s)
00206             *s++ = 0;
00207         /* Send privmsg instead of notice if:
00208          * - UsePrivmsg is enabled
00209          * - The user is not registered and NSDefMsg is enabled
00210          * - The user is registered and has set /ns set msg on
00211          */
00212         if (UsePrivmsg && ((!dest->na && (NSDefFlags & NI_MSG))
00213                            || (dest->na
00214                                && (dest->na->nc->flags & NI_MSG)))) {
00215             anope_cmd_privmsg2(source, dest->nick, *t ? t : " ");
00216         } else {
00217             anope_cmd_notice2(source, dest->nick, *t ? t : " ");
00218         }
00219     }
00220     va_end(args);
00221 }
00222 
00223 /*************************************************************************/
00224 
00235 void notice_help(char *source, User * dest, int message, ...)
00236 {
00237     va_list args;
00238     char buf[4096], buf2[4096], outbuf[BUFSIZE];
00239     char *s, *t;
00240     const char *fmt;
00241 
00242     if (!dest || !message) {
00243         return;
00244     }
00245     va_start(args, message);
00246     fmt = getstring(dest->na, message);
00247     if (!fmt)
00248         return;
00249     /* Some sprintf()'s eat %S or turn it into just S, so change all %S's
00250      * into \1\1... we assume this doesn't occur anywhere else in the
00251      * string. */
00252     strscpy(buf2, fmt, sizeof(buf2));
00253     strnrepl(buf2, sizeof(buf2), "%S", "\1\1");
00254     vsnprintf(buf, sizeof(buf), buf2, args);
00255     s = buf;
00256     while (*s) {
00257         t = s;
00258         s += strcspn(s, "\n");
00259         if (*s)
00260             *s++ = 0;
00261         strscpy(outbuf, t, sizeof(outbuf));
00262         strnrepl(outbuf, sizeof(outbuf), "\1\1", source);
00263         /* Send privmsg instead of notice if:
00264          * - UsePrivmsg is enabled
00265          * - The user is not registered and NSDefMsg is enabled
00266          * - The user is registered and has set /ns set msg on
00267          */
00268         if (UsePrivmsg && ((!dest->na && (NSDefFlags & NI_MSG))
00269                            || (dest->na
00270                                && (dest->na->nc->flags & NI_MSG)))) {
00271             anope_cmd_privmsg2(source, dest->nick, *outbuf ? outbuf : " ");
00272         } else {
00273             anope_cmd_notice2(source, dest->nick, *outbuf ? outbuf : " ");
00274         }
00275     }
00276     va_end(args);
00277 }
00278 
00279 /*************************************************************************/
00280 
00289 void notice(char *source, char *dest, const char *fmt, ...)
00290 {
00291     va_list args;
00292     char buf[BUFSIZE];
00293     *buf = '\0';
00294 
00295     if (fmt) {
00296         va_start(args, fmt);
00297         vsnprintf(buf, BUFSIZE - 1, fmt, args);
00298 
00299         if (!buf) {
00300             va_end(args);
00301             return;
00302         }
00303 
00304         if (NSDefFlags & NI_MSG) {
00305             anope_cmd_privmsg2(source, dest, buf);
00306         } else {
00307             anope_cmd_notice2(source, dest, buf);
00308         }
00309         va_end(args);
00310     }
00311 }
00312 
00313 /*************************************************************************/
00314 
00323 void privmsg(char *source, char *dest, const char *fmt, ...)
00324 {
00325     va_list args;
00326     char buf[BUFSIZE];
00327     *buf = '\0';
00328 
00329     if (fmt) {
00330         va_start(args, fmt);
00331         vsnprintf(buf, BUFSIZE - 1, fmt, args);
00332         va_end(args);
00333     }
00334 
00335     if (!buf) {
00336         return;
00337     }
00338     anope_cmd_privmsg2(source, dest, buf);
00339 }
00340 
00341 /*************************************************************************/
00342 
00350 void wallops(char *source, const char *fmt, ...)
00351 {
00352     va_list args;
00353     char buf[BUFSIZE];
00354     *buf = '\0';
00355 
00356     if (fmt) {
00357         va_start(args, fmt);
00358         vsnprintf(buf, BUFSIZE - 1, fmt, args);
00359         va_end(args);
00360     }
00361     if (!buf) {
00362         return;
00363     }
00364 
00365     anope_cmd_global_legacy(source, buf);
00366 }

Generated on Sun Dec 30 09:26:53 2007 for Anope by  doxygen 1.5.1-20070107