comparison libpurple/proxy.c @ 32817:6fa0e854cfc6

Add support for reading GNOME3 proxy settings. Patch by Mihai Serban, with some small changes to remove extra variable parameters. committer: Elliott Sales de Andrade <qulogic@pidgin.im>
author mihai.serban@gmail.com
date Sat, 02 Jun 2012 02:27:41 +0000
parents 5ae7e1f36b43
children 2c6510167895
comparison
equal deleted inserted replaced
32814:626c339b60ae 32817:6fa0e854cfc6
220 purple_proxy_info_destroy(global_proxy_info); 220 purple_proxy_info_destroy(global_proxy_info);
221 221
222 global_proxy_info = info; 222 global_proxy_info = info;
223 } 223 }
224 224
225
226 /* index in gproxycmds below, keep them in sync */
227 #define GNOME_PROXY_MODE 0
228 #define GNOME_PROXY_USE_SAME_PROXY 1
229 #define GNOME_PROXY_SOCKS_HOST 2
230 #define GNOME_PROXY_SOCKS_PORT 3
231 #define GNOME_PROXY_HTTP_HOST 4
232 #define GNOME_PROXY_HTTP_PORT 5
233 #define GNOME_PROXY_HTTP_USER 6
234 #define GNOME_PROXY_HTTP_PASS 7
235 #define GNOME2_CMDS 0
236 #define GNOME3_CMDS 1
237
238 /* detect proxy settings for gnome2/gnome3 */
239 static const char* gproxycmds[][2] = {
240 { "gconftool-2 -g /system/proxy/mode" , "gsettings get org.gnome.system.proxy mode" },
241 { "gconftool-2 -g /system/http_proxy/use_same_proxy", "gsettings get org.gnome.system.proxy use-same-proxy" },
242 { "gconftool-2 -g /system/proxy/socks_host", "gsettings get org.gnome.system.proxy.socks host" },
243 { "gconftool-2 -g /system/proxy/socks_port", "gsettings get org.gnome.system.proxy.socks port" },
244 { "gconftool-2 -g /system/http_proxy/host", "gsettings get org.gnome.system.proxy.http host" },
245 { "gconftool-2 -g /system/http_proxy/port", "gsettings get org.gnome.system.proxy.http port"},
246 { "gconftool-2 -g /system/http_proxy/authentication_user", "gsettings get org.gnome.system.proxy.http authentication-user" },
247 { "gconftool-2 -g /system/http_proxy/authentication_password", "gsettings get org.gnome.system.proxy.http authentication-password" },
248 };
249
250 /**
251 * This is a utility function used to retrieve proxy parameter values from
252 * GNOME 2/3 environment.
253 *
254 * @param parameter One of the GNOME_PROXY_x constants defined above
255 * @param gnome_version GNOME2_CMDS or GNOME3_CMDS
256 *
257 * @return The value of requested proxy parameter
258 */
259 static char *
260 purple_gnome_proxy_get_parameter(guint8 parameter, guint8 gnome_version)
261 {
262 gchar *param, *err;
263 size_t param_len;
264
265 if (parameter > GNOME_PROXY_HTTP_PASS)
266 return NULL;
267 if (gnome_version > GNOME3_CMDS)
268 return NULL;
269
270 if (!g_spawn_command_line_sync(gproxycmds[parameter][gnome_version],
271 &param, &err, NULL, NULL))
272 return NULL;
273 g_free(err);
274
275 g_strstrip(param);
276 if (param[0] == '\'' || param[0] == '\"') {
277 param_len = strlen(param);
278 memmove(param, param + 1, param_len); /* copy last \0 too */
279 --param_len;
280 if (param_len > 0 && (param[param_len - 1] == '\'' || param[param_len - 1] == '\"'))
281 param[param_len - 1] = '\0';
282 g_strstrip(param);
283 }
284
285 return param;
286 }
287
225 static PurpleProxyInfo * 288 static PurpleProxyInfo *
226 purple_gnome_proxy_get_info(void) 289 purple_gnome_proxy_get_info(void)
227 { 290 {
228 static PurpleProxyInfo info = {0, NULL, 0, NULL, NULL}; 291 static PurpleProxyInfo info = {0, NULL, 0, NULL, NULL};
229 gboolean use_same_proxy = FALSE; 292 gboolean use_same_proxy = FALSE;
230 gchar *tmp, *err = NULL; 293 gchar *tmp;
231 294 guint8 gnome_version = GNOME3_CMDS;
232 tmp = g_find_program_in_path("gconftool-2"); 295
296 tmp = g_find_program_in_path("gsettings");
297 if (tmp == NULL) {
298 tmp = g_find_program_in_path("gconftool-2");
299 gnome_version = GNOME2_CMDS;
300 }
233 if (tmp == NULL) 301 if (tmp == NULL)
234 return purple_global_proxy_get_info(); 302 return purple_global_proxy_get_info();
235 303
236 g_free(tmp); 304 g_free(tmp);
237 tmp = NULL;
238 305
239 /* Check whether to use a proxy. */ 306 /* Check whether to use a proxy. */
240 if (!g_spawn_command_line_sync("gconftool-2 -g /system/proxy/mode", 307 tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_MODE, gnome_version);
241 &tmp, &err, NULL, NULL)) 308 if (!tmp)
242 return purple_global_proxy_get_info(); 309 return purple_global_proxy_get_info();
243 g_free(err); 310
244 err = NULL; 311 if (purple_strequal(tmp, "none")) {
245
246 if (purple_strequal(tmp, "none\n")) {
247 info.type = PURPLE_PROXY_NONE; 312 info.type = PURPLE_PROXY_NONE;
248 g_free(tmp); 313 g_free(tmp);
249 return &info; 314 return &info;
250 } 315 }
251 316
252 if (!purple_strequal(tmp, "manual\n")) { 317 if (!purple_strequal(tmp, "manual")) {
253 /* Unknown setting. Fallback to using our global proxy settings. */ 318 /* Unknown setting. Fallback to using our global proxy settings. */
254 g_free(tmp); 319 g_free(tmp);
255 return purple_global_proxy_get_info(); 320 return purple_global_proxy_get_info();
256 } 321 }
257 322
258 g_free(tmp); 323 g_free(tmp);
259 tmp = NULL;
260 324
261 /* Free the old fields */ 325 /* Free the old fields */
262 if (info.host) { 326 if (info.host) {
263 g_free(info.host); 327 g_free(info.host);
264 info.host = NULL; 328 info.host = NULL;
270 if (info.password) { 334 if (info.password) {
271 g_free(info.password); 335 g_free(info.password);
272 info.password = NULL; 336 info.password = NULL;
273 } 337 }
274 338
275 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/use_same_proxy", 339 tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_USE_SAME_PROXY, gnome_version);
276 &tmp, &err, NULL, NULL)) 340 if (!tmp)
277 return purple_global_proxy_get_info(); 341 return purple_global_proxy_get_info();
278 g_free(err); 342
279 err = NULL; 343 if (purple_strequal(tmp, "true"))
280
281 if (purple_strequal(tmp, "true\n"))
282 use_same_proxy = TRUE; 344 use_same_proxy = TRUE;
345
283 g_free(tmp); 346 g_free(tmp);
284 tmp = NULL;
285 347
286 if (!use_same_proxy) { 348 if (!use_same_proxy) {
287 if (!g_spawn_command_line_sync("gconftool-2 -g /system/proxy/socks_host", 349 info.host = purple_gnome_proxy_get_parameter(GNOME_PROXY_SOCKS_HOST, gnome_version);
288 &info.host, &err, NULL, NULL)) 350 if (!info.host)
289 return purple_global_proxy_get_info(); 351 return purple_global_proxy_get_info();
290 g_free(err); 352 }
291 err = NULL;
292 }
293
294 if(info.host != NULL)
295 g_strchomp(info.host);
296 353
297 if (!use_same_proxy && (info.host != NULL) && (*info.host != '\0')) { 354 if (!use_same_proxy && (info.host != NULL) && (*info.host != '\0')) {
298 info.type = PURPLE_PROXY_SOCKS5; 355 info.type = PURPLE_PROXY_SOCKS5;
299 if (!g_spawn_command_line_sync("gconftool-2 -g /system/proxy/socks_port", 356 tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_SOCKS_PORT, gnome_version);
300 &tmp, &err, NULL, NULL)) 357 if (!tmp) {
301 {
302 g_free(info.host); 358 g_free(info.host);
303 info.host = NULL; 359 info.host = NULL;
304 return purple_global_proxy_get_info(); 360 return purple_global_proxy_get_info();
305 } 361 }
306 g_free(err);
307 info.port = atoi(tmp); 362 info.port = atoi(tmp);
308 g_free(tmp); 363 g_free(tmp);
309 } else { 364 } else {
310 g_free(info.host); 365 g_free(info.host);
311 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/host", 366 info.host = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_HOST, gnome_version);
312 &info.host, &err, NULL, NULL)) 367 if (!info.host)
313 return purple_global_proxy_get_info(); 368 return purple_global_proxy_get_info();
314 g_free(err);
315 err = NULL;
316 369
317 /* If we get this far then we know we're using an HTTP proxy */ 370 /* If we get this far then we know we're using an HTTP proxy */
318 info.type = PURPLE_PROXY_HTTP; 371 info.type = PURPLE_PROXY_HTTP;
319 372
320 g_strchomp(info.host);
321 if (*info.host == '\0') 373 if (*info.host == '\0')
322 { 374 {
323 purple_debug_info("proxy", "Gnome proxy settings are set to " 375 purple_debug_info("proxy", "Gnome proxy settings are set to "
324 "'manual' but no suitable proxy server is specified. Using " 376 "'manual' but no suitable proxy server is specified. Using "
325 "Pidgin's proxy settings instead.\n"); 377 "Pidgin's proxy settings instead.\n");
326 g_free(info.host); 378 g_free(info.host);
327 info.host = NULL; 379 info.host = NULL;
328 return purple_global_proxy_get_info(); 380 return purple_global_proxy_get_info();
329 } 381 }
330 382
331 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/authentication_user", 383 info.username = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_USER, gnome_version);
332 &info.username, &err, NULL, NULL)) 384 if (!info.username)
333 { 385 {
334 g_free(info.host); 386 g_free(info.host);
335 info.host = NULL; 387 info.host = NULL;
336 return purple_global_proxy_get_info(); 388 return purple_global_proxy_get_info();
337 } 389 }
338 g_free(err); 390
339 err = NULL; 391 info.password = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_PASS, gnome_version);
340 g_strchomp(info.username); 392 if (!info.password)
341
342 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/authentication_password",
343 &info.password, &err, NULL, NULL))
344 { 393 {
345 g_free(info.host); 394 g_free(info.host);
346 info.host = NULL; 395 info.host = NULL;
347 g_free(info.username); 396 g_free(info.username);
348 info.username = NULL; 397 info.username = NULL;
349 return purple_global_proxy_get_info(); 398 return purple_global_proxy_get_info();
350 } 399 }
351 g_free(err); 400
352 err = NULL; 401 tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_PORT, gnome_version);
353 g_strchomp(info.password); 402 if (!tmp)
354
355 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/port",
356 &tmp, &err, NULL, NULL))
357 { 403 {
358 g_free(info.host); 404 g_free(info.host);
359 info.host = NULL; 405 info.host = NULL;
360 g_free(info.username); 406 g_free(info.username);
361 info.username = NULL; 407 info.username = NULL;
362 g_free(info.password); 408 g_free(info.password);
363 info.password = NULL; 409 info.password = NULL;
364 return purple_global_proxy_get_info(); 410 return purple_global_proxy_get_info();
365 } 411 }
366 g_free(err);
367 info.port = atoi(tmp); 412 info.port = atoi(tmp);
368 g_free(tmp); 413 g_free(tmp);
369 } 414 }
370 415
371 return &info; 416 return &info;