Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_opmode.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_tmpl.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
6  *
7  * See file AUTHORS in IRC package for additional names of
8  * the programmers.
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 1, or (at your option)
13  * 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
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * $Id$
25  */
26
27 /*
28  * m_functions execute protocol messages on this server:
29  *
30  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
31  *            structure (with an open socket connected!). This
32  *            identifies the physical socket where the message
33  *            originated (or which caused the m_function to be
34  *            executed--some m_functions may call others...).
35  *
36  *    sptr    is the source of the message, defined by the
37  *            prefix part of the message if present. If not
38  *            or prefix not found, then sptr==cptr.
39  *
40  *            (!IsServer(cptr)) => (cptr == sptr), because
41  *            prefixes are taken *only* from servers...
42  *
43  *            (IsServer(cptr))
44  *                    (sptr == cptr) => the message didn't
45  *                    have the prefix.
46  *
47  *                    (sptr != cptr && IsServer(sptr) means
48  *                    the prefix specified servername. (?)
49  *
50  *                    (sptr != cptr && !IsServer(sptr) means
51  *                    that message originated from a remote
52  *                    user (not local).
53  *
54  *            combining
55  *
56  *            (!IsServer(sptr)) means that, sptr can safely
57  *            taken as defining the target structure of the
58  *            message in this server.
59  *
60  *    *Always* true (if 'parse' and others are working correct):
61  *
62  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
63  *
64  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
65  *            *cannot* be a local connection, unless it's
66  *            actually cptr!). [MyConnect(x) should probably
67  *            be defined as (x == x->from) --msa ]
68  *
69  *    parc    number of variable parameter strings (if zero,
70  *            parv is allowed to be NULL)
71  *
72  *    parv    a NULL terminated list of parameter pointers,
73  *
74  *                    parv[0], sender (prefix string), if not present
75  *                            this points to an empty string.
76  *                    parv[1]...parv[parc-1]
77  *                            pointers to additional parameters
78  *                    parv[parc] == NULL, *always*
79  *
80  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
81  *                    non-NULL pointers.
82  */
83 #if 0
84 /*
85  * No need to include handlers.h here the signatures must match
86  * and we don't need to force a rebuild of all the handlers everytime
87  * we add a new one to the list. --Bleep
88  */
89 #include "handlers.h"
90 #endif /* 0 */
91 #include "client.h"
92 #include "channel.h"
93 #include "hash.h"
94 #include "ircd.h"
95 #include "ircd_features.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
98 #include "msg.h"
99 #include "numeric.h"
100 #include "numnicks.h"
101 #include "send.h"
102
103 #include <assert.h>
104
105 /*
106  * ms_opmode - server message handler
107  */
108 int ms_opmode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
109 {
110   struct Channel *chptr = 0;
111   struct ModeBuf mbuf;
112
113   if (parc < 3)
114     return need_more_params(sptr, "OPMODE");
115
116   if (IsLocalChannel(parv[1]))
117     return 0;
118
119   if ('#' != *parv[1] || !(chptr = FindChannel(parv[1])))
120     return send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
121
122   modebuf_init(&mbuf, sptr, cptr, chptr,
123                (MODEBUF_DEST_CHANNEL | /* Send MODE to channel */
124                 MODEBUF_DEST_SERVER  | /* And to server */
125                 MODEBUF_DEST_OPMODE  | /* Use OPMODE */
126                 MODEBUF_DEST_HACK4   | /* Generate a HACK(4) notice */
127                 MODEBUF_DEST_LOG));    /* Log the mode changes to OPATH */
128
129   mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
130              (MODE_PARSE_SET    | /* Set the modes on the channel */
131               MODE_PARSE_STRICT | /* Be strict about it */
132               MODE_PARSE_FORCE)); /* And force them to be accepted */
133
134   modebuf_flush(&mbuf); /* flush the modes */
135
136   return 0;
137 }
138
139 /*
140  * mo_opmode - oper message handler
141  */
142 int mo_opmode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
143 {
144   struct Channel *chptr = 0;
145   struct ModeBuf mbuf;
146   struct Membership *member;
147
148   if (!feature_bool(FEAT_CONFIG_OPERCMDS))
149     return send_reply(sptr, ERR_DISABLED, "OPMODE");
150
151   if (parc < 3)
152     return need_more_params(sptr, "OPMODE");
153
154   clean_channelname(parv[1]);
155
156   if (!HasPriv(sptr,
157                IsLocalChannel(parv[1]) ? PRIV_LOCAL_OPMODE : PRIV_OPMODE))
158     return send_reply(sptr, ERR_NOPRIVILEGES);
159
160   if (('#' != *parv[1] && '&' != *parv[1]) || !(chptr = FindChannel(parv[1])))
161     return send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
162
163   if (!(member = find_member_link(chptr, sptr)))
164     return send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
165
166   modebuf_init(&mbuf, sptr, cptr, chptr,
167                (MODEBUF_DEST_CHANNEL | /* Send MODE to channel */
168                 MODEBUF_DEST_SERVER  | /* And to server */
169                 MODEBUF_DEST_OPMODE  | /* Use OPMODE */
170                 MODEBUF_DEST_HACK4   | /* Generate a HACK(4) notice */
171                 MODEBUF_DEST_LOG));    /* Log the mode changes to OPATH */
172
173   mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
174              (MODE_PARSE_SET |    /* set the modes on the channel */
175               MODE_PARSE_FORCE)); /* And force them to be accepted */
176
177   modebuf_flush(&mbuf); /* flush the modes */
178
179   return 0;
180 }
181