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