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