Anope IRC Services  Version 1.8
commands.c
Go to the documentation of this file.
1 /* Routines for looking up commands in a *Serv command list.
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 #include "services.h"
15 #include "commands.h"
16 #include "language.h"
17 
18 /*************************************************************************/
19 
27 Command *lookup_cmd(Command * list, char *cmd)
28 {
29  Command *c;
30 
31  for (c = list; c->name; c++) {
32  if (stricmp(c->name, cmd) == 0) {
33  return c;
34  }
35  }
36  return NULL;
37 }
38 
39 /*************************************************************************/
40 
50 void run_cmd(char *service, User * u, Command * list, char *cmd)
51 {
52  Command *c = lookup_cmd(list, cmd);
53  do_run_cmd(service, u, c, cmd);
54 }
55 
56 /*************************************************************************/
57 
67 void mod_run_cmd(char *service, User * u, CommandHash * cmdTable[],
68  const char *cmd)
69 {
70  Command *c = findCommand(cmdTable, cmd);
71  do_run_cmd(service, u, c, cmd);
72 }
73 
74 
75 /*************************************************************************/
76 
85 void do_run_cmd(char *service, User * u, Command * c, const char *cmd)
86 {
87  int retVal = 0;
89 
90  if (c && c->routine) {
94  notice_lang(service, u, OPER_DEFCON_DENIED);
95  }
96  } else {
97  char *mod_current_module_name_save = mod_current_module_name;
98  Module *mod_current_module_save = mod_current_module;
101  if ((c->has_priv == NULL) || c->has_priv(u)) {
102  retVal = c->routine(u);
103  if (retVal == MOD_CONT) {
104  current = c->next;
105  while (current && retVal == MOD_CONT) {
108  if (current->routine)
109  retVal = current->routine(u);
110  current = current->next;
111  }
112  }
113  } else {
114  notice_lang(service, u, ACCESS_DENIED);
115  alog("Access denied for %s with service %s and command %s",
116  u->nick, service, cmd);
117  }
118  mod_current_module_name = mod_current_module_name_save;
119  mod_current_module = mod_current_module_save;
120  }
121  } else {
123  notice_lang(service, u, UNKNOWN_COMMAND_HELP, cmd, service);
124  }
125  }
126 }
127 
128 /*************************************************************************/
129 
137 void do_help_limited(char *service, User * u, Command * c)
138 {
139  if (c->has_priv == is_services_oper)
140  notice_lang(service, u, HELP_LIMIT_SERV_OPER);
141  else if (c->has_priv == is_services_admin)
142  notice_lang(service, u, HELP_LIMIT_SERV_ADMIN);
143  else if (c->has_priv == is_services_root)
144  notice_lang(service, u, HELP_LIMIT_SERV_ROOT);
145  else if (c->has_priv == is_oper)
146  notice_lang(service, u, HELP_LIMIT_IRC_OPER);
147  else if (c->has_priv == is_host_setter)
148  notice_lang(service, u, HELP_LIMIT_HOST_SETTER);
149  else if (c->has_priv == is_host_remover)
150  notice_lang(service, u, HELP_LIMIT_HOST_REMOVER);
151 }
152 
153 /*************************************************************************/
154 
163 void do_help_cmd(char *service, User * u, Command * c, const char *cmd)
164 {
165  Command *current;
166  int has_had_help = 0;
167  int cont = MOD_CONT;
168  const char *p1 = NULL, *p2 = NULL, *p3 = NULL, *p4 = NULL;
169  Module *calling_module = mod_current_module;
170  char *calling_module_name = mod_current_module_name;
171  int help_message;
172  int (*help_message_ptr)(User *u) = NULL;
173 
174  for (current = c; (current) && (cont == MOD_CONT);
175  current = current->next) {
179  else
180  mod_current_module = NULL;
181 
182  p1 = current->help_param1;
183  p2 = current->help_param2;
184  p3 = current->help_param3;
185  p4 = current->help_param4;
186 
187  help_message = 0;
188  help_message_ptr = NULL;
189 
190  if (current->helpmsg_all >= 0) {
191  notice_help(service, u, current->helpmsg_all, p1, p2, p3, p4);
192  has_had_help = 1;
193  } else if (current->all_help) {
194  cont = current->all_help(u);
195  has_had_help = 1;
196  }
197 
198  if (is_services_root(u) && (current->helpmsg_root >= 0 || current->root_help)) {
199  if (current->helpmsg_root >= 0)
200  help_message = current->helpmsg_root;
201  else if (current->root_help)
202  help_message_ptr = current->root_help;
203  } else if (is_services_admin(u) && (current->helpmsg_admin >= 0 || current->admin_help)) {
204  if (current->helpmsg_admin >= 0)
205  help_message = current->helpmsg_admin;
206  else if (current->admin_help)
207  help_message_ptr = current->admin_help;
208  } else if (is_services_oper(u) && (current->helpmsg_oper >= 0 || current->oper_help)) {
209  if (current->helpmsg_oper >= 0)
210  help_message = current->helpmsg_oper;
211  else if (current->oper_help)
212  help_message_ptr = current->oper_help;
213  } else {
214  /* Shouldn't we check for the user to be identified? */
215  if (current->helpmsg_reg >= 0)
216  help_message = current->helpmsg_reg;
217  else if (current->regular_help)
218  help_message_ptr = current->regular_help;
219  }
220 
221  if (help_message) {
222  notice_help(service, u, help_message, p1, p2, p3, p4);
223  has_had_help = 1;
224  } else if (help_message_ptr) {
225  cont = help_message_ptr(u);
226  has_had_help = 1;
227  }
228 
229  }
230  if (has_had_help == 0) {
231  notice_lang(service, u, NO_HELP_AVAILABLE, cmd);
232  } else {
233  do_help_limited(service, u, c);
234  }
235 
236  mod_current_module = calling_module;
237  mod_current_module_name = calling_module_name;
238 }
239 
240 /*************************************************************************/
241 
250 void help_cmd(char *service, User * u, Command * list, char *cmd)
251 {
252  Command *c = lookup_cmd(list, cmd);
253  do_help_cmd(service, u, c, cmd);
254 }
255 
256 /*************************************************************************/
257 
266 void mod_help_cmd(char *service, User * u, CommandHash * cmdTable[],
267  const char *cmd)
268 {
269  Command *c = findCommand(cmdTable, cmd);
270  do_help_cmd(service, u, c, cmd);
271 }
272 
273 /*************************************************************************/
E int is_oper(User *user)
Definition: users.c:937
Command * next
Definition: modules.h:202
void do_run_cmd(char *service, User *u, Command *c, const char *cmd)
Definition: commands.c:85
char nick[NICKMAX]
Definition: services.h:875
int(* root_help)(User *u)
Definition: modules.h:200
static Channel * current
Definition: channels.c:427
E int checkDefCon(int level)
Definition: operserv.c:1608
void run_cmd(char *service, User *u, Command *list, char *cmd)
Definition: commands.c:50
int helpmsg_oper
Definition: modules.h:184
E int stricmp(const char *s1, const char *s2)
Definition: compat.c:58
char * help_param3
Definition: modules.h:189
int(* regular_help)(User *u)
Definition: modules.h:197
Command * lookup_cmd(Command *list, char *cmd)
Definition: commands.c:27
char * help_param2
Definition: modules.h:188
int helpmsg_all
Definition: modules.h:182
E void notice_lang(char *source, User *dest, int message,...)
Definition: send.c:169
void do_help_limited(char *service, User *u, Command *c)
Definition: commands.c:137
char * help_param1
Definition: modules.h:187
#define DEFCON_OPER_ONLY
Definition: services.h:1260
E int is_services_root(User *u)
Definition: operserv.c:577
int(* all_help)(User *u)
Definition: modules.h:196
void mod_run_cmd(char *service, User *u, CommandHash *cmdTable[], const char *cmd)
Definition: commands.c:67
void mod_help_cmd(char *service, User *u, CommandHash *cmdTable[], const char *cmd)
Definition: commands.c:266
MDE Module * findModule(char *name)
Definition: modules.c:473
char * name
Definition: modules.h:174
void do_help_cmd(char *service, User *u, Command *c, const char *cmd)
Definition: commands.c:163
int(* routine)(User *u)
Definition: modules.h:175
int helpmsg_root
Definition: modules.h:186
Command * c
Definition: ns_recover.c:17
E void notice_help(char *source, User *dest, int message,...)
Definition: send.c:220
E void alog(const char *fmt,...) FORMAT(printf
int(* oper_help)(User *u)
Definition: modules.h:198
#define MOD_CONT
Definition: modules.h:54
char * mod_name
Definition: modules.h:194
int(* has_priv)(User *u)
Definition: modules.h:176
E int is_services_admin(User *u)
Definition: operserv.c:591
int helpmsg_reg
Definition: modules.h:183
MDE Module * mod_current_module
Definition: modules.c:52
MDE Command * findCommand(CommandHash *cmdTable[], const char *name)
Definition: modules.c:1442
char * help_param4
Definition: modules.h:190
void help_cmd(char *service, User *u, Command *list, char *cmd)
Definition: commands.c:250
#define DEFCON_SILENT_OPER_ONLY
Definition: services.h:1261
E int is_services_oper(User *u)
Definition: operserv.c:606
E int is_host_setter(User *u)
Definition: hostserv.c:614
int(* admin_help)(User *u)
Definition: modules.h:199
E int is_host_remover(User *u)
Definition: hostserv.c:639
MDE char * mod_current_module_name
Definition: modules.c:53
int helpmsg_admin
Definition: modules.h:185