comparison mcabber/src/settings.c @ 1358:005df14df743

Extend command /source Now a "sourced" file can contain any mcabber command once mcabber is running (i.e. if not at startup time).
author Mikael Berthe <mikael@lilotux.net>
date Sun, 11 Nov 2007 12:22:41 +0100
parents 3b338a5c01fc
children 7daf906fbcdc
comparison
equal deleted inserted replaced
1357:7bbfb0073f88 1358:005df14df743
85 // If mainfile is TRUE, the permissions of the configuration file will 85 // If mainfile is TRUE, the permissions of the configuration file will
86 // be fixed if they're insecure. 86 // be fixed if they're insecure.
87 // 87 //
88 int cfg_read_file(char *filename, guint mainfile) 88 int cfg_read_file(char *filename, guint mainfile)
89 { 89 {
90 static unsigned int runtime;
90 FILE *fp; 91 FILE *fp;
91 char *buf; 92 char *buf;
92 char *line, *eol; 93 char *line, *eol;
93 unsigned int ln = 0; 94 unsigned int ln = 0;
94 int err = 0; 95 int err = 0;
104 105
105 home = getenv("HOME"); 106 home = getenv("HOME");
106 if (!home) { 107 if (!home) {
107 scr_LogPrint(LPRINT_LOG, "Can't find home dir!"); 108 scr_LogPrint(LPRINT_LOG, "Can't find home dir!");
108 fprintf(stderr, "Can't find home dir!\n"); 109 fprintf(stderr, "Can't find home dir!\n");
109 return -1; 110 err = -1;
111 goto cfg_read_file_return;
110 } 112 }
111 filename = g_new(char, strlen(home)+24); 113 filename = g_new(char, strlen(home)+24);
112 sprintf(filename, "%s/.mcabber/mcabberrc", home); 114 sprintf(filename, "%s/.mcabber/mcabberrc", home);
113 if ((fp = fopen(filename, "r")) == NULL) { 115 if ((fp = fopen(filename, "r")) == NULL) {
114 // 2nd try... 116 // 2nd try...
115 sprintf(filename, "%s/.mcabberrc", home); 117 sprintf(filename, "%s/.mcabberrc", home);
116 if ((fp = fopen(filename, "r")) == NULL) { 118 if ((fp = fopen(filename, "r")) == NULL) {
117 fprintf(stderr, "Cannot open config file!\n"); 119 fprintf(stderr, "Cannot open config file!\n");
118 g_free(filename); 120 g_free(filename);
119 return -1; 121 err = -1;
122 goto cfg_read_file_return;
120 } 123 }
121 } 124 }
122 // Check configuration file permissions 125 // Check configuration file permissions
123 // As it could contain sensitive data, we make it user-readable only 126 // As it could contain sensitive data, we make it user-readable only.
124 checkset_perm(filename, TRUE); 127 checkset_perm(filename, TRUE);
125 scr_LogPrint(LPRINT_LOGNORM, "Reading %s", filename); 128 scr_LogPrint(LPRINT_LOGNORM, "Reading %s", filename);
126 // Check mcabber dir. There we just warn, we don't change the modes 129 // Check mcabber dir. Here we just warn, we don't change the modes.
127 sprintf(filename, "%s/.mcabber/", home); 130 sprintf(filename, "%s/.mcabber/", home);
128 checkset_perm(filename, FALSE); 131 checkset_perm(filename, FALSE);
129 g_free(filename); 132 g_free(filename);
130 filename = NULL; 133 filename = NULL;
131 } else { 134 } else {
133 const char *msg = "Cannot open configuration file"; 136 const char *msg = "Cannot open configuration file";
134 if (mainfile) 137 if (mainfile)
135 perror(msg); 138 perror(msg);
136 else 139 else
137 scr_LogPrint(LPRINT_LOGNORM, "%s (%s).", msg, filename); 140 scr_LogPrint(LPRINT_LOGNORM, "%s (%s).", msg, filename);
138 return -2; 141 err = -2;
142 goto cfg_read_file_return;
139 } 143 }
140 // Check configuration file permissions (see above) 144 // Check configuration file permissions (see above)
141 // We don't change the permissions if that's not the main file. 145 // We don't change the permissions if that's not the main file.
142 if (mainfile) 146 if (mainfile)
143 checkset_perm(filename, TRUE); 147 checkset_perm(filename, TRUE);
168 // Ignore empty lines and comments 172 // Ignore empty lines and comments
169 if ((*line == '\n') || (*line == '\0') || (*line == '#')) 173 if ((*line == '\n') || (*line == '\0') || (*line == '#'))
170 continue; 174 continue;
171 175
172 // We only allow assignments line, except for commands "pgp", "source", 176 // We only allow assignments line, except for commands "pgp", "source",
173 // "color" and "otrpolicy" 177 // "color" and "otrpolicy", unless we're in runtime (i.e. not startup).
174 if ((strchr(line, '=') != NULL) || 178 if (runtime ||
175 startswith(line, "pgp ", FALSE) || startswith(line, "source ", FALSE) || 179 (strchr(line, '=') != NULL) ||
176 startswith(line, "color ", FALSE) || 180 startswith(line, "pgp ", FALSE) ||
181 startswith(line, "source ", FALSE) ||
182 startswith(line, "color ", FALSE) ||
177 startswith(line, "otrpolicy", FALSE)) { 183 startswith(line, "otrpolicy", FALSE)) {
178 // Only accept the set, alias, bind, pgp and source commands 184 // Only accept the set, alias, bind, pgp and source commands
179 if (!startswith(line, "set ", FALSE) && 185 if (!runtime &&
180 !startswith(line, "bind ", FALSE) && 186 !startswith(line, "set ", FALSE) &&
181 !startswith(line, "alias ", FALSE) && 187 !startswith(line, "bind ", FALSE) &&
182 !startswith(line, "pgp ", FALSE) && 188 !startswith(line, "alias ", FALSE) &&
189 !startswith(line, "pgp ", FALSE) &&
183 !startswith(line, "source ", FALSE) && 190 !startswith(line, "source ", FALSE) &&
184 !startswith(line, "otrpolicy ", FALSE) && 191 !startswith(line, "color ", FALSE) &&
185 !startswith(line, "color ", FALSE)) { 192 !startswith(line, "otrpolicy ", FALSE)) {
186 scr_LogPrint(LPRINT_LOGNORM, 193 scr_LogPrint(LPRINT_LOGNORM, "Error in configuration file (l. %d): "
187 "Error in configuration file (l. %d): bad command", ln); 194 "this command can't be used here", ln);
188 err++; 195 err++;
189 continue; 196 continue;
190 } 197 }
191 // Set the leading COMMAND_CHAR to build a command line 198 // Set the leading COMMAND_CHAR to build a command line
192 // and process the command 199 // and process the command
193 *(--line) = COMMAND_CHAR; 200 *(--line) = COMMAND_CHAR;
194 process_command(line, TRUE); 201 process_command(line, TRUE);
195 } else { 202 } else {
196 scr_LogPrint(LPRINT_LOGNORM, 203 scr_LogPrint(LPRINT_LOGNORM, "Error in configuration file (l. %d): "
197 "Error in configuration file (l. %d): no assignment", ln); 204 "this is not an assignment", ln);
198 err++; 205 err++;
199 } 206 }
200 } 207 }
201 g_free(buf); 208 g_free(buf);
202 fclose(fp); 209 fclose(fp);
203 210
204 if (filename) 211 if (filename)
205 scr_LogPrint(LPRINT_LOGNORM, "Loaded %s.", filename); 212 scr_LogPrint(LPRINT_LOGNORM, "Loaded %s.", filename);
213
214 cfg_read_file_return:
215 // If we're done with the main file parsing, we can assume that
216 // the next time this function is called will be at run time.
217 if (mainfile)
218 runtime = TRUE;
206 return err; 219 return err;
207 } 220 }
208 221
209 // parse_assigment(assignment, pkey, pval) 222 // parse_assigment(assignment, pkey, pval)
210 // Read assignment and split it to key, value 223 // Read assignment and split it to key, value