Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / m_mode.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_mode.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
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "handlers.h"
83 #include "channel.h"
84 #include "client.h"
85 #include "hash.h"
86 #include "ircd.h"
87 #include "ircd_reply.h"
88 #include "ircd_string.h"
89 #include "msg.h"
90 #include "numeric.h"
91 #include "numnicks.h"
92 #include "s_debug.h"
93 #include "s_user.h"
94 #include "send.h"
95
96 #include <assert.h>
97 #include <stdlib.h>
98 #include <string.h>
99
100 /*
101  * m_mode - generic message handler
102  */
103 int m_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
104 {
105   int             badop;
106   int             sendts;
107   struct Channel* chptr = 0;
108   char modebuf[MODEBUFLEN];
109   char parabuf[MODEBUFLEN];
110   char nparabuf[MODEBUFLEN];
111
112
113   if (parc < 2)
114     return need_more_params(sptr, "MODE");
115
116   /*
117    * if local user, cleanup channel name, don't allow local channel operations
118    * for remote clients
119    */
120   if (MyUser(sptr))
121     clean_channelname(parv[1]);
122   else if (IsLocalChannel(parv[1]))
123     return 0;
124
125   /* 
126    * try to find the channel
127    */
128   if ('#' == *parv[1] || '&' == *parv[1])
129     chptr = FindChannel(parv[1]);
130   if (!chptr)
131     return set_user_mode(cptr, sptr, parc, parv);
132
133   sptr->flags &= ~FLAGS_TS8;
134   /*
135    * sending an error wasnt good, lets just send an empty mode reply..  poptix
136    */
137   if (IsModelessChannel(chptr->chname)) {
138     if (IsUser(sptr))
139       sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
140                  chptr->chname, "+nt", "");
141     return 0;
142   }
143
144   if (parc < 3) {
145     /*
146      * no parameters, send channel modes
147      */
148     *modebuf = *parabuf = '\0';
149     modebuf[1] = '\0';
150     channel_modes(sptr, modebuf, parabuf, chptr);
151     sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
152                chptr->chname, modebuf, parabuf);
153     sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
154                chptr->chname, chptr->creationtime);
155     return 0;
156   }
157
158   LocalChanOperMode = 0;
159
160   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
161                           modebuf, parabuf, nparabuf, &badop))) {
162     sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
163         ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
164     return 0;
165   }
166
167   if (badop >= 2)
168     send_hack_notice(cptr, sptr, parc, parv, badop, 1);
169
170   if (strlen(modebuf) > 1 || sendts > 0) {
171     if (badop != 2 && strlen(modebuf) > 1) {
172 #ifdef OPER_MODE_LCHAN
173       if (LocalChanOperMode) {
174         sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
175                                me.name, chptr->chname, modebuf, parabuf);
176         sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
177                        sptr->name, chptr->chname, modebuf, parabuf);
178       }
179       else
180 #endif
181       sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
182           parv[0], chptr->chname, modebuf, parabuf);
183     }
184     if (IsLocalChannel(chptr->chname))
185       return 0;
186     /* We send a creationtime of 0, to mark it as a hack --Run */
187     if (IsServer(sptr) && (badop == 2 || sendts > 0)) {
188       if (*modebuf == '\0')
189         strcpy(modebuf, "+");
190       if (badop != 2) {
191         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
192             NumServ(sptr), chptr->chname, modebuf, nparabuf,
193             (badop == 4) ? (time_t) 0 : chptr->creationtime);
194       }
195     }
196     else {
197       if (IsServer(sptr))
198          sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
199            NumServ(sptr), chptr->chname, modebuf, nparabuf);
200       else
201          sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
202            NumNick(sptr), chptr->chname, modebuf, nparabuf);
203     }
204   }
205   return 0;
206 }
207
208 /*
209  * ms_mode - server message handler
210  */
211 int ms_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
212 {
213   int             badop;
214   int             sendts;
215   struct Channel* chptr = 0;
216   char modebuf[MODEBUFLEN];
217   char parabuf[MODEBUFLEN];
218   char nparabuf[MODEBUFLEN];
219
220   if (parc < 2)
221     return need_more_params(sptr, "MODE");
222
223   /*
224    * if local user, cleanup channel name, don't allow local channel operations
225    * for remote clients
226    */
227   if (MyUser(sptr))
228     clean_channelname(parv[1]);
229   else if (IsLocalChannel(parv[1]))
230     return 0;
231
232   /* 
233    * try to find the channel
234    */
235   if ('#' == *parv[1] || '&' == *parv[1])
236     chptr = FindChannel(parv[1]);
237   if (!chptr)
238     return set_user_mode(cptr, sptr, parc, parv);
239
240   sptr->flags &= ~FLAGS_TS8;
241   /*
242    * sending an error wasnt good, lets just send an empty mode reply..  poptix
243    */
244   if (IsModelessChannel(chptr->chname)) {
245     if (IsUser(sptr))
246       sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
247                  chptr->chname, "+nt", "");
248     return 0;
249   }
250
251   if (parc < 3) {
252     /*
253      * no parameters, send channel modes
254      */
255     *modebuf = *parabuf = '\0';
256     modebuf[1] = '\0';
257     channel_modes(sptr, modebuf, parabuf, chptr);
258     sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
259                chptr->chname, modebuf, parabuf);
260     sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
261                chptr->chname, chptr->creationtime);
262     return 0;
263   }
264
265   LocalChanOperMode = 0;
266
267   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
268                           modebuf, parabuf, nparabuf, &badop))) {
269     sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
270         ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
271     return 0;
272   }
273
274   if (badop >= 2)
275     send_hack_notice(cptr, sptr, parc, parv, badop, 1);
276
277   if (strlen(modebuf) > 1 || sendts > 0) {
278     if (badop != 2 && strlen(modebuf) > 1) {
279 #ifdef OPER_MODE_LCHAN
280       if (LocalChanOperMode) {
281         sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
282                                me.name, chptr->chname, modebuf, parabuf);
283         sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
284                        me.name, chptr->chname, modebuf, parabuf);
285       }
286       else
287 #endif
288       sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
289           parv[0], chptr->chname, modebuf, parabuf);
290     }
291     if (IsLocalChannel(chptr->chname))
292       return 0;
293     /* We send a creationtime of 0, to mark it as a hack --Run */
294     if (IsServer(sptr) && (badop == 2 || sendts > 0)) {
295       if (*modebuf == '\0')
296         strcpy(modebuf, "+");
297       if (badop != 2) {
298         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
299             NumServ(sptr), chptr->chname, modebuf, nparabuf,
300             (badop == 4) ? (time_t) 0 : chptr->creationtime);
301       }
302     }
303     else {
304       if (IsServer(sptr))
305          sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
306            NumServ(sptr), chptr->chname, modebuf, nparabuf);
307       else
308          sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
309            NumNick(sptr), chptr->chname, modebuf, nparabuf);
310     }
311   }
312   return 0;
313 }
314
315 #if 0
316 /*
317  * m_mode
318  * parv[0] - sender
319  * parv[1] - channel
320  */
321 int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
322 {
323   int             badop;
324   int             sendts;
325   struct Channel* chptr = 0;
326
327   if (parc < 2)
328     return need_more_params(sptr, "MODE");
329
330   /*
331    * if local user, cleanup channel name, don't allow local channel operations
332    * for remote clients
333    */
334   if (MyUser(sptr))
335     clean_channelname(parv[1]);
336   else if (IsLocalChannel(parv[1]))
337     return 0;
338
339   /* 
340    * try to find the channel
341    */
342   if ('#' == *parv[1] || '&' == *parv[1])
343     chptr = FindChannel(parv[1]);
344   if (!chptr)
345     return set_user_mode(cptr, sptr, parc, parv);
346
347   sptr->flags &= ~FLAGS_TS8;
348   /*
349    * sending an error wasnt good, lets just send an empty mode reply..  poptix
350    */
351   if (IsModelessChannel(chptr->chname)) {
352     if (IsUser(sptr))
353       sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
354                  chptr->chname, "+nt", "");
355     return 0;
356   }
357
358   if (parc < 3) {
359     /*
360      * no parameters, send channel modes
361      */
362     *modebuf = *parabuf = '\0';
363     modebuf[1] = '\0';
364     channel_modes(sptr, modebuf, parabuf, chptr);
365     sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
366                chptr->chname, modebuf, parabuf);
367     sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
368                chptr->chname, chptr->creationtime);
369     return 0;
370   }
371
372   LocalChanOperMode = 0;
373
374   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
375                           modebuf, parabuf, nparabuf, &badop))) {
376     sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
377         ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
378     return 0;
379   }
380
381   if (badop >= 2)
382     send_hack_notice(cptr, sptr, parc, parv, badop, 1);
383
384   if (strlen(modebuf) > 1 || sendts > 0) {
385     if (badop != 2 && strlen(modebuf) > 1) {
386 #ifdef OPER_MODE_LCHAN
387       if (LocalChanOperMode) {
388         sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
389                                me.name, chptr->chname, modebuf, parabuf);
390         sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
391                        me.name, chptr->chname, modebuf, parabuf);
392       }
393       else
394 #endif
395       sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
396           parv[0], chptr->chname, modebuf, parabuf);
397     }
398     if (IsLocalChannel(chptr->chname))
399       return 0;
400     /* We send a creationtime of 0, to mark it as a hack --Run */
401     if (IsServer(sptr) && (badop == 2 || sendts > 0)) {
402       if (*modebuf == '\0')
403         strcpy(modebuf, "+");
404       if (badop != 2) {
405         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
406             NumServ(sptr), chptr->chname, modebuf, nparabuf,
407             (badop == 4) ? (time_t) 0 : chptr->creationtime);
408       }
409     }
410     else {
411       if (IsServer(sptr))
412          sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
413            NumServ(sptr), chptr->chname, modebuf, nparabuf);
414       else
415          sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
416            NumNick(sptr), chptr->chname, modebuf, nparabuf);
417     }
418   }
419   return 0;
420 }
421
422 #endif /* 0 */