plugins/filectl.c

changeset 106
420c81f47ca3
child 179
3d1884b2ad14
equal deleted inserted replaced
105:ed9cd76f7d19 106:420c81f47ca3
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
12 void *handle;
13 int check;
14 time_t mtime;
15
16 void init_file();
17 void check_file();
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;
39 printf("read: %s\n", buffer);
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)) {
47 signoff();
48 } else if (!strncasecmp(command, "send", 4)) {
49 struct conversation *c;
50 arg1 = getarg(buffer, 1, 0);
51 arg2 = getarg(buffer, 2, 1);
52 c = find_conversation(arg1);
53 if (!c) c = new_conversation(arg1);
54 write_to_conv(c, arg2, WFLAG_SEND);
55 serv_send_im(arg1, arg2, 0);
56 free(arg1);
57 free(arg2);
58 } else if (!strncasecmp(command, "away", 4)) {
59 struct away_message a;
60 arg1 = getarg(buffer, 1, 1);
61 snprintf(a.message, 2048, "%s", arg1);
62 a.name[0] = 0;
63 do_away_message(NULL, &a);
64 free(arg1);
65 } else if (!strncasecmp(command, "back", 4)) {
66 do_im_back();
67 } else if (!strncasecmp(command, "quit", 4)) {
68 do_quit();
69 }
70 free(command);
71 }
72
73 fclose(file);
74
75 if (stat (filename, &finfo) != 0)
76 return;
77 mtime = finfo.st_mtime;
78 }
79
80 void gaim_plugin_init(void *h) {
81 handle = h;
82 init_file();
83 check = gtk_timeout_add(5000, (GtkFunction)check_file, NULL);
84 }
85
86 void gaim_plugin_remove() {
87 gtk_timeout_remove(check);
88 }
89
90 char *name() {
91 return "Gaim File Control";
92 }
93
94 char *description() {
95 return "Allows you to control gaim by entering commands in a file.";
96 }
97
98 /* check to see if the size of the file is > 0. if so, run commands */
99 void init_file() {
100 /* most of this was taken from Bash v2.04 by the FSF */
101 struct stat finfo;
102 char file[256];
103
104 sprintf(file, "%s/.gaim/control", getenv("HOME"));
105
106 if ((stat (file, &finfo) == 0) && (finfo.st_size > 0))
107 run_commands();
108 }
109
110 /* check to see if we need to run commands from the file */
111 void check_file() {
112 /* most of this was taken from Bash v2.04 by the FSF */
113 struct stat finfo;
114 char file[256];
115
116 sprintf(file, "%s/.gaim/control", getenv("HOME"));
117
118 if ((stat (file, &finfo) == 0) && (finfo.st_size > 0))
119 if (mtime != finfo.st_mtime)
120 run_commands();
121 }
122
123 char *getarg(char *line, int which, int remain) {
124 char *arr;
125 char *val;
126 int count = -1;
127 int i;
128 int state = 0;
129
130 for (i = 0; i < strlen(line) && count < which; i++) {
131 switch (state) {
132 case 0: /* in whitespace, expecting word */
133 if (isalnum(line[i])) {
134 count++;
135 state = 1;
136 }
137 break;
138 case 1: /* inside word, waiting for whitespace */
139 if (isspace(line[i])) {
140 state = 0;
141 }
142 break;
143 }
144 }
145
146 arr = strdup(&line[i - 1]);
147 if (remain)
148 return arr;
149
150 for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++);
151 arr[i] = 0;
152 val = strdup(arr);
153 arr[i] = ' ';
154 free(arr);
155 return val;
156 }

mercurial