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,
487 gchar *dirpath, *imappath;
488 IMAPSession *session;
489 FolderItem *new_item;
491 g_return_val_if_fail(folder != NULL, NULL);
492 g_return_val_if_fail(parent != NULL, NULL);
493 g_return_val_if_fail(name != NULL, NULL);
495 session = imap_session_connect_if_not(folder);
496 if (!session) return NULL;
499 dirpath = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
502 dirpath = g_strdup(name);
504 imappath = g_strdup(dirpath);
505 /* imap_path_subst_slash_to_dot(imappath); */
507 if (strcasecmp(name, "INBOX") != 0) {
510 ok = imap_create(SESSION(session)->sock, imappath);
511 if (ok != IMAP_SUCCESS) {
512 log_warning(_("can't create mailbox\n"));
519 new_item = folder_item_new(name, dirpath);
520 folder_item_append(parent, new_item);
527 gint imap_remove_folder(Folder *folder, FolderItem *item)
530 IMAPSession *session;
532 g_return_val_if_fail(folder != NULL, -1);
533 g_return_val_if_fail(item != NULL, -1);
534 g_return_val_if_fail(item->path != NULL, -1);
536 session = imap_session_connect_if_not(folder);
537 if (!session) return -1;
539 ok = imap_delete(SESSION(session)->sock, item->path);
540 if (ok != IMAP_SUCCESS) {
541 log_warning(_("can't delete mailbox\n"));
545 folder_item_remove(item);
550 static GSList *imap_get_uncached_messages(IMAPSession *session,
552 gint first, gint last)
554 gchar buf[IMAPBUFSIZE];
555 GSList *newlist = NULL;
556 GSList *llast = NULL;
559 g_return_val_if_fail(session != NULL, NULL);
560 g_return_val_if_fail(item != NULL, NULL);
561 g_return_val_if_fail(item->folder != NULL, NULL);
562 g_return_val_if_fail(item->folder->type == F_IMAP, NULL);
563 g_return_val_if_fail(first <= last, NULL);
565 if (imap_get_envelope(SESSION(session)->sock, first, last)
567 log_warning(_("can't get envelope\n"));
572 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
573 log_warning(_("error occurred while getting envelope.\n"));
577 if (buf[0] != '*' || buf[1] != ' ') break;
579 msginfo = imap_parse_envelope(SESSION(session)->sock, buf);
581 log_warning(_("can't parse envelope: %s\n"), buf);
585 msginfo->folder = item;
588 llast = newlist = g_slist_append(newlist, msginfo);
590 llast = g_slist_append(llast, msginfo);
598 static GSList *imap_delete_messages(GSList *mlist, gint first, gint last)
604 for (cur = mlist; cur != NULL; ) {
607 msginfo = (MsgInfo *)cur->data;
609 (first <= msginfo->msgnum ) &&
610 (msginfo->msgnum <= last)) {
611 debug_print(_("deleting message %d...\n"),
614 cache_file = procmsg_get_message_file_path(msginfo);
615 if (is_file_exist(cache_file)) unlink(cache_file);
618 procmsg_msginfo_free(msginfo);
619 mlist = g_slist_remove(mlist, msginfo);
628 static void imap_delete_all_messages(FolderItem *item)
635 g_return_if_fail(item != NULL);
636 g_return_if_fail(item->folder != NULL);
637 g_return_if_fail(item->folder->type == F_IMAP);
639 dir = folder_item_get_path(item);
640 if ((dp = opendir(dir)) == NULL) {
641 FILE_OP_ERROR(dir, "opendir");
646 debug_print(_("\tDeleting all cached messages... "));
648 while ((d = readdir(dp)) != NULL) {
649 if (to_number(d->d_name) < 0) continue;
651 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
653 if (is_file_exist(file)) {
654 if (unlink(file) < 0)
655 FILE_OP_ERROR(file, "unlink");
664 debug_print(_("done.\n"));
668 static SockInfo *imap_open(const gchar *server, gushort port, gchar *buf)
672 if ((sock = sock_connect(server, port)) == NULL) {
673 log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
680 if (imap_noop(sock) != IMAP_SUCCESS) {
688 static gint imap_auth(SockInfo *sock, const gchar *user, const gchar *pass)
693 imap_gen_send(sock, "LOGIN \"%s\" %s", user, pass);
694 argbuf = g_ptr_array_new();
695 ok = imap_ok(sock, argbuf);
696 if (ok != IMAP_SUCCESS) log_warning(_("IMAP4 login failed.\n"));
697 ptr_array_free_strings(argbuf);
698 g_ptr_array_free(argbuf, TRUE);
703 static gint imap_logout(SockInfo *sock)
705 imap_gen_send(sock, "LOGOUT");
706 return imap_ok(sock, NULL);
709 static gint imap_noop(SockInfo *sock)
711 imap_gen_send(sock, "NOOP");
712 return imap_ok(sock, NULL);
715 static gint imap_select(SockInfo *sock, const gchar *folder,
716 gint *exists, gint *recent, gint *unseen, gulong *uid)
723 *exists = *recent = *unseen = *uid = 0;
724 argbuf = g_ptr_array_new();
726 Xstrdup_a(imappath, folder, return -1);
727 /* imap_path_subst_slash_to_dot(imappath); */
729 imap_gen_send(sock, "SELECT \"%s\"", imappath);
730 if ((ok = imap_ok(sock, argbuf)) != IMAP_SUCCESS)
733 resp_str = search_array_contain_str(argbuf, "EXISTS");
735 if (sscanf(resp_str,"%d EXISTS", exists) != 1) {
736 g_warning("imap_select(): invalid EXISTS line.\n");
741 resp_str = search_array_contain_str(argbuf, "RECENT");
743 if (sscanf(resp_str, "%d RECENT", recent) != 1) {
744 g_warning("imap_select(): invalid RECENT line.\n");
749 resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
751 if (sscanf(resp_str, "OK [UIDVALIDITY %lu] ", uid) != 1) {
752 g_warning("imap_select(): invalid UIDVALIDITY line.\n");
757 resp_str = search_array_contain_str(argbuf, "UNSEEN");
759 if (sscanf(resp_str, "OK [UNSEEN %d] ", unseen) != 1) {
760 g_warning("imap_select(): invalid UNSEEN line.\n");
766 ptr_array_free_strings(argbuf);
767 g_ptr_array_free(argbuf, TRUE);
772 static gint imap_create(SockInfo *sock, const gchar *folder)
774 imap_gen_send(sock, "CREATE \"%s\"", folder);
775 return imap_ok(sock, NULL);
778 static gint imap_delete(SockInfo *sock, const gchar *folder)
780 imap_gen_send(sock, "DELETE \"%s\"", folder);
781 return imap_ok(sock, NULL);
784 static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)
789 tmp = strchr(src, ch);
790 if (!tmp || tmp == src)
793 memcpy(dest, src, MIN(tmp - src, len - 1));
794 dest[MIN(tmp - src, len - 1)] = '\0';
799 static gint imap_get_message(SockInfo *sock, gint num, const gchar *filename)
802 gchar buf[IMAPBUFSIZE];
807 g_return_val_if_fail(filename != NULL, IMAP_ERROR);
809 imap_gen_send(sock, "FETCH %d BODY[]", num);
811 if (sock_gets(sock, buf, sizeof(buf)) < 0)
814 if (buf[0] != '*' || buf[1] != ' ')
816 log_print("IMAP4< %s\n", buf);
818 cur_pos = strchr(buf, '{');
819 g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
820 cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
821 g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
822 size_num = atol(size_str);
824 if (*cur_pos != '\0') return IMAP_ERROR;
826 if (recv_bytes_write_to_file(sock, size_num, filename) != 0)
829 if (imap_gen_recv(sock, buf, sizeof(buf)) != IMAP_SUCCESS)
835 ok = imap_ok(sock, NULL);
840 static gint imap_copy_message(SockInfo *sock, gint num, const gchar *destfolder)
844 g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
846 imap_gen_send(sock, "COPY %d %s", num, destfolder);
847 ok = imap_ok(sock, NULL);
848 if (ok != IMAP_SUCCESS) {
849 log_warning(_("can't copy %d to %s\n"), num, destfolder);
856 static gchar *imap_parse_atom(SockInfo *sock, gchar *src, gchar *dest,
859 gchar *cur_pos = src;
861 while (*cur_pos == ' ') cur_pos++;
863 if (!strncmp(cur_pos, "NIL", 3)) {
866 } else if (*cur_pos == '\"') {
869 p = strchr_cpy(cur_pos + 1, '\"', dest, IMAPBUFSIZE);
870 cur_pos = p ? p : cur_pos + 2;
871 } else if (*cur_pos == '{') {
875 cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
878 g_return_val_if_fail(orig_buf != NULL, cur_pos);
880 if (sock_gets(sock, orig_buf, IMAPBUFSIZE) < 0)
882 strretchomp(orig_buf);
883 log_print("IMAP4< %s\n", orig_buf);
884 memcpy(dest, orig_buf, len);
886 cur_pos = orig_buf + len;
892 static gchar *imap_parse_one_address(SockInfo *sock, gchar *start,
894 gchar *out_fromname_str,
897 gchar buf[IMAPBUFSIZE];
898 gchar *cur_pos = start;
900 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
901 conv_unmime_header(out_fromname_str, 256, buf, NULL);
903 if (out_fromname_str[0] != '\0') {
904 strcat(out_from_str, "\"");
905 strcat(out_from_str, out_fromname_str);
906 strcat(out_from_str, "\"");
909 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
911 strcat(out_from_str, " <");
913 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
914 strcat(out_from_str, buf);
916 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
917 strcat(out_from_str, "@");
918 strcat(out_from_str, buf);
919 strcat(out_from_str, ">");
921 while (*cur_pos == ' ') cur_pos++;
923 g_return_val_if_fail(*cur_pos == ')', cur_pos + 1);
928 static gchar *imap_parse_address(SockInfo *sock, gchar *start,
929 gchar **out_from_str,
930 gchar **out_fromname_str,
933 gchar buf[IMAPBUFSIZE];
934 gchar name_buf[IMAPBUFSIZE];
935 gchar *cur_pos = start;
936 gboolean first = TRUE;
938 if (out_from_str) *out_from_str = NULL;
939 if (out_fromname_str) *out_fromname_str = NULL;
940 buf[0] = name_buf[0] = '\0';
942 if (!strncmp(cur_pos, "NIL", 3)) {
943 if (out_from_str) *out_from_str = g_strdup("");
944 if (out_fromname_str) *out_fromname_str = g_strdup("");
948 g_return_val_if_fail(*cur_pos == '(', NULL);
952 gchar ch = *cur_pos++;
953 if (ch == ')') break;
955 if (!first) strcat(buf, ", ");
957 cur_pos = imap_parse_one_address
958 (sock, cur_pos, buf, name_buf, orig_buf);
959 if (!cur_pos) return NULL;
963 if (out_from_str) *out_from_str = g_strdup(buf);
964 if (out_fromname_str) *out_fromname_str = g_strdup(name_buf);
969 static MsgFlags imap_parse_flags(const gchar *flag_str)
972 const gchar *cur_pos = flag_str;
973 const gchar *last_pos;
977 MSG_SET_FLAGS(flags, MSG_UNREAD|MSG_IMAP);
979 while (cur_pos != NULL) {
980 cur_pos = strchr(cur_pos, '\\');
981 if (cur_pos == NULL) break;
983 last_pos = cur_pos + 1;
984 cur_pos = strchr_cpy(last_pos, ' ', buf, sizeof(buf));
986 strncpy2(buf, last_pos, sizeof(buf));
988 if (g_strcasecmp(buf, "Recent") == 0) {
989 MSG_SET_FLAGS(flags, MSG_NEW|MSG_UNREAD);
990 } else if (g_strcasecmp(buf, "Seen") == 0) {
991 MSG_UNSET_FLAGS(flags, MSG_NEW|MSG_UNREAD);
992 } else if (g_strcasecmp(buf, "Deleted") == 0) {
993 MSG_SET_FLAGS(flags, MSG_DELETED);
994 } else if (g_strcasecmp(buf, "Flagged") == 0) {
995 MSG_SET_FLAGS(flags, MSG_MARKED);
1002 static MsgInfo *imap_parse_envelope(SockInfo *sock, gchar *line_str)
1005 gchar buf[IMAPBUFSIZE];
1006 gchar tmp[IMAPBUFSIZE];
1012 gchar *subject = NULL;
1014 gchar *tmp_fromname;
1016 gchar *fromname = NULL;
1019 gchar *inreplyto = NULL;
1020 gchar *msgid = NULL;
1023 log_print("IMAP4< %s\n", line_str);
1025 g_return_val_if_fail(line_str != NULL, NULL);
1026 g_return_val_if_fail(line_str[0] == '*' && line_str[1] == ' ', NULL);
1028 cur_pos = line_str + 2;
1030 #define PARSE_ONE_ELEMENT(ch) \
1032 cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf)); \
1033 g_return_val_if_fail(cur_pos != NULL, NULL); \
1036 PARSE_ONE_ELEMENT(' ');
1039 PARSE_ONE_ELEMENT(' ');
1040 g_return_val_if_fail(!strcmp(buf, "FETCH"), NULL);
1042 PARSE_ONE_ELEMENT(' ');
1043 g_return_val_if_fail(!strcmp(buf, "(FLAGS"), NULL);
1045 PARSE_ONE_ELEMENT(')');
1046 g_return_val_if_fail(*buf == '(', NULL);
1047 flags = imap_parse_flags(buf + 1);
1049 g_return_val_if_fail(*cur_pos == ' ', NULL);
1050 g_return_val_if_fail
1051 ((cur_pos = strchr_cpy(cur_pos + 1, ' ', buf, sizeof(buf))),
1053 g_return_val_if_fail(!strcmp(buf, "RFC822.SIZE"), NULL);
1055 PARSE_ONE_ELEMENT(' ');
1058 PARSE_ONE_ELEMENT(' ');
1059 g_return_val_if_fail(!strcmp(buf, "ENVELOPE"), NULL);
1061 g_return_val_if_fail(*cur_pos == '(', NULL);
1062 cur_pos = imap_parse_atom(sock, cur_pos + 1, buf, line_str);
1063 Xstrdup_a(date, buf, return NULL);
1064 date_t = procheader_date_parse(NULL, date, 0);
1066 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1067 if (buf[0] != '\0') {
1068 conv_unmime_header(tmp, sizeof(tmp), buf, NULL);
1069 Xstrdup_a(subject, tmp, return NULL);
1072 g_return_val_if_fail(*cur_pos == ' ', NULL);
1073 cur_pos = imap_parse_address(sock, cur_pos + 1,
1074 &tmp_from, &tmp_fromname, line_str);
1075 Xstrdup_a(from, tmp_from,
1076 {g_free(tmp_from); g_free(tmp_fromname); return NULL;});
1077 Xstrdup_a(fromname, tmp_fromname,
1078 {g_free(tmp_from); g_free(tmp_fromname); return NULL;});
1080 g_free(tmp_fromname);
1082 #define SKIP_ONE_ELEMENT() \
1084 g_return_val_if_fail(*cur_pos == ' ', NULL); \
1085 cur_pos = imap_parse_address(sock, cur_pos + 1, \
1086 NULL, NULL, line_str); \
1089 /* skip sender and reply-to */
1093 g_return_val_if_fail(*cur_pos == ' ', NULL);
1094 cur_pos = imap_parse_address(sock, cur_pos + 1, &tmp_to, NULL, line_str);
1095 Xstrdup_a(to, tmp_to, {g_free(tmp_to); return NULL;});
1098 /* skip Cc and Bcc */
1102 #undef SKIP_ONE_ELEMENT
1104 g_return_val_if_fail(*cur_pos == ' ', NULL);
1105 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1106 if (buf[0] != '\0') {
1107 eliminate_parenthesis(buf, '(', ')');
1108 extract_parenthesis(buf, '<', '>');
1110 Xstrdup_a(inreplyto, buf, return NULL);
1113 g_return_val_if_fail(*cur_pos == ' ', NULL);
1114 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1115 if (buf[0] != '\0') {
1116 extract_parenthesis(buf, '<', '>');
1118 Xstrdup_a(msgid, buf, return NULL);
1121 msginfo = g_new0(MsgInfo, 1);
1122 msginfo->msgnum = msgnum;
1123 msginfo->size = size;
1124 msginfo->date = g_strdup(date);
1125 msginfo->date_t = date_t;
1126 msginfo->subject = g_strdup(subject);
1127 msginfo->from = g_strdup(from);
1128 msginfo->fromname = g_strdup(fromname);
1129 msginfo->to = g_strdup(to);
1130 msginfo->inreplyto = g_strdup(inreplyto);
1131 msginfo->msgid = g_strdup(msgid);
1132 msginfo->flags = flags;
1137 gint imap_get_envelope(SockInfo *sock, gint first, gint last)
1139 imap_gen_send(sock, "FETCH %d:%d (FLAGS RFC822.SIZE ENVELOPE)",
1142 return IMAP_SUCCESS;
1145 static gint imap_store(SockInfo *sock, gint first, gint last, gchar *sub_cmd)
1150 argbuf = g_ptr_array_new();
1152 imap_gen_send(sock, "STORE %d:%d %s", first, last, sub_cmd);
1154 if ((ok = imap_ok(sock, argbuf)) != IMAP_SUCCESS) {
1155 g_ptr_array_free(argbuf, TRUE);
1156 log_warning(_("error while imap command: STORE %d:%d %s\n"),
1157 first, last, sub_cmd);
1161 g_ptr_array_free(argbuf, TRUE);
1163 return IMAP_SUCCESS;
1166 static gint imap_set_article_flags(IMAPSession *session,
1175 buf = g_string_new(is_set ? "+FLAGS (" : "-FLAGS (");
1177 if (IMAP_IS_SEEN(flags)) g_string_append(buf, "\\Seen ");
1178 if (IMAP_IS_ANSWERED(flags)) g_string_append(buf, "\\Answered ");
1179 if (IMAP_IS_FLAGGED(flags)) g_string_append(buf, "\\Flagged ");
1180 if (IMAP_IS_DELETED(flags)) g_string_append(buf, "\\Deleted ");
1181 if (IMAP_IS_DRAFT(flags)) g_string_append(buf, "\\Draft");
1183 if (buf->str[buf->len - 1] == ' ')
1184 g_string_truncate(buf, buf->len - 1);
1186 g_string_append_c(buf, ')');
1188 ok = imap_store(SESSION(session)->sock, first, last, buf->str);
1189 g_string_free(buf, TRUE);
1194 static gint imap_expunge(IMAPSession *session)
1199 argbuf = g_ptr_array_new();
1201 imap_gen_send(SESSION(session)->sock, "EXPUNGE");
1203 if ((ok = imap_ok(SESSION(session)->sock, argbuf)) != IMAP_SUCCESS) {
1204 log_warning(_("error while imap command: EXPUNGE\n"));
1205 g_ptr_array_free(argbuf, TRUE);
1209 g_ptr_array_free(argbuf, TRUE);
1211 return IMAP_SUCCESS;
1214 static gint imap_ok(SockInfo *sock, GPtrArray *argbuf)
1217 gchar buf[IMAPBUFSIZE];
1219 gchar cmd_status[IMAPBUFSIZE];
1221 while ((ok = imap_gen_recv(sock, buf, sizeof(buf))) == IMAP_SUCCESS) {
1222 if (buf[0] == '*' && buf[1] == ' ') {
1224 g_ptr_array_add(argbuf, g_strdup(&buf[2]));
1226 if (sscanf(buf, "%d %s", &cmd_num, cmd_status) < 2)
1228 else if (cmd_num == imap_cmd_count &&
1229 !strcmp(cmd_status, "OK")) {
1231 g_ptr_array_add(argbuf, g_strdup(buf));
1232 return IMAP_SUCCESS;
1241 static gchar *search_array_contain_str(GPtrArray *array, gchar *str)
1245 for (i = 0; i < array->len; i++) {
1248 tmp = g_ptr_array_index(array, i);
1249 if (strstr(tmp, str) != NULL)
1256 static void imap_gen_send(SockInfo *sock, const gchar *format, ...)
1258 gchar buf[IMAPBUFSIZE];
1259 gchar tmp[IMAPBUFSIZE];
1263 va_start(args, format);
1264 g_vsnprintf(tmp, sizeof(tmp), format, args);
1269 g_snprintf(buf, sizeof(buf), "%d %s\r\n", imap_cmd_count, tmp);
1270 if (!strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
1272 log_print("IMAP4> %d %s ********\n", imap_cmd_count, tmp);
1274 log_print("IMAP4> %d %s\n", imap_cmd_count, tmp);
1276 sock_write(sock, buf, strlen(buf));
1279 static gint imap_gen_recv(SockInfo *sock, gchar *buf, gint size)
1281 if (sock_gets(sock, buf, size) == -1)
1286 log_print("IMAP4< %s\n", buf);
1288 return IMAP_SUCCESS;
1291 static void imap_path_subst_slash_to_dot(gchar *str)
1293 subst_char(str, '/', '.');