changeset 1988:dd65a18dc480

Add cmd_set_safe() / cmd_is_safe() (Myhailo Danylenko) Patch merged from isbear's mcabber-experimental repository.
author Mikael Berthe <mikael@lilotux.net>
date Sun, 27 Mar 2011 18:57:42 +0200
parents 212c9589b25c
children d23f1b8aa716
files mcabber/mcabber/api.h mcabber/mcabber/commands.c mcabber/mcabber/commands.h mcabber/mcabber/settings.c
diffstat 4 files changed, 68 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/mcabber/api.h	Sun Mar 27 18:34:13 2011 +0200
+++ b/mcabber/mcabber/api.h	Sun Mar 27 18:57:42 2011 +0200
@@ -3,7 +3,7 @@
 
 #include <mcabber/config.h> // For MCABBER_BRANCH
 
-#define MCABBER_API_VERSION 19
+#define MCABBER_API_VERSION 20
 #define MCABBER_API_MIN     19
 
 extern const gchar *mcabber_branch;
--- a/mcabber/mcabber/commands.c	Sun Mar 27 18:34:13 2011 +0200
+++ b/mcabber/mcabber/commands.c	Sun Mar 27 18:57:42 2011 +0200
@@ -99,6 +99,7 @@
 
 // Global variable for the commands list
 static GSList *Commands;
+static GSList *safe_commands;
 
 #ifdef MODULES_ENABLE
 #include "modules.h"
@@ -137,6 +138,42 @@
   return n_cmd;
 }
 
+//  cmd_set_safe(name, safe)
+// Sets if command can be used in startup configuration file.
+gboolean cmd_set_safe(const gchar *name, gboolean safe)
+{
+  GSList *sel;
+  if (!name)
+    return FALSE;
+  for (sel = safe_commands; sel; sel = sel->next)
+    if (!strcmp((const char *)sel->data, name)) {
+      if (safe)
+        return FALSE;
+      else {
+        g_free(sel->data);
+        safe_commands = g_slist_delete_link(safe_commands, sel);
+      }
+    }
+  if (safe)
+    safe_commands = g_slist_append(safe_commands, g_strdup(name));
+  else
+    return FALSE;
+  return TRUE;
+}
+
+//  cmd_is_safe(name)
+// Returns if command is safe or not
+gboolean cmd_is_safe(const gchar *name)
+{
+  GSList *sel;
+  if (!name)
+    return FALSE;
+  for (sel = safe_commands; sel; sel = sel->next)
+    if (!strcmp((const char *)sel->data, name))
+      return TRUE;
+  return FALSE;
+}
+
 //  cmd_init()
 // Commands table initialization
 // !!!
@@ -197,6 +234,16 @@
           COMPL_JID, COMPL_STATUS, &do_status_to, NULL);
   cmd_add("version", "Show mcabber version", 0, 0, &do_version, NULL);
 
+  cmd_set_safe("set", TRUE);
+  cmd_set_safe("bind", TRUE);
+  cmd_set_safe("alias", TRUE);
+  cmd_set_safe("pgp", TRUE);
+  cmd_set_safe("source", TRUE);
+  cmd_set_safe("status", TRUE);
+  cmd_set_safe("color", TRUE);
+  cmd_set_safe("otrpolicy", TRUE);
+  cmd_set_safe("module", TRUE);
+
   // Status category
   compl_add_category_word(COMPL_STATUS, "online");
   compl_add_category_word(COMPL_STATUS, "avail");
--- a/mcabber/mcabber/commands.h	Sun Mar 27 18:34:13 2011 +0200
+++ b/mcabber/mcabber/commands.h	Sun Mar 27 18:57:42 2011 +0200
@@ -23,7 +23,9 @@
 gpointer cmd_del(gpointer id);
 gpointer cmd_add(const char *name, const char *help, guint flags1, guint flags2,
                  void (*f)(char*), gpointer userdata);
+gboolean cmd_set_safe(const gchar *name, gboolean safe);
 #endif
+gboolean cmd_is_safe(const gchar *name);
 
 void cmd_room_whois(gpointer bud, const char *nick, guint interactive);
 void cmd_room_leave(gpointer bud, char *arg);
--- a/mcabber/mcabber/settings.c	Sun Mar 27 18:34:13 2011 +0200
+++ b/mcabber/mcabber/settings.c	Sun Mar 27 18:57:42 2011 +0200
@@ -183,46 +183,28 @@
     if ((*line == '\n') || (*line == '\0') || (*line == '#'))
       continue;
 
-    // We only allow assignments line, except for commands "pgp", "source",
-    // "color", "load" and "otrpolicy", unless we're in runtime (i.e. not startup).
-    if (runtime ||
-        (strchr(line, '=') != NULL)        ||
-        startswith(line, "pgp ", FALSE)    ||
-        startswith(line, "source ", FALSE) ||
-        startswith(line, "color ", FALSE)  ||
-#ifdef MODULES_ENABLE
-        startswith(line, "module ", FALSE) ||
-#endif
-        startswith(line, "status ", FALSE) ||
-        startswith(line, "otrpolicy", FALSE)) {
-      // Only accept a few "safe" commands
-      if (!runtime &&
-          !startswith(line, "set ", FALSE)    &&
-          !startswith(line, "bind ", FALSE)   &&
-          !startswith(line, "alias ", FALSE)  &&
-          !startswith(line, "pgp ", FALSE)    &&
-          !startswith(line, "source ", FALSE) &&
-          !startswith(line, "status ", FALSE) &&
-          !startswith(line, "color ", FALSE)  &&
-#ifdef MODULES_ENABLE
-          !startswith(line, "module ", FALSE) &&
-#endif
-          !startswith(line, "otrpolicy ", FALSE)) {
-        scr_LogPrint(LPRINT_LOGNORM, "Error in configuration file (l. %d): "
-                     "this command can't be used here", ln);
+    // If we aren't in runtime (i.e. startup) we'll only accept "safe" commands
+    if (!runtime) {
+      const gchar *cmdend = strchr(line, ' ');
+      gchar *cmdname = NULL;
+      gboolean safe;
+      if (cmdend)
+        cmdname = g_strndup(line, cmdend - line);
+      safe = cmd_is_safe(cmdname ? cmdname : line);
+      g_free(cmdname);
+      if (!safe) {
+        scr_log_print(LPRINT_LOGNORM, "Error in configuration file (l. %d): "
+                      "this command can't be used here", ln);
         err++;
         continue;
       }
-      // Set the leading COMMAND_CHAR to build a command line
-      // and process the command
-      *(--line) = COMMAND_CHAR;
-      if (process_command(line, TRUE) == 255)
-        mcabber_set_terminate_ui();
-    } else {
-      scr_LogPrint(LPRINT_LOGNORM, "Error in configuration file (l. %d): "
-                   "this is not an assignment", ln);
-      err++;
     }
+
+    // Set the leading COMMAND_CHAR to build a command line
+    // and process the command
+    *(--line) = COMMAND_CHAR;
+    if (process_command(line, TRUE) == 255)
+      mcabber_set_terminate_ui();
   }
   g_free(buf);
   fclose(fp);