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_get_envelope (SockInfo *sock,
74 static gint imap_search (SockInfo *sock,
77 static gint imap_get_message (SockInfo *sock,
79 const gchar *filename);
80 static gint imap_copy_message (SockInfo *sock,
82 const gchar *destfolder);
83 static gint imap_store (SockInfo *sock,
88 static gint imap_set_article_flags (IMAPSession *session,
93 static gint imap_expunge (IMAPSession *session);
95 static gchar *imap_parse_atom (SockInfo *sock,
99 static gchar *imap_parse_one_address (SockInfo *sock,
102 gchar *out_fromname_str,
104 static gchar *imap_parse_address (SockInfo *sock,
106 gchar **out_from_str,
107 gchar **out_fromname_str,
109 static MsgFlags imap_parse_flags (const gchar *flag_str);
110 static MsgInfo *imap_parse_envelope (SockInfo *sock,
113 static gint imap_ok (SockInfo *sock,
115 static void imap_gen_send (SockInfo *sock,
116 const gchar *format, ...);
117 static gint imap_gen_recv (SockInfo *sock,
121 static gchar *search_array_contain_str (GPtrArray *array,
123 static void imap_path_subst_slash_to_dot(gchar *str);
126 static IMAPSession *imap_session_connect_if_not(Folder *folder)
128 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
130 g_return_val_if_fail(folder != NULL, NULL);
131 g_return_val_if_fail(folder->type == F_IMAP, NULL);
132 g_return_val_if_fail(folder->account != NULL, NULL);
134 if (!rfolder->session) {
136 imap_session_new(folder->account->recv_server, 143,
137 folder->account->userid,
138 folder->account->passwd);
140 if (imap_noop(rfolder->session->sock) != IMAP_SUCCESS) {
141 log_warning(_("IMAP4 connection to %s:%d has been"
142 " disconnected. Reconnecting...\n"),
143 folder->account->recv_server, 143);
144 session_destroy(rfolder->session);
146 imap_session_new(folder->account->recv_server,
147 143, folder->account->userid,
148 folder->account->passwd);
152 return IMAP_SESSION(rfolder->session);
155 Session *imap_session_new(const gchar *server, gushort port,
156 const gchar *user, const gchar *pass)
158 gchar buf[IMAPBUFSIZE];
159 IMAPSession *session;
162 g_return_val_if_fail(server != NULL, NULL);
164 log_message(_("creating IMAP4 connection to %s:%d ...\n"),
167 if ((imap_sock = imap_open(server, port, buf)) == NULL)
169 if (imap_auth(imap_sock, user, pass) != IMAP_SUCCESS) {
170 imap_logout(imap_sock);
171 sock_close(imap_sock);
175 session = g_new(IMAPSession, 1);
176 SESSION(session)->type = SESSION_IMAP;
177 SESSION(session)->server = g_strdup(server);
178 SESSION(session)->sock = imap_sock;
179 SESSION(session)->connected = TRUE;
180 SESSION(session)->phase = SESSION_READY;
181 SESSION(session)->data = NULL;
182 session->mbox = NULL;
184 session_list = g_list_append(session_list, session);
186 return SESSION(session);
189 void imap_session_destroy(IMAPSession *session)
191 sock_close(SESSION(session)->sock);
192 SESSION(session)->sock = NULL;
194 g_free(session->mbox);
196 session_list = g_list_remove(session_list, session);
199 void imap_session_destroy_all(void)
201 while (session_list != NULL) {
202 IMAPSession *session = (IMAPSession *)session_list->data;
204 imap_logout(SESSION(session)->sock);
205 imap_session_destroy(session);
209 GSList *imap_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
211 GSList *mlist = NULL;
212 IMAPSession *session;
214 g_return_val_if_fail(folder != NULL, NULL);
215 g_return_val_if_fail(item != NULL, NULL);
216 g_return_val_if_fail(folder->type == F_IMAP, NULL);
217 g_return_val_if_fail(folder->account != NULL, NULL);
219 session = imap_session_connect_if_not(folder);
222 mlist = procmsg_read_cache(item, FALSE);
223 item->last_num = procmsg_get_last_num_in_cache(mlist);
224 procmsg_set_flags(mlist, item);
226 gint ok, exists = 0, recent = 0, unseen = 0, begin = 1;
227 gulong uid = 0, last_uid = item->mtime;
229 ok = imap_select(SESSION(session)->sock, item->path,
230 &exists, &recent, &unseen, &uid);
231 if (ok != IMAP_SUCCESS) {
232 log_warning(_("can't select folder: %s\n"), item->path);
239 mlist = procmsg_read_cache(item, FALSE);
240 procmsg_set_flags(mlist, item);
241 cache_last = procmsg_get_last_num_in_cache(mlist);
243 /* calculating the range of envelope to get */
244 if (exists < cache_last) {
245 /* some messages are deleted (get all) */
247 } else if (exists == cache_last) {
248 if (last_uid != 0 && last_uid != uid) {
249 /* some recent but deleted (get all) */
252 /* mailbox unchanged (get none)*/
256 if (exists == cache_last + recent) {
258 begin = cache_last + 1;
260 /* some recent but deleted (get all) */
268 if (1 < begin && begin <= exists) {
271 newlist = imap_get_uncached_messages
272 (session, item, begin, exists);
273 imap_delete_messages(mlist, begin, INT_MAX);
274 mlist = g_slist_concat(mlist, newlist);
275 } else if (begin == 1) {
276 mlist = imap_get_uncached_messages
277 (session, item, 1, exists);
278 imap_delete_all_messages(item);
281 item->last_num = exists;
289 gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint num)
291 gchar *path, *filename;
292 IMAPSession *session;
295 g_return_val_if_fail(folder != NULL, NULL);
296 g_return_val_if_fail(item != NULL, NULL);
298 session = imap_session_connect_if_not(folder);
300 path = folder_item_get_path(item);
301 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
304 if (is_file_exist(filename)) {
305 debug_print(_("message %d has been already cached.\n"), num);
314 debug_print(_("getting message %d...\n"), num);
315 ok = imap_get_message(SESSION(session)->sock, num, filename);
319 if (ok != IMAP_SUCCESS) {
320 g_warning(_("can't fetch message %d\n"), num);
328 gint imap_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
331 IMAPSession *session;
333 g_return_val_if_fail(folder != NULL, -1);
334 g_return_val_if_fail(dest != NULL, -1);
335 g_return_val_if_fail(msginfo != NULL, -1);
337 session = imap_session_connect_if_not(folder);
338 if (!session) return -1;
340 if (msginfo->folder == dest) {
341 g_warning(_("the src folder is identical to the dest.\n"));
345 Xstrdup_a(destdir, dest->path, return -1);
346 /* imap_path_subst_slash_to_dot(destdir); */
348 imap_copy_message(SESSION(session)->sock, msginfo->msgnum, destdir);
349 imap_set_article_flags(session, msginfo->msgnum, msginfo->msgnum,
350 IMAP_FLAG_DELETED, TRUE);
351 debug_print(_("Moving message %s%c%d to %s ...\n"),
352 msginfo->folder->path, G_DIR_SEPARATOR,
353 msginfo->msgnum, dest->path);
355 imap_expunge(session);
360 gint imap_move_msgs_with_dest(Folder *folder, FolderItem *dest,
366 IMAPSession *session;
368 g_return_val_if_fail(folder != NULL, -1);
369 g_return_val_if_fail(dest != NULL, -1);
370 g_return_val_if_fail(msglist != NULL, -1);
372 session = imap_session_connect_if_not(folder);
373 if (!session) return -1;
375 Xstrdup_a(destdir, dest->path, return -1);
376 /* imap_path_subst_slash_to_dot(destdir); */
378 for (cur = msglist; cur != NULL; cur = cur->next) {
379 msginfo = (MsgInfo *)cur->data;
381 if (msginfo->folder == dest) {
382 g_warning(_("the src folder is identical to the dest.\n"));
385 imap_copy_message(SESSION(session)->sock,
386 msginfo->msgnum, destdir);
387 imap_set_article_flags
388 (session, msginfo->msgnum, msginfo->msgnum,
389 IMAP_FLAG_DELETED, TRUE);
391 debug_print(_("Moving message %s%c%d to %s ...\n"),
392 msginfo->folder->path, G_DIR_SEPARATOR,
393 msginfo->msgnum, dest->path);
397 imap_expunge(session);
403 gint imap_remove_msg(Folder *folder, FolderItem *item, gint num)
405 gint exists, recent, unseen;
408 IMAPSession *session;
410 g_return_val_if_fail(folder != NULL, -1);
411 g_return_val_if_fail(item != NULL, -1);
412 g_return_val_if_fail(num > 0 && num <= item->last_num, -1);
414 session = imap_session_connect_if_not(folder);
415 if (!session) return -1;
417 ok = imap_select(SESSION(session)->sock, item->path,
418 &exists, &recent, &unseen, &uid);
419 if (ok != IMAP_SUCCESS) {
420 log_warning(_("can't select folder: %s\n"), item->path);
424 ok = imap_set_article_flags(IMAP_SESSION(REMOTE_FOLDER(folder)->session),
425 num, num, IMAP_FLAG_DELETED, TRUE);
426 if (ok != IMAP_SUCCESS) {
427 log_warning(_("can't set deleted flags: %d\n"), num);
431 ok = imap_expunge(session);
432 if (ok != IMAP_SUCCESS) {
433 log_warning(_("can't expunge\n"));
440 gint imap_remove_all_msg(Folder *folder, FolderItem *item)
442 gint exists, recent, unseen;
445 IMAPSession *session;
447 g_return_val_if_fail(folder != NULL, -1);
448 g_return_val_if_fail(item != NULL, -1);
450 session = imap_session_connect_if_not(folder);
451 if (!session) return -1;
453 ok = imap_select(SESSION(session)->sock, item->path,
454 &exists, &recent, &unseen, &uid);
455 if (ok != IMAP_SUCCESS) {
456 log_warning(_("can't select folder: %s\n"), item->path);
460 ok = imap_set_article_flags(session, 1, exists,
461 IMAP_FLAG_DELETED, TRUE);
462 if (ok != IMAP_SUCCESS) {
463 log_warning(_("can't set deleted flags: 1:%d\n"), exists);
467 ok = imap_expunge(session);
468 if (ok != IMAP_SUCCESS) {
469 log_warning(_("can't expunge\n"));
476 void imap_scan_folder(Folder *folder, FolderItem *item)
480 FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
484 FolderItem *new_item;
486 g_return_val_if_fail(folder != NULL, NULL);
487 g_return_val_if_fail(parent != NULL, NULL);
488 g_return_val_if_fail(name != NULL, NULL);
490 imap_session_connect_if_not(folder);
493 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
496 path = g_strdup(name);
498 new_item = folder_item_new(name, path);
499 folder_item_append(parent, new_item);
505 static GSList *imap_get_uncached_messages(IMAPSession *session,
507 gint first, gint last)
509 gchar buf[IMAPBUFSIZE];
510 GSList *newlist = NULL;
511 GSList *llast = NULL;
514 g_return_val_if_fail(session != NULL, NULL);
515 g_return_val_if_fail(item != NULL, NULL);
516 g_return_val_if_fail(item->folder != NULL, NULL);
517 g_return_val_if_fail(item->folder->type == F_IMAP, NULL);
518 g_return_val_if_fail(first <= last, NULL);
520 if (imap_get_envelope(SESSION(session)->sock, first, last)
522 log_warning(_("can't get envelope\n"));
527 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
528 log_warning(_("error occurred while getting envelope.\n"));
532 if (buf[0] != '*' || buf[1] != ' ') break;
534 msginfo = imap_parse_envelope(SESSION(session)->sock, buf);
536 log_warning(_("can't parse envelope: %s\n"), buf);
540 msginfo->folder = item;
543 llast = newlist = g_slist_append(newlist, msginfo);
545 llast = g_slist_append(llast, msginfo);
553 static GSList *imap_delete_messages(GSList *mlist, gint first, gint last)
559 for (cur = mlist; cur != NULL; ) {
562 msginfo = (MsgInfo *)cur->data;
564 (first <= msginfo->msgnum ) &&
565 (msginfo->msgnum <= last)) {
566 debug_print(_("deleting message %d...\n"),
569 cache_file = procmsg_get_message_file_path(msginfo);
570 if (is_file_exist(cache_file)) unlink(cache_file);
573 procmsg_msginfo_free(msginfo);
574 mlist = g_slist_remove(mlist, msginfo);
583 static void imap_delete_all_messages(FolderItem *item)
590 g_return_if_fail(item != NULL);
591 g_return_if_fail(item->folder != NULL);
592 g_return_if_fail(item->folder->type == F_IMAP);
594 dir = folder_item_get_path(item);
595 if ((dp = opendir(dir)) == NULL) {
596 FILE_OP_ERROR(dir, "opendir");
601 debug_print(_("\tDeleting all cached messages... "));
603 while ((d = readdir(dp)) != NULL) {
604 if (to_number(d->d_name) < 0) continue;
606 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
608 if (is_file_exist(file)) {
609 if (unlink(file) < 0)
610 FILE_OP_ERROR(file, "unlink");
619 debug_print(_("done.\n"));
623 static SockInfo *imap_open(const gchar *server, gushort port, gchar *buf)
627 if ((sock = sock_connect(server, port)) == NULL) {
628 log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
635 if (imap_noop(sock) != IMAP_SUCCESS) {
643 static gint imap_auth(SockInfo *sock, const gchar *user, const gchar *pass)
648 imap_gen_send(sock, "LOGIN \"%s\" %s", user, pass);
649 argbuf = g_ptr_array_new();
650 ok = imap_ok(sock, argbuf);
651 if (ok != IMAP_SUCCESS) log_warning(_("IMAP4 login failed.\n"));
652 ptr_array_free_strings(argbuf);
653 g_ptr_array_free(argbuf, TRUE);
658 static gint imap_logout(SockInfo *sock)
660 imap_gen_send(sock, "LOGOUT");
661 return imap_ok(sock, NULL);
664 static gint imap_noop(SockInfo *sock)
666 imap_gen_send(sock, "NOOP");
667 return imap_ok(sock, NULL);
670 static gint imap_select(SockInfo *sock, const gchar *folder,
671 gint *exists, gint *recent, gint *unseen, gulong *uid)
678 *exists = *recent = *unseen = *uid = 0;
679 argbuf = g_ptr_array_new();
681 Xstrdup_a(imappath, folder, return -1);
682 /* imap_path_subst_slash_to_dot(imappath); */
684 imap_gen_send(sock, "SELECT \"%s\"", imappath);
685 if ((ok = imap_ok(sock, argbuf)) != IMAP_SUCCESS)
688 resp_str = search_array_contain_str(argbuf, "EXISTS");
690 if (sscanf(resp_str,"%d EXISTS", exists) != 1) {
691 g_warning("imap_select(): invalid EXISTS line.\n");
696 resp_str = search_array_contain_str(argbuf, "RECENT");
698 if (sscanf(resp_str, "%d RECENT", recent) != 1) {
699 g_warning("imap_select(): invalid RECENT line.\n");
704 resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
706 if (sscanf(resp_str, "OK [UIDVALIDITY %lu] ", uid) != 1) {
707 g_warning("imap_select(): invalid UIDVALIDITY line.\n");
712 resp_str = search_array_contain_str(argbuf, "UNSEEN");
714 if (sscanf(resp_str, "OK [UNSEEN %d] ", unseen) != 1) {
715 g_warning("imap_select(): invalid UNSEEN line.\n");
721 ptr_array_free_strings(argbuf);
722 g_ptr_array_free(argbuf, TRUE);
727 static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)
732 tmp = strchr(src, ch);
733 if (!tmp || tmp == src)
736 memcpy(dest, src, MIN(tmp - src, len - 1));
737 dest[MIN(tmp - src, len - 1)] = '\0';
742 static gint imap_get_message(SockInfo *sock, gint num, const gchar *filename)
745 gchar buf[IMAPBUFSIZE];
750 g_return_val_if_fail(filename != NULL, IMAP_ERROR);
752 imap_gen_send(sock, "FETCH %d BODY[]", num);
754 if (sock_gets(sock, buf, sizeof(buf)) < 0)
757 if (buf[0] != '*' || buf[1] != ' ')
759 log_print("IMAP4< %s\n", buf);
761 cur_pos = strchr(buf, '{');
762 g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
763 cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
764 g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
765 size_num = atol(size_str);
767 if (*cur_pos != '\0') return IMAP_ERROR;
769 if (recv_bytes_write_to_file(sock, size_num, filename) != 0)
772 if (imap_gen_recv(sock, buf, sizeof(buf)) != IMAP_SUCCESS)
778 ok = imap_ok(sock, NULL);
783 static gint imap_copy_message(SockInfo *sock, gint num, const gchar *destfolder)
787 g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
789 imap_gen_send(sock, "COPY %d %s", num, destfolder);
790 ok = imap_ok(sock, NULL);
791 if (ok != IMAP_SUCCESS) {
792 log_warning(_("can't copy %d to %s\n"), num, destfolder);
799 static gchar *imap_parse_atom(SockInfo *sock, gchar *src, gchar *dest,
802 gchar *cur_pos = src;
804 while (*cur_pos == ' ') cur_pos++;
806 if (!strncmp(cur_pos, "NIL", 3)) {
809 } else if (*cur_pos == '\"') {
812 p = strchr_cpy(cur_pos + 1, '\"', dest, IMAPBUFSIZE);
813 cur_pos = p ? p : cur_pos + 2;
814 } else if (*cur_pos == '{') {
818 cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
821 g_return_val_if_fail(orig_buf != NULL, cur_pos);
823 if (sock_gets(sock, orig_buf, IMAPBUFSIZE) < 0)
825 strretchomp(orig_buf);
826 log_print("IMAP4< %s\n", orig_buf);
827 memcpy(dest, orig_buf, len);
829 cur_pos = orig_buf + len;
835 static gchar *imap_parse_one_address(SockInfo *sock, gchar *start,
837 gchar *out_fromname_str,
840 gchar buf[IMAPBUFSIZE];
841 gchar *cur_pos = start;
843 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
844 conv_unmime_header(out_fromname_str, 256, buf, NULL);
846 if (out_fromname_str[0] != '\0') {
847 strcat(out_from_str, "\"");
848 strcat(out_from_str, out_fromname_str);
849 strcat(out_from_str, "\"");
852 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
854 strcat(out_from_str, " <");
856 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
857 strcat(out_from_str, buf);
859 cur_pos = imap_parse_atom(sock, cur_pos, buf, orig_buf);
860 strcat(out_from_str, "@");
861 strcat(out_from_str, buf);
862 strcat(out_from_str, ">");
864 while (*cur_pos == ' ') cur_pos++;
866 g_return_val_if_fail(*cur_pos == ')', cur_pos + 1);
871 static gchar *imap_parse_address(SockInfo *sock, gchar *start,
872 gchar **out_from_str,
873 gchar **out_fromname_str,
876 gchar buf[IMAPBUFSIZE];
877 gchar name_buf[IMAPBUFSIZE];
878 gchar *cur_pos = start;
879 gboolean first = TRUE;
881 if (out_from_str) *out_from_str = NULL;
882 if (out_fromname_str) *out_fromname_str = NULL;
883 buf[0] = name_buf[0] = '\0';
885 if (!strncmp(cur_pos, "NIL", 3)) {
886 if (out_from_str) *out_from_str = g_strdup("");
887 if (out_fromname_str) *out_fromname_str = g_strdup("");
891 g_return_val_if_fail(*cur_pos == '(', NULL);
895 gchar ch = *cur_pos++;
896 if (ch == ')') break;
898 if (!first) strcat(buf, ", ");
900 cur_pos = imap_parse_one_address
901 (sock, cur_pos, buf, name_buf, orig_buf);
902 if (!cur_pos) return NULL;
906 if (out_from_str) *out_from_str = g_strdup(buf);
907 if (out_fromname_str) *out_fromname_str = g_strdup(name_buf);
912 static MsgFlags imap_parse_flags(const gchar *flag_str)
915 const gchar *cur_pos = flag_str;
916 const gchar *last_pos;
920 MSG_SET_FLAGS(flags, MSG_UNREAD|MSG_IMAP);
922 while (cur_pos != NULL) {
923 cur_pos = strchr(cur_pos, '\\');
924 if (cur_pos == NULL) break;
926 last_pos = cur_pos + 1;
927 cur_pos = strchr_cpy(last_pos, ' ', buf, sizeof(buf));
929 strncpy2(buf, last_pos, sizeof(buf));
931 if (g_strcasecmp(buf, "Recent") == 0) {
932 MSG_SET_FLAGS(flags, MSG_NEW|MSG_UNREAD);
933 } else if (g_strcasecmp(buf, "Seen") == 0) {
934 MSG_UNSET_FLAGS(flags, MSG_NEW|MSG_UNREAD);
935 } else if (g_strcasecmp(buf, "Deleted") == 0) {
936 MSG_SET_FLAGS(flags, MSG_DELETED);
937 } else if (g_strcasecmp(buf, "Flagged") == 0) {
938 MSG_SET_FLAGS(flags, MSG_MARKED);
945 static MsgInfo *imap_parse_envelope(SockInfo *sock, gchar *line_str)
948 gchar buf[IMAPBUFSIZE];
949 gchar tmp[IMAPBUFSIZE];
955 gchar *subject = NULL;
959 gchar *fromname = NULL;
962 gchar *inreplyto = NULL;
966 log_print("IMAP4< %s\n", line_str);
968 g_return_val_if_fail(line_str != NULL, NULL);
969 g_return_val_if_fail(line_str[0] == '*' && line_str[1] == ' ', NULL);
971 cur_pos = line_str + 2;
973 #define PARSE_ONE_ELEMENT(ch) \
975 cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf)); \
976 g_return_val_if_fail(cur_pos != NULL, NULL); \
979 PARSE_ONE_ELEMENT(' ');
982 PARSE_ONE_ELEMENT(' ');
983 g_return_val_if_fail(!strcmp(buf, "FETCH"), NULL);
985 PARSE_ONE_ELEMENT(' ');
986 g_return_val_if_fail(!strcmp(buf, "(FLAGS"), NULL);
988 PARSE_ONE_ELEMENT(')');
989 g_return_val_if_fail(*buf == '(', NULL);
990 flags = imap_parse_flags(buf + 1);
992 g_return_val_if_fail(*cur_pos == ' ', NULL);
994 ((cur_pos = strchr_cpy(cur_pos + 1, ' ', buf, sizeof(buf))),
996 g_return_val_if_fail(!strcmp(buf, "RFC822.SIZE"), NULL);
998 PARSE_ONE_ELEMENT(' ');
1001 PARSE_ONE_ELEMENT(' ');
1002 g_return_val_if_fail(!strcmp(buf, "ENVELOPE"), NULL);
1004 g_return_val_if_fail(*cur_pos == '(', NULL);
1005 cur_pos = imap_parse_atom(sock, cur_pos + 1, buf, line_str);
1006 Xstrdup_a(date, buf, return NULL);
1007 date_t = procheader_date_parse(NULL, date, 0);
1009 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1010 if (buf[0] != '\0') {
1011 conv_unmime_header(tmp, sizeof(tmp), buf, NULL);
1012 Xstrdup_a(subject, tmp, return NULL);
1015 g_return_val_if_fail(*cur_pos == ' ', NULL);
1016 cur_pos = imap_parse_address(sock, cur_pos + 1,
1017 &tmp_from, &tmp_fromname, line_str);
1018 Xstrdup_a(from, tmp_from,
1019 {g_free(tmp_from); g_free(tmp_fromname); return NULL;});
1020 Xstrdup_a(fromname, tmp_fromname,
1021 {g_free(tmp_from); g_free(tmp_fromname); return NULL;});
1023 g_free(tmp_fromname);
1025 #define SKIP_ONE_ELEMENT() \
1027 g_return_val_if_fail(*cur_pos == ' ', NULL); \
1028 cur_pos = imap_parse_address(sock, cur_pos + 1, \
1029 NULL, NULL, line_str); \
1032 /* skip sender and reply-to */
1036 g_return_val_if_fail(*cur_pos == ' ', NULL);
1037 cur_pos = imap_parse_address(sock, cur_pos + 1, &tmp_to, NULL, line_str);
1038 Xstrdup_a(to, tmp_to, {g_free(tmp_to); return NULL;});
1041 /* skip Cc and Bcc */
1045 #undef SKIP_ONE_ELEMENT
1047 g_return_val_if_fail(*cur_pos == ' ', NULL);
1048 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1049 if (buf[0] != '\0') {
1050 eliminate_parenthesis(buf, '(', ')');
1051 extract_parenthesis(buf, '<', '>');
1053 Xstrdup_a(inreplyto, buf, return NULL);
1056 g_return_val_if_fail(*cur_pos == ' ', NULL);
1057 cur_pos = imap_parse_atom(sock, cur_pos, buf, line_str);
1058 if (buf[0] != '\0') {
1059 extract_parenthesis(buf, '<', '>');
1061 Xstrdup_a(msgid, buf, return NULL);
1064 msginfo = g_new0(MsgInfo, 1);
1065 msginfo->msgnum = msgnum;
1066 msginfo->size = size;
1067 msginfo->date = g_strdup(date);
1068 msginfo->date_t = date_t;
1069 msginfo->subject = g_strdup(subject);
1070 msginfo->from = g_strdup(from);
1071 msginfo->fromname = g_strdup(fromname);
1072 msginfo->to = g_strdup(to);
1073 msginfo->inreplyto = g_strdup(inreplyto);
1074 msginfo->msgid = g_strdup(msgid);
1075 msginfo->flags = flags;
1080 gint imap_get_envelope(SockInfo *sock, gint first, gint last)
1082 imap_gen_send(sock, "FETCH %d:%d (FLAGS RFC822.SIZE ENVELOPE)",
1085 return IMAP_SUCCESS;
1088 static gint imap_store(SockInfo *sock, gint first, gint last, gchar *sub_cmd)
1093 argbuf = g_ptr_array_new();
1095 imap_gen_send(sock, "STORE %d:%d %s", first, last, sub_cmd);
1097 if ((ok = imap_ok(sock, argbuf)) != IMAP_SUCCESS) {
1098 g_ptr_array_free(argbuf, TRUE);
1099 log_warning(_("error while imap command: STORE %d:%d %s\n"),
1100 first, last, sub_cmd);
1104 g_ptr_array_free(argbuf, TRUE);
1106 return IMAP_SUCCESS;
1109 static gint imap_set_article_flags(IMAPSession *session,
1118 buf = g_string_new(is_set ? "+FLAGS (" : "-FLAGS (");
1120 if (IMAP_IS_SEEN(flags)) g_string_append(buf, "\\Seen ");
1121 if (IMAP_IS_ANSWERED(flags)) g_string_append(buf, "\\Answered ");
1122 if (IMAP_IS_FLAGGED(flags)) g_string_append(buf, "\\Flagged ");
1123 if (IMAP_IS_DELETED(flags)) g_string_append(buf, "\\Deleted ");
1124 if (IMAP_IS_DRAFT(flags)) g_string_append(buf, "\\Draft");
1126 if (buf->str[buf->len - 1] == ' ')
1127 g_string_truncate(buf, buf->len - 1);
1129 g_string_append_c(buf, ')');
1131 ok = imap_store(SESSION(session)->sock, first, last, buf->str);
1132 g_string_free(buf, TRUE);
1137 static gint imap_expunge(IMAPSession *session)
1142 argbuf = g_ptr_array_new();
1144 imap_gen_send(SESSION(session)->sock, "EXPUNGE");
1146 if ((ok = imap_ok(SESSION(session)->sock, argbuf)) != IMAP_SUCCESS) {
1147 log_warning(_("error while imap command: EXPUNGE\n"));
1148 g_ptr_array_free(argbuf, TRUE);
1152 g_ptr_array_free(argbuf, TRUE);
1154 return IMAP_SUCCESS;
1157 static gint imap_ok(SockInfo *sock, GPtrArray *argbuf)
1160 gchar buf[IMAPBUFSIZE];
1162 gchar cmd_status[IMAPBUFSIZE];
1164 while ((ok = imap_gen_recv(sock, buf, sizeof(buf))) == IMAP_SUCCESS) {
1165 if (buf[0] == '*' && buf[1] == ' ') {
1167 g_ptr_array_add(argbuf, g_strdup(&buf[2]));
1169 if (sscanf(buf, "%d %s", &cmd_num, cmd_status) < 2)
1171 else if (cmd_num == imap_cmd_count &&
1172 !strcmp(cmd_status, "OK")) {
1174 g_ptr_array_add(argbuf, g_strdup(buf));
1175 return IMAP_SUCCESS;
1184 static gchar *search_array_contain_str(GPtrArray *array, gchar *str)
1188 for (i = 0; i < array->len; i++) {
1191 tmp = g_ptr_array_index(array, i);
1192 if (strstr(tmp, str) != NULL)
1199 static void imap_gen_send(SockInfo *sock, const gchar *format, ...)
1201 gchar buf[IMAPBUFSIZE];
1202 gchar tmp[IMAPBUFSIZE];
1206 va_start(args, format);
1207 g_vsnprintf(tmp, sizeof(tmp), format, args);
1212 g_snprintf(buf, sizeof(buf), "%d %s\r\n", imap_cmd_count, tmp);
1213 if (!strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
1215 log_print("IMAP4> %d %s ********\n", imap_cmd_count, tmp);
1217 log_print("IMAP4> %d %s\n", imap_cmd_count, tmp);
1219 sock_write(sock, buf, strlen(buf));
1222 static gint imap_gen_recv(SockInfo *sock, gchar *buf, gint size)
1224 if (sock_gets(sock, buf, size) == -1)
1229 log_print("IMAP4< %s\n", buf);
1231 return IMAP_SUCCESS;
1234 static void imap_path_subst_slash_to_dot(gchar *str)
1236 subst_char(str, '/', '.');