Mercurial > pidgin
annotate plugins/filectl.c @ 13224:2f6c932e5742
[gaim-migrate @ 15588]
Function-sized #defines are kinda ugly to work with.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Sat, 11 Feb 2006 07:51:04 +0000 |
parents | 8d1cf3f847b1 |
children | f5bc36ecf8d0 |
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 |
12397
8d1cf3f847b1
[gaim-migrate @ 14704]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
32 static void init_file(void); |
8d1cf3f847b1
[gaim-migrate @ 14704]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
33 static gboolean check_file(void); |
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 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10416
diff
changeset
|
50 file = g_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 */ | |
10744 | 67 gaim_account_connect(account); |
10256 | 68 |
69 free(arg1); | |
70 free(arg2); | |
71 | |
10272 | 72 } else if (!strncasecmp(command, "logout", 7)) { |
10256 | 73 GaimAccount *account; |
74 | |
75 arg1 = getarg(buffer, 1, 1); | |
76 arg2 = getarg(buffer, 2, 1); | |
77 | |
78 account = gaim_accounts_find(arg1, arg2); | |
79 if (account != NULL) | |
7658 | 80 { |
10738 | 81 gaim_account_disconnect(account); |
10256 | 82 } |
83 else if (arg1 == NULL) | |
84 gaim_connections_disconnect_all(); | |
85 | |
86 free(arg1); | |
87 free(arg2); | |
88 | |
89 } else if (!strncasecmp(command, "send", 4)) { | |
90 GaimConversation *conv; | |
91 | |
92 arg1 = getarg(buffer, 1, 0); | |
93 arg2 = getarg(buffer, 2, 1); | |
94 | |
11338 | 95 conv = gaim_find_conversation(GAIM_CONV_TYPE_ANY, arg1); |
10256 | 96 if (conv != NULL) |
97 { | |
98 /* | |
99 gaim_conversation_write(conv, arg2, WFLAG_SEND, NULL, time(NULL), -1); | |
100 serv_send_im(conv->gc, arg1, arg2, 0); | |
9863 | 101 */ |
7658 | 102 } |
10256 | 103 |
106 | 104 free(arg1); |
105 free(arg2); | |
10256 | 106 |
106 | 107 } else if (!strncasecmp(command, "away", 4)) { |
108 arg1 = getarg(buffer, 1, 1); | |
10256 | 109 /* serv_set_away_all(arg1); */ |
106 | 110 free(arg1); |
10256 | 111 |
3198 | 112 } else if (!strncasecmp(command, "hide", 4)) { |
10256 | 113 gaim_blist_set_visible(FALSE); |
114 | |
3198 | 115 } else if (!strncasecmp(command, "unhide", 6)) { |
10256 | 116 gaim_blist_set_visible(TRUE); |
117 | |
106 | 118 } else if (!strncasecmp(command, "back", 4)) { |
9863 | 119 /* do_im_back(); */ |
10256 | 120 |
106 | 121 } else if (!strncasecmp(command, "quit", 4)) { |
10256 | 122 gaim_core_quit(); |
123 | |
106 | 124 } |
10256 | 125 |
106 | 126 free(command); |
127 } | |
128 | |
129 fclose(file); | |
130 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10416
diff
changeset
|
131 if (g_stat(filename, &finfo) != 0) |
106 | 132 return; |
133 mtime = finfo.st_mtime; | |
134 } | |
135 | |
10256 | 136 /** |
137 * Check to see if the size of the file is > 0. if so, run commands. | |
138 */ | |
139 void | |
140 init_file() | |
141 { | |
106 | 142 /* most of this was taken from Bash v2.04 by the FSF */ |
143 struct stat finfo; | |
10256 | 144 char filename[256]; |
106 | 145 |
10256 | 146 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); |
106 | 147 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10416
diff
changeset
|
148 if ((g_stat(filename, &finfo) == 0) && (finfo.st_size > 0)) |
106 | 149 run_commands(); |
150 } | |
151 | |
10256 | 152 /** |
153 * Check to see if we need to run commands from the file. | |
154 */ | |
155 gboolean | |
156 check_file() | |
157 { | |
106 | 158 /* most of this was taken from Bash v2.04 by the FSF */ |
159 struct stat finfo; | |
10256 | 160 char filename[256]; |
106 | 161 |
10256 | 162 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); |
106 | 163 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10416
diff
changeset
|
164 if ((g_stat(filename, &finfo) == 0) && (finfo.st_size > 0)) |
7658 | 165 { |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
166 if (mtime != finfo.st_mtime) { |
10256 | 167 gaim_debug_info("filectl", "control changed, checking\n"); |
106 | 168 run_commands(); |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
169 } |
7658 | 170 } |
171 | |
172 return TRUE; | |
106 | 173 } |
174 | |
10256 | 175 char * |
176 getarg(char *line, int which, int remain) | |
177 { | |
106 | 178 char *arr; |
179 char *val; | |
180 int count = -1; | |
181 int i; | |
182 int state = 0; | |
183 | |
184 for (i = 0; i < strlen(line) && count < which; i++) { | |
185 switch (state) { | |
186 case 0: /* in whitespace, expecting word */ | |
187 if (isalnum(line[i])) { | |
188 count++; | |
189 state = 1; | |
190 } | |
191 break; | |
192 case 1: /* inside word, waiting for whitespace */ | |
193 if (isspace(line[i])) { | |
194 state = 0; | |
195 } | |
196 break; | |
197 } | |
198 } | |
199 | |
200 arr = strdup(&line[i - 1]); | |
201 if (remain) | |
202 return arr; | |
203 | |
204 for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++); | |
205 arr[i] = 0; | |
206 val = strdup(arr); | |
207 arr[i] = ' '; | |
208 free(arr); | |
209 return val; | |
210 } | |
10256 | 211 |
5255 | 212 /* |
213 * EXPORTED FUNCTIONS | |
214 */ | |
215 | |
216 static gboolean | |
217 plugin_load(GaimPlugin *plugin) | |
218 { | |
219 init_file(); | |
10256 | 220 check = gaim_timeout_add(5000, (GSourceFunc)check_file, NULL); |
5255 | 221 |
222 return TRUE; | |
223 } | |
224 | |
225 static gboolean | |
226 plugin_unload(GaimPlugin *plugin) | |
227 { | |
10256 | 228 gaim_timeout_remove(check); |
5255 | 229 |
230 return TRUE; | |
231 } | |
232 | |
233 static GaimPluginInfo info = | |
234 { | |
9954 | 235 GAIM_PLUGIN_MAGIC, |
236 GAIM_MAJOR_VERSION, | |
237 GAIM_MINOR_VERSION, | |
5255 | 238 GAIM_PLUGIN_STANDARD, /**< type */ |
239 NULL, /**< ui_requirement */ | |
240 0, /**< flags */ | |
241 NULL, /**< dependencies */ | |
242 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
243 | |
244 FILECTL_PLUGIN_ID, /**< id */ | |
245 N_("Gaim File Control"), /**< name */ | |
246 VERSION, /**< version */ | |
247 /** summary */ | |
248 N_("Allows you to control Gaim by entering commands in a file."), | |
249 /** description */ | |
250 N_("Allows you to control Gaim by entering commands in a file."), | |
251 "Eric Warmenhoven <eric@warmenhoven.org>", /**< author */ | |
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6179
diff
changeset
|
252 GAIM_WEBSITE, /**< homepage */ |
5255 | 253 |
254 plugin_load, /**< load */ | |
255 plugin_unload, /**< unload */ | |
256 NULL, /**< destroy */ | |
257 | |
258 NULL, /**< ui_info */ | |
259 NULL /**< extra_info */ | |
260 }; | |
261 | |
262 static void | |
5920
7d385de2f9cd
[gaim-migrate @ 6360]
Christian Hammond <chipx86@chipx86.com>
parents:
5676
diff
changeset
|
263 init_plugin(GaimPlugin *plugin) |
5255 | 264 { |
265 } | |
266 | |
6063 | 267 GAIM_INIT_PLUGIN(filectl, init_plugin, info) |