Anope IRC Services  Version 1.8
ns_access.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_access(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("ACCESS", do_access, NULL, NICK_HELP_ACCESS, -1, -1,
35  -1, -1);
37 
39 
40  return MOD_CONT;
41 }
42 
46 void AnopeFini(void)
47 {
48 
49 }
50 
51 
52 
57 static void myNickServHelp(User * u)
58 {
59  notice_lang(s_NickServ, u, NICK_HELP_CMD_ACCESS);
60 }
61 
67 static int do_access(User * u)
68 {
69  char *cmd = strtok(NULL, " ");
70  char *mask = strtok(NULL, " ");
71  NickAlias *na;
72  int i;
73  char **access;
74 
75  if (cmd && stricmp(cmd, "LIST") == 0 && mask && is_services_admin(u)
76  && (na = findnick(mask))) {
77 
78  if (na->nc->accesscount == 0) {
79  notice_lang(s_NickServ, u, NICK_ACCESS_LIST_X_EMPTY, na->nick);
80  return MOD_CONT;
81  }
82 
83  if (na->status & NS_VERBOTEN) {
84  notice_lang(s_NickServ, u, NICK_X_FORBIDDEN, na->nick);
85  return MOD_CONT;
86  }
87 
88  if (na->nc->flags & NI_SUSPENDED) {
89  notice_lang(s_NickServ, u, NICK_X_SUSPENDED, na->nick);
90  return MOD_CONT;
91  }
92 
93 
94  notice_lang(s_NickServ, u, NICK_ACCESS_LIST_X, mask);
95  mask = strtok(NULL, " ");
96  for (access = na->nc->access, i = 0; i < na->nc->accesscount;
97  access++, i++) {
98  if (mask && !match_wild(mask, *access))
99  continue;
100  notice_user(s_NickServ, u, " %s", *access);
101  }
102 
103  } else if (!cmd || ((stricmp(cmd, "LIST") == 0) ? !!mask : !mask)) {
104  syntax_error(s_NickServ, u, "ACCESS", NICK_ACCESS_SYNTAX);
105 
106  } else if (mask && !strchr(mask, '@')) {
107  notice_lang(s_NickServ, u, BAD_USERHOST_MASK);
108  notice_lang(s_NickServ, u, MORE_INFO, s_NickServ, "ACCESS");
109 
110  } else if (!(na = u->na)) {
111  notice_lang(s_NickServ, u, NICK_NOT_REGISTERED);
112 
113  } else if (na->status & NS_VERBOTEN) {
114  notice_lang(s_NickServ, u, NICK_X_FORBIDDEN, na->nick);
115 
116  } else if (na->nc->flags & NI_SUSPENDED) {
117  notice_lang(s_NickServ, u, NICK_X_SUSPENDED, na->nick);
118 
119  } else if (!nick_identified(u)) {
120  notice_lang(s_NickServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
121 
122  } else if (stricmp(cmd, "ADD") == 0) {
123  if (na->nc->accesscount >= NSAccessMax) {
124  notice_lang(s_NickServ, u, NICK_ACCESS_REACHED_LIMIT,
125  NSAccessMax);
126  return MOD_CONT;
127  }
128 
129  for (access = na->nc->access, i = 0; i < na->nc->accesscount;
130  access++, i++) {
131  if (strcmp(*access, mask) == 0) {
132  notice_lang(s_NickServ, u, NICK_ACCESS_ALREADY_PRESENT,
133  *access);
134  return MOD_CONT;
135  }
136  }
137 
138  na->nc->accesscount++;
139  na->nc->access =
140  srealloc(na->nc->access, sizeof(char *) * na->nc->accesscount);
141  na->nc->access[na->nc->accesscount - 1] = sstrdup(mask);
142  alog("%s: %s!%s@%s added %s to their access list",
143  s_NickServ, u->nick, u->username, u->host, mask);
144  notice_lang(s_NickServ, u, NICK_ACCESS_ADDED, mask);
145 
146  } else if (stricmp(cmd, "DEL") == 0) {
147 
148  for (access = na->nc->access, i = 0; i < na->nc->accesscount;
149  access++, i++) {
150  if (stricmp(*access, mask) == 0)
151  break;
152  }
153  if (i == na->nc->accesscount) {
154  notice_lang(s_NickServ, u, NICK_ACCESS_NOT_FOUND, mask);
155  return MOD_CONT;
156  }
157  alog("%s: %s!%s@%s deleted %s from their access list",
158  s_NickServ, u->nick, u->username, u->host, mask);
159  notice_lang(s_NickServ, u, NICK_ACCESS_DELETED, *access);
160  free(*access);
161  na->nc->accesscount--;
162  if (i < na->nc->accesscount) /* if it wasn't the last entry... */
163  memmove(access, access + 1,
164  (na->nc->accesscount - i) * sizeof(char *));
165  if (na->nc->accesscount) /* if there are any entries left... */
166  na->nc->access =
167  srealloc(na->nc->access,
168  na->nc->accesscount * sizeof(char *));
169  else {
170  free(na->nc->access);
171  na->nc->access = NULL;
172  }
173  } else if (stricmp(cmd, "LIST") == 0) {
174  if (na->nc->accesscount == 0) {
175  notice_lang(s_NickServ, u, NICK_ACCESS_LIST_EMPTY, u->nick);
176  return MOD_CONT;
177  }
178 
179  notice_lang(s_NickServ, u, NICK_ACCESS_LIST);
180  for (access = na->nc->access, i = 0; i < na->nc->accesscount;
181  access++, i++) {
182  if (mask && !match_wild(mask, *access))
183  continue;
184  notice_user(s_NickServ, u, " %s", *access);
185  }
186  } else {
187  syntax_error(s_NickServ, u, "ACCESS", NICK_ACCESS_SYNTAX);
188 
189  }
190  return MOD_CONT;
191 }
char ** access
Definition: services.h:551
char nick[NICKMAX]
Definition: services.h:875
E NickAlias * findnick(const char *nick)
Definition: db-merger.c:1857
#define NI_SUSPENDED
Definition: services.h:1308
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
#define NS_VERBOTEN
Definition: services.h:1273
char * host
Definition: services.h:878
uint16 accesscount
Definition: services.h:550
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
E char * sstrdup(const char *s)
Definition: memory.c:105
NickCore * nc
Definition: services.h:533
void AnopeFini(void)
Definition: ns_access.c:46
uint16 status
Definition: services.h:532
static void myNickServHelp(User *u)
Definition: ns_access.c:57
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
Command * c
Definition: ns_recover.c:17
E void alog(const char *fmt,...) FORMAT(printf
int AnopeInit(int argc, char **argv)
Definition: ns_access.c:26
#define MOD_CONT
Definition: modules.h:54
E int is_services_admin(User *u)
Definition: operserv.c:591
char * nick
Definition: services.h:526
char * username
Definition: services.h:877
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
E void * srealloc(void *oldptr, long newsize)
Definition: memory.c:80
uint32 flags
Definition: services.h:548
E int match_wild(const char *pattern, const char *str)
Definition: misc.c:255
E char * s_NickServ
Definition: extern.h:284
MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos)
Definition: modules.c:1082
E int NSAccessMax
Definition: extern.h:385
static int do_access(User *u)
Definition: ns_access.c:67
#define NICKSERV
Definition: modules.h:59
NickAlias * na
Definition: services.h:892
#define MOD_UNIQUE
Definition: module.h:11