OSDN Git Service

v1.0.2.6
[ntch/develop.git] / src / env.c
1 /* Copyright 2013,2014 Akira Ohta (akohta001@gmail.com)
2     This file is part of ntch.
3
4     The ntch is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8
9     The ntch is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with ntch.  If not, see <http://www.gnu.org/licenses/>.
16     
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <limits.h>
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <pwd.h>
27
28 #define _GNU_SOURCE
29 #include <getopt.h>
30
31 #include "env.h"
32 #include "utils/nt_std_t.h"
33 #include "utils/file.h"
34 #include "utils/text.h"
35
36 char LOG_PATH[1024];
37 char USR_PATH[1024];
38 char USR_COOKIE_PATH[1024];
39 char USR_LOG_DB_PATH[1024];
40 char USR_FAVORITE_BOARD_FILE_PATH[1024];
41 char USR_FAVORITE_GRP_FILE_PATH[1024];
42 char USR_FAVORITE_THREAD_FILE_PATH[1024];
43 char USR_NG_WORD_FILE_PATH[1024];
44 char USR_NG_NAME_FILE_PATH[1024];
45 char USR_NG_ID_FILE_PATH[1024];
46 char EDITOR_CMD[1024]; 
47 int FORCE_REFRESH = 0; 
48 int THREAD_SORT_TYPE = NT_THREAD_SORT_BY_READ; 
49 int INIT_DISP_STATE = NT_INTI_DISP_BOARDMENU; 
50 int NT_MAINLOOP_POLLING_INTERVAL;
51 int NT_PTHREAD_POOL_SIZE;
52 int NT_PTHREAD_POOL_QUEUE_SIZE;
53 int NT_AUTO_SCROLL_INTERVAL;
54 int NT_AUTO_UPDATE_INTERVAL;
55 int APP_MODE;
56
57 char *RFC2898_SALT;
58 int  RFC2898_ITERATION;
59 char *AES256_PASS;
60 char *NCE_AUTH_URL;
61 char *NCE_ID;
62 char *NCE_PASS;
63
64 char CA_FILE_PATH[1024];
65
66 static char *app_name = PACKAGE_NAME;
67 static char *version_name = PACKAGE_VERSION;
68 static char *def_editor_cmd = "vi";
69
70 char *MARU_ID;
71 char *MARU_PW;
72
73 struct option longopts[] = {
74         {"refresh-screen", 0, NULL, 'r'},
75         {"version", 0, NULL, 'v'},
76         {"help", 0, NULL, 'h'},
77         {"editor", 1, NULL, 'e'},
78         {"out-path", 1, NULL, 'o'},
79         {"pem", 1, NULL, 'p'},
80         {"server", 2, NULL, 's'},
81         {NULL, 0, NULL, 0}
82 };
83
84 extern void print_help();
85 extern BOOL read_resource(const char *path);
86
87 void nt_env_free()
88 {
89         if(MARU_ID != NULL) free(MARU_ID);
90         if(MARU_PW != NULL) free(MARU_PW);
91         if(RFC2898_SALT != NULL) free(RFC2898_SALT);
92         if(AES256_PASS != NULL) free(AES256_PASS);
93         if(NCE_AUTH_URL != NULL) free(NCE_AUTH_URL);
94         if(NCE_ID != NULL) free(NCE_ID);
95         if(NCE_PASS != NULL) free(NCE_PASS);
96 }
97
98 int set_option(int argc, char* argv[])
99 {
100         int opt ,len;
101         uid_t uid;
102         struct passwd *pw;
103         char buf[256];
104         
105         APP_MODE = NT_APP_MODE_CLIENT;
106
107         MARU_ID = NULL;
108         MARU_PW = NULL;
109         RFC2898_SALT = NULL;
110         RFC2898_ITERATION = 0;
111         AES256_PASS = NULL;
112         NCE_AUTH_URL = NULL;
113         NCE_ID = NULL;
114         NCE_PASS = NULL;
115         CA_FILE_PATH[0] = '\0';
116         
117         NT_MAINLOOP_POLLING_INTERVAL = 100;
118         NT_PTHREAD_POOL_SIZE = 5;
119         NT_PTHREAD_POOL_QUEUE_SIZE = 20;
120         
121         NT_AUTO_SCROLL_INTERVAL = 500;
122         NT_AUTO_UPDATE_INTERVAL = 60000;
123         
124         uid = getuid();
125         pw = getpwuid(uid);
126         if(!pw){
127                 perror("Could not find user info.(home dir)\n");
128                 return -1;
129         }
130         sprintf(USR_PATH, "%s/.ntch", pw->pw_dir);
131         sprintf(USR_COOKIE_PATH, "%s/cookie", USR_PATH);
132         sprintf(USR_LOG_DB_PATH, "%s/usr_log.sqlite3", USR_PATH);
133         sprintf(USR_FAVORITE_BOARD_FILE_PATH, "%s/fb.txt", USR_PATH);
134         sprintf(USR_FAVORITE_GRP_FILE_PATH, "%s/ftab.txt", USR_PATH);
135         sprintf(USR_FAVORITE_THREAD_FILE_PATH, "%s/ft.txt", USR_PATH);
136         sprintf(USR_NG_WORD_FILE_PATH, "%s/ngwd.txt", USR_PATH);
137         sprintf(USR_NG_NAME_FILE_PATH, "%s/ngnm.txt", USR_PATH);
138         sprintf(USR_NG_ID_FILE_PATH, "%s/ngid.txt", USR_PATH);
139
140         sprintf(buf, "%s/.ntchrc", pw->pw_dir);
141         if(!read_resource(buf)){
142                 fputs(
143                         "Could not read user's resource file. \n\tuse default.\n", 
144                         stderr);
145         }
146
147         strcpy(LOG_PATH, "./log");
148         strcpy(EDITOR_CMD, def_editor_cmd); 
149
150         if(!nt_mkdir(USR_PATH)){
151                 return -1;
152         }
153
154         while((opt = getopt_long(
155                         argc, argv, "hvro:p:s::", longopts, NULL)) != -1){
156
157                 switch(opt){
158                 case 'r':
159                         printf("screen refresh: on\n");
160                         FORCE_REFRESH = 1; 
161                         break;
162                 case 'v':
163                         printf("%s (NTCH) 2channel browser version - %s\n", 
164                                 app_name , version_name);
165                         return 1;
166                 case 'h':
167                         print_help();
168                         return 1;
169                 case 'o':
170                         printf("output path: %s\n", optarg);
171                         len = strlen(optarg);
172                         if(len == 0 || len > sizeof(LOG_PATH)-5)
173                                 break;
174                         strcpy(LOG_PATH, optarg);
175                         if(LOG_PATH[len-1] != '/'){
176                                 LOG_PATH[len++] = '/';
177                         }
178                         strcpy(LOG_PATH+len, "log");
179                         break;
180                 case 'p':
181                         printf("ca file path: %s\n", optarg);
182                         len = strlen(optarg);
183                         if(len == 0 || len > sizeof(CA_FILE_PATH)-1)
184                                 break;
185                         strcpy(CA_FILE_PATH, optarg);
186                         break;
187                 case 's':
188                         APP_MODE = NT_APP_MODE_SERVER;
189                         break;
190                 default:
191                         break;
192                 }
193         }
194         
195         if(CA_FILE_PATH[0] == '\0'){
196                 strcpy(CA_FILE_PATH, "cacert.pem");
197         }
198         /*for(; optind < argc; optind++){
199                 fprintf(stderr, "Unrecognized argument %s\n",
200                                         argv[optind]);
201         }*/
202         return 0;
203 }
204
205
206 extern BOOL read_resource(const char *path)
207 {
208         FILE *fp;
209         char *cptr, *key, *val;
210         char *sort_type, *init_disp;
211         char buf[128];
212         long int li;
213
214         fp = fopen(path, "r");
215         if(!fp)
216                 return FALSE;
217         
218         while(fgets(buf, sizeof(buf), fp)){
219                 if(buf[0] == '#')
220                         continue;
221                 cptr = strchr(buf, '=');
222                 if(!cptr)
223                         continue;
224
225                 *cptr = '\0';
226                 key = strstr(buf, "maru-id");
227                 if(key){
228                         val = cptr+1;
229                         MARU_ID = nt_trim(val);
230                         continue;
231                 }
232                 key = strstr(buf, "maru-pw");
233                 if(key){
234                         val = cptr+1;
235                         MARU_PW = nt_trim(val);
236                         continue;
237                 }
238                 key = strstr(buf, "sort");
239                 if(key){
240                         val = cptr+1;
241                         sort_type = nt_trim(val);
242                         if(!sort_type)
243                                 break;
244                         if(0 == strcmp(sort_type, "number")){
245                                 THREAD_SORT_TYPE = NT_THREAD_SORT_BY_NUMBER;
246                         }else if(0 == strcmp(sort_type, "read")){
247                                 THREAD_SORT_TYPE = NT_THREAD_SORT_BY_READ;
248                         }else if(0 == strcmp(sort_type, "unread")){
249                                 THREAD_SORT_TYPE = NT_THREAD_SORT_BY_UNREAD;
250                         }
251                         free(sort_type);
252                         continue;
253                 }
254                 key = strstr(buf, "show");
255                 if(key){
256                         val = cptr+1;
257                         init_disp = nt_trim(val);
258                         if(!init_disp)
259                                 break;
260                         if(0 == strcmp(init_disp, "favorite")){
261                                 INIT_DISP_STATE = NT_INTI_DISP_FAVORITE;
262                         }else if(0 == strcmp(init_disp, "history")){
263                                 INIT_DISP_STATE = NT_INTI_DISP_HISTORY;
264                         }
265                         free(init_disp);
266                         continue;
267                 }
268                 key = strstr(buf, "auto-scroll-interval");
269                 if(key){
270                         val = cptr+1;
271                         li = strtol(val, NULL, 10);
272                         if(li > 0 && li < LONG_MAX )
273                                 NT_AUTO_SCROLL_INTERVAL = li;
274                         continue;
275                 }
276                 key = strstr(buf, "auto-update-interval");
277                 if(key){
278                         val = cptr+1;
279                         li = strtol(val, NULL, 10);
280                         if(li > 0 && li < LONG_MAX )
281                                 NT_AUTO_UPDATE_INTERVAL = li;
282                         continue;
283                 }
284                 key = strstr(buf, "rfc2898-salt");
285                 if(key){
286                         val = cptr+1;
287                         RFC2898_SALT = nt_trim(val);
288                         continue;
289                 }
290                 key = strstr(buf, "rfc2898-iteration");
291                 if(key){
292                         val = cptr+1;
293                         li = strtol(val, NULL, 10);
294                         if(li > 0 && li < LONG_MAX )
295                                 RFC2898_ITERATION = li;
296                         continue;
297                 }
298                 key = strstr(buf, "aes256-pass");
299                 if(key){
300                         val = cptr+1;
301                         AES256_PASS = nt_trim(val);
302                         continue;
303                 }
304                 key = strstr(buf, "nce-auth-url");
305                 if(key){
306                         val = cptr+1;
307                         NCE_AUTH_URL = nt_trim(val);
308                         continue;
309                 }
310                 key = strstr(buf, "nce-id");
311                 if(key){
312                         val = cptr+1;
313                         NCE_ID = nt_trim(val);
314                         continue;
315                 }
316                 key = strstr(buf, "nce-pass");
317                 if(key){
318                         val = cptr+1;
319                         NCE_PASS = nt_trim(val);
320                         continue;
321                 }
322         }
323         fclose(fp);
324         return TRUE;
325 }