Mercurial > pidgin
annotate plugins/filectl.c @ 8033:c417617ae0cf
[gaim-migrate @ 8713]
I can't think of a good british joke, so insert your favorite one here.
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Wed, 07 Jan 2004 04:04:44 +0000 |
parents | 251e2e434a86 |
children | e4952ba22fc1 |
rev | line source |
---|---|
7658 | 1 /* |
2 ** Send commands to gaim via ~/.gaim/control | |
3 ** | |
4 ** By Eric Warmenhoven <eric@warmenhoven.org> | |
5 ** compile fixes/mini hacks Alex Bennee <alex@bennee.com> | |
6 */ | |
106 | 7 |
7658 | 8 /* system includes */ |
106 | 9 #include <gtk/gtk.h> |
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 | |
7658 | 18 /* gaim includes */ |
19 #include "internal.h" | |
20 | |
21 #include "config.h" | |
22 #include "gaim.h" | |
23 #include "debug.h" | |
24 #include "account.h" | |
25 #include "conversation.h" | |
26 | |
27 | |
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 */ | |
40 void run_commands() { | |
41 struct stat finfo; | |
42 char filename[256]; | |
43 char buffer[1024]; | |
44 char *command, *arg1, *arg2; | |
45 FILE *file; | |
46 | |
47 sprintf(filename, "%s/.gaim/control", getenv("HOME")); | |
48 | |
49 file = fopen(filename, "r+"); | |
50 while (fgets(buffer, sizeof buffer, file)) { | |
51 if (buffer[strlen(buffer) - 1] == '\n') | |
52 buffer[strlen(buffer) - 1] = 0; | |
5227
6d1707dc8c3d
[gaim-migrate @ 5597]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
53 gaim_debug(GAIM_DEBUG_MISC, "filectl", "read: %s\n", buffer); |
106 | 54 command = getarg(buffer, 0, 0); |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
55 if (!strncasecmp(command, "signon", 6)) { |
7658 | 56 GaimAccount *account = NULL; |
57 GList *accts = gaim_accounts_get_all(); | |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
58 arg1 = getarg(buffer, 1, 1); |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
59 if (arg1) { |
4491 | 60 while (accts) { |
7658 | 61 GaimAccount *a = accts->data; |
4491 | 62 if (!strcmp(a->username, arg1)) { |
63 account = a; | |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
64 break; |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
65 } |
4491 | 66 accts = accts->next; |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
67 } |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
68 free(arg1); |
106 | 69 } |
4491 | 70 if (account) /* username found */ |
6036 | 71 gaim_account_connect(account); |
106 | 72 } else if (!strncasecmp(command, "signoff", 7)) { |
7658 | 73 GaimConnection *gc = NULL; |
74 GList *c = gaim_connections_get_all(); | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
75 arg1 = getarg(buffer, 1, 1); |
2766
0e082a9e4c32
[gaim-migrate @ 2779]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2765
diff
changeset
|
76 while (arg1 && c) { |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
77 gc = c->data; |
7658 | 78 if (!strcmp(gc->account->username, arg1)) { |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
79 break; |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
80 } |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
81 gc = NULL; |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
82 c = c->next; |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
83 } |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
84 if (gc) |
7658 | 85 gaim_connection_disconnect(gc); |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
86 else if (!arg1) |
7658 | 87 gaim_connections_disconnect_all(); |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
88 free(arg1); |
106 | 89 } else if (!strncasecmp(command, "send", 4)) { |
5676
dae79aefac8d
[gaim-migrate @ 6094]
Christian Hammond <chipx86@chipx86.com>
parents:
5255
diff
changeset
|
90 GaimConversation *c; |
106 | 91 arg1 = getarg(buffer, 1, 0); |
92 arg2 = getarg(buffer, 2, 1); | |
7658 | 93 c = gaim_find_conversation(arg1); |
94 if (c) | |
95 { | |
96 /* disable for now | |
97 gaim_conversation_write(c, arg2, WFLAG_SEND, NULL, time(NULL), -1); | |
98 serv_send_im(c->gc, arg1, arg2, 0); | |
99 */ | |
100 } | |
106 | 101 free(arg1); |
102 free(arg2); | |
103 } else if (!strncasecmp(command, "away", 4)) { | |
104 arg1 = getarg(buffer, 1, 1); | |
7658 | 105 serv_set_away_all(arg1); |
106 | 106 free(arg1); |
3198 | 107 } else if (!strncasecmp(command, "hide", 4)) { |
7658 | 108 //hide_buddy_list(); |
3198 | 109 } else if (!strncasecmp(command, "unhide", 6)) { |
7658 | 110 //unhide_buddy_list(); |
106 | 111 } else if (!strncasecmp(command, "back", 4)) { |
7658 | 112 //do_im_back(); |
106 | 113 } else if (!strncasecmp(command, "quit", 4)) { |
7658 | 114 //gaim_core_quit(); |
106 | 115 } |
116 free(command); | |
117 } | |
118 | |
119 fclose(file); | |
120 | |
121 if (stat (filename, &finfo) != 0) | |
122 return; | |
123 mtime = finfo.st_mtime; | |
124 } | |
125 | |
126 /* check to see if the size of the file is > 0. if so, run commands */ | |
127 void init_file() { | |
128 /* most of this was taken from Bash v2.04 by the FSF */ | |
129 struct stat finfo; | |
130 char file[256]; | |
131 | |
132 sprintf(file, "%s/.gaim/control", getenv("HOME")); | |
133 | |
134 if ((stat (file, &finfo) == 0) && (finfo.st_size > 0)) | |
135 run_commands(); | |
136 } | |
137 | |
138 /* check to see if we need to run commands from the file */ | |
7658 | 139 gboolean check_file() { |
106 | 140 /* most of this was taken from Bash v2.04 by the FSF */ |
141 struct stat finfo; | |
142 char file[256]; | |
143 | |
144 sprintf(file, "%s/.gaim/control", getenv("HOME")); | |
145 | |
146 if ((stat (file, &finfo) == 0) && (finfo.st_size > 0)) | |
7658 | 147 { |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
148 if (mtime != finfo.st_mtime) { |
5227
6d1707dc8c3d
[gaim-migrate @ 5597]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
149 gaim_debug(GAIM_DEBUG_INFO, "filectl", |
6d1707dc8c3d
[gaim-migrate @ 5597]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
150 "control changed, checking\n"); |
106 | 151 run_commands(); |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
152 } |
7658 | 153 } |
154 | |
155 return TRUE; | |
106 | 156 } |
157 | |
158 char *getarg(char *line, int which, int remain) { | |
159 char *arr; | |
160 char *val; | |
161 int count = -1; | |
162 int i; | |
163 int state = 0; | |
164 | |
165 for (i = 0; i < strlen(line) && count < which; i++) { | |
166 switch (state) { | |
167 case 0: /* in whitespace, expecting word */ | |
168 if (isalnum(line[i])) { | |
169 count++; | |
170 state = 1; | |
171 } | |
172 break; | |
173 case 1: /* inside word, waiting for whitespace */ | |
174 if (isspace(line[i])) { | |
175 state = 0; | |
176 } | |
177 break; | |
178 } | |
179 } | |
180 | |
181 arr = strdup(&line[i - 1]); | |
182 if (remain) | |
183 return arr; | |
184 | |
185 for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++); | |
186 arr[i] = 0; | |
187 val = strdup(arr); | |
188 arr[i] = ' '; | |
189 free(arr); | |
190 return val; | |
191 } | |
5255 | 192 /* |
193 * EXPORTED FUNCTIONS | |
194 */ | |
195 | |
196 static gboolean | |
197 plugin_load(GaimPlugin *plugin) | |
198 { | |
199 init_file(); | |
7658 | 200 check = g_timeout_add(5000, (GSourceFunc) check_file, NULL); |
5255 | 201 |
202 return TRUE; | |
203 } | |
204 | |
205 static gboolean | |
206 plugin_unload(GaimPlugin *plugin) | |
207 { | |
208 g_source_remove(check); | |
209 | |
210 return TRUE; | |
211 } | |
212 | |
213 static GaimPluginInfo info = | |
214 { | |
215 2, /**< api_version */ | |
216 GAIM_PLUGIN_STANDARD, /**< type */ | |
217 NULL, /**< ui_requirement */ | |
218 0, /**< flags */ | |
219 NULL, /**< dependencies */ | |
220 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
221 | |
222 FILECTL_PLUGIN_ID, /**< id */ | |
223 N_("Gaim File Control"), /**< name */ | |
224 VERSION, /**< version */ | |
225 /** summary */ | |
226 N_("Allows you to control Gaim by entering commands in a file."), | |
227 /** description */ | |
228 N_("Allows you to control Gaim by entering commands in a file."), | |
229 "Eric Warmenhoven <eric@warmenhoven.org>", /**< author */ | |
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6179
diff
changeset
|
230 GAIM_WEBSITE, /**< homepage */ |
5255 | 231 |
232 plugin_load, /**< load */ | |
233 plugin_unload, /**< unload */ | |
234 NULL, /**< destroy */ | |
235 | |
236 NULL, /**< ui_info */ | |
237 NULL /**< extra_info */ | |
238 }; | |
239 | |
240 static void | |
5920
7d385de2f9cd
[gaim-migrate @ 6360]
Christian Hammond <chipx86@chipx86.com>
parents:
5676
diff
changeset
|
241 init_plugin(GaimPlugin *plugin) |
5255 | 242 { |
243 } | |
244 | |
6063 | 245 GAIM_INIT_PLUGIN(filectl, init_plugin, info) |