Mercurial > pidgin.yaz
annotate plugins/filectl.c @ 2912:152d726312b2
[gaim-migrate @ 2925]
eh
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Fri, 21 Dec 2001 23:46:41 +0000 |
parents | 538c58b43eff |
children | 9349de0706e6 |
rev | line source |
---|---|
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
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
12 static void *handle; |
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
13 static int check; |
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
14 static time_t mtime; |
106 | 15 |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
16 static void init_file(); |
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
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; | |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
38 debug_printf("read: %s\n", buffer); |
106 | 39 command = getarg(buffer, 0, 0); |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
40 if (!strncasecmp(command, "signon", 6)) { |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
41 struct aim_user *u = NULL; |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
42 GSList *userlist = aim_users; |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
43 arg1 = getarg(buffer, 1, 1); |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
44 if (arg1) { |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
45 while (userlist) { |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
46 struct aim_user *current = userlist->data; |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
47 if (!strcmp(current->username, arg1)) { |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
48 u = current; |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
49 break; |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
50 } |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
51 userlist = userlist->next; |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
52 } |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
53 free(arg1); |
106 | 54 } |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
55 if (u) /* username found */ |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
56 serv_login(u); |
106 | 57 } else if (!strncasecmp(command, "signoff", 7)) { |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
58 struct gaim_connection *gc = NULL; |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
59 GSList *c = connections; |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
60 arg1 = getarg(buffer, 1, 1); |
2766
0e082a9e4c32
[gaim-migrate @ 2779]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2765
diff
changeset
|
61 while (arg1 && c) { |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
62 gc = c->data; |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
63 if (!strcmp(gc->username, arg1)) { |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
64 break; |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
65 } |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
66 gc = NULL; |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
67 c = c->next; |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
68 } |
2765
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
69 if (gc) |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
70 signoff(gc); |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
71 else if (!arg1) |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
72 signoff_all(NULL, NULL); |
dfd9705bc750
[gaim-migrate @ 2778]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2324
diff
changeset
|
73 free(arg1); |
106 | 74 } else if (!strncasecmp(command, "send", 4)) { |
75 struct conversation *c; | |
76 arg1 = getarg(buffer, 1, 0); | |
77 arg2 = getarg(buffer, 2, 1); | |
78 c = find_conversation(arg1); | |
79 if (!c) c = new_conversation(arg1); | |
2906
538c58b43eff
[gaim-migrate @ 2919]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2766
diff
changeset
|
80 write_to_conv(c, arg2, WFLAG_SEND, NULL, time(NULL), -1); |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
81 serv_send_im(c->gc, arg1, arg2, 0); |
106 | 82 free(arg1); |
83 free(arg2); | |
84 } else if (!strncasecmp(command, "away", 4)) { | |
85 struct away_message a; | |
86 arg1 = getarg(buffer, 1, 1); | |
87 snprintf(a.message, 2048, "%s", arg1); | |
88 a.name[0] = 0; | |
89 do_away_message(NULL, &a); | |
90 free(arg1); | |
91 } else if (!strncasecmp(command, "back", 4)) { | |
92 do_im_back(); | |
93 } else if (!strncasecmp(command, "quit", 4)) { | |
94 do_quit(); | |
95 } | |
96 free(command); | |
97 } | |
98 | |
99 fclose(file); | |
100 | |
101 if (stat (filename, &finfo) != 0) | |
102 return; | |
103 mtime = finfo.st_mtime; | |
104 } | |
105 | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
106 char *gaim_plugin_init(GModule *h) { |
106 | 107 handle = h; |
108 init_file(); | |
109 check = gtk_timeout_add(5000, (GtkFunction)check_file, NULL); | |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
110 return NULL; |
106 | 111 } |
112 | |
113 void gaim_plugin_remove() { | |
114 gtk_timeout_remove(check); | |
115 } | |
116 | |
117 char *name() { | |
118 return "Gaim File Control"; | |
119 } | |
120 | |
121 char *description() { | |
122 return "Allows you to control gaim by entering commands in a file."; | |
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 */ | |
138 void check_file() { | |
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)) | |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
146 if (mtime != finfo.st_mtime) { |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
147 debug_printf("control changed, checking\n"); |
106 | 148 run_commands(); |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
149 } |
106 | 150 } |
151 | |
152 char *getarg(char *line, int which, int remain) { | |
153 char *arr; | |
154 char *val; | |
155 int count = -1; | |
156 int i; | |
157 int state = 0; | |
158 | |
159 for (i = 0; i < strlen(line) && count < which; i++) { | |
160 switch (state) { | |
161 case 0: /* in whitespace, expecting word */ | |
162 if (isalnum(line[i])) { | |
163 count++; | |
164 state = 1; | |
165 } | |
166 break; | |
167 case 1: /* inside word, waiting for whitespace */ | |
168 if (isspace(line[i])) { | |
169 state = 0; | |
170 } | |
171 break; | |
172 } | |
173 } | |
174 | |
175 arr = strdup(&line[i - 1]); | |
176 if (remain) | |
177 return arr; | |
178 | |
179 for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++); | |
180 arr[i] = 0; | |
181 val = strdup(arr); | |
182 arr[i] = ' '; | |
183 free(arr); | |
184 return val; | |
185 } |