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