Anope IRC Services  Version 1.8
os_oper.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_oper(User * u);
18 static int oper_list_callback(SList * slist, int number, void *item,
19  va_list args);
20 static int oper_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  c = createCommand("OPER", do_oper, NULL, OPER_HELP_OPER, -1, -1, -1,
37  -1);
40 
42 
43  return MOD_CONT;
44 }
45 
49 void AnopeFini(void)
50 {
51 
52 }
53 
54 
59 static void myOperServHelp(User * u)
60 {
61  notice_lang(s_OperServ, u, OPER_HELP_CMD_OPER);
62 }
63 
69 static int do_oper(User * u)
70 {
71  char *cmd = strtok(NULL, " ");
72  char *nick = strtok(NULL, " ");
73  NickAlias *na;
74  int res = 0;
75 
76  if (skeleton) {
77  notice_lang(s_OperServ, u, OPER_OPER_SKELETON);
78  return MOD_CONT;
79  }
80 
81  if (!cmd || (!nick && stricmp(cmd, "LIST") && stricmp(cmd, "CLEAR"))) {
82  syntax_error(s_OperServ, u, "OPER", OPER_OPER_SYNTAX);
83  } else if (!stricmp(cmd, "ADD")) {
84  if (!is_services_root(u)) {
85  notice_lang(s_OperServ, u, PERMISSION_DENIED);
86  return MOD_CONT;
87  }
88 
89  if (!(na = findnick(nick))) {
90  notice_lang(s_OperServ, u, NICK_X_NOT_REGISTERED, nick);
91  return MOD_CONT;
92  }
93 
94  if (na->status & NS_VERBOTEN) {
95  notice_lang(s_OperServ, u, NICK_X_FORBIDDEN, nick);
96  return MOD_CONT;
97  }
98 
99  if (na->nc->flags & NI_SERVICES_OPER
100  || slist_indexof(&servopers, na->nc) != -1) {
101  notice_lang(s_OperServ, u, OPER_OPER_EXISTS, nick);
102  return MOD_CONT;
103  }
104 
105  res = slist_add(&servopers, na->nc);
106  if (res == -2) {
107  notice_lang(s_OperServ, u, OPER_OPER_REACHED_LIMIT, nick);
108  return MOD_CONT;
109  } else {
110  if (na->nc->flags & NI_SERVICES_ADMIN
111  && (res = slist_indexof(&servadmins, na->nc)) != -1) {
112  if (!is_services_root(u)) {
113  notice_lang(s_OperServ, u, PERMISSION_DENIED);
114  return MOD_CONT;
115  }
116  slist_delete(&servadmins, res);
117  na->nc->flags |= NI_SERVICES_OPER;
118  notice_lang(s_OperServ, u, OPER_OPER_MOVED, nick);
119  } else {
120  na->nc->flags |= NI_SERVICES_OPER;
121  notice_lang(s_OperServ, u, OPER_OPER_ADDED, nick);
122  }
123  }
124 
125  if (readonly)
126  notice_lang(s_OperServ, u, READ_ONLY_MODE);
127  } else if (!stricmp(cmd, "DEL")) {
128  if (!is_services_root(u)) {
129  notice_lang(s_OperServ, u, PERMISSION_DENIED);
130  return MOD_CONT;
131  }
132 
133  if (isdigit(*nick) && strspn(nick, "1234567890,-") == strlen(nick)) {
134  /* Deleting a range */
135  res = slist_delete_range(&servopers, nick, NULL);
136  if (res == 0) {
137  notice_lang(s_OperServ, u, OPER_OPER_NO_MATCH);
138  return MOD_CONT;
139  } else if (res == 1) {
140  notice_lang(s_OperServ, u, OPER_OPER_DELETED_ONE);
141  } else {
142  notice_lang(s_OperServ, u, OPER_OPER_DELETED_SEVERAL, res);
143  }
144  } else {
145  if (!(na = findnick(nick))) {
146  notice_lang(s_OperServ, u, NICK_X_NOT_REGISTERED, nick);
147  return MOD_CONT;
148  }
149 
150  if (na->status & NS_VERBOTEN) {
151  notice_lang(s_OperServ, u, NICK_X_FORBIDDEN, nick);
152  return MOD_CONT;
153  }
154 
155  if (!(na->nc->flags & NI_SERVICES_OPER)
156  || (res = slist_indexof(&servopers, na->nc)) == -1) {
157  notice_lang(s_OperServ, u, OPER_OPER_NOT_FOUND, nick);
158  return MOD_CONT;
159  }
160 
161  slist_delete(&servopers, res);
162  notice_lang(s_OperServ, u, OPER_OPER_DELETED, nick);
163  }
164 
165  if (readonly)
166  notice_lang(s_OperServ, u, READ_ONLY_MODE);
167  } else if (!stricmp(cmd, "LIST")) {
168  int sent_header = 0;
169 
170  if (!is_oper(u)) {
171  notice_lang(s_OperServ, u, PERMISSION_DENIED);
172  return MOD_CONT;
173  }
174 
175  if (servopers.count == 0) {
176  notice_lang(s_OperServ, u, OPER_OPER_LIST_EMPTY);
177  return MOD_CONT;
178  }
179 
180  if (!nick || (isdigit(*nick)
181  && strspn(nick, "1234567890,-") == strlen(nick))) {
182  res =
184  &sent_header);
185  if (res == 0) {
186  notice_lang(s_OperServ, u, OPER_OPER_NO_MATCH);
187  return MOD_CONT;
188  } else {
189  notice_lang(s_OperServ, u, END_OF_ANY_LIST, "Oper");
190  }
191  } else {
192  int i;
193 
194  for (i = 0; i < servopers.count; i++)
195  if (!stricmp
196  (nick, ((NickCore *) servopers.list[i])->display)
197  || match_wild_nocase(nick,
198  ((NickCore *) servopers.list[i])->
199  display))
200  oper_list(i + 1, servopers.list[i], u, &sent_header);
201 
202  if (!sent_header)
203  notice_lang(s_OperServ, u, OPER_OPER_NO_MATCH);
204  else {
205  notice_lang(s_OperServ, u, END_OF_ANY_LIST, "Oper");
206  }
207  }
208  } else if (!stricmp(cmd, "CLEAR")) {
209  if (!is_services_root(u)) {
210  notice_lang(s_OperServ, u, PERMISSION_DENIED);
211  return MOD_CONT;
212  }
213 
214  if (servopers.count == 0) {
215  notice_lang(s_OperServ, u, OPER_OPER_LIST_EMPTY);
216  return MOD_CONT;
217  }
218 
219  slist_clear(&servopers, 1);
220  notice_lang(s_OperServ, u, OPER_OPER_CLEAR);
221  } else {
222  syntax_error(s_OperServ, u, "OPER", OPER_OPER_SYNTAX);
223  }
224  return MOD_CONT;
225 }
226 
227 /* Lists an oper entry, prefixing it with the header if needed */
228 
229 static int oper_list(int number, NickCore * nc, User * u, int *sent_header)
230 {
231  if (!nc)
232  return 0;
233 
234  if (!*sent_header) {
235  notice_lang(s_OperServ, u, OPER_OPER_LIST_HEADER);
236  *sent_header = 1;
237  }
238 
239  notice_lang(s_OperServ, u, OPER_OPER_LIST_FORMAT, number, nc->display);
240  return 1;
241 }
242 
243 /* Callback for enumeration purposes */
244 
245 static int oper_list_callback(SList * slist, int number, void *item, va_list args)
246 {
247  User *u = va_arg(args, User *);
248  int *sent_header = va_arg(args, int *);
249 
250  return oper_list(number, item, u, sent_header);
251 }
E int is_oper(User *user)
Definition: users.c:937
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
static int oper_list(int number, NickCore *nc, User *u, int *sent_header)
Definition: os_oper.c:229
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
void AnopeFini(void)
Definition: os_oper.c:49
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
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
uint16 status
Definition: services.h:532
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
static int do_oper(User *u)
Definition: os_oper.c:69
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 void myOperServHelp(User *u)
Definition: os_oper.c:59
static int oper_list_callback(SList *slist, int number, void *item, va_list args)
Definition: os_oper.c:245
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
int AnopeInit(int argc, char **argv)
Definition: os_oper.c:29
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