1 #ifndef __COMMANDS_H 2 #define __COMMANDS_H 3 4 #include "signals.h" 5 6 typedef struct { 7 SIGNAL_FUNC func; 8 void *user_data; 9 } COMMAND_CALLBACK_REC; 10 11 typedef struct { 12 char *name; 13 char *options; 14 int protocol; /* chat protocol required for this command */ 15 GSList *callbacks; 16 } COMMAND_MODULE_REC; 17 18 typedef struct { 19 GSList *modules; 20 char *category; 21 char *cmd; 22 char **options; /* combined from modules[..]->options */ 23 } COMMAND_REC; 24 25 enum { 26 CMDERR_OPTION_UNKNOWN = -3, /* unknown -option */ 27 CMDERR_OPTION_AMBIGUOUS = -2, /* ambiguous -option */ 28 CMDERR_OPTION_ARG_MISSING = -1, /* argument missing for -option */ 29 30 CMDERR_UNKNOWN, /* unknown command */ 31 CMDERR_AMBIGUOUS, /* ambiguous command */ 32 33 CMDERR_ERRNO, /* get the error from errno */ 34 CMDERR_NOT_ENOUGH_PARAMS, /* not enough parameters given */ 35 CMDERR_NOT_CONNECTED, /* not connected to server */ 36 CMDERR_NOT_JOINED, /* not joined to any channels in this window */ 37 CMDERR_CHAN_NOT_FOUND, /* channel not found */ 38 CMDERR_CHAN_NOT_SYNCED, /* channel not fully synchronized yet */ 39 CMDERR_ILLEGAL_PROTO, /* requires different chat protocol than the active server */ 40 CMDERR_NOT_GOOD_IDEA, /* not good idea to do, -yes overrides this */ 41 CMDERR_INVALID_TIME, /* invalid time specification */ 42 CMDERR_INVALID_CHARSET, /* invalid charset specification */ 43 CMDERR_EVAL_MAX_RECURSE, /* eval hit recursion limit */ 44 CMDERR_PROGRAM_NOT_FOUND, /* program not found */ 45 CMDERR_NO_SERVER_DEFINED, /* no server has been defined for a given chatnet */ 46 }; 47 48 /* Return the full command for `alias' */ 49 #define alias_find(alias) \ 50 iconfig_get_str("aliases", alias, NULL) 51 52 /* Returning from command function with error */ 53 #define cmd_return_error(a) \ 54 G_STMT_START { \ 55 signal_emit("error command", 1, GINT_TO_POINTER(a)); \ 56 signal_stop(); \ 57 return; \ 58 } G_STMT_END 59 60 #define cmd_param_error(a) \ 61 G_STMT_START { \ 62 cmd_params_free(free_arg); \ 63 cmd_return_error(a); \ 64 } G_STMT_END 65 66 extern GSList *commands; 67 extern char *current_command; /* the command we're right now. */ 68 69 /* Bind command to specified function. */ 70 void command_bind_full(const char *module, int priority, const char *cmd, 71 int protocol, const char *category, SIGNAL_FUNC func, 72 void *user_data); 73 #define command_bind(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, -1, b, c, NULL) 74 #define command_bind_first(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, -1, b, c, NULL) 75 #define command_bind_last(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, -1, b, c, NULL) 76 77 #define command_bind_data(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, -1, b, c, d) 78 #define command_bind_data_first(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, -1, b, c, d) 79 #define command_bind_data_last(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, -1, b, c, d) 80 81 #define command_bind_proto(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, b, c, d, NULL) 82 #define command_bind_proto_first(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, b, c, d, NULL) 83 #define command_bind_proto_last(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, b, c, d, NULL) 84 85 void command_unbind_full(const char *cmd, SIGNAL_FUNC func, void *user_data); 86 #define command_unbind(cmd, func) command_unbind_full(cmd, func, NULL) 87 88 /* Run subcommand, `cmd' contains the base command, first word in `data' 89 contains the subcommand */ 90 void command_runsub(const char *cmd, const char *data, 91 void *server, void *item); 92 93 COMMAND_REC *command_find(const char *cmd); 94 int command_have_sub(const char *command); 95 96 /* Specify options that command can accept. `options' contains list of 97 options separated with space, each option can contain a special 98 char in front of it: 99 100 '!': no argument (default) 101 '-': optional argument 102 '+': argument required 103 '@': optional numeric argument 104 105 for example if options = "save -file +nick", you can use 106 /command -save -file [<filename>] -nick <nickname> 107 108 You can call this command multiple times for same command, options 109 will be merged. If there's any conflicts with option types, the last 110 call will override the previous */ 111 #define iscmdtype(c) \ 112 ((c) == '!' || (c) == '-' || (c) == '+' || (c) == '@') 113 void command_set_options_module(const char *module, 114 const char *cmd, const char *options); 115 #define command_set_options(cmd, options) \ 116 command_set_options_module(MODULE_NAME, cmd, options) 117 118 /* Returns TRUE if command has specified option. */ 119 int command_have_option(const char *cmd, const char *option); 120 121 /* count can have these flags: */ 122 #define PARAM_WITHOUT_FLAGS(a) ((a) & 0x00000fff) 123 /* don't check for quotes - "arg1 arg2" is NOT treated as one argument */ 124 #define PARAM_FLAG_NOQUOTES 0x00001000 125 /* final argument gets all the rest of the arguments */ 126 #define PARAM_FLAG_GETREST 0x00002000 127 /* command contains options - first you need to specify them with 128 command_set_options() function. Example: 129 130 -cmd requiredarg -noargcmd -cmd2 "another arg" -optnumarg rest of text 131 132 You would call this with: 133 134 // only once in init 135 command_set_options("mycmd", "+cmd noargcmd -cmd2 @optnumarg"); 136 137 GHashTable *optlist; 138 139 cmd_get_params(data, &free_me, 1 | PARAM_FLAG_OPTIONS | 140 PARAM_FLAG_GETREST, "mycmd", &optlist, &rest); 141 142 The optlist hash table is filled: 143 144 "cmd" = "requiredarg" 145 "noargcmd" = "" 146 "cmd2" = "another arg" 147 "optnumarg" = "" - this is because "rest" isn't a numeric value 148 */ 149 #define PARAM_FLAG_OPTIONS 0x00004000 150 /* don't complain about unknown options */ 151 #define PARAM_FLAG_UNKNOWN_OPTIONS 0x00008000 152 /* optional channel in first argument */ 153 #define PARAM_FLAG_OPTCHAN 0x00010000 154 /* optional channel in first argument, but don't treat "*" as current channel */ 155 #define PARAM_FLAG_OPTCHAN_NAME (0x00020000|PARAM_FLAG_OPTCHAN) 156 /* strip the trailing whitespace */ 157 #define PARAM_FLAG_STRIP_TRAILING_WS 0x00040000 158 159 char *cmd_get_param(char **data); 160 char *cmd_get_quoted_param(char **data); 161 /* get parameters from command - you should point free_me somewhere and 162 cmd_params_free() it after you don't use any of the parameters anymore. 163 164 Returns TRUE if all ok, FALSE if error occurred. */ 165 int cmd_get_params(const char *data, gpointer *free_me, int count, ...); 166 167 void cmd_params_free(void *free_me); 168 169 void commands_remove_module(const char *module); 170 171 void commands_init(void); 172 void commands_deinit(void); 173 174 #endif 175