9e93a007514ffd89d93bc25225f947c206b2044f
[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_conf.h"
93 #include "s_debug.h"
94 #include "s_user.h"
95 #include "send.h"
96
97 #include <assert.h>
98 #include <stdlib.h>
99 #include <string.h>
100
101 #ifdef CONFIG_NEW_MODE
102 int
103 m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
104 {
105   struct Channel *chptr = 0;
106   struct ModeBuf mbuf;
107   struct Membership *member;
108
109   if (parc < 2)
110     return need_more_params(sptr, "MODE");
111
112   clean_channelname(parv[1]);
113
114   if (('#' != *parv[1] && '&' != *parv[1]) || !(chptr = FindChannel(parv[1])))
115     return set_user_mode(cptr, sptr, parc, parv);
116
117   sptr->flags &= ~FLAGS_TS8;
118
119   if (parc < 3) {
120     char modebuf[MODEBUFLEN];
121     char parabuf[MODEBUFLEN];
122
123     *modebuf = *parabuf = '\0';
124     modebuf[1] = '\0';
125     channel_modes(sptr, modebuf, parabuf, chptr);
126     sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
127                chptr->chname, modebuf, parabuf);
128     sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
129                chptr->chname, chptr->creationtime);
130     return 0;
131   }
132
133   if (!(member = find_member_link(chptr, sptr)) || !IsChanOp(member)) {
134 #ifdef OPER_MODE_LCHAN
135     if (IsOperOnLocalChannel(sptr, chptr->chname)) {
136       modebuf_init(&mbuf, sptr, cptr, chptr,
137                    (MODEBUF_DEST_CHANNEL | /* Send mode to channel */
138                     MODEBUF_DEST_HACK4));  /* Send HACK(4) notice */
139       mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
140                  (MODE_PARSE_SET |    /* Set the mode */
141                   MODE_PARSE_FORCE)); /* Force it to take */
142       return modebuf_flush(&mbuf);
143     } else
144 #endif
145       mode_parse(0, cptr, sptr, chptr, parc - 2, parv + 2,
146                  (member ? MODE_PARSE_NOTOPER : MODE_PARSE_NOTMEMBER));
147     return 0;
148   }
149
150   modebuf_init(&mbuf, sptr, cptr, chptr,
151                (MODEBUF_DEST_CHANNEL | /* Send mode to channel */
152                 MODEBUF_DEST_SERVER)); /* Send mode to servers */
153   mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2, MODE_PARSE_SET);
154   return modebuf_flush(&mbuf);
155 }
156
157 int
158 ms_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
159 {
160   struct Channel *chptr = 0;
161   struct ModeBuf mbuf;
162   struct Membership *member;
163
164   if (parc < 3)
165     return need_more_params(sptr, "MODE");
166
167   if (IsLocalChannel(parv[1]))
168     return 0;
169
170   if ('#' != *parv[1] || !(chptr = FindChannel(parv[1])))
171     return set_user_mode(cptr, sptr, parc, parv);
172
173   sptr->flags &= ~FLAGS_TS8;
174
175   if (IsServer(sptr)) {
176     if (find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD))
177       modebuf_init(&mbuf, sptr, cptr, chptr,
178                    (MODEBUF_DEST_CHANNEL | /* Send mode to clients */
179                     MODEBUF_DEST_SERVER  | /* Send mode to servers */
180                     MODEBUF_DEST_HACK4));  /* Send a HACK(4) message */
181     else
182       modebuf_init(&mbuf, sptr, cptr, chptr,
183                    (MODEBUF_DEST_CHANNEL | /* Send mode to clients */
184                     MODEBUF_DEST_SERVER  | /* Send mode to servers */
185                     MODEBUF_DEST_HACK3));  /* Send a HACK(3) message */
186
187     mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
188                (MODE_PARSE_SET    | /* Set the mode */
189                 MODE_PARSE_STRICT | /* Interpret it strictly */
190                 MODE_PARSE_FORCE)); /* And force it to be accepted */
191   } else {
192     if (!(member = find_member_link(chptr, sptr)) || !IsChanOp(member)) {
193       modebuf_init(&mbuf, sptr, cptr, chptr,
194                    (MODEBUF_DEST_SERVER |  /* Send mode to server */
195                     MODEBUF_DEST_HACK2  |  /* Send a HACK(2) message */
196                     MODEBUF_DEST_DEOP   |  /* Deop the source */
197                     MODEBUF_DEST_BOUNCE)); /* And bounce the MODE */
198       mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
199                  (MODE_PARSE_STRICT |  /* Interpret it strictly */
200                   MODE_PARSE_BOUNCE)); /* And bounce the MODE */
201     } else {
202       modebuf_init(&mbuf, sptr, cptr, chptr,
203                    (MODEBUF_DEST_CHANNEL | /* Send mode to clients */
204                     MODEBUF_DEST_SERVER)); /* Send mode to servers */
205       mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
206                  (MODE_PARSE_SET    | /* Set the mode */
207                   MODE_PARSE_STRICT | /* Interpret it strictly */
208                   MODE_PARSE_FORCE)); /* And force it to be accepted */
209     }
210   }
211
212   return modebuf_flush(&mbuf);
213 }
214 #else /* CONFIG_NEW_MODE */
215 /*
216  * m_mode - generic message handler
217  */
218 int m_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
219 {
220   int             badop;
221   int             sendts;
222   struct Channel* chptr = 0;
223   char modebuf[MODEBUFLEN];
224   char parabuf[MODEBUFLEN];
225   char nparabuf[MODEBUFLEN];
226
227
228   if (parc < 2)
229     return need_more_params(sptr, "MODE");
230
231   /*
232    * if local user, cleanup channel name, don't allow local channel operations
233    * for remote clients
234    */
235   if (MyUser(sptr))
236     clean_channelname(parv[1]);
237   else if (IsLocalChannel(parv[1]))
238     return 0;
239
240   /* 
241    * try to find the channel
242    */
243   if ('#' == *parv[1] || '&' == *parv[1])
244     chptr = FindChannel(parv[1]);
245   if (!chptr)
246     return set_user_mode(cptr, sptr, parc, parv);
247
248   sptr->flags &= ~FLAGS_TS8;
249   /*
250    * sending an error wasnt good, lets just send an empty mode reply..  poptix
251    */
252   if (IsModelessChannel(chptr->chname)) {
253     if (IsUser(sptr))
254       sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
255                  chptr->chname, "+nt", "");
256     return 0;
257   }
258
259   if (parc < 3) {
260     /*
261      * no parameters, send channel modes
262      */
263     *modebuf = *parabuf = '\0';
264     modebuf[1] = '\0';
265     channel_modes(sptr, modebuf, parabuf, chptr);
266     sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
267                chptr->chname, modebuf, parabuf);
268     sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
269                chptr->chname, chptr->creationtime);
270     return 0;
271   }
272
273   LocalChanOperMode = 0;
274
275   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
276                           modebuf, parabuf, nparabuf, &badop))) {
277     sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
278         ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
279     return 0;
280   }
281
282   if (badop >= 2)
283     send_hack_notice(cptr, sptr, parc, parv, badop, 1);
284
285   if (strlen(modebuf) > 1 || sendts > 0) {
286     if (badop != 2 && strlen(modebuf) > 1) {
287 #ifdef OPER_MODE_LCHAN
288       if (LocalChanOperMode) {
289         sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
290                                me.name, chptr->chname, modebuf, parabuf);
291         sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
292                        sptr->name, chptr->chname, modebuf, parabuf);
293       }
294       else
295 #endif
296       sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
297           parv[0], chptr->chname, modebuf, parabuf);
298     }
299     if (IsLocalChannel(chptr->chname))
300       return 0;
301     /* We send a creationtime of 0, to mark it as a hack --Run */
302     if (IsServer(sptr) && (badop == 2 || sendts > 0)) {
303       if (*modebuf == '\0')
304         strcpy(modebuf, "+");
305       if (badop != 2) {
306         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
307             NumServ(sptr), chptr->chname, modebuf, nparabuf,
308             (badop == 4) ? (time_t) 0 : chptr->creationtime);
309       }
310     }
311     else {
312       if (IsServer(sptr))
313          sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
314            NumServ(sptr), chptr->chname, modebuf, nparabuf);
315       else
316          sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
317            NumNick(sptr), chptr->chname, modebuf, nparabuf);
318     }
319   }
320   return 0;
321 }
322
323 /*
324  * ms_mode - server message handler
325  */
326 int ms_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
327 {
328   int             badop;
329   int             sendts;
330   struct Channel* chptr = 0;
331   char modebuf[MODEBUFLEN];
332   char parabuf[MODEBUFLEN];
333   char nparabuf[MODEBUFLEN];
334
335   if (parc < 2)
336     return need_more_params(sptr, "MODE");
337
338   /*
339    * if local user, cleanup channel name, don't allow local channel operations
340    * for remote clients
341    */
342   if (MyUser(sptr))
343     clean_channelname(parv[1]);
344   else if (IsLocalChannel(parv[1]))
345     return 0;
346
347   /* 
348    * try to find the channel
349    */
350   if ('#' == *parv[1] || '&' == *parv[1])
351     chptr = FindChannel(parv[1]);
352   if (!chptr)
353     return set_user_mode(cptr, sptr, parc, parv);
354
355   sptr->flags &= ~FLAGS_TS8;
356   /*
357    * sending an error wasnt good, lets just send an empty mode reply..  poptix
358    */
359   if (IsModelessChannel(chptr->chname)) {
360     if (IsUser(sptr))
361       sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
362                  chptr->chname, "+nt", "");
363     return 0;
364   }
365
366   if (parc < 3) {
367     /*
368      * no parameters, send channel modes
369      */
370     *modebuf = *parabuf = '\0';
371     modebuf[1] = '\0';
372     channel_modes(sptr, modebuf, parabuf, chptr);
373     sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
374                chptr->chname, modebuf, parabuf);
375     sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
376                chptr->chname, chptr->creationtime);
377     return 0;
378   }
379
380   LocalChanOperMode = 0;
381
382   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
383                           modebuf, parabuf, nparabuf, &badop))) {
384     sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
385         ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
386     return 0;
387   }
388
389   if (badop >= 2)
390     send_hack_notice(cptr, sptr, parc, parv, badop, 1);
391
392   if (strlen(modebuf) > 1 || sendts > 0) {
393     if (badop != 2 && strlen(modebuf) > 1) {
394 #ifdef OPER_MODE_LCHAN
395       if (LocalChanOperMode) {
396         sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
397                                me.name, chptr->chname, modebuf, parabuf);
398         sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
399                        me.name, chptr->chname, modebuf, parabuf);
400       }
401       else
402 #endif
403       sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
404           parv[0], chptr->chname, modebuf, parabuf);
405     }
406     if (IsLocalChannel(chptr->chname))
407       return 0;
408     /* We send a creationtime of 0, to mark it as a hack --Run */
409     if (IsServer(sptr) && (badop == 2 || sendts > 0)) {
410       if (*modebuf == '\0')
411         strcpy(modebuf, "+");
412       if (badop != 2) {
413         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
414             NumServ(sptr), chptr->chname, modebuf, nparabuf,
415             (badop == 4) ? (time_t) 0 : chptr->creationtime);
416       }
417     }
418     else {
419       if (IsServer(sptr))
420          sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
421            NumServ(sptr), chptr->chname, modebuf, nparabuf);
422       else
423          sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
424            NumNick(sptr), chptr->chname, modebuf, nparabuf);
425     }
426   }
427   return 0;
428 }
429 #endif /* CONFIG_NEW_MODE */
430
431 #if 0
432 /*
433  * m_mode
434  * parv[0] - sender
435  * parv[1] - channel
436  */
437 int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
438 {
439   int             badop;
440   int             sendts;
441   struct Channel* chptr = 0;
442
443   if (parc < 2)
444     return need_more_params(sptr, "MODE");
445
446   /*
447    * if local user, cleanup channel name, don't allow local channel operations
448    * for remote clients
449    */
450   if (MyUser(sptr))
451     clean_channelname(parv[1]);
452   else if (IsLocalChannel(parv[1]))
453     return 0;
454
455   /* 
456    * try to find the channel
457    */
458   if ('#' == *parv[1] || '&' == *parv[1])
459     chptr = FindChannel(parv[1]);
460   if (!chptr)
461     return set_user_mode(cptr, sptr, parc, parv);
462
463   sptr->flags &= ~FLAGS_TS8;
464   /*
465    * sending an error wasnt good, lets just send an empty mode reply..  poptix
466    */
467   if (IsModelessChannel(chptr->chname)) {
468     if (IsUser(sptr))
469       sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
470                  chptr->chname, "+nt", "");
471     return 0;
472   }
473
474   if (parc < 3) {
475     /*
476      * no parameters, send channel modes
477      */
478     *modebuf = *parabuf = '\0';
479     modebuf[1] = '\0';
480     channel_modes(sptr, modebuf, parabuf, chptr);
481     sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
482                chptr->chname, modebuf, parabuf);
483     sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
484                chptr->chname, chptr->creationtime);
485     return 0;
486   }
487
488   LocalChanOperMode = 0;
489
490   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
491                           modebuf, parabuf, nparabuf, &badop))) {
492     sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
493         ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
494     return 0;
495   }
496
497   if (badop >= 2)
498     send_hack_notice(cptr, sptr, parc, parv, badop, 1);
499
500   if (strlen(modebuf) > 1 || sendts > 0) {
501     if (badop != 2 && strlen(modebuf) > 1) {
502 #ifdef OPER_MODE_LCHAN
503       if (LocalChanOperMode) {
504         sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
505                                me.name, chptr->chname, modebuf, parabuf);
506         sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
507                        me.name, chptr->chname, modebuf, parabuf);
508       }
509       else
510 #endif
511       sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
512           parv[0], chptr->chname, modebuf, parabuf);
513     }
514     if (IsLocalChannel(chptr->chname))
515       return 0;
516     /* We send a creationtime of 0, to mark it as a hack --Run */
517     if (IsServer(sptr) && (badop == 2 || sendts > 0)) {
518       if (*modebuf == '\0')
519         strcpy(modebuf, "+");
520       if (badop != 2) {
521         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
522             NumServ(sptr), chptr->chname, modebuf, nparabuf,
523             (badop == 4) ? (time_t) 0 : chptr->creationtime);
524       }
525     }
526     else {
527       if (IsServer(sptr))
528          sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
529            NumServ(sptr), chptr->chname, modebuf, nparabuf);
530       else
531          sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
532            NumNick(sptr), chptr->chname, modebuf, nparabuf);
533     }
534   }
535   return 0;
536 }
537
538 #endif /* 0 */