Adapt migration for files
[pidgin-git.git] / libpurple / plugins / filectl.c
blobc2f2d1093cbc7be923b01dcd39225fd184a86343
1 /**
2 * Send commands to Purple via ~/.purple/control
4 * Originally by Eric Warmenhoven <eric@warmenhoven.org>
5 * Compile fixes/mini hacks Alex Bennee <alex@bennee.com>
6 * and Brian Tarricone <bjt23@users.sourceforge.net>
7 */
9 /* system includes */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <string.h>
16 #include <ctype.h>
18 #include "internal.h"
19 #include "account.h"
20 #include "config.h"
21 #include "core.h"
22 #include "conversation.h"
23 #include "debug.h"
24 #include "eventloop.h"
25 #include "util.h"
26 #include "version.h"
28 #define FILECTL_PLUGIN_ID "core-filectl"
29 static int check;
30 static time_t mtime;
32 static void init_file(void);
33 static gboolean check_file(void);
35 /* parse char * as if were word array */
36 char *getarg(char *, int, int);
38 /* go through file and run any commands */
39 void
40 run_commands()
42 GStatBuf finfo;
43 char filename[MAXPATHLEN];
44 char buffer[1024];
45 char *command, *arg1, *arg2;
46 FILE *file;
48 snprintf(filename, MAXPATHLEN, "%s" G_DIR_SEPARATOR_S "control", purple_user_dir());
50 file = g_fopen(filename, "r+");
51 while (fgets(buffer, sizeof(buffer), file)) {
53 /* Read the next command */
54 if (buffer[strlen(buffer) - 1] == '\n')
55 buffer[strlen(buffer) - 1] = 0;
56 purple_debug_misc("filectl", "read: %s\n", buffer);
57 command = getarg(buffer, 0, 0);
59 if (!g_ascii_strncasecmp(command, "login", 6)) {
60 PurpleAccount *account;
62 arg1 = getarg(buffer, 1, 0);
63 arg2 = getarg(buffer, 2, 1);
65 account = purple_accounts_find(arg1, arg2);
66 if (account != NULL) /* username found */
67 purple_account_connect(account);
69 free(arg1);
70 free(arg2);
72 } else if (!g_ascii_strncasecmp(command, "logout", 7)) {
73 PurpleAccount *account;
75 arg1 = getarg(buffer, 1, 1);
76 arg2 = getarg(buffer, 2, 1);
78 account = purple_accounts_find(arg1, arg2);
79 if (account != NULL)
81 purple_account_disconnect(account);
83 else if (arg1 == NULL)
84 purple_connections_disconnect_all();
86 free(arg1);
87 free(arg2);
89 /* purple_find_conversation() is gone in 2.0.0. */
90 #if 0
91 } else if (!g_ascii_strncasecmp(command, "send", 4)) {
92 PurpleConversation *conv;
94 arg1 = getarg(buffer, 1, 0);
95 arg2 = getarg(buffer, 2, 1);
97 conv = purple_find_conversation(PURPLE_CONV_TYPE_ANY, arg1);
98 if (conv != NULL)
101 purple_conversation_write(conv, arg2, WFLAG_SEND, NULL, time(NULL), -1);
102 purple_serv_send_im(conv->gc, arg1, arg2, 0);
106 free(arg1);
107 free(arg2);
108 #endif
110 } else if (!g_ascii_strncasecmp(command, "away", 4)) {
111 arg1 = getarg(buffer, 1, 1);
112 /* serv_set_away_all(arg1); */
113 free(arg1);
115 } else if (!g_ascii_strncasecmp(command, "hide", 4)) {
116 purple_blist_set_visible(FALSE);
118 } else if (!g_ascii_strncasecmp(command, "unhide", 6)) {
119 purple_blist_set_visible(TRUE);
121 } else if (!g_ascii_strncasecmp(command, "back", 4)) {
122 /* do_im_back(); */
124 } else if (!g_ascii_strncasecmp(command, "quit", 4)) {
125 purple_core_quit();
129 free(command);
132 fclose(file);
134 if (g_stat(filename, &finfo) != 0)
135 return;
136 mtime = finfo.st_mtime;
140 * Check to see if the size of the file is > 0. if so, run commands.
142 void
143 init_file()
145 /* most of this was taken from Bash v2.04 by the FSF */
146 GStatBuf finfo;
147 char filename[MAXPATHLEN];
149 snprintf(filename, MAXPATHLEN, "%s" G_DIR_SEPARATOR_S "control", purple_user_dir());
151 if ((g_stat(filename, &finfo) == 0) && (finfo.st_size > 0))
152 run_commands();
156 * Check to see if we need to run commands from the file.
158 gboolean
159 check_file()
161 /* most of this was taken from Bash v2.04 by the FSF */
162 GStatBuf finfo;
163 char filename[MAXPATHLEN];
165 snprintf(filename, MAXPATHLEN, "%s" G_DIR_SEPARATOR_S "control", purple_user_dir());
167 if ((g_stat(filename, &finfo) == 0) && (finfo.st_size > 0))
169 if (mtime != finfo.st_mtime) {
170 purple_debug_info("filectl", "control changed, checking\n");
171 run_commands();
175 return TRUE;
178 char *
179 getarg(char *line, int which, int remain)
181 char *arr;
182 char *val;
183 int count = -1;
184 int i;
185 int state = 0;
187 for (i = 0; i < strlen(line) && count < which; i++) {
188 switch (state) {
189 case 0: /* in whitespace, expecting word */
190 if (isalnum(line[i])) {
191 count++;
192 state = 1;
194 break;
195 case 1: /* inside word, waiting for whitespace */
196 if (isspace(line[i])) {
197 state = 0;
199 break;
203 arr = strdup(&line[i - 1]);
204 if (remain)
205 return arr;
207 for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++);
208 arr[i] = 0;
209 val = strdup(arr);
210 arr[i] = ' ';
211 free(arr);
212 return val;
216 * EXPORTED FUNCTIONS
219 static PurplePluginInfo *
220 plugin_query(GError **error)
222 const gchar * const authors[] = {
223 "Eric Warmenhoven <eric@warmenhoven.org>",
224 NULL
227 return purple_plugin_info_new(
228 "id", FILECTL_PLUGIN_ID,
229 "name", N_("File Control"),
230 "version", DISPLAY_VERSION,
231 "category", N_("Utility"),
232 "summary", N_("Allows control by entering commands in a file."),
233 "description", N_("Allows control by entering commands in a file."),
234 "authors", authors,
235 "website", PURPLE_WEBSITE,
236 "abi-version", PURPLE_ABI_VERSION,
237 NULL
241 static gboolean
242 plugin_load(PurplePlugin *plugin, GError **error)
244 init_file();
245 check = purple_timeout_add_seconds(5, (GSourceFunc)check_file, NULL);
247 return TRUE;
250 static gboolean
251 plugin_unload(PurplePlugin *plugin, GError **error)
253 purple_timeout_remove(check);
255 return TRUE;
258 PURPLE_PLUGIN_INIT(filectl, plugin_query, plugin_load, plugin_unload);