Anope IRC Services  Version 1.8
cs_list.c
Go to the documentation of this file.
1 /* ChanServ core functions
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 /*************************************************************************/
14 
15 #include "module.h"
16 
17 static int do_list(User * u);
18 static void myChanServHelp(User * u);
19 
26 int AnopeInit(int argc, char **argv)
27 {
28  Command *c;
29 
30  moduleAddAuthor("Anope");
31  moduleAddVersion(VERSION_STRING);
33 
34  c = createCommand("LIST", do_list, NULL, -1, CHAN_HELP_LIST,
35  CHAN_SERVADMIN_HELP_LIST,
36  CHAN_SERVADMIN_HELP_LIST, CHAN_SERVADMIN_HELP_LIST);
37 
39 
41 
42  return MOD_CONT;
43 }
44 
48 void AnopeFini(void)
49 {
50 
51 }
52 
53 
54 
59 static void myChanServHelp(User * u)
60 {
61  if (!CSListOpersOnly || (is_oper(u))) {
62  notice_lang(s_ChanServ, u, CHAN_HELP_CMD_LIST);
63  }
64 }
65 
71 static int do_list(User * u)
72 {
73  char *pattern = strtok(NULL, " ");
74  int spattern_size;
75  char *spattern;
76  ChannelInfo *ci;
77  int nchans, i;
78  char buf[BUFSIZE];
79  int is_servadmin = is_services_admin(u);
80  int count = 0, from = 0, to = 0, tofree = 0;
81  char *tmp = NULL;
82  char *s = NULL;
83  char *keyword;
84  int32 matchflags = 0;
85 
86  if (!(!CSListOpersOnly || (is_oper(u)))) {
87  notice_lang(s_ChanServ, u, ACCESS_DENIED);
88  return MOD_STOP;
89  }
90 
91  if (!pattern) {
92  syntax_error(s_ChanServ, u, "LIST",
93  is_servadmin ? CHAN_LIST_SERVADMIN_SYNTAX :
94  CHAN_LIST_SYNTAX);
95  } else {
96 
97  if (pattern) {
98  if (pattern[0] == '#') {
99  tmp = myStrGetOnlyToken((pattern + 1), '-', 0); /* Read FROM out */
100  if (!tmp) {
101  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
102  notice_lang(s_ChanServ, u, CS_LIST_INCORRECT_RANGE);
103  return MOD_CONT;
104  }
105  for (s = tmp; *s; s++) {
106  if (!isdigit(*s)) {
107  free(tmp);
108  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
109  notice_lang(s_ChanServ, u, CS_LIST_INCORRECT_RANGE);
110  return MOD_CONT;
111  }
112  }
113  from = atoi(tmp);
114  free(tmp);
115  tmp = myStrGetTokenRemainder(pattern, '-', 1); /* Read TO out */
116  if (!tmp) {
117  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
118  notice_lang(s_ChanServ, u, CS_LIST_INCORRECT_RANGE);
119  return MOD_CONT;
120  }
121  for (s = tmp; *s; s++) {
122  if (!isdigit(*s)) {
123  free(tmp);
124  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
125  notice_lang(s_ChanServ, u, CS_LIST_INCORRECT_RANGE);
126  return MOD_CONT;
127  }
128  }
129  to = atoi(tmp);
130  free(tmp);
131  pattern = sstrdup("*");
132  tofree = 1;
133  }
134  }
135 
136  nchans = 0;
137 
138  while (is_servadmin && (keyword = strtok(NULL, " "))) {
139  if (stricmp(keyword, "FORBIDDEN") == 0)
140  matchflags |= CI_VERBOTEN;
141  if (stricmp(keyword, "SUSPENDED") == 0)
142  matchflags |= CI_SUSPENDED;
143  if (stricmp(keyword, "NOEXPIRE") == 0)
144  matchflags |= CI_NO_EXPIRE;
145  }
146 
147  spattern_size = (strlen(pattern) + 2) * sizeof(char);
148  spattern = smalloc(spattern_size);
149  snprintf(spattern, spattern_size, "#%s", pattern);
150 
151 
152  notice_lang(s_ChanServ, u, CHAN_LIST_HEADER, pattern);
153  for (i = 0; i < 256; i++) {
154  for (ci = chanlists[i]; ci; ci = ci->next) {
155  if (!is_servadmin && ((ci->flags & CI_PRIVATE)
156  || (ci->flags & CI_VERBOTEN) || (ci->flags & CI_SUSPENDED)))
157  continue;
158  if ((matchflags != 0) && !(ci->flags & matchflags))
159  continue;
160 
161  if ((stricmp(pattern, ci->name) == 0)
162  || (stricmp(spattern, ci->name) == 0)
163  || match_wild_nocase(pattern, ci->name)
164  || match_wild_nocase(spattern, ci->name)) {
165  if ((((count + 1 >= from) && (count + 1 <= to))
166  || ((from == 0) && (to == 0)))
167  && (++nchans <= CSListMax)) {
168  char noexpire_char = ' ';
169  if (is_servadmin && (ci->flags & CI_NO_EXPIRE))
170  noexpire_char = '!';
171 
172  if (ci->flags & CI_VERBOTEN) {
173  snprintf(buf, sizeof(buf),
174  "%-20s [Forbidden]", ci->name);
175  } else if (ci->flags & CI_SUSPENDED) {
176  snprintf(buf, sizeof(buf),
177  "%-20s [Suspended]", ci->name);
178  } else {
179  snprintf(buf, sizeof(buf), "%-20s %s",
180  ci->name, ci->desc ? ci->desc : "");
181  }
182 
183  notice_user(s_ChanServ, u, " %c%s",
184  noexpire_char, buf);
185  }
186  count++;
187  }
188  }
189  }
190  notice_lang(s_ChanServ, u, CHAN_LIST_END,
191  nchans > CSListMax ? CSListMax : nchans, nchans);
192  free(spattern);
193  }
194  if (tofree)
195  free(pattern);
196  return MOD_CONT;
197 
198 }
E int is_oper(User *user)
Definition: users.c:937
#define CI_SUSPENDED
Definition: services.h:743
E int CSListMax
Definition: extern.h:411
static int do_list(User *u)
Definition: cs_list.c:71
E int match_wild_nocase(const char *pattern, const char *str)
Definition: misc.c:268
E int snprintf(char *buf, size_t size, const char *fmt,...)
Definition: compat.c:37
#define CI_NO_EXPIRE
Definition: services.h:729
E int stricmp(const char *s1, const char *s2)
Definition: compat.c:58
E void E void E void E void notice_user(char *source, User *u, const char *fmt,...) FORMAT(printf
E char * myStrGetTokenRemainder(const char *str, const char dilim, int token_number)
Definition: misc.c:720
#define CI_VERBOTEN
Definition: services.h:725
MDE void moduleAddAuthor(const char *author)
Definition: modules.c:1772
E void syntax_error(char *service, User *u, const char *command, int msgnum)
Definition: language.c:295
char name[CHANMAX]
Definition: services.h:654
E void notice_lang(char *source, User *dest, int message,...)
Definition: send.c:169
MDE void moduleSetType(MODType type)
Definition: modules.c:818
E ChannelInfo * chanlists[256]
Definition: extern.h:167
E char * sstrdup(const char *s)
Definition: memory.c:105
#define CI_PRIVATE
Definition: services.h:715
void AnopeFini(void)
Definition: cs_list.c:48
#define MOD_STOP
Definition: modules.h:53
uint32 flags
Definition: services.h:669
int32_t int32
Definition: db-merger.c:122
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
MDE void moduleSetChanHelp(void(*func)(User *u))
Definition: modules.c:2090
Command * c
Definition: ns_recover.c:17
#define CHANSERV
Definition: modules.h:60
E char * myStrGetOnlyToken(const char *str, const char dilim, int token_number)
Definition: misc.c:685
E int CSListOpersOnly
Definition: extern.h:410
#define MOD_CONT
Definition: modules.h:54
E int is_services_admin(User *u)
Definition: operserv.c:591
Definition: modules.h:99
MDE Command * createCommand(const char *name, int(*func)(User *u), int(*has_priv)(User *u), int help_all, int help_reg, int help_oper, int help_admin, int help_root)
Definition: modules.c:987
E void * smalloc(long size)
Definition: memory.c:30
int AnopeInit(int argc, char **argv)
Definition: cs_list.c:26
E char * s_ChanServ
Definition: extern.h:285
char * desc
Definition: services.h:659
static void myChanServHelp(User *u)
Definition: cs_list.c:59
MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos)
Definition: modules.c:1082
ChannelInfo * next
Definition: services.h:653
#define BUFSIZE
Definition: config.h:47
#define MOD_UNIQUE
Definition: module.h:11