[svn-upgrade] new version bti (029)
[debian/bti.git] / config.c
1 /*
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>
5  *
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.
9  *
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.
14  *
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.
18  */
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <curl/curl.h>
36 #include <libxml/xmlmemory.h>
37 #include <libxml/parser.h>
38 #include <libxml/tree.h>
39 #include <pcre.h>
40 #include <termios.h>
41 #include <dlfcn.h>
42 #include <oauth.h>
43 #include "bti.h"
44
45 void bti_parse_configfile(struct session *session)
46 {
47         FILE *config_file;
48         char *line = NULL;
49         size_t len = 0;
50         char *account = NULL;
51         char *password = NULL;
52         char *consumer_key = NULL;
53         char *consumer_secret = NULL;
54         char *access_token_key = NULL;
55         char *access_token_secret = NULL;
56         char *host = NULL;
57         char *proxy = NULL;
58         char *logfile = NULL;
59         char *action = NULL;
60         char *user = NULL;
61         char *replyto = NULL;
62         char *retweet = NULL;
63         int shrink_urls = 0;
64
65         config_file = fopen(session->configfile, "r");
66
67         /* No error if file does not exist or is unreadable.  */
68         if (config_file == NULL)
69                 return;
70
71         do {
72                 ssize_t n = getline(&line, &len, config_file);
73                 if (n < 0)
74                         break;
75                 if (line[n - 1] == '\n')
76                         line[n - 1] = '\0';
77                 /* Parse file.  Format is the usual value pairs:
78                    account=name
79                    passwort=value
80                    # is a comment character
81                 */
82                 *strchrnul(line, '#') = '\0';
83                 char *c = line;
84                 while (isspace(*c))
85                         c++;
86                 /* Ignore blank lines.  */
87                 if (c[0] == '\0')
88                         continue;
89
90                 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
91                         c += 8;
92                         if (c[0] != '\0')
93                                 account = strdup(c);
94                 } else if (!strncasecmp(c, "password", 8) &&
95                            (c[8] == '=')) {
96                         c += 9;
97                         if (c[0] != '\0')
98                                 password = strdup(c);
99                 } else if (!strncasecmp(c, "consumer_key", 12) &&
100                            (c[12] == '=')) {
101                         c += 13;
102                         if (c[0] != '\0')
103                                 consumer_key = strdup(c);
104                 } else if (!strncasecmp(c, "consumer_secret", 15) &&
105                            (c[15] == '=')) {
106                         c += 16;
107                         if (c[0] != '\0')
108                                 consumer_secret = strdup(c);
109                 } else if (!strncasecmp(c, "access_token_key", 16) &&
110                            (c[16] == '=')) {
111                         c += 17;
112                         if (c[0] != '\0')
113                                 access_token_key = strdup(c);
114                 } else if (!strncasecmp(c, "access_token_secret", 19) &&
115                            (c[19] == '=')) {
116                         c += 20;
117                         if (c[0] != '\0')
118                                 access_token_secret = strdup(c);
119                 } else if (!strncasecmp(c, "host", 4) &&
120                            (c[4] == '=')) {
121                         c += 5;
122                         if (c[0] != '\0')
123                                 host = strdup(c);
124                 } else if (!strncasecmp(c, "proxy", 5) &&
125                            (c[5] == '=')) {
126                         c += 6;
127                         if (c[0] != '\0')
128                                 proxy = strdup(c);
129                 } else if (!strncasecmp(c, "logfile", 7) &&
130                            (c[7] == '=')) {
131                         c += 8;
132                         if (c[0] != '\0')
133                                 logfile = strdup(c);
134                 } else if (!strncasecmp(c, "replyto", 7) &&
135                            (c[7] == '=')) {
136                         c += 8;
137                         if (c[0] != '\0')
138                                 replyto = strdup(c);
139                 } else if (!strncasecmp(c, "action", 6) &&
140                            (c[6] == '=')) {
141                         c += 7;
142                         if (c[0] != '\0')
143                                 action = strdup(c);
144                 } else if (!strncasecmp(c, "user", 4) &&
145                                 (c[4] == '=')) {
146                         c += 5;
147                         if (c[0] != '\0')
148                                 user = strdup(c);
149                 } else if (!strncasecmp(c, "shrink-urls", 11) &&
150                                 (c[11] == '=')) {
151                         c += 12;
152                         if (!strncasecmp(c, "true", 4) ||
153                                         !strncasecmp(c, "yes", 3))
154                                 shrink_urls = 1;
155                 } else if (!strncasecmp(c, "verbose", 7) &&
156                                 (c[7] == '=')) {
157                         c += 8;
158                         if (!strncasecmp(c, "true", 4) ||
159                                         !strncasecmp(c, "yes", 3))
160                                 session->verbose = 1;
161                 } else if (!strncasecmp(c,"retweet", 7) &&
162                                 (c[7] == '=')) {
163                         c += 8;
164                         if (c[0] != '\0')
165                                 retweet = strdup(c);
166                 }
167         } while (!feof(config_file));
168
169         if (password)
170                 session->password = password;
171         if (account)
172                 session->account = account;
173         if (consumer_key)
174                 session->consumer_key = consumer_key;
175         if (consumer_secret)
176                 session->consumer_secret = consumer_secret;
177         if (access_token_key)
178                 session->access_token_key = access_token_key;
179         if (access_token_secret)
180                 session->access_token_secret = access_token_secret;
181         if (host) {
182                 if (strcasecmp(host, "twitter") == 0) {
183                         session->host = HOST_TWITTER;
184                         session->hosturl = strdup(twitter_host);
185                         session->hostname = strdup(twitter_name);
186                 } else if (strcasecmp(host, "identica") == 0) {
187                         session->host = HOST_IDENTICA;
188                         session->hosturl = strdup(identica_host);
189                         session->hostname = strdup(identica_name);
190                 } else {
191                         session->host = HOST_CUSTOM;
192                         session->hosturl = strdup(host);
193                         session->hostname = strdup(host);
194                 }
195                 free(host);
196         }
197         if (proxy) {
198                 if (session->proxy)
199                         free(session->proxy);
200                 session->proxy = proxy;
201         }
202         if (logfile)
203                 session->logfile = logfile;
204         if (replyto)
205                 session->replyto = replyto;
206         if (retweet)
207                 session->retweet = retweet;
208         if (action) {
209                 if (strcasecmp(action, "update") == 0)
210                         session->action = ACTION_UPDATE;
211                 else if (strcasecmp(action, "friends") == 0)
212                         session->action = ACTION_FRIENDS;
213                 else if (strcasecmp(action, "user") == 0)
214                         session->action = ACTION_USER;
215                 else if (strcasecmp(action, "replies") == 0)
216                         session->action = ACTION_REPLIES;
217                 else if (strcasecmp(action, "public") == 0)
218                         session->action = ACTION_PUBLIC;
219                 else if (strcasecmp(action, "group") == 0)
220                         session->action = ACTION_GROUP;
221                 else
222                         session->action = ACTION_UNKNOWN;
223                 free(action);
224         }
225         if (user)
226                 session->user = user;
227         session->shrink_urls = shrink_urls;
228
229         /* Free buffer and close file.  */
230         free(line);
231         fclose(config_file);
232 }
233
234