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