Mercurial > pidgin
comparison libgaim/plugins/filectl.c @ 14192:60b1bc8dbf37
[gaim-migrate @ 16863]
Renamed 'core' to 'libgaim'
committer: Tailor Script <tailor@pidgin.im>
| author | Evan Schoenberg <evan.s@dreskin.net> |
|---|---|
| date | Sat, 19 Aug 2006 01:50:10 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 14191:009db0b357b5 | 14192:60b1bc8dbf37 |
|---|---|
| 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 */ | |
| 8 | |
| 9 /* system includes */ | |
| 10 #include <stdlib.h> | |
| 11 #include <stdio.h> | |
| 12 #include <unistd.h> | |
| 13 #include <sys/types.h> | |
| 14 #include <sys/stat.h> | |
| 15 #include <string.h> | |
| 16 #include <ctype.h> | |
| 17 | |
| 18 #include "account.h" | |
| 19 #include "config.h" | |
| 20 #include "core.h" | |
| 21 #include "conversation.h" | |
| 22 #include "debug.h" | |
| 23 #include "eventloop.h" | |
| 24 #include "internal.h" | |
| 25 #include "util.h" | |
| 26 #include "version.h" | |
| 27 | |
| 28 #define FILECTL_PLUGIN_ID "core-filectl" | |
| 29 static int check; | |
| 30 static time_t mtime; | |
| 31 | |
| 32 static void init_file(void); | |
| 33 static gboolean check_file(void); | |
| 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 | |
| 40 run_commands() | |
| 41 { | |
| 42 struct stat finfo; | |
| 43 char filename[256]; | |
| 44 char buffer[1024]; | |
| 45 char *command, *arg1, *arg2; | |
| 46 FILE *file; | |
| 47 | |
| 48 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); | |
| 49 | |
| 50 file = g_fopen(filename, "r+"); | |
| 51 while (fgets(buffer, sizeof(buffer), file)) { | |
| 52 | |
| 53 /* Read the next command */ | |
| 54 if (buffer[strlen(buffer) - 1] == '\n') | |
| 55 buffer[strlen(buffer) - 1] = 0; | |
| 56 gaim_debug_misc("filectl", "read: %s\n", buffer); | |
| 57 command = getarg(buffer, 0, 0); | |
| 58 | |
| 59 if (!strncasecmp(command, "login", 6)) { | |
| 60 GaimAccount *account; | |
| 61 | |
| 62 arg1 = getarg(buffer, 1, 0); | |
| 63 arg2 = getarg(buffer, 2, 1); | |
| 64 | |
| 65 account = gaim_accounts_find(arg1, arg2); | |
| 66 if (account != NULL) /* username found */ | |
| 67 gaim_account_connect(account); | |
| 68 | |
| 69 free(arg1); | |
| 70 free(arg2); | |
| 71 | |
| 72 } else if (!strncasecmp(command, "logout", 7)) { | |
| 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) | |
| 80 { | |
| 81 gaim_account_disconnect(account); | |
| 82 } | |
| 83 else if (arg1 == NULL) | |
| 84 gaim_connections_disconnect_all(); | |
| 85 | |
| 86 free(arg1); | |
| 87 free(arg2); | |
| 88 | |
| 89 /* gaim_find_conversation() is gone in 2.0.0. */ | |
| 90 #if 0 | |
| 91 } else if (!strncasecmp(command, "send", 4)) { | |
| 92 GaimConversation *conv; | |
| 93 | |
| 94 arg1 = getarg(buffer, 1, 0); | |
| 95 arg2 = getarg(buffer, 2, 1); | |
| 96 | |
| 97 conv = gaim_find_conversation(GAIM_CONV_TYPE_ANY, arg1); | |
| 98 if (conv != NULL) | |
| 99 { | |
| 100 /* | |
| 101 gaim_conversation_write(conv, arg2, WFLAG_SEND, NULL, time(NULL), -1); | |
| 102 serv_send_im(conv->gc, arg1, arg2, 0); | |
| 103 */ | |
| 104 } | |
| 105 | |
| 106 free(arg1); | |
| 107 free(arg2); | |
| 108 #endif | |
| 109 | |
| 110 } else if (!strncasecmp(command, "away", 4)) { | |
| 111 arg1 = getarg(buffer, 1, 1); | |
| 112 /* serv_set_away_all(arg1); */ | |
| 113 free(arg1); | |
| 114 | |
| 115 } else if (!strncasecmp(command, "hide", 4)) { | |
| 116 gaim_blist_set_visible(FALSE); | |
| 117 | |
| 118 } else if (!strncasecmp(command, "unhide", 6)) { | |
| 119 gaim_blist_set_visible(TRUE); | |
| 120 | |
| 121 } else if (!strncasecmp(command, "back", 4)) { | |
| 122 /* do_im_back(); */ | |
| 123 | |
| 124 } else if (!strncasecmp(command, "quit", 4)) { | |
| 125 gaim_core_quit(); | |
| 126 | |
| 127 } | |
| 128 | |
| 129 free(command); | |
| 130 } | |
| 131 | |
| 132 fclose(file); | |
| 133 | |
| 134 if (g_stat(filename, &finfo) != 0) | |
| 135 return; | |
| 136 mtime = finfo.st_mtime; | |
| 137 } | |
| 138 | |
| 139 /** | |
| 140 * Check to see if the size of the file is > 0. if so, run commands. | |
| 141 */ | |
| 142 void | |
| 143 init_file() | |
| 144 { | |
| 145 /* most of this was taken from Bash v2.04 by the FSF */ | |
| 146 struct stat finfo; | |
| 147 char filename[256]; | |
| 148 | |
| 149 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); | |
| 150 | |
| 151 if ((g_stat(filename, &finfo) == 0) && (finfo.st_size > 0)) | |
| 152 run_commands(); | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * Check to see if we need to run commands from the file. | |
| 157 */ | |
| 158 gboolean | |
| 159 check_file() | |
| 160 { | |
| 161 /* most of this was taken from Bash v2.04 by the FSF */ | |
| 162 struct stat finfo; | |
| 163 char filename[256]; | |
| 164 | |
| 165 sprintf(filename, "%s" G_DIR_SEPARATOR_S "control", gaim_user_dir()); | |
| 166 | |
| 167 if ((g_stat(filename, &finfo) == 0) && (finfo.st_size > 0)) | |
| 168 { | |
| 169 if (mtime != finfo.st_mtime) { | |
| 170 gaim_debug_info("filectl", "control changed, checking\n"); | |
| 171 run_commands(); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 return TRUE; | |
| 176 } | |
| 177 | |
| 178 char * | |
| 179 getarg(char *line, int which, int remain) | |
| 180 { | |
| 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 } | |
| 214 | |
| 215 /* | |
| 216 * EXPORTED FUNCTIONS | |
| 217 */ | |
| 218 | |
| 219 static gboolean | |
| 220 plugin_load(GaimPlugin *plugin) | |
| 221 { | |
| 222 init_file(); | |
| 223 check = gaim_timeout_add(5000, (GSourceFunc)check_file, NULL); | |
| 224 | |
| 225 return TRUE; | |
| 226 } | |
| 227 | |
| 228 static gboolean | |
| 229 plugin_unload(GaimPlugin *plugin) | |
| 230 { | |
| 231 gaim_timeout_remove(check); | |
| 232 | |
| 233 return TRUE; | |
| 234 } | |
| 235 | |
| 236 static GaimPluginInfo info = | |
| 237 { | |
| 238 GAIM_PLUGIN_MAGIC, | |
| 239 GAIM_MAJOR_VERSION, | |
| 240 GAIM_MINOR_VERSION, | |
| 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 */ | |
| 255 GAIM_WEBSITE, /**< homepage */ | |
| 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 | |
| 266 init_plugin(GaimPlugin *plugin) | |
| 267 { | |
| 268 } | |
| 269 | |
| 270 GAIM_INIT_PLUGIN(filectl, init_plugin, info) |
