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