comparison mcabber/src/commands.c @ 288:1eea0fa0955e

Add /bind command * Split process_line() to process_line() + process_command() * Do not expand aliases in multi-line mode * Add /bind command The binding system is ugly (not UTF-8 compatible, I think, and certainly system dependant because it is using ncurses wgetch() values).
author Mikael Berthe <mikael@lilotux.net>
date Fri, 08 Jul 2005 21:47:17 +0100
parents edc263a5d350
children f63839a4cb35
comparison
equal deleted inserted replaced
287:c2a7e78d9ff5 288:1eea0fa0955e
45 void do_info(char *arg); 45 void do_info(char *arg);
46 void do_rename(char *arg); 46 void do_rename(char *arg);
47 void do_move(char *arg); 47 void do_move(char *arg);
48 void do_set(char *arg); 48 void do_set(char *arg);
49 void do_alias(char *arg); 49 void do_alias(char *arg);
50 void do_bind(char *arg);
50 51
51 // Global variable for the commands list 52 // Global variable for the commands list
52 static GSList *Commands; 53 static GSList *Commands;
53 54
54 55
72 // ... 73 // ...
73 void cmd_init(void) 74 void cmd_init(void)
74 { 75 {
75 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add); 76 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add);
76 cmd_add("alias", "Add an alias", 0, 0, &do_alias); 77 cmd_add("alias", "Add an alias", 0, 0, &do_alias);
78 cmd_add("bind", "Add an key binding", 0, 0, &do_bind);
77 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)", 79 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)",
78 COMPL_BUFFER, 0, &do_buffer); 80 COMPL_BUFFER, 0, &do_buffer);
79 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear); 81 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear);
80 cmd_add("del", "Delete the current buddy", 0, 0, &do_del); 82 cmd_add("del", "Delete the current buddy", 0, 0, &do_del);
81 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group); 83 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group);
220 222
221 // Network part 223 // Network part
222 jb_send_msg(jid, msg); 224 jb_send_msg(jid, msg);
223 } 225 }
224 226
227 // process_command(line)
228 // Process a command line.
229 // Return 255 if this is the /quit command, and 0 for the other commands.
230 int process_command(char *line)
231 {
232 char *p;
233 char *xpline;
234 cmd *curcmd;
235
236 // Remove trailing spaces:
237 for (p=line ; *p ; p++)
238 ;
239 for (p-- ; p>line && (*p == ' ') ; p--)
240 *p = 0;
241
242 // We do alias expansion here
243 if (scr_get_multimode() != 2)
244 xpline = expandalias(line);
245 else
246 xpline = line; // No expansion in verbatim multi-line mode
247
248 // Command "quit"?
249 if ((!strncasecmp(xpline, "/quit", 5)) && (scr_get_multimode() != 2) )
250 if (!xpline[5] || xpline[5] == ' ')
251 return 255;
252
253 // If verbatim multi-line mode, we check if another /msay command is typed
254 if ((scr_get_multimode() == 2) && (strncasecmp(xpline, "/msay ", 6))) {
255 // It isn't an /msay command
256 scr_append_multiline(xpline);
257 return 0;
258 }
259
260 // Commands handling
261 curcmd = cmd_get(xpline);
262
263 if (!curcmd) {
264 scr_LogPrint("Unrecognized command, sorry.");
265 if (xpline != line) g_free(xpline);
266 return 0;
267 }
268 if (!curcmd->func) {
269 scr_LogPrint("Not yet implemented, sorry.");
270 if (xpline != line) g_free(xpline);
271 return 0;
272 }
273 // Lets go to the command parameters
274 for (p = xpline+1; *p && (*p != ' ') ; p++)
275 ;
276 // Skip spaces
277 while (*p && (*p == ' '))
278 p++;
279 // Call command-specific function
280 (*curcmd->func)(p);
281 if (xpline != line) g_free(xpline);
282 return 0;
283 }
284
225 // process_line(line) 285 // process_line(line)
226 // Process a command/message line. 286 // Process a command/message line.
227 // If this isn't a command, this is a message and it is sent to the 287 // If this isn't a command, this is a message and it is sent to the
228 // currently selected buddy. 288 // currently selected buddy.
289 // Return 255 if the line is the /quit command, or 0.
229 int process_line(char *line) 290 int process_line(char *line)
230 { 291 {
231 char *p;
232 char *xpline;
233 cmd *curcmd;
234
235 if (!*line) { // User only pressed enter 292 if (!*line) { // User only pressed enter
236 if (scr_get_multimode()) { 293 if (scr_get_multimode()) {
237 scr_append_multiline(""); 294 scr_append_multiline("");
238 return 0; 295 return 0;
239 } 296 }
252 else 309 else
253 do_say(line); 310 do_say(line);
254 return 0; 311 return 0;
255 } 312 }
256 313
257 /* It is a command */ 314 /* It is (probably) a command -- except for verbatim multi-line mode */
258 // Remove trailing spaces: 315 return process_command(line);
259 for (p=line ; *p ; p++)
260 ;
261 for (p-- ; p>line && (*p == ' ') ; p--)
262 *p = 0;
263
264 // We do alias expansion here
265 xpline = expandalias(line);
266
267 // Command "quit"?
268 if ((!strncasecmp(xpline, "/quit", 5)) && (scr_get_multimode() != 2) )
269 if (!xpline[5] || xpline[5] == ' ')
270 return 255;
271
272 // If verbatim multi-line mode, we check if another /msay command is typed
273 if ((scr_get_multimode() == 2) && (strncasecmp(xpline, "/msay ", 6))) {
274 // It isn't an /msay command
275 scr_append_multiline(xpline);
276 return 0;
277 }
278
279 // Commands handling
280 curcmd = cmd_get(xpline);
281
282 if (!curcmd) {
283 scr_LogPrint("Unrecognized command, sorry.");
284 if (xpline != line) g_free(xpline);
285 return 0;
286 }
287 if (!curcmd->func) {
288 scr_LogPrint("Not yet implemented, sorry.");
289 if (xpline != line) g_free(xpline);
290 return 0;
291 }
292 // Lets go to the command parameters
293 for (p = xpline+1; *p && (*p != ' ') ; p++)
294 ;
295 // Skip spaces
296 while (*p && (*p == ' '))
297 p++;
298 // Call command-specific function
299 (*curcmd->func)(p);
300 if (xpline != line) g_free(xpline);
301 return 0;
302 } 316 }
303 317
304 /* Commands callback functions */ 318 /* Commands callback functions */
305 319
306 void do_roster(char *arg) 320 void do_roster(char *arg)
722 compl_add_category_word(COMPL_CMD, alias); 736 compl_add_category_word(COMPL_CMD, alias);
723 settings_set(SETTINGS_TYPE_ALIAS, alias, value); 737 settings_set(SETTINGS_TYPE_ALIAS, alias, value);
724 } 738 }
725 } 739 }
726 740
741 void do_bind(char *arg)
742 {
743 guint assign;
744 const gchar *keycode, *value;
745
746 assign = parse_assigment(arg, &keycode, &value);
747 if (!keycode) {
748 scr_LogPrint("Huh?");
749 return;
750 }
751 if (!assign) {
752 // This is a query
753 value = settings_get(SETTINGS_TYPE_BINDING, keycode);
754 if (value) {
755 scr_LogPrint("Key %s is bound to: %s", keycode, value);
756 } else
757 scr_LogPrint("Key %s is not bound", keycode);
758 return;
759 }
760 // Update the key binding
761 if (!value)
762 settings_del(SETTINGS_TYPE_BINDING, keycode);
763 else
764 settings_set(SETTINGS_TYPE_BINDING, keycode, value);
765 }
766