comparison src/gaimrc.c @ 4137:22875a399312

[gaim-migrate @ 4355] this patch from Robert Krawitz (rlk) should fix some of the proxy preference mess committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Wed, 25 Dec 2002 00:51:00 +0000
parents b1c9eef8fa12
children 56e07e5ccd9a
comparison
equal deleted inserted replaced
4136:0b9487eb5e0a 4137:22875a399312
55 guint chat_options; 55 guint chat_options;
56 guint font_options; 56 guint font_options;
57 guint sound_options; 57 guint sound_options;
58 guint away_options; 58 guint away_options;
59 guint away_resend; 59 guint away_resend;
60 static guint is_loading_prefs = 0;
61 static guint request_save_prefs = 0;
62 static guint is_saving_prefs = 0;
63 static guint request_load_prefs = 0;
64 guint proxy_info_is_from_gaimrc = 1; /* Only save proxy info if it
65 * was loaded from the file
66 * or otherwise explicitly requested */
60 67
61 int report_idle; 68 int report_idle;
62 int web_browser; 69 int web_browser;
63 struct save_pos blist_pos; 70 struct save_pos blist_pos;
64 struct window_size conv_size, buddy_chat_size; 71 struct window_size conv_size, buddy_chat_size;
75 82
76 static struct parse *parse_line(char *line, struct parse *p) 83 static struct parse *parse_line(char *line, struct parse *p)
77 { 84 {
78 char *c = line; 85 char *c = line;
79 int inopt = 1, inval = 0, curval = -1; 86 int inopt = 1, inval = 0, curval = -1;
80 int optlen = 0, vallen = 0; 87 int optlen = 0, vallen = 0, last_non_space = 0;
81 int x; 88 int x;
82 89
83 for (x = 0; x < MAX_VALUES; x++) { 90 for (x = 0; x < MAX_VALUES; x++) {
84 p->value[x][0] = 0; 91 p->value[x][0] = 0;
85 } 92 }
107 /* if we have a \ take the char after it literally.. */ 114 /* if we have a \ take the char after it literally.. */
108 c++; 115 c++;
109 p->value[curval][vallen] = *c; 116 p->value[curval][vallen] = *c;
110 117
111 vallen++; 118 vallen++;
119 last_non_space = vallen;
112 c++; 120 c++;
113 continue; 121 continue;
114 } else if (*c == '}') { 122 } else if (*c == '}') {
115 /* } that isn't escaped should end this chunk of data, and 123 /* } that isn't escaped should end this chunk of data, and
116 * should have a space before it.. */ 124 * should have a space before it.. */
117 p->value[curval][vallen - 1] = 0; 125 p->value[curval][last_non_space] = 0;
118 inval = 0; 126 inval = 0;
119 c++; 127 c++;
120 continue; 128 continue;
121 } else { 129 } else {
122 p->value[curval][vallen] = *c; 130 p->value[curval][vallen] = *c;
123 131
124 vallen++; 132 vallen++;
133 if (! isspace(*c))
134 last_non_space = vallen;
125 c++; 135 c++;
126 continue; 136 continue;
127 } 137 }
128 } else if (*c == '{') { 138 } else if (*c == '{') {
129 /* i really don't think this if ever succeeds, but i'm 139 /* i really don't think this if ever succeeds, but i'm
130 * not brave enough to take it out... */ 140 * not brave enough to take it out... */
131 if (*(c - 1) == '\\') { 141 if (*(c - 1) == '\\') {
132 p->value[curval][vallen - 1] = *c; 142 p->value[curval][vallen] = *c;
133 c++; 143 c++;
134 continue; 144 continue;
135 } else { 145 } else {
136 /* { that isn't escaped should signify the start of a 146 /* { that isn't escaped should signify the start of a
137 * piece of data and should have a space after it.. */ 147 * piece of data and should have a space after it.. */
138 curval++; 148 curval++;
139 vallen = 0; 149 vallen = 0;
150 last_non_space = vallen;
140 inval = 1; 151 inval = 1;
141 c++; 152 c++;
142 c++; 153 while (*c && isspace(*c))
154 c++;
143 continue; 155 continue;
144 } 156 }
145 } 157 }
146 c++; 158 c++;
147 } 159 }
502 if (!fgets(buf, sizeof(buf), f)) 514 if (!fgets(buf, sizeof(buf), f))
503 return u; 515 return u;
504 516
505 while (strncmp(buf, "\t\t}", 3)) { 517 while (strncmp(buf, "\t\t}", 3)) {
506 if (strlen(buf) > 3) 518 if (strlen(buf) > 3)
507 strcat(u->user_info, &buf[3]); 519 strcat(u->user_info, buf + 3);
508 520
509 if (!fgets(buf, sizeof(buf), f)) { 521 if (!fgets(buf, sizeof(buf), f)) {
510 return u; 522 return u;
511 } 523 }
512 } 524 }
962 char user[128]; 974 char user[128];
963 char pass[128]; 975 char pass[128];
964 int port = 0; 976 int port = 0;
965 int len = 0; 977 int len = 0;
966 978
979 host[0] = '\0';
980 user[0] = '\0';
981 pass[0] = '\0';
982
983 debug_printf("gaimrc_parse_proxy_uri(%s)\n", proxy);
984
967 if ((c = strchr(proxy, ':')) == NULL) 985 if ((c = strchr(proxy, ':')) == NULL)
968 { 986 {
987 debug_printf("no uri detected\n");
969 /* No URI detected. */ 988 /* No URI detected. */
970 return FALSE; 989 return FALSE;
971 } 990 }
972 991
973 len = c - proxy; 992 len = c - proxy;
974 993
975 if (!strncmp(proxy, "http", len)) 994 if (!strncmp(proxy, "http://", len + 3))
976 proxytype = PROXY_HTTP; 995 proxytype = PROXY_HTTP;
977 else 996 else
978 return FALSE; 997 return FALSE;
979 998
999 debug_printf("found http proxy\n");
980 /* Get past "://" */ 1000 /* Get past "://" */
981 c += 3; 1001 c += 3;
1002
1003 debug_printf("looking at %s\n", c);
982 1004
983 for (;;) 1005 for (;;)
984 { 1006 {
985 *buffer = '\0'; 1007 *buffer = '\0';
986 d = buffer; 1008 d = buffer;
995 /* 1017 /*
996 * If there is a '@' in there somewhere, we are in the auth part. 1018 * If there is a '@' in there somewhere, we are in the auth part.
997 * If not, host. 1019 * If not, host.
998 */ 1020 */
999 if (strchr(c, '@') != NULL) 1021 if (strchr(c, '@') != NULL)
1000 strcmp(user, buffer); 1022 strcpy(user, buffer);
1001 else 1023 else
1002 strcmp(host, buffer); 1024 strcpy(host, buffer);
1003 } 1025 }
1004 else if (*c == '@') 1026 else if (*c == '@')
1005 { 1027 {
1006 if (user == NULL) 1028 if (user[0] == '\0')
1007 strcmp(user, buffer); 1029 strcpy(user, buffer);
1008 else 1030 else
1009 strcmp(pass, buffer); 1031 strcpy(pass, buffer);
1010 } 1032 }
1011 else if (*c == '/' || *c == '\0') 1033 else if (*c == '/' || *c == '\0')
1012 { 1034 {
1013 if (host == NULL) 1035 if (host[0] == '\0')
1014 strcmp(host, buffer); 1036 strcpy(host, buffer);
1015 else 1037 else
1016 port = atoi(buffer); 1038 port = atoi(buffer);
1017 1039
1018 /* Done. */ 1040 /* Done. */
1019 break; 1041 break;
1021 1043
1022 c++; 1044 c++;
1023 } 1045 }
1024 1046
1025 /* NOTE: HTTP_PROXY takes precendence. */ 1047 /* NOTE: HTTP_PROXY takes precendence. */
1026 if (host) 1048 if (host[0])
1027 strcpy(proxyhost, host); 1049 strcpy(proxyhost, host);
1028 else 1050 else
1029 *proxyhost = '\0'; 1051 *proxyhost = '\0';
1030 1052
1031 if (user) 1053 if (user[0])
1032 strcpy(proxyuser, user); 1054 strcpy(proxyuser, user);
1033 else 1055 else
1034 *proxyuser = '\0'; 1056 *proxyuser = '\0';
1035 1057
1036 if (pass) 1058 if (pass[0])
1037 strcpy(proxypass, pass); 1059 strcpy(proxypass, pass);
1038 else 1060 else
1039 *proxypass = '\0'; 1061 *proxypass = '\0';
1040 1062
1041 proxyport = port; 1063 proxyport = port;
1042 1064
1065 debug_printf("host '%s'\nuser '%s'\npassword '%s'\nport %d\n",
1066 proxyhost, proxyuser, proxypass, proxyport);
1067
1043 return TRUE; 1068 return TRUE;
1044 } 1069 }
1045 1070
1046 static void gaimrc_read_proxy(FILE *f) 1071 static void gaimrc_read_proxy(FILE *f)
1047 { 1072 {
1049 struct parse parse_buffer; 1074 struct parse parse_buffer;
1050 struct parse *p; 1075 struct parse *p;
1051 1076
1052 buf[0] = 0; 1077 buf[0] = 0;
1053 proxyhost[0] = 0; 1078 proxyhost[0] = 0;
1079 debug_printf("gaimrc_read_proxy\n");
1054 1080
1055 while (buf[0] != '}') { 1081 while (buf[0] != '}') {
1056 if (buf[0] == '#') 1082 if (buf[0] == '#')
1057 continue; 1083 continue;
1058 1084
1060 return; 1086 return;
1061 1087
1062 p = parse_line(buf, &parse_buffer); 1088 p = parse_line(buf, &parse_buffer);
1063 1089
1064 if (!strcmp(p->option, "host")) { 1090 if (!strcmp(p->option, "host")) {
1065 g_snprintf(proxyhost, sizeof(proxyhost), "%s", p->value[0]); 1091 g_snprintf(proxyhost, sizeof(proxyhost), "%s",
1092 p->value[0]);
1093 debug_printf("set proxyhost %s\n", proxyhost);
1066 } else if (!strcmp(p->option, "port")) { 1094 } else if (!strcmp(p->option, "port")) {
1067 proxyport = atoi(p->value[0]); 1095 proxyport = atoi(p->value[0]);
1068 } else if (!strcmp(p->option, "type")) { 1096 } else if (!strcmp(p->option, "type")) {
1069 proxytype = atoi(p->value[0]); 1097 proxytype = atoi(p->value[0]);
1070 } else if (!strcmp(p->option, "user")) { 1098 } else if (!strcmp(p->option, "user")) {
1071 g_snprintf(proxyuser, sizeof(proxyuser), "%s", p->value[0]); 1099 g_snprintf(proxyuser, sizeof(proxyuser), "%s",
1100 p->value[0]);
1072 } else if (!strcmp(p->option, "pass")) { 1101 } else if (!strcmp(p->option, "pass")) {
1073 g_snprintf(proxypass, sizeof(proxypass), "%s", p->value[0]); 1102 g_snprintf(proxypass, sizeof(proxypass), "%s",
1074 } 1103 p->value[0]);
1075 } 1104 }
1076 if (!proxyhost[0]) { 1105 }
1106 if (proxyhost[0])
1107 proxy_info_is_from_gaimrc = 1;
1108 else if (!proxyhost[0]) {
1109
1077 gboolean getVars = TRUE; 1110 gboolean getVars = TRUE;
1111 proxy_info_is_from_gaimrc = 0;
1078 1112
1079 if (g_getenv("HTTP_PROXY")) 1113 if (g_getenv("HTTP_PROXY"))
1080 g_snprintf(proxyhost, sizeof(proxyhost), "%s", g_getenv("HTTP_PROXY")); 1114 g_snprintf(proxyhost, sizeof(proxyhost), "%s",
1115 g_getenv("HTTP_PROXY"));
1081 else if (g_getenv("http_proxy")) 1116 else if (g_getenv("http_proxy"))
1082 g_snprintf(proxyhost, sizeof(proxyhost), "%s", g_getenv("http_proxy")); 1117 g_snprintf(proxyhost, sizeof(proxyhost), "%s",
1118 g_getenv("http_proxy"));
1083 else if (g_getenv("HTTPPROXY")) 1119 else if (g_getenv("HTTPPROXY"))
1084 g_snprintf(proxyhost, sizeof(proxyhost), "%s", g_getenv("HTTPPROXY")); 1120 g_snprintf(proxyhost, sizeof(proxyhost), "%s",
1121 g_getenv("HTTPPROXY"));
1085 1122
1086 if (*proxyhost != '\0') 1123 if (*proxyhost != '\0')
1087 getVars = !gaimrc_parse_proxy_uri(proxyhost); 1124 getVars = !gaimrc_parse_proxy_uri(proxyhost);
1088 1125
1089 if (getVars) 1126 if (getVars)
1094 proxyport = atoi(g_getenv("http_proxy_port")); 1131 proxyport = atoi(g_getenv("http_proxy_port"));
1095 else if (g_getenv("HTTPPROXYPORT")) 1132 else if (g_getenv("HTTPPROXYPORT"))
1096 proxyport = atoi(g_getenv("HTTPPROXYPORT")); 1133 proxyport = atoi(g_getenv("HTTPPROXYPORT"));
1097 1134
1098 if (g_getenv("HTTP_PROXY_USER")) 1135 if (g_getenv("HTTP_PROXY_USER"))
1099 g_snprintf(proxyuser, sizeof(proxyuser), "%s", g_getenv("HTTP_PROXY_USER")); 1136 g_snprintf(proxyuser, sizeof(proxyuser), "%s",
1137 g_getenv("HTTP_PROXY_USER"));
1100 else if (g_getenv("http_proxy_user")) 1138 else if (g_getenv("http_proxy_user"))
1101 g_snprintf(proxyuser, sizeof(proxyuser), "%s", g_getenv("http_proxy_user")); 1139 g_snprintf(proxyuser, sizeof(proxyuser), "%s",
1140 g_getenv("http_proxy_user"));
1102 else if (g_getenv("HTTPPROXYUSER")) 1141 else if (g_getenv("HTTPPROXYUSER"))
1103 g_snprintf(proxyuser, sizeof(proxyuser), "%s", g_getenv("HTTPPROXYUSER")); 1142 g_snprintf(proxyuser, sizeof(proxyuser), "%s",
1143 g_getenv("HTTPPROXYUSER"));
1104 1144
1105 if (g_getenv("HTTP_PROXY_PASS")) 1145 if (g_getenv("HTTP_PROXY_PASS"))
1106 g_snprintf(proxypass, sizeof(proxypass), "%s", g_getenv("HTTP_PROXY_PASS")); 1146 g_snprintf(proxypass, sizeof(proxypass), "%s",
1147 g_getenv("HTTP_PROXY_PASS"));
1107 else if (g_getenv("http_proxy_pass")) 1148 else if (g_getenv("http_proxy_pass"))
1108 g_snprintf(proxypass, sizeof(proxypass), "%s", g_getenv("http_proxy_pass")); 1149 g_snprintf(proxypass, sizeof(proxypass), "%s",
1150 g_getenv("http_proxy_pass"));
1109 else if (g_getenv("HTTPPROXYPASS")) 1151 else if (g_getenv("HTTPPROXYPASS"))
1110 g_snprintf(proxypass, sizeof(proxypass), "%s", g_getenv("HTTPPROXYPASS")); 1152 g_snprintf(proxypass, sizeof(proxypass), "%s",
1153 g_getenv("HTTPPROXYPASS"));
1111 1154
1112 1155
1113 if (proxyhost[0]) 1156 if (proxyhost[0])
1114 proxytype = PROXY_HTTP; 1157 proxytype = PROXY_HTTP;
1115 } 1158 }
1119 static void gaimrc_write_proxy(FILE *f) 1162 static void gaimrc_write_proxy(FILE *f)
1120 { 1163 {
1121 char *str; 1164 char *str;
1122 1165
1123 fprintf(f, "proxy {\n"); 1166 fprintf(f, "proxy {\n");
1124 fprintf(f, "\thost { %s }\n", proxyhost); 1167 if (proxy_info_is_from_gaimrc) {
1125 fprintf(f, "\tport { %d }\n", proxyport); 1168 fprintf(f, "\thost { %s }\n", proxyhost);
1126 fprintf(f, "\ttype { %d }\n", proxytype); 1169 fprintf(f, "\tport { %d }\n", proxyport);
1127 fprintf(f, "\tuser { %s }\n", proxyuser); 1170 fprintf(f, "\ttype { %d }\n", proxytype);
1128 fprintf(f, "\tpass { %s }\n", (str = escape_text2(proxypass))); 1171 fprintf(f, "\tuser { %s }\n", proxyuser);
1129 free(str); 1172 fprintf(f, "\tpass { %s }\n", (str = escape_text2(proxypass)));
1173 free(str);
1174 } else {
1175 fprintf(f, "\thost { %s }\n", "");
1176 fprintf(f, "\tport { %d }\n", 0);
1177 fprintf(f, "\ttype { %d }\n", 0);
1178 fprintf(f, "\tuser { %s }\n", "");
1179 fprintf(f, "\tpass { %s }\n", "");
1180 }
1130 fprintf(f, "}\n"); 1181 fprintf(f, "}\n");
1131 } 1182 }
1132 1183
1133 static void set_defaults() 1184 static void set_defaults()
1134 { 1185 {
1222 void load_prefs() 1273 void load_prefs()
1223 { 1274 {
1224 FILE *f; 1275 FILE *f;
1225 char buf[1024]; 1276 char buf[1024];
1226 int ver = 0; 1277 int ver = 0;
1278 debug_printf("load_prefs\n");
1279 if (is_saving_prefs) {
1280 request_load_prefs = 1;
1281 debug_printf("currently saving, will request load\n");
1282 return;
1283 }
1227 1284
1228 if (opt_rcfile_arg) 1285 if (opt_rcfile_arg)
1229 g_snprintf(buf, sizeof(buf), "%s", opt_rcfile_arg); 1286 g_snprintf(buf, sizeof(buf), "%s", opt_rcfile_arg);
1230 else if (gaim_home_dir()) 1287 else if (gaim_home_dir())
1231 g_snprintf(buf, sizeof(buf), "%s" G_DIR_SEPARATOR_S ".gaimrc", gaim_home_dir()); 1288 g_snprintf(buf, sizeof(buf), "%s" G_DIR_SEPARATOR_S ".gaimrc", gaim_home_dir());
1233 set_defaults(); 1290 set_defaults();
1234 return; 1291 return;
1235 } 1292 }
1236 1293
1237 if ((f = fopen(buf, "r"))) { 1294 if ((f = fopen(buf, "r"))) {
1295 is_loading_prefs = 1;
1296 debug_printf("start load_prefs\n");
1238 fgets(buf, sizeof(buf), f); 1297 fgets(buf, sizeof(buf), f);
1239 sscanf(buf, "# .gaimrc v%d", &ver); 1298 sscanf(buf, "# .gaimrc v%d", &ver);
1240 if ((ver <= 3) || (buf[0] != '#')) 1299 if ((ver <= 3) || (buf[0] != '#'))
1241 set_defaults(); 1300 set_defaults();
1242 1301
1243 while (!feof(f)) { 1302 while (!feof(f)) {
1244 switch (gaimrc_parse_tag(f)) { 1303 int tag = gaimrc_parse_tag(f);
1304 debug_printf("starting read tag %d\n", tag);
1305 switch (tag) {
1245 case -1: 1306 case -1:
1246 /* Let the loop end, EOF */ 1307 /* Let the loop end, EOF */
1247 break; 1308 break;
1248 case 0: 1309 case 0:
1249 gaimrc_read_users(f); 1310 gaimrc_read_users(f);
1270 break; 1331 break;
1271 default: 1332 default:
1272 /* NOOP */ 1333 /* NOOP */
1273 break; 1334 break;
1274 } 1335 }
1336 debug_printf("ending read tag %d\n", tag);
1275 } 1337 }
1276 fclose(f); 1338 fclose(f);
1339 is_loading_prefs = 0;
1340 debug_printf("end load_prefs\n");
1341 if (request_save_prefs) {
1342 debug_printf("saving prefs on request\n");
1343 save_prefs();
1344 request_save_prefs = 0;
1345 }
1277 } else if (opt_rcfile_arg) { 1346 } else if (opt_rcfile_arg) {
1278 g_snprintf(buf, sizeof(buf), _("Could not open config file %s."), opt_rcfile_arg); 1347 g_snprintf(buf, sizeof(buf), _("Could not open config file %s."), opt_rcfile_arg);
1279 do_error_dialog(buf, NULL, GAIM_ERROR); 1348 do_error_dialog(buf, NULL, GAIM_ERROR);
1280 set_defaults(); 1349 set_defaults();
1281 } else { 1350 } else {
1286 1355
1287 void save_prefs() 1356 void save_prefs()
1288 { 1357 {
1289 FILE *f; 1358 FILE *f;
1290 char buf[BUF_LONG]; 1359 char buf[BUF_LONG];
1360 debug_printf("enter save_prefs\n");
1361 if (is_loading_prefs) {
1362 request_save_prefs = 1;
1363 debug_printf("currently loading, will request save\n");
1364 return;
1365 }
1291 1366
1292 if (opt_rcfile_arg) { 1367 if (opt_rcfile_arg) {
1293 g_snprintf(buf, sizeof(buf), "%s", opt_rcfile_arg); 1368 g_snprintf(buf, sizeof(buf), "%s", opt_rcfile_arg);
1294 } 1369 }
1295 else if (gaim_home_dir()) { 1370 else if (gaim_home_dir()) {
1297 } 1372 }
1298 else { 1373 else {
1299 return; 1374 return;
1300 } 1375 }
1301 if ((f = fopen(buf, "w"))) { 1376 if ((f = fopen(buf, "w"))) {
1377 is_saving_prefs = 1;
1302 fprintf(f, "# .gaimrc v%d\n", 4); 1378 fprintf(f, "# .gaimrc v%d\n", 4);
1303 gaimrc_write_users(f); 1379 gaimrc_write_users(f);
1304 gaimrc_write_options(f); 1380 gaimrc_write_options(f);
1305 gaimrc_write_sounds(f); 1381 gaimrc_write_sounds(f);
1306 gaimrc_write_away(f); 1382 gaimrc_write_away(f);
1310 #endif 1386 #endif
1311 gaimrc_write_proxy(f); 1387 gaimrc_write_proxy(f);
1312 fclose(f); 1388 fclose(f);
1313 1389
1314 chmod(buf, S_IRUSR | S_IWUSR); 1390 chmod(buf, S_IRUSR | S_IWUSR);
1391 is_saving_prefs = 0;
1315 } 1392 }
1316 else 1393 else
1317 debug_printf("Error opening .gaimrc\n"); 1394 debug_printf("Error opening .gaimrc\n");
1395 if (request_load_prefs) {
1396 debug_printf("loading prefs on request\n");
1397 load_prefs();
1398 request_load_prefs = 0;
1399 }
1400 debug_printf("exit save_prefs\n");
1318 } 1401 }
1319 1402
1320 1403
1321 1404
1322 /* This function is called by g_slist_insert_sorted to compare the item 1405 /* This function is called by g_slist_insert_sorted to compare the item