about summary refs log tree commit diff stats
path: root/src/plugins
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2016-04-10 01:15:11 +0100
committerJames Booth <boothj5@gmail.com>2016-04-10 01:15:11 +0100
commit03ab8baf4d5b1ad5474e571b66d0572624ef1c7a (patch)
tree04c0dcdee6f04064cd61d31cabdeec6a2adad003 /src/plugins
parent21aa08fdb13a62fbf8f13b445813741da1b60903 (diff)
downloadprofani-tty-03ab8baf4d5b1ad5474e571b66d0572624ef1c7a.tar.gz
Added /plugins load command
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/c_plugins.c5
-rw-r--r--src/plugins/plugins.c99
-rw-r--r--src/plugins/plugins.h4
-rw-r--r--src/plugins/python_plugins.c2
4 files changed, 88 insertions, 22 deletions
diff --git a/src/plugins/c_plugins.c b/src/plugins/c_plugins.c
index 07a618b6..ff05791d 100644
--- a/src/plugins/c_plugins.c
+++ b/src/plugins/c_plugins.c
@@ -74,10 +74,8 @@ c_plugin_create(const char *const filename)
         return NULL;
     }
 
-    gchar *module_name = g_strndup(filename, strlen(filename) - 3);
-
     plugin = malloc(sizeof(ProfPlugin));
-    plugin->name = strdup(module_name);
+    plugin->name = strdup(filename);
     plugin->lang = LANG_C;
     plugin->module = handle;
     plugin->init_func = c_init_hook;
@@ -110,7 +108,6 @@ c_plugin_create(const char *const filename)
     plugin->on_room_win_focus = c_on_room_win_focus_hook;
 
     g_string_free(path, TRUE);
-    g_free(module_name);
 
     return plugin;
 }
diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c
index 73f3f873..e2d0bdf3 100644
--- a/src/plugins/plugins.c
+++ b/src/plugins/plugins.c
@@ -78,13 +78,13 @@ plugins_init(void)
     plugin_settings_init();
 
     // load plugins
-    gchar **plugins_load = prefs_get_plugins();
-    if (plugins_load) {
+    gchar **plugins_pref = prefs_get_plugins();
+    if (plugins_pref) {
         int i;
-        for (i = 0; i < g_strv_length(plugins_load); i++)
+        for (i = 0; i < g_strv_length(plugins_pref); i++)
         {
             gboolean loaded = FALSE;
-            gchar *filename = plugins_load[i];
+            gchar *filename = plugins_pref[i];
 #ifdef HAVE_PYTHON
             if (g_str_has_suffix(filename, ".py")) {
                 ProfPlugin *plugin = python_plugin_create(filename);
@@ -103,8 +103,10 @@ plugins_init(void)
                 }
             }
 #endif
-            if (loaded == TRUE) {
+            if (loaded) {
                 log_info("Loaded plugin: %s", filename);
+            } else {
+                log_info("Failed to load plugin: %s", filename);
             }
         }
 
@@ -117,29 +119,94 @@ plugins_init(void)
         }
     }
 
-    prefs_free_plugins(plugins_load);
+    prefs_free_plugins(plugins_pref);
 
     return;
 }
 
+gboolean
+_find_by_name(gconstpointer pluginp, gconstpointer namep)
+{
+    char *name = (char*)namep;
+    ProfPlugin *plugin = (ProfPlugin*)pluginp;
+
+    return g_strcmp0(name, plugin->name);
+}
+
+gboolean
+plugins_load(const char *const name)
+{
+    GSList *found = g_slist_find_custom(plugins, name, (GCompareFunc)_find_by_name);
+    if (found) {
+        log_info("Failed to load plugin: %s, plugin already loaded", name);
+        return FALSE;
+    }
+
+    ProfPlugin *plugin = NULL;
+#ifdef HAVE_PYTHON
+    if (g_str_has_suffix(name, ".py")) {
+        plugin = python_plugin_create(name);
+    }
+#endif
+#ifdef HAVE_C
+    if (g_str_has_suffix(name, ".so")) {
+        plugin = c_plugin_create(name);
+    }
+#endif
+    if (plugin) {
+        plugins = g_slist_append(plugins, plugin);
+        plugin->init_func(plugin, PACKAGE_VERSION, PACKAGE_STATUS);
+        log_info("Loaded plugin: %s", name);
+        return TRUE;
+    } else {
+        log_info("Failed to load plugin: %s", name);
+        return FALSE;
+    }
+}
+
 GSList *
 plugins_get_list(void)
 {
     return plugins;
 }
 
-char *
-plugins_get_lang_string(ProfPlugin *plugin)
+static gchar*
+_get_plugins_dir(void)
+{
+    gchar *xdg_data = xdg_get_data_home();
+    GString *plugins_dir = g_string_new(xdg_data);
+    g_free(xdg_data);
+    g_string_append(plugins_dir, "/profanity/plugins");
+    return g_string_free(plugins_dir, FALSE);
+}
+
+void
+_plugins_list_dir(const gchar *const dir, GSList **result)
 {
-    switch (plugin->lang)
-    {
-        case LANG_PYTHON:
-            return "Python";
-        case LANG_C:
-            return "C";
-        default:
-            return "Unknown";
+    GDir *plugins = g_dir_open(dir, 0, NULL);
+    if (plugins == NULL) {
+        return;
     }
+
+    const gchar *plugin = g_dir_read_name(plugins);
+    while (plugin) {
+        if (g_str_has_suffix(plugin, ".so") || g_str_has_suffix(plugin, ".py")) {
+            *result = g_slist_append(*result, strdup(plugin));
+        }
+        plugin = g_dir_read_name(plugins);
+    }
+    g_dir_close(plugins);
+}
+
+GSList*
+plugins_file_list(void)
+{
+    GSList *result = NULL;
+    char *plugins_dir = _get_plugins_dir();
+    _plugins_list_dir(plugins_dir, &result);
+    free(plugins_dir);
+
+    return result;
 }
 
 char *
diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h
index 335bf8f5..85792674 100644
--- a/src/plugins/plugins.h
+++ b/src/plugins/plugins.h
@@ -99,11 +99,13 @@ typedef struct prof_plugin_t {
 
 void plugins_init(void);
 GSList* plugins_get_list(void);
-char* plugins_get_lang_string(ProfPlugin *plugin);
+GSList *plugins_file_list(void);
 char* plugins_autocomplete(const char *const input);
 void plugins_reset_autocomplete(void);
 void plugins_shutdown(void);
 
+gboolean plugins_load(const char *const name);
+
 void plugins_on_start(void);
 void plugins_on_shutdown(void);
 
diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c
index 90ff36ed..a73cc15b 100644
--- a/src/plugins/python_plugins.c
+++ b/src/plugins/python_plugins.c
@@ -95,7 +95,7 @@ python_plugin_create(const char *const filename)
     python_check_error();
     if (p_module) {
         ProfPlugin *plugin = malloc(sizeof(ProfPlugin));
-        plugin->name = strdup(module_name);
+        plugin->name = strdup(filename);
         plugin->lang = LANG_PYTHON;
         plugin->module = p_module;
         plugin->init_func = python_init_hook;
tter hut <hut@lavabit.com> 2013-02-10 03:35:27 +0100 replaced tabs with 4 spaces in all python files' href='/akspecs/ranger/commit/ranger/core/tab.py?id=d1a1173ddc315f21a3d468f43ac55aa43d31883d'>d1a1173d ^
7ee4b45b ^




d1a1173d ^





11549e2c ^
d1a1173d ^



1687e0f4 ^
d1a1173d ^












b3d031a9 ^

d1a1173d ^

























1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174