1
|
1 /*
|
|
2 * gaim
|
|
3 *
|
|
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 */
|
|
21
|
|
22 #include <string.h>
|
|
23 #include <sys/time.h>
|
|
24
|
|
25 #include <sys/types.h>
|
|
26 #include <sys/stat.h>
|
|
27
|
|
28 #include <unistd.h>
|
|
29 #include <stdio.h>
|
|
30 #include <stdlib.h>
|
|
31 #include <gtk/gtk.h>
|
|
32 #include "gaim.h"
|
|
33 #include "proxy.h"
|
|
34
|
|
35
|
|
36 struct aim_user *current_user = NULL;
|
|
37 GList *aim_users = NULL;
|
|
38 int general_options;
|
|
39 int display_options;
|
|
40 int sound_options;
|
|
41 int font_options;
|
|
42
|
|
43 int report_idle, web_browser;
|
|
44 struct save_pos blist_pos;
|
|
45 char web_command[2048];
|
|
46 char aim_host[512];
|
|
47 int aim_port;
|
|
48 char login_host[512];
|
|
49 int login_port;
|
|
50
|
|
51
|
|
52 struct parse {
|
|
53 char option[256];
|
|
54 char value[6][256];
|
|
55 };
|
|
56
|
|
57 static struct parse *parse_line(char *line)
|
|
58 {
|
|
59 char *c = line;
|
|
60 int inopt = 1, inval = 0, curval = -1;
|
|
61 int optlen = 0, vallen = 0;
|
|
62 static struct parse p;
|
|
63
|
|
64
|
|
65 while(*c) {
|
|
66 if (*c == '\t') {
|
|
67 c++;
|
|
68 continue;
|
|
69 }
|
|
70 if (inopt) {
|
|
71 // if ((*c < 'a' || *c > 'z') && *c != '_') {
|
|
72 if ((*c < 'a' || *c > 'z') && *c != '_' && (*c < 'A' || *c > 'Z')) {
|
|
73 inopt = 0;
|
|
74 p.option[optlen] = 0;
|
|
75 c++;
|
|
76 continue;
|
|
77 }
|
|
78
|
|
79 p.option[optlen] = *c;
|
|
80 optlen++;
|
|
81 c++;
|
|
82 continue;
|
|
83 } else if (inval) {
|
|
84 if ( (*c == '}') ) {
|
|
85 if (*(c-1) == '\\') {
|
|
86 p.value[curval][vallen - 1] = *c;
|
|
87 c++;
|
|
88 continue;
|
|
89 } else {
|
|
90 p.value[curval][vallen - 1] = 0;
|
|
91 inval = 0;
|
|
92 c++;
|
|
93 continue;
|
|
94 }
|
|
95 } else {
|
|
96 p.value[curval][vallen] = *c;
|
|
97 vallen++;
|
|
98 c++;
|
|
99 continue; }
|
|
100 } else if (*c == '{') {
|
|
101 if (*(c-1) == '\\') {
|
|
102 p.value[curval][vallen-1] = *c;
|
|
103 c++;
|
|
104 continue;
|
|
105 }
|
|
106 else
|
|
107 {
|
|
108 curval++;
|
|
109 vallen = 0;
|
|
110 inval = 1;
|
|
111 c++;
|
|
112 c++;
|
|
113 continue;
|
|
114 }
|
|
115 }
|
|
116 c++;
|
|
117 }
|
|
118 return &p;
|
|
119 }
|
|
120
|
|
121
|
|
122 static int gaimrc_parse_tag(FILE *f)
|
|
123 {
|
|
124 char buf[2048];
|
|
125 char tag[256];
|
|
126 buf[0] = '#';
|
|
127
|
|
128 while (buf[0] == '#' && !feof(f))
|
|
129 fgets(buf, sizeof(buf), f);
|
|
130
|
|
131 if (feof(f))
|
|
132 return -1;
|
|
133
|
|
134 sscanf(buf, "%s {", tag);
|
|
135
|
|
136 if (!strcmp(tag, "users")) {
|
|
137 return 0;
|
|
138 } else if (!strcmp(tag, "options")) {
|
|
139 return 1;
|
|
140 } else if (!strcmp(tag, "away")) {
|
|
141 return 2;
|
|
142 }
|
|
143
|
|
144 return -1;
|
|
145 }
|
|
146
|
|
147 void filter_break(char *msg)
|
|
148 {
|
44
|
149 char *c;
|
|
150 int mc;
|
|
151 int cc;
|
|
152
|
|
153 c = g_malloc(strlen(msg)+1);
|
|
154 strcpy(c, msg);
|
1
|
155
|
44
|
156 mc = 0;
|
|
157 cc = 0;
|
|
158 while (c[cc] != '\0')
|
|
159 {
|
|
160 if (c[cc] == '\\') {
|
|
161 cc++;
|
|
162 msg[mc] = c[cc];
|
|
163 }
|
|
164 else {
|
|
165 msg[mc] = c[cc];
|
|
166 }
|
|
167 mc++;
|
|
168 cc++;
|
|
169 }
|
|
170 msg[mc] = 0;
|
|
171 g_free(c);
|
1
|
172 }
|
|
173
|
44
|
174
|
1
|
175 static void gaimrc_read_away(FILE *f)
|
|
176 {
|
|
177 struct parse *p;
|
|
178 char buf[4096];
|
|
179 struct away_message *a;
|
|
180
|
|
181 buf[0] = 0;
|
|
182
|
|
183 while (buf[0] != '}')
|
|
184 {
|
|
185 if (!fgets(buf, sizeof(buf), f))
|
|
186 return;
|
|
187
|
|
188 if (buf[0] == '}')
|
|
189 return;
|
|
190
|
|
191 p = parse_line(buf);
|
15
|
192 if (!strcmp(p->option, "message"))
|
|
193 {
|
|
194 a = g_new0(struct away_message, 1);
|
1
|
195
|
15
|
196 g_snprintf(a->name, sizeof(a->name), "%s", p->value[0]);
|
|
197 g_snprintf(a->message, sizeof(a->message), "%s", p->value[1]);
|
|
198 filter_break(a->message);
|
|
199 away_messages = g_list_append(away_messages, a);
|
|
200 }
|
1
|
201 }
|
|
202 }
|
|
203
|
|
204 static void gaimrc_write_away(FILE *f)
|
|
205 {
|
|
206 GList *awy = away_messages;
|
|
207 struct away_message *a;
|
|
208
|
|
209 fprintf(f, "away {\n");
|
|
210
|
|
211 while (awy) {
|
26
|
212 char *str1, *str2;
|
|
213
|
1
|
214 a = (struct away_message *)awy->data;
|
26
|
215
|
|
216 str1 = escape_text2(a->name);
|
|
217 str2 = escape_text2(a->message);
|
|
218
|
|
219 fprintf(f, "\tmessage { %s } { %s }\n", str1, str2);
|
|
220
|
|
221 /* escape_text2 uses malloc(), so we don't want to g_free these */
|
|
222 free(str1);
|
|
223 free(str2);
|
|
224
|
1
|
225 awy = awy->next;
|
|
226 }
|
|
227
|
|
228 fprintf(f, "}\n");
|
|
229 }
|
|
230
|
|
231
|
|
232
|
|
233
|
|
234 static struct aim_user *gaimrc_read_user(FILE *f)
|
|
235 {
|
|
236 struct parse *p;
|
|
237 struct aim_user *u;
|
|
238 char buf[4096];
|
|
239
|
|
240 if (!fgets(buf, sizeof(buf), f))
|
|
241 return NULL;
|
|
242
|
|
243 p = parse_line(buf);
|
|
244
|
|
245 if (strcmp(p->option, "ident"))
|
|
246 return NULL;
|
|
247
|
|
248 u = g_new0(struct aim_user, 1);
|
|
249
|
|
250 strcpy(u->username, p->value[0]);
|
|
251 strcpy(u->password, p->value[1]);
|
|
252
|
|
253 u->user_info[0] = 0;
|
|
254
|
|
255 if (!fgets(buf, sizeof(buf), f))
|
|
256 return u;
|
|
257
|
|
258 if (strcmp(buf, "\t\tuser_info {\n")) {
|
|
259 return u;
|
|
260 }
|
|
261
|
|
262 if (!fgets(buf, sizeof(buf), f))
|
|
263 return u;
|
|
264
|
|
265 while (strncmp(buf, "\t\t}", 3)) {
|
|
266 if (strlen(buf) > 3)
|
|
267 strcat(u->user_info, &buf[3]);
|
|
268
|
|
269 if (!fgets(buf, sizeof(buf), f)) {
|
|
270 return u;
|
|
271 }
|
|
272 }
|
|
273
|
|
274 return u;
|
|
275
|
|
276 }
|
|
277
|
|
278 static void gaimrc_write_user(FILE *f, struct aim_user *u)
|
|
279 {
|
|
280 char *c;
|
|
281 int nl = 1;;
|
|
282 fprintf(f, "\t\tident { %s } { %s }\n", u->username, u->password);
|
|
283 fprintf(f, "\t\tuser_info {");
|
|
284 c = u->user_info;
|
|
285 while(*c) {
|
|
286 /* This is not as silly as it looks. */
|
|
287 if (*c == '\n') {
|
|
288 nl++;
|
|
289 } else {
|
|
290 if (nl) {
|
|
291 while(nl) {
|
|
292 fprintf(f, "\n\t\t\t");
|
|
293 nl--;
|
|
294 }
|
|
295 }
|
|
296 fprintf(f, "%c", *c);
|
|
297 }
|
|
298 c++;
|
|
299 }
|
|
300 fprintf(f, "\n\t\t}\n");
|
|
301
|
|
302 }
|
|
303
|
|
304
|
|
305 static void gaimrc_read_users(FILE *f)
|
|
306 {
|
|
307 char buf[2048];
|
|
308 struct aim_user *u;
|
|
309 struct parse *p;
|
|
310 int cur = 0;
|
|
311
|
|
312 buf[0] = 0;
|
|
313
|
|
314 while (buf[0] != '}') {
|
|
315 if (buf[0] == '#')
|
|
316 continue;
|
|
317
|
|
318 if (!fgets(buf, sizeof(buf), f))
|
|
319 return;
|
|
320
|
|
321
|
|
322
|
|
323 p = parse_line(buf);
|
|
324
|
|
325 if (!strcmp(p->option, "current_user")) {
|
|
326 cur = 1;;
|
|
327 } else if (strcmp(p->option, "user")) {
|
|
328 continue;
|
|
329 }
|
|
330
|
|
331 u = gaimrc_read_user(f);
|
|
332
|
|
333 if (cur)
|
|
334 current_user = u;
|
|
335
|
|
336 aim_users = g_list_append(aim_users, u);
|
|
337 }
|
|
338 }
|
|
339
|
|
340 static void gaimrc_write_users(FILE *f)
|
|
341 {
|
|
342 GList *usr = aim_users;
|
|
343 struct aim_user *u;
|
|
344
|
|
345 fprintf(f, "users {\n");
|
|
346
|
|
347 while(usr) {
|
|
348 u = (struct aim_user *)usr->data;
|
|
349 if (current_user == u) {
|
|
350 fprintf(f, "\tcurrent_user {\n");
|
|
351 } else {
|
|
352 fprintf(f, "\tuser {\n");
|
|
353 }
|
|
354 gaimrc_write_user(f, u);
|
|
355
|
|
356 fprintf(f, "\t}\n");
|
|
357
|
|
358 usr = usr->next;
|
|
359 }
|
|
360
|
|
361 fprintf(f, "}\n");
|
|
362 }
|
|
363
|
|
364
|
|
365
|
|
366
|
|
367 static void gaimrc_read_options(FILE *f)
|
|
368 {
|
|
369 char buf[2048];
|
|
370 struct parse *p;
|
|
371
|
|
372 buf[0] = 0;
|
|
373
|
|
374 while (buf[0] != '}') {
|
|
375 if (buf[0] == '#')
|
|
376 continue;
|
|
377
|
|
378 if (!fgets(buf, sizeof(buf), f))
|
|
379 return;
|
|
380
|
|
381 p = parse_line(buf);
|
|
382
|
|
383 if (!strcmp(p->option, "general_options")) {
|
|
384 general_options = atoi(p->value[0]);
|
|
385 } else if (!strcmp(p->option, "display_options")) {
|
|
386 display_options = atoi(p->value[0]);
|
|
387 } else if (!strcmp(p->option, "sound_options")) {
|
|
388 sound_options = atoi(p->value[0]);
|
|
389 } else if (!strcmp(p->option, "font_options")) {
|
|
390 font_options = atoi(p->value[0]);
|
|
391 } else if (!strcmp(p->option, "report_idle")) {
|
|
392 report_idle = atoi(p->value[0]);
|
|
393 } else if (!strcmp(p->option, "web_browser")) {
|
|
394 web_browser = atoi(p->value[0]);
|
|
395 } else if (!strcmp(p->option, "web_command")) {
|
|
396 strcpy(web_command, p->value[0]);
|
|
397 } else if (!strcmp(p->option, "proxy_type")) {
|
|
398 proxy_type = atoi(p->value[0]);
|
|
399 } else if (!strcmp(p->option, "proxy_host")) {
|
|
400 strcpy(proxy_host, p->value[0]);
|
|
401 } else if (!strcmp(p->option, "proxy_port")) {
|
|
402 proxy_port = atoi(p->value[0]);
|
|
403 } else if (!strcmp(p->option, "aim_host")) {
|
|
404 strcpy(aim_host, p->value[0]);
|
|
405 } else if (!strcmp(p->option, "aim_port")) {
|
|
406 aim_port = atoi(p->value[0]);
|
|
407 } else if (!strcmp(p->option, "login_host")) {
|
|
408 strcpy(login_host, p->value[0]);
|
|
409 } else if (!strcmp(p->option, "login_port")) {
|
|
410 login_port = atoi(p->value[0]);
|
|
411 } else if (!strcmp(p->option, "blist_pos")) {
|
|
412 blist_pos.x = atoi(p->value[0]);
|
|
413 blist_pos.y = atoi(p->value[1]);
|
|
414 blist_pos.width = atoi(p->value[2]);
|
|
415 blist_pos.height = atoi(p->value[3]);
|
|
416 blist_pos.xoff = atoi(p->value[4]);
|
|
417 blist_pos.yoff = atoi(p->value[5]);
|
|
418 }
|
|
419
|
|
420 }
|
|
421
|
|
422 }
|
|
423
|
|
424 static void gaimrc_write_options(FILE *f)
|
|
425 {
|
|
426
|
|
427 fprintf(f, "options {\n");
|
|
428 fprintf(f, "\tgeneral_options { %d }\n", general_options);
|
|
429 fprintf(f, "\tdisplay_options { %d }\n", display_options);
|
|
430 fprintf(f, "\tsound_options { %d }\n", sound_options);
|
|
431 fprintf(f, "\tfont_options { %d }\n", font_options);
|
|
432 fprintf(f, "\treport_idle { %d }\n", report_idle);
|
|
433 fprintf(f, "\tweb_browser { %d }\n", web_browser);
|
|
434 fprintf(f, "\tweb_command { %s }\n", web_command);
|
|
435 fprintf(f, "\tproxy_type { %d }\n", proxy_type);
|
|
436 fprintf(f, "\tproxy_host { %s }\n", proxy_host);
|
|
437 fprintf(f, "\tproxy_port { %d }\n", proxy_port);
|
|
438 fprintf(f, "\taim_host { %s }\n", aim_host);
|
|
439 fprintf(f, "\taim_port { %d }\n", aim_port);
|
|
440 fprintf(f, "\tlogin_host { %s }\n", login_host);
|
|
441 fprintf(f, "\tlogin_port { %d }\n", login_port);
|
|
442 fprintf(f, "\tblist_pos { %d } { %d } { %d } { %d } { %d } { %d }\n",
|
|
443 blist_pos.x, blist_pos.y, blist_pos.width, blist_pos.height,
|
|
444 blist_pos.xoff, blist_pos.yoff);
|
|
445 fprintf(f, "}\n");
|
|
446 }
|
|
447
|
|
448
|
|
449 void set_defaults()
|
|
450 {
|
|
451 general_options =
|
|
452 OPT_GEN_SEND_LINKS |
|
|
453 OPT_GEN_ENTER_SENDS |
|
|
454 OPT_GEN_SAVED_WINDOWS |
|
|
455 OPT_GEN_REMEMBER_PASS |
|
|
456 OPT_GEN_REGISTERED;
|
|
457 display_options =
|
|
458 OPT_DISP_SHOW_IDLETIME |
|
|
459 OPT_DISP_SHOW_TIME |
|
|
460 OPT_DISP_SHOW_PIXMAPS |
|
|
461 OPT_DISP_SHOW_BUTTON_XPM;
|
|
462 font_options = 0;
|
|
463 sound_options = OPT_SOUND_LOGIN | OPT_SOUND_LOGOUT | OPT_SOUND_RECV | OPT_SOUND_SEND;
|
|
464 report_idle = IDLE_GAIM;
|
|
465 web_browser = BROWSER_NETSCAPE;
|
|
466 proxy_type = PROXY_NONE;
|
|
467
|
|
468 aim_port = TOC_PORT;
|
|
469 login_port = AUTH_PORT;
|
|
470 g_snprintf(aim_host, sizeof(aim_host), "%s", TOC_HOST);
|
|
471 g_snprintf(login_host, sizeof(login_host), "%s", AUTH_HOST);
|
|
472 proxy_host[0] = 0;
|
|
473 proxy_port = 0;
|
|
474 g_snprintf(web_command, sizeof(web_command), "xterm -e lynx %%s");
|
|
475 blist_pos.width = 0;
|
|
476 blist_pos.height = 0;
|
|
477 blist_pos.x = 0;
|
|
478 blist_pos.y = 0;
|
|
479 blist_pos.xoff = 0;
|
|
480 blist_pos.yoff = 0;
|
|
481 }
|
|
482
|
|
483
|
|
484 void load_prefs()
|
|
485 {
|
|
486 FILE *f;
|
|
487 char buf[1024];
|
|
488 int ver = 0;
|
|
489
|
|
490 if (getenv("HOME")) {
|
|
491 g_snprintf(buf, sizeof(buf), "%s/.gaimrc", getenv("HOME"));
|
|
492 if ((f = fopen(buf,"r"))) {
|
|
493 fgets(buf, sizeof(buf), f);
|
|
494 sscanf(buf, "# .gaimrc v%d", &ver);
|
15
|
495 if ( (ver <= 1) || (buf[0] != '#')) {
|
1
|
496 fclose(f);
|
|
497 set_defaults();
|
|
498 save_prefs();
|
|
499 load_prefs();
|
|
500 return;
|
|
501 }
|
|
502 while(!feof(f)) {
|
|
503 switch(gaimrc_parse_tag(f)) {
|
|
504 case -1:
|
|
505 /* Let the loop end, EOF*/
|
|
506 break;
|
|
507 case 0:
|
|
508 gaimrc_read_users(f);
|
|
509 break;
|
|
510 case 1:
|
|
511 gaimrc_read_options(f);
|
|
512 break;
|
|
513 case 2:
|
|
514 gaimrc_read_away(f);
|
|
515 break;
|
|
516 default:
|
|
517 /* NOOP */
|
|
518 break;
|
|
519 }
|
|
520 }
|
|
521 fclose(f);
|
|
522 }
|
|
523 }
|
|
524
|
|
525 }
|
|
526
|
|
527 void save_prefs()
|
|
528 {
|
|
529 FILE *f;
|
|
530 char buf[BUF_LONG];
|
|
531
|
|
532 if (getenv("HOME")) {
|
|
533 g_snprintf(buf, sizeof(buf), "%s/.gaimrc", getenv("HOME"));
|
|
534 if ((f = fopen(buf,"w"))) {
|
15
|
535 fprintf(f, "# .gaimrc v%d\n", 2);
|
1
|
536 gaimrc_write_users(f);
|
|
537 gaimrc_write_options(f);
|
|
538 gaimrc_write_away(f);
|
|
539 fclose(f);
|
|
540 chmod(buf, S_IRUSR | S_IWUSR);
|
|
541 }
|
|
542
|
|
543 }
|
|
544 }
|
|
545
|