2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 Hiroyuki Yamamoto
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include "procheader.h"
38 #include "statusbar.h"
39 #include "prefs_account.h"
43 static GList *session_list = NULL;
45 static gint imap_cmd_count = 0;
47 static GSList *imap_get_uncached_messages(IMAPSession *session,
51 static GSList *imap_delete_messages (GSList *mlist,
54 static void imap_delete_all_messages (FolderItem *item);
56 static SockInfo *imap_open (const gchar *server,
59 static gint imap_auth (SockInfo *sock,
62 static gint imap_logout (SockInfo *sock);
63 static gint imap_noop (SockInfo *sock);
64 static gint imap_select (SockInfo *sock,
70 static gint imap_create (SockInfo *sock,
72 static gint imap_delete (SockInfo *sock,
74 static gint imap_get_envelope (SockInfo *sock,
78 static gint imap_search (SockInfo *sock,
81 static gint imap_get_message (SockInfo *sock,
83 const gchar *filename);
84 static gint imap_copy_message (SockInfo *sock,
86 const gchar *destfolder);
87 static gint imap_store (SockInfo *sock,
92 static gint imap_set_article_flags (IMAPSession *session,
97 static gint imap_expunge (IMAPSession *session);
99 static gchar *imap_parse_atom (SockInfo *sock,
103 static gchar *imap_parse_one_address (SockInfo *sock,
106 gchar *out_fromname_str,
108 static gchar *imap_parse_address (SockInfo *sock,
110 gchar **out_from_str,
111 gchar **out_fromname_str,
113 static MsgFlags imap_parse_flags (const gchar *flag_str);
114 static MsgInfo *imap_parse_envelope (SockInfo *sock,
117 static gint imap_ok (SockInfo *sock,
119 static void imap_gen_send (SockInfo *sock,
120 const gchar *format, ...);
121 static gint imap_gen_recv (SockInfo *sock,
125 static gchar *search_array_contain_str (GPtrArray *array,
127 static void imap_path_subst_slash_to_dot(gchar *str);
130 static IMAPSession *imap_session_connect_if_not(Folder *folder)
132 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
134 g_return_val_if_fail(folder != NULL, NULL);
135 g_return_val_if_fail(folder->type == F_IMAP, NULL);
136 g_return_val_if_fail(folder->account != NULL, NULL);
138 if (!rfolder->session) {
140 imap_session_new(folder->account->recv_server, 143,
141 folder->account->userid,
142 folder->account->passwd);
144 if (imap_noop(rfolder->session->sock) != IMAP_SUCCESS) {
145 log_warning(_("IMAP4 connection to %s:%d has been"
146 " disconnected. Reconnecting...\n"),
147 folder->account->recv_server, 143);
148 session_destroy(rfolder->session);
150 imap_session_new(folder->account->recv_server,
151 143, folder->account->userid,
152 folder->account->passwd);
156 return IMAP_SESSION(rfolder->session);
159 Session *imap_session_new(const gchar *server, gushort port,
160 const gchar *user, const gchar *pass)
162 gchar buf[IMAPBUFSIZE];
163 IMAPSession *session;
166 g_return_val_if_fail(server != NULL, NULL);
168 log_message(_("creating IMAP4 connection to %s:%d ...\n"),
171 if ((imap_sock = imap_open(server, port, buf)) == NULL)
173 if (imap_auth(imap_sock, user, pass) != IMAP_SUCCESS) {
174 imap_logout(imap_sock);
175 sock_close(imap_sock);
179 session = g_new(IMAPSession, 1);
180 SESSION(session)->type = SESSION_IMAP;
181 SESSION(session)->server = g_strdup(server);
182 SESSION(session)->sock = imap_sock;
183 SESSION(session)->connected = TRUE;
184 SESSION(session)->phase = SESSION_READY;
185 SESSION(session)->data = NULL;
186 session->mbox = NULL;
188 session_list = g_list_append(session_list, session);
190 return SESSION(session);
193 void imap_session_destroy(IMAPSession *session)
195 sock_close(SESSION(session)->sock);
196 SESSION(session)->sock = NULL;
198 g_free(session->mbox);
200 session_list = g_list_remove(session_list, session);
203 void imap_session_destroy_all(void)
205 while (session_list != NULL) {
206 IMAPSession *session = (IMAPSession *)session_list->data;
208 imap_logout(SESSION(session)->sock);
209 imap_session_destroy(session);
213 GSList *imap_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
215 GSList *mlist = NULL;
216 IMAPSession *session;
218 g_return_val_if_fail(folder != NULL, NULL);
219 g_return_val_if_fail(item != NULL, NULL);
220 g_return_val_if_fail(folder->type == F_IMAP, NULL);
221 g_return_val_if_fail(folder->account != NULL, NULL);
223 session = imap_session_connect_if_not(folder);
226 mlist = procmsg_read_cache(item, FALSE);
227 item->last_num = procmsg_get_last_num_in_cache(mlist);
228 procmsg_set_flags(mlist, item);
230 gint ok, exists = 0, recent = 0, unseen = 0, begin = 1;
231 gulong uid = 0, last_uid = item->mtime;
233 ok = imap_select(SESSION(session)->sock, item->path,
234 &exists, &recent, &unseen, &uid);
235 if (ok != IMAP_SUCCESS) {
236 log_warning(_("can't select folder: %s\n"), item->path);
243 mlist = procmsg_read_cache(item, FALSE);
244 procmsg_set_flags(mlist, item);
245 cache_last = procmsg_get_last_num_in_cache(mlist);
247 /* calculating the range of envelope to get */
248 if (exists < cache_last) {
249 /* some messages are deleted (get all) */
251 } else if (exists == cache_last) {
252 if (last_uid != 0 && last_uid != uid) {
253 /* some recent but deleted (get all) */
256 /* mailbox unchanged (get none)*/
260 if (exists == cache_last + recent) {
262 begin = cache_last + 1;
264 /* some recent but deleted (get all) */
272 if (1 < begin && begin <= exists) {
275 newlist = imap_get_uncached_messages
276 (session, item, begin, exists);
277 imap_delete_messages(mlist, begin, INT_MAX);
278 mlist = g_slist_concat(mlist, newlist);
279 } else if (begin == 1) {
280 mlist = imap_get_uncached_messages
281 (session, item, 1, exists);
282 imap_delete_all_messages(item);
285 item->last_num = exists;
293 gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint num)
295 gchar *path, *filename;
296 IMAPSession *session;
299 g_return_val_if_fail(folder != NULL, NULL);
300 g_return_val_if_fail(item != NULL, NULL);
302 session = imap_session_connect_if_not(folder);
304 path = folder_item_get_path(item);
305 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
308 if (is_file_exist(filename)) {
309 debug_print(_("message %d has been already cached.\n"), num);
318 debug_print(_("getting message %d...\n"), num);
319 ok = imap_get_message(SESSION(session)->sock, num, filename);
323 if (ok != IMAP_SUCCESS) {
324 g_warning(_("can't fetch message %d\n"), num);
332 gint imap_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
335 IMAPSession *session;
337 g_return_val_if_fail(folder != NULL, -1);
338 g_return_val_if_fail(dest != NULL, -1);
339 g_return_val_if_fail(msginfo != NULL, -1);
341 session = imap_session_connect_if_not(folder);
342 if (!session) return -1;
344 if (msginfo->folder == dest) {
345 g_warning(_("the src folder is identical to the dest.\n"));
349 Xstrdup_a(destdir, dest->path, return -1);
350 /* imap_path_subst_slash_to_dot(destdir); */
352 imap_copy_message(SESSION(session)->sock, msginfo->msgnum, destdir);
353 imap_set_article_flags(session, msginfo->msgnum, msginfo->msgnum,
354 IMAP_FLAG_DELETED, TRUE);
355 debug_print(_("Moving message %s%c%d to %s ...\n"),
356 msginfo->folder->path, G_DIR_SEPARATOR,
357 msginfo->msgnum, dest->path);
359 imap_expunge(session);
364 gint imap_move_msgs_with_dest(Folder *folder, FolderItem *dest,
370 IMAPSession *session;
372 g_return_val_if_fail(folder != NULL, -1);
373 g_return_val_if_fail(dest != NULL, -1);
374 g_return_val_if_fail(msglist != NULL, -1);
376 session = imap_session_connect_if_not(folder);
377 if (!session) return -1;
379 Xstrdup_a(destdir, dest->path, return -1);
380 /* imap_path_subst_slash_to_dot(destdir); */
382 for (cur = msglist; cur != NULL; cur = cur->next) {
383 msginfo = (MsgInfo *)cur->data;
385 if (msginfo->folder == dest) {
386 g_warning(_("the src folder is identical to the dest.\n"));
389 imap_copy_message(SESSION(session)->sock,
390 msginfo->msgnum, destdir);
391 imap_set_article_flags
392 (session, msginfo->msgnum, msginfo->msgnum,
393 IMAP_FLAG_DELETED, TRUE);
395 debug_print(_("Moving message %s%c%d to %s ...\n"),
396 msginfo->folder->path, G_DIR_SEPARATOR,
397 msginfo->msgnum, dest->path);
401 imap_expunge(session);
407 gint imap_remove_msg(Folder *folder, FolderItem *item, gint num)
409 gint exists, recent, unseen;
412 IMAPSession *session;
414 g_return_val_if_fail(folder != NULL, -1);
415 g_return_val_if_fail(item != NULL, -1);
416 g_return_val_if_fail(num > 0 && num <= item->last_num, -1);
418 session = imap_session_connect_if_not(folder);
419 if (!session) return -1;
421 ok = imap_select(SESSION(session)->sock, item->path,
422 &exists, &recent, &unseen, &uid);
423 if (ok != IMAP_SUCCESS) {
424 log_warning(_("can't select folder: %s\n"), item->path);
428 ok = imap_set_article_flags(IMAP_SESSION(REMOTE_FOLDER(folder)->session),
429 num, num, IMAP_FLAG_DELETED, TRUE);
430 if (ok != IMAP_SUCCESS) {
431 log_warning(_("can't set deleted flags: %d\n"), num);
435 ok = imap_expunge(session);
436 if (ok != IMAP_SUCCESS) {
437 log_warning(_("can't expunge\n"));
444 gint imap_remove_all_msg(Folder *folder, FolderItem *item)
446 gint exists, recent, unseen;
449 IMAPSession *session;
451 g_return_val_if_fail(folder != NULL, -1);
452 g_return_val_if_fail(item != NULL, -1);
454 session = imap_session_connect_if_not(folder);
455 if (!session) return -1;
457 ok = imap_select(SESSION(session)->sock, item->path,
458 &exists, &recent, &unseen, &uid);
459 if (ok != IMAP_SUCCESS) {
460 log_warning(_("can't select folder: %s\n"), item->path);
464 ok = imap_set_article_flags(session, 1, exists,
465 IMAP_FLAG_DELETED, TRUE);
466 if (ok != IMAP_SUCCESS) {
467 log_warning(_("can't set deleted flags: 1:%d\n"), exists);
471 ok = imap_expunge(session);
472 if (ok != IMAP_SUCCESS) {
473 log_warning(_("can't expunge\n"));
480 void imap_scan_folder(Folder *folder, FolderItem *item)
484 FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
488 gchar *dirpath, *imappath;
489 IMAPSession *session;
490 FolderItem *new_item;
492 g_return_val_if_fail(folder != NULL, NULL);
493 g_return_val_if_fail(parent != NULL, NULL);
494 g_return_val_if_fail(name != NULL, NULL);
496 session = imap_session_connect_if_not(folder);
497 if (!session) return NULL;
500 dirpath = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
503 dirpath = g_strdup(name);
505 imappath = g_strdup(dirpath);
506 /* imap_path_subst_slash_to_dot(imappath); */
508 if (strcmp(name, "INBOX") != 0) {
509 ok = imap_create(SESSION(session)->sock, imappath);
510 if (ok != IMAP_SUCCESS) {
511 log_warning(_("can't create mailbox\n"));
518 new_item = folder_item_new(name, dirpath);
519 folder_item_append(parent, new_item);
526 gint imap_remove_folder(Folder *folder, FolderItem *item)
529 IMAPSession *session;
531 g_return_val_if_fail(folder != NULL, -1);
532 g_return_val_if_fail(item != NULL, -1);
533 g_return_val_if_fail(item->path != NULL, -1);
535 session = imap_session_connect_if_not(folder);
536 if (!session) return -1;
538 ok = imap_delete(SESSION(session)->sock, item->path);
539 if (ok != IMAP_SUCCESS) {
540 log_warning(_("can't delete mailbox\n"));
544 folder_item_remove(item);
549 static GSList *imap_get_uncached_messages(IMAPSession *session,
551 gint first, gint last)
553 gchar buf[IMAPBUFSIZE];
554 GSList *newlist = NULL;
555 GSList *llast = NULL;
558 g_return_val_if_fail(session != NULL, NULL);
559 g_return_val_if_fail(item != NULL, NULL);
560 g_return_val_if_fail(item->folder != NULL, NULL);
561 g_return_val_if_fail(item->folder->type == F_IMAP, NULL);
562 g_return_val_if_fail(first <= last, NULL);
564 if (imap_get_envelope(SESSION(session)->sock, first, last)
566 log_warning(_("can't get envelope\n"));
571 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
572 log_warning(_("error occurred while getting envelope.\n"));
576 if (buf[0] != '*' || buf[1] != ' ') break;
578 msginfo = imap_parse_envelope(SESSION(session)->sock, buf);
580 log_warning(_("can't parse envelope: %s\n"), buf);
584 msginfo->folder = item;
587 llast = newlist = g_slist_append(newlist, msginfo);
589 llast = g_slist_append(llast, msginfo);
597 static GSList *imap_delete_messages(GSList *mlist, gint first, gint last)
603 for (cur = mlist; cur != NULL; ) {
606 msginfo = (MsgInfo *)cur->data;
608 (first <= msginfo->msgnum ) &&
609 (msginfo->msgnum <= last)) {
610 debug_print(_("deleting message %d...\n"),
613 cache_file = procmsg_get_message_file_path(msginfo);
614 if (is_file_exist(cache_file)) unlink(cache_file);
617 procmsg_msginfo_free(msginfo);
618 mlist = g_slist_remove(mlist, msginfo);
627 static void imap_delete_all_messages(FolderItem *item)
634 g_return_if_fail(item != NULL);
635 g_return_if_fail(item->folder != NULL);
636 g_return_if_fail(item->folder->type == F_IMAP);
638 dir = folder_item_get_path(item);
639 if ((dp = opendir(dir)) == NULL) {
640 FILE_OP_ERROR(dir, "opendir");
645 debug_print(_("\tDeleting all cached messages... "));
647 while ((d = readdir(dp)) != NULL) {
648 if (to_number(d->d_name) < 0) continue;
650 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
652 if (is_file_exist(file)) {
653 if (unlink(file) < 0)
654 FILE_OP_ERROR(file, "unlink");
663 debug_print(_("done.\n"));
667 static SockInfo *imap_open(const gchar *server, gushort port, gchar *buf)
671 if ((sock = sock_connect(server, port)) == NULL) {
672 log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
679 if (imap_noop(sock) != IMAP_SUCCESS) {
687 static gint imap_auth(SockInfo *sock, const gchar *user, const gchar *pass)
692 imap_gen_send(sock, "LOGIN \"%s\" %s", user, pass);
693 argbuf = g_ptr_array_new();
694 ok = imap_ok(sock, argbuf);
695 if (ok != IMAP_SUCCESS) log_warning(_("IMAP4 login failed.\n"));
696 ptr_array_free_strings(argbuf);
697 g_ptr_array_free(argbuf, TRUE);
702 static gint imap_logout(SockInfo *sock)
704 imap_gen_send(sock, "LOGOUT");
705 return imap_ok(sock, NULL);
708 static gint imap_noop(SockInfo *sock)
710 imap_gen_send(sock, "NOOP");
711 return imap_ok(sock, NULL);
714 static gint imap_select(SockInfo *sock, const gchar *folder,
715 gint *exists, gint *recent, gint *unseen, gulong *uid)
722 *exists = *recent = *unseen = *uid = 0;
723 argbuf = g_ptr_array_new();
725 Xstrdup_a(imappath, folder, return -1);
726 /* imap_path_subst_slash_to_dot(imappath); */
728 imap_gen_send(sock, "SELECT \"%s\"", imappath);
729 if ((ok = imap_ok(sock, argbuf)) != IMAP_SUCCESS)
732 resp_str = search_array_contain_str(argbuf, "EXISTS");
734 if (sscanf(resp_str,"%d EXISTS", exists) != 1) {
735 g_warning("imap_select(): invalid EXISTS line.\n");
740 resp_str = search_array_contain_str(argbuf, "RECENT");
742 if (sscanf(resp_str, "%d RECENT", recent) != 1) {
743 g_warning("imap_select(): invalid RECENT line.\n");
748 resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
750 if (sscanf(resp_str, "OK [UIDVALIDITY %lu] ", uid) != 1) {
751 g_warning("imap_select(): invalid UIDVALIDITY line.\n");
756 resp_str = search_array_contain_str(argbuf, "UNSEEN");
758 if (sscanf(resp_str, "OK [UNSEEN %d] ", unseen) != 1) {
759 g_warning("imap_select(): invalid UNSEEN line.\n");
765 ptr_array_free_strings(argbuf);
766 g_ptr_array_free(argbuf, TRUE);
771 static gint imap_create(SockInfo *sock, const gchar *folder)
773 imap_gen_send(sock, "CREATE \"%s\"", folder);
774 return imap_ok(sock, NULL);
777 static gint imap_delete(SockInfo *sock, const gchar *folder)
779 imap_gen_send(sock, "DELETE \"%s\"", folder);
780 return imap_ok(sock, NULL);
783 static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)
788 tmp = strchr(src, ch);
789 if (!tmp || tmp == src)
792 memcpy(dest, src, MIN(tmp - src, len - 1));
793 dest[MIN(tmp - src, len - 1)] = '\0';
798 static gint imap_get_message(SockInfo *sock, gint num, const gchar *filename)
801 gchar buf[IMAPBUFSIZE];
806 g_return_val_if_fail(filename != NULL, IMAP_ERROR);
808 imap_gen_send(sock, "FETCH %d BODY[]", num);
810 if (sock_gets(sock, buf, sizeof(buf)) < 0)
813 if (buf[0] != '*' || buf[1] != ' ')
815 log_print("IMAP4< %s\n", buf);
817 cur_pos = strchr(buf, '{');
818 g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
819 cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
820 g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
821 size_num = atol(size_str);
823 if (*cur_pos != '\0') return IMAP_ERROR;
825 if (recv_bytes_write_to_file(sock, size_num, filename) != 0)
828 if (imap_gen_recv(sock, buf, sizeof(buf)) != IMAP_SUCCESS)
834 ok = imap_ok(sock, NULL);
839 static gint imap_copy_message(SockInfo *sock, gint num, const gchar *destfolder)
843 g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
845 imap_gen_send(sock, "COPY %d %s", num, destfolder);
846 ok = imap_ok(sock, NULL);
847 if (ok != IMAP_SUCCESS) {
848 log_warning(_("can't copy %d to %s\n"), num, destfolder);
855 static gchar *imap_parse_atom(SockInfo *sock, gchar *src, gchar *dest,
858 gchar *cur_pos = src;
860 while (*cur_pos == ' ') cur_pos++;
862 if (!strncmp(cur_pos, "NIL", 3)) {
865 } else if (*cur_pos == '\"') {
868 p = strchr_cpy(cur_pos + 1, '\"', dest, IMAPBUFSIZE);
869 cur_pos = p ? p : cur_pos + 2;
870 } else if (*cur_pos == '{') {
874 cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
877 g_return_val_if_fail(orig_buf != NULL, cur_pos);
879 if (sock_gets(sock, orig_buf, IMAPBUFSIZE) < 0)
881 strretchomp(orig_buf);
882 log_print("IMAP4< %s\n", orig_buf);
883 memcpy(dest, orig_buf, len);
885 cur_pos = orig_buf + len;
891 static gchar *imap_parse_one_address(SockInfo *sock, gchar *start,
893 gchar *out_fromname_str,
896 gchar buf[IMAPBUFSIZE];
897 gchar *cur_pos = start;
899 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
900 conv_unmime_header(out_fromname_str, 256, buf, NULL);
902 if (out_fromname_str[0] != '\0') {
903 strcat(out_from_str, "\"");
904 strcat(out_from_str, out_fromname_str);
905 strcat(out_from_str, "\"");
908 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
910 strcat(out_from_str, " <");
912 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
913 strcat(out_from_str, buf);
915 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
916 strcat(out_from_str, "@");
917 strcat(out_from_str, buf);
918 strcat(out_from_str, ">");
920 while (*cur_pos == ' ') cur_pos++;
922 g_return_val_if_fail(*cur_pos == ')', cur_pos + 1);
927 static gchar *imap_parse_address(SockInfo *sock, gchar *start,
928 gchar **out_from_str,
929 gchar **out_fromname_str,
932 gchar buf[IMAPBUFSIZE];
933 gchar name_buf[IMAPBUFSIZE];
934 gchar *cur_pos = start;
935 gboolean first = TRUE;
937 if (out_from_str) *out_from_str = NULL;
938 if (out_fromname_str) *out_fromname_str = NULL;
939 buf[0] = name_buf[0] = '\0';
941 if (!strncmp(cur_pos, "NIL", 3)) {
942 if (out_from_str) *out_from_str = g_strdup("");
943 if (out_fromname_str) *out_fromname_str = g_strdup("");
947 g_return_val_if_fail(*cur_pos == '(', NULL);
951 gchar ch = *cur_pos++;
952 if (ch == ')') break;
954 if (!first) strcat(buf, ", ");
956 cur_pos = imap_parse_one_address
957 (sock, cur_pos, buf, name_buf, orig_buf);
958 if (!cur_pos) return NULL;
962 if (out_from_str) *out_from_str = g_strdup(buf);
963 if (out_fromname_str) *out_fromname_str = g_strdup(name_buf);
968 static MsgFlags imap_parse_flags(const gchar *flag_str)
971 const gchar *cur_pos = flag_str;
972 const gchar *last_pos;
976 MSG_SET_FLAGS(flags, MSG_UNREAD|MSG_IMAP);
978 while (cur_pos != NULL) {
979 cur_pos = strchr(cur_pos, '\\');
980 if (cur_pos == NULL) break;
982 last_pos = cur_pos + 1;
983 cur_pos = strchr_cpy(last_pos, ' ', buf, sizeof(buf));
985 strncpy2(buf, last_pos, sizeof(buf));
987 if (g_strcasecmp(buf, "Recent") == 0) {
988 MSG_SET_FLAGS(flags, MSG_NEW|MSG_UNREAD);
989 } else if (g_strcasecmp(buf, "Seen") == 0) {
990 MSG_UNSET_FLAGS(flags, MSG_NEW|MSG_UNREAD);
991 } else if (g_strcasecmp(buf, "Deleted") == 0) {
992 MSG_SET_FLAGS(flags, MSG_DELETED);
993 } else if (g_strcasecmp(buf, "Flagged") == 0) {
994 MSG_SET_FLAGS(flags, MSG_MARKED);
1001 static MsgInfo *imap_parse_envelope(SockInfo *sock, gchar *line_str)
1004 gchar buf[IMAPBUFSIZE];
1005 gchar tmp[IMAPBUFSIZE];
1011 gchar *subject = NULL;
1013 gchar *tmp_fromname;
1015 gchar *fromname = NULL;
1018 gchar *inreplyto = NULL;
1019 gchar *msgid = NULL;
1022 log_print("IMAP4< %s\n", line_str);
1024 g_return_val_if_fail(line_str != NULL, NULL);
1025 g_return_val_if_fail(line_str[0] == '*' && line_str[1] == ' ', NULL);
1027 cur_pos = line_str + 2;
1029 #define PARSE_ONE_ELEMENT(ch) \
1031 cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf)); \
1032 g_return_val_if_fail(cur_pos != NULL, NULL); \
1035 PARSE_ONE_ELEMENT(' ');
1038 PARSE_ONE_ELEMENT(' ');
1039 g_return_val_if_fail(!strcmp(buf, "FETCH"), NULL);
1041 PARSE_ONE_ELEMENT(' ');
1042 g_return_val_if_fail(!strcmp(buf, "(FLAGS"), NULL);
1044 PARSE_ONE_ELEMENT(')');
1045 g_return_val_if_fail(*buf == '(', NULL);
1046 flags = imap_parse_flags(buf + 1);
1048 g_return_val_if_fail(*cur_pos == ' ', NULL);
1049 g_return_val_if_fail
1050 ((cur_pos = strchr_cpy(cur_pos + 1, ' ', buf, sizeof(buf))),
1052 g_return_val_if_fail(!strcmp(buf, "RFC822.SIZE"), NULL);
1054 PARSE_ONE_ELEMENT(' ');
1057 PARSE_ONE_ELEMENT(' ');
1058 g_return_val_if_fail(!strcmp(buf, "ENVELOPE"), NULL);
1060 g_return_val_if_fail(*cur_pos == '(', NULL);
1061 cur_pos = imap_parse_atom(sock, cur_pos + 1, buf, line_str);
1062 Xstrdup_a(date, buf, return NULL);
1063 date_t = procheader_date_parse(NULL, date, 0);
1065 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1066 if (buf[0] != '\0') {
1067 conv_unmime_header(tmp, sizeof(tmp), buf, NULL);
1068 Xstrdup_a(subject, tmp, return NULL);
1071 g_return_val_if_fail(*cur_pos == ' ', NULL);
1072 cur_pos = imap_parse_address(sock, cur_pos + 1,
1073 &tmp_from, &tmp_fromname, line_str);
1074 Xstrdup_a(from, tmp_from,
1075 {g_free(tmp_from); g_free(tmp_fromname); return NULL;});
1076 Xstrdup_a(fromname, tmp_fromname,
1077 {g_free(tmp_from); g_free(tmp_fromname); return NULL;});
1079 g_free(tmp_fromname);
1081 #define SKIP_ONE_ELEMENT() \
1083 g_return_val_if_fail(*cur_pos == ' ', NULL); \
1084 cur_pos = imap_parse_address(sock, cur_pos + 1, \
1085 NULL, NULL, line_str); \
1088 /* skip sender and reply-to */
1092 g_return_val_if_fail(*cur_pos == ' ', NULL);
1093 cur_pos = imap_parse_address(sock, cur_pos + 1, &tmp_to, NULL, line_str);
1094 Xstrdup_a(to, tmp_to, {g_free(tmp_to); return NULL;});
1097 /* skip Cc and Bcc */
1101 #undef SKIP_ONE_ELEMENT
1103 g_return_val_if_fail(*cur_pos == ' ', NULL);
1104 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1105 if (buf[0] != '\0') {
1106 eliminate_parenthesis(buf, '(', ')');
1107 extract_parenthesis(buf, '<', '>');
1109 Xstrdup_a(inreplyto, buf, return NULL);
1112 g_return_val_if_fail(*cur_pos == ' ', NULL);
1113 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1114 if (buf[0] != '\0') {
1115 extract_parenthesis(buf, '<', '>');
1117 Xstrdup_a(msgid, buf, return NULL);
1120 msginfo = g_new0(MsgInfo, 1);
1121 msginfo->msgnum = msgnum;
1122 msginfo->size = size;
1123 msginfo->date = g_strdup(date);
1124 msginfo->date_t = date_t;
1125 msginfo->subject = g_strdup(subject);
1126 msginfo->from = g_strdup(from);
1127 msginfo->fromname = g_strdup(fromname);
1128 msginfo->to = g_strdup(to);
1129 msginfo->inreplyto = g_strdup(inreplyto);
1130 msginfo->msgid = g_strdup(msgid);
1131 msginfo->flags = flags;
1136 gint imap_get_envelope(SockInfo *sock, gint first, gint last)
1138 imap_gen_send(sock, "FETCH %d:%d (FLAGS RFC822.SIZE ENVELOPE)",
1141 return IMAP_SUCCESS;
1144 static gint imap_store(SockInfo *sock, gint first, gint last, gchar *sub_cmd)
1149 argbuf = g_ptr_array_new();
1151 imap_gen_send(sock, "STORE %d:%d %s", first, last, sub_cmd);
1153 if ((ok = imap_ok(sock, argbuf)) != IMAP_SUCCESS) {
1154 g_ptr_array_free(argbuf, TRUE);
1155 log_warning(_("error while imap command: STORE %d:%d %s\n"),
1156 first, last, sub_cmd);
1160 g_ptr_array_free(argbuf, TRUE);
1162 return IMAP_SUCCESS;
1165 static gint imap_set_article_flags(IMAPSession *session,
1174 buf = g_string_new(is_set ? "+FLAGS (" : "-FLAGS (");
1176 if (IMAP_IS_SEEN(flags)) g_string_append(buf, "\\Seen ");
1177 if (IMAP_IS_ANSWERED(flags)) g_string_append(buf, "\\Answered ");
1178 if (IMAP_IS_FLAGGED(flags)) g_string_append(buf, "\\Flagged ");
1179 if (IMAP_IS_DELETED(flags)) g_string_append(buf, "\\Deleted ");
1180 if (IMAP_IS_DRAFT(flags)) g_string_append(buf, "\\Draft");
1182 if (buf->str[buf->len - 1] == ' ')
1183 g_string_truncate(buf, buf->len - 1);
1185 g_string_append_c(buf, ')');
1187 ok = imap_store(SESSION(session)->sock, first, last, buf->str);
1188 g_string_free(buf, TRUE);
1193 static gint imap_expunge(IMAPSession *session)
1198 argbuf = g_ptr_array_new();
1200 imap_gen_send(SESSION(session)->sock, "EXPUNGE");
1202 if ((ok = imap_ok(SESSION(session)->sock, argbuf)) != IMAP_SUCCESS) {
1203 log_warning(_("error while imap command: EXPUNGE\n"));
1204 g_ptr_array_free(argbuf, TRUE);
1208 g_ptr_array_free(argbuf, TRUE);
1210 return IMAP_SUCCESS;
1213 static gint imap_ok(SockInfo *sock, GPtrArray *argbuf)
1216 gchar buf[IMAPBUFSIZE];
1218 gchar cmd_status[IMAPBUFSIZE];
1220 while ((ok = imap_gen_recv(sock, buf, sizeof(buf))) == IMAP_SUCCESS) {
1221 if (buf[0] == '*' && buf[1] == ' ') {
1223 g_ptr_array_add(argbuf, g_strdup(&buf[2]));
1225 if (sscanf(buf, "%d %s", &cmd_num, cmd_status) < 2)
1227 else if (cmd_num == imap_cmd_count &&
1228 !strcmp(cmd_status, "OK")) {
1230 g_ptr_array_add(argbuf, g_strdup(buf));
1231 return IMAP_SUCCESS;
1240 static gchar *search_array_contain_str(GPtrArray *array, gchar *str)
1244 for (i = 0; i < array->len; i++) {
1247 tmp = g_ptr_array_index(array, i);
1248 if (strstr(tmp, str) != NULL)
1255 static void imap_gen_send(SockInfo *sock, const gchar *format, ...)
1257 gchar buf[IMAPBUFSIZE];
1258 gchar tmp[IMAPBUFSIZE];
1262 va_start(args, format);
1263 g_vsnprintf(tmp, sizeof(tmp), format, args);
1268 g_snprintf(buf, sizeof(buf), "%d %s\r\n", imap_cmd_count, tmp);
1269 if (!strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
1271 log_print("IMAP4> %d %s ********\n", imap_cmd_count, tmp);
1273 log_print("IMAP4> %d %s\n", imap_cmd_count, tmp);
1275 sock_write(sock, buf, strlen(buf));
1278 static gint imap_gen_recv(SockInfo *sock, gchar *buf, gint size)
1280 if (sock_gets(sock, buf, size) == -1)
1285 log_print("IMAP4< %s\n", buf);
1287 return IMAP_SUCCESS;
1290 static void imap_path_subst_slash_to_dot(gchar *str)
1292 subst_char(str, '/', '.');