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