BBS2chProxyHTML2Dat.cpp 34 KB

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