Mercurial > pidgin.yaz
comparison src/gaimrc.c @ 1:2846a03bda67
[gaim-migrate @ 10]
The other missing files :)
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Thu, 23 Mar 2000 03:13:54 +0000 |
parents | |
children | b9cc0a3a68b2 |
comparison
equal
deleted
inserted
replaced
0:a5ace2e037bc | 1:2846a03bda67 |
---|---|
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 { | |
149 char *c; | |
150 int mc; | |
151 | |
152 c = g_strdup(msg); | |
153 mc = 0; | |
154 while (*c) | |
155 { | |
156 if (*c == '\\') { | |
157 c++; | |
158 msg[mc] = *c; | |
159 } | |
160 else { | |
161 msg[mc] = *c; | |
162 } | |
163 mc++; | |
164 c++; | |
165 } | |
166 msg[mc] = 0; | |
167 } | |
168 | |
169 static void gaimrc_read_away(FILE *f) | |
170 { | |
171 struct parse *p; | |
172 char buf[4096]; | |
173 struct away_message *a; | |
174 | |
175 buf[0] = 0; | |
176 | |
177 while (buf[0] != '}') | |
178 { | |
179 if (!fgets(buf, sizeof(buf), f)) | |
180 return; | |
181 | |
182 if (buf[0] == '}') | |
183 return; | |
184 | |
185 p = parse_line(buf); | |
186 a = g_new0(struct away_message, 1); | |
187 | |
188 g_snprintf(a->name, sizeof(a->name), "%s", p->option); | |
189 g_snprintf(a->message, sizeof(a->message), "%s", p->value[0]); | |
190 filter_break(a->message); | |
191 away_messages = g_list_append(away_messages, a); | |
192 } | |
193 } | |
194 | |
195 static void gaimrc_write_away(FILE *f) | |
196 { | |
197 GList *awy = away_messages; | |
198 struct away_message *a; | |
199 | |
200 fprintf(f, "away {\n"); | |
201 | |
202 while (awy) { | |
203 a = (struct away_message *)awy->data; | |
204 // escape_text(a->name); | |
205 // escape_text(a->message); | |
206 fprintf(f, "\t%s { %s }\n", escape_text2(a->name), escape_text2(a->message)); | |
207 awy = awy->next; | |
208 } | |
209 | |
210 fprintf(f, "}\n"); | |
211 } | |
212 | |
213 | |
214 | |
215 | |
216 static struct aim_user *gaimrc_read_user(FILE *f) | |
217 { | |
218 struct parse *p; | |
219 struct aim_user *u; | |
220 char buf[4096]; | |
221 | |
222 if (!fgets(buf, sizeof(buf), f)) | |
223 return NULL; | |
224 | |
225 p = parse_line(buf); | |
226 | |
227 if (strcmp(p->option, "ident")) | |
228 return NULL; | |
229 | |
230 u = g_new0(struct aim_user, 1); | |
231 | |
232 strcpy(u->username, p->value[0]); | |
233 strcpy(u->password, p->value[1]); | |
234 | |
235 u->user_info[0] = 0; | |
236 | |
237 if (!fgets(buf, sizeof(buf), f)) | |
238 return u; | |
239 | |
240 if (strcmp(buf, "\t\tuser_info {\n")) { | |
241 return u; | |
242 } | |
243 | |
244 if (!fgets(buf, sizeof(buf), f)) | |
245 return u; | |
246 | |
247 while (strncmp(buf, "\t\t}", 3)) { | |
248 if (strlen(buf) > 3) | |
249 strcat(u->user_info, &buf[3]); | |
250 | |
251 if (!fgets(buf, sizeof(buf), f)) { | |
252 return u; | |
253 } | |
254 } | |
255 | |
256 return u; | |
257 | |
258 } | |
259 | |
260 static void gaimrc_write_user(FILE *f, struct aim_user *u) | |
261 { | |
262 char *c; | |
263 int nl = 1;; | |
264 fprintf(f, "\t\tident { %s } { %s }\n", u->username, u->password); | |
265 fprintf(f, "\t\tuser_info {"); | |
266 c = u->user_info; | |
267 while(*c) { | |
268 /* This is not as silly as it looks. */ | |
269 if (*c == '\n') { | |
270 nl++; | |
271 } else { | |
272 if (nl) { | |
273 while(nl) { | |
274 fprintf(f, "\n\t\t\t"); | |
275 nl--; | |
276 } | |
277 } | |
278 fprintf(f, "%c", *c); | |
279 } | |
280 c++; | |
281 } | |
282 fprintf(f, "\n\t\t}\n"); | |
283 | |
284 } | |
285 | |
286 | |
287 static void gaimrc_read_users(FILE *f) | |
288 { | |
289 char buf[2048]; | |
290 struct aim_user *u; | |
291 struct parse *p; | |
292 int cur = 0; | |
293 | |
294 buf[0] = 0; | |
295 | |
296 while (buf[0] != '}') { | |
297 if (buf[0] == '#') | |
298 continue; | |
299 | |
300 if (!fgets(buf, sizeof(buf), f)) | |
301 return; | |
302 | |
303 | |
304 | |
305 p = parse_line(buf); | |
306 | |
307 if (!strcmp(p->option, "current_user")) { | |
308 cur = 1;; | |
309 } else if (strcmp(p->option, "user")) { | |
310 continue; | |
311 } | |
312 | |
313 u = gaimrc_read_user(f); | |
314 | |
315 if (cur) | |
316 current_user = u; | |
317 | |
318 aim_users = g_list_append(aim_users, u); | |
319 } | |
320 } | |
321 | |
322 static void gaimrc_write_users(FILE *f) | |
323 { | |
324 GList *usr = aim_users; | |
325 struct aim_user *u; | |
326 | |
327 fprintf(f, "users {\n"); | |
328 | |
329 while(usr) { | |
330 u = (struct aim_user *)usr->data; | |
331 if (current_user == u) { | |
332 fprintf(f, "\tcurrent_user {\n"); | |
333 } else { | |
334 fprintf(f, "\tuser {\n"); | |
335 } | |
336 gaimrc_write_user(f, u); | |
337 | |
338 fprintf(f, "\t}\n"); | |
339 | |
340 usr = usr->next; | |
341 } | |
342 | |
343 fprintf(f, "}\n"); | |
344 } | |
345 | |
346 | |
347 | |
348 | |
349 static void gaimrc_read_options(FILE *f) | |
350 { | |
351 char buf[2048]; | |
352 struct parse *p; | |
353 | |
354 buf[0] = 0; | |
355 | |
356 while (buf[0] != '}') { | |
357 if (buf[0] == '#') | |
358 continue; | |
359 | |
360 if (!fgets(buf, sizeof(buf), f)) | |
361 return; | |
362 | |
363 p = parse_line(buf); | |
364 | |
365 if (!strcmp(p->option, "general_options")) { | |
366 general_options = atoi(p->value[0]); | |
367 } else if (!strcmp(p->option, "display_options")) { | |
368 display_options = atoi(p->value[0]); | |
369 } else if (!strcmp(p->option, "sound_options")) { | |
370 sound_options = atoi(p->value[0]); | |
371 } else if (!strcmp(p->option, "font_options")) { | |
372 font_options = atoi(p->value[0]); | |
373 } else if (!strcmp(p->option, "report_idle")) { | |
374 report_idle = atoi(p->value[0]); | |
375 } else if (!strcmp(p->option, "web_browser")) { | |
376 web_browser = atoi(p->value[0]); | |
377 } else if (!strcmp(p->option, "web_command")) { | |
378 strcpy(web_command, p->value[0]); | |
379 } else if (!strcmp(p->option, "proxy_type")) { | |
380 proxy_type = atoi(p->value[0]); | |
381 } else if (!strcmp(p->option, "proxy_host")) { | |
382 strcpy(proxy_host, p->value[0]); | |
383 } else if (!strcmp(p->option, "proxy_port")) { | |
384 proxy_port = atoi(p->value[0]); | |
385 } else if (!strcmp(p->option, "aim_host")) { | |
386 strcpy(aim_host, p->value[0]); | |
387 } else if (!strcmp(p->option, "aim_port")) { | |
388 aim_port = atoi(p->value[0]); | |
389 } else if (!strcmp(p->option, "login_host")) { | |
390 strcpy(login_host, p->value[0]); | |
391 } else if (!strcmp(p->option, "login_port")) { | |
392 login_port = atoi(p->value[0]); | |
393 } else if (!strcmp(p->option, "blist_pos")) { | |
394 blist_pos.x = atoi(p->value[0]); | |
395 blist_pos.y = atoi(p->value[1]); | |
396 blist_pos.width = atoi(p->value[2]); | |
397 blist_pos.height = atoi(p->value[3]); | |
398 blist_pos.xoff = atoi(p->value[4]); | |
399 blist_pos.yoff = atoi(p->value[5]); | |
400 } | |
401 | |
402 } | |
403 | |
404 } | |
405 | |
406 static void gaimrc_write_options(FILE *f) | |
407 { | |
408 | |
409 fprintf(f, "options {\n"); | |
410 fprintf(f, "\tgeneral_options { %d }\n", general_options); | |
411 fprintf(f, "\tdisplay_options { %d }\n", display_options); | |
412 fprintf(f, "\tsound_options { %d }\n", sound_options); | |
413 fprintf(f, "\tfont_options { %d }\n", font_options); | |
414 fprintf(f, "\treport_idle { %d }\n", report_idle); | |
415 fprintf(f, "\tweb_browser { %d }\n", web_browser); | |
416 fprintf(f, "\tweb_command { %s }\n", web_command); | |
417 fprintf(f, "\tproxy_type { %d }\n", proxy_type); | |
418 fprintf(f, "\tproxy_host { %s }\n", proxy_host); | |
419 fprintf(f, "\tproxy_port { %d }\n", proxy_port); | |
420 fprintf(f, "\taim_host { %s }\n", aim_host); | |
421 fprintf(f, "\taim_port { %d }\n", aim_port); | |
422 fprintf(f, "\tlogin_host { %s }\n", login_host); | |
423 fprintf(f, "\tlogin_port { %d }\n", login_port); | |
424 fprintf(f, "\tblist_pos { %d } { %d } { %d } { %d } { %d } { %d }\n", | |
425 blist_pos.x, blist_pos.y, blist_pos.width, blist_pos.height, | |
426 blist_pos.xoff, blist_pos.yoff); | |
427 fprintf(f, "}\n"); | |
428 } | |
429 | |
430 | |
431 void set_defaults() | |
432 { | |
433 general_options = | |
434 OPT_GEN_SEND_LINKS | | |
435 OPT_GEN_ENTER_SENDS | | |
436 OPT_GEN_SAVED_WINDOWS | | |
437 OPT_GEN_REMEMBER_PASS | | |
438 OPT_GEN_REGISTERED; | |
439 display_options = | |
440 OPT_DISP_SHOW_IDLETIME | | |
441 OPT_DISP_SHOW_TIME | | |
442 OPT_DISP_SHOW_PIXMAPS | | |
443 OPT_DISP_SHOW_BUTTON_XPM; | |
444 font_options = 0; | |
445 sound_options = OPT_SOUND_LOGIN | OPT_SOUND_LOGOUT | OPT_SOUND_RECV | OPT_SOUND_SEND; | |
446 report_idle = IDLE_GAIM; | |
447 web_browser = BROWSER_NETSCAPE; | |
448 proxy_type = PROXY_NONE; | |
449 | |
450 aim_port = TOC_PORT; | |
451 login_port = AUTH_PORT; | |
452 g_snprintf(aim_host, sizeof(aim_host), "%s", TOC_HOST); | |
453 g_snprintf(login_host, sizeof(login_host), "%s", AUTH_HOST); | |
454 proxy_host[0] = 0; | |
455 proxy_port = 0; | |
456 g_snprintf(web_command, sizeof(web_command), "xterm -e lynx %%s"); | |
457 blist_pos.width = 0; | |
458 blist_pos.height = 0; | |
459 blist_pos.x = 0; | |
460 blist_pos.y = 0; | |
461 blist_pos.xoff = 0; | |
462 blist_pos.yoff = 0; | |
463 } | |
464 | |
465 | |
466 void load_prefs() | |
467 { | |
468 FILE *f; | |
469 char buf[1024]; | |
470 int ver = 0; | |
471 | |
472 if (getenv("HOME")) { | |
473 g_snprintf(buf, sizeof(buf), "%s/.gaimrc", getenv("HOME")); | |
474 if ((f = fopen(buf,"r"))) { | |
475 fgets(buf, sizeof(buf), f); | |
476 sscanf(buf, "# .gaimrc v%d", &ver); | |
477 if ( (ver <= 0) || (buf[0] != '#')) { | |
478 fclose(f); | |
479 set_defaults(); | |
480 save_prefs(); | |
481 load_prefs(); | |
482 return; | |
483 } | |
484 while(!feof(f)) { | |
485 switch(gaimrc_parse_tag(f)) { | |
486 case -1: | |
487 /* Let the loop end, EOF*/ | |
488 break; | |
489 case 0: | |
490 gaimrc_read_users(f); | |
491 break; | |
492 case 1: | |
493 gaimrc_read_options(f); | |
494 break; | |
495 case 2: | |
496 gaimrc_read_away(f); | |
497 break; | |
498 default: | |
499 /* NOOP */ | |
500 break; | |
501 } | |
502 } | |
503 fclose(f); | |
504 } | |
505 } | |
506 | |
507 } | |
508 | |
509 void save_prefs() | |
510 { | |
511 FILE *f; | |
512 char buf[BUF_LONG]; | |
513 | |
514 if (getenv("HOME")) { | |
515 g_snprintf(buf, sizeof(buf), "%s/.gaimrc", getenv("HOME")); | |
516 if ((f = fopen(buf,"w"))) { | |
517 fprintf(f, "# .gaimrc v%d\n", 1); | |
518 gaimrc_write_users(f); | |
519 gaimrc_write_options(f); | |
520 gaimrc_write_away(f); | |
521 fclose(f); | |
522 chmod(buf, S_IRUSR | S_IWUSR); | |
523 } | |
524 | |
525 } | |
526 } | |
527 |