Mikael Berthe avatar Mikael Berthe committed dd65a18

Add cmd_set_safe() / cmd_is_safe() (Myhailo Danylenko)
Patch merged from isbear's mcabber-experimental repository.

Comments (0)

Files changed (4)

mcabber/mcabber/api.h

 
 #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;

mcabber/mcabber/commands.c

 
 // Global variable for the commands list
 static GSList *Commands;
+static GSList *safe_commands;
 
 #ifdef MODULES_ENABLE
 #include "modules.h"
   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
 // !!!
           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");

mcabber/mcabber/commands.h

 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);

mcabber/mcabber/settings.c

     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);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.