Added SILC Thread Queue API
[runtime.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         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 };
46
47 /* Return the full command for `alias' */
48 #define alias_find(alias) \
49         iconfig_get_str("aliases", alias, NULL)
50
51 /* Returning from command function with error */
52 #define cmd_return_error(a) \
53         G_STMT_START { \
54           signal_emit("error command", 1, GINT_TO_POINTER(a)); \
55           signal_stop(); \
56           return; \
57         } G_STMT_END
58
59 #define cmd_param_error(a) \
60         G_STMT_START { \
61           cmd_params_free(free_arg); \
62           cmd_return_error(a); \
63         } G_STMT_END
64
65 extern GSList *commands;
66 extern char *current_command; /* the command we're right now. */
67
68 /* Bind command to specified function. */
69 void command_bind_full(const char *module, int priority, const char *cmd,
70                        int protocol, const char *category, SIGNAL_FUNC func,
71                        void *user_data);
72 #define command_bind(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, -1, b, c, NULL)
73 #define command_bind_first(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, -1, b, c, NULL)
74 #define command_bind_last(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, -1, b, c, NULL)
75
76 #define command_bind_data(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, -1, b, c, d)
77 #define command_bind_data_first(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, -1, b, c, d)
78 #define command_bind_data_last(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, -1, b, c, d)
79
80 #define command_bind_proto(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, b, c, d, NULL)
81 #define command_bind_proto_first(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, b, c, d, NULL)
82 #define command_bind_proto_last(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, b, c, d, NULL)
83
84 void command_unbind_full(const char *cmd, SIGNAL_FUNC func, void *user_data);
85 #define command_unbind(cmd, func) command_unbind_full(cmd, func, NULL)
86
87 /* Run subcommand, `cmd' contains the base command, first word in `data'
88    contains the subcommand */
89 void command_runsub(const char *cmd, const char *data,
90                     void *server, void *item);
91
92 COMMAND_REC *command_find(const char *cmd);
93 int command_have_sub(const char *command);
94
95 /* Specify options that command can accept. `options' contains list of
96    options separated with space, each option can contain a special
97    char in front of it:
98
99    '!': no argument (default)
100    '-': optional argument
101    '+': argument required
102    '@': optional numeric argument
103
104    for example if options = "save -file +nick", you can use
105    /command -save -file [<filename>] -nick <nickname>
106
107    You can call this command multiple times for same command, options
108    will be merged. If there's any conflicts with option types, the last
109    call will override the previous */
110 #define iscmdtype(c) \
111         ((c) == '!' || (c) == '-' || (c) == '+' || (c) == '@')
112 void command_set_options_module(const char *module,
113                                 const char *cmd, const char *options);
114 #define command_set_options(cmd, options) \
115         command_set_options_module(MODULE_NAME, cmd, options)
116
117 /* Returns TRUE if command has specified option. */
118 int command_have_option(const char *cmd, const char *option);
119
120 /* count can have these flags: */
121 #define PARAM_WITHOUT_FLAGS(a) ((a) & 0x00000fff)
122 /* don't check for quotes - "arg1 arg2" is NOT treated as one argument */
123 #define PARAM_FLAG_NOQUOTES 0x00001000
124 /* final argument gets all the rest of the arguments */
125 #define PARAM_FLAG_GETREST 0x00002000
126 /* command contains options - first you need to specify them with
127    command_set_options() function. Example:
128
129      -cmd requiredarg -noargcmd -cmd2 "another arg" -optnumarg rest of text
130
131    You would call this with:
132
133    // only once in init
134    command_set_options("mycmd", "+cmd noargcmd -cmd2 @optnumarg");
135
136    GHashTable *optlist;
137
138    cmd_get_params(data, &free_me, 1 | PARAM_FLAG_OPTIONS |
139                   PARAM_FLAG_GETREST, "mycmd", &optlist, &rest);
140
141    The optlist hash table is filled:
142
143    "cmd" = "requiredarg"
144    "noargcmd" = ""
145    "cmd2" = "another arg"
146    "optnumarg" = "" - this is because "rest" isn't a numeric value
147 */
148 #define PARAM_FLAG_OPTIONS 0x00004000
149 /* don't complain about unknown options */
150 #define PARAM_FLAG_UNKNOWN_OPTIONS 0x00008000
151 /* optional channel in first argument */
152 #define PARAM_FLAG_OPTCHAN 0x00010000
153 /* optional channel in first argument, but don't treat "*" as current channel */
154 #define PARAM_FLAG_OPTCHAN_NAME (0x00020000|PARAM_FLAG_OPTCHAN)
155
156 char *cmd_get_param(char **data);
157 char *cmd_get_quoted_param(char **data);
158 /* get parameters from command - you should point free_me somewhere and
159    cmd_params_free() it after you don't use any of the parameters anymore.
160
161    Returns TRUE if all ok, FALSE if error occured. */
162 int cmd_get_params(const char *data, gpointer *free_me, int count, ...);
163
164 void cmd_params_free(void *free_me);
165
166 void commands_remove_module(const char *module);
167
168 void commands_init(void);
169 void commands_deinit(void);
170
171 #endif