commands.c

Go to the documentation of this file.
00001 /* Routines for looking up commands in a *Serv command list.
00002  *
00003  * (C) 2003-2007 Anope Team
00004  * Contact us at info@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church. 
00010  * 
00011  * $Id: commands.c 1282 2007-08-28 14:20:44Z geniusdex $ 
00012  *
00013  */
00014 
00015 #include "services.h"
00016 #include "commands.h"
00017 #include "language.h"
00018 
00019 /*************************************************************************/
00020 
00028 Command *lookup_cmd(Command * list, char *cmd)
00029 {
00030     Command *c;
00031 
00032     for (c = list; c->name; c++) {
00033         if (stricmp(c->name, cmd) == 0) {
00034             return c;
00035         }
00036     }
00037     return NULL;
00038 }
00039 
00040 /*************************************************************************/
00041 
00051 void run_cmd(char *service, User * u, Command * list, char *cmd)
00052 {
00053     Command *c = lookup_cmd(list, cmd);
00054     do_run_cmd(service, u, c, cmd);
00055 }
00056 
00057 /*************************************************************************/
00058 
00068 void mod_run_cmd(char *service, User * u, CommandHash * cmdTable[],
00069                  const char *cmd)
00070 {
00071     Command *c = findCommand(cmdTable, cmd);
00072     do_run_cmd(service, u, c, cmd);
00073 }
00074 
00075 
00076 /*************************************************************************/
00077 
00086 void do_run_cmd(char *service, User * u, Command * c, const char *cmd)
00087 {
00088     int retVal = 0;
00089     Command *current;
00090 
00091     if (c && c->routine) {
00092         if ((checkDefCon(DEFCON_OPER_ONLY)
00093              || checkDefCon(DEFCON_SILENT_OPER_ONLY)) && !is_oper(u)) {
00094             if (!checkDefCon(DEFCON_SILENT_OPER_ONLY)) {
00095                 notice_lang(service, u, OPER_DEFCON_DENIED);
00096             }
00097         } else {
00098             mod_current_module_name = c->mod_name;
00099             mod_current_module = NULL;
00100             if ((c->has_priv == NULL) || c->has_priv(u)) {
00101                 retVal = c->routine(u);
00102                 mod_current_module_name = NULL;
00103                 if (retVal == MOD_CONT) {
00104                     current = c->next;
00105                     while (current && retVal == MOD_CONT) {
00106                         mod_current_module_name = current->mod_name;
00107                         mod_current_module = NULL;
00108                         retVal = current->routine(u);
00109                         mod_current_module_name = NULL;
00110                         current = current->next;
00111                     }
00112                 }
00113             } else {
00114                 notice_lang(service, u, ACCESS_DENIED);
00115                 alog("Access denied for %s with service %s and command %s",
00116                      u->nick, service, cmd);
00117             }
00118             mod_current_module_name = NULL;
00119         }
00120     } else {
00121         if ((!checkDefCon(DEFCON_SILENT_OPER_ONLY)) || is_oper(u)) {
00122             notice_lang(service, u, UNKNOWN_COMMAND_HELP, cmd, service);
00123         }
00124     }
00125 }
00126 
00127 /*************************************************************************/
00128 
00136 void do_help_limited(char *service, User * u, Command * c)
00137 {
00138     if (c->has_priv == is_services_oper)
00139         notice_lang(service, u, HELP_LIMIT_SERV_OPER);
00140     else if (c->has_priv == is_services_admin)
00141         notice_lang(service, u, HELP_LIMIT_SERV_ADMIN);
00142     else if (c->has_priv == is_services_root)
00143         notice_lang(service, u, HELP_LIMIT_SERV_ROOT);
00144     else if (c->has_priv == is_oper)
00145         notice_lang(service, u, HELP_LIMIT_IRC_OPER);
00146     else if (c->has_priv == is_host_setter)
00147         notice_lang(service, u, HELP_LIMIT_HOST_SETTER);
00148     else if (c->has_priv == is_host_remover)
00149         notice_lang(service, u, HELP_LIMIT_HOST_REMOVER);
00150 }
00151 
00152 /*************************************************************************/
00153 
00162 void do_help_cmd(char *service, User * u, Command * c, const char *cmd)
00163 {
00164     Command *current;
00165     int has_had_help = 0;
00166     int cont = MOD_CONT;
00167     const char *p1 = NULL, *p2 = NULL, *p3 = NULL, *p4 = NULL;
00168     Module *calling_module = mod_current_module;
00169     char *calling_module_name = mod_current_module_name;
00170 
00171     for (current = c; (current) && (cont == MOD_CONT);
00172          current = current->next) {
00173         mod_current_module_name = current->mod_name;
00174         if (mod_current_module_name)
00175             mod_current_module = findModule(mod_current_module_name);
00176         else
00177             mod_current_module = NULL;
00178 
00179         p1 = current->help_param1;
00180         p2 = current->help_param2;
00181         p3 = current->help_param3;
00182         p4 = current->help_param4;
00183         if (current->helpmsg_all >= 0) {
00184             notice_help(service, u, current->helpmsg_all, p1, p2, p3, p4);
00185             has_had_help = 1;
00186         } else if (current->all_help) {
00187             cont = current->all_help(u);
00188             has_had_help = 1;
00189         }
00190         if (is_services_root(u)) {
00191             if (current->helpmsg_root >= 0) {
00192                 notice_help(service, u, current->helpmsg_root, p1, p2, p3,
00193                             p4);
00194                 has_had_help = 1;
00195             } else if (current->root_help) {
00196                 cont = current->root_help(u);
00197                 has_had_help = 1;
00198             }
00199         } else if (is_services_admin(u)) {
00200             if (current->helpmsg_admin >= 0) {
00201                 notice_help(service, u, current->helpmsg_admin, p1, p2, p3,
00202                             p4);
00203                 has_had_help = 1;
00204             } else if (current->admin_help) {
00205                 cont = current->admin_help(u);
00206                 has_had_help = 1;
00207             }
00208         } else if (is_services_oper(u)) {
00209             if (current->helpmsg_oper >= 0) {
00210                 notice_help(service, u, current->helpmsg_oper, p1, p2, p3,
00211                             p4);
00212                 has_had_help = 1;
00213             } else if (current->oper_help) {
00214                 cont = current->oper_help(u);
00215                 has_had_help = 1;
00216             }
00217         } else {
00218             if (current->helpmsg_reg >= 0) {
00219                 notice_help(service, u, current->helpmsg_reg, p1, p2, p3,
00220                             p4);
00221                 has_had_help = 1;
00222             } else if (current->regular_help) {
00223                 cont = current->regular_help(u);
00224                 has_had_help = 1;
00225             }
00226         }
00227     }
00228     if (has_had_help == 0) {
00229         notice_lang(service, u, NO_HELP_AVAILABLE, cmd);
00230     } else {
00231         do_help_limited(service, u, c);
00232     }
00233 
00234     mod_current_module = calling_module;
00235     mod_current_module_name = calling_module_name;
00236 }
00237 
00238 /*************************************************************************/
00239 
00248 void help_cmd(char *service, User * u, Command * list, char *cmd)
00249 {
00250     Command *c = lookup_cmd(list, cmd);
00251     do_help_cmd(service, u, c, cmd);
00252 }
00253 
00254 /*************************************************************************/
00255 
00264 void mod_help_cmd(char *service, User * u, CommandHash * cmdTable[],
00265                   const char *cmd)
00266 {
00267     Command *c = findCommand(cmdTable, cmd);
00268     do_help_cmd(service, u, c, cmd);
00269 }
00270 
00271 /*************************************************************************/

Generated on Sun Dec 30 09:26:46 2007 for Anope by  doxygen 1.5.1-20070107