Author: Isomer <perry@coders.net>
[ircu2.10.12-pk.git] / ircd / ircd_reply.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_proto.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25 #include "ircd_reply.h"
26 #include "client.h"
27 #include "ircd.h"
28 #include "ircd_snprintf.h"
29 #include "numeric.h"
30 #include "msg.h"
31 #include "s_conf.h"
32 #include "s_debug.h"
33 #include "send.h"
34
35 #include <assert.h>
36 #include <string.h>
37
38 /* Report a protocol violation warning to anyone listening.  This can be
39  * easily used to cleanup the last couple of parts of the code up.
40  */
41  
42 int protocol_violation(struct Client* cptr, const char* pattern, ...)
43 {
44         va_list vl;
45         char buffer[512];
46         assert(pattern);
47         assert(cptr);
48         va_start(vl,pattern);
49         ircd_snprintf(0,buffer,sizeof(buffer)-2, 
50                 "Protocol Violation from %C: %v",vl);
51         sendcmdto_flag_butone(&me, CMD_DESYNCH, NULL, FLAGS_DEBUG, 
52                 ":%s", cptr, buffer);
53         va_end(vl);
54         return 0;
55 }
56
57 int need_more_params(struct Client* cptr, const char* cmd)
58 {
59   if (!MyUser(cptr))
60     protocol_violation(cptr,"Not enough parameters for %s",cmd);
61   send_reply(cptr, ERR_NEEDMOREPARAMS, cmd);
62   return 0;
63 }
64
65 /*
66  * send_error_to_client - send an error message to a client
67  * I don't know if this function is any faster than the other version
68  * but it is a bit easier to use. It's reentrant until it hits vsendto_one
69  * at least :) --Bleep
70  */
71 int send_error_to_client(struct Client* cptr, int error, ...)
72 {
73   va_list               vl;
74   char                  buf[BUFSIZE];
75   char*                 dest = buf;
76   const char*           src  = me.name;
77   const struct Numeric* num  = get_error_numeric(error);
78
79   assert(0 != cptr);
80   assert(0 != num);
81   /*
82    * prefix
83    */
84   *dest++ = ':';
85   while ((*dest = *src++))
86     ++dest;
87   *dest++ = ' ';
88   /*
89    * numeric
90    */
91   src = num->str;
92   while ((*dest = *src++))
93     ++dest;
94   *dest++ = ' ';
95   /*
96    * client name (nick)
97    */
98   src = cptr->name;
99   while ((*dest = *src++))
100     ++dest;
101   *dest++ = ' ';
102   /*
103    * reply format
104    */
105   strcpy(dest, num->format);
106
107 #if 0
108   Debug((DEBUG_INFO, "send_error_to_client: format: ->%s<-", buf));
109 #endif
110
111   va_start(vl, error);
112   vsendto_one(cptr, buf, vl);
113   va_end(vl);
114   return 0;
115 }
116
117
118 int send_reply(struct Client *to, int reply, ...)
119 {
120   struct VarData vd;
121   char sndbuf[IRC_BUFSIZE];
122   const struct Numeric *num;
123
124   assert(0 != to);
125   assert(0 != reply);
126
127   num = get_error_numeric(reply & ~SND_EXPLICIT); /* get reply... */
128
129   va_start(vd.vd_args, reply);
130
131   if (reply & SND_EXPLICIT) /* get right pattern */
132     vd.vd_format = (const char *) va_arg(vd.vd_args, char *);
133   else
134     vd.vd_format = num->format;
135
136   assert(0 != vd.vd_format);
137
138   /* build buffer */
139   ircd_snprintf(to->from, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %C %v", &me,
140                 num->str, to, &vd);
141
142   va_end(vd.vd_args);
143
144   /* send it to the user */
145   send_buffer(to, sndbuf);
146
147   return 0; /* convenience return */
148 }
149
150 int send_admin_info(struct Client* sptr)
151 {
152   const struct LocalConf* admin = conf_get_local();
153   assert(0 != sptr);
154
155   send_reply(sptr, RPL_ADMINME,    me.name);
156   send_reply(sptr, RPL_ADMINLOC1,  admin->location1);
157   send_reply(sptr, RPL_ADMINLOC2,  admin->location2);
158   send_reply(sptr, RPL_ADMINEMAIL, admin->contact);
159   return 0;
160 }
161
162