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