Tue, 31 Jul 2001 23:23:40 +0000
[gaim-migrate @ 2100]
change ../config.h to <config.h> because that's better. change from GdkInput functions to GaimInput for reasons mentioned elsewhere.
| 106 | 1 | #define GAIM_PLUGINS |
| 2 | #include "gaim.h" | |
| 3 | ||
| 4 | #include <gtk/gtk.h> | |
| 5 | #include <stdlib.h> | |
| 6 | #include <unistd.h> | |
| 7 | #include <sys/types.h> | |
| 8 | #include <sys/stat.h> | |
| 9 | #include <string.h> | |
| 10 | #include <ctype.h> | |
| 11 | ||
|
179
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
12 | static void *handle; |
|
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
13 | static int check; |
|
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
14 | static time_t mtime; |
| 106 | 15 | |
|
179
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
16 | static void init_file(); |
|
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
17 | static void check_file(); |
| 106 | 18 | |
| 19 | extern void do_quit(); | |
| 20 | ||
| 21 | /* parse char * as if were word array */ | |
| 22 | char *getarg(char *, int, int); | |
| 23 | ||
| 24 | /* go through file and run any commands */ | |
| 25 | void run_commands() { | |
| 26 | struct stat finfo; | |
| 27 | char filename[256]; | |
| 28 | char buffer[1024]; | |
| 29 | char *command, *arg1, *arg2; | |
| 30 | FILE *file; | |
| 31 | ||
| 32 | sprintf(filename, "%s/.gaim/control", getenv("HOME")); | |
| 33 | ||
| 34 | file = fopen(filename, "r+"); | |
| 35 | while (fgets(buffer, sizeof buffer, file)) { | |
| 36 | if (buffer[strlen(buffer) - 1] == '\n') | |
| 37 | buffer[strlen(buffer) - 1] = 0; | |
|
179
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
38 | sprintf(debug_buff, "read: %s\n", buffer); |
|
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
39 | debug_print(debug_buff); |
| 106 | 40 | command = getarg(buffer, 0, 0); |
| 41 | if (!strncasecmp(command, "signon", 6)) { | |
| 42 | if (!blist) { | |
| 43 | show_login(); | |
| 44 | dologin(NULL, NULL); | |
| 45 | } | |
| 46 | } else if (!strncasecmp(command, "signoff", 7)) { | |
|
1047
783f8520d9a0
[gaim-migrate @ 1057]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
576
diff
changeset
|
47 | struct gaim_connection *gc = NULL; |
|
783f8520d9a0
[gaim-migrate @ 1057]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
576
diff
changeset
|
48 | arg1 = getarg(buffer, 1, 1); |
|
783f8520d9a0
[gaim-migrate @ 1057]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
576
diff
changeset
|
49 | if (arg1) gc = find_gaim_conn_by_name(arg1); |
|
783f8520d9a0
[gaim-migrate @ 1057]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
576
diff
changeset
|
50 | if (gc) signoff(gc); |
|
783f8520d9a0
[gaim-migrate @ 1057]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
576
diff
changeset
|
51 | else signoff_all(NULL, NULL); |
| 106 | 52 | } else if (!strncasecmp(command, "send", 4)) { |
| 53 | struct conversation *c; | |
| 54 | arg1 = getarg(buffer, 1, 0); | |
| 55 | arg2 = getarg(buffer, 2, 1); | |
| 56 | c = find_conversation(arg1); | |
| 57 | if (!c) c = new_conversation(arg1); | |
|
576
07c058b3e498
[gaim-migrate @ 586]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
179
diff
changeset
|
58 | write_to_conv(c, arg2, WFLAG_SEND, NULL); |
|
1047
783f8520d9a0
[gaim-migrate @ 1057]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
576
diff
changeset
|
59 | serv_send_im(c->gc, arg1, arg2, 0); |
| 106 | 60 | free(arg1); |
| 61 | free(arg2); | |
| 62 | } else if (!strncasecmp(command, "away", 4)) { | |
| 63 | struct away_message a; | |
| 64 | arg1 = getarg(buffer, 1, 1); | |
| 65 | snprintf(a.message, 2048, "%s", arg1); | |
| 66 | a.name[0] = 0; | |
| 67 | do_away_message(NULL, &a); | |
| 68 | free(arg1); | |
| 69 | } else if (!strncasecmp(command, "back", 4)) { | |
| 70 | do_im_back(); | |
| 71 | } else if (!strncasecmp(command, "quit", 4)) { | |
| 72 | do_quit(); | |
| 73 | } | |
| 74 | free(command); | |
| 75 | } | |
| 76 | ||
| 77 | fclose(file); | |
| 78 | ||
| 79 | if (stat (filename, &finfo) != 0) | |
| 80 | return; | |
| 81 | mtime = finfo.st_mtime; | |
| 82 | } | |
| 83 | ||
|
1047
783f8520d9a0
[gaim-migrate @ 1057]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
576
diff
changeset
|
84 | char *gaim_plugin_init(GModule *h) { |
| 106 | 85 | handle = h; |
| 86 | init_file(); | |
| 87 | check = gtk_timeout_add(5000, (GtkFunction)check_file, NULL); | |
| 88 | } | |
| 89 | ||
| 90 | void gaim_plugin_remove() { | |
| 91 | gtk_timeout_remove(check); | |
| 92 | } | |
| 93 | ||
| 94 | char *name() { | |
| 95 | return "Gaim File Control"; | |
| 96 | } | |
| 97 | ||
| 98 | char *description() { | |
| 99 | return "Allows you to control gaim by entering commands in a file."; | |
| 100 | } | |
| 101 | ||
| 102 | /* check to see if the size of the file is > 0. if so, run commands */ | |
| 103 | void init_file() { | |
| 104 | /* most of this was taken from Bash v2.04 by the FSF */ | |
| 105 | struct stat finfo; | |
| 106 | char file[256]; | |
| 107 | ||
| 108 | sprintf(file, "%s/.gaim/control", getenv("HOME")); | |
| 109 | ||
| 110 | if ((stat (file, &finfo) == 0) && (finfo.st_size > 0)) | |
| 111 | run_commands(); | |
| 112 | } | |
| 113 | ||
| 114 | /* check to see if we need to run commands from the file */ | |
| 115 | void check_file() { | |
| 116 | /* most of this was taken from Bash v2.04 by the FSF */ | |
| 117 | struct stat finfo; | |
| 118 | char file[256]; | |
| 119 | ||
| 120 | sprintf(file, "%s/.gaim/control", getenv("HOME")); | |
| 121 | ||
| 122 | if ((stat (file, &finfo) == 0) && (finfo.st_size > 0)) | |
|
179
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
123 | if (mtime != finfo.st_mtime) { |
|
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
124 | sprintf(debug_buff, "control changed, checking\n"); |
|
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
125 | debug_print(debug_buff); |
| 106 | 126 | run_commands(); |
|
179
3d1884b2ad14
[gaim-migrate @ 189]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
106
diff
changeset
|
127 | } |
| 106 | 128 | } |
| 129 | ||
| 130 | char *getarg(char *line, int which, int remain) { | |
| 131 | char *arr; | |
| 132 | char *val; | |
| 133 | int count = -1; | |
| 134 | int i; | |
| 135 | int state = 0; | |
| 136 | ||
| 137 | for (i = 0; i < strlen(line) && count < which; i++) { | |
| 138 | switch (state) { | |
| 139 | case 0: /* in whitespace, expecting word */ | |
| 140 | if (isalnum(line[i])) { | |
| 141 | count++; | |
| 142 | state = 1; | |
| 143 | } | |
| 144 | break; | |
| 145 | case 1: /* inside word, waiting for whitespace */ | |
| 146 | if (isspace(line[i])) { | |
| 147 | state = 0; | |
| 148 | } | |
| 149 | break; | |
| 150 | } | |
| 151 | } | |
| 152 | ||
| 153 | arr = strdup(&line[i - 1]); | |
| 154 | if (remain) | |
| 155 | return arr; | |
| 156 | ||
| 157 | for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++); | |
| 158 | arr[i] = 0; | |
| 159 | val = strdup(arr); | |
| 160 | arr[i] = ' '; | |
| 161 | free(arr); | |
| 162 | return val; | |
| 163 | } |