Anope IRC Services  Version 1.8
ns_list.c
Go to the documentation of this file.
1 /* NickServ 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 myNickServHelp(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, NICK_HELP_LIST, -1,
35  NICK_SERVADMIN_HELP_LIST, NICK_SERVADMIN_HELP_LIST);
36 
38 
40 
41  return MOD_CONT;
42 }
43 
47 void AnopeFini(void)
48 {
49 
50 }
51 
52 
53 
58 static void myNickServHelp(User * u)
59 {
60  if (!NSListOpersOnly || (is_oper(u))) {
61  notice_lang(s_NickServ, u, NICK_HELP_CMD_LIST);
62  }
63 }
64 
70 static int do_list(User * u)
71 {
72 
73 /* SADMINS can search for nicks based on their NS_VERBOTEN and NS_NO_EXPIRE
74  * status. The keywords FORBIDDEN and NOEXPIRE represent these two states
75  * respectively. These keywords should be included after the search pattern.
76  * Multiple keywords are accepted and should be separated by spaces. Only one
77  * of the keywords needs to match a nick's state for the nick to be displayed.
78  * Forbidden nicks can be identified by "[Forbidden]" appearing in the last
79  * seen address field. Nicks with NOEXPIRE set are preceeded by a "!". Only
80  * SADMINS will be shown forbidden nicks and the "!" indicator.
81  * Syntax for sadmins: LIST pattern [FORBIDDEN] [NOEXPIRE]
82  * -TheShadow
83  *
84  * UPDATE: SUSPENDED keyword is now accepted as well.
85  */
86 
87 
88  char *pattern = strtok(NULL, " ");
89  char *keyword;
90  NickAlias *na;
91  NickCore *mync;
92  int nnicks, i;
93  char buf[BUFSIZE];
94  int is_servadmin = is_services_admin(u);
95  int16 matchflags = 0;
96  NickRequest *nr = NULL;
97  int nronly = 0;
98  int susp_keyword = 0;
99  char noexpire_char = ' ';
100  int count = 0, from = 0, to = 0, tofree = 0;
101  char *tmp = NULL;
102  char *s = NULL;
103 
104  if (!(!NSListOpersOnly || (is_oper(u)))) { /* reverse the help logic */
105  notice_lang(s_NickServ, u, ACCESS_DENIED);
106  return MOD_STOP;
107  }
108 
109  if (!pattern) {
110  syntax_error(s_NickServ, u, "LIST",
111  is_servadmin ? NICK_LIST_SERVADMIN_SYNTAX :
112  NICK_LIST_SYNTAX);
113  } else {
114 
115  if (pattern) {
116  if (pattern[0] == '#') {
117  tmp = myStrGetOnlyToken((pattern + 1), '-', 0); /* Read FROM out */
118  if (!tmp) {
119  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
120  return MOD_CONT;
121  }
122  for (s = tmp; *s; s++) {
123  if (!isdigit(*s)) {
124  free(tmp);
125  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
126  return MOD_CONT;
127  }
128  }
129  from = atoi(tmp);
130  free(tmp);
131  tmp = myStrGetTokenRemainder(pattern, '-', 1); /* Read TO out */
132  if (!tmp) {
133  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
134  return MOD_CONT;
135  }
136  for (s = tmp; *s; s++) {
137  if (!isdigit(*s)) {
138  free(tmp);
139  notice_lang(s_ChanServ, u, LIST_INCORRECT_RANGE);
140  return MOD_CONT;
141  }
142  }
143  to = atoi(tmp);
144  free(tmp);
145  pattern = sstrdup("*");
146  tofree = 1;
147  }
148  }
149 
150  nnicks = 0;
151 
152  while (is_servadmin && (keyword = strtok(NULL, " "))) {
153  if (stricmp(keyword, "FORBIDDEN") == 0)
154  matchflags |= NS_VERBOTEN;
155  if (stricmp(keyword, "NOEXPIRE") == 0)
156  matchflags |= NS_NO_EXPIRE;
157  if (stricmp(keyword, "SUSPENDED") == 0)
158  susp_keyword = 1;
159  if (stricmp(keyword, "UNCONFIRMED") == 0)
160  nronly = 1;
161  }
162 
163  mync = (nick_identified(u) ? u->na->nc : NULL);
164 
165  notice_lang(s_NickServ, u, NICK_LIST_HEADER, pattern);
166  if (nronly != 1) {
167  for (i = 0; i < 1024; i++) {
168  for (na = nalists[i]; na; na = na->next) {
169  /* Don't show private and forbidden nicks to non-services admins. */
170  if ((na->status & NS_VERBOTEN) && !is_servadmin)
171  continue;
172  if ((na->nc->flags & NI_PRIVATE) && !is_servadmin
173  && na->nc != mync)
174  continue;
175  if ((matchflags != 0) && !(na->status & matchflags) && (susp_keyword == 0))
176  continue;
177  else if ((susp_keyword == 1) && !(na->nc->flags & NI_SUSPENDED))
178  continue;
179 
180  /* We no longer compare the pattern against the output buffer.
181  * Instead we build a nice nick!user@host buffer to compare.
182  * The output is then generated separately. -TheShadow */
183  snprintf(buf, sizeof(buf), "%s!%s", na->nick,
184  (na->last_usermask
185  && !(na->status & NS_VERBOTEN)) ? na->
186  last_usermask : "*@*");
187  if (stricmp(pattern, na->nick) == 0
188  || match_wild_nocase(pattern, buf)) {
189 
190  if ((((count + 1 >= from) && (count + 1 <= to))
191  || ((from == 0) && (to == 0)))
192  && (++nnicks <= NSListMax)) {
193  if (is_servadmin
194  && (na->status & NS_NO_EXPIRE))
195  noexpire_char = '!';
196  else {
197  noexpire_char = ' ';
198  }
199  if ((na->nc->flags & NI_HIDE_MASK)
200  && !is_servadmin && na->nc != mync) {
201  snprintf(buf, sizeof(buf),
202  "%-20s [Hostname Hidden]",
203  na->nick);
204  } else if (na->status & NS_VERBOTEN) {
205  snprintf(buf, sizeof(buf),
206  "%-20s [Forbidden]", na->nick);
207  } else if (na->nc->flags & NI_SUSPENDED) {
208  snprintf(buf, sizeof(buf),
209  "%-20s [Suspended]", na->nick);
210  } else {
211  snprintf(buf, sizeof(buf), "%-20s %s",
212  na->nick, na->last_usermask);
213  }
214  notice_user(s_NickServ, u, " %c%s",
215  noexpire_char, buf);
216  }
217  count++;
218  }
219  }
220  }
221  }
222 
223  if (nronly == 1 || (is_servadmin && matchflags == 0 && susp_keyword == 0)) {
224  noexpire_char = ' ';
225  for (i = 0; i < 1024; i++) {
226  for (nr = nrlists[i]; nr; nr = nr->next) {
227  snprintf(buf, sizeof(buf), "%s!*@*", nr->nick);
228  if (stricmp(pattern, nr->nick) == 0
229  || match_wild_nocase(pattern, buf)) {
230  if (++nnicks <= NSListMax) {
231  snprintf(buf, sizeof(buf),
232  "%-20s [UNCONFIRMED]", nr->nick);
233  notice_user(s_NickServ, u, " %c%s",
234  noexpire_char, buf);
235  }
236  }
237  }
238  }
239  }
240  notice_lang(s_NickServ, u, NICK_LIST_RESULTS,
241  nnicks > NSListMax ? NSListMax : nnicks, nnicks);
242  }
243  if (tofree)
244  free(pattern);
245  return MOD_CONT;
246 }
E int is_oper(User *user)
Definition: users.c:937
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
static int do_list(User *u)
Definition: ns_list.c:70
#define NI_SUSPENDED
Definition: services.h:1308
static void myNickServHelp(User *u)
Definition: ns_list.c:58
E int nick_identified(User *u)
Definition: nickserv.c:1111
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 NS_VERBOTEN
Definition: services.h:1273
NickAlias * next
Definition: services.h:525
E NickRequest * nrlists[1024]
Definition: extern.h:925
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
E void notice_lang(char *source, User *dest, int message,...)
Definition: send.c:169
MDE void moduleSetType(MODType type)
Definition: modules.c:818
#define NI_PRIVATE
Definition: services.h:1296
E char * sstrdup(const char *s)
Definition: memory.c:105
void AnopeFini(void)
Definition: ns_list.c:47
NickCore * nc
Definition: services.h:533
NickRequest * next
Definition: services.h:515
#define MOD_STOP
Definition: modules.h:53
uint16 status
Definition: services.h:532
#define NI_HIDE_MASK
Definition: services.h:1298
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
E int NSListOpersOnly
Definition: extern.h:391
Command * c
Definition: ns_recover.c:17
E char * myStrGetOnlyToken(const char *str, const char dilim, int token_number)
Definition: misc.c:685
#define MOD_CONT
Definition: modules.h:54
int16_t int16
Definition: db-merger.c:120
E int is_services_admin(User *u)
Definition: operserv.c:591
E NickAlias * nalists[1024]
Definition: extern.h:923
char * nick
Definition: services.h:526
E int NSListMax
Definition: extern.h:392
MDE void moduleSetNickHelp(void(*func)(User *u))
Definition: modules.c:2078
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
uint32 flags
Definition: services.h:548
E char * s_ChanServ
Definition: extern.h:285
#define NS_NO_EXPIRE
Definition: services.h:1274
int AnopeInit(int argc, char **argv)
Definition: ns_list.c:26
char * nick
Definition: services.h:516
E char * s_NickServ
Definition: extern.h:284
MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos)
Definition: modules.c:1082
char * last_usermask
Definition: services.h:529
#define BUFSIZE
Definition: config.h:47
#define NICKSERV
Definition: modules.h:59
NickAlias * na
Definition: services.h:892
#define MOD_UNIQUE
Definition: module.h:11