it builds! (note that some autogenerated files are hardcoded now, and you can't build...
[k8lowj.git] / src / conf.c
blob799ed84a9dc0b8902e499fff36ab15b843069ee0
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
7 #include "glib-all.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
14 #include "liblj/livejournal.h"
16 #include "conf_xml.h"
17 #include "conf.h"
18 #include "account.h"
20 Configuration conf;
21 Application app;
23 #define PATH_BUF_SIZE 1024
25 JamHost*
26 conf_host_by_name(Configuration *c, const char *hostname) {
27 GSList *l;
28 for (l = c->hosts; l != NULL; l = l->next) {
29 if (strcmp(hostname, ((JamHost*)l->data)->name) == 0) {
30 return l->data;
33 return NULL;
36 gboolean
37 conf_verify_dir(void) {
38 return verify_dir(app.conf_dir, NULL);
41 void
42 conf_verify_a_host_exists() {
43 if (conf.hosts == NULL) {
44 /* make a default host. */
45 LJServer *s = lj_server_new("http://www.livejournal.com");
46 JamHost *host = (JamHost*)jam_host_lj_new(s);
47 host->name = g_strdup("LiveJournal.com");
48 conf.hosts = g_slist_append(conf.hosts, host);
52 void
53 conf_make_path(char *file, char *buf, int len) {
54 char *path;
55 path = g_build_filename(app.conf_dir, file, NULL);
56 strncpy(buf, path, len);
57 g_free(path);
60 char*
61 conf_make_account_path(JamAccount *acc, const char *path) {
62 return g_build_filename(app.conf_dir,
63 "servers", jam_account_get_host(acc)->name,
64 "users", jam_account_get_username(acc),
65 path ? path : NULL,
66 NULL);
69 gboolean
70 conf_rename_host(JamHost *host, const char *newname, GError **err) {
71 char *oldpath, *newpath;
73 /* disallow:
74 * [empty string]
75 * .
76 * ..
77 * ./../foo
78 * /foo
79 * allow:
80 * .lAmE.sErVeR.
82 if ((newname[0] == 0) ||
83 (newname[0] == '.' &&
84 (newname[1] == '.' || newname[1] == '/' || newname[1] == 0)) ||
85 (newname[0] == '/')) {
86 g_set_error(err, 0, 0, _("new host name is invalid"));
87 return FALSE;
89 oldpath = g_build_filename(app.conf_dir, "servers", host->name, NULL);
90 if (!g_file_test(oldpath, G_FILE_TEST_EXISTS)) {
91 string_replace(&host->name, g_strdup(newname));
92 g_free(oldpath);
93 return TRUE;
95 newpath = g_build_filename(app.conf_dir, "servers", newname, NULL);
96 if (rename(oldpath, newpath) < 0) {
97 g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errno),
98 _("renaming '%s' to '%s': %s"), oldpath, newpath,
99 g_strerror(errno));
100 g_free(oldpath);
101 g_free(newpath);
102 return FALSE;
104 string_replace(&host->name, g_strdup(newname));
105 g_free(oldpath);
106 g_free(newpath);
107 return TRUE;