1 /* Copyright 2013 Akira Ohta (akohta001@gmail.com)
2 This file is part of ntch.
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.
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.
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/>.
28 #include "utils/nt_std_t.h"
29 #include "utils/text.h"
30 #include "utils/file.h"
31 #include "utils/base64.h"
32 #include "utils/crypt.h"
33 #include "utils/nt_conv_char.h"
34 #include "net/nt_http.h"
35 #include "cloud/nt_cloud.h"
38 #define NT_CLOUD_CHK_SUM (1478428)
40 typedef struct tag_nt_cloud_t *nt_cloud_tp;
41 typedef struct tag_nt_cloud_t {
42 nt_cloud_handle_t handle;
54 static nt_link_tp nt_split_line(char *data);
55 static int nt_get_cloud_result(const char *ptr);
56 static BOOL nt_cloud_edit_lines_file(nt_cloud_handle handle,
57 const char *file_name, nt_link_tp lines, const char *php_file, int depth);
58 static BOOL nt_cloud_upload_file_local(nt_cloud_handle handle,
59 const char *file_name, nt_link_tp lines, int depth);
61 nt_cloud_handle nt_cloud_init(
62 const char *auth_url, const char *usr_id, const char *pass)
66 assert(usr_id && pass);
68 cloudp = malloc(sizeof(nt_cloud_t));
71 cloudp->handle.chk_sum = NT_CLOUD_CHK_SUM;
72 cloudp->ref_count = 1;
73 cloudp->usr_id = nt_str_clone(usr_id);
78 cloudp->passwd = nt_str_clone(pass);
84 cloudp->auth_url = nt_str_clone(auth_url);
85 if(!cloudp->auth_url){
91 cloudp->query_url = NULL;
92 cloudp->home_url = NULL;
95 if(!nt_cloud_chk_user(&cloudp->handle)){
96 nt_cloud_release_ref(&cloudp->handle);
100 return &cloudp->handle;
103 int nt_cloud_add_ref(nt_cloud_handle handle)
107 assert(handle->chk_sum == NT_CLOUD_CHK_SUM);
108 cloudp = (nt_cloud_tp)handle;
109 assert(0 < cloudp->ref_count);
110 return ++cloudp->ref_count;
112 int nt_cloud_release_ref(nt_cloud_handle handle)
116 assert(handle->chk_sum == NT_CLOUD_CHK_SUM);
117 cloudp = (nt_cloud_tp)handle;
118 assert(0 < cloudp->ref_count);
119 if(0 != --cloudp->ref_count)
120 return cloudp->ref_count;
121 assert(cloudp->usr_id);
122 free(cloudp->usr_id);
123 assert(cloudp->passwd);
124 free(cloudp->passwd);
125 assert(cloudp->auth_url);
126 free(cloudp->auth_url);
127 if(cloudp->query_url)
128 free(cloudp->query_url);
130 free(cloudp->home_url);
137 BOOL nt_cloud_chk_user(nt_cloud_handle handle)
140 char post_data[1024];
142 nt_link_tp linkp, wrkp;
146 assert(handle->chk_sum == NT_CLOUD_CHK_SUM);
147 cloudp = (nt_cloud_tp)handle;
148 assert(cloudp->usr_id);
149 assert(cloudp->passwd);
150 if(cloudp->query_url)
151 free(cloudp->query_url);
153 free(cloudp->home_url);
156 cloudp->query_url = NULL;
157 cloudp->home_url = NULL;
158 cloudp->token = NULL;
160 sprintf(post_data, "user=%s&passwd=%s",
161 cloudp->usr_id, cloudp->passwd);
163 if(!nt_http_post(cloudp->auth_url, post_data,
164 buf, sizeof(buf), NULL, NULL, NULL, NULL)){
167 linkp = nt_split_line(buf);
175 cloudp->error_no = atoi((const char*)wrkp->data);
176 if(100 != cloudp->error_no)
180 cloudp->query_url = nt_str_clone((const char*)wrkp->data);
181 if(!cloudp->query_url)
185 cloudp->home_url = nt_str_clone((const char*)wrkp->data);
186 if(!cloudp->query_url)
190 cloudp->token = nt_str_clone((const char*)wrkp->data);
197 }while(wrkp != linkp && idx < 5);
198 nt_all_link_free(linkp, NULL);
201 if(cloudp->query_url)
202 free(cloudp->query_url);
204 free(cloudp->home_url);
207 cloudp->query_url = NULL;
208 cloudp->home_url = NULL;
209 cloudp->token = NULL;
211 nt_all_link_free(linkp, NULL);
215 BOOL nt_cloud_insert_lines_into_file(nt_cloud_handle handle,
216 const char *file_name, nt_link_tp lines)
218 return nt_cloud_edit_lines_file(handle, file_name, lines, "nc_insert_lines_file.php", 0);
220 BOOL nt_cloud_delete_lines_from_file(nt_cloud_handle handle,
221 const char *file_name, nt_link_tp lines)
223 return nt_cloud_edit_lines_file(handle, file_name, lines, "nc_delete_lines_file.php", 0);
226 static BOOL nt_cloud_edit_lines_file(nt_cloud_handle handle,
227 const char *file_name, nt_link_tp lines, const char *php_file, int depth)
233 static const int buf_delta = 2048*2;
237 nt_crypt_handle h_crypt;
243 assert(handle->chk_sum == NT_CLOUD_CHK_SUM);
244 cloudp = (nt_cloud_tp)handle;
245 assert(cloudp->usr_id);
246 assert(cloudp->token);
248 num = nt_link_num(lines);
251 len = strlen(cloudp->query_url);
252 len += strlen(php_file);
256 strcpy(url, cloudp->query_url);
257 strcat(url, php_file);
259 post_data = malloc(buf_delta);
265 h_crypt = nt_crypt_get_handle();
271 icd = iconv_open("cp932", "wchar_t");
272 if(((iconv_t)-1) == icd){
273 nt_crypt_release_ref(h_crypt);
278 sprintf(post_data, "user=%s&token=%s&file_name=%s&num=%d",
279 cloudp->usr_id, cloudp->token, file_name, num);
284 if(nt_conv_wc2sjis(icd, (const wchar_t*)linkp->data,
285 buf1, sizeof(buf1))){
286 len = nt_crypt_encrypt(h_crypt,
287 (const unsigned char*)buf1,strlen(buf1),
288 (unsigned char*)buf2, sizeof(buf2));
290 if(nt_base64_url_encode((const unsigned char*)buf2,
291 len, buf1, sizeof(buf1))){
292 snprintf(buf2, sizeof(buf2), "&n%d=", idx++);
293 strcat(post_data, buf2);
294 strcat(post_data, buf1);
299 }while(linkp != lines);
302 nt_crypt_release_ref(h_crypt);
304 if(!nt_http_post2(url, post_data,
305 buf1, sizeof(buf1), NULL, NULL, NULL, NULL)){
311 result_code = nt_get_cloud_result(buf1);
314 if(result_code == 301 && depth == 0){
315 if(!nt_cloud_chk_user(handle))
317 return nt_cloud_edit_lines_file(handle,
318 file_name, lines, php_file, ++depth);
319 }else if(result_code != 100){
326 nt_link_tp nt_cloud_download_file(nt_cloud_handle handle,
327 const char *file_name)
332 nt_link_tp linkp, wrkp;
340 nt_crypt_handle h_crypt;
345 assert(handle->chk_sum == NT_CLOUD_CHK_SUM);
346 cloudp = (nt_cloud_tp)handle;
347 assert(cloudp->home_url);
349 len = strlen(cloudp->home_url);
350 len += strlen(file_name);
354 strcpy(url, cloudp->home_url);
355 strcat(url, file_name);
357 if(!nt_make_sha1_path(LOG_PATH, url, &outp)){
362 if(!nt_http_get(url, outp, NULL, NULL, NULL, FALSE, FALSE)){
368 fp_src = fopen(outp, "r");
375 h_crypt = nt_crypt_get_handle();
383 icd = iconv_open("wchar_t", "cp932");
384 if(((iconv_t)-1) == icd){
395 if(!fgets(buf1, sizeof(buf1), fp_src))
397 cptr = nt_trim2(buf1, buf2, sizeof(buf2));
398 if(!cptr || *cptr == '\0')
400 len = nt_base64_url_decode(cptr, strlen(cptr),
401 (unsigned char*)buf1, sizeof(buf1));
404 len = nt_crypt_decrypt(h_crypt,
405 (const unsigned char*)buf1, len,
406 (unsigned char*)buf2, sizeof(buf2));
410 if(!nt_conv_sjis2wc(icd, (const char*)buf2,
414 wcptr = nt_w_str_clone(wc);
417 wrkp = nt_link_add_data(linkp, wcptr);
426 nt_crypt_release_ref(h_crypt);
433 BOOL nt_cloud_upload_file(nt_cloud_handle handle,
434 const char *file_name, nt_link_tp lines)
436 return nt_cloud_upload_file_local(handle,
437 file_name, lines, 0);
440 static BOOL nt_cloud_upload_file_local(nt_cloud_handle handle,
441 const char *file_name, nt_link_tp lines, int depth)
447 static const int buf_delta = 2048*2;
451 nt_crypt_handle h_crypt;
456 assert(handle->chk_sum == NT_CLOUD_CHK_SUM);
457 cloudp = (nt_cloud_tp)handle;
458 assert(cloudp->usr_id);
459 assert(cloudp->token);
461 len = strlen(cloudp->query_url);
462 len += strlen("nc_write_file.php");
466 strcpy(url, cloudp->query_url);
467 strcat(url, "nc_write_file.php");
469 post_data = malloc(buf_delta);
475 h_crypt = nt_crypt_get_handle();
481 icd = iconv_open("cp932", "wchar_t");
482 if(((iconv_t)-1) == icd){
483 nt_crypt_release_ref(h_crypt);
488 sprintf(post_data, "user=%s&token=%s&file_name=%s&data=",
489 cloudp->usr_id, cloudp->token, file_name);
492 if(nt_conv_wc2sjis(icd, (const wchar_t*)linkp->data,
493 buf1, sizeof(buf1))){
494 len = nt_crypt_encrypt(h_crypt,
495 (const unsigned char*)buf1,strlen(buf1),
496 (unsigned char*)buf2, sizeof(buf2));
498 if(nt_base64_url_encode((const unsigned char*)buf2,
499 len, buf1, sizeof(buf1))){
500 strcat(post_data, buf1);
501 strcat(post_data, "\n");
506 }while(linkp != lines);
509 nt_crypt_release_ref(h_crypt);
511 if(!nt_http_post(url, post_data,
512 buf1, sizeof(buf1), NULL, NULL, NULL, NULL)){
518 result_code = nt_get_cloud_result(buf1);
519 if(result_code == 301 && depth == 0){
520 if(!nt_cloud_chk_user(handle))
522 return nt_cloud_upload_file_local(handle,
523 file_name, lines, ++depth);
524 }else if(result_code != 100){
532 static int nt_get_cloud_result(const char *ptr)
538 if(*ptr < '0' || *ptr > '9')
543 if(*ptr >= '0' && *ptr <= '9'){
554 static nt_link_tp nt_split_line(char *data)
556 nt_link_tp linkp, wrkp;
564 wrkp = nt_link_add_data(linkp, cptr);
569 p = strchr(cptr, '\n');
580 nt_all_link_free(linkp, NULL);