Anope IRC Services  Version 1.8
os_admin.c
Go to the documentation of this file.
1 /* OperServ 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_admin(User * u);
18 static int admin_list_callback(SList * slist, int number, void *item,
19  va_list args);
20 static int admin_list(int number, NickCore * nc, User * u, int *sent_header);
21 static void myOperServHelp(User * u);
22 
29 int AnopeInit(int argc, char **argv)
30 {
31  Command *c;
32 
33  moduleAddAuthor("Anope");
34  moduleAddVersion(VERSION_STRING);
36 
37  c = createCommand("ADMIN", do_admin, NULL, OPER_HELP_ADMIN, -1, -1,
38  -1, -1);
41 
43 
44  return MOD_CONT;
45 }
46 
50 void AnopeFini(void)
51 {
52 
53 }
54 
55 
56 
61 static void myOperServHelp(User * u)
62 {
63  notice_lang(s_OperServ, u, OPER_HELP_CMD_ADMIN);
64 }
65 
71 static int do_admin(User * u)
72 {
73  char *cmd = strtok(NULL, " ");
74  char *nick = strtok(NULL, " ");
75  NickAlias *na;
76  int res = 0;
77 
78  if (skeleton) {
79  notice_lang(s_OperServ, u, OPER_ADMIN_SKELETON);
80  return MOD_CONT;
81  }
82 
83  if (!cmd || (!nick && stricmp(cmd, "LIST") && stricmp(cmd, "CLEAR"))) {
84  syntax_error(s_OperServ, u, "ADMIN", OPER_ADMIN_SYNTAX);
85  } else if (!stricmp(cmd, "ADD")) {
86  if (!is_services_root(u)) {
87  notice_lang(s_OperServ, u, PERMISSION_DENIED);
88  return MOD_CONT;
89  }
90 
91  if (!(na = findnick(nick))) {
92  notice_lang(s_OperServ, u, NICK_X_NOT_REGISTERED, nick);
93  return MOD_CONT;
94  }
95 
96  if (na->status & NS_VERBOTEN) {
97  notice_lang(s_OperServ, u, NICK_X_FORBIDDEN, nick);
98  return MOD_CONT;
99  }
100 
101  if (na->nc->flags & NI_SERVICES_ADMIN
102  || slist_indexof(&servadmins, na->nc) != -1) {
103  notice_lang(s_OperServ, u, OPER_ADMIN_EXISTS, nick);
104  return MOD_CONT;
105  }
106 
107  res = slist_add(&servadmins, na->nc);
108  if (res == -2) {
109  notice_lang(s_OperServ, u, OPER_ADMIN_REACHED_LIMIT, nick);
110  return MOD_CONT;
111  } else {
112  if (na->nc->flags & NI_SERVICES_OPER
113  && (res = slist_indexof(&servopers, na->nc)) != -1) {
114  slist_delete(&servopers, res);
115  na->nc->flags |= NI_SERVICES_ADMIN;
116  notice_lang(s_OperServ, u, OPER_ADMIN_MOVED, nick);
117  } else {
118  na->nc->flags |= NI_SERVICES_ADMIN;
119  notice_lang(s_OperServ, u, OPER_ADMIN_ADDED, nick);
120  }
121  }
122 
123  if (readonly)
124  notice_lang(s_OperServ, u, READ_ONLY_MODE);
125  } else if (!stricmp(cmd, "DEL")) {
126  if (!is_services_root(u)) {
127  notice_lang(s_OperServ, u, PERMISSION_DENIED);
128  return MOD_CONT;
129  }
130 
131  if (servadmins.count == 0) {
132  notice_lang(s_OperServ, u, OPER_ADMIN_LIST_EMPTY);
133  return MOD_CONT;
134  }
135 
136  if (isdigit(*nick) && strspn(nick, "1234567890,-") == strlen(nick)) {
137  /* Deleting a range */
138  res = slist_delete_range(&servadmins, nick, NULL);
139  if (res == 0) {
140  notice_lang(s_OperServ, u, OPER_ADMIN_NO_MATCH);
141  return MOD_CONT;
142  } else if (res == 1) {
143  notice_lang(s_OperServ, u, OPER_ADMIN_DELETED_ONE);
144  } else {
145  notice_lang(s_OperServ, u, OPER_ADMIN_DELETED_SEVERAL,
146  res);
147  }
148  } else {
149  if (!(na = findnick(nick))) {
150  notice_lang(s_OperServ, u, NICK_X_NOT_REGISTERED, nick);
151  return MOD_CONT;
152  }
153 
154  if (na->status & NS_VERBOTEN) {
155  notice_lang(s_OperServ, u, NICK_X_FORBIDDEN, nick);
156  return MOD_CONT;
157  }
158 
159  if (!(na->nc->flags & NI_SERVICES_ADMIN)
160  || (res = slist_indexof(&servadmins, na->nc)) == -1) {
161  notice_lang(s_OperServ, u, OPER_ADMIN_NOT_FOUND, nick);
162  return MOD_CONT;
163  }
164 
165  slist_delete(&servadmins, res);
166  notice_lang(s_OperServ, u, OPER_ADMIN_DELETED, nick);
167  }
168 
169  if (readonly)
170  notice_lang(s_OperServ, u, READ_ONLY_MODE);
171  } else if (!stricmp(cmd, "LIST")) {
172  int sent_header = 0;
173 
174  if (servadmins.count == 0) {
175  notice_lang(s_OperServ, u, OPER_ADMIN_LIST_EMPTY);
176  return MOD_CONT;
177  }
178 
179  if (!nick || (isdigit(*nick)
180  && strspn(nick, "1234567890,-") == strlen(nick))) {
181  res =
183  &sent_header);
184  if (res == 0) {
185  notice_lang(s_OperServ, u, OPER_ADMIN_NO_MATCH);
186  return MOD_CONT;
187  } else {
188  notice_lang(s_OperServ, u, END_OF_ANY_LIST, "Admin");
189  }
190  } else {
191  int i;
192 
193  for (i = 0; i < servadmins.count; i++)
194  if (!stricmp
195  (nick, ((NickCore *) servadmins.list[i])->display)
196  || match_wild_nocase(nick,
197  ((NickCore *) servadmins.
198  list[i])->display))
199  admin_list(i + 1, servadmins.list[i], u, &sent_header);
200 
201  if (!sent_header)
202  notice_lang(s_OperServ, u, OPER_ADMIN_NO_MATCH);
203  else {
204  notice_lang(s_OperServ, u, END_OF_ANY_LIST, "Admin");
205  }
206  }
207  } else if (!stricmp(cmd, "CLEAR")) {
208  if (!is_services_root(u)) {
209  notice_lang(s_OperServ, u, PERMISSION_DENIED);
210  return MOD_CONT;
211  }
212 
213  if (servadmins.count == 0) {
214  notice_lang(s_OperServ, u, OPER_ADMIN_LIST_EMPTY);
215  return MOD_CONT;
216  }
217 
218  slist_clear(&servadmins, 1);
219  notice_lang(s_OperServ, u, OPER_ADMIN_CLEAR);
220  } else {
221  syntax_error(s_OperServ, u, "ADMIN", OPER_ADMIN_SYNTAX);
222  }
223  return MOD_CONT;
224 }
225 
226 static int admin_list_callback(SList * slist, int number, void *item,
227  va_list args)
228 {
229  User *u = va_arg(args, User *);
230  int *sent_header = va_arg(args, int *);
231 
232  return admin_list(number, item, u, sent_header);
233 }
234 
235 static int admin_list(int number, NickCore * nc, User * u, int *sent_header)
236 {
237  if (!nc)
238  return 0;
239 
240  if (!*sent_header) {
241  notice_lang(s_OperServ, u, OPER_ADMIN_LIST_HEADER);
242  *sent_header = 1;
243  }
244 
245  notice_lang(s_OperServ, u, OPER_ADMIN_LIST_FORMAT, number,
246  nc->display);
247  return 1;
248 }
E size_t strspn(const char *s, const char *accept)
Definition: compat.c:106
E int readonly
Definition: extern.h:776
E int match_wild_nocase(const char *pattern, const char *str)
Definition: misc.c:268
E NickAlias * findnick(const char *nick)
Definition: db-merger.c:1857
E int slist_delete_range(SList *slist, char *range, slist_delcheckcb_t cb,...)
Definition: slist.c:129
E SList servadmins
Definition: extern.h:971
int AnopeInit(int argc, char **argv)
Definition: os_admin.c:29
E int stricmp(const char *s1, const char *s2)
Definition: compat.c:58
E int slist_enum(SList *slist, char *range, slist_enumcb_t cb,...)
Definition: slist.c:203
#define NS_VERBOTEN
Definition: services.h:1273
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
char * help_param1
Definition: modules.h:187
NickCore * nc
Definition: services.h:533
static void myOperServHelp(User *u)
Definition: os_admin.c:61
char * display
Definition: services.h:542
E int is_services_root(User *u)
Definition: operserv.c:577
E void slist_clear(SList *slist, int free)
Definition: slist.c:70
#define OPERSERV
Definition: modules.h:62
void ** list
Definition: slist.h:21
E char * s_OperServ
Definition: extern.h:289
E int slist_delete(SList *slist, int index)
Definition: slist.c:97
int16 count
Definition: slist.h:23
static int admin_list_callback(SList *slist, int number, void *item, va_list args)
Definition: os_admin.c:226
static int admin_list(int number, NickCore *nc, User *u, int *sent_header)
Definition: os_admin.c:235
uint16 status
Definition: services.h:532
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
Command * c
Definition: ns_recover.c:17
E int slist_indexof(SList *slist, void *item)
Definition: slist.c:317
MDE void moduleSetOperHelp(void(*func)(User *u))
Definition: modules.c:2126
#define MOD_CONT
Definition: modules.h:54
static int do_admin(User *u)
Definition: os_admin.c:71
E int skeleton
Definition: extern.h:778
#define NI_SERVICES_OPER
Definition: services.h:1302
Definition: modules.h:99
Definition: slist.h:20
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
void AnopeFini(void)
Definition: os_admin.c:50
uint32 flags
Definition: services.h:548
#define NI_SERVICES_ADMIN
Definition: services.h:1303
E int slist_add(SList *slist, void *item)
Definition: slist.c:29
E char * s_NickServ
Definition: extern.h:284
MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos)
Definition: modules.c:1082
E SList servopers
Definition: extern.h:972
#define MOD_UNIQUE
Definition: module.h:11