procheader.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include <glib.h>
  23. #include <glib/gi18n.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <sys/stat.h>
  29. #include "procheader.h"
  30. #include "procmsg.h"
  31. #include "codeconv.h"
  32. #include "prefs_common.h"
  33. #include "utils.h"
  34. #define BUFFSIZE 8192
  35. typedef char *(*getlinefunc) (char *, size_t, void *);
  36. typedef int (*peekcharfunc) (void *);
  37. typedef int (*getcharfunc) (void *);
  38. typedef gint (*get_one_field_func) (gchar *, size_t, void *, HeaderEntry[]);
  39. static gint string_get_one_field(gchar *buf, size_t len, char **str,
  40. HeaderEntry hentry[]);
  41. static char *string_getline(char *buf, size_t len, char **str);
  42. static int string_peekchar(char **str);
  43. static int file_peekchar(FILE *fp);
  44. static gint generic_get_one_field(gchar *buf, size_t len, void *data,
  45. HeaderEntry hentry[],
  46. getlinefunc getline,
  47. peekcharfunc peekchar,
  48. gboolean unfold);
  49. static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
  50. gboolean full, gboolean decrypted);
  51. gint procheader_get_one_field(gchar *buf, size_t len, FILE *fp,
  52. HeaderEntry hentry[])
  53. {
  54. return generic_get_one_field(buf, len, fp, hentry,
  55. (getlinefunc)fgets, (peekcharfunc)file_peekchar,
  56. TRUE);
  57. }
  58. static gint string_get_one_field(gchar *buf, size_t len, char **str,
  59. HeaderEntry hentry[])
  60. {
  61. return generic_get_one_field(buf, len, str, hentry,
  62. (getlinefunc)string_getline,
  63. (peekcharfunc)string_peekchar,
  64. TRUE);
  65. }
  66. static char *string_getline(char *buf, size_t len, char **str)
  67. {
  68. if (!*str || !**str)
  69. return NULL;
  70. for (; **str && len > 1; --len)
  71. if ((*buf++ = *(*str)++) == '\n')
  72. break;
  73. *buf = '\0';
  74. return buf;
  75. }
  76. static int string_peekchar(char **str)
  77. {
  78. return **str;
  79. }
  80. static int file_peekchar(FILE *fp)
  81. {
  82. return ungetc(getc(fp), fp);
  83. }
  84. static gint generic_get_one_field(gchar *buf, size_t len, void *data,
  85. HeaderEntry *hentry,
  86. getlinefunc getline, peekcharfunc peekchar,
  87. gboolean unfold)
  88. {
  89. gint nexthead;
  90. gint hnum = 0;
  91. HeaderEntry *hp = NULL;
  92. if (hentry != NULL) {
  93. /* skip non-required headers */
  94. do {
  95. do {
  96. if (getline(buf, len, data) == NULL)
  97. return -1;
  98. if (buf[0] == '\r' || buf[0] == '\n')
  99. return -1;
  100. } while (buf[0] == ' ' || buf[0] == '\t');
  101. for (hp = hentry, hnum = 0; hp->name != NULL;
  102. hp++, hnum++) {
  103. if (!g_ascii_strncasecmp(hp->name, buf,
  104. strlen(hp->name)))
  105. break;
  106. }
  107. } while (hp->name == NULL);
  108. } else {
  109. if (getline(buf, len, data) == NULL) return -1;
  110. if (buf[0] == '\r' || buf[0] == '\n') return -1;
  111. }
  112. /* unfold line */
  113. while (1) {
  114. nexthead = peekchar(data);
  115. /* ([*WSP CRLF] 1*WSP) */
  116. if (nexthead == ' ' || nexthead == '\t') {
  117. size_t buflen;
  118. gboolean skiptab = (nexthead == '\t');
  119. /* trim previous trailing \n if requesting one header or
  120. * unfolding was requested */
  121. if ((!hentry && unfold) || (hp && hp->unfold))
  122. strretchomp(buf);
  123. buflen = strlen(buf);
  124. /* concatenate next line */
  125. if ((len - buflen) > 2) {
  126. if (getline(buf + buflen, len - buflen, data) == NULL)
  127. break;
  128. if (skiptab) { /* replace tab with space */
  129. *(buf + buflen) = ' ';
  130. }
  131. } else
  132. break;
  133. } else {
  134. /* remove trailing new line */
  135. strretchomp(buf);
  136. break;
  137. }
  138. }
  139. return hnum;
  140. }
  141. gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
  142. {
  143. return generic_get_one_field(buf, len, fp, NULL,
  144. (getlinefunc)fgets,
  145. (peekcharfunc)file_peekchar,
  146. FALSE);
  147. }
  148. GPtrArray *procheader_get_header_array_asis(FILE *fp)
  149. {
  150. gchar buf[BUFFSIZE];
  151. GPtrArray *headers;
  152. Header *header;
  153. g_return_val_if_fail(fp != NULL, NULL);
  154. headers = g_ptr_array_new();
  155. while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
  156. if ((header = procheader_parse_header(buf)) != NULL)
  157. g_ptr_array_add(headers, header);
  158. /*
  159. if (*buf == ':') continue;
  160. for (p = buf; *p && *p != ' '; p++) {
  161. if (*p == ':') {
  162. header = g_new(Header, 1);
  163. header->name = g_strndup(buf, p - buf);
  164. p++;
  165. conv_unmime_header(tmp, sizeof(tmp), p, NULL);
  166. header->body = g_strdup(tmp);
  167. g_ptr_array_add(headers, header);
  168. break;
  169. }
  170. }
  171. */
  172. }
  173. return headers;
  174. }
  175. void procheader_header_array_destroy(GPtrArray *harray)
  176. {
  177. gint i;
  178. Header *header;
  179. for (i = 0; i < harray->len; i++) {
  180. header = g_ptr_array_index(harray, i);
  181. procheader_header_free(header);
  182. }
  183. g_ptr_array_free(harray, TRUE);
  184. }
  185. void procheader_header_free(Header *header)
  186. {
  187. if (!header) return;
  188. g_free(header->name);
  189. g_free(header->body);
  190. g_free(header);
  191. }
  192. /*
  193. tests whether two headers' names are equal
  194. remove the trailing ':' or ' ' before comparing
  195. */
  196. gboolean procheader_headername_equal(char * hdr1, char * hdr2)
  197. {
  198. int len1;
  199. int len2;
  200. len1 = strlen(hdr1);
  201. len2 = strlen(hdr2);
  202. if (hdr1[len1 - 1] == ':')
  203. len1--;
  204. if (hdr2[len2 - 1] == ':')
  205. len2--;
  206. if (len1 != len2)
  207. return 0;
  208. return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
  209. }
  210. /*
  211. parse headers, for example :
  212. From: dinh@enseirb.fr becomes :
  213. header->name = "From:"
  214. header->body = "dinh@enseirb.fr"
  215. */
  216. Header * procheader_parse_header(gchar * buf)
  217. {
  218. gchar *p = buf;
  219. Header * header;
  220. if ((*buf == ':') || (*buf == ' '))
  221. return NULL;
  222. for (p = buf; *p ; p++) {
  223. if ((*p == ':') || (*p == ' ')) {
  224. header = g_new(Header, 1);
  225. header->name = g_strndup(buf, p - buf + 1);
  226. p++;
  227. while (*p == ' ' || *p == '\t') p++;
  228. header->body = conv_unmime_header(p, NULL);
  229. return header;
  230. }
  231. }
  232. return NULL;
  233. }
  234. void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
  235. {
  236. gchar buf[BUFFSIZE];
  237. HeaderEntry *hp;
  238. gint hnum;
  239. gchar *p;
  240. if (hentry == NULL) return;
  241. while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
  242. != -1) {
  243. hp = hentry + hnum;
  244. p = buf + strlen(hp->name);
  245. while (*p == ' ' || *p == '\t') p++;
  246. if (hp->body == NULL)
  247. hp->body = g_strdup(p);
  248. else if (procheader_headername_equal(hp->name, "To") ||
  249. procheader_headername_equal(hp->name, "Cc")) {
  250. gchar *tp = hp->body;
  251. hp->body = g_strconcat(tp, ", ", p, NULL);
  252. g_free(tp);
  253. }
  254. }
  255. }
  256. MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
  257. gboolean full, gboolean decrypted)
  258. {
  259. struct stat s;
  260. FILE *fp;
  261. MsgInfo *msginfo;
  262. if (stat(file, &s) < 0) {
  263. FILE_OP_ERROR(file, "stat");
  264. return NULL;
  265. }
  266. if (!S_ISREG(s.st_mode))
  267. return NULL;
  268. if ((fp = g_fopen(file, "rb")) == NULL) {
  269. FILE_OP_ERROR(file, "fopen");
  270. return NULL;
  271. }
  272. msginfo = procheader_parse_stream(fp, flags, full, decrypted);
  273. fclose(fp);
  274. if (msginfo) {
  275. msginfo->size = s.st_size;
  276. msginfo->mtime = s.st_mtime;
  277. }
  278. return msginfo;
  279. }
  280. MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
  281. gboolean decrypted)
  282. {
  283. return parse_stream(&str, TRUE, flags, full, decrypted);
  284. }
  285. enum
  286. {
  287. H_DATE = 0,
  288. H_FROM = 1,
  289. H_TO = 2,
  290. H_CC = 3,
  291. H_NEWSGROUPS = 4,
  292. H_SUBJECT = 5,
  293. H_MSG_ID = 6,
  294. H_REFERENCES = 7,
  295. H_IN_REPLY_TO = 8,
  296. H_CONTENT_TYPE = 9,
  297. H_SEEN = 10,
  298. H_STATUS = 11,
  299. H_X_STATUS = 12,
  300. H_FROM_SPACE = 13,
  301. H_SC_PLANNED_DOWNLOAD = 14,
  302. H_SC_MESSAGE_SIZE = 15,
  303. H_FACE = 16,
  304. H_X_FACE = 17,
  305. H_DISPOSITION_NOTIFICATION_TO = 18,
  306. H_RETURN_RECEIPT_TO = 19,
  307. H_SC_PARTIALLY_RETRIEVED = 20,
  308. H_SC_ACCOUNT_SERVER = 21,
  309. H_SC_ACCOUNT_LOGIN = 22,
  310. H_LIST_POST = 23,
  311. H_LIST_SUBSCRIBE = 24,
  312. H_LIST_UNSUBSCRIBE = 25,
  313. H_LIST_HELP = 26,
  314. H_LIST_ARCHIVE = 27,
  315. H_LIST_OWNER = 28,
  316. };
  317. static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
  318. {"From:", NULL, TRUE},
  319. {"To:", NULL, TRUE},
  320. {"Cc:", NULL, TRUE},
  321. {"Newsgroups:", NULL, TRUE},
  322. {"Subject:", NULL, TRUE},
  323. {"Message-ID:", NULL, FALSE},
  324. {"References:", NULL, FALSE},
  325. {"In-Reply-To:", NULL, FALSE},
  326. {"Content-Type:", NULL, FALSE},
  327. {"Seen:", NULL, FALSE},
  328. {"Status:", NULL, FALSE},
  329. {"X-Status:", NULL, FALSE},
  330. {"From ", NULL, FALSE},
  331. {"SC-Marked-For-Download:", NULL, FALSE},
  332. {"SC-Message-Size:", NULL, FALSE},
  333. {"Face:", NULL, FALSE},
  334. {"X-Face:", NULL, FALSE},
  335. {"Disposition-Notification-To:", NULL, FALSE},
  336. {"Return-Receipt-To:", NULL, FALSE},
  337. {"SC-Partially-Retrieved:", NULL, FALSE},
  338. {"SC-Account-Server:", NULL, FALSE},
  339. {"SC-Account-Login:",NULL, FALSE},
  340. {"List-Post:", NULL, TRUE},
  341. {"List-Subscribe:", NULL, TRUE},
  342. {"List-Unsubscribe:",NULL, TRUE},
  343. {"List-Help:", NULL, TRUE},
  344. {"List-Archive:", NULL, TRUE},
  345. {"List-Owner:", NULL, TRUE},
  346. {NULL, NULL, FALSE}};
  347. static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
  348. {"From:", NULL, TRUE},
  349. {"To:", NULL, TRUE},
  350. {"Cc:", NULL, TRUE},
  351. {"Newsgroups:", NULL, TRUE},
  352. {"Subject:", NULL, TRUE},
  353. {"Message-ID:", NULL, FALSE},
  354. {"References:", NULL, FALSE},
  355. {"In-Reply-To:", NULL, FALSE},
  356. {"Content-Type:", NULL, FALSE},
  357. {"Seen:", NULL, FALSE},
  358. {"Status:", NULL, FALSE},
  359. {"X-Status:", NULL, FALSE},
  360. {"From ", NULL, FALSE},
  361. {"SC-Marked-For-Download:", NULL, FALSE},
  362. {"SC-Message-Size:",NULL, FALSE},
  363. {NULL, NULL, FALSE}};
  364. HeaderEntry* procheader_get_headernames(gboolean full)
  365. {
  366. return full ? hentry_full : hentry_short;
  367. }
  368. MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
  369. gboolean decrypted)
  370. {
  371. return parse_stream(fp, FALSE, flags, full, decrypted);
  372. }
  373. static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
  374. gboolean full, gboolean decrypted)
  375. {
  376. MsgInfo *msginfo;
  377. gchar buf[BUFFSIZE];
  378. gchar *p, *tmp;
  379. gchar *hp;
  380. HeaderEntry *hentry;
  381. gint hnum;
  382. void *orig_data = data;
  383. get_one_field_func get_one_field =
  384. isstring ? (get_one_field_func)string_get_one_field
  385. : (get_one_field_func)procheader_get_one_field;
  386. hentry = procheader_get_headernames(full);
  387. if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
  388. while (get_one_field(buf, sizeof(buf), data, NULL) != -1) {
  389. if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
  390. strlen("X-Sylpheed-End-Special-Headers:")))
  391. break;
  392. /* from other mailers */
  393. if (!strncmp(buf, "Date: ", 6)
  394. || !strncmp(buf, "To: ", 4)
  395. || !strncmp(buf, "From: ", 6)
  396. || !strncmp(buf, "Subject: ", 9)) {
  397. if (isstring)
  398. data = orig_data;
  399. else
  400. rewind((FILE *)data);
  401. break;
  402. }
  403. }
  404. }
  405. msginfo = procmsg_msginfo_new();
  406. if (flags.tmp_flags || flags.perm_flags)
  407. msginfo->flags = flags;
  408. else
  409. MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
  410. msginfo->inreplyto = NULL;
  411. while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
  412. != -1) {
  413. hp = buf + strlen(hentry[hnum].name);
  414. while (*hp == ' ' || *hp == '\t') hp++;
  415. switch (hnum) {
  416. case H_DATE:
  417. if (msginfo->date) break;
  418. msginfo->date_t =
  419. procheader_date_parse(NULL, hp, 0);
  420. if (g_utf8_validate(hp, -1, NULL)) {
  421. msginfo->date = g_strdup(hp);
  422. } else {
  423. gchar *utf = conv_codeset_strdup(
  424. hp,
  425. conv_get_locale_charset_str_no_utf8(),
  426. CS_INTERNAL);
  427. if (utf == NULL ||
  428. !g_utf8_validate(utf, -1, NULL)) {
  429. g_free(utf);
  430. utf = g_malloc(strlen(buf)*2+1);
  431. conv_localetodisp(utf,
  432. strlen(hp)*2+1, hp);
  433. }
  434. msginfo->date = utf;
  435. }
  436. break;
  437. case H_FROM:
  438. if (msginfo->from) break;
  439. msginfo->from = conv_unmime_header(hp, NULL);
  440. msginfo->fromname = procheader_get_fromname(msginfo->from);
  441. remove_return(msginfo->from);
  442. remove_return(msginfo->fromname);
  443. break;
  444. case H_TO:
  445. tmp = conv_unmime_header(hp, NULL);
  446. remove_return(tmp);
  447. if (msginfo->to) {
  448. p = msginfo->to;
  449. msginfo->to =
  450. g_strconcat(p, ", ", tmp, NULL);
  451. g_free(p);
  452. } else
  453. msginfo->to = g_strdup(tmp);
  454. g_free(tmp);
  455. break;
  456. case H_CC:
  457. tmp = conv_unmime_header(hp, NULL);
  458. remove_return(tmp);
  459. if (msginfo->cc) {
  460. p = msginfo->cc;
  461. msginfo->cc =
  462. g_strconcat(p, ", ", tmp, NULL);
  463. g_free(p);
  464. } else
  465. msginfo->cc = g_strdup(tmp);
  466. g_free(tmp);
  467. break;
  468. case H_NEWSGROUPS:
  469. if (msginfo->newsgroups) {
  470. p = msginfo->newsgroups;
  471. msginfo->newsgroups =
  472. g_strconcat(p, ",", hp, NULL);
  473. g_free(p);
  474. } else
  475. msginfo->newsgroups = g_strdup(hp);
  476. break;
  477. case H_SUBJECT:
  478. if (msginfo->subject) break;
  479. msginfo->subject = conv_unmime_header(hp, NULL);
  480. break;
  481. case H_MSG_ID:
  482. if (msginfo->msgid) break;
  483. extract_parenthesis(hp, '<', '>');
  484. remove_space(hp);
  485. msginfo->msgid = g_strdup(hp);
  486. break;
  487. case H_REFERENCES:
  488. msginfo->references =
  489. references_list_prepend(msginfo->references,
  490. hp);
  491. break;
  492. case H_IN_REPLY_TO:
  493. if (msginfo->inreplyto) break;
  494. eliminate_parenthesis(hp, '(', ')');
  495. if ((p = strrchr(hp, '<')) != NULL &&
  496. strchr(p + 1, '>') != NULL) {
  497. extract_parenthesis(p, '<', '>');
  498. remove_space(p);
  499. if (*p != '\0')
  500. msginfo->inreplyto = g_strdup(p);
  501. }
  502. break;
  503. case H_CONTENT_TYPE:
  504. if (!g_ascii_strncasecmp(hp, "multipart/", 10))
  505. MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
  506. break;
  507. #ifdef ALLOW_HEADER_HINT
  508. case H_SEEN:
  509. /* mnews Seen header */
  510. MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
  511. break;
  512. #endif
  513. case H_FACE:
  514. if (!msginfo->extradata)
  515. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  516. if (msginfo->extradata->face) break;
  517. msginfo->extradata->face = g_strdup(hp);
  518. break;
  519. case H_X_FACE:
  520. if (!msginfo->extradata)
  521. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  522. if (msginfo->extradata->xface) break;
  523. msginfo->extradata->xface = g_strdup(hp);
  524. break;
  525. case H_DISPOSITION_NOTIFICATION_TO:
  526. if (!msginfo->extradata)
  527. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  528. if (msginfo->extradata->dispositionnotificationto) break;
  529. msginfo->extradata->dispositionnotificationto = g_strdup(hp);
  530. break;
  531. case H_RETURN_RECEIPT_TO:
  532. if (!msginfo->extradata)
  533. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  534. if (msginfo->extradata->returnreceiptto) break;
  535. msginfo->extradata->returnreceiptto = g_strdup(hp);
  536. break;
  537. /* partial download infos */
  538. case H_SC_PARTIALLY_RETRIEVED:
  539. if (!msginfo->extradata)
  540. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  541. if (msginfo->extradata->partial_recv) break;
  542. msginfo->extradata->partial_recv = g_strdup(hp);
  543. break;
  544. case H_SC_ACCOUNT_SERVER:
  545. if (!msginfo->extradata)
  546. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  547. if (msginfo->extradata->account_server) break;
  548. msginfo->extradata->account_server = g_strdup(hp);
  549. break;
  550. case H_SC_ACCOUNT_LOGIN:
  551. if (!msginfo->extradata)
  552. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  553. if (msginfo->extradata->account_login) break;
  554. msginfo->extradata->account_login = g_strdup(hp);
  555. break;
  556. case H_SC_MESSAGE_SIZE:
  557. if (msginfo->total_size) break;
  558. msginfo->total_size = atoi(hp);
  559. break;
  560. case H_SC_PLANNED_DOWNLOAD:
  561. msginfo->planned_download = atoi(hp);
  562. break;
  563. /* end partial download infos */
  564. #ifdef ALLOW_HEADER_HINT
  565. case H_STATUS:
  566. if (strchr(hp, 'R') != NULL)
  567. MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
  568. if (strchr(hp, 'O') != NULL)
  569. MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
  570. if (strchr(hp, 'U') != NULL)
  571. MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
  572. break;
  573. case H_X_STATUS:
  574. if (strchr(hp, 'D') != NULL)
  575. MSG_SET_PERM_FLAGS(msginfo->flags,
  576. MSG_REALLY_DELETED);
  577. if (strchr(hp, 'F') != NULL)
  578. MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
  579. if (strchr(hp, 'd') != NULL)
  580. MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
  581. if (strchr(hp, 'r') != NULL)
  582. MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
  583. if (strchr(hp, 'f') != NULL)
  584. MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
  585. break;
  586. #endif
  587. case H_FROM_SPACE:
  588. if (msginfo->fromspace) break;
  589. msginfo->fromspace = g_strdup(hp);
  590. remove_return(msginfo->fromspace);
  591. break;
  592. /* list infos */
  593. case H_LIST_POST:
  594. if (!msginfo->extradata)
  595. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  596. if (msginfo->extradata->list_post) break;
  597. msginfo->extradata->list_post = g_strdup(hp);
  598. break;
  599. case H_LIST_SUBSCRIBE:
  600. if (!msginfo->extradata)
  601. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  602. if (msginfo->extradata->list_subscribe) break;
  603. msginfo->extradata->list_subscribe = g_strdup(hp);
  604. break;
  605. case H_LIST_UNSUBSCRIBE:
  606. if (!msginfo->extradata)
  607. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  608. if (msginfo->extradata->list_unsubscribe) break;
  609. msginfo->extradata->list_unsubscribe = g_strdup(hp);
  610. break;
  611. case H_LIST_HELP:
  612. if (!msginfo->extradata)
  613. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  614. if (msginfo->extradata->list_help) break;
  615. msginfo->extradata->list_help = g_strdup(hp);
  616. break;
  617. case H_LIST_ARCHIVE:
  618. if (!msginfo->extradata)
  619. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  620. if (msginfo->extradata->list_archive) break;
  621. msginfo->extradata->list_archive = g_strdup(hp);
  622. break;
  623. case H_LIST_OWNER:
  624. if (!msginfo->extradata)
  625. msginfo->extradata = g_new0(MsgInfoExtraData, 1);
  626. if (msginfo->extradata->list_owner) break;
  627. msginfo->extradata->list_owner = g_strdup(hp);
  628. break;
  629. /* end list infos */
  630. default:
  631. break;
  632. }
  633. }
  634. if (!msginfo->inreplyto && msginfo->references)
  635. msginfo->inreplyto =
  636. g_strdup((gchar *)msginfo->references->data);
  637. return msginfo;
  638. }
  639. gchar *procheader_get_fromname(const gchar *str)
  640. {
  641. gchar *tmp, *name;
  642. Xstrdup_a(tmp, str, return NULL);
  643. if (*tmp == '\"') {
  644. extract_quote(tmp, '\"');
  645. g_strstrip(tmp);
  646. } else if (strchr(tmp, '<')) {
  647. eliminate_parenthesis(tmp, '<', '>');
  648. g_strstrip(tmp);
  649. if (*tmp == '\0') {
  650. strcpy(tmp, str);
  651. extract_parenthesis(tmp, '<', '>');
  652. g_strstrip(tmp);
  653. }
  654. } else if (strchr(tmp, '(')) {
  655. extract_parenthesis(tmp, '(', ')');
  656. g_strstrip(tmp);
  657. }
  658. if (*tmp == '\0')
  659. name = g_strdup(str);
  660. else
  661. name = g_strdup(tmp);
  662. return name;
  663. }
  664. static gint procheader_scan_date_string(const gchar *str,
  665. gchar *weekday, gint *day,
  666. gchar *month, gint *year,
  667. gint *hh, gint *mm, gint *ss,
  668. gchar *zone)
  669. {
  670. gint result;
  671. result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
  672. weekday, day, month, year, hh, mm, ss, zone);
  673. if (result == 8) return 0;
  674. result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
  675. weekday, day, month, year, hh, mm, ss, zone);
  676. if (result == 8) return 0;
  677. result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
  678. day, month, year, hh, mm, ss, zone);
  679. if (result == 7) return 0;
  680. *zone = '\0';
  681. result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
  682. weekday, day, month, year, hh, mm, ss);
  683. if (result == 7) return 0;
  684. result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
  685. day, month, year, hh, mm, ss);
  686. if (result == 6) return 0;
  687. *ss = 0;
  688. result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
  689. weekday, day, month, year, hh, mm, zone);
  690. if (result == 7) return 0;
  691. result = sscanf(str, "%d %9s %d %2d:%2d %5s",
  692. day, month, year, hh, mm, zone);
  693. if (result == 6) return 0;
  694. *zone = '\0';
  695. result = sscanf(str, "%10s %d %9s %d %2d:%2d",
  696. weekday, day, month, year, hh, mm);
  697. if (result == 6) return 0;
  698. result = sscanf(str, "%d %9s %d %2d:%2d",
  699. day, month, year, hh, mm);
  700. if (result == 5) return 0;
  701. return -1;
  702. }
  703. /*
  704. * Hiro, most UNIXen support this function:
  705. * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
  706. */
  707. gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
  708. {
  709. static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  710. gchar weekday[11];
  711. gint day;
  712. gchar month[10];
  713. gint year;
  714. gint hh, mm, ss;
  715. GDateMonth dmonth;
  716. gchar *p;
  717. if (!t)
  718. return FALSE;
  719. memset(t, 0, sizeof *t);
  720. if (procheader_scan_date_string(src, weekday, &day, month, &year,
  721. &hh, &mm, &ss, zone) < 0) {
  722. g_warning("Invalid date: %s\n", src);
  723. return FALSE;
  724. }
  725. /* Y2K compliant :) */
  726. if (year < 100) {
  727. if (year < 70)
  728. year += 2000;
  729. else
  730. year += 1900;
  731. }
  732. month[3] = '\0';
  733. if ((p = strstr(monthstr, month)) != NULL)
  734. dmonth = (gint)(p - monthstr) / 3 + 1;
  735. else {
  736. g_warning("Invalid month: %s\n", month);
  737. dmonth = G_DATE_BAD_MONTH;
  738. }
  739. t->tm_sec = ss;
  740. t->tm_min = mm;
  741. t->tm_hour = hh;
  742. t->tm_mday = day;
  743. t->tm_mon = dmonth - 1;
  744. t->tm_year = year - 1900;
  745. t->tm_wday = 0;
  746. t->tm_yday = 0;
  747. t->tm_isdst = -1;
  748. mktime(t);
  749. return TRUE;
  750. }
  751. time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
  752. {
  753. static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  754. gchar weekday[11];
  755. gint day;
  756. gchar month[10];
  757. gint year;
  758. gint hh, mm, ss;
  759. gchar zone[6];
  760. GDateMonth dmonth = G_DATE_BAD_MONTH;
  761. struct tm t;
  762. gchar *p;
  763. time_t timer;
  764. time_t tz_offset;
  765. if (procheader_scan_date_string(src, weekday, &day, month, &year,
  766. &hh, &mm, &ss, zone) < 0) {
  767. if (dest && len > 0)
  768. strncpy2(dest, src, len);
  769. return 0;
  770. }
  771. /* Y2K compliant :) */
  772. if (year < 1000) {
  773. if (year < 50)
  774. year += 2000;
  775. else
  776. year += 1900;
  777. }
  778. month[3] = '\0';
  779. for (p = monthstr; *p != '\0'; p += 3) {
  780. if (!g_ascii_strncasecmp(p, month, 3)) {
  781. dmonth = (gint)(p - monthstr) / 3 + 1;
  782. break;
  783. }
  784. }
  785. t.tm_sec = ss;
  786. t.tm_min = mm;
  787. t.tm_hour = hh;
  788. t.tm_mday = day;
  789. t.tm_mon = dmonth - 1;
  790. t.tm_year = year - 1900;
  791. t.tm_wday = 0;
  792. t.tm_yday = 0;
  793. t.tm_isdst = -1;
  794. timer = mktime(&t);
  795. tz_offset = remote_tzoffset_sec(zone);
  796. if (tz_offset != -1)
  797. timer += tzoffset_sec(&timer) - tz_offset;
  798. if (dest)
  799. procheader_date_get_localtime(dest, len, timer);
  800. return timer;
  801. }
  802. void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
  803. {
  804. struct tm *lt;
  805. gchar *default_format = "%y/%m/%d(%a) %H:%M";
  806. gchar *str;
  807. const gchar *src_codeset, *dest_codeset;
  808. lt = localtime(&timer);
  809. if (prefs_common.date_format)
  810. fast_strftime(dest, len, prefs_common.date_format, lt);
  811. else
  812. fast_strftime(dest, len, default_format, lt);
  813. if (!g_utf8_validate(dest, -1, NULL)) {
  814. src_codeset = conv_get_locale_charset_str_no_utf8();
  815. dest_codeset = CS_UTF_8;
  816. str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
  817. if (str) {
  818. strncpy2(dest, str, len);
  819. g_free(str);
  820. }
  821. }
  822. }
  823. /* Added by Mel Hadasht on 27 Aug 2001 */
  824. /* Get a header from msginfo */
  825. gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
  826. {
  827. gchar *file;
  828. FILE *fp;
  829. HeaderEntry hentry[]={ { NULL, NULL, TRUE },
  830. { NULL, NULL, FALSE } };
  831. gint val;
  832. hentry[0].name = header;
  833. g_return_val_if_fail(msginfo != NULL, -1);
  834. file = procmsg_get_message_file_path(msginfo);
  835. if ((fp = g_fopen(file, "rb")) == NULL) {
  836. FILE_OP_ERROR(file, "fopen");
  837. g_free(file);
  838. return -1;
  839. }
  840. val = procheader_get_one_field(buf,len, fp, hentry);
  841. if (fclose(fp) == EOF) {
  842. FILE_OP_ERROR(file, "fclose");
  843. g_unlink(file);
  844. g_free(file);
  845. return -1;
  846. }
  847. g_free(file);
  848. if (val == -1)
  849. return -1;
  850. return 0;
  851. }