utils.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #pragma once
  2. static const char threadTimestampFmt[] = "%Y/%m/%d %H:%M:%S %Z";
  3. static const char httpTimestampFmt[] = "%a, %d %b %Y %H:%M:%S GMT";
  4. static void *
  5. memmem_priv(const void *l, size_t l_len, const void *s, size_t s_len)
  6. {
  7. register char *cur, *last;
  8. const char *cl = (const char *)l;
  9. const char *cs = (const char *)s;
  10. /* we need something to compare */
  11. if (l_len == 0 || s_len == 0)
  12. return NULL;
  13. /* "s" must be smaller or equal to "l" */
  14. if (l_len < s_len)
  15. return NULL;
  16. /* special case where s_len == 1 */
  17. if (s_len == 1)
  18. return (void *)memchr(l, (int)*cs, l_len);
  19. /* the last position where its possible to find "s" in "l" */
  20. last = (char *)cl + l_len - s_len;
  21. for (cur = (char *)cl; cur <= last; cur++)
  22. if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
  23. return cur;
  24. return NULL;
  25. }
  26. static int decryptMail(unsigned char *decrypted, char *encrypted)
  27. {
  28. char current[5]="0x";
  29. unsigned char *ptr = decrypted;
  30. current[2] = encrypted[0];
  31. current[3] = encrypted[1];
  32. unsigned int r = strtol(current,NULL,16);
  33. int len = strlen(encrypted);
  34. int n = 2;
  35. for(;n<len;n+=2) {
  36. current[2] = encrypted[n];
  37. current[3] = encrypted[n+1];
  38. unsigned int i = strtol(current,NULL,16);
  39. *ptr++ = i^r;
  40. }
  41. *ptr = 0;
  42. //fprintf(stderr,"%s->%s\n",encrypted,decrypted);
  43. return ptr - decrypted;
  44. }
  45. static size_t header_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  46. {
  47. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  48. if(conn->status) return size*nitems;
  49. if(!strncasecmp("Connection",buffer,10)) {
  50. fprintf(conn->fpw,"Connection: Close\r\n");
  51. return size*nitems;
  52. }
  53. else if(!strncasecmp("Transfer-Encoding",buffer,17)) {
  54. if(allow_chunked && strstr(buffer+19,"chunked")) {
  55. conn->chunked = true;
  56. //fprintf(stderr,"%s",buffer);
  57. size_t ret = fwrite(buffer,size,nitems,conn->fpw);
  58. return ret;
  59. }
  60. return size*nitems;
  61. }
  62. else if(conn->force5ch && !strncasecmp("Set-Cookie:",buffer,11)) {
  63. char *ptr = (char *)memmem_priv(buffer,size*nitems,"domain=",7);
  64. if(ptr) {
  65. char *end = ptr;
  66. while(*end != ';' && *end != '\r') end++;
  67. ptr = (char *)memmem_priv(ptr,end-ptr,"5ch.net",7);
  68. if(ptr) {
  69. fwrite(buffer,1,ptr-buffer,conn->fpw);
  70. fputc('2',conn->fpw);
  71. fwrite(ptr+1,1,size*nitems-(ptr+1-buffer),conn->fpw);
  72. return size*nitems;
  73. }
  74. }
  75. return fwrite(buffer,1,size*nitems,conn->fpw);
  76. }
  77. else {
  78. if (conn->bbscgi) {
  79. if (!strncasecmp("X-Chx-Error:", buffer, 12)) {
  80. const char *ptr = buffer + 12;
  81. while (*ptr == ' ') ptr++;
  82. log_printf(0, "Posting to bbs.cgi has been cancelled. Reason: %s", ptr);
  83. #ifdef USE_LUA
  84. if (*ptr == 'E') {
  85. int code = atoi(ptr+1);
  86. if (code == 3310 || code == 3324 || code == 3391) conn->setMonaKey("");
  87. }
  88. #endif
  89. }
  90. #ifdef USE_LUA
  91. else if (!strncasecmp("X-MonaKey:", buffer, 10)) {
  92. const char *ptr = buffer + 10;
  93. while (*ptr == ' ') ptr++;
  94. const char *end = ptr;
  95. while (*end != '\n' && *end != '\r' && *end) end++;
  96. conn->setMonaKey(std::string(ptr, end-ptr));
  97. }
  98. #endif
  99. }
  100. size_t ret = fwrite(buffer,size,nitems,conn->fpw);
  101. //fprintf(stderr,"%s",buffer);
  102. if(!memcmp(buffer,"\r\n",2)) {
  103. fflush(conn->fpw);
  104. conn->status = 1;
  105. }
  106. return ret;
  107. }
  108. }
  109. static size_t write_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  110. {
  111. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  112. if(conn->chunked) fprintf(conn->fpw,"%lx\r\n",size*nitems);
  113. size_t ret = fwrite(buffer,size,nitems,conn->fpw);
  114. if(conn->chunked) fprintf(conn->fpw,"\r\n");
  115. fflush(conn->fpw);
  116. return ret;
  117. }
  118. static size_t header_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
  119. {
  120. DataStorage *data = reinterpret_cast<DataStorage *>(userdata);
  121. if(!strncasecmp("Connection",buffer,10)) {
  122. data->appendBytes("Connection: Close\r\n",19);
  123. return size*nitems;
  124. }
  125. else if(!strncasecmp("Transfer-Encoding",buffer,17)) {
  126. return size*nitems;
  127. }
  128. else {
  129. data->appendBytes(buffer,size*nitems);
  130. return size*nitems;
  131. }
  132. }
  133. static size_t write_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
  134. {
  135. DataStorage *data = reinterpret_cast<DataStorage *>(userdata);
  136. size_t downloaded = size*nitems;
  137. data->appendBytes(buffer, downloaded);
  138. return downloaded;
  139. }
  140. static size_t read_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  141. {
  142. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  143. if(size*nitems < 1) return 0;
  144. if(conn->content_length) {
  145. size_t bytesToRead = conn->content_length;
  146. if(size*nitems < conn->content_length) bytesToRead = size*nitems;
  147. size_t ret = fread(buffer,1,bytesToRead,conn->fpr);
  148. conn->content_length -= ret;
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static void sendBasicHeaders(int respCode, const char *respMsg, FILE *fpw)
  154. {
  155. char date[256];
  156. time_t now = time(0);
  157. strftime(date, 256, httpTimestampFmt, gmtime(&now));
  158. if(0 > fprintf(fpw, "HTTP/1.1 %d %s\r\n",respCode,respMsg)) return;
  159. if(0 > fprintf(fpw, "Connection: Close\r\n")) return;
  160. if(0 > fprintf(fpw, "Server: 2ch Proxy\r\n")) return;
  161. if(0 > fprintf(fpw, "Date: %s\r\n",date)) return;
  162. fflush(fpw);
  163. }
  164. static void sendResponse(int respCode, const char *respMsg, FILE *fpw)
  165. {
  166. sendBasicHeaders(respCode, respMsg, fpw);
  167. if(0 > fprintf(fpw, "Content-Type: text/plain; charset=UTF-8\r\n")) return;
  168. if(0 > fprintf(fpw, "\r\n")) return;
  169. if(respCode >= 400) {
  170. if(0 > fprintf(fpw, "   ∧_∧   / ̄ ̄ ̄ ̄ ̄\n")) return;
  171. if(0 > fprintf(fpw, "  ( ´∀`)< %s\n",respMsg)) return;
  172. if(0 > fprintf(fpw, "  (    ) \_____\n")) return;
  173. if(0 > fprintf(fpw, "   │ │ │\n")) return;
  174. if(0 > fprintf(fpw, "  (__)_)\n")) return;
  175. }
  176. fflush(fpw);
  177. }
  178. #ifdef __WIN32__
  179. const char * strp_weekdays[] =
  180. { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
  181. const char * strp_monthnames[] =
  182. { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
  183. bool strp_atoi(const char * & s, int & result, int low, int high, int offset)
  184. {
  185. bool worked = false;
  186. char * end;
  187. unsigned long num = strtoul(s, & end, 10);
  188. if (num >= (unsigned long)low && num <= (unsigned long)high)
  189. {
  190. result = (int)(num + offset);
  191. s = end;
  192. worked = true;
  193. }
  194. return worked;
  195. }
  196. char * strptime(const char *s, const char *format, struct tm *tm)
  197. {
  198. bool working = true;
  199. while (working && *format && *s)
  200. {
  201. switch (*format)
  202. {
  203. case '%':
  204. {
  205. ++format;
  206. switch (*format)
  207. {
  208. case 'a':
  209. case 'A': // weekday name
  210. tm->tm_wday = -1;
  211. working = false;
  212. for (size_t i = 0; i < 7; ++ i)
  213. {
  214. size_t len = strlen(strp_weekdays[i]);
  215. if (!strnicmp(strp_weekdays[i], s, len))
  216. {
  217. tm->tm_wday = i;
  218. s += len;
  219. working = true;
  220. break;
  221. }
  222. else if (!strnicmp(strp_weekdays[i], s, 3))
  223. {
  224. tm->tm_wday = i;
  225. s += 3;
  226. working = true;
  227. break;
  228. }
  229. }
  230. break;
  231. case 'b':
  232. case 'B':
  233. case 'h': // month name
  234. tm->tm_mon = -1;
  235. working = false;
  236. for (size_t i = 0; i < 12; ++ i)
  237. {
  238. size_t len = strlen(strp_monthnames[i]);
  239. if (!strnicmp(strp_monthnames[i], s, len))
  240. {
  241. tm->tm_mon = i;
  242. s += len;
  243. working = true;
  244. break;
  245. }
  246. else if (!strnicmp(strp_monthnames[i], s, 3))
  247. {
  248. tm->tm_mon = i;
  249. s += 3;
  250. working = true;
  251. break;
  252. }
  253. }
  254. break;
  255. case 'd':
  256. case 'e': // day of month number
  257. working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
  258. break;
  259. case 'D': // %m/%d/%y
  260. {
  261. const char * s_save = s;
  262. working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
  263. if (working && *s == '/')
  264. {
  265. ++ s;
  266. working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
  267. if (working && *s == '/')
  268. {
  269. ++ s;
  270. working = strp_atoi(s, tm->tm_year, 0, 99, 0);
  271. if (working && tm->tm_year < 69)
  272. tm->tm_year += 100;
  273. }
  274. }
  275. if (!working)
  276. s = s_save;
  277. }
  278. break;
  279. case 'H': // hour
  280. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  281. break;
  282. case 'I': // hour 12-hour clock
  283. working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
  284. break;
  285. case 'j': // day number of year
  286. working = strp_atoi(s, tm->tm_yday, 1, 366, -1);
  287. break;
  288. case 'm': // month number
  289. working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
  290. break;
  291. case 'M': // minute
  292. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  293. break;
  294. case 'n': // arbitrary whitespace
  295. case 't':
  296. while (isspace((int)*s))
  297. ++s;
  298. break;
  299. case 'p': // am / pm
  300. if (!strnicmp(s, "am", 2))
  301. { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
  302. if (tm->tm_hour == 12) // 12 am == 00 hours
  303. tm->tm_hour = 0;
  304. }
  305. else if (!strnicmp(s, "pm", 2))
  306. {
  307. if (tm->tm_hour < 12) // 12 pm == 12 hours
  308. tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
  309. }
  310. else
  311. working = false;
  312. break;
  313. case 'r': // 12 hour clock %I:%M:%S %p
  314. {
  315. const char * s_save = s;
  316. working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
  317. if (working && *s == ':')
  318. {
  319. ++ s;
  320. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  321. if (working && *s == ':')
  322. {
  323. ++ s;
  324. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  325. if (working && isspace((int)*s))
  326. {
  327. ++ s;
  328. while (isspace((int)*s))
  329. ++s;
  330. if (!strnicmp(s, "am", 2))
  331. { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
  332. if (tm->tm_hour == 12) // 12 am == 00 hours
  333. tm->tm_hour = 0;
  334. }
  335. else if (!strnicmp(s, "pm", 2))
  336. {
  337. if (tm->tm_hour < 12) // 12 pm == 12 hours
  338. tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
  339. }
  340. else
  341. working = false;
  342. }
  343. }
  344. }
  345. if (!working)
  346. s = s_save;
  347. }
  348. break;
  349. case 'R': // %H:%M
  350. {
  351. const char * s_save = s;
  352. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  353. if (working && *s == ':')
  354. {
  355. ++ s;
  356. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  357. }
  358. if (!working)
  359. s = s_save;
  360. }
  361. break;
  362. case 'S': // seconds
  363. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  364. break;
  365. case 'T': // %H:%M:%S
  366. {
  367. const char * s_save = s;
  368. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  369. if (working && *s == ':')
  370. {
  371. ++ s;
  372. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  373. if (working && *s == ':')
  374. {
  375. ++ s;
  376. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  377. }
  378. }
  379. if (!working)
  380. s = s_save;
  381. }
  382. break;
  383. case 'w': // weekday number 0->6 sunday->saturday
  384. working = strp_atoi(s, tm->tm_wday, 0, 6, 0);
  385. break;
  386. case 'Y': // year
  387. working = strp_atoi(s, tm->tm_year, 1900, 65535, -1900);
  388. break;
  389. case 'y': // 2-digit year
  390. working = strp_atoi(s, tm->tm_year, 0, 99, 0);
  391. if (working && tm->tm_year < 69)
  392. tm->tm_year += 100;
  393. break;
  394. case '%': // escaped
  395. if (*s != '%')
  396. working = false;
  397. ++s;
  398. break;
  399. default:
  400. working = false;
  401. }
  402. }
  403. break;
  404. case ' ':
  405. case '\t':
  406. case '\r':
  407. case '\n':
  408. case '\f':
  409. case '\v':
  410. // zero or more whitespaces:
  411. while (isspace((int)*s))
  412. ++ s;
  413. break;
  414. default:
  415. // match character
  416. if (*s != *format)
  417. working = false;
  418. else
  419. ++s;
  420. break;
  421. }
  422. ++format;
  423. }
  424. return (working?(char *)s:0);
  425. }
  426. #endif