fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[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: m_list.c 1841 2007-11-05 03:01:34Z entrope $
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,                          /* flags */
114   2147483647,                 /* max_topic_time */
115   0,                          /* min_topic_time */
116   0,                          /* bucket */
117   {0}                         /* wildcard */
118 };
119
120 static struct ListingArgs la_default = {
121   2147483647,                 /* max_time */
122   0,                          /* min_time */
123   4294967295U,                /* max_users */
124   0,                          /* min_users */
125   0,                          /* flags */
126   2147483647,                 /* max_topic_time */
127   0,                          /* min_topic_time */
128   0,                          /* bucket */
129   {0}                         /* wildcard */
130 };
131
132 static int
133 show_usage(struct Client *sptr)
134 {
135   if (!sptr) { /* configuration file error... */
136     log_write(LS_CONFIG, L_ERROR, 0, "Invalid default list parameter");
137     return LPARAM_ERROR;
138   }
139
140   send_reply(sptr, RPL_LISTUSAGE,
141              "Usage: \002/QUOTE LIST\002 \037parameters\037");
142   send_reply(sptr, RPL_LISTUSAGE,
143              "Where \037parameters\037 is a space or comma separated "
144              "list of one or more of:");
145   send_reply(sptr, RPL_LISTUSAGE,
146              " \002<\002\037max_users\037    ; Show all channels with less "
147              "than \037max_users\037.");
148   send_reply(sptr, RPL_LISTUSAGE,
149              " \002>\002\037min_users\037    ; Show all channels with more "
150              "than \037min_users\037.");
151   send_reply(sptr, RPL_LISTUSAGE,
152              " \002C<\002\037max_minutes\037 ; Channels that exist less "
153              "than \037max_minutes\037.");
154   send_reply(sptr, RPL_LISTUSAGE,
155              " \002C>\002\037min_minutes\037 ; Channels that exist more "
156              "than \037min_minutes\037.");
157   send_reply(sptr, RPL_LISTUSAGE,
158              " \002T<\002\037max_minutes\037 ; Channels with a topic last "
159              "set less than \037max_minutes\037 ago.");
160   send_reply(sptr, RPL_LISTUSAGE,
161              " \002T>\002\037min_minutes\037 ; Channels with a topic last "
162              "set more than \037min_minutes\037 ago.");
163   send_reply(sptr, RPL_LISTUSAGE,
164              " \037pattern\037       ; Channels with names matching "
165              "\037pattern\037. ");
166   send_reply(sptr, RPL_LISTUSAGE,
167              " !\037pattern\037      ; Channels with names not "
168              "matching \037pattern\037. ");
169   send_reply(sptr, RPL_LISTUSAGE, "Note: Patterns may contain * and ?. "
170              "You may only give one pattern match constraint.");
171   if (IsAnOper(sptr)) {
172     send_reply(sptr, RPL_LISTUSAGE,
173                " \002S\002             ; Show secret channels.");
174     send_reply(sptr, RPL_LISTUSAGE,
175                " \002M\002             ; Show channel modes.");
176   }
177   send_reply(sptr, RPL_LISTUSAGE,
178              "Example: LIST <3,>1,C<10,T>0,#a*  ; 2 users, younger than 10 "
179              "min., topic set., starts with #a");
180
181   return LPARAM_ERROR; /* return error condition */
182 }
183
184 static int
185 param_parse(struct Client *sptr, const char *param, struct ListingArgs *args,
186             int permit_chan)
187 {
188   int is_time = 0;
189   char dir;
190   unsigned int val;
191   char *tmp1, *tmp2;
192
193   assert(0 != args);
194
195   if (!param) /* NULL param == default--no list param */
196     return LPARAM_SUCCESS;
197
198   while (1) {
199     switch (*param) {
200     case 'T':
201     case 't':
202       is_time++;
203       args->flags |= LISTARG_TOPICLIMITS;
204       /*FALLTHROUGH*/
205
206     case 'C':
207     case 'c':
208       is_time++;
209       param++;
210       if (*param != '<' && *param != '>')
211         return show_usage(sptr);
212       /*FALLTHROUGH*/
213
214     case '<':
215     case '>':
216       dir = *(param++);
217
218       if (!IsDigit(*param)) /* must start with a digit */
219         return show_usage(sptr);
220
221       val = strtol(param, (char **)&param, 10); /* convert it... */
222
223       if (*param != ',' && *param != ' ' && *param != '\0') /* check syntax */
224         return show_usage(sptr);
225
226       if (is_time && val < 80000000) {
227         /* Convert age to timestamp and reverse direction */
228         val = TStime() - val * 60;
229         dir = (dir == '>') ? '<' : '>';
230       }
231       
232       switch (is_time) {
233       case 0: /* number of users on channel */
234         if (dir == '<')
235           args->max_users = val;
236         else
237           args->min_users = val;
238         break;
239
240       case 1: /* channel creation time */
241         if (dir == '<')
242           args->max_time = val;
243         else
244           args->min_time = val;
245         break;
246
247       case 2: /* channel topic */
248         if (dir == '<')
249           args->max_topic_time = val;
250         else
251           args->min_topic_time = val;
252         break;
253       }
254       break;
255
256     case 'S':
257     case 's':
258       if (!IsAnOper(sptr) || !HasPriv(sptr, PRIV_LIST_CHAN))
259         return show_usage(sptr);
260
261       args->flags |= LISTARG_SHOWSECRET;
262       param++;
263
264       if (*param != ',' && *param != ' ' && *param != '\0') /* check syntax */
265         return show_usage(sptr);
266       break;
267
268     case 'M':
269     case 'm':
270       if (!IsAnOper(sptr) || !HasPriv(sptr, PRIV_LIST_CHAN))
271         return show_usage(sptr);
272
273       args->flags |= LISTARG_SHOWMODES;
274       param++;
275
276       if (*param != ',' && *param != ' ' && *param != '\0') /* check syntax */
277         return show_usage(sptr);
278       break;
279
280     default:
281       /* It might be a wildcard... */
282       if (strchr(param, '*') ||
283           strchr(param, '?'))
284       {
285         if (param[0] == '!')
286         {
287           param++;
288           args->flags |= LISTARG_NEGATEWILDCARD;
289         }
290
291         /* Only one wildcard allowed... */
292         if (args->wildcard[0] != 0)
293           return show_usage(sptr);
294
295         /* If its not going to match anything, don't bother. */
296         if (param[0] != '*' &&
297             param[0] != '?' &&
298             param[0] != '#' &&
299             param[0] != '&')
300           return show_usage(sptr);
301
302         tmp1 = strchr(param, ',');
303         tmp2 = strchr(param, ' ');
304         if (tmp2 && (!tmp1 || (tmp2 < tmp1)))
305           tmp1 = tmp2;
306         
307         if (tmp1)
308           *tmp1++ = 0;
309
310         ircd_strncpy(args->wildcard, param, CHANNELLEN-1);
311         args->wildcard[CHANNELLEN-1] = 0;
312
313         if (tmp1 == NULL)
314           return LPARAM_SUCCESS;
315
316         param = tmp1;
317         continue;
318       }
319
320       /* channel name? */
321       if (!permit_chan || !IsChannelName(param))
322         return show_usage(sptr);
323
324       return LPARAM_CHANNEL;
325     }
326
327     if (!*param) /* hit end of string? */
328       break;
329
330     param++;
331   }
332
333   return LPARAM_SUCCESS;
334 }
335
336 void
337 list_set_default(void)
338 {
339   la_default = la_init; /* start off with a clean slate... */
340
341   if (param_parse(0, feature_str(FEAT_DEFAULT_LIST_PARAM), &la_default, 0) !=
342       LPARAM_SUCCESS)
343     la_default = la_init; /* recover from error by switching to default */
344 }
345
346 /*
347  * m_list - generic message handler
348  *
349  * parv[0] = sender prefix
350  * parv[1] = channel list or user/time limit
351  * parv[2...] = more user/time limits
352  */
353 int m_list(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
354 {
355   struct Channel *chptr;
356   char *name, *p = 0;
357   int show_channels = 0, param;
358   struct ListingArgs args;
359
360   if (cli_listing(sptr))            /* Already listing ? */
361   {
362     if (cli_listing(sptr))
363     MyFree(cli_listing(sptr));
364     cli_listing(sptr) = 0;
365     send_reply(sptr, RPL_LISTEND);
366     update_write(sptr);
367     if (parc < 2 || 0 == ircd_strcmp("STOP", parv[1]))
368       return 0;                 /* Let LIST or LIST STOP abort a listing. */
369   }
370
371   if (parc < 2)                 /* No arguments given to /LIST ? */
372     args = la_default;
373   else {
374     args = la_init; /* initialize argument to blank slate */
375
376     for (param = 1; parv[param]; param++) { /* process each parameter */
377       switch (param_parse(sptr, parv[param], &args, parc == 2)) {
378       case LPARAM_ERROR: /* error encountered, usage already sent, return */
379         return 0;
380
381       case LPARAM_CHANNEL: /* show channel instead */
382         show_channels++;
383         break;
384
385       case LPARAM_SUCCESS: /* parse succeeded */
386         break;
387       }
388     }
389   }
390
391   send_reply(sptr, RPL_LISTSTART);
392
393   if (!show_channels)
394   {
395     if (args.max_users > args.min_users + 1 && args.max_time > args.min_time &&
396         args.max_topic_time > args.min_topic_time)      /* Sanity check */
397     {
398       cli_listing(sptr) = (struct ListingArgs*) MyMalloc(sizeof(struct ListingArgs));
399       assert(0 != cli_listing(sptr));
400       memcpy(cli_listing(sptr), &args, sizeof(struct ListingArgs));
401       list_next_channels(sptr);
402       return 0;
403     }
404     send_reply(sptr, RPL_LISTEND);
405     return 0;
406   }
407
408   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
409   {
410     chptr = FindChannel(name);
411     if (!chptr)
412         continue;
413     if (ShowChannel(sptr, chptr)
414         || (IsAnOper(sptr) && HasPriv(sptr, PRIV_LIST_CHAN)))
415       send_reply(sptr, RPL_LIST, chptr->chname,
416                  chptr->users - number_of_zombies(chptr), chptr->topic);
417   }
418
419   send_reply(sptr, RPL_LISTEND);
420   return 0;
421 }