comparison mcabber/mcabber/commands.c @ 1939:932d1b250a55

Simplify cmd_add() (Myhailo Danylenko) Merge patch from isbear's mcabber-experimental repository.
author Mikael Berthe <mikael@lilotux.net>
date Sun, 27 Feb 2011 16:51:55 +0100
parents 26e437e79e60
children 11bda6d8daab
comparison
equal deleted inserted replaced
1938:26e437e79e60 1939:932d1b250a55
89 static void do_source(char *arg); 89 static void do_source(char *arg);
90 static void do_color(char *arg); 90 static void do_color(char *arg);
91 static void do_otr(char *arg); 91 static void do_otr(char *arg);
92 static void do_otrpolicy(char *arg); 92 static void do_otrpolicy(char *arg);
93 static void do_echo(char *arg); 93 static void do_echo(char *arg);
94 static void do_module(char *arg);
94 95
95 // Global variable for the commands list 96 // Global variable for the commands list
96 static GSList *Commands; 97 static GSList *Commands;
97 98
98 #ifdef MODULES_ENABLE 99 #ifdef MODULES_ENABLE
99 #include "modules.h" 100 #include "modules.h"
100
101 static void do_module(char *arg);
102 101
103 gpointer cmd_del(const char *name) 102 gpointer cmd_del(const char *name)
104 { 103 {
105 GSList *sl_cmd; 104 GSList *sl_cmd;
106 for (sl_cmd = Commands; sl_cmd; sl_cmd = sl_cmd->next) { 105 for (sl_cmd = Commands; sl_cmd; sl_cmd = sl_cmd->next) {
113 return userdata; 112 return userdata;
114 } 113 }
115 } 114 }
116 return NULL; 115 return NULL;
117 } 116 }
117 #endif
118 118
119 // cmd_add() 119 // cmd_add()
120 // Adds a command to the commands list and to the CMD completion list 120 // Adds a command to the commands list and to the CMD completion list
121 void cmd_add(const char *name, const char *help, guint flags_row1, 121 void cmd_add(const char *name, const char *help, guint flags_row1,
122 guint flags_row2, void (*f)(char*), gpointer userdata) 122 guint flags_row2, void (*f)(char*), gpointer userdata)
123 #define cmd_add(A, B, C, D, E) cmd_add (A, B, C, D, E, NULL);
124 #else
125 static void cmd_add(const char *name, const char *help,
126 guint flags_row1, guint flags_row2, void (*f)(char*))
127 #endif
128 { 123 {
129 cmd *n_cmd = g_new0(cmd, 1); 124 cmd *n_cmd = g_new0(cmd, 1);
130 strncpy(n_cmd->name, name, 32-1); 125 strncpy(n_cmd->name, name, 32-1);
131 n_cmd->help = help; 126 n_cmd->help = help;
132 n_cmd->completion_flags[0] = flags_row1; 127 n_cmd->completion_flags[0] = flags_row1;
133 n_cmd->completion_flags[1] = flags_row2; 128 n_cmd->completion_flags[1] = flags_row2;
134 n_cmd->func = f; 129 n_cmd->func = f;
135 #ifdef MODULES_ENABLE
136 n_cmd->userdata = userdata; 130 n_cmd->userdata = userdata;
137 #endif
138 Commands = g_slist_prepend(Commands, n_cmd); 131 Commands = g_slist_prepend(Commands, n_cmd);
139 // Add to completion CMD category 132 // Add to completion CMD category
140 compl_add_category_word(COMPL_CMD, name); 133 compl_add_category_word(COMPL_CMD, name);
141 } 134 }
142 135
146 // After changing commands names and it arguments names here, you must change 139 // After changing commands names and it arguments names here, you must change
147 // ones in init_bindings()! 140 // ones in init_bindings()!
148 // 141 //
149 void cmd_init(void) 142 void cmd_init(void)
150 { 143 {
151 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add); 144 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add, NULL);
152 cmd_add("alias", "Add an alias", 0, 0, &do_alias); 145 cmd_add("alias", "Add an alias", 0, 0, &do_alias, NULL);
153 cmd_add("authorization", "Manage subscription authorizations", 146 cmd_add("authorization", "Manage subscription authorizations",
154 COMPL_AUTH, COMPL_JID, &do_authorization); 147 COMPL_AUTH, COMPL_JID, &do_authorization, NULL);
155 cmd_add("bind", "Add an key binding", 0, 0, &do_bind); 148 cmd_add("bind", "Add an key binding", 0, 0, &do_bind, NULL);
156 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)", 149 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)",
157 COMPL_BUFFER, 0, &do_buffer); 150 COMPL_BUFFER, 0, &do_buffer, NULL);
158 cmd_add("chat_disable", "Disable chat mode", 0, 0, &do_chat_disable); 151 cmd_add("chat_disable", "Disable chat mode", 0, 0, &do_chat_disable, NULL);
159 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear); 152 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear, NULL);
160 cmd_add("color", "Set coloring options", COMPL_COLOR, 0, &do_color); 153 cmd_add("color", "Set coloring options", COMPL_COLOR, 0, &do_color, NULL);
161 cmd_add("connect", "Connect to the server", 0, 0, &do_connect); 154 cmd_add("connect", "Connect to the server", 0, 0, &do_connect, NULL);
162 cmd_add("del", "Delete the current buddy", 0, 0, &do_del); 155 cmd_add("del", "Delete the current buddy", 0, 0, &do_del, NULL);
163 cmd_add("disconnect", "Disconnect from server", 0, 0, &do_disconnect); 156 cmd_add("disconnect", "Disconnect from server", 0, 0, &do_disconnect, NULL);
164 cmd_add("echo", "Display a string in the log window", 0, 0, &do_echo); 157 cmd_add("echo", "Display a string in the log window", 0, 0, &do_echo, NULL);
165 cmd_add("event", "Process an event", COMPL_EVENTSID, COMPL_EVENTS, &do_event); 158 cmd_add("event", "Process an event", COMPL_EVENTSID, COMPL_EVENTS, &do_event,
159 NULL);
166 cmd_add("group", "Change group display settings", 160 cmd_add("group", "Change group display settings",
167 COMPL_GROUP, COMPL_GROUPNAME, &do_group); 161 COMPL_GROUP, COMPL_GROUPNAME, &do_group, NULL);
168 cmd_add("help", "Display some help", COMPL_CMD, 0, &do_help); 162 cmd_add("help", "Display some help", COMPL_CMD, 0, &do_help, NULL);
169 cmd_add("iline", "Manipulate input buffer", 0, 0, &do_iline); 163 cmd_add("iline", "Manipulate input buffer", 0, 0, &do_iline, NULL);
170 cmd_add("info", "Show basic info on current buddy", 0, 0, &do_info); 164 cmd_add("info", "Show basic info on current buddy", 0, 0, &do_info, NULL);
165 cmd_add("module", "Manipulations with modules", COMPL_MODULE, 0, &do_module,
166 NULL);
171 cmd_add("move", "Move the current buddy to another group", COMPL_GROUPNAME, 167 cmd_add("move", "Move the current buddy to another group", COMPL_GROUPNAME,
172 0, &do_move); 168 0, &do_move, NULL);
173 cmd_add("msay", "Send a multi-lines message to the selected buddy", 169 cmd_add("msay", "Send a multi-lines message to the selected buddy",
174 COMPL_MULTILINE, 0, &do_msay); 170 COMPL_MULTILINE, 0, &do_msay, NULL);
175 cmd_add("otr", "Manage OTR settings", COMPL_OTR, COMPL_JID, &do_otr); 171 cmd_add("otr", "Manage OTR settings", COMPL_OTR, COMPL_JID, &do_otr, NULL);
176 cmd_add("otrpolicy", "Manage OTR policies", COMPL_JID, COMPL_OTRPOLICY, 172 cmd_add("otrpolicy", "Manage OTR policies", COMPL_JID, COMPL_OTRPOLICY,
177 &do_otrpolicy); 173 &do_otrpolicy, NULL);
178 cmd_add("pgp", "Manage PGP settings", COMPL_PGP, COMPL_JID, &do_pgp); 174 cmd_add("pgp", "Manage PGP settings", COMPL_PGP, COMPL_JID, &do_pgp, NULL);
179 cmd_add("quit", "Exit the software", 0, 0, NULL); 175 cmd_add("quit", "Exit the software", 0, 0, NULL, NULL);
180 cmd_add("rawxml", "Send a raw XML string", 0, 0, &do_rawxml); 176 cmd_add("rawxml", "Send a raw XML string", 0, 0, &do_rawxml, NULL);
181 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename); 177 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename, NULL);
182 cmd_add("request", "Send a Jabber IQ request", COMPL_REQUEST, COMPL_JID, 178 cmd_add("request", "Send a Jabber IQ request", COMPL_REQUEST, COMPL_JID,
183 &do_request); 179 &do_request, NULL);
184 cmd_add("room", "MUC actions command", COMPL_ROOM, 0, &do_room); 180 cmd_add("room", "MUC actions command", COMPL_ROOM, 0, &do_room, NULL);
185 cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0, 181 cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0,
186 &do_roster); 182 &do_roster, NULL);
187 cmd_add("say", "Say something to the selected buddy", 0, 0, &do_say); 183 cmd_add("say", "Say something to the selected buddy", 0, 0, &do_say, NULL);
188 cmd_add("say_to", "Say something to a specific buddy", COMPL_JID, 0, 184 cmd_add("say_to", "Say something to a specific buddy", COMPL_JID, 0,
189 &do_say_to); 185 &do_say_to, NULL);
190 cmd_add("screen_refresh", "Redraw mcabber screen", 0, 0, &do_screen_refresh); 186 cmd_add("screen_refresh", "Redraw mcabber screen", 0, 0, &do_screen_refresh,
191 cmd_add("set", "Set/query an option value", 0, 0, &do_set); 187 NULL);
192 cmd_add("source", "Read a configuration file", 0, 0, &do_source); 188 cmd_add("set", "Set/query an option value", 0, 0, &do_set, NULL);
193 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status); 189 cmd_add("source", "Read a configuration file", 0, 0, &do_source, NULL);
190 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status,
191 NULL);
194 cmd_add("status_to", "Show or set your status for one recipient", 192 cmd_add("status_to", "Show or set your status for one recipient",
195 COMPL_JID, COMPL_STATUS, &do_status_to); 193 COMPL_JID, COMPL_STATUS, &do_status_to, NULL);
196 cmd_add("version", "Show mcabber version", 0, 0, &do_version); 194 cmd_add("version", "Show mcabber version", 0, 0, &do_version, NULL);
197 #ifdef MODULES_ENABLE
198 cmd_add("module", "Manipulations with modules", COMPL_MODULE, 0, &do_module);
199 #endif
200 195
201 // Status category 196 // Status category
202 compl_add_category_word(COMPL_STATUS, "online"); 197 compl_add_category_word(COMPL_STATUS, "online");
203 compl_add_category_word(COMPL_STATUS, "avail"); 198 compl_add_category_word(COMPL_STATUS, "avail");
204 compl_add_category_word(COMPL_STATUS, "invisible"); 199 compl_add_category_word(COMPL_STATUS, "invisible");
2828 free_arg_lst(paramlst); 2823 free_arg_lst(paramlst);
2829 } 2824 }
2830 2825
2831 // cmd_room_whois(..) 2826 // cmd_room_whois(..)
2832 // If interactive is TRUE, chatmode can be enabled. 2827 // If interactive is TRUE, chatmode can be enabled.
2833 // Please note that usernick is expected in UTF-8 locale iff interactive is FALSE 2828 // Please note that usernick is expected in UTF-8 locale iff interactive is
2834 // (in order to work correctly with auto_whois). 2829 // FALSE (in order to work correctly with auto_whois).
2835 void cmd_room_whois(gpointer bud, const char *usernick, guint interactive) 2830 void cmd_room_whois(gpointer bud, const char *usernick, guint interactive)
2836 { 2831 {
2837 char **paramlst = NULL; 2832 char **paramlst = NULL;
2838 gchar *nick, *buffer; 2833 gchar *nick, *buffer;
2839 const char *bjid, *realjid; 2834 const char *bjid, *realjid;
3010 ROSTER_UI_PRIO_STATUS_WIN_MESSAGE, prio_max); 3005 ROSTER_UI_PRIO_STATUS_WIN_MESSAGE, prio_max);
3011 g_string_free(sbuf, TRUE); 3006 g_string_free(sbuf, TRUE);
3012 g_slist_free(bm); 3007 g_slist_free(bm);
3013 } 3008 }
3014 3009
3010 static void do_module(char *arg)
3011 {
3015 #ifdef MODULES_ENABLE 3012 #ifdef MODULES_ENABLE
3016 static void do_module(char *arg)
3017 {
3018 gboolean force = FALSE; 3013 gboolean force = FALSE;
3019 char **args; 3014 char **args;
3020 3015
3021 args = split_arg(arg, 2, 0); 3016 args = split_arg(arg, 2, 0);
3022 if (!args[0] || !strcmp(args[0], "list")) { 3017 if (!args[0] || !strcmp(args[0], "list")) {
3042 error = "Unknown subcommand"; 3037 error = "Unknown subcommand";
3043 if (error) 3038 if (error)
3044 scr_LogPrint(LPRINT_LOGNORM, "Error: %s.", error); 3039 scr_LogPrint(LPRINT_LOGNORM, "Error: %s.", error);
3045 } 3040 }
3046 free_arg_lst(args); 3041 free_arg_lst(args);
3047 } 3042 #else
3043 scr_log_print(LPRINT_NORMAL,
3044 "Please recompile mcabber with modules enabled.");
3048 #endif 3045 #endif
3046 }
3049 3047
3050 static void do_room(char *arg) 3048 static void do_room(char *arg)
3051 { 3049 {
3052 char **paramlst; 3050 char **paramlst;
3053 char *subcmd; 3051 char *subcmd;
3811 readline_disable_chat_mode(show_roster); 3809 readline_disable_chat_mode(show_roster);
3812 } 3810 }
3813 3811
3814 static int source_print_error(const char *path, int eerrno) 3812 static int source_print_error(const char *path, int eerrno)
3815 { 3813 {
3816 scr_LogPrint(LPRINT_DEBUG, "Source: glob (%s) error: %s.", path, strerror(eerrno)); 3814 scr_LogPrint(LPRINT_DEBUG, "Source: glob (%s) error: %s.",
3815 path, strerror(eerrno));
3817 return 0; 3816 return 0;
3818 } 3817 }
3819 3818
3820 static void do_source(char *arg) 3819 static void do_source(char *arg)
3821 { 3820 {