28755b03c40c7afa75e1e036f1ac253612a373b8
[ircu2.10.12-pk.git] / ircd / m_list.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_list.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 "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_alloc.h"
89 #include "ircd_chattr.h"
90 #include "ircd_features.h"
91 #include "ircd_log.h"
92 #include "ircd_reply.h"
93 #include "ircd_string.h"
94 #include "msg.h"
95 #include "numeric.h"
96 #include "numnicks.h"
97 #include "s_bsd.h"
98 #include "send.h"
99
100 /* #include <assert.h> -- Now using assert in ircd_log.h */
101 #include <stdlib.h>
102 #include <string.h>
103
104 #define LPARAM_ERROR    -1
105 #define LPARAM_SUCCESS   0
106 #define LPARAM_CHANNEL   1
107
108 static struct ListingArgs la_init = {
109   2147483647,                 /* max_time */
110   0,                          /* min_time */
111   4294967295U,                /* max_users */
112   0,                          /* min_users */
113   0,                          /* topic_limits */
114   2147483647,                 /* max_topic_time */
115   0,                          /* min_topic_time */
116   0                           /* chptr */
117 };
118
119 static struct ListingArgs la_default = {
120   2147483647,                 /* max_time */
121   0,                          /* min_time */
122   4294967295U,                /* max_users */
123   0,                          /* min_users */
124   0,                          /* topic_limits */
125   2147483647,                 /* max_topic_time */
126   0,                          /* min_topic_time */
127   0                           /* chptr */
128 };
129
130 static int
131 show_usage(struct Client *sptr)
132 {
133   if (!sptr) { /* configuration file error... */
134     log_write(LS_CONFIG, L_ERROR, 0, "Invalid default list parameter");
135     return LPARAM_ERROR;
136   }
137
138   send_reply(sptr, RPL_LISTUSAGE,
139              "Usage: \002/QUOTE LIST\002 \037parameters\037");
140   send_reply(sptr, RPL_LISTUSAGE,
141              "Where \037parameters\037 is a space or comma separated "
142              "list of one or more of:");
143   send_reply(sptr, RPL_LISTUSAGE,
144              " \002<\002\037max_users\037    ; Show all channels with less "
145              "than \037max_users\037.");
146   send_reply(sptr, RPL_LISTUSAGE,
147              " \002>\002\037min_users\037    ; Show all channels with more "
148              "than \037min_users\037.");
149   send_reply(sptr, RPL_LISTUSAGE,
150              " \002C<\002\037max_minutes\037 ; Channels that exist less "
151              "than \037max_minutes\037.");
152   send_reply(sptr, RPL_LISTUSAGE,
153              " \002C>\002\037min_minutes\037 ; Channels that exist more "
154              "than \037min_minutes\037.");
155   send_reply(sptr, RPL_LISTUSAGE,
156              " \002T<\002\037max_minutes\037 ; Channels with a topic last "
157              "set less than \037max_minutes\037 ago.");
158   send_reply(sptr, RPL_LISTUSAGE,
159              " \002T>\002\037min_minutes\037 ; Channels with a topic last "
160              "set more than \037min_minutes\037 ago.");
161   if (IsAnOper(sptr))
162     send_reply(sptr, RPL_LISTUSAGE,
163                " \002S\002             ; Show secret channels.");
164   send_reply(sptr, RPL_LISTUSAGE,
165              "Example: LIST <3,>1,C<10,T>0  ; 2 users, younger than 10 "
166              "min., topic set.");
167
168   return LPARAM_ERROR; /* return error condition */
169 }
170
171 static int
172 param_parse(struct Client *sptr, const char *param, struct ListingArgs *args,
173             int permit_chan)
174 {
175   int is_time = 0;
176   char dir;
177   unsigned int val;
178
179   assert(0 != args);
180
181   if (!param) /* NULL param == default--no list param */
182     return LPARAM_SUCCESS;
183
184   while (1) {
185     switch (*param) {
186     case 'T':
187     case 't':
188       is_time++;
189       args->flags |= LISTARG_TOPICLIMITS;
190       /*FALLTHROUGH*/
191
192     case 'C':
193     case 'c':
194       is_time++;
195       param++;
196       if (*param != '<' && *param != '>')
197         return show_usage(sptr);
198       /*FALLTHROUGH*/
199
200     case '<':
201     case '>':
202       dir = *(param++);
203
204       if (!IsDigit(*param)) /* must start with a digit */
205         return show_usage(sptr);
206
207       val = strtol(param, (char **)&param, 10); /* convert it... */
208
209       if (*param != ',' && *param != ' ' && *param != '\0') /* check syntax */
210         return show_usage(sptr);
211
212       if (is_time && val < 80000000) /* Toggle UTC/offset */
213         val = TStime() - val * 60;
214       
215       switch (is_time) {
216       case 0: /* number of users on channel */
217         if (dir == '<')
218           args->max_users = val;
219         else
220           args->min_users = val;
221         break;
222
223       case 1: /* channel topic */
224         if (dir == '<')
225           args->min_topic_time = val;
226         else
227           args->max_topic_time = val;
228         break;
229
230       case 2: /* channel creation time */
231         if (dir == '<')
232           args->min_time = val;
233         else
234           args->max_time = val;
235         break;
236       }
237       break;
238
239     case 'S':
240     case 's':
241       if (!IsAnOper(sptr) || !HasPriv(sptr, PRIV_LIST_CHAN))
242         return show_usage(sptr);
243
244       args->flags |= LISTARG_SHOWSECRET;
245       param++;
246
247       if (*param != ',' && *param != ' ' && *param != '\0') /* check syntax */
248         return show_usage(sptr);
249       break;
250
251     default: /* channel name? */
252       if (!permit_chan || !IsChannelName(param))
253         return show_usage(sptr);
254
255       return LPARAM_CHANNEL;
256       break;
257     }
258
259     if (!*param) /* hit end of string? */
260       break;
261
262     param++;
263   }
264
265   return LPARAM_SUCCESS;
266 }
267
268 void
269 list_set_default(void)
270 {
271   la_default = la_init; /* start off with a clean slate... */
272
273   if (param_parse(0, feature_str(FEAT_DEFAULT_LIST_PARAM), &la_default, 0) !=
274       LPARAM_SUCCESS)
275     la_default = la_init; /* recover from error by switching to default */
276 }
277
278 /*
279  * m_list - generic message handler
280  *
281  * parv[0] = sender prefix
282  * parv[1] = channel list or user/time limit
283  * parv[2...] = more user/time limits
284  */
285 int m_list(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
286 {
287   struct Channel *chptr;
288   char *name, *p = 0;
289   int show_channels = 0, param;
290   struct ListingArgs args;
291
292   if (cli_listing(sptr))            /* Already listing ? */
293   {
294     MyFree(cli_listing(sptr));
295     cli_listing(sptr) = 0;
296     send_reply(sptr, RPL_LISTEND);
297     update_write(sptr);
298     if (parc < 2 || 0 == ircd_strcmp("STOP", parv[1]))
299       return 0;                 /* Let LIST or LIST STOP abort a listing. */
300   }
301
302   if (parc < 2)                 /* No arguments given to /LIST ? */
303     args = la_default;
304   else {
305     args = la_init; /* initialize argument to blank slate */
306
307     for (param = 1; parv[param]; param++) { /* process each parameter */
308       switch (param_parse(sptr, parv[param], &args, parc == 2)) {
309       case LPARAM_ERROR: /* error encountered, usage already sent, return */
310         return 0;
311         break;
312
313       case LPARAM_CHANNEL: /* show channel instead */
314         show_channels++;
315         break;
316
317       case LPARAM_SUCCESS: /* parse succeeded */
318         break;
319       }
320     }
321   }
322
323   send_reply(sptr, RPL_LISTSTART);
324
325   if (!show_channels)
326   {
327     if (args.max_users > args.min_users + 1 && args.max_time > args.min_time &&
328         args.max_topic_time > args.min_topic_time)      /* Sanity check */
329     {
330       cli_listing(sptr) = (struct ListingArgs*) MyMalloc(sizeof(struct ListingArgs));
331       assert(0 != cli_listing(sptr));
332       memcpy(cli_listing(sptr), &args, sizeof(struct ListingArgs));
333       list_next_channels(sptr);
334       return 0;
335     }
336     send_reply(sptr, RPL_LISTEND);
337     return 0;
338   }
339
340   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
341   {
342     chptr = FindChannel(name);
343     if (!chptr)
344         continue;
345     if (ShowChannel(sptr, chptr)
346         || (IsAnOper(sptr) && HasPriv(sptr, PRIV_LIST_CHAN)))
347       send_reply(sptr, RPL_LIST, chptr->chname,
348                  chptr->users - number_of_zombies(chptr), chptr->topic);
349   }
350
351   send_reply(sptr, RPL_LISTEND);
352   return 0;
353 }