BBS2chProxyHTML2Dat.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. #include <sstream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "BBS2chProxyHTML2Dat.h"
  6. #include "stringEncodingConverter.h"
  7. extern char *proxy_server;
  8. extern long proxy_port;
  9. extern long proxy_type;
  10. extern long timeout;
  11. extern char *user_agent;
  12. extern int force_ipv4;
  13. extern CURLSH *curl_share;
  14. extern void log_printf(int level, const char *format ...);
  15. extern void *memmem_priv(const void *l, size_t l_len, const void *s, size_t s_len);
  16. #ifdef _WIN32
  17. #define gmtime_r(a, b) gmtime_s(b, a)
  18. #endif
  19. static const char threadTimestampFmt[] = "%Y/%m/%d %H:%M:%S %Z";
  20. static const char *wdays[7] = {
  21. "日",
  22. "月",
  23. "火",
  24. "水",
  25. "木",
  26. "金",
  27. "土"
  28. };
  29. static int decryptMail(unsigned char *decrypted, char *encrypted)
  30. {
  31. char current[5]="0x";
  32. unsigned char *ptr = decrypted;
  33. current[2] = encrypted[0];
  34. current[3] = encrypted[1];
  35. unsigned int r = strtol(current,NULL,16);
  36. int len = strlen(encrypted);
  37. int n = 2;
  38. for(;n<len;n+=2) {
  39. current[2] = encrypted[n];
  40. current[3] = encrypted[n+1];
  41. unsigned int i = strtol(current,NULL,16);
  42. *ptr++ = i^r;
  43. }
  44. *ptr = 0;
  45. //fprintf(stderr,"%s->%s\n",encrypted,decrypted);
  46. return ptr - decrypted;
  47. }
  48. static void replaceAll(std::string &input, const std::string &oldValue, const std::string &newValue)
  49. {
  50. if (!oldValue.empty()) {
  51. size_t pos = 0;
  52. while ((pos = input.find(oldValue, pos)) != std::string::npos) {
  53. input.replace(pos, oldValue.size(), newValue);
  54. pos += newValue.size();
  55. }
  56. }
  57. }
  58. static void escapeForHTML(std::string &input)
  59. {
  60. replaceAll(input, "&", "&amp;");
  61. replaceAll(input, "<", "&lt;");
  62. replaceAll(input, ">", "&gt;");
  63. replaceAll(input, "\"", "&quot;");
  64. replaceAll(input, "'", "&#39;");
  65. }
  66. static size_t write_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
  67. {
  68. std::vector<char> *data = static_cast<std::vector<char> *>(userdata);
  69. size_t downloaded = size*nitems;
  70. data->insert(data->end(), buffer, buffer+downloaded);
  71. return downloaded;
  72. }
  73. BBS2chProxyHTML2Dat5ch::BBS2chProxyHTML2Dat5ch(BBS2chProxyThreadCache *cache, const BBS2chThreadIdentifier &identifier, bool useHttps, CURL *curl)
  74. : IBBS2chProxyHTML2Dat(cache, identifier, curl)
  75. {
  76. _url = useHttps ? "https://" : "http://";
  77. _url += identifier.host;
  78. _url += "/test/read.cgi/";
  79. _url += identifier.board;
  80. _url += '/';
  81. _url += identifier.key;
  82. _url += '/';
  83. }
  84. BBS2chProxyHTML2DatTalk::BBS2chProxyHTML2DatTalk(BBS2chProxyThreadCache *cache, const BBS2chThreadIdentifier &identifier, CURL *curl)
  85. : IBBS2chProxyHTML2Dat(cache, identifier, curl), _cachedJson(NULL)
  86. {
  87. _url = "https://talk.jp/api/boards/";
  88. _url += identifier.board;
  89. _url += "/threads/";
  90. _url += identifier.key;
  91. }
  92. BBS2chProxyHTML2DatTalkHTML::BBS2chProxyHTML2DatTalkHTML(BBS2chProxyThreadCache *cache, const BBS2chThreadIdentifier &identifier, CURL *curl)
  93. : BBS2chProxyHTML2DatTalk(cache, identifier, curl)
  94. {
  95. _url = "https://talk.jp/boards/";
  96. _url += identifier.board;
  97. _url += '/';
  98. _url += identifier.key;
  99. _url += '/';
  100. }
  101. std::vector<char> IBBS2chProxyHTML2Dat::getHtmlFromURL(const std::string &url, long *outStatusCode)
  102. {
  103. CURLcode res;
  104. long statusCode = 0;
  105. std::vector<char> html;
  106. if (curl_share) curl_easy_setopt(_curl, CURLOPT_SHARE, curl_share);
  107. curl_easy_setopt(_curl, CURLOPT_URL, url.c_str());
  108. curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1L);
  109. curl_easy_setopt(_curl, CURLOPT_TIMEOUT, timeout);
  110. curl_easy_setopt(_curl, CURLOPT_ENCODING, "");
  111. curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, write_callback_download);
  112. curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &html);
  113. curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L);
  114. curl_easy_setopt(_curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  115. curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYHOST, 0L);
  116. curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYPEER, 0L);
  117. if (force_ipv4) curl_easy_setopt(_curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  118. if (proxy_server) {
  119. curl_easy_setopt(_curl, CURLOPT_PROXY, proxy_server);
  120. curl_easy_setopt(_curl, CURLOPT_PROXYPORT, proxy_port);
  121. curl_easy_setopt(_curl, CURLOPT_PROXYTYPE, proxy_type);
  122. }
  123. if (user_agent) {
  124. curl_easy_setopt(_curl, CURLOPT_USERAGENT, user_agent);
  125. }
  126. else if (!_userAgent.empty()) {
  127. curl_easy_setopt(_curl, CURLOPT_USERAGENT, _userAgent.c_str());
  128. }
  129. res = curl_easy_perform(_curl);
  130. if (res == CURLE_OK) {
  131. curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &statusCode);
  132. if (statusCode != 200) html.clear();
  133. } else {
  134. log_printf(0, "curl error: %s (%s)\n", curl_easy_strerror(res), url.c_str());
  135. }
  136. curl_easy_reset(_curl);
  137. if (outStatusCode) *outStatusCode = statusCode;
  138. return html;
  139. }
  140. void IBBS2chProxyHTML2Dat::setRequestHeaders(BBS2chProxyHttpHeaders &headers)
  141. {
  142. if (headers.has("User-Agent")) {
  143. _userAgent = headers.get("User-Agent");
  144. }
  145. }
  146. const std::string& IBBS2chProxyHTML2Dat::getKey()
  147. {
  148. return _threadKey;
  149. }
  150. std::string BBS2chProxyHTML2Dat5ch::generateDatFrom(int startFrom, time_t *lastModifiedOut, bool useCache, long *outStatusCode)
  151. {
  152. std::string tmpURL(_url);
  153. if (startFrom > 1) {
  154. std::ostringstream ss;
  155. ss << startFrom << "-n";
  156. tmpURL += ss.str();
  157. } else {
  158. tmpURL += "1-";
  159. }
  160. std::vector<char> html = getHtmlFromURL(tmpURL, outStatusCode);
  161. return html2dat(html, startFrom, lastModifiedOut, useCache);
  162. }
  163. std::string BBS2chProxyHTML2DatTalk::generateDatFrom(int startFrom, time_t *lastModifiedOut, bool useCache, long *outStatusCode)
  164. {
  165. if (!_cachedJson) {
  166. std::vector<char> json = getHtmlFromURL(_url, outStatusCode);
  167. if (json.empty()) return "";
  168. json.push_back(0);
  169. _cachedJson = json_parse_string(&json.front());
  170. }
  171. if (!_cachedJson) return "";
  172. return json2dat(_cachedJson, startFrom, lastModifiedOut, useCache);
  173. }
  174. std::string BBS2chProxyHTML2DatTalkHTML::generateDatFrom(int startFrom, time_t *lastModifiedOut, bool useCache, long *outStatusCode)
  175. {
  176. if (!_cachedJson) {
  177. std::vector<char> html = getHtmlFromURL(_url, outStatusCode);
  178. if (html.empty()) return "";
  179. html.push_back(0);
  180. const char *ptr = strstr(&html.front(), "id=\"__NEXT_DATA__\"");
  181. if (ptr) {
  182. ptr += strlen("id=\"__NEXT_DATA__\"");
  183. while (*ptr != '>' && *ptr != 0) ptr++;
  184. if (*ptr) {
  185. const char *end = strstr(++ptr, "</script>");
  186. if (end) {
  187. std::string jsonStr(ptr, end-ptr);
  188. _cachedJson = json_parse_string(jsonStr.c_str());
  189. }
  190. }
  191. }
  192. }
  193. if (!_cachedJson) return "";
  194. JSON_Value *threadData = json_object_dotget_value(json_object(_cachedJson), "props.pageProps.threadData");
  195. return json2dat(threadData, startFrom, lastModifiedOut, useCache);
  196. }
  197. std::string BBS2chProxyHTML2Dat5ch::html2dat_old(std::vector<char> &html, int startResNum, time_t *lastModified, bool useCache)
  198. {
  199. char *ptr = &html.front();
  200. char *end = &html.back();
  201. std::string txt;
  202. int res = startResNum, i=0;
  203. char signature[32];
  204. char title[1024];
  205. int cachedSize = 0;
  206. bool bbspink = strstr(_threadKey.c_str(),"bbspink.com") ? true : false;
  207. ptr = (char *)memmem_priv(ptr, end-ptr+1, "<title>", 7);
  208. if(!ptr) {
  209. return "";
  210. }
  211. ptr += 7;
  212. while(1) {
  213. if(*ptr == '<') {
  214. if(!strncasecmp(ptr,"</title>",8)) {
  215. ptr += 8;
  216. break;
  217. }
  218. else title[i++] = *ptr++;
  219. }
  220. else title[i++] = *ptr++;
  221. }
  222. title[i] = 0;
  223. snprintf(signature,32,"<dt>%d ",res);
  224. ptr = (char *)memmem_priv(ptr, end-ptr+1, signature, strlen(signature));
  225. if(!ptr) {
  226. return "";
  227. }
  228. unsigned char *buffer = (unsigned char *)malloc(65536+1024+1024+1024+2048);
  229. if(!buffer) {
  230. return "";
  231. }
  232. unsigned char *body = buffer;
  233. char *mail = (char *)body + 65536;
  234. char *name = mail + 1024;
  235. char *date = name + 1024;
  236. char *encrypted = date + 1024;
  237. while(ptr < end) {
  238. //fprintf(stderr,"%s\n",signature);
  239. std::string resData;
  240. i=0;
  241. mail[0] = 0;
  242. ptr = strstr(ptr,signature);
  243. ptr += strlen(signature);
  244. while(*ptr != '<') ptr++;
  245. ptr++;
  246. const char *endStr;
  247. if(*ptr == 'a' || *ptr == 'A') {
  248. replay:
  249. // has mail
  250. while(*ptr != '"') ptr++;
  251. ptr++;
  252. if(!strncmp(ptr,"/cdn-cgi/l/email-protection#",28)) {
  253. ptr += 28;
  254. while(*ptr != '"' && *ptr != 'X') encrypted[i++] = *ptr++;
  255. encrypted[i] = 0;
  256. i = decryptMail((unsigned char *)mail,encrypted);
  257. int reconstruct_len = *ptr == 'X' ? i + 15 : i + 16;
  258. ptr -= reconstruct_len;
  259. char *start = ptr;
  260. memcpy(ptr, "<a href=\"mailto:", 16);
  261. ptr += 16;
  262. memcpy(ptr, mail, i);
  263. ptr = start;
  264. i=0;
  265. goto replay;
  266. }
  267. else {
  268. if(!strncmp(ptr,"mailto:",7)) ptr += 7;
  269. while(*ptr != '"') mail[i++] = *ptr++;
  270. mail[i] = 0;
  271. }
  272. endStr = "</a>";
  273. }
  274. else if(*ptr == 'b') {
  275. endStr = NULL;
  276. }
  277. else {
  278. endStr = "</font>";
  279. }
  280. if(endStr) {
  281. ptr = strstr(ptr,"<b>");
  282. ptr += 3;
  283. }
  284. else {
  285. ptr = strchr(ptr,'>');
  286. ptr++;
  287. }
  288. i=0;
  289. while(1) {
  290. if(*ptr == '<') {
  291. if(!strncasecmp(ptr,"</b>",4) && (!endStr || !strncasecmp(ptr+4,endStr,strlen(endStr)))) {
  292. ptr += 4;
  293. if(endStr) ptr += strlen(endStr);
  294. break;
  295. }
  296. else if(!strncmp(ptr,"<span class=\"__cf_email__\"",26)) {
  297. int j=0;
  298. ptr = strstr(ptr,"data-cfemail=\"");
  299. ptr += 14;
  300. while(*ptr != '"') encrypted[j++] = *ptr++;
  301. encrypted[j] = 0;
  302. j = decryptMail((unsigned char *)name+i,encrypted);
  303. i += j;
  304. ptr = strstr(ptr,"</script>");
  305. ptr += 9;
  306. }
  307. else name[i++] = *ptr++;
  308. }
  309. else name[i++] = *ptr++;
  310. }
  311. resData.append(name, i);
  312. resData.append("<>");
  313. if(mail[0]) resData.append(mail);
  314. resData.append("<>");
  315. ptr += 2;
  316. i=0;
  317. while(1) {
  318. if(*ptr == '<') {
  319. if(!strncasecmp(ptr,"<dd>",4)) {
  320. ptr += 4;
  321. break;
  322. }
  323. else if(!strncmp(ptr,"<a href=\"javascript:be(",23)) {
  324. memcpy(date+i,"BE:",3);
  325. ptr += 23;
  326. i += 3;
  327. while(*ptr != ')') date[i++] = *ptr++;
  328. date[i++] = '-';
  329. ptr = strchr(ptr,'?');
  330. ptr++;
  331. char *tmp = strstr(ptr,"</a>");
  332. memcpy(date+i,ptr,tmp-ptr);
  333. i += tmp-ptr;
  334. ptr = tmp + 4;
  335. }
  336. else date[i++] = *ptr++;
  337. }
  338. else date[i++] = *ptr++;
  339. }
  340. resData.append(date);
  341. resData.append("<>");
  342. i=0;
  343. while(1) {
  344. if(*ptr == '<') {
  345. if(!strncasecmp(ptr,"<br><br>\n",9)) {
  346. ptr += 9;
  347. break;
  348. }
  349. else if(!strncasecmp(ptr,"<dt>",4) || !strncasecmp(ptr,"</dl>",5)) {
  350. while(i>0 &&body[i-1] == '\n') i--;
  351. break;
  352. }
  353. else if(!strncmp(ptr,"<span class=\"__cf_email__\"",26) || !strncmp(ptr,"<a class=\"__cf_email__\"",23)) {
  354. int j=0;
  355. ptr = strstr(ptr,"data-cfemail=\"");
  356. ptr += 14;
  357. while(*ptr != '"') encrypted[j++] = *ptr++;
  358. encrypted[j] = 0;
  359. j = decryptMail(body+i,encrypted);
  360. i += j;
  361. ptr = strstr(ptr,"</script>");
  362. ptr += 9;
  363. }
  364. else if(!strncmp(ptr,"<a href=\"http",13)) {
  365. ptr = strchr(ptr,'>');
  366. ptr++;
  367. char *link = ptr;
  368. ptr = strstr(link,"</a>");
  369. memcpy(body+i,link,ptr-link);
  370. i += ptr-link;
  371. ptr += 4;
  372. }
  373. else if(!strncmp(ptr,"<img src=\"",10)) {
  374. ptr += 10;
  375. char *img = ptr;
  376. ptr = strstr(img,"\">");
  377. memcpy(body+i,img,ptr-img);
  378. if(memmem_priv(img,ptr-img,"/img.2ch.net",12) || memmem_priv(img,ptr-img,"/img.5ch.net",12) || memmem_priv(img,ptr-img,"/o.8ch.net",10) || memmem_priv(img,ptr-img,"/o.5ch.net",10)) {
  379. int length = ptr-img;
  380. while(*img != '/') {
  381. img++;
  382. length--;
  383. }
  384. memcpy(body+i,"sssp:",5);
  385. memcpy(body+i+5,img,length);
  386. i += length + 5;
  387. }
  388. else i += ptr-img;
  389. ptr += 2;
  390. }
  391. else if(!bbspink && !strncmp(ptr,"<br>",4)) {
  392. if(i>5 && !strncmp((char *)body+i-5,"<br> ",5)) {
  393. memcpy(body+i," <br>",5);
  394. i += 5;
  395. }
  396. else {
  397. memcpy(body+i,"<br>",4);
  398. i += 4;
  399. }
  400. ptr += 4;
  401. }
  402. else body[i++] = *ptr++;
  403. }
  404. else if(!bbspink && *ptr == ' ') {
  405. if(*(ptr+1) == ' ') ptr++;
  406. else body[i++] = *ptr++;
  407. }
  408. else body[i++] = *ptr++;
  409. }
  410. resData.append((const char *)body ,i);
  411. resData.append("<>");
  412. if(res == 1) resData.append(title);
  413. resData.append("\n");
  414. if(useCache && res == startResNum) {
  415. PBBS2chProxyThreadInfo info = _threadCache->pop(_threadKey);
  416. bool hit = false;
  417. if(info) {
  418. log_printf(5,"cache hit");
  419. if(info->cachedData.size() == resData.size()) {
  420. log_printf(5,"... size match");
  421. if(info->cachedData == resData) {
  422. log_printf(5,"... content match");
  423. hit = true;
  424. cachedSize = info->cachedSize - resData.size();
  425. }
  426. }
  427. log_printf(5,"\n");
  428. }
  429. if(!hit) {
  430. free(buffer);
  431. return NULL;
  432. }
  433. }
  434. txt += resData;
  435. res++;
  436. while(*ptr == '\n' || *ptr == '\r') ptr++;
  437. snprintf(signature,32,"<dt>%d ",res);
  438. if(!memmem_priv(ptr, end-ptr+1, signature, strlen(signature))) {
  439. PBBS2chProxyThreadInfo info(new BBS2chProxyThreadInfo());
  440. info->lastResNum = res-1;
  441. info->cachedSize = txt.size()+cachedSize;
  442. info->cachedData = resData;
  443. _threadCache->set(_threadKey, info);
  444. log_printf(5,"cached thread %s (%ld bytes)\n",_threadKey.c_str(),resData.size());
  445. if(lastModified) {
  446. *lastModified = 0;
  447. char formattedDate[256];
  448. char *ptr;
  449. ptr = date;
  450. int year = strtol(ptr,&ptr,10);
  451. if(*ptr != '/') break;
  452. ptr++;
  453. int month = strtol(ptr,&ptr,10);
  454. if(*ptr != '/') break;
  455. ptr++;
  456. int day = strtol(ptr,&ptr,10);
  457. if(!*ptr) break;
  458. while(*ptr != ' ' && *ptr != 0) ptr++;
  459. if(!*ptr) break;
  460. ptr++;
  461. int hour = strtol(ptr,&ptr,10);
  462. if(*ptr != ':') break;
  463. ptr++;
  464. int minutes = strtol(ptr,&ptr,10);
  465. if(*ptr != ':') break;
  466. ptr++;
  467. int seconds = strtol(ptr,&ptr,10);
  468. if(!(month>0 && month<13) || !(day>0 && day<32)) break;
  469. if(year < 100) year += 2000;
  470. #if LIBCURL_VERSION_NUM >= 0x070c02 /* curl 7.12.2 or later */
  471. snprintf(formattedDate, 256, "%d%02d%02d %02d:%02d:%02d +0900", year, month, day, hour, minutes, seconds);
  472. *lastModified = curl_getdate(formattedDate, NULL);
  473. #else
  474. snprintf(formattedDate,256,"%d/%d/%d %02d:%02d:%02d JST",year,month,day,hour,minutes,seconds);
  475. struct tm time = {0};
  476. strptime(formattedDate,threadTimestampFmt,&time);
  477. *lastModified = mktime(&time);
  478. #endif
  479. }
  480. //fprintf(stderr,"not found,%ld\n",end-ptr+1);
  481. break;
  482. }
  483. }
  484. free(buffer);
  485. return txt;
  486. }
  487. std::string BBS2chProxyHTML2Dat5ch::html2dat(std::vector<char> &html, int startResNum, time_t *lastModified, bool useCache)
  488. {
  489. char *ptr = &html.front();
  490. char *end = &html.back();
  491. std::string txt;
  492. int res = startResNum, i=0;
  493. char signature[64];
  494. char title[1024];
  495. int cachedSize = 0;
  496. char signatureTag[32];
  497. char closeTag[32];
  498. int closeTagLen;
  499. bool isNewHTML = false;
  500. if (html.empty()) return "";
  501. ptr = (char *)memmem_priv(ptr, end-ptr+1, "<div id=\"threadtitle\">", 22);
  502. if (ptr) {
  503. isNewHTML = true;
  504. char *ptr2 = (char *)memmem_priv(ptr, end-ptr+1, "<article id=\"", 13);
  505. if (!ptr2) {
  506. return "";
  507. }
  508. ptr += 22;
  509. while (1) {
  510. if (*ptr == '<') {
  511. if (!strncasecmp(ptr, "</div>", 6)) {
  512. ptr += 6;
  513. break;
  514. }
  515. else title[i++] = *ptr++;
  516. }
  517. else if(*ptr == '\n') break;
  518. else title[i++] = *ptr++;
  519. }
  520. title[i] = 0;
  521. snprintf(signature, 32, "<article id=\"%d\"", res);
  522. }
  523. else {
  524. ptr = &html.front();
  525. ptr = (char *)memmem_priv(ptr, end-ptr+1, "<h1 class=\"title\">", 18);
  526. if(!ptr) {
  527. return html2dat_old(html, startResNum, lastModified, useCache);
  528. }
  529. else {
  530. char *ptr2 = (char *)memmem_priv(ptr, end-ptr+1, " class=\"post\"", 13);
  531. if(ptr2) {
  532. char *tmp = ptr2;
  533. *ptr2 = 0;
  534. while(*ptr2 != '<') ptr2--;
  535. strcpy(signatureTag, ptr2);
  536. *tmp = ' ';
  537. }
  538. else {
  539. return "";
  540. }
  541. /*char *ptr2 = (char *)memmem_priv(ptr, end-ptr+1, "<dl class=\"post\"", 16);
  542. if(ptr2) {
  543. return html2dat_pink(html, startResNum, lastModified, useCache);
  544. }*/
  545. }
  546. ptr += 18;
  547. while(1) {
  548. if(*ptr == '<') {
  549. if(!strncasecmp(ptr,"</h1>",5)) {
  550. ptr += 5;
  551. break;
  552. }
  553. else title[i++] = *ptr++;
  554. }
  555. else if(*ptr == '\n') break;
  556. else title[i++] = *ptr++;
  557. }
  558. title[i] = 0;
  559. snprintf(signature,32,"%s class=\"post\" id=\"%d\"",signatureTag,res);
  560. }
  561. ptr = (char *)memmem_priv(ptr, end-ptr+1, signature, strlen(signature));
  562. if(!ptr) {
  563. return "";
  564. }
  565. unsigned char *buffer = (unsigned char *)malloc(65536+1024+1024+1024+2048);
  566. if(!buffer) {
  567. return "";
  568. }
  569. unsigned char *body = buffer;
  570. char *mail = (char *)body + 65536;
  571. char *name = mail + 1024;
  572. char *date = name + 1024;
  573. char *encrypted = date + 1024;
  574. while(ptr < end) {
  575. //fprintf(stderr,"%s\n",signature);
  576. std::string resData;
  577. i=0;
  578. mail[0] = 0;
  579. if (isNewHTML) ptr = strstr(ptr," class=\"postusername\"><b>");
  580. else ptr = strstr(ptr," class=\"name\"><b>");
  581. if(ptr) {
  582. char *tmp = ptr;
  583. *ptr = 0;
  584. while(*ptr != '<') ptr--;
  585. snprintf(closeTag,32,"</%s>",ptr+1);
  586. closeTagLen = strlen(closeTag);
  587. if (isNewHTML) ptr = tmp + 25;
  588. else ptr = tmp + 17;
  589. }
  590. else {
  591. break;
  592. }
  593. char endStr[64];
  594. if(!strncmp(ptr,"<a href=\"mailto:",16)) {
  595. replay:
  596. // has mail
  597. while(*ptr != '"') ptr++;
  598. ptr++;
  599. if(!strncmp(ptr,"/cdn-cgi/l/email-protection#",28)) {
  600. ptr += 28;
  601. while(*ptr != '"' && *ptr != 'X') encrypted[i++] = *ptr++;
  602. encrypted[i] = 0;
  603. i = decryptMail((unsigned char *)mail,encrypted);
  604. int reconstruct_len = *ptr == 'X' ? i + 15 : i + 16;
  605. ptr -= reconstruct_len;
  606. char *start = ptr;
  607. memcpy(ptr, "<a href=\"mailto:", 16);
  608. ptr += 16;
  609. memcpy(ptr, mail, i);
  610. ptr = start;
  611. i=0;
  612. goto replay;
  613. }
  614. else {
  615. if(!strncmp(ptr,"mailto:",7)) ptr += 7;
  616. while(1) {
  617. if(*ptr == '<' && !strncmp(ptr,"<a href=\"",9)) {
  618. ptr = strchr(ptr,'>');
  619. ptr++;
  620. char *link = ptr;
  621. ptr = strstr(link,"</a>");
  622. memcpy(mail+i,link,ptr-link);
  623. i += ptr-link;
  624. ptr += 4;
  625. }
  626. else if(*ptr == '"') break;
  627. else mail[i++] = *ptr++;
  628. }
  629. //while(*ptr != '"') mail[i++] = *ptr++;
  630. mail[i] = 0;
  631. }
  632. snprintf(endStr,64,"</a></b>%s",closeTag);
  633. while(*ptr != '>') ptr++;
  634. ptr++;
  635. }
  636. /* we do not have to handle this special case because read.cgi on bbspink doesn't
  637. emit font tags anymore and it conflicts with text decorations using "melon point" */
  638. /*else if(!strncmp(ptr,"<font",5)) {
  639. snprintf(endStr,64,"</font></b>%s",closeTag);
  640. while(*ptr != '>') ptr++;
  641. ptr++;
  642. }*/
  643. else {
  644. snprintf(endStr,64,"</b>%s",closeTag);
  645. }
  646. i=0;
  647. while(1) {
  648. if(*ptr == '<') {
  649. if(!strncmp(ptr,endStr,strlen(endStr))) {
  650. ptr += strlen(endStr);
  651. break;
  652. }
  653. else if(!strncmp(ptr,"<span class=\"__cf_email__\"",26)) {
  654. int j=0;
  655. ptr = strstr(ptr,"data-cfemail=\"");
  656. ptr += 14;
  657. while(*ptr != '"') encrypted[j++] = *ptr++;
  658. encrypted[j] = 0;
  659. j = decryptMail((unsigned char *)name+i,encrypted);
  660. i += j;
  661. ptr = strstr(ptr,"</script>");
  662. ptr += 9;
  663. }
  664. else if(!strncmp(ptr,"<a href=\"",9)) {
  665. ptr = strchr(ptr,'>');
  666. ptr++;
  667. char *link = ptr;
  668. ptr = strstr(link,"</a>");
  669. memcpy(name+i,link,ptr-link);
  670. i += ptr-link;
  671. ptr += 4;
  672. }
  673. else name[i++] = *ptr++;
  674. }
  675. else name[i++] = *ptr++;
  676. }
  677. resData.append(name, i);
  678. resData.append("<>");
  679. if(mail[0]) resData.append(mail);
  680. resData.append("<>");
  681. ptr = strstr(ptr," class=\"date\">");
  682. if(ptr) {
  683. char *tmp = ptr;
  684. *ptr = 0;
  685. while(*ptr != '<') ptr--;
  686. snprintf(closeTag,32,"</%s>",ptr+1);
  687. closeTagLen = strlen(closeTag);
  688. ptr = tmp + 14;
  689. }
  690. else {
  691. break;
  692. }
  693. i=0;
  694. while(1) {
  695. if(*ptr == '<') {
  696. if(!strncasecmp(ptr,closeTag,closeTagLen)) {
  697. ptr += closeTagLen;
  698. break;
  699. }
  700. else date[i++] = *ptr++;
  701. }
  702. else date[i++] = *ptr++;
  703. }
  704. if(!strncmp(ptr,"<div class=\"uid",15) || !strncmp(ptr,"<span class=\"uid",16)) {
  705. char *tmp = ptr+1;
  706. while(*ptr != ' ') ptr++;
  707. *ptr = 0;
  708. snprintf(closeTag,32,"</%s>",tmp);
  709. closeTagLen = strlen(closeTag);
  710. ptr += 11;
  711. while(*ptr != '>') ptr++;
  712. ptr++;
  713. date[i++] = ' ';
  714. while(1) {
  715. if(*ptr == '<') {
  716. if(!strncasecmp(ptr,closeTag,closeTagLen)) {
  717. ptr += closeTagLen;
  718. break;
  719. }
  720. else date[i++] = *ptr++;
  721. }
  722. else date[i++] = *ptr++;
  723. }
  724. }
  725. if(!strncmp(ptr,"<div class=\"be",14) || !strncmp(ptr,"<span class=\"be",15)) {
  726. ptr += 14;
  727. while(*ptr != '>') ptr++;
  728. ptr++;
  729. if(!strncmp(ptr,"<a href=\"",9)) {
  730. ptr += 9;
  731. while(*ptr != '/' && *ptr != '"') ptr++;
  732. if(*ptr == '/' && (!strncmp(ptr,"//be.2ch.net/user/",18) || !strncmp(ptr,"//be.5ch.net/user/",18))) {
  733. memcpy(date+i," BE:",4);
  734. i += 4;
  735. ptr += 18;
  736. while(*ptr != '"') date[i++] = *ptr++;
  737. date[i++] = '-';
  738. ptr = strchr(ptr,'?');
  739. ptr++;
  740. char *tmp = strstr(ptr,"</a>");
  741. memcpy(date+i,ptr,tmp-ptr);
  742. i += tmp-ptr;
  743. ptr = tmp + 4;
  744. }
  745. }
  746. }
  747. resData.append(date, i);
  748. resData.append("<>");
  749. if (isNewHTML) {
  750. ptr = strstr(ptr,"<section class=\"post-content\">");
  751. if (!ptr) {
  752. break;
  753. }
  754. else {
  755. ptr += 30;
  756. if (!strncasecmp(ptr, "<span class=\"AA\">", 17)) {
  757. strcpy(closeTag, "</span></section>");
  758. closeTagLen = 17;
  759. ptr += 17;
  760. }
  761. else {
  762. strcpy(closeTag, "</section>");
  763. closeTagLen = 10;
  764. }
  765. }
  766. }
  767. else if(!strcmp(signatureTag,"<div")) {
  768. ptr = strstr(ptr,"<div class=\"message\">");
  769. if(!ptr) {
  770. break;
  771. }
  772. else {
  773. ptr += 21;
  774. if(!strncasecmp(ptr,"<span class=\"escaped\">",22)) {
  775. if(!strncasecmp(ptr+22,"<span class=\"AA\">",17)) {
  776. strcpy(closeTag,"</span></span></div>");
  777. closeTagLen = 20;
  778. ptr += 22+17;
  779. }
  780. else {
  781. strcpy(closeTag,"</span></div>");
  782. closeTagLen = 13;
  783. ptr += 22;
  784. }
  785. }
  786. else {
  787. strcpy(closeTag,"</div>");
  788. closeTagLen = 6;
  789. }
  790. }
  791. }
  792. else {
  793. ptr = strstr(ptr,"<dd class=\"thread_in\">");
  794. if(!ptr) {
  795. break;
  796. }
  797. strcpy(closeTag,"</dd>");
  798. closeTagLen = 5;
  799. ptr += 22;
  800. }
  801. i=0;
  802. while(1) {
  803. if(*ptr == '<') {
  804. if(!strncasecmp(ptr,closeTag,closeTagLen)) {
  805. ptr += closeTagLen;
  806. break;
  807. }
  808. else if(!strncmp(ptr,"<span class=\"__cf_email__\"",26) || !strncmp(ptr,"<a class=\"__cf_email__\"",23)) {
  809. int j=0;
  810. ptr = strstr(ptr,"data-cfemail=\"");
  811. ptr += 14;
  812. while(*ptr != '"') encrypted[j++] = *ptr++;
  813. encrypted[j] = 0;
  814. j = decryptMail(body+i,encrypted);
  815. i += j;
  816. ptr = strstr(ptr,"</script>");
  817. ptr += 9;
  818. }
  819. else if(!strncmp(ptr,"<a ",3)) {
  820. char *tmp = strchr(ptr,'>');
  821. char *href = (char *)memmem_priv(ptr,tmp-ptr,"href=\"",6);
  822. char *link = tmp+1;
  823. if(href && !strncmp(link,"&gt;&gt;",8) && memmem_priv(href,link-href,"test/read.cgi/",14)) {
  824. while(ptr < link) {
  825. if(!strncmp(ptr," class=\"",8)) {
  826. ptr += 8;
  827. while(*ptr != '"' && *ptr != '>') ptr++;
  828. if(*ptr == '"') ptr++;
  829. }
  830. else body[i++] = *ptr++;
  831. }
  832. }
  833. else {
  834. ptr = strstr(link,"</a>");
  835. memcpy(body+i,link,ptr-link);
  836. i += ptr-link;
  837. ptr += 4;
  838. }
  839. }
  840. else if(!strncmp(ptr,"<img src=\"",10)) {
  841. ptr += 10;
  842. char *img = ptr;
  843. ptr = strstr(img,"\">");
  844. memcpy(body+i,img,ptr-img);
  845. if(memmem_priv(img,ptr-img,"/img.2ch.net",12) || memmem_priv(img,ptr-img,"/img.5ch.net",12) || memmem_priv(img,ptr-img,"/o.8ch.net",10) || memmem_priv(img,ptr-img,"/o.5ch.net",10)) {
  846. int length = ptr-img;
  847. while(*img != '/') {
  848. img++;
  849. length--;
  850. }
  851. memcpy(body+i,"sssp:",5);
  852. memcpy(body+i+5,img,length);
  853. i += length + 5;
  854. }
  855. else i += ptr-img;
  856. ptr += 2;
  857. }
  858. else if(!strncmp(ptr,"<br>",4)) {
  859. if(i>5 && !strncmp((char *)body+i-5,"<br> ",5)) {
  860. memcpy(body+i," <br>",5);
  861. i += 5;
  862. }
  863. else {
  864. memcpy(body+i,"<br>",4);
  865. i += 4;
  866. }
  867. ptr += 4;
  868. }
  869. else body[i++] = *ptr++;
  870. }
  871. else body[i++] = *ptr++;
  872. }
  873. resData.append((const char *)body, i);
  874. resData.append("<>");
  875. if(res == 1) resData.append(title);
  876. resData.append("\n");
  877. if(useCache && res == startResNum) {
  878. PBBS2chProxyThreadInfo info = _threadCache->pop(_threadKey);
  879. bool hit = false;
  880. if(info) {
  881. log_printf(5,"cache hit");
  882. if(info->cachedData.size() == resData.size()) {
  883. log_printf(5,"... size match");
  884. if(info->cachedData == resData) {
  885. log_printf(5,"... content match");
  886. hit = true;
  887. cachedSize = info->cachedSize - resData.size();
  888. }
  889. }
  890. log_printf(5,"\n");
  891. }
  892. if(!hit) {
  893. free(buffer);
  894. return NULL;
  895. }
  896. }
  897. txt += resData;
  898. res++;
  899. while(*ptr == '\n' || *ptr == '\r') ptr++;
  900. if (isNewHTML) strcpy(signature, "<article id=\"");
  901. else snprintf(signature,64,"%s class=\"post\" id=\"",signatureTag);
  902. ptr = (char *)memmem_priv(ptr, end-ptr+1, signature, strlen(signature));
  903. if(ptr) {
  904. int next = atoi(ptr+strlen(signature));
  905. if(next >= res) {
  906. while(next > res) {
  907. txt += "broken<><>broken<> broken <>\n";
  908. res++;
  909. }
  910. }
  911. else ptr = NULL;
  912. }
  913. if(!ptr) {
  914. PBBS2chProxyThreadInfo info(new BBS2chProxyThreadInfo());
  915. info->lastResNum = res-1;
  916. info->cachedSize = txt.size()+cachedSize;
  917. info->cachedData = resData;
  918. _threadCache->set(_threadKey, info);
  919. log_printf(5,"cached thread %s (%ld bytes)\n",_threadKey.c_str(),resData.size());
  920. if(lastModified) {
  921. *lastModified = 0;
  922. char formattedDate[256];
  923. char *ptr;
  924. ptr = date;
  925. int year = strtol(ptr,&ptr,10);
  926. if(*ptr != '/') break;
  927. ptr++;
  928. int month = strtol(ptr,&ptr,10);
  929. if(*ptr != '/') break;
  930. ptr++;
  931. int day = strtol(ptr,&ptr,10);
  932. if(!*ptr) break;
  933. while(*ptr != ' ' && *ptr != 0) ptr++;
  934. if(!*ptr) break;
  935. ptr++;
  936. int hour = strtol(ptr,&ptr,10);
  937. if(*ptr != ':') break;
  938. ptr++;
  939. int minutes = strtol(ptr,&ptr,10);
  940. if(*ptr != ':') break;
  941. ptr++;
  942. int seconds = strtol(ptr,&ptr,10);
  943. if(!(month>0 && month<13) || !(day>0 && day<32)) break;
  944. if(year < 100) year += 2000;
  945. #if LIBCURL_VERSION_NUM >= 0x070c02 /* curl 7.12.2 or later */
  946. snprintf(formattedDate, 256, "%d%02d%02d %02d:%02d:%02d +0900", year, month, day, hour, minutes, seconds);
  947. *lastModified = curl_getdate(formattedDate, NULL);
  948. #else
  949. snprintf(formattedDate,256,"%d/%d/%d %02d:%02d:%02d JST",year,month,day,hour,minutes,seconds);
  950. struct tm time = {0};
  951. strptime(formattedDate,threadTimestampFmt,&time);
  952. *lastModified = mktime(&time);
  953. #endif
  954. }
  955. //fprintf(stderr,"not found,%ld\n",end-ptr+1);
  956. break;
  957. }
  958. }
  959. free(buffer);
  960. return txt;
  961. }
  962. std::string BBS2chProxyHTML2DatTalk::json2dat(JSON_Value *json, int startFrom, time_t *lastModifiedOut, bool useCache)
  963. {
  964. std::string out;
  965. if (!json || json_type(json) != JSONObject) {
  966. return "";
  967. }
  968. JSON_Object *root = json_object(json);
  969. const char *title = json_object_dotget_string(root, "data.title");
  970. const char *quoteSource = json_object_dotget_string(root, "data.quote_source");
  971. JSON_Array *comments = json_object_dotget_array(root, "data.comments");
  972. if (!title || !comments) {
  973. return "";
  974. }
  975. if (startFrom < 1) startFrom = 1;
  976. int prevNumber = startFrom - 1;
  977. time_t lastModified = 0;
  978. size_t cachedSize = 0;
  979. std::string lastLine;
  980. for (size_t i=0, length=json_array_get_count(comments); i<length; i++) {
  981. std::stringstream line;
  982. JSON_Object *comment = json_array_get_object(comments, i);
  983. if (!comment) continue;
  984. int number = json_object_get_number(comment, "number");
  985. if (number < startFrom) continue;
  986. const char *name = json_object_dotget_string(comment, "writer.name");
  987. const char *trip = json_object_dotget_string(comment, "writer.trip");
  988. const char *slip = json_object_dotget_string(comment, "writer.slip");
  989. const char *id = json_object_dotget_string(comment, "writer.id");
  990. time_t timestamp = json_object_get_number(comment, "timestamp");
  991. const char *body = json_object_get_string(comment, "body");
  992. if (timestamp > lastModified) lastModified = timestamp;
  993. for (int j=prevNumber+1; j<number; j++) {
  994. out += "broken<><>broken<> broken <>\n";
  995. }
  996. if (name) {
  997. std::string tmp(name);
  998. escapeForHTML(tmp);
  999. line << tmp;
  1000. if (trip) line << "</b>◆" << trip << "<b>";
  1001. if (slip) line << " </b>(" << slip << ")<b>";
  1002. }
  1003. else line << "削除";
  1004. line << "<><>"; //mail cannot be obtained from json!
  1005. if (timestamp) {
  1006. char dateStr[256] = "";
  1007. struct tm timestamp_tm = {0};
  1008. timestamp += 32400;
  1009. gmtime_r(&timestamp, &timestamp_tm);
  1010. strftime(dateStr, 256, "%Y/%m/%d(", &timestamp_tm);
  1011. line << dateStr << wdays[timestamp_tm.tm_wday];
  1012. strftime(dateStr, 256, ") %T", &timestamp_tm);
  1013. line << dateStr;
  1014. if (id) {
  1015. line << " ID:" << id;
  1016. }
  1017. }
  1018. else line << "削除";
  1019. line << "<>";
  1020. if (body) {
  1021. std::string tmp(body);
  1022. escapeForHTML(tmp);
  1023. replaceAll(tmp, "\n", " <br> ");
  1024. line << " " << tmp;
  1025. if (number == 1 && quoteSource) {
  1026. line << " <br> <br> 出典 " << quoteSource;
  1027. }
  1028. line << " ";
  1029. }
  1030. else line << "削除";
  1031. line << "<>";
  1032. if (number == 1) {
  1033. std::string tmp(title);
  1034. escapeForHTML(tmp);
  1035. line << tmp;
  1036. }
  1037. line << "\n";
  1038. prevNumber = number;
  1039. char *lineSJIS = convertUTF8ToShiftJISWithNCR(line.str().c_str(), line.str().size());
  1040. if (lineSJIS) {
  1041. lastLine = lineSJIS;
  1042. out += lastLine;
  1043. free(lineSJIS);
  1044. } else {
  1045. lastLine = "broken<><>broken<> broken <>\n";
  1046. out += lastLine;
  1047. }
  1048. if (useCache && startFrom == number) {
  1049. PBBS2chProxyThreadInfo info = _threadCache->pop(_threadKey);
  1050. bool hit = false;
  1051. if (info) {
  1052. log_printf(5, "cache hit");
  1053. if (info->cachedData.size() == lastLine.size()) {
  1054. log_printf(5, "... size match");
  1055. if (info->cachedData == lastLine) {
  1056. log_printf(5, "... content match");
  1057. hit = true;
  1058. cachedSize = info->cachedSize - lastLine.size();
  1059. }
  1060. }
  1061. log_printf(5, "\n");
  1062. }
  1063. if (!hit) {
  1064. return "";
  1065. }
  1066. }
  1067. }
  1068. if (!lastLine.empty()) {
  1069. PBBS2chProxyThreadInfo info(new BBS2chProxyThreadInfo());
  1070. info->lastResNum = prevNumber;
  1071. info->cachedSize = out.size() + cachedSize;
  1072. info->cachedData = lastLine;
  1073. _threadCache->set(_threadKey, info);
  1074. log_printf(5, "cached thread %s (%ld bytes)\n", _threadKey.c_str(), lastLine.size());
  1075. }
  1076. if (lastModifiedOut) *lastModifiedOut = lastModified;
  1077. return out;
  1078. }