Mercurial > pidgin
annotate plugins/filectl.c @ 10312:1f2fe577ffb6
[gaim-migrate @ 11506]
Thanks to Gary Kramlich
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Fri, 03 Dec 2004 04:17:00 +0000 |
parents | bf1ebc8f3bf3 |
children | 61852117568f |
rev | line source |
---|---|
10256 | 1 /** |
2 * Send commands to Gaim via ~/.gaim/control | |
3 * | |
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 */ | |
106 | 8 |
7658 | 9 /* system includes */ |
106 | 10 #include <stdlib.h> |
7658 | 11 #include <stdio.h> |
106 | 12 #include <unistd.h> |
13 #include <sys/types.h> | |
14 #include <sys/stat.h> | |
15 #include <string.h> | |
16 #include <ctype.h> | |
17 | |
10256 | 18 #include "account.h" |
7658 | 19 #include "config.h" |
10256 | 20 #include "core.h" |
21 #include "conversation.h" | |
22 #include "debug.h" | |
23 #include "eventloop.h" | |
7658 | 24 #include "gaim.h" |
10256 | 25 #include "internal.h" |
26 #include "util.h" | |
9954 | 27 #include "version.h" |
7658 | 28 |
5255 | 29 #define FILECTL_PLUGIN_ID "core-filectl" |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
30 static int check; |
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
31 static time_t mtime; |
106 | 32 |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
33 static void init_file(); |
7658 | 34 static gboolean check_file(); |
106 | 35 |
36 /* parse char * as if were word array */ | |
37 char *getarg(char *, int, int); | |
38 | |
39 /* go through file and run any commands */ | |
10256 | 40 void |
41 run_commands() | |
42 { | |
106 | 43 struct stat finfo; |
44 char filename[256]; | |
45 char buffer[1024]; | |
46 char *command, *arg1, *arg2; | |
47 FILE *file; | |
48 | |
10256 | 49 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); |
106 | 50 |
51 file = fopen(filename, "r+"); | |
10256 | 52 while (fgets(buffer, sizeof(buffer), file)) { |
53 | |
54 /* Read the next command */ | |
106 | 55 if (buffer[strlen(buffer) - 1] == '\n') |
56 buffer[strlen(buffer) - 1] = 0; | |
10256 | 57 gaim_debug_misc("filectl", "read: %s\n", buffer); |
106 | 58 command = getarg(buffer, 0, 0); |
10256 | 59 |
10272 | 60 if (!strncasecmp(command, "login", 6)) { |
10256 | 61 GaimAccount *account; |
62 | |
106 | 63 arg1 = getarg(buffer, 1, 0); |
64 arg2 = getarg(buffer, 2, 1); | |
10256 | 65 |
66 account = gaim_accounts_find(arg1, arg2); | |
67 if (account != NULL) /* username found */ | |
68 gaim_account_connect(account); | |
69 | |
70 free(arg1); | |
71 free(arg2); | |
72 | |
10272 | 73 } else if (!strncasecmp(command, "logout", 7)) { |
10256 | 74 GaimAccount *account; |
75 GaimConnection *gc; | |
76 | |
77 arg1 = getarg(buffer, 1, 1); | |
78 arg2 = getarg(buffer, 2, 1); | |
79 | |
80 account = gaim_accounts_find(arg1, arg2); | |
81 if (account != NULL) | |
7658 | 82 { |
10256 | 83 gc = gaim_account_get_connection(account); |
84 if (gc != NULL) | |
85 gaim_connection_disconnect(gc); | |
86 } | |
87 else if (arg1 == NULL) | |
88 gaim_connections_disconnect_all(); | |
89 | |
90 free(arg1); | |
91 free(arg2); | |
92 | |
93 } else if (!strncasecmp(command, "send", 4)) { | |
94 GaimConversation *conv; | |
95 | |
96 arg1 = getarg(buffer, 1, 0); | |
97 arg2 = getarg(buffer, 2, 1); | |
98 | |
99 conv = gaim_find_conversation(GAIM_CONV_ANY, arg1); | |
100 if (conv != NULL) | |
101 { | |
102 /* | |
103 gaim_conversation_write(conv, arg2, WFLAG_SEND, NULL, time(NULL), -1); | |
104 serv_send_im(conv->gc, arg1, arg2, 0); | |
9863 | 105 */ |
7658 | 106 } |
10256 | 107 |
106 | 108 free(arg1); |
109 free(arg2); | |
10256 | 110 |
106 | 111 } else if (!strncasecmp(command, "away", 4)) { |
112 arg1 = getarg(buffer, 1, 1); | |
10256 | 113 /* serv_set_away_all(arg1); */ |
106 | 114 free(arg1); |
10256 | 115 |
3198 | 116 } else if (!strncasecmp(command, "hide", 4)) { |
10256 | 117 gaim_blist_set_visible(FALSE); |
118 | |
3198 | 119 } else if (!strncasecmp(command, "unhide", 6)) { |
10256 | 120 gaim_blist_set_visible(TRUE); |
121 | |
106 | 122 } else if (!strncasecmp(command, "back", 4)) { |
9863 | 123 /* do_im_back(); */ |
10256 | 124 |
106 | 125 } else if (!strncasecmp(command, "quit", 4)) { |
10256 | 126 gaim_core_quit(); |
127 | |
106 | 128 } |
10256 | 129 |
106 | 130 free(command); |
131 } | |
132 | |
133 fclose(file); | |
134 | |
135 if (stat (filename, &finfo) != 0) | |
136 return; | |
137 mtime = finfo.st_mtime; | |
138 } | |
139 | |
10256 | 140 /** |
141 * Check to see if the size of the file is > 0. if so, run commands. | |
142 */ | |
143 void | |
144 init_file() | |
145 { | |
106 | 146 /* most of this was taken from Bash v2.04 by the FSF */ |
147 struct stat finfo; | |
10256 | 148 char filename[256]; |
106 | 149 |
10256 | 150 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); |
106 | 151 |
10256 | 152 if ((stat(filename, &finfo) == 0) && (finfo.st_size > 0)) |
106 | 153 run_commands(); |
154 } | |
155 | |
10256 | 156 /** |
157 * Check to see if we need to run commands from the file. | |
158 */ | |
159 gboolean | |
160 check_file() | |
161 { | |
106 | 162 /* most of this was taken from Bash v2.04 by the FSF */ |
163 struct stat finfo; | |
10256 | 164 char filename[256]; |
106 | 165 |
10256 | 166 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); |
106 | 167 |
10256 | 168 if ((stat(filename, &finfo) == 0) && (finfo.st_size > 0)) |
7658 | 169 { |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
170 if (mtime != finfo.st_mtime) { |
10256 | 171 gaim_debug_info("filectl", "control changed, checking\n"); |
106 | 172 run_commands(); |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
173 } |
7658 | 174 } |
175 | |
176 return TRUE; | |
106 | 177 } |
178 | |
10256 | 179 char * |
180 getarg(char *line, int which, int remain) | |
181 { | |
106 | 182 char *arr; |
183 char *val; | |
184 int count = -1; | |
185 int i; | |
186 int state = 0; | |
187 | |
188 for (i = 0; i < strlen(line) && count < which; i++) { | |
189 switch (state) { | |
190 case 0: /* in whitespace, expecting word */ | |
191 if (isalnum(line[i])) { | |
192 count++; | |
193 state = 1; | |
194 } | |
195 break; | |
196 case 1: /* inside word, waiting for whitespace */ | |
197 if (isspace(line[i])) { | |
198 state = 0; | |
199 } | |
200 break; | |
201 } | |
202 } | |
203 | |
204 arr = strdup(&line[i - 1]); | |
205 if (remain) | |
206 return arr; | |
207 | |
208 for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++); | |
209 arr[i] = 0; | |
210 val = strdup(arr); | |
211 arr[i] = ' '; | |
212 free(arr); | |
213 return val; | |
214 } | |
10256 | 215 |
5255 | 216 /* |
217 * EXPORTED FUNCTIONS | |
218 */ | |
219 | |
220 static gboolean | |
221 plugin_load(GaimPlugin *plugin) | |
222 { | |
223 init_file(); | |
10256 | 224 check = gaim_timeout_add(5000, (GSourceFunc)check_file, NULL); |
5255 | 225 |
226 return TRUE; | |
227 } | |
228 | |
229 static gboolean | |
230 plugin_unload(GaimPlugin *plugin) | |
231 { | |
10256 | 232 gaim_timeout_remove(check); |
5255 | 233 |
234 return TRUE; | |
235 } | |
236 | |
237 static GaimPluginInfo info = | |
238 { | |
9954 | 239 GAIM_PLUGIN_MAGIC, |
240 GAIM_MAJOR_VERSION, | |
241 GAIM_MINOR_VERSION, | |
5255 | 242 GAIM_PLUGIN_STANDARD, /**< type */ |
243 NULL, /**< ui_requirement */ | |
244 0, /**< flags */ | |
245 NULL, /**< dependencies */ | |
246 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
247 | |
248 FILECTL_PLUGIN_ID, /**< id */ | |
249 N_("Gaim File Control"), /**< name */ | |
250 VERSION, /**< version */ | |
251 /** summary */ | |
252 N_("Allows you to control Gaim by entering commands in a file."), | |
253 /** description */ | |
254 N_("Allows you to control Gaim by entering commands in a file."), | |
255 "Eric Warmenhoven <eric@warmenhoven.org>", /**< author */ | |
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6179
diff
changeset
|
256 GAIM_WEBSITE, /**< homepage */ |
5255 | 257 |
258 plugin_load, /**< load */ | |
259 plugin_unload, /**< unload */ | |
260 NULL, /**< destroy */ | |
261 | |
262 NULL, /**< ui_info */ | |
263 NULL /**< extra_info */ | |
264 }; | |
265 | |
266 static void | |
5920
7d385de2f9cd
[gaim-migrate @ 6360]
Christian Hammond <chipx86@chipx86.com>
parents:
5676
diff
changeset
|
267 init_plugin(GaimPlugin *plugin) |
5255 | 268 { |
269 } | |
270 | |
6063 | 271 GAIM_INIT_PLUGIN(filectl, init_plugin, info) |