Mercurial > pidgin
annotate plugins/filectl.c @ 2352:39e66e80bb31
[gaim-migrate @ 2365]
what the hell?
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sun, 23 Sep 2001 20:32:30 +0000 |
parents | 9ab820049ede |
children | dfd9705bc750 |
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; |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
42 GList *userlist = aim_users; |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
43 arg1 = getarg(buffer, 1, 1); |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
44 if(arg1) { |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
45 while(userlist) { |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
46 struct aim_user *current = userlist->data; |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
47 if(!strcmp(current->username, arg1)) { |
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 } |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
55 if(u) /* username found */ |
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; |
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
59 arg1 = getarg(buffer, 1, 1); |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
60 if (arg1) { |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
61 gc = find_gaim_conn_by_name(arg1); |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
62 free(arg1); |
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
63 } |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
64 if (gc) signoff(gc); |
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
65 else signoff_all(NULL, NULL); |
106 | 66 } else if (!strncasecmp(command, "send", 4)) { |
67 struct conversation *c; | |
68 arg1 = getarg(buffer, 1, 0); | |
69 arg2 = getarg(buffer, 2, 1); | |
70 c = find_conversation(arg1); | |
71 if (!c) c = new_conversation(arg1); | |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
72 write_to_conv(c, arg2, WFLAG_SEND, NULL, time(NULL)); |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
73 serv_send_im(c->gc, arg1, arg2, 0); |
106 | 74 free(arg1); |
75 free(arg2); | |
76 } else if (!strncasecmp(command, "away", 4)) { | |
77 struct away_message a; | |
78 arg1 = getarg(buffer, 1, 1); | |
79 snprintf(a.message, 2048, "%s", arg1); | |
80 a.name[0] = 0; | |
81 do_away_message(NULL, &a); | |
82 free(arg1); | |
83 } else if (!strncasecmp(command, "back", 4)) { | |
84 do_im_back(); | |
85 } else if (!strncasecmp(command, "quit", 4)) { | |
86 do_quit(); | |
87 } | |
88 free(command); | |
89 } | |
90 | |
91 fclose(file); | |
92 | |
93 if (stat (filename, &finfo) != 0) | |
94 return; | |
95 mtime = finfo.st_mtime; | |
96 } | |
97 | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
576
diff
changeset
|
98 char *gaim_plugin_init(GModule *h) { |
106 | 99 handle = h; |
100 init_file(); | |
101 check = gtk_timeout_add(5000, (GtkFunction)check_file, NULL); | |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
102 return NULL; |
106 | 103 } |
104 | |
105 void gaim_plugin_remove() { | |
106 gtk_timeout_remove(check); | |
107 } | |
108 | |
109 char *name() { | |
110 return "Gaim File Control"; | |
111 } | |
112 | |
113 char *description() { | |
114 return "Allows you to control gaim by entering commands in a file."; | |
115 } | |
116 | |
117 /* check to see if the size of the file is > 0. if so, run commands */ | |
118 void init_file() { | |
119 /* most of this was taken from Bash v2.04 by the FSF */ | |
120 struct stat finfo; | |
121 char file[256]; | |
122 | |
123 sprintf(file, "%s/.gaim/control", getenv("HOME")); | |
124 | |
125 if ((stat (file, &finfo) == 0) && (finfo.st_size > 0)) | |
126 run_commands(); | |
127 } | |
128 | |
129 /* check to see if we need to run commands from the file */ | |
130 void check_file() { | |
131 /* most of this was taken from Bash v2.04 by the FSF */ | |
132 struct stat finfo; | |
133 char file[256]; | |
134 | |
135 sprintf(file, "%s/.gaim/control", getenv("HOME")); | |
136 | |
137 if ((stat (file, &finfo) == 0) && (finfo.st_size > 0)) | |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
138 if (mtime != finfo.st_mtime) { |
2324
9ab820049ede
[gaim-migrate @ 2334]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
139 debug_printf("control changed, checking\n"); |
106 | 140 run_commands(); |
179
8d8faeab01f6
[gaim-migrate @ 189]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
106
diff
changeset
|
141 } |
106 | 142 } |
143 | |
144 char *getarg(char *line, int which, int remain) { | |
145 char *arr; | |
146 char *val; | |
147 int count = -1; | |
148 int i; | |
149 int state = 0; | |
150 | |
151 for (i = 0; i < strlen(line) && count < which; i++) { | |
152 switch (state) { | |
153 case 0: /* in whitespace, expecting word */ | |
154 if (isalnum(line[i])) { | |
155 count++; | |
156 state = 1; | |
157 } | |
158 break; | |
159 case 1: /* inside word, waiting for whitespace */ | |
160 if (isspace(line[i])) { | |
161 state = 0; | |
162 } | |
163 break; | |
164 } | |
165 } | |
166 | |
167 arr = strdup(&line[i - 1]); | |
168 if (remain) | |
169 return arr; | |
170 | |
171 for (i = 0; i < strlen(arr) && isalnum(arr[i]); i++); | |
172 arr[i] = 0; | |
173 val = strdup(arr); | |
174 arr[i] = ' '; | |
175 free(arr); | |
176 return val; | |
177 } |