2 * Copyright (C) 2008-2011 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
4 * Copyright (C) 2009-2010 Amir Mohammad Saied <amirsaied@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/types.h>
35 #include <curl/curl.h>
36 #include <libxml/xmlmemory.h>
37 #include <libxml/parser.h>
38 #include <libxml/tree.h>
50 * Read a line from the config file and assign it a key and a value.
52 * This logic taken almost identically from taken from udev's rule file parsing
53 * logic in the file udev-rules.c, written by Kay Sievers and licensed under
54 * the GPLv2+. I hate writing parsers, so it makes sense to borrow working
55 * logic from those smarter than I...
57 static int get_key(struct session *session, char *line, char **key, char **value)
64 if (linepos == NULL || linepos[0] == '\0')
68 while (isspace(linepos[0]) || linepos[0] == ',')
70 if (linepos[0] == '\0')
77 if (linepos[0] == '\0')
79 if (isspace(linepos[0]))
81 if (linepos[0] == '=')
85 /* remember the end of the key */
88 /* skip whitespace after key */
89 while (isspace(linepos[0]))
91 if (linepos[0] == '\0')
94 /* make sure this is a = operation */
96 * udev likes to check for += and == and lots of other complex
97 * assignments that we don't care about.
99 if (linepos[0] == '=')
107 /* skip whitespace after opearator */
108 while (isspace(linepos[0]))
110 if (linepos[0] == '\0')
114 * if the value is quoted, then terminate on a ", otherwise space is
117 if (linepos[0] == '"') {
127 temp = strchr(linepos, terminator);
133 * perhaps we just hit the end of the line, so there would not
134 * be a terminator, so just use the whole rest of the string as
138 printf("%s = %s\n", *key, *value);
142 typedef int (*config_function_callback)(struct session *session, char *value);
144 struct config_table {
146 config_function_callback callback;
150 int account_callback(struct session *session, char *value)
155 static struct config_table config_table[] = {
156 { "account", account_callback },
162 void bti_parse_configfile(struct session *session)
167 char *account = NULL;
168 char *password = NULL;
169 char *consumer_key = NULL;
170 char *consumer_secret = NULL;
171 char *access_token_key = NULL;
172 char *access_token_secret = NULL;
175 char *logfile = NULL;
178 char *replyto = NULL;
179 char *retweet = NULL;
182 config_file = fopen(session->configfile, "r");
184 /* No error if file does not exist or is unreadable. */
185 if (config_file == NULL)
191 ssize_t n = getline(&line, &len, config_file);
194 if (line[n - 1] == '\n')
196 /* Parse file. Format is the usual value pairs:
199 # is a comment character
201 *strchrnul(line, '#') = '\0';
205 /* Ignore blank lines. */
209 get_key(session, line, &key, &value);
211 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
215 } else if (!strncasecmp(c, "password", 8) &&
219 password = strdup(c);
220 } else if (!strncasecmp(c, "consumer_key", 12) &&
224 consumer_key = strdup(c);
225 } else if (!strncasecmp(c, "consumer_secret", 15) &&
229 consumer_secret = strdup(c);
230 } else if (!strncasecmp(c, "access_token_key", 16) &&
234 access_token_key = strdup(c);
235 } else if (!strncasecmp(c, "access_token_secret", 19) &&
239 access_token_secret = strdup(c);
240 } else if (!strncasecmp(c, "host", 4) &&
245 } else if (!strncasecmp(c, "proxy", 5) &&
250 } else if (!strncasecmp(c, "logfile", 7) &&
255 } else if (!strncasecmp(c, "replyto", 7) &&
260 } else if (!strncasecmp(c, "action", 6) &&
265 } else if (!strncasecmp(c, "user", 4) &&
270 } else if (!strncasecmp(c, "shrink-urls", 11) &&
273 if (!strncasecmp(c, "true", 4) ||
274 !strncasecmp(c, "yes", 3))
276 } else if (!strncasecmp(c, "verbose", 7) &&
279 if (!strncasecmp(c, "true", 4) ||
280 !strncasecmp(c, "yes", 3))
281 session->verbose = 1;
282 } else if (!strncasecmp(c,"retweet", 7) &&
288 } while (!feof(config_file));
291 session->password = password;
293 session->account = account;
295 session->consumer_key = consumer_key;
297 session->consumer_secret = consumer_secret;
298 if (access_token_key)
299 session->access_token_key = access_token_key;
300 if (access_token_secret)
301 session->access_token_secret = access_token_secret;
303 if (strcasecmp(host, "twitter") == 0) {
304 session->host = HOST_TWITTER;
305 session->hosturl = strdup(twitter_host);
306 session->hostname = strdup(twitter_name);
307 } else if (strcasecmp(host, "identica") == 0) {
308 session->host = HOST_IDENTICA;
309 session->hosturl = strdup(identica_host);
310 session->hostname = strdup(identica_name);
312 session->host = HOST_CUSTOM;
313 session->hosturl = strdup(host);
314 session->hostname = strdup(host);
320 free(session->proxy);
321 session->proxy = proxy;
324 session->logfile = logfile;
326 session->replyto = replyto;
328 session->retweet = retweet;
330 if (strcasecmp(action, "update") == 0)
331 session->action = ACTION_UPDATE;
332 else if (strcasecmp(action, "friends") == 0)
333 session->action = ACTION_FRIENDS;
334 else if (strcasecmp(action, "user") == 0)
335 session->action = ACTION_USER;
336 else if (strcasecmp(action, "replies") == 0)
337 session->action = ACTION_REPLIES;
338 else if (strcasecmp(action, "public") == 0)
339 session->action = ACTION_PUBLIC;
340 else if (strcasecmp(action, "group") == 0)
341 session->action = ACTION_GROUP;
343 session->action = ACTION_UNKNOWN;
347 session->user = user;
348 session->shrink_urls = shrink_urls;
350 /* Free buffer and close file. */