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