root / libsylph / imap.c @ 3192
History | View | Annotate | Download (124 kB)
| 1 | /*
|
|---|---|
| 2 | * LibSylph -- E-Mail client library |
| 3 | * Copyright (C) 1999-2012 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library 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 GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <dirent.h> |
| 32 | #include <unistd.h> |
| 33 | #include <ctype.h> |
| 34 | #include <time.h> |
| 35 | #if HAVE_ICONV
|
| 36 | # include <iconv.h> |
| 37 | #endif
|
| 38 | |
| 39 | #include "sylmain.h" |
| 40 | #include "imap.h" |
| 41 | #include "socket.h" |
| 42 | #include "socks.h" |
| 43 | #include "ssl.h" |
| 44 | #include "recv.h" |
| 45 | #include "procmsg.h" |
| 46 | #include "procheader.h" |
| 47 | #include "folder.h" |
| 48 | #include "prefs_account.h" |
| 49 | #include "codeconv.h" |
| 50 | #include "md5_hmac.h" |
| 51 | #include "base64.h" |
| 52 | #include "utils.h" |
| 53 | #include "prefs_common.h" |
| 54 | #include "virtual.h" |
| 55 | |
| 56 | #define IMAP4_PORT 143 |
| 57 | #if USE_SSL
|
| 58 | #define IMAPS_PORT 993 |
| 59 | #endif
|
| 60 | |
| 61 | #define IMAP_COPY_LIMIT 200 |
| 62 | #define IMAP_CMD_LIMIT 1000 |
| 63 | |
| 64 | #define QUOTE_IF_REQUIRED(out, str) \
|
| 65 | { \
|
| 66 | if (*str != '"' && strpbrk(str, " \t(){}[]%&*") != NULL) { \ |
| 67 | gchar *__tmp; \ |
| 68 | gint len; \ |
| 69 | \ |
| 70 | len = strlen(str) + 3; \
|
| 71 | Xalloca(__tmp, len, return IMAP_ERROR); \
|
| 72 | g_snprintf(__tmp, len, "\"%s\"", str); \
|
| 73 | out = __tmp; \ |
| 74 | } else { \
|
| 75 | Xstrdup_a(out, str, return IMAP_ERROR); \
|
| 76 | } \ |
| 77 | } |
| 78 | |
| 79 | typedef gint (*IMAPThreadFunc) (IMAPSession *session,
|
| 80 | gpointer data); |
| 81 | typedef gint (*IMAPProgressFunc) (IMAPSession *session,
|
| 82 | gint count, |
| 83 | gint total, |
| 84 | gpointer data); |
| 85 | |
| 86 | typedef struct _IMAPRealSession |
| 87 | {
|
| 88 | IMAPSession imap_session; |
| 89 | #if USE_THREADS
|
| 90 | GThreadPool *pool; |
| 91 | IMAPThreadFunc thread_func; |
| 92 | gpointer thread_data; |
| 93 | gboolean is_running; |
| 94 | gint prog_count; |
| 95 | gint prog_total; |
| 96 | gint flag; |
| 97 | gint retval; |
| 98 | #endif
|
| 99 | } IMAPRealSession; |
| 100 | |
| 101 | static GList *session_list = NULL; |
| 102 | |
| 103 | static void imap_folder_init (Folder *folder, |
| 104 | const gchar *name,
|
| 105 | const gchar *path);
|
| 106 | |
| 107 | static Folder *imap_folder_new (const gchar *name, |
| 108 | const gchar *path);
|
| 109 | static void imap_folder_destroy (Folder *folder); |
| 110 | |
| 111 | static Session *imap_session_new (PrefsAccount *account);
|
| 112 | static gint imap_session_connect (IMAPSession *session);
|
| 113 | static gint imap_session_reconnect (IMAPSession *session);
|
| 114 | static void imap_session_destroy (Session *session); |
| 115 | /* static void imap_session_destroy_all (void); */
|
| 116 | |
| 117 | static gint imap_search_flags (IMAPSession *session,
|
| 118 | GArray **uids, |
| 119 | GHashTable **flags_table); |
| 120 | static gint imap_fetch_flags (IMAPSession *session,
|
| 121 | GArray **uids, |
| 122 | GHashTable **flags_table); |
| 123 | |
| 124 | static GSList *imap_get_msg_list (Folder *folder,
|
| 125 | FolderItem *item, |
| 126 | gboolean use_cache); |
| 127 | static GSList *imap_get_uncached_msg_list
|
| 128 | (Folder *folder, |
| 129 | FolderItem *item); |
| 130 | static gchar *imap_fetch_msg (Folder *folder,
|
| 131 | FolderItem *item, |
| 132 | gint uid); |
| 133 | static MsgInfo *imap_get_msginfo (Folder *folder,
|
| 134 | FolderItem *item, |
| 135 | gint uid); |
| 136 | static gint imap_add_msg (Folder *folder,
|
| 137 | FolderItem *dest, |
| 138 | const gchar *file,
|
| 139 | MsgFlags *flags, |
| 140 | gboolean remove_source); |
| 141 | static gint imap_add_msgs (Folder *folder,
|
| 142 | FolderItem *dest, |
| 143 | GSList *file_list, |
| 144 | gboolean remove_source, |
| 145 | gint *first); |
| 146 | static gint imap_add_msg_msginfo (Folder *folder,
|
| 147 | FolderItem *dest, |
| 148 | MsgInfo *msginfo, |
| 149 | gboolean remove_source); |
| 150 | static gint imap_add_msgs_msginfo (Folder *folder,
|
| 151 | FolderItem *dest, |
| 152 | GSList *msglist, |
| 153 | gboolean remove_source, |
| 154 | gint *first); |
| 155 | |
| 156 | static gint imap_move_msg (Folder *folder,
|
| 157 | FolderItem *dest, |
| 158 | MsgInfo *msginfo); |
| 159 | static gint imap_move_msgs (Folder *folder,
|
| 160 | FolderItem *dest, |
| 161 | GSList *msglist); |
| 162 | static gint imap_copy_msg (Folder *folder,
|
| 163 | FolderItem *dest, |
| 164 | MsgInfo *msginfo); |
| 165 | static gint imap_copy_msgs (Folder *folder,
|
| 166 | FolderItem *dest, |
| 167 | GSList *msglist); |
| 168 | |
| 169 | static gint imap_remove_msg (Folder *folder,
|
| 170 | FolderItem *item, |
| 171 | MsgInfo *msginfo); |
| 172 | static gint imap_remove_msgs (Folder *folder,
|
| 173 | FolderItem *item, |
| 174 | GSList *msglist); |
| 175 | static gint imap_remove_all_msg (Folder *folder,
|
| 176 | FolderItem *item); |
| 177 | |
| 178 | static gboolean imap_is_msg_changed (Folder *folder,
|
| 179 | FolderItem *item, |
| 180 | MsgInfo *msginfo); |
| 181 | |
| 182 | static gint imap_close (Folder *folder,
|
| 183 | FolderItem *item); |
| 184 | |
| 185 | static gint imap_scan_folder (Folder *folder,
|
| 186 | FolderItem *item); |
| 187 | static gint imap_scan_tree (Folder *folder);
|
| 188 | |
| 189 | static gint imap_create_tree (Folder *folder);
|
| 190 | |
| 191 | static FolderItem *imap_create_folder (Folder *folder,
|
| 192 | FolderItem *parent, |
| 193 | const gchar *name);
|
| 194 | static gint imap_rename_folder (Folder *folder,
|
| 195 | FolderItem *item, |
| 196 | const gchar *name);
|
| 197 | static gint imap_move_folder (Folder *folder,
|
| 198 | FolderItem *item, |
| 199 | FolderItem *new_parent); |
| 200 | static gint imap_remove_folder (Folder *folder,
|
| 201 | FolderItem *item); |
| 202 | |
| 203 | static IMAPSession *imap_session_get (Folder *folder);
|
| 204 | |
| 205 | static gint imap_greeting (IMAPSession *session);
|
| 206 | static gint imap_auth (IMAPSession *session,
|
| 207 | const gchar *user,
|
| 208 | const gchar *pass,
|
| 209 | IMAPAuthType type); |
| 210 | |
| 211 | static gint imap_scan_tree_recursive (IMAPSession *session,
|
| 212 | FolderItem *item, |
| 213 | GSList *item_list); |
| 214 | static GSList *imap_get_folder_list (IMAPSession *session,
|
| 215 | FolderItem *item); |
| 216 | static GSList *imap_parse_list (IMAPSession *session,
|
| 217 | const gchar *real_path,
|
| 218 | gchar *separator); |
| 219 | static GSList *imap_add_inter_folders (GSList *item_list,
|
| 220 | const gchar *root_path);
|
| 221 | static GSList *imap_get_part_folder_list(GSList *item_list,
|
| 222 | FolderItem *item); |
| 223 | |
| 224 | static void imap_create_missing_folders (Folder *folder); |
| 225 | static FolderItem *imap_create_special_folder
|
| 226 | (Folder *folder, |
| 227 | SpecialFolderItemType stype, |
| 228 | const gchar *name);
|
| 229 | |
| 230 | static gint imap_do_copy_msgs (Folder *folder,
|
| 231 | FolderItem *dest, |
| 232 | GSList *msglist, |
| 233 | gboolean remove_source); |
| 234 | static gint imap_remove_msgs_by_seq_set (Folder *folder,
|
| 235 | FolderItem *item, |
| 236 | GSList *seq_list); |
| 237 | |
| 238 | static GSList *imap_get_uncached_messages (IMAPSession *session,
|
| 239 | FolderItem *item, |
| 240 | guint32 first_uid, |
| 241 | guint32 last_uid, |
| 242 | gint exists, |
| 243 | gboolean update_count); |
| 244 | static void imap_delete_cached_message (FolderItem *item, |
| 245 | guint32 uid); |
| 246 | static GSList *imap_delete_cached_messages (GSList *mlist,
|
| 247 | FolderItem *item, |
| 248 | guint32 first_uid, |
| 249 | guint32 last_uid); |
| 250 | static void imap_delete_all_cached_messages (FolderItem *item); |
| 251 | |
| 252 | #if USE_SSL
|
| 253 | static SockInfo *imap_open (const gchar *server, |
| 254 | gushort port, |
| 255 | SocksInfo *socks_info, |
| 256 | SSLType ssl_type); |
| 257 | #else
|
| 258 | static SockInfo *imap_open (const gchar *server, |
| 259 | gushort port, |
| 260 | SocksInfo *socks_info); |
| 261 | #endif
|
| 262 | |
| 263 | static gint imap_msg_list_change_perm_flags (GSList *msglist,
|
| 264 | MsgPermFlags flags, |
| 265 | gboolean is_set); |
| 266 | static gchar *imap_get_flag_str (IMAPFlags flags);
|
| 267 | static gint imap_set_message_flags (IMAPSession *session,
|
| 268 | const gchar *seq_set,
|
| 269 | IMAPFlags flags, |
| 270 | gboolean is_set); |
| 271 | static gint imap_select (IMAPSession *session,
|
| 272 | IMAPFolder *folder, |
| 273 | const gchar *path,
|
| 274 | gint *exists, |
| 275 | gint *recent, |
| 276 | gint *unseen, |
| 277 | guint32 *uid_validity); |
| 278 | static gint imap_status (IMAPSession *session,
|
| 279 | IMAPFolder *folder, |
| 280 | const gchar *path,
|
| 281 | gint *messages, |
| 282 | gint *recent, |
| 283 | guint32 *uid_next, |
| 284 | guint32 *uid_validity, |
| 285 | gint *unseen); |
| 286 | |
| 287 | static void imap_parse_namespace (IMAPSession *session, |
| 288 | IMAPFolder *folder); |
| 289 | static void imap_get_namespace_by_list (IMAPSession *session, |
| 290 | IMAPFolder *folder); |
| 291 | static IMAPNameSpace *imap_find_namespace (IMAPFolder *folder,
|
| 292 | const gchar *path);
|
| 293 | static gchar imap_get_path_separator (IMAPFolder *folder,
|
| 294 | const gchar *path);
|
| 295 | static gchar *imap_get_real_path (IMAPFolder *folder,
|
| 296 | const gchar *path);
|
| 297 | |
| 298 | static gchar *imap_parse_atom (IMAPSession *session,
|
| 299 | gchar *src, |
| 300 | gchar *dest, |
| 301 | gint dest_len, |
| 302 | GString *str); |
| 303 | static MsgFlags imap_parse_flags (const gchar *flag_str); |
| 304 | static IMAPFlags imap_parse_imap_flags (const gchar *flag_str); |
| 305 | static MsgInfo *imap_parse_envelope (IMAPSession *session,
|
| 306 | FolderItem *item, |
| 307 | GString *line_str); |
| 308 | |
| 309 | static gboolean imap_has_capability (IMAPSession *session,
|
| 310 | const gchar *capability);
|
| 311 | static void imap_capability_free (IMAPSession *session); |
| 312 | |
| 313 | /* low-level IMAP4rev1 commands */
|
| 314 | static gint imap_cmd_capability (IMAPSession *session);
|
| 315 | static gint imap_cmd_authenticate
|
| 316 | (IMAPSession *session, |
| 317 | const gchar *user,
|
| 318 | const gchar *pass,
|
| 319 | IMAPAuthType type); |
| 320 | static gint imap_cmd_login (IMAPSession *session,
|
| 321 | const gchar *user,
|
| 322 | const gchar *pass);
|
| 323 | static gint imap_cmd_logout (IMAPSession *session);
|
| 324 | static gint imap_cmd_noop (IMAPSession *session);
|
| 325 | #if USE_SSL
|
| 326 | static gint imap_cmd_starttls (IMAPSession *session);
|
| 327 | #endif
|
| 328 | static gint imap_cmd_namespace (IMAPSession *session,
|
| 329 | gchar **ns_str); |
| 330 | static gint imap_cmd_list (IMAPSession *session,
|
| 331 | const gchar *ref,
|
| 332 | const gchar *mailbox,
|
| 333 | GPtrArray *argbuf); |
| 334 | static gint imap_cmd_do_select (IMAPSession *session,
|
| 335 | const gchar *folder,
|
| 336 | gboolean examine, |
| 337 | gint *exists, |
| 338 | gint *recent, |
| 339 | gint *unseen, |
| 340 | guint32 *uid_validity); |
| 341 | static gint imap_cmd_select (IMAPSession *session,
|
| 342 | const gchar *folder,
|
| 343 | gint *exists, |
| 344 | gint *recent, |
| 345 | gint *unseen, |
| 346 | guint32 *uid_validity); |
| 347 | static gint imap_cmd_examine (IMAPSession *session,
|
| 348 | const gchar *folder,
|
| 349 | gint *exists, |
| 350 | gint *recent, |
| 351 | gint *unseen, |
| 352 | guint32 *uid_validity); |
| 353 | static gint imap_cmd_create (IMAPSession *session,
|
| 354 | const gchar *folder);
|
| 355 | static gint imap_cmd_rename (IMAPSession *session,
|
| 356 | const gchar *oldfolder,
|
| 357 | const gchar *newfolder);
|
| 358 | static gint imap_cmd_delete (IMAPSession *session,
|
| 359 | const gchar *folder);
|
| 360 | static gint imap_cmd_envelope (IMAPSession *session,
|
| 361 | const gchar *seq_set);
|
| 362 | static gint imap_cmd_search (IMAPSession *session,
|
| 363 | const gchar *criteria,
|
| 364 | GArray **result); |
| 365 | static gint imap_cmd_fetch (IMAPSession *session,
|
| 366 | guint32 uid, |
| 367 | const gchar *filename);
|
| 368 | static gint imap_cmd_append (IMAPSession *session,
|
| 369 | const gchar *destfolder,
|
| 370 | const gchar *file,
|
| 371 | IMAPFlags flags, |
| 372 | guint32 *new_uid); |
| 373 | static gint imap_cmd_copy (IMAPSession *session,
|
| 374 | const gchar *seq_set,
|
| 375 | const gchar *destfolder);
|
| 376 | static gint imap_cmd_store (IMAPSession *session,
|
| 377 | const gchar *seq_set,
|
| 378 | const gchar *sub_cmd);
|
| 379 | static gint imap_cmd_expunge (IMAPSession *session);
|
| 380 | static gint imap_cmd_close (IMAPSession *session);
|
| 381 | |
| 382 | static gint imap_cmd_ok (IMAPSession *session,
|
| 383 | GPtrArray *argbuf); |
| 384 | static gint imap_cmd_ok_real (IMAPSession *session,
|
| 385 | GPtrArray *argbuf); |
| 386 | static gint imap_cmd_gen_send (IMAPSession *session,
|
| 387 | const gchar *format, ...);
|
| 388 | static gint imap_cmd_gen_recv (IMAPSession *session,
|
| 389 | gchar **ret); |
| 390 | |
| 391 | static gint imap_cmd_gen_recv_silent (IMAPSession *session,
|
| 392 | gchar **ret); |
| 393 | |
| 394 | /* misc utility functions */
|
| 395 | static gchar *strchr_cpy (const gchar *src, |
| 396 | gchar ch, |
| 397 | gchar *dest, |
| 398 | gint len); |
| 399 | static gchar *get_quoted (const gchar *src, |
| 400 | gchar ch, |
| 401 | gchar *dest, |
| 402 | gint len); |
| 403 | static gchar *search_array_contain_str (GPtrArray *array,
|
| 404 | gchar *str); |
| 405 | static gchar *search_array_str (GPtrArray *array,
|
| 406 | gchar *str); |
| 407 | static void imap_path_separator_subst (gchar *str, |
| 408 | gchar separator); |
| 409 | |
| 410 | static gchar *imap_modified_utf7_to_utf8 (const gchar *mutf7_str); |
| 411 | static gchar *imap_utf8_to_modified_utf7 (const gchar *from); |
| 412 | |
| 413 | static GSList *imap_get_seq_set_from_msglist (GSList *msglist,
|
| 414 | gint limit); |
| 415 | static gint imap_seq_set_get_count (const gchar *seq_set); |
| 416 | static void imap_seq_set_free (GSList *seq_list); |
| 417 | |
| 418 | static GHashTable *imap_get_uid_table (GArray *array);
|
| 419 | |
| 420 | static gboolean imap_rename_folder_func (GNode *node,
|
| 421 | gpointer data); |
| 422 | |
| 423 | #if USE_THREADS
|
| 424 | static gint imap_thread_run (IMAPSession *session,
|
| 425 | IMAPThreadFunc func, |
| 426 | gpointer data); |
| 427 | static gint imap_thread_run_progress (IMAPSession *session,
|
| 428 | IMAPThreadFunc func, |
| 429 | IMAPProgressFunc progress_func, |
| 430 | gpointer data); |
| 431 | #endif
|
| 432 | |
| 433 | static FolderClass imap_class =
|
| 434 | {
|
| 435 | F_IMAP, |
| 436 | |
| 437 | imap_folder_new, |
| 438 | imap_folder_destroy, |
| 439 | |
| 440 | imap_scan_tree, |
| 441 | imap_create_tree, |
| 442 | |
| 443 | imap_get_msg_list, |
| 444 | imap_get_uncached_msg_list, |
| 445 | imap_fetch_msg, |
| 446 | imap_get_msginfo, |
| 447 | imap_add_msg, |
| 448 | imap_add_msgs, |
| 449 | imap_add_msg_msginfo, |
| 450 | imap_add_msgs_msginfo, |
| 451 | imap_move_msg, |
| 452 | imap_move_msgs, |
| 453 | imap_copy_msg, |
| 454 | imap_copy_msgs, |
| 455 | imap_remove_msg, |
| 456 | imap_remove_msgs, |
| 457 | imap_remove_all_msg, |
| 458 | imap_is_msg_changed, |
| 459 | imap_close, |
| 460 | imap_scan_folder, |
| 461 | |
| 462 | imap_create_folder, |
| 463 | imap_rename_folder, |
| 464 | imap_move_folder, |
| 465 | imap_remove_folder |
| 466 | }; |
| 467 | |
| 468 | |
| 469 | FolderClass *imap_get_class(void)
|
| 470 | {
|
| 471 | return &imap_class;
|
| 472 | } |
| 473 | |
| 474 | static Folder *imap_folder_new(const gchar *name, const gchar *path) |
| 475 | {
|
| 476 | Folder *folder; |
| 477 | |
| 478 | folder = (Folder *)g_new0(IMAPFolder, 1);
|
| 479 | imap_folder_init(folder, name, path); |
| 480 | |
| 481 | return folder;
|
| 482 | } |
| 483 | |
| 484 | static void imap_folder_destroy(Folder *folder) |
| 485 | {
|
| 486 | g_return_if_fail(folder->account != NULL);
|
| 487 | |
| 488 | if (REMOTE_FOLDER(folder)->remove_cache_on_destroy) {
|
| 489 | gchar *dir; |
| 490 | gchar *server; |
| 491 | |
| 492 | dir = folder_get_path(folder); |
| 493 | if (is_dir_exist(dir))
|
| 494 | remove_dir_recursive(dir); |
| 495 | g_free(dir); |
| 496 | |
| 497 | server = uriencode_for_filename(folder->account->recv_server); |
| 498 | dir = g_strconcat(get_imap_cache_dir(), G_DIR_SEPARATOR_S, |
| 499 | server, NULL);
|
| 500 | if (is_dir_exist(dir))
|
| 501 | g_rmdir(dir); |
| 502 | g_free(dir); |
| 503 | g_free(server); |
| 504 | } |
| 505 | |
| 506 | folder_remote_folder_destroy(REMOTE_FOLDER(folder)); |
| 507 | } |
| 508 | |
| 509 | static void imap_folder_init(Folder *folder, const gchar *name, |
| 510 | const gchar *path)
|
| 511 | {
|
| 512 | folder->klass = imap_get_class(); |
| 513 | folder_remote_folder_init(folder, name, path); |
| 514 | } |
| 515 | |
| 516 | static IMAPSession *imap_session_get(Folder *folder)
|
| 517 | {
|
| 518 | RemoteFolder *rfolder = REMOTE_FOLDER(folder); |
| 519 | gint ret; |
| 520 | |
| 521 | g_return_val_if_fail(folder != NULL, NULL); |
| 522 | g_return_val_if_fail(FOLDER_TYPE(folder) == F_IMAP, NULL);
|
| 523 | g_return_val_if_fail(folder->account != NULL, NULL); |
| 524 | |
| 525 | if (!prefs_common.online_mode)
|
| 526 | return NULL; |
| 527 | |
| 528 | if (!rfolder->session) {
|
| 529 | rfolder->session = imap_session_new(folder->account); |
| 530 | if (rfolder->session)
|
| 531 | imap_parse_namespace(IMAP_SESSION(rfolder->session), |
| 532 | IMAP_FOLDER(folder)); |
| 533 | return IMAP_SESSION(rfolder->session);
|
| 534 | } |
| 535 | |
| 536 | if (imap_is_session_active(IMAP_FOLDER(folder))) {
|
| 537 | g_warning("imap_session_get: session is busy.");
|
| 538 | return NULL; |
| 539 | } |
| 540 | |
| 541 | if (time(NULL) - rfolder->session->last_access_time < |
| 542 | SESSION_TIMEOUT_INTERVAL) {
|
| 543 | return IMAP_SESSION(rfolder->session);
|
| 544 | } |
| 545 | |
| 546 | if ((ret = imap_cmd_noop(IMAP_SESSION(rfolder->session))) != IMAP_SUCCESS) {
|
| 547 | if (ret == IMAP_EAGAIN) {
|
| 548 | g_warning("imap_session_get: session is busy.");
|
| 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | log_warning(_("IMAP4 connection to %s has been"
|
| 553 | " disconnected. Reconnecting...\n"),
|
| 554 | folder->account->recv_server); |
| 555 | if (imap_session_reconnect(IMAP_SESSION(rfolder->session))
|
| 556 | == IMAP_SUCCESS) |
| 557 | imap_parse_namespace(IMAP_SESSION(rfolder->session), |
| 558 | IMAP_FOLDER(folder)); |
| 559 | else {
|
| 560 | session_destroy(rfolder->session); |
| 561 | rfolder->session = NULL;
|
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return IMAP_SESSION(rfolder->session);
|
| 566 | } |
| 567 | |
| 568 | static gint imap_greeting(IMAPSession *session)
|
| 569 | {
|
| 570 | gchar *greeting; |
| 571 | gint ok; |
| 572 | |
| 573 | if ((ok = imap_cmd_gen_recv(session, &greeting)) != IMAP_SUCCESS) {
|
| 574 | log_warning("Cannot get greeting message (%d)\n", ok);
|
| 575 | return ok;
|
| 576 | } |
| 577 | |
| 578 | if (greeting[0] != '*' || greeting[1] != ' ') |
| 579 | ok = IMAP_ERROR; |
| 580 | else if (!strncmp(greeting + 2, "OK", 2)) |
| 581 | ok = IMAP_SUCCESS; |
| 582 | else if (!strncmp(greeting + 2, "PREAUTH", 7)) { |
| 583 | session->authenticated = TRUE; |
| 584 | ok = IMAP_SUCCESS; |
| 585 | } else
|
| 586 | ok = IMAP_ERROR; |
| 587 | |
| 588 | g_free(greeting); |
| 589 | return ok;
|
| 590 | } |
| 591 | |
| 592 | static gint imap_auth(IMAPSession *session, const gchar *user, |
| 593 | const gchar *pass, IMAPAuthType type)
|
| 594 | {
|
| 595 | gboolean nologin; |
| 596 | gint ok = IMAP_AUTHFAIL; |
| 597 | |
| 598 | nologin = imap_has_capability(session, "LOGINDISABLED");
|
| 599 | |
| 600 | switch (type) {
|
| 601 | case 0: |
| 602 | if (imap_has_capability(session, "AUTH=CRAM-MD5")) |
| 603 | ok = imap_cmd_authenticate(session, user, pass, |
| 604 | IMAP_AUTH_CRAM_MD5); |
| 605 | else if (imap_has_capability(session, "AUTH=PLAIN")) |
| 606 | ok = imap_cmd_authenticate(session, user, pass, |
| 607 | IMAP_AUTH_PLAIN); |
| 608 | else if (nologin) |
| 609 | log_print(_("IMAP4 server disables LOGIN.\n"));
|
| 610 | else
|
| 611 | ok = imap_cmd_login(session, user, pass); |
| 612 | break;
|
| 613 | case IMAP_AUTH_LOGIN:
|
| 614 | if (nologin)
|
| 615 | log_warning(_("IMAP4 server disables LOGIN.\n"));
|
| 616 | else
|
| 617 | ok = imap_cmd_login(session, user, pass); |
| 618 | break;
|
| 619 | case IMAP_AUTH_CRAM_MD5:
|
| 620 | case IMAP_AUTH_PLAIN:
|
| 621 | ok = imap_cmd_authenticate(session, user, pass, type); |
| 622 | break;
|
| 623 | default:
|
| 624 | break;
|
| 625 | } |
| 626 | |
| 627 | if (ok == IMAP_SUCCESS)
|
| 628 | session->authenticated = TRUE; |
| 629 | |
| 630 | return ok;
|
| 631 | } |
| 632 | |
| 633 | static Session *imap_session_new(PrefsAccount *account)
|
| 634 | {
|
| 635 | IMAPSession *session; |
| 636 | gushort port; |
| 637 | |
| 638 | g_return_val_if_fail(account != NULL, NULL); |
| 639 | g_return_val_if_fail(account->recv_server != NULL, NULL); |
| 640 | g_return_val_if_fail(account->userid != NULL, NULL); |
| 641 | |
| 642 | #if USE_SSL
|
| 643 | port = account->set_imapport ? account->imapport |
| 644 | : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT; |
| 645 | #else
|
| 646 | port = account->set_imapport ? account->imapport : IMAP4_PORT; |
| 647 | #endif
|
| 648 | |
| 649 | session = IMAP_SESSION(g_new0(IMAPRealSession, 1));
|
| 650 | |
| 651 | session_init(SESSION(session)); |
| 652 | |
| 653 | SESSION(session)->type = SESSION_IMAP; |
| 654 | SESSION(session)->sock = NULL;
|
| 655 | SESSION(session)->server = g_strdup(account->recv_server); |
| 656 | SESSION(session)->port = port; |
| 657 | #if USE_SSL
|
| 658 | SESSION(session)->ssl_type = account->ssl_imap; |
| 659 | #endif
|
| 660 | SESSION(session)->last_access_time = time(NULL);
|
| 661 | SESSION(session)->data = account; |
| 662 | |
| 663 | SESSION(session)->destroy = imap_session_destroy; |
| 664 | |
| 665 | session->authenticated = FALSE; |
| 666 | session->capability = NULL;
|
| 667 | session->uidplus = FALSE; |
| 668 | session->mbox = NULL;
|
| 669 | session->cmd_count = 0;
|
| 670 | |
| 671 | session_list = g_list_append(session_list, session); |
| 672 | |
| 673 | if (imap_session_connect(session) != IMAP_SUCCESS) {
|
| 674 | log_warning(_("Could not establish IMAP connection.\n"));
|
| 675 | session_destroy(SESSION(session)); |
| 676 | return NULL; |
| 677 | } |
| 678 | |
| 679 | return SESSION(session);
|
| 680 | } |
| 681 | |
| 682 | static gint imap_session_connect(IMAPSession *session)
|
| 683 | {
|
| 684 | SockInfo *sock; |
| 685 | SocksInfo *socks_info = NULL;
|
| 686 | PrefsAccount *account; |
| 687 | const gchar *pass;
|
| 688 | |
| 689 | g_return_val_if_fail(session != NULL, IMAP_ERROR);
|
| 690 | |
| 691 | account = (PrefsAccount *)(SESSION(session)->data); |
| 692 | |
| 693 | log_message(_("creating IMAP4 connection to %s:%d ...\n"),
|
| 694 | SESSION(session)->server, SESSION(session)->port); |
| 695 | |
| 696 | pass = account->passwd; |
| 697 | if (!pass)
|
| 698 | pass = account->tmp_pass; |
| 699 | if (!pass) {
|
| 700 | gchar *tmp_pass; |
| 701 | |
| 702 | tmp_pass = input_query_password(account->recv_server, |
| 703 | account->userid); |
| 704 | if (!tmp_pass)
|
| 705 | return IMAP_ERROR;
|
| 706 | |
| 707 | account->tmp_pass = tmp_pass; |
| 708 | pass = account->tmp_pass; |
| 709 | } |
| 710 | |
| 711 | if (account->use_socks && account->use_socks_for_recv &&
|
| 712 | account->proxy_host) {
|
| 713 | socks_info = socks_info_new(account->socks_type, account->proxy_host, account->proxy_port, account->use_proxy_auth ? account->proxy_name : NULL, account->use_proxy_auth ? account->proxy_pass : NULL); |
| 714 | } |
| 715 | |
| 716 | #if USE_SSL
|
| 717 | if ((sock = imap_open(SESSION(session)->server, SESSION(session)->port,
|
| 718 | socks_info, SESSION(session)->ssl_type)) == NULL)
|
| 719 | #else
|
| 720 | if ((sock = imap_open(SESSION(session)->server, SESSION(session)->port,
|
| 721 | socks_info)) |
| 722 | == NULL)
|
| 723 | #endif
|
| 724 | return IMAP_ERROR;
|
| 725 | |
| 726 | if (socks_info)
|
| 727 | socks_info_free(socks_info); |
| 728 | |
| 729 | SESSION(session)->sock = sock; |
| 730 | |
| 731 | if (imap_greeting(session) != IMAP_SUCCESS)
|
| 732 | return IMAP_ERROR;
|
| 733 | if (imap_cmd_capability(session) != IMAP_SUCCESS)
|
| 734 | return IMAP_ERROR;
|
| 735 | |
| 736 | if (imap_has_capability(session, "UIDPLUS")) |
| 737 | session->uidplus = TRUE; |
| 738 | |
| 739 | #if USE_SSL
|
| 740 | if (account->ssl_imap == SSL_STARTTLS &&
|
| 741 | imap_has_capability(session, "STARTTLS")) {
|
| 742 | gint ok; |
| 743 | |
| 744 | ok = imap_cmd_starttls(session); |
| 745 | if (ok != IMAP_SUCCESS) {
|
| 746 | log_warning(_("Can't start TLS session.\n"));
|
| 747 | return IMAP_ERROR;
|
| 748 | } |
| 749 | if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1))
|
| 750 | return IMAP_SOCKET;
|
| 751 | |
| 752 | /* capability can be changed after STARTTLS */
|
| 753 | if (imap_cmd_capability(session) != IMAP_SUCCESS)
|
| 754 | return IMAP_ERROR;
|
| 755 | } |
| 756 | #endif
|
| 757 | |
| 758 | if (!session->authenticated &&
|
| 759 | imap_auth(session, account->userid, pass, account->imap_auth_type) |
| 760 | != IMAP_SUCCESS) {
|
| 761 | if (account->tmp_pass) {
|
| 762 | g_free(account->tmp_pass); |
| 763 | account->tmp_pass = NULL;
|
| 764 | } |
| 765 | imap_cmd_logout(session); |
| 766 | return IMAP_AUTHFAIL;
|
| 767 | } |
| 768 | |
| 769 | return IMAP_SUCCESS;
|
| 770 | } |
| 771 | |
| 772 | static gint imap_session_reconnect(IMAPSession *session)
|
| 773 | {
|
| 774 | g_return_val_if_fail(session != NULL, IMAP_ERROR);
|
| 775 | |
| 776 | session_disconnect(SESSION(session)); |
| 777 | |
| 778 | imap_capability_free(session); |
| 779 | session->uidplus = FALSE; |
| 780 | g_free(session->mbox); |
| 781 | session->mbox = NULL;
|
| 782 | session->authenticated = FALSE; |
| 783 | SESSION(session)->state = SESSION_READY; |
| 784 | |
| 785 | return imap_session_connect(session);
|
| 786 | } |
| 787 | |
| 788 | static void imap_session_destroy(Session *session) |
| 789 | {
|
| 790 | #if USE_THREADS
|
| 791 | IMAPRealSession *real = (IMAPRealSession *)session; |
| 792 | |
| 793 | if (real->pool)
|
| 794 | g_thread_pool_free(real->pool, TRUE, TRUE); |
| 795 | #endif
|
| 796 | imap_capability_free(IMAP_SESSION(session)); |
| 797 | g_free(IMAP_SESSION(session)->mbox); |
| 798 | session_list = g_list_remove(session_list, session); |
| 799 | } |
| 800 | |
| 801 | #if 0
|
| 802 | static void imap_session_destroy_all(void) |
| 803 | {
|
| 804 | while (session_list != NULL) {
|
| 805 | IMAPSession *session = (IMAPSession *)session_list->data; |
| 806 | |
| 807 | imap_cmd_logout(session); |
| 808 | session_destroy(SESSION(session)); |
| 809 | } |
| 810 | } |
| 811 | #endif |
| 812 | |
| 813 | #define THROW goto catch |
| 814 | |
| 815 | static gint imap_search_flags(IMAPSession *session, GArray **uids,
|
| 816 | GHashTable **flags_table) |
| 817 | {
|
| 818 | gint ok; |
| 819 | gint i; |
| 820 | GArray *flag_uids; |
| 821 | GHashTable *unseen_table; |
| 822 | GHashTable *flagged_table; |
| 823 | GHashTable *answered_table; |
| 824 | guint32 uid; |
| 825 | IMAPFlags flags; |
| 826 | |
| 827 | ok = imap_cmd_search(session, "ALL", uids);
|
| 828 | if (ok != IMAP_SUCCESS) return ok; |
| 829 | |
| 830 | ok = imap_cmd_search(session, "UNSEEN", &flag_uids);
|
| 831 | if (ok != IMAP_SUCCESS) {
|
| 832 | g_array_free(*uids, TRUE); |
| 833 | return ok;
|
| 834 | } |
| 835 | unseen_table = imap_get_uid_table(flag_uids); |
| 836 | g_array_free(flag_uids, TRUE); |
| 837 | ok = imap_cmd_search(session, "FLAGGED", &flag_uids);
|
| 838 | if (ok != IMAP_SUCCESS) {
|
| 839 | g_hash_table_destroy(unseen_table); |
| 840 | g_array_free(*uids, TRUE); |
| 841 | return ok;
|
| 842 | } |
| 843 | flagged_table = imap_get_uid_table(flag_uids); |
| 844 | g_array_free(flag_uids, TRUE); |
| 845 | ok = imap_cmd_search(session, "ANSWERED", &flag_uids);
|
| 846 | if (ok != IMAP_SUCCESS) {
|
| 847 | g_hash_table_destroy(flagged_table); |
| 848 | g_hash_table_destroy(unseen_table); |
| 849 | g_array_free(*uids, TRUE); |
| 850 | return ok;
|
| 851 | } |
| 852 | answered_table = imap_get_uid_table(flag_uids); |
| 853 | g_array_free(flag_uids, TRUE); |
| 854 | |
| 855 | *flags_table = g_hash_table_new(NULL, g_direct_equal);
|
| 856 | |
| 857 | for (i = 0; i < (*uids)->len; i++) { |
| 858 | uid = g_array_index(*uids, guint32, i); |
| 859 | flags = IMAP_FLAG_DRAFT; |
| 860 | if (!g_hash_table_lookup(unseen_table, GUINT_TO_POINTER(uid)))
|
| 861 | flags |= IMAP_FLAG_SEEN; |
| 862 | if (g_hash_table_lookup(flagged_table, GUINT_TO_POINTER(uid)))
|
| 863 | flags |= IMAP_FLAG_FLAGGED; |
| 864 | if (g_hash_table_lookup(answered_table, GUINT_TO_POINTER(uid)))
|
| 865 | flags |= IMAP_FLAG_ANSWERED; |
| 866 | g_hash_table_insert(*flags_table, GUINT_TO_POINTER(uid), |
| 867 | GINT_TO_POINTER(flags)); |
| 868 | } |
| 869 | |
| 870 | g_hash_table_destroy(answered_table); |
| 871 | g_hash_table_destroy(flagged_table); |
| 872 | g_hash_table_destroy(unseen_table); |
| 873 | |
| 874 | return IMAP_SUCCESS;
|
| 875 | } |
| 876 | |
| 877 | static gint imap_fetch_flags(IMAPSession *session, GArray **uids,
|
| 878 | GHashTable **flags_table) |
| 879 | {
|
| 880 | gint ok; |
| 881 | gchar *tmp; |
| 882 | gchar *cur_pos; |
| 883 | gchar buf[IMAPBUFSIZE]; |
| 884 | guint32 uid; |
| 885 | IMAPFlags flags; |
| 886 | |
| 887 | if (imap_cmd_gen_send(session, "UID FETCH 1:* (UID FLAGS)") != IMAP_SUCCESS) |
| 888 | return IMAP_ERROR;
|
| 889 | |
| 890 | *uids = g_array_new(FALSE, FALSE, sizeof(guint32));
|
| 891 | *flags_table = g_hash_table_new(NULL, g_direct_equal);
|
| 892 | |
| 893 | log_print("IMAP4< %s\n", _("(retrieving FLAGS...)")); |
| 894 | |
| 895 | while ((ok = imap_cmd_gen_recv_silent(session, &tmp)) == IMAP_SUCCESS) {
|
| 896 | if (tmp[0] != '*' || tmp[1] != ' ') { |
| 897 | log_print("IMAP4< %s\n", tmp);
|
| 898 | g_free(tmp); |
| 899 | break;
|
| 900 | } |
| 901 | cur_pos = tmp + 2;
|
| 902 | |
| 903 | #define PARSE_ONE_ELEMENT(ch) \
|
| 904 | { \
|
| 905 | cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf)); \
|
| 906 | if (cur_pos == NULL) { \ |
| 907 | g_warning("cur_pos == NULL\n"); \
|
| 908 | g_free(tmp); \ |
| 909 | g_hash_table_destroy(*flags_table); \ |
| 910 | g_array_free(*uids, TRUE); \ |
| 911 | return IMAP_ERROR; \
|
| 912 | } \ |
| 913 | } |
| 914 | |
| 915 | PARSE_ONE_ELEMENT(' ');
|
| 916 | PARSE_ONE_ELEMENT(' ');
|
| 917 | if (strcmp(buf, "FETCH") != 0) { |
| 918 | g_free(tmp); |
| 919 | continue;
|
| 920 | } |
| 921 | if (*cur_pos != '(') { |
| 922 | g_free(tmp); |
| 923 | continue;
|
| 924 | } |
| 925 | cur_pos++; |
| 926 | uid = 0;
|
| 927 | flags = 0;
|
| 928 | |
| 929 | while (*cur_pos != '\0' && *cur_pos != ')') { |
| 930 | while (*cur_pos == ' ') cur_pos++; |
| 931 | |
| 932 | if (!strncmp(cur_pos, "UID ", 4)) { |
| 933 | cur_pos += 4;
|
| 934 | uid = strtoul(cur_pos, &cur_pos, 10);
|
| 935 | } else if (!strncmp(cur_pos, "FLAGS ", 6)) { |
| 936 | cur_pos += 6;
|
| 937 | if (*cur_pos != '(') { |
| 938 | g_warning("*cur_pos != '('\n");
|
| 939 | break;
|
| 940 | } |
| 941 | cur_pos++; |
| 942 | PARSE_ONE_ELEMENT(')');
|
| 943 | flags = imap_parse_imap_flags(buf); |
| 944 | flags |= IMAP_FLAG_DRAFT; |
| 945 | } else {
|
| 946 | g_warning("invalid FETCH response: %s\n", cur_pos);
|
| 947 | break;
|
| 948 | } |
| 949 | } |
| 950 | |
| 951 | #undef PARSE_ONE_ELEMENT
|
| 952 | |
| 953 | if (uid > 0) { |
| 954 | g_array_append_val(*uids, uid); |
| 955 | g_hash_table_insert(*flags_table, GUINT_TO_POINTER(uid), |
| 956 | GINT_TO_POINTER(flags)); |
| 957 | } |
| 958 | |
| 959 | g_free(tmp); |
| 960 | } |
| 961 | |
| 962 | if (ok != IMAP_SUCCESS) {
|
| 963 | g_hash_table_destroy(*flags_table); |
| 964 | g_array_free(*uids, TRUE); |
| 965 | } |
| 966 | |
| 967 | return ok;
|
| 968 | } |
| 969 | |
| 970 | static GSList *imap_get_msg_list_full(Folder *folder, FolderItem *item,
|
| 971 | gboolean use_cache, |
| 972 | gboolean uncached_only) |
| 973 | {
|
| 974 | GSList *mlist = NULL;
|
| 975 | IMAPSession *session; |
| 976 | gint ok, exists = 0, recent = 0, unseen = 0; |
| 977 | guint32 uid_validity = 0;
|
| 978 | guint32 first_uid = 0, last_uid = 0; |
| 979 | GSList *newlist = NULL;
|
| 980 | |
| 981 | g_return_val_if_fail(folder != NULL, NULL); |
| 982 | g_return_val_if_fail(item != NULL, NULL); |
| 983 | g_return_val_if_fail(FOLDER_TYPE(folder) == F_IMAP, NULL);
|
| 984 | g_return_val_if_fail(folder->account != NULL, NULL); |
| 985 | |
| 986 | item->new = item->unread = item->total = 0;
|
| 987 | |
| 988 | session = imap_session_get(folder); |
| 989 | |
| 990 | if (!session) {
|
| 991 | if (uncached_only)
|
| 992 | return NULL; |
| 993 | mlist = procmsg_read_cache(item, FALSE); |
| 994 | item->last_num = procmsg_get_last_num_in_msg_list(mlist); |
| 995 | procmsg_set_flags(mlist, item); |
| 996 | return mlist;
|
| 997 | } |
| 998 | |
| 999 | ok = imap_select(session, IMAP_FOLDER(folder), item->path, |
| 1000 | &exists, &recent, &unseen, &uid_validity); |
| 1001 | if (ok != IMAP_SUCCESS) THROW;
|
| 1002 | |
| 1003 | if (exists == 0) { |
| 1004 | imap_delete_all_cached_messages(item); |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
| 1008 | /* invalidate current cache if UIDVALIDITY has been changed */
|
| 1009 | if (item->mtime != uid_validity) {
|
| 1010 | debug_print("imap_get_msg_list: "
|
| 1011 | "UIDVALIDITY has been changed.\n");
|
| 1012 | use_cache = FALSE; |
| 1013 | } |
| 1014 | |
| 1015 | if (use_cache) {
|
| 1016 | GArray *uids; |
| 1017 | GHashTable *msg_table; |
| 1018 | GHashTable *flags_table; |
| 1019 | guint32 cache_last; |
| 1020 | guint32 begin = 0;
|
| 1021 | GSList *cur, *next = NULL;
|
| 1022 | MsgInfo *msginfo; |
| 1023 | IMAPFlags imap_flags; |
| 1024 | guint color; |
| 1025 | |
| 1026 | /* get cache data */
|
| 1027 | mlist = procmsg_read_cache(item, FALSE); |
| 1028 | procmsg_set_flags(mlist, item); |
| 1029 | cache_last = procmsg_get_last_num_in_msg_list(mlist); |
| 1030 | |
| 1031 | /* get all UID list and flags */
|
| 1032 | #if 0
|
| 1033 | ok = imap_search_flags(session, &uids, &flags_table); |
| 1034 | if (ok != IMAP_SUCCESS) {
|
| 1035 | if (ok == IMAP_SOCKET || ok == IMAP_IOERR) THROW; |
| 1036 | ok = imap_fetch_flags(session, &uids, &flags_table); |
| 1037 | if (ok != IMAP_SUCCESS) THROW; |
| 1038 | } |
| 1039 | #else |
| 1040 | ok = imap_fetch_flags(session, &uids, &flags_table); |
| 1041 | if (ok != IMAP_SUCCESS) THROW;
|
| 1042 | #endif
|
| 1043 | |
| 1044 | if (uids->len > 0) { |
| 1045 | first_uid = g_array_index(uids, guint32, 0);
|
| 1046 | last_uid = g_array_index(uids, guint32, uids->len - 1);
|
| 1047 | } else {
|
| 1048 | g_array_free(uids, TRUE); |
| 1049 | g_hash_table_destroy(flags_table); |
| 1050 | THROW; |
| 1051 | } |
| 1052 | |
| 1053 | /* sync message flags with server */
|
| 1054 | for (cur = mlist; cur != NULL; cur = next) { |
| 1055 | msginfo = (MsgInfo *)cur->data; |
| 1056 | next = cur->next; |
| 1057 | imap_flags = GPOINTER_TO_INT(g_hash_table_lookup |
| 1058 | (flags_table, |
| 1059 | GUINT_TO_POINTER(msginfo->msgnum))); |
| 1060 | |
| 1061 | if (imap_flags == 0) { |
| 1062 | debug_print("imap_get_msg_list: "
|
| 1063 | "message %u has been deleted.\n",
|
| 1064 | msginfo->msgnum); |
| 1065 | imap_delete_cached_message |
| 1066 | (item, msginfo->msgnum); |
| 1067 | if (MSG_IS_NEW(msginfo->flags))
|
| 1068 | item->new--; |
| 1069 | if (MSG_IS_UNREAD(msginfo->flags))
|
| 1070 | item->unread--; |
| 1071 | item->total--; |
| 1072 | mlist = g_slist_remove(mlist, msginfo); |
| 1073 | procmsg_msginfo_free(msginfo); |
| 1074 | item->cache_dirty = TRUE; |
| 1075 | item->mark_dirty = TRUE; |
| 1076 | continue;
|
| 1077 | } |
| 1078 | |
| 1079 | if (!IMAP_IS_SEEN(imap_flags)) {
|
| 1080 | if (!MSG_IS_UNREAD(msginfo->flags)) {
|
| 1081 | item->unread++; |
| 1082 | MSG_SET_PERM_FLAGS(msginfo->flags, |
| 1083 | MSG_UNREAD); |
| 1084 | item->mark_dirty = TRUE; |
| 1085 | } |
| 1086 | } else {
|
| 1087 | if (MSG_IS_NEW(msginfo->flags)) {
|
| 1088 | item->new--; |
| 1089 | item->mark_dirty = TRUE; |
| 1090 | } |
| 1091 | if (MSG_IS_UNREAD(msginfo->flags)) {
|
| 1092 | item->unread--; |
| 1093 | item->mark_dirty = TRUE; |
| 1094 | } |
| 1095 | MSG_UNSET_PERM_FLAGS(msginfo->flags, |
| 1096 | MSG_NEW|MSG_UNREAD); |
| 1097 | } |
| 1098 | |
| 1099 | if (IMAP_IS_FLAGGED(imap_flags)) {
|
| 1100 | if (!MSG_IS_MARKED(msginfo->flags)) {
|
| 1101 | MSG_SET_PERM_FLAGS(msginfo->flags, |
| 1102 | MSG_MARKED); |
| 1103 | item->mark_dirty = TRUE; |
| 1104 | } |
| 1105 | } else {
|
| 1106 | if (MSG_IS_MARKED(msginfo->flags)) {
|
| 1107 | MSG_UNSET_PERM_FLAGS(msginfo->flags, |
| 1108 | MSG_MARKED); |
| 1109 | item->mark_dirty = TRUE; |
| 1110 | } |
| 1111 | } |
| 1112 | if (IMAP_IS_ANSWERED(imap_flags)) {
|
| 1113 | if (!MSG_IS_REPLIED(msginfo->flags)) {
|
| 1114 | MSG_SET_PERM_FLAGS(msginfo->flags, |
| 1115 | MSG_REPLIED); |
| 1116 | item->mark_dirty = TRUE; |
| 1117 | } |
| 1118 | } else {
|
| 1119 | if (MSG_IS_REPLIED(msginfo->flags)) {
|
| 1120 | MSG_UNSET_PERM_FLAGS(msginfo->flags, |
| 1121 | MSG_REPLIED); |
| 1122 | item->mark_dirty = TRUE; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | color = IMAP_GET_COLORLABEL_VALUE(imap_flags); |
| 1127 | if (MSG_GET_COLORLABEL_VALUE(msginfo->flags) != color) {
|
| 1128 | MSG_UNSET_PERM_FLAGS(msginfo->flags, |
| 1129 | MSG_CLABEL_FLAG_MASK); |
| 1130 | MSG_SET_COLORLABEL_VALUE(msginfo->flags, color); |
| 1131 | item->mark_dirty = TRUE; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /* check for the first new message */
|
| 1136 | msg_table = procmsg_msg_hash_table_create(mlist); |
| 1137 | if (msg_table == NULL) |
| 1138 | begin = first_uid; |
| 1139 | else {
|
| 1140 | gint i; |
| 1141 | |
| 1142 | for (i = 0; i < uids->len; i++) { |
| 1143 | guint32 uid; |
| 1144 | |
| 1145 | uid = g_array_index(uids, guint32, i); |
| 1146 | if (g_hash_table_lookup
|
| 1147 | (msg_table, GUINT_TO_POINTER(uid)) |
| 1148 | == NULL) {
|
| 1149 | debug_print("imap_get_msg_list: "
|
| 1150 | "first new UID: %u\n", uid);
|
| 1151 | begin = uid; |
| 1152 | break;
|
| 1153 | } |
| 1154 | } |
| 1155 | g_hash_table_destroy(msg_table); |
| 1156 | } |
| 1157 | |
| 1158 | g_array_free(uids, TRUE); |
| 1159 | g_hash_table_destroy(flags_table); |
| 1160 | |
| 1161 | /* remove ununsed caches */
|
| 1162 | if (first_uid > 0 && last_uid > 0) { |
| 1163 | mlist = imap_delete_cached_messages |
| 1164 | (mlist, item, 0, first_uid - 1); |
| 1165 | mlist = imap_delete_cached_messages |
| 1166 | (mlist, item, begin > 0 ? begin : last_uid + 1, |
| 1167 | UINT_MAX); |
| 1168 | } |
| 1169 | |
| 1170 | if (begin > 0 && begin <= last_uid) { |
| 1171 | newlist = imap_get_uncached_messages |
| 1172 | (session, item, begin, last_uid, |
| 1173 | exists - item->total, TRUE); |
| 1174 | if (newlist) {
|
| 1175 | item->cache_dirty = TRUE; |
| 1176 | item->mark_dirty = TRUE; |
| 1177 | } |
| 1178 | mlist = g_slist_concat(mlist, newlist); |
| 1179 | } |
| 1180 | } else {
|
| 1181 | imap_delete_all_cached_messages(item); |
| 1182 | mlist = imap_get_uncached_messages(session, item, 0, 0, exists, |
| 1183 | TRUE); |
| 1184 | last_uid = procmsg_get_last_num_in_msg_list(mlist); |
| 1185 | item->cache_dirty = TRUE; |
| 1186 | item->mark_dirty = TRUE; |
| 1187 | newlist = mlist; |
| 1188 | } |
| 1189 | |
| 1190 | if (!uncached_only)
|
| 1191 | mlist = procmsg_sort_msg_list(mlist, item->sort_key, |
| 1192 | item->sort_type); |
| 1193 | |
| 1194 | item->last_num = last_uid; |
| 1195 | |
| 1196 | if (item->mark_queue)
|
| 1197 | item->mark_dirty = TRUE; |
| 1198 | |
| 1199 | debug_print("cache_dirty: %d, mark_dirty: %d\n",
|
| 1200 | item->cache_dirty, item->mark_dirty); |
| 1201 | |
| 1202 | if (!item->opened) {
|
| 1203 | item->mtime = uid_validity; |
| 1204 | if (item->cache_dirty)
|
| 1205 | procmsg_write_cache_list(item, mlist); |
| 1206 | if (item->mark_dirty)
|
| 1207 | procmsg_write_flags_list(item, mlist); |
| 1208 | } |
| 1209 | |
| 1210 | catch:
|
| 1211 | if (uncached_only) {
|
| 1212 | GSList *cur; |
| 1213 | |
| 1214 | if (newlist == NULL) { |
| 1215 | procmsg_msg_list_free(mlist); |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | if (mlist == newlist)
|
| 1219 | return newlist;
|
| 1220 | for (cur = mlist; cur != NULL; cur = cur->next) { |
| 1221 | if (cur->next == newlist) {
|
| 1222 | cur->next = NULL;
|
| 1223 | procmsg_msg_list_free(mlist); |
| 1224 | return newlist;
|
| 1225 | } |
| 1226 | } |
| 1227 | procmsg_msg_list_free(mlist); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
| 1231 | return mlist;
|
| 1232 | } |
| 1233 | |
| 1234 | #undef THROW
|
| 1235 | |
| 1236 | static GSList *imap_get_msg_list(Folder *folder, FolderItem *item,
|
| 1237 | gboolean use_cache) |
| 1238 | {
|
| 1239 | return imap_get_msg_list_full(folder, item, use_cache, FALSE);
|
| 1240 | } |
| 1241 | |
| 1242 | static GSList *imap_get_uncached_msg_list(Folder *folder, FolderItem *item)
|
| 1243 | {
|
| 1244 | return imap_get_msg_list_full(folder, item, TRUE, TRUE);
|
| 1245 | } |
| 1246 | |
| 1247 | static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
|
| 1248 | {
|
| 1249 | gchar *path, *filename; |
| 1250 | IMAPSession *session; |
| 1251 | gchar nstr[16];
|
| 1252 | guint32 uid32 = (guint32)uid; |
| 1253 | gint ok; |
| 1254 | |
| 1255 | g_return_val_if_fail(folder != NULL, NULL); |
| 1256 | g_return_val_if_fail(item != NULL, NULL); |
| 1257 | |
| 1258 | path = folder_item_get_path(item); |
| 1259 | if (!is_dir_exist(path))
|
| 1260 | make_dir_hier(path); |
| 1261 | g_snprintf(nstr, sizeof(nstr), "%u", uid32); |
| 1262 | filename = g_strconcat(path, G_DIR_SEPARATOR_S, nstr, NULL);
|
| 1263 | g_free(path); |
| 1264 | |
| 1265 | if (is_file_exist(filename) && get_file_size(filename) > 0) { |
| 1266 | debug_print("message %u has been already cached.\n", uid32);
|
| 1267 | return filename;
|
| 1268 | } |
| 1269 | |
| 1270 | session = imap_session_get(folder); |
| 1271 | if (!session) {
|
| 1272 | g_free(filename); |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | |
| 1276 | ok = imap_select(session, IMAP_FOLDER(folder), item->path, |
| 1277 | NULL, NULL, NULL, NULL); |
| 1278 | if (ok != IMAP_SUCCESS) {
|
| 1279 | g_warning("can't select mailbox %s\n", item->path);
|
| 1280 | g_free(filename); |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | |
| 1284 | status_print(_("Getting message %u"), uid32);
|
| 1285 | debug_print("getting message %u...\n", uid32);
|
| 1286 | ok = imap_cmd_fetch(session, uid32, filename); |
| 1287 | |
| 1288 | if (ok != IMAP_SUCCESS) {
|
| 1289 | g_warning("can't fetch message %u\n", uid32);
|
| 1290 | g_free(filename); |
| 1291 | return NULL; |
| 1292 | } |
| 1293 | |
| 1294 | return filename;
|
| 1295 | } |
| 1296 | |
| 1297 | static MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
|
| 1298 | {
|
| 1299 | IMAPSession *session; |
| 1300 | GSList *list; |
| 1301 | MsgInfo *msginfo = NULL;
|
| 1302 | gint ok; |
| 1303 | |
| 1304 | g_return_val_if_fail(folder != NULL, NULL); |
| 1305 | g_return_val_if_fail(item != NULL, NULL); |
| 1306 | |
| 1307 | session = imap_session_get(folder); |
| 1308 | g_return_val_if_fail(session != NULL, NULL); |
| 1309 | |
| 1310 | ok = imap_select(session, IMAP_FOLDER(folder), item->path, |
| 1311 | NULL, NULL, NULL, NULL); |
| 1312 | if (ok != IMAP_SUCCESS)
|
| 1313 | return NULL; |
| 1314 | |
| 1315 | list = imap_get_uncached_messages(session, item, uid, uid, 0, FALSE);
|
| 1316 | if (list) {
|
| 1317 | msginfo = (MsgInfo *)list->data; |
| 1318 | list->data = NULL;
|
| 1319 | } |
| 1320 | procmsg_msg_list_free(list); |
| 1321 | |
| 1322 | return msginfo;
|
| 1323 | } |
| 1324 | |
| 1325 | static gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file, |
| 1326 | MsgFlags *flags, gboolean remove_source) |
| 1327 | {
|
| 1328 | GSList file_list; |
| 1329 | MsgFileInfo fileinfo; |
| 1330 | |
| 1331 | g_return_val_if_fail(file != NULL, -1); |
| 1332 | |
| 1333 | fileinfo.file = (gchar *)file; |
| 1334 | fileinfo.flags = flags; |
| 1335 | file_list.data = &fileinfo; |
| 1336 | file_list.next = NULL;
|
| 1337 | |
| 1338 | return imap_add_msgs(folder, dest, &file_list, remove_source, NULL); |
| 1339 | } |
| 1340 | |
| 1341 | static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
|
| 1342 | gboolean remove_source, gint *first) |
| 1343 | {
|
| 1344 | gchar *destdir; |
| 1345 | IMAPSession *session; |
| 1346 | gint messages, recent, unseen; |
| 1347 | guint32 uid_next, uid_validity; |
| 1348 | guint32 last_uid = 0;
|
| 1349 | GSList *cur; |
| 1350 | MsgFileInfo *fileinfo; |
| 1351 | gint count = 1;
|
| 1352 | gint total; |
| 1353 | gint ok; |
| 1354 | GTimeVal tv_prev, tv_cur; |
| 1355 | |
| 1356 | g_return_val_if_fail(folder != NULL, -1); |
| 1357 | g_return_val_if_fail(dest != NULL, -1); |
| 1358 | g_return_val_if_fail(file_list != NULL, -1); |
| 1359 | |
| 1360 | session = imap_session_get(folder); |
| 1361 | if (!session) return -1; |
| 1362 | |
| 1363 | g_get_current_time(&tv_prev); |
| 1364 | ui_update(); |
| 1365 | |
| 1366 | ok = imap_status(session, IMAP_FOLDER(folder), dest->path, |
| 1367 | &messages, &recent, &uid_next, &uid_validity, &unseen); |
| 1368 | if (ok != IMAP_SUCCESS) {
|
| 1369 | g_warning("can't append messages\n");
|
| 1370 | return -1; |
| 1371 | } |
| 1372 | |
| 1373 | destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path); |
| 1374 | |
| 1375 | if (!session->uidplus)
|
| 1376 | last_uid = uid_next - 1;
|
| 1377 | if (first)
|
| 1378 | *first = uid_next; |
| 1379 | |
| 1380 | total = g_slist_length(file_list); |
| 1381 | |
| 1382 | for (cur = file_list; cur != NULL; cur = cur->next) { |
| 1383 | IMAPFlags iflags = 0;
|
| 1384 | guint32 new_uid = 0;
|
| 1385 | |
| 1386 | fileinfo = (MsgFileInfo *)cur->data; |
| 1387 | |
| 1388 | if (fileinfo->flags) {
|
| 1389 | if (MSG_IS_MARKED(*fileinfo->flags))
|
| 1390 | iflags |= IMAP_FLAG_FLAGGED; |
| 1391 | if (MSG_IS_REPLIED(*fileinfo->flags))
|
| 1392 | iflags |= IMAP_FLAG_ANSWERED; |
| 1393 | if (!MSG_IS_UNREAD(*fileinfo->flags))
|
| 1394 | iflags |= IMAP_FLAG_SEEN; |
| 1395 | } |
| 1396 | |
| 1397 | if (dest->stype == F_OUTBOX ||
|
| 1398 | dest->stype == F_QUEUE || |
| 1399 | dest->stype == F_DRAFT) |
| 1400 | iflags |= IMAP_FLAG_SEEN; |
| 1401 | |
| 1402 | g_get_current_time(&tv_cur); |
| 1403 | if (tv_cur.tv_sec > tv_prev.tv_sec ||
|
| 1404 | tv_cur.tv_usec - tv_prev.tv_usec > |
| 1405 | PROGRESS_UPDATE_INTERVAL * 1000) {
|
| 1406 | status_print(_("Appending messages to %s (%d / %d)"),
|
| 1407 | dest->path, count, total); |
| 1408 | progress_show(count, total); |
| 1409 | ui_update(); |
| 1410 | tv_prev = tv_cur; |
| 1411 | } |
| 1412 | ++count; |
| 1413 | |
| 1414 | ok = imap_cmd_append(session, destdir, fileinfo->file, iflags, |
| 1415 | &new_uid); |
| 1416 | |
| 1417 | if (ok != IMAP_SUCCESS) {
|
| 1418 | g_warning("can't append message %s\n", fileinfo->file);
|
| 1419 | g_free(destdir); |
| 1420 | progress_show(0, 0); |
| 1421 | return -1; |
| 1422 | } |
| 1423 | |
| 1424 | if (syl_app_get())
|
| 1425 | g_signal_emit_by_name(syl_app_get(), "add-msg", dest, fileinfo->file, new_uid);
|
| 1426 | |
| 1427 | if (!session->uidplus)
|
| 1428 | last_uid++; |
| 1429 | else if (last_uid < new_uid) |
| 1430 | last_uid = new_uid; |
| 1431 | |
| 1432 | dest->last_num = last_uid; |
| 1433 | dest->total++; |
| 1434 | dest->updated = TRUE; |
| 1435 | |
| 1436 | if (fileinfo->flags) {
|
| 1437 | if (MSG_IS_UNREAD(*fileinfo->flags))
|
| 1438 | dest->unread++; |
| 1439 | } else
|
| 1440 | dest->unread++; |
| 1441 | } |
| 1442 | |
| 1443 | progress_show(0, 0); |
| 1444 | g_free(destdir); |
| 1445 | |
| 1446 | if (remove_source) {
|
| 1447 | for (cur = file_list; cur != NULL; cur = cur->next) { |
| 1448 | fileinfo = (MsgFileInfo *)cur->data; |
| 1449 | if (g_unlink(fileinfo->file) < 0) |
| 1450 | FILE_OP_ERROR(fileinfo->file, "unlink");
|
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | return last_uid;
|
| 1455 | } |
| 1456 | |
| 1457 | static gint imap_add_msg_msginfo(Folder *folder, FolderItem *dest,
|
| 1458 | MsgInfo *msginfo, gboolean remove_source) |
| 1459 | {
|
| 1460 | GSList msglist; |
| 1461 | |
| 1462 | g_return_val_if_fail(msginfo != NULL, -1); |
| 1463 | |
| 1464 | msglist.data = msginfo; |
| 1465 | msglist.next = NULL;
|
| 1466 | |
| 1467 | return imap_add_msgs_msginfo(folder, dest, &msglist, remove_source,
|
| 1468 | NULL);
|
| 1469 | } |
| 1470 | |
| 1471 | static gint imap_add_msgs_msginfo(Folder *folder, FolderItem *dest,
|
| 1472 | GSList *msglist, gboolean remove_source, |
| 1473 | gint *first) |
| 1474 | {
|
| 1475 | GSList *file_list; |
| 1476 | gint ret; |
| 1477 | |
| 1478 | file_list = procmsg_get_message_file_list(msglist); |
| 1479 | g_return_val_if_fail(file_list != NULL, -1); |
| 1480 | |
| 1481 | ret = imap_add_msgs(folder, dest, file_list, remove_source, first); |
| 1482 | |
| 1483 | procmsg_message_file_list_free(file_list); |
| 1484 | |
| 1485 | return ret;
|
| 1486 | } |
| 1487 | |
| 1488 | static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest, GSList *msglist,
|
| 1489 | gboolean remove_source) |
| 1490 | {
|
| 1491 | FolderItem *src; |
| 1492 | gchar *destdir; |
| 1493 | GSList *seq_list, *cur; |
| 1494 | MsgInfo *msginfo; |
| 1495 | IMAPSession *session; |
| 1496 | gint count = 0, total;
|
| 1497 | gint ok = IMAP_SUCCESS; |
| 1498 | |
| 1499 | g_return_val_if_fail(folder != NULL, -1); |
| 1500 | g_return_val_if_fail(dest != NULL, -1); |
| 1501 | g_return_val_if_fail(msglist != NULL, -1); |
| 1502 | |
| 1503 | session = imap_session_get(folder); |
| 1504 | if (!session) return -1; |
| 1505 | |
| 1506 | ui_update(); |
| 1507 | |
| 1508 | msginfo = (MsgInfo *)msglist->data; |
| 1509 | |
| 1510 | src = msginfo->folder; |
| 1511 | if (src == dest) {
|
| 1512 | g_warning("the src folder is identical to the dest.\n");
|
| 1513 | return -1; |
| 1514 | } |
| 1515 | |
| 1516 | ok = imap_select(session, IMAP_FOLDER(folder), src->path, |
| 1517 | NULL, NULL, NULL, NULL); |
| 1518 | if (ok != IMAP_SUCCESS)
|
| 1519 | return ok;
|
| 1520 | |
| 1521 | destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path); |
| 1522 | |
| 1523 | total = g_slist_length(msglist); |
| 1524 | seq_list = imap_get_seq_set_from_msglist(msglist, IMAP_COPY_LIMIT); |
| 1525 | |
| 1526 | for (cur = seq_list; cur != NULL; cur = cur->next) { |
| 1527 | gchar *seq_set = (gchar *)cur->data; |
| 1528 | |
| 1529 | count += imap_seq_set_get_count(seq_set); |
| 1530 | |
| 1531 | if (remove_source) {
|
| 1532 | status_print(_("Moving messages %s to %s ..."),
|
| 1533 | seq_set, dest->path); |
| 1534 | debug_print("Moving message %s/[%s] to %s ...\n",
|
| 1535 | src->path, seq_set, dest->path); |
| 1536 | } else {
|
| 1537 | status_print(_("Copying messages %s to %s ..."),
|
| 1538 | seq_set, dest->path); |
| 1539 | debug_print("Copying message %s/[%s] to %s ...\n",
|
| 1540 | src->path, seq_set, dest->path); |
| 1541 | } |
| 1542 | progress_show(count, total); |
| 1543 | ui_update(); |
| 1544 | |
| 1545 | ok = imap_cmd_copy(session, seq_set, destdir); |
| 1546 | if (ok != IMAP_SUCCESS) {
|
| 1547 | imap_seq_set_free(seq_list); |
| 1548 | progress_show(0, 0); |
| 1549 | return -1; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | progress_show(0, 0); |
| 1554 | |
| 1555 | dest->updated = TRUE; |
| 1556 | |
| 1557 | imap_seq_set_free(seq_list); |
| 1558 | g_free(destdir); |
| 1559 | |
| 1560 | for (cur = msglist; cur != NULL; cur = cur->next) { |
| 1561 | msginfo = (MsgInfo *)cur->data; |
| 1562 | |
| 1563 | if (syl_app_get())
|
| 1564 | g_signal_emit_by_name(syl_app_get(), "add-msg", dest, NULL, 0); |
| 1565 | |
| 1566 | dest->total++; |
| 1567 | if (MSG_IS_NEW(msginfo->flags))
|
| 1568 | dest->new++; |
| 1569 | if (MSG_IS_UNREAD(msginfo->flags))
|
| 1570 | dest->unread++; |
| 1571 | } |
| 1572 | |
| 1573 | if (remove_source) {
|
| 1574 | ok = imap_remove_msgs(folder, src, msglist); |
| 1575 | if (ok != IMAP_SUCCESS)
|
| 1576 | return ok;
|
| 1577 | } |
| 1578 | |
| 1579 | if (ok == IMAP_SUCCESS)
|
| 1580 | return 0; |
| 1581 | else
|
| 1582 | return -1; |
| 1583 | } |
| 1584 | |
| 1585 | static gint imap_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
|
| 1586 | {
|
| 1587 | GSList msglist; |
| 1588 | |
| 1589 | g_return_val_if_fail(msginfo != NULL, -1); |
| 1590 | |
| 1591 | msglist.data = msginfo; |
| 1592 | msglist.next = NULL;
|
| 1593 | |
| 1594 | return imap_move_msgs(folder, dest, &msglist);
|
| 1595 | } |
| 1596 | |
| 1597 | static gint imap_move_msgs(Folder *folder, FolderItem *dest, GSList *msglist)
|
| 1598 | {
|
| 1599 | MsgInfo *msginfo; |
| 1600 | GSList *file_list; |
| 1601 | gint ret = 0;
|
| 1602 | |
| 1603 | g_return_val_if_fail(folder != NULL, -1); |
| 1604 | g_return_val_if_fail(dest != NULL, -1); |
| 1605 | g_return_val_if_fail(msglist != NULL, -1); |
| 1606 | |
| 1607 | msginfo = (MsgInfo *)msglist->data; |
| 1608 | g_return_val_if_fail(msginfo->folder != NULL, -1); |
| 1609 | |
| 1610 | if (folder == msginfo->folder->folder)
|
| 1611 | return imap_do_copy_msgs(folder, dest, msglist, TRUE);
|
| 1612 | |
| 1613 | file_list = procmsg_get_message_file_list(msglist); |
| 1614 | g_return_val_if_fail(file_list != NULL, -1); |
| 1615 | |
| 1616 | ret = imap_add_msgs(folder, dest, file_list, FALSE, NULL);
|
| 1617 | |
| 1618 | procmsg_message_file_list_free(file_list); |
| 1619 | |
| 1620 | if (ret != -1) |
| 1621 | ret = folder_item_remove_msgs(msginfo->folder, msglist); |
| 1622 | |
| 1623 | return ret;
|
| 1624 | } |
| 1625 | |
| 1626 | static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
|
| 1627 | {
|
| 1628 | GSList msglist; |
| 1629 | |
| 1630 | g_return_val_if_fail(msginfo != NULL, -1); |
| 1631 | |
| 1632 | msglist.data = msginfo; |
| 1633 | msglist.next = NULL;
|
| 1634 | |
| 1635 | return imap_copy_msgs(folder, dest, &msglist);
|
| 1636 | } |
| 1637 | |
| 1638 | static gint imap_copy_msgs(Folder *folder, FolderItem *dest, GSList *msglist)
|
| 1639 | {
|
| 1640 | MsgInfo *msginfo; |
| 1641 | GSList *file_list; |
| 1642 | gint ret; |
| 1643 | |
| 1644 | g_return_val_if_fail(folder != NULL, -1); |
| 1645 | g_return_val_if_fail(dest != NULL, -1); |
| 1646 | g_return_val_if_fail(msglist != NULL, -1); |
| 1647 | |
| 1648 | msginfo = (MsgInfo *)msglist->data; |
| 1649 | g_return_val_if_fail(msginfo->folder != NULL, -1); |
| 1650 | |
| 1651 | if (folder == msginfo->folder->folder)
|
| 1652 | return imap_do_copy_msgs(folder, dest, msglist, FALSE);
|
| 1653 | |
| 1654 | file_list = procmsg_get_message_file_list(msglist); |
| 1655 | g_return_val_if_fail(file_list != NULL, -1); |
| 1656 | |
| 1657 | ret = imap_add_msgs(folder, dest, file_list, FALSE, NULL);
|
| 1658 | |
| 1659 | procmsg_message_file_list_free(file_list); |
| 1660 | |
| 1661 | return ret;
|
| 1662 | } |
| 1663 | |
| 1664 | static gint imap_remove_msgs_by_seq_set(Folder *folder, FolderItem *item,
|
| 1665 | GSList *seq_list) |
| 1666 | {
|
| 1667 | gint ok; |
| 1668 | IMAPSession *session; |
| 1669 | GSList *cur; |
| 1670 | |
| 1671 | g_return_val_if_fail(seq_list != NULL, -1); |
| 1672 | |
| 1673 | session = imap_session_get(folder); |
| 1674 | if (!session) return -1; |
| 1675 | |
| 1676 | for (cur = seq_list; cur != NULL; cur = cur->next) { |
| 1677 | gchar *seq_set = (gchar *)cur->data; |
| 1678 | |
| 1679 | status_print(_("Removing messages %s"), seq_set);
|
| 1680 | ui_update(); |
| 1681 | |
| 1682 | ok = imap_set_message_flags(session, seq_set, IMAP_FLAG_DELETED, |
| 1683 | TRUE); |
| 1684 | if (ok != IMAP_SUCCESS) {
|
| 1685 | log_warning(_("can't set deleted flags: %s\n"),
|
| 1686 | seq_set); |
| 1687 | return ok;
|
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | ok = imap_cmd_expunge(session); |
| 1692 | if (ok != IMAP_SUCCESS) {
|
| 1693 | log_warning(_("can't expunge\n"));
|
| 1694 | } else {
|
| 1695 | /* for some broken IMAP servers */
|
| 1696 | ok = imap_cmd_noop(session); |
| 1697 | } |
| 1698 | |
| 1699 | item->updated = TRUE; |
| 1700 | |
| 1701 | return ok;
|
| 1702 | } |
| 1703 | |
| 1704 | static gint imap_remove_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
|
| 1705 | {
|
| 1706 | GSList msglist; |
| 1707 | |
| 1708 | g_return_val_if_fail(msginfo != NULL, -1); |
| 1709 | |
| 1710 | msglist.data = msginfo; |
| 1711 | msglist.next = NULL;
|
| 1712 | |
| 1713 | return imap_remove_msgs(folder, item, &msglist);
|
| 1714 | } |
| 1715 | |
| 1716 | static gint imap_remove_msgs(Folder *folder, FolderItem *item, GSList *msglist)
|
| 1717 | {
|
| 1718 | gint ok; |
| 1719 | IMAPSession *session; |
| 1720 | GSList *seq_list, *cur; |
| 1721 | gchar *dir; |
| 1722 | gboolean dir_exist; |
| 1723 | |
| 1724 | g_return_val_if_fail(folder != NULL, -1); |
| 1725 | g_return_val_if_fail(FOLDER_TYPE(folder) == F_IMAP, -1);
|
| 1726 | g_return_val_if_fail(item != NULL, -1); |
| 1727 | g_return_val_if_fail(msglist != NULL, -1); |
| 1728 | |
| 1729 | session = imap_session_get(folder); |
| 1730 | if (!session) return -1; |
| 1731 | |
| 1732 | ok = imap_select(session, IMAP_FOLDER(folder), item->path, |
| 1733 | NULL, NULL, NULL, NULL); |
| 1734 | if (ok != IMAP_SUCCESS)
|
| 1735 | return ok;
|
| 1736 | |
| 1737 | seq_list = imap_get_seq_set_from_msglist(msglist, 0);
|
| 1738 | ok = imap_remove_msgs_by_seq_set(folder, item, seq_list); |
| 1739 | imap_seq_set_free(seq_list); |
| 1740 | if (ok != IMAP_SUCCESS)
|
| 1741 | return ok;
|
| 1742 | |
| 1743 | dir = folder_item_get_path(item); |
| 1744 | dir_exist = is_dir_exist(dir); |
| 1745 | for (cur = msglist; cur != NULL; cur = cur->next) { |
| 1746 | MsgInfo *msginfo = (MsgInfo *)cur->data; |
| 1747 | guint32 uid = msginfo->msgnum; |
| 1748 | |
| 1749 | if (syl_app_get())
|
| 1750 | g_signal_emit_by_name(syl_app_get(), "remove-msg", item, NULL, uid); |
| 1751 | |
| 1752 | if (dir_exist)
|
| 1753 | remove_numbered_files(dir, uid, uid); |
| 1754 | item->total--; |
| 1755 | if (MSG_IS_NEW(msginfo->flags))
|
| 1756 | item->new--; |
| 1757 | if (MSG_IS_UNREAD(msginfo->flags))
|
| 1758 | item->unread--; |
| 1759 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_INVALID); |
| 1760 | } |
| 1761 | g_free(dir); |
| 1762 | |
| 1763 | return IMAP_SUCCESS;
|
| 1764 | } |
| 1765 | |
| 1766 | static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
|
| 1767 | {
|
| 1768 | gint ok; |
| 1769 | IMAPSession *session; |
| 1770 | gchar *dir; |
| 1771 | |
| 1772 | g_return_val_if_fail(folder != NULL, -1); |
| 1773 | g_return_val_if_fail(item != NULL, -1); |
| 1774 | |
| 1775 | session = imap_session_get(folder); |
| 1776 | if (!session) return -1; |
| 1777 | |
| 1778 | ok = imap_select(session, IMAP_FOLDER(folder), item->path, |
| 1779 | NULL, NULL, NULL, NULL); |
| 1780 | if (ok != IMAP_SUCCESS)
|
| 1781 | return ok;
|
| 1782 | |
| 1783 | status_print(_("Removing all messages in %s"), item->path);
|
| 1784 | ui_update(); |
| 1785 | |
| 1786 | ok = imap_cmd_gen_send(session, "STORE 1:* +FLAGS.SILENT (\\Deleted)");
|
| 1787 | if (ok != IMAP_SUCCESS) {
|
| 1788 | log_warning(_("can't set deleted flags: 1:*\n"));
|
| 1789 | return ok;
|
| 1790 | } |
| 1791 | ok = imap_cmd_ok(session, NULL);
|
| 1792 | if (ok != IMAP_SUCCESS) {
|
| 1793 | log_warning(_("can't set deleted flags: 1:*\n"));
|
| 1794 | return ok;
|
| 1795 | } |
| 1796 | |
| 1797 | ok = imap_cmd_expunge(session); |
| 1798 | if (ok != IMAP_SUCCESS) {
|
| 1799 | log_warning(_("can't expunge\n"));
|
| 1800 | return ok;
|
| 1801 | } |
| 1802 | |
| 1803 | if (syl_app_get())
|
| 1804 | g_signal_emit_by_name(syl_app_get(), "remove-all-msg", item);
|
| 1805 | |
| 1806 | item->new = item->unread = item->total = 0;
|
| 1807 | item->updated = TRUE; |
| 1808 | |
| 1809 | dir = folder_item_get_path(item); |
| 1810 | if (is_dir_exist(dir))
|
| 1811 | remove_all_numbered_files(dir); |
| 1812 | g_free(dir); |
| 1813 | |
| 1814 | return IMAP_SUCCESS;
|
| 1815 | } |
| 1816 | |
| 1817 | static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
|
| 1818 | MsgInfo *msginfo) |
| 1819 | {
|
| 1820 | /* TODO: properly implement this method */
|
| 1821 | return FALSE;
|
| 1822 | } |
| 1823 | |
| 1824 | static gint imap_close(Folder *folder, FolderItem *item)
|
| 1825 | {
|
| 1826 | gint ok; |
| 1827 | IMAPSession *session; |
| 1828 | |
| 1829 | g_return_val_if_fail(folder != NULL, -1); |
| 1830 | |
| 1831 | if (!item->path) return 0; |
| 1832 | |
| 1833 | if (!REMOTE_FOLDER(folder)->session)
|
| 1834 | return 0; |
| 1835 | |
| 1836 | session = imap_session_get(folder); |
| 1837 | if (!session) return -1; |
| 1838 | |
| 1839 | if (session->mbox) {
|
| 1840 | if (strcmp2(session->mbox, item->path) != 0) return -1; |
| 1841 | |
| 1842 | ok = imap_cmd_close(session); |
| 1843 | if (ok != IMAP_SUCCESS)
|
| 1844 | log_warning(_("can't close folder\n"));
|
| 1845 | |
| 1846 | g_free(session->mbox); |
| 1847 | session->mbox = NULL;
|
| 1848 | |
| 1849 | return ok;
|
| 1850 | } else
|
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | static gint imap_scan_folder(Folder *folder, FolderItem *item)
|
| 1855 | {
|
| 1856 | IMAPSession *session; |
| 1857 | gint messages, recent, unseen; |
| 1858 | guint32 uid_next, uid_validity; |
| 1859 | gint ok; |
| 1860 | |
| 1861 | g_return_val_if_fail(folder != NULL, -1); |
| 1862 | g_return_val_if_fail(item != NULL, -1); |
| 1863 | |
| 1864 | session = imap_session_get(folder); |
| 1865 | if (!session) return -1; |
| 1866 | |
| 1867 | ok = imap_status(session, IMAP_FOLDER(folder), item->path, |
| 1868 | &messages, &recent, &uid_next, &uid_validity, &unseen); |
| 1869 | if (ok != IMAP_SUCCESS) return -1; |
| 1870 | |
| 1871 | item->new = unseen > 0 ? recent : 0; |
| 1872 | item->unread = unseen; |
| 1873 | item->total = messages; |
| 1874 | item->last_num = (messages > 0 && uid_next > 0) ? uid_next - 1 : 0; |
| 1875 | /* item->mtime = uid_validity; */
|
| 1876 | item->updated = TRUE; |
| 1877 | |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
| 1881 | static gint imap_scan_tree(Folder *folder)
|
| 1882 | {
|
| 1883 | FolderItem *item = NULL;
|
| 1884 | IMAPSession *session; |
| 1885 | gchar *root_folder = NULL;
|
| 1886 | GSList *item_list, *cur; |
| 1887 | |
| 1888 | g_return_val_if_fail(folder != NULL, -1); |
| 1889 | g_return_val_if_fail(folder->account != NULL, -1); |
| 1890 | |
| 1891 | session = imap_session_get(folder); |
| 1892 | if (!session) {
|
| 1893 | if (!folder->node) {
|
| 1894 | folder_tree_destroy(folder); |
| 1895 | item = folder_item_new(folder->name, NULL);
|
| 1896 | item->folder = folder; |
| 1897 | folder->node = item->node = g_node_new(item); |
| 1898 | } |
| 1899 | return -1; |
| 1900 | } |
| 1901 | |
| 1902 | if (folder->account->imap_dir && *folder->account->imap_dir) {
|
| 1903 | gchar *real_path; |
| 1904 | GPtrArray *argbuf; |
| 1905 | gint ok; |
| 1906 | |
| 1907 | Xstrdup_a(root_folder, folder->account->imap_dir, return -1); |
| 1908 | extract_quote(root_folder, '"');
|
| 1909 | subst_char(root_folder, |
| 1910 | imap_get_path_separator(IMAP_FOLDER(folder), |
| 1911 | root_folder), |
| 1912 | '/');
|
| 1913 | strtailchomp(root_folder, '/');
|
| 1914 | real_path = imap_get_real_path |
| 1915 | (IMAP_FOLDER(folder), root_folder); |
| 1916 | debug_print("IMAP root directory: %s\n", real_path);
|
| 1917 | |
| 1918 | /* check if root directory exist */
|
| 1919 | argbuf = g_ptr_array_new(); |
| 1920 | ok = imap_cmd_list(session, NULL, real_path, argbuf);
|
| 1921 | if (ok != IMAP_SUCCESS ||
|
| 1922 | search_array_str(argbuf, "LIST ") == NULL) { |
| 1923 | log_warning(_("root folder %s not exist\n"), real_path);
|
| 1924 | g_ptr_array_free(argbuf, TRUE); |
| 1925 | g_free(real_path); |
| 1926 | return -1; |
| 1927 | } |
| 1928 | g_ptr_array_free(argbuf, TRUE); |
| 1929 | g_free(real_path); |
| 1930 | } |
| 1931 | |
| 1932 | if (folder->node)
|
| 1933 | item = FOLDER_ITEM(folder->node->data); |
| 1934 | if (!item || ((item->path || root_folder) &&
|
| 1935 | strcmp2(item->path, root_folder) != 0)) {
|
| 1936 | folder_tree_destroy(folder); |
| 1937 | item = folder_item_new(folder->name, root_folder); |
| 1938 | item->folder = folder; |
| 1939 | folder->node = item->node = g_node_new(item); |
| 1940 | } |
| 1941 | |
| 1942 | item_list = imap_get_folder_list(session, item); |
| 1943 | imap_scan_tree_recursive(session, item, item_list); |
| 1944 | imap_create_missing_folders(folder); |
| 1945 | |
| 1946 | for (cur = item_list; cur != NULL; cur = cur->next) |
| 1947 | folder_item_destroy(FOLDER_ITEM(cur->data)); |
| 1948 | g_slist_free(item_list); |
| 1949 | |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
| 1953 | static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item,
|
| 1954 | GSList *item_list) |
| 1955 | {
|
| 1956 | Folder *folder; |
| 1957 | FolderItem *new_item; |
| 1958 | GSList *part_list, *cur; |
| 1959 | GNode *node; |
| 1960 | |
| 1961 | g_return_val_if_fail(item != NULL, -1); |
| 1962 | g_return_val_if_fail(item->folder != NULL, -1); |
| 1963 | g_return_val_if_fail(item->no_sub == FALSE, -1);
|
| 1964 | |
| 1965 | folder = item->folder; |
| 1966 | |
| 1967 | part_list = imap_get_part_folder_list(item_list, item); |
| 1968 | |
| 1969 | node = item->node->children; |
| 1970 | while (node != NULL) { |
| 1971 | FolderItem *old_item = FOLDER_ITEM(node->data); |
| 1972 | GNode *next = node->next; |
| 1973 | |
| 1974 | new_item = NULL;
|
| 1975 | |
| 1976 | for (cur = part_list; cur != NULL; cur = cur->next) { |
| 1977 | FolderItem *cur_item = FOLDER_ITEM(cur->data); |
| 1978 | if (!strcmp2(old_item->path, cur_item->path)) {
|
| 1979 | new_item = cur_item; |
| 1980 | break;
|
| 1981 | } |
| 1982 | } |
| 1983 | if (!new_item) {
|
| 1984 | if (old_item->stype != F_VIRTUAL) {
|
| 1985 | debug_print("folder '%s' not found. removing...\n", old_item->path);
|
| 1986 | folder_item_remove(old_item); |
| 1987 | } |
| 1988 | } else if (old_item->stype == F_VIRTUAL) { |
| 1989 | debug_print("IMAP4 folder found at the location of virtual folder '%s'. removing virtual folder...\n", old_item->path);
|
| 1990 | virtual_get_class()->remove_folder |
| 1991 | (folder, old_item); |
| 1992 | } else {
|
| 1993 | old_item->no_sub = new_item->no_sub; |
| 1994 | old_item->no_select = new_item->no_select; |
| 1995 | if (old_item->no_select == TRUE)
|
| 1996 | old_item->new = old_item->unread = |
| 1997 | old_item->total = 0;
|
| 1998 | if (old_item->no_sub == TRUE && node->children) {
|
| 1999 | debug_print("folder '%s' doesn't have "
|
| 2000 | "subfolders. removing...\n",
|
| 2001 | old_item->path); |
| 2002 | folder_item_remove_children(old_item); |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | node = next; |
| 2007 | } |
| 2008 | |
| 2009 | for (cur = part_list; cur != NULL; cur = cur->next) { |
| 2010 | FolderItem *cur_item = FOLDER_ITEM(cur->data); |
| 2011 | new_item = NULL;
|
| 2012 | for (node = item->node->children; node != NULL; |
| 2013 | node = node->next) {
|
| 2014 | if (!strcmp2(FOLDER_ITEM(node->data)->path,
|
| 2015 | cur_item->path)) {
|
| 2016 | new_item = FOLDER_ITEM(node->data); |
| 2017 | cur_item = NULL;
|
| 2018 | break;
|
| 2019 | } |
| 2020 | } |
| 2021 | if (!new_item) {
|
| 2022 | new_item = folder_item_copy(cur_item); |
| 2023 | debug_print("new folder '%s' found.\n", new_item->path);
|
| 2024 | folder_item_append(item, new_item); |
| 2025 | } |
| 2026 | |
| 2027 | if (!g_ascii_strcasecmp(new_item->path, "INBOX")) { |
| 2028 | new_item->stype = F_INBOX; |
| 2029 | folder->inbox = new_item; |
| 2030 | } else if (!item->parent || item->stype == F_INBOX) { |
| 2031 | const gchar *base;
|
| 2032 | |
| 2033 | base = g_basename(new_item->path); |
| 2034 | |
| 2035 | if (!folder->outbox &&
|
| 2036 | !g_ascii_strcasecmp(base, "Sent")) {
|
| 2037 | new_item->stype = F_OUTBOX; |
| 2038 | folder->outbox = new_item; |
| 2039 | } else if (!folder->draft && |
| 2040 | !g_ascii_strcasecmp(base, "Drafts")) {
|
| 2041 | new_item->stype = F_DRAFT; |
| 2042 | folder->draft = new_item; |
| 2043 | } else if (!folder->queue && |
| 2044 | !g_ascii_strcasecmp(base, "Queue")) {
|
| 2045 | new_item->stype = F_QUEUE; |
| 2046 | folder->queue = new_item; |
| 2047 | } else if (!folder->trash && |
| 2048 | !g_ascii_strcasecmp(base, "Trash")) {
|
| 2049 | new_item->stype = F_TRASH; |
| 2050 | folder->trash = new_item; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | #if 0
|
| 2055 | if (new_item->no_select == FALSE) |
| 2056 | imap_scan_folder(folder, new_item); |
| 2057 | #endif |
| 2058 | if (new_item->no_sub == FALSE)
|
| 2059 | imap_scan_tree_recursive(session, new_item, item_list); |
| 2060 | } |
| 2061 | |
| 2062 | g_slist_free(part_list); |
| 2063 | |
| 2064 | return IMAP_SUCCESS;
|
| 2065 | } |
| 2066 | |
| 2067 | static GSList *imap_get_folder_list(IMAPSession *session, FolderItem *item)
|
| 2068 | {
|
| 2069 | Folder *folder; |
| 2070 | IMAPFolder *imapfolder; |
| 2071 | gchar *real_path; |
| 2072 | gchar *wildcard_path; |
| 2073 | gchar separator; |
| 2074 | GSList *item_list = NULL;
|
| 2075 | |
| 2076 | folder = item->folder; |
| 2077 | imapfolder = IMAP_FOLDER(folder); |
| 2078 | |
| 2079 | separator = imap_get_path_separator(imapfolder, item->path); |
| 2080 | |
| 2081 | if (folder->ui_func)
|
| 2082 | folder->ui_func(folder, item, folder->ui_func_data); |
| 2083 | |
| 2084 | if (item->path) {
|
| 2085 | real_path = imap_get_real_path(imapfolder, item->path); |
| 2086 | strtailchomp(real_path, separator); |
| 2087 | wildcard_path = g_strdup_printf("%s%c*", real_path, separator);
|
| 2088 | } else {
|
| 2089 | real_path = g_strdup("");
|
| 2090 | wildcard_path = g_strdup("*");
|
| 2091 | } |
| 2092 | |
| 2093 | if (imap_cmd_gen_send(session, "LIST \"\" \"%s\"", wildcard_path) == IMAP_SUCCESS) { |
| 2094 | item_list = imap_parse_list(session, real_path, NULL);
|
| 2095 | item_list = imap_add_inter_folders(item_list, item->path); |
| 2096 | } |
| 2097 | g_free(real_path); |
| 2098 | g_free(wildcard_path); |
| 2099 | |
| 2100 | return item_list;
|
| 2101 | } |
| 2102 | |
| 2103 | static GSList *imap_parse_list(IMAPSession *session, const gchar *real_path, |
| 2104 | gchar *separator) |
| 2105 | {
|
| 2106 | gchar buf[IMAPBUFSIZE]; |
| 2107 | gchar flags[256];
|
| 2108 | gchar separator_str[16];
|
| 2109 | gchar *p; |
| 2110 | const gchar *name;
|
| 2111 | gchar *loc_name, *loc_path; |
| 2112 | GSList *item_list = NULL;
|
| 2113 | GString *str; |
| 2114 | FolderItem *new_item; |
| 2115 | |
| 2116 | debug_print("getting list of %s ...\n",
|
| 2117 | *real_path ? real_path : "\"\"");
|
| 2118 | |
| 2119 | str = g_string_new(NULL);
|
| 2120 | |
| 2121 | for (;;) {
|
| 2122 | if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) <= 0) { |
| 2123 | log_warning(_("error occurred while getting LIST.\n"));
|
| 2124 | break;
|
| 2125 | } |
| 2126 | strretchomp(buf); |
| 2127 | if (buf[0] != '*' || buf[1] != ' ') { |
| 2128 | log_print("IMAP4< %s\n", buf);
|
| 2129 | if (sscanf(buf, "%*d %16s", buf) < 1 || |
| 2130 | strcmp(buf, "OK") != 0) |
| 2131 | log_warning(_("error occurred while getting LIST.\n"));
|
| 2132 | |
| 2133 | break;
|
| 2134 | } |
| 2135 | debug_print("IMAP4< %s\n", buf);
|
| 2136 | |
| 2137 | g_string_assign(str, buf); |
| 2138 | p = str->str + 2;
|
| 2139 | if (strncmp(p, "LIST ", 5) != 0) continue; |
| 2140 | p += 5;
|
| 2141 | |
| 2142 | if (*p != '(') continue; |
| 2143 | p++; |
| 2144 | p = strchr_cpy(p, ')', flags, sizeof(flags)); |
| 2145 | if (!p) continue; |
| 2146 | while (*p == ' ') p++; |
| 2147 | |
| 2148 | p = strchr_cpy(p, ' ', separator_str, sizeof(separator_str)); |
| 2149 | if (!p) continue; |
| 2150 | extract_quote(separator_str, '"');
|
| 2151 | if (!strcmp(separator_str, "NIL")) |
| 2152 | separator_str[0] = '\0'; |
| 2153 | if (separator)
|
| 2154 | *separator = separator_str[0];
|
| 2155 | |
| 2156 | buf[0] = '\0'; |
| 2157 | while (*p == ' ') p++; |
| 2158 | if ((*p == '~' && *(p + 1) == '{') || *p == '{' || *p == '"') |
| 2159 | p = imap_parse_atom(session, p, buf, sizeof(buf), str);
|
| 2160 | else
|
| 2161 | strncpy2(buf, p, sizeof(buf));
|
| 2162 | strtailchomp(buf, separator_str[0]);
|
| 2163 | if (buf[0] == '\0') continue; |
| 2164 | if (!strcmp(buf, real_path)) continue; |
| 2165 | |
| 2166 | if (separator_str[0] != '\0') |
| 2167 | subst_char(buf, separator_str[0], '/'); |
| 2168 | name = g_basename(buf); |
| 2169 | if (name[0] == '.') continue; |
| 2170 | |
| 2171 | loc_name = imap_modified_utf7_to_utf8(name); |
| 2172 | loc_path = imap_modified_utf7_to_utf8(buf); |
| 2173 | new_item = folder_item_new(loc_name, loc_path); |
| 2174 | if (strcasestr(flags, "\\Noinferiors") != NULL) |
| 2175 | new_item->no_sub = TRUE; |
| 2176 | if (g_ascii_strcasecmp(buf, "INBOX") != 0 && |
| 2177 | strcasestr(flags, "\\Noselect") != NULL) |
| 2178 | new_item->no_select = TRUE; |
| 2179 | |
| 2180 | item_list = g_slist_prepend(item_list, new_item); |
| 2181 | |
| 2182 | debug_print("folder '%s' found.\n", loc_path);
|
| 2183 | g_free(loc_path); |
| 2184 | g_free(loc_name); |
| 2185 | } |
| 2186 | |
| 2187 | g_string_free(str, TRUE); |
| 2188 | |
| 2189 | item_list = g_slist_reverse(item_list); |
| 2190 | return item_list;
|
| 2191 | } |
| 2192 | |
| 2193 | static GSList *imap_add_inter_folders(GSList *item_list, const gchar *root_path) |
| 2194 | {
|
| 2195 | FolderItem *item; |
| 2196 | GSList *cur; |
| 2197 | GSList *add_list = NULL;
|
| 2198 | GHashTable *exist; |
| 2199 | const gchar *p;
|
| 2200 | gint root_path_len = 0;
|
| 2201 | |
| 2202 | if (root_path)
|
| 2203 | root_path_len = strlen(root_path); |
| 2204 | |
| 2205 | exist = g_hash_table_new(g_str_hash, g_str_equal); |
| 2206 | |
| 2207 | for (cur = item_list; cur != NULL; cur = cur->next) { |
| 2208 | item = FOLDER_ITEM(cur->data); |
| 2209 | |
| 2210 | if (root_path_len > 0 && |
| 2211 | strncmp(root_path, item->path, root_path_len) != 0)
|
| 2212 | continue;
|
| 2213 | p = item->path + root_path_len; |
| 2214 | if (root_path_len > 0 && *p != '/') continue; |
| 2215 | while (*p == '/') p++; |
| 2216 | if (*p == '\0') continue; |
| 2217 | g_hash_table_insert(exist, (gpointer)p, GINT_TO_POINTER(1));
|
| 2218 | } |
| 2219 | |
| 2220 | for (cur = item_list; cur != NULL; cur = cur->next) { |
| 2221 | const gchar *q, *r;
|
| 2222 | gchar *parent, *full_parent; |
| 2223 | FolderItem *new_item; |
| 2224 | |
| 2225 | item = FOLDER_ITEM(cur->data); |
| 2226 | |
| 2227 | if (root_path_len > 0 && |
| 2228 | strncmp(root_path, item->path, root_path_len) != 0)
|
| 2229 | continue;
|
| 2230 | p = item->path + root_path_len; |
| 2231 | if (root_path_len > 0 && *p != '/') continue; |
| 2232 | while (*p == '/') p++; |
| 2233 | if (*p == '\0') continue; |
| 2234 | |
| 2235 | q = p; |
| 2236 | while ((q = strchr(q, '/')) != NULL) { |
| 2237 | parent = g_strndup(p, q - p); |
| 2238 | if (!g_hash_table_lookup(exist, parent)) {
|
| 2239 | if (root_path_len > 0) |
| 2240 | full_parent = g_strconcat |
| 2241 | (root_path, "/", parent, NULL); |
| 2242 | else
|
| 2243 | full_parent = g_strdup(parent); |
| 2244 | new_item = folder_item_new(g_basename(parent), |
| 2245 | full_parent); |
| 2246 | new_item->no_select = TRUE; |
| 2247 | add_list = g_slist_prepend(add_list, new_item); |
| 2248 | r = new_item->path + root_path_len; |
| 2249 | while (*r == '/') r++; |
| 2250 | g_hash_table_insert(exist, (gpointer)r, |
| 2251 | GINT_TO_POINTER(1));
|
| 2252 | debug_print("intermediate folder '%s' added\n",
|
| 2253 | full_parent); |
| 2254 | g_free(full_parent); |
| 2255 | } |
| 2256 | g_free(parent); |
| 2257 | while (*q == '/') q++; |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | g_hash_table_destroy(exist); |
| 2262 | |
| 2263 | add_list = g_slist_reverse(add_list); |
| 2264 | item_list = g_slist_concat(item_list, add_list); |
| 2265 | |
| 2266 | return item_list;
|
| 2267 | } |
| 2268 | |
| 2269 | static GSList *imap_get_part_folder_list(GSList *item_list, FolderItem *item)
|
| 2270 | {
|
| 2271 | FolderItem *cur_item; |
| 2272 | GSList *part_list = NULL, *cur;
|
| 2273 | gint len; |
| 2274 | |
| 2275 | if (!item->path) {
|
| 2276 | debug_print("imap_get_part_folder_list(): get root folders\n");
|
| 2277 | for (cur = item_list; cur != NULL; cur = cur->next) { |
| 2278 | cur_item = FOLDER_ITEM(cur->data); |
| 2279 | |
| 2280 | if (!strchr(cur_item->path, '/')) { |
| 2281 | part_list = g_slist_prepend(part_list, |
| 2282 | cur_item); |
| 2283 | debug_print("append '%s'\n", cur_item->path);
|
| 2284 | } |
| 2285 | } |
| 2286 | part_list = g_slist_reverse(part_list); |
| 2287 | return part_list;
|
| 2288 | } |
| 2289 | |
| 2290 | len = strlen(item->path); |
| 2291 | debug_print("imap_get_part_folder_list(): get folders under '%s'\n",
|
| 2292 | item->path); |
| 2293 | |
| 2294 | for (cur = item_list; cur != NULL; cur = cur->next) { |
| 2295 | cur_item = FOLDER_ITEM(cur->data); |
| 2296 | |
| 2297 | if (!strncmp(cur_item->path, item->path, len) &&
|
| 2298 | cur_item->path[len] == '/' &&
|
| 2299 | !strchr(cur_item->path + len + 1, '/')) { |
| 2300 | part_list = g_slist_prepend(part_list, cur_item); |
| 2301 | debug_print("append '%s'\n", cur_item->path);
|
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | part_list = g_slist_reverse(part_list); |
| 2306 | return part_list;
|
| 2307 | } |
| 2308 | |
| 2309 | static gint imap_create_tree(Folder *folder)
|
| 2310 | {
|
| 2311 | g_return_val_if_fail(folder != NULL, -1); |
| 2312 | g_return_val_if_fail(folder->node != NULL, -1); |
| 2313 | g_return_val_if_fail(folder->node->data != NULL, -1); |
| 2314 | g_return_val_if_fail(folder->account != NULL, -1); |
| 2315 | |
| 2316 | if (imap_scan_tree(folder) < 0) |
| 2317 | return -1; |
| 2318 | imap_create_missing_folders(folder); |
| 2319 | |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
| 2323 | static void imap_create_missing_folders(Folder *folder) |
| 2324 | {
|
| 2325 | g_return_if_fail(folder != NULL);
|
| 2326 | |
| 2327 | if (!folder->inbox)
|
| 2328 | folder->inbox = imap_create_special_folder |
| 2329 | (folder, F_INBOX, "INBOX");
|
| 2330 | #if 0
|
| 2331 | if (!folder->outbox) |
| 2332 | folder->outbox = imap_create_special_folder |
| 2333 | (folder, F_OUTBOX, "Sent"); |
| 2334 | if (!folder->draft) |
| 2335 | folder->draft = imap_create_special_folder |
| 2336 | (folder, F_DRAFT, "Drafts"); |
| 2337 | if (!folder->queue) |
| 2338 | folder->queue = imap_create_special_folder |
| 2339 | (folder, F_QUEUE, "Queue"); |
| 2340 | #endif |
| 2341 | if (!folder->trash)
|
| 2342 | folder->trash = imap_create_special_folder |
| 2343 | (folder, F_TRASH, "Trash");
|
| 2344 | } |
| 2345 | |
| 2346 | static FolderItem *imap_create_special_folder(Folder *folder,
|
| 2347 | SpecialFolderItemType stype, |
| 2348 | const gchar *name)
|
| 2349 | {
|
| 2350 | FolderItem *item; |
| 2351 | FolderItem *new_item; |
| 2352 | |
| 2353 | g_return_val_if_fail(folder != NULL, NULL); |
| 2354 | g_return_val_if_fail(folder->node != NULL, NULL); |
| 2355 | g_return_val_if_fail(folder->node->data != NULL, NULL); |
| 2356 | g_return_val_if_fail(folder->account != NULL, NULL); |
| 2357 | g_return_val_if_fail(name != NULL, NULL); |
| 2358 | |
| 2359 | item = FOLDER_ITEM(folder->node->data); |
| 2360 | new_item = imap_create_folder(folder, item, name); |
| 2361 | |
| 2362 | if (!new_item) {
|
| 2363 | g_warning(_("Can't create '%s'\n"), name);
|
| 2364 | if (!folder->inbox) return NULL; |
| 2365 | |
| 2366 | new_item = imap_create_folder(folder, folder->inbox, name); |
| 2367 | if (!new_item)
|
| 2368 | g_warning(_("Can't create '%s' under INBOX\n"), name);
|
| 2369 | else
|
| 2370 | new_item->stype = stype; |
| 2371 | } else
|
| 2372 | new_item->stype = stype; |
| 2373 | |
| 2374 | return new_item;
|
| 2375 | } |
| 2376 | |
| 2377 | static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
|
| 2378 | const gchar *name)
|
| 2379 | {
|
| 2380 | gchar *dirpath, *imap_path; |
| 2381 | IMAPSession *session; |
| 2382 | FolderItem *new_item; |
| 2383 | gchar separator; |
| 2384 | gchar *new_name; |
| 2385 | const gchar *p;
|
| 2386 | gboolean no_sub = FALSE, no_select = FALSE; |
| 2387 | gint ok; |
| 2388 | |
| 2389 | g_return_val_if_fail(folder != NULL, NULL); |
| 2390 | g_return_val_if_fail(folder->account != NULL, NULL); |
| 2391 | g_return_val_if_fail(parent != NULL, NULL); |
| 2392 | g_return_val_if_fail(name != NULL, NULL); |
| 2393 | |
| 2394 | session = imap_session_get(folder); |
| 2395 | if (!session) return NULL; |
| 2396 | |
| 2397 | if (!parent->parent && g_ascii_strcasecmp(name, "INBOX") == 0) |
| 2398 | dirpath = g_strdup(name); |
| 2399 | else if (parent->path) |
| 2400 | dirpath = g_strconcat(parent->path, "/", name, NULL); |
| 2401 | else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0') |
| 2402 | dirpath = g_strdup(name); |
| 2403 | else if (folder->account->imap_dir && *folder->account->imap_dir) { |
| 2404 | gchar *imap_dir; |
| 2405 | |
| 2406 | Xstrdup_a(imap_dir, folder->account->imap_dir, return NULL); |
| 2407 | strtailchomp(imap_dir, '/');
|
| 2408 | dirpath = g_strconcat(imap_dir, "/", name, NULL); |
| 2409 | } else
|
| 2410 | dirpath = g_strdup(name); |
| 2411 | |
| 2412 | /* keep trailing directory separator to create a folder that contains
|
| 2413 | sub folder */ |
| 2414 | imap_path = imap_utf8_to_modified_utf7(dirpath); |
| 2415 | strtailchomp(dirpath, '/');
|
| 2416 | Xstrdup_a(new_name, name, {g_free(dirpath); return NULL;});
|
| 2417 | strtailchomp(new_name, '/');
|
| 2418 | separator = imap_get_path_separator(IMAP_FOLDER(folder), imap_path); |
| 2419 | imap_path_separator_subst(imap_path, separator); |
| 2420 | subst_char(new_name, '/', separator);
|
| 2421 | |
| 2422 | if (g_ascii_strcasecmp(name, "INBOX") != 0) { |
| 2423 | GPtrArray *argbuf; |
| 2424 | gint i; |
| 2425 | gboolean exist = FALSE; |
| 2426 | |
| 2427 | argbuf = g_ptr_array_new(); |
| 2428 | ok = imap_cmd_list(session, NULL, imap_path, argbuf);
|
| 2429 | if (ok != IMAP_SUCCESS) {
|
| 2430 | log_warning(_("can't create mailbox: LIST failed\n"));
|
| 2431 | g_free(imap_path); |
| 2432 | g_free(dirpath); |
| 2433 | g_ptr_array_free(argbuf, TRUE); |
| 2434 | return NULL; |
| 2435 | } |
| 2436 | |
| 2437 | for (i = 0; i < argbuf->len; i++) { |
| 2438 | gchar *str; |
| 2439 | str = g_ptr_array_index(argbuf, i); |
| 2440 | if (!strncmp(str, "LIST ", 5)) { |
| 2441 | exist = TRUE; |
| 2442 | if (strcasestr(str + 5, "\\Noinferiors")) |
| 2443 | no_sub = TRUE; |
| 2444 | if (strcasestr(str + 5, "\\Noselect")) |
| 2445 | no_select = TRUE; |
| 2446 | break;
|
| 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | if (!exist) {
|
| 2451 | ok = imap_cmd_create(session, imap_path); |
| 2452 | if (ok != IMAP_SUCCESS) {
|
| 2453 | log_warning(_("can't create mailbox\n"));
|
| 2454 | g_free(imap_path); |
| 2455 | g_free(dirpath); |
| 2456 | return NULL; |
| 2457 | } |
| 2458 | |
| 2459 | g_ptr_array_free(argbuf, TRUE); |
| 2460 | argbuf = g_ptr_array_new(); |
| 2461 | ok = imap_cmd_list(session, NULL, imap_path, argbuf);
|
| 2462 | if (ok != IMAP_SUCCESS) {
|
| 2463 | log_warning("LIST failed\n");
|
| 2464 | g_free(imap_path); |
| 2465 | g_free(dirpath); |
| 2466 | g_ptr_array_free(argbuf, TRUE); |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | |
| 2470 | for (i = 0; i < argbuf->len; i++) { |
| 2471 | gchar *str; |
| 2472 | str = g_ptr_array_index(argbuf, i); |
| 2473 | if (!strncmp(str, "LIST ", 5)) { |
| 2474 | if (strcasestr(str + 5, "\\Noinferiors")) |
| 2475 | no_sub = TRUE; |
| 2476 | if (strcasestr(str + 5, "\\Noselect")) |
| 2477 | no_select = TRUE; |
| 2478 | break;
|
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | g_ptr_array_free(argbuf, TRUE); |
| 2484 | } |
| 2485 | |
| 2486 | new_item = folder_item_new(new_name, dirpath); |
| 2487 | new_item->no_sub = no_sub; |
| 2488 | new_item->no_select = no_select; |
| 2489 | folder_item_append(parent, new_item); |
| 2490 | g_free(imap_path); |
| 2491 | g_free(dirpath); |
| 2492 | |
| 2493 | dirpath = folder_item_get_path(new_item); |
| 2494 | if (!is_dir_exist(dirpath))
|
| 2495 | make_dir_hier(dirpath); |
| 2496 | g_free(dirpath); |
| 2497 | |
| 2498 | return new_item;
|
| 2499 | } |
| 2500 | |
| 2501 | static gint imap_rename_folder_real(Folder *folder, FolderItem *item,
|
| 2502 | FolderItem *new_parent, const gchar *name)
|
| 2503 | {
|
| 2504 | gchar *newpath; |
| 2505 | gchar *real_oldpath; |
| 2506 | gchar *real_newpath; |
| 2507 | gchar *paths[2];
|
| 2508 | gchar *old_cache_dir; |
| 2509 | gchar *new_cache_dir; |
| 2510 | IMAPSession *session; |
| 2511 | gchar separator; |
| 2512 | gint ok; |
| 2513 | gint exists, recent, unseen; |
| 2514 | guint32 uid_validity; |
| 2515 | gchar *old_id, *new_id; |
| 2516 | |
| 2517 | g_return_val_if_fail(folder != NULL, -1); |
| 2518 | g_return_val_if_fail(item != NULL, -1); |
| 2519 | g_return_val_if_fail(folder == item->folder, -1);
|
| 2520 | g_return_val_if_fail(item->path != NULL, -1); |
| 2521 | g_return_val_if_fail(new_parent != NULL || name != NULL, -1); |
| 2522 | if (new_parent) {
|
| 2523 | g_return_val_if_fail(item != new_parent, -1);
|
| 2524 | g_return_val_if_fail(item->parent != new_parent, -1);
|
| 2525 | g_return_val_if_fail(item->folder == new_parent->folder, -1);
|
| 2526 | if (g_node_is_ancestor(item->node, new_parent->node)) {
|
| 2527 | g_warning("folder to be moved is ancestor of new parent\n");
|
| 2528 | return -1; |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | session = imap_session_get(folder); |
| 2533 | if (!session) return -1; |
| 2534 | |
| 2535 | real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path); |
| 2536 | |
| 2537 | g_free(session->mbox); |
| 2538 | session->mbox = NULL;
|
| 2539 | ok = imap_cmd_examine(session, "INBOX",
|
| 2540 | &exists, &recent, &unseen, &uid_validity); |
| 2541 | if (ok != IMAP_SUCCESS) {
|
| 2542 | g_free(real_oldpath); |
| 2543 | return -1; |
| 2544 | } |
| 2545 | |
| 2546 | separator = imap_get_path_separator(IMAP_FOLDER(folder), item->path); |
| 2547 | if (new_parent) {
|
| 2548 | if (name) {
|
| 2549 | if (new_parent->path)
|
| 2550 | newpath = g_strconcat(new_parent->path, |
| 2551 | "/", name, NULL); |
| 2552 | else
|
| 2553 | newpath = g_strdup(name); |
| 2554 | } else {
|
| 2555 | gchar *name_; |
| 2556 | |
| 2557 | name_ = g_path_get_basename(item->path); |
| 2558 | if (new_parent->path)
|
| 2559 | newpath = g_strconcat(new_parent->path, |
| 2560 | "/", name_, NULL); |
| 2561 | else
|
| 2562 | newpath = g_strdup(name_); |
| 2563 | AUTORELEASE_STR(name_, ); |
| 2564 | name = name_; |
| 2565 | } |
| 2566 | } else {
|
| 2567 | if (strchr(item->path, '/')) { |
| 2568 | gchar *dirpath; |
| 2569 | |
| 2570 | dirpath = g_dirname(item->path); |
| 2571 | newpath = g_strconcat(dirpath, "/", name, NULL); |
| 2572 | g_free(dirpath); |
| 2573 | } else
|
| 2574 | newpath = g_strdup(name); |
| 2575 | } |
| 2576 | |
| 2577 | real_newpath = imap_utf8_to_modified_utf7(newpath); |
| 2578 | imap_path_separator_subst(real_newpath, separator); |
| 2579 | |
| 2580 | ok = imap_cmd_rename(session, real_oldpath, real_newpath); |
| 2581 | if (ok != IMAP_SUCCESS) {
|
| 2582 | log_warning(_("can't rename mailbox: %s to %s\n"),
|
| 2583 | real_oldpath, real_newpath); |
| 2584 | g_free(real_oldpath); |
| 2585 | g_free(newpath); |
| 2586 | g_free(real_newpath); |
| 2587 | return -1; |
| 2588 | } |
| 2589 | |
| 2590 | old_id = folder_item_get_identifier(item); |
| 2591 | |
| 2592 | if (new_parent) {
|
| 2593 | g_node_unlink(item->node); |
| 2594 | g_node_append(new_parent->node, item->node); |
| 2595 | item->parent = new_parent; |
| 2596 | } |
| 2597 | |
| 2598 | g_free(item->name); |
| 2599 | item->name = g_strdup(name); |
| 2600 | |
| 2601 | old_cache_dir = folder_item_get_path(item); |
| 2602 | |
| 2603 | paths[0] = g_strdup(item->path);
|
| 2604 | paths[1] = newpath;
|
| 2605 | g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
|
| 2606 | imap_rename_folder_func, paths); |
| 2607 | |
| 2608 | if (is_dir_exist(old_cache_dir)) {
|
| 2609 | new_cache_dir = folder_item_get_path(item); |
| 2610 | if (g_rename(old_cache_dir, new_cache_dir) < 0) { |
| 2611 | FILE_OP_ERROR(old_cache_dir, "rename");
|
| 2612 | } |
| 2613 | g_free(new_cache_dir); |
| 2614 | } |
| 2615 | |
| 2616 | g_free(old_cache_dir); |
| 2617 | g_free(paths[0]);
|
| 2618 | g_free(newpath); |
| 2619 | g_free(real_oldpath); |
| 2620 | g_free(real_newpath); |
| 2621 | |
| 2622 | new_id = folder_item_get_identifier(item); |
| 2623 | if (syl_app_get())
|
| 2624 | g_signal_emit_by_name(syl_app_get(), "move-folder", item,
|
| 2625 | old_id, new_id); |
| 2626 | g_free(new_id); |
| 2627 | g_free(old_id); |
| 2628 | |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
| 2632 | static gint imap_rename_folder(Folder *folder, FolderItem *item,
|
| 2633 | const gchar *name)
|
| 2634 | {
|
| 2635 | return imap_rename_folder_real(folder, item, NULL, name); |
| 2636 | } |
| 2637 | |
| 2638 | static gint imap_move_folder(Folder *folder, FolderItem *item,
|
| 2639 | FolderItem *new_parent) |
| 2640 | {
|
| 2641 | return imap_rename_folder_real(folder, item, new_parent, NULL); |
| 2642 | } |
| 2643 | |
| 2644 | static gint imap_remove_folder(Folder *folder, FolderItem *item)
|
| 2645 | {
|
| 2646 | gint ok; |
| 2647 | IMAPSession *session; |
| 2648 | gchar *path; |
| 2649 | gchar *cache_dir; |
| 2650 | gint exists, recent, unseen; |
| 2651 | guint32 uid_validity; |
| 2652 | |
| 2653 | g_return_val_if_fail(folder != NULL, -1); |
| 2654 | g_return_val_if_fail(item != NULL, -1); |
| 2655 | g_return_val_if_fail(item->path != NULL, -1); |
| 2656 | |
| 2657 | session = imap_session_get(folder); |
| 2658 | if (!session) return -1; |
| 2659 | |
| 2660 | path = imap_get_real_path(IMAP_FOLDER(folder), item->path); |
| 2661 | |
| 2662 | ok = imap_cmd_examine(session, "INBOX",
|
| 2663 | &exists, &recent, &unseen, &uid_validity); |
| 2664 | if (ok != IMAP_SUCCESS) {
|
| 2665 | g_free(path); |
| 2666 | return -1; |
| 2667 | } |
| 2668 | |
| 2669 | ok = imap_cmd_delete(session, path); |
| 2670 | if (ok != IMAP_SUCCESS) {
|
| 2671 | log_warning(_("can't delete mailbox\n"));
|
| 2672 | g_free(path); |
| 2673 | return -1; |
| 2674 | } |
| 2675 | |
| 2676 | g_free(path); |
| 2677 | cache_dir = folder_item_get_path(item); |
| 2678 | if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0) |
| 2679 | g_warning("can't remove directory '%s'\n", cache_dir);
|
| 2680 | g_free(cache_dir); |
| 2681 | |
| 2682 | if (syl_app_get())
|
| 2683 | g_signal_emit_by_name(syl_app_get(), "remove-folder", item);
|
| 2684 | folder_item_remove(item); |
| 2685 | |
| 2686 | return 0; |
| 2687 | } |
| 2688 | |
| 2689 | typedef struct _IMAPGetData |
| 2690 | {
|
| 2691 | FolderItem *item; |
| 2692 | gint exists; |
| 2693 | gboolean update_count; |
| 2694 | GSList *newlist; |
| 2695 | } IMAPGetData; |
| 2696 | |
| 2697 | static gint imap_get_uncached_messages_progress_func(IMAPSession *session,
|
| 2698 | gint count, gint total, |
| 2699 | gpointer data) |
| 2700 | {
|
| 2701 | status_print(_("Getting message headers (%d / %d)"), count, total);
|
| 2702 | progress_show(count, total); |
| 2703 | #ifndef USE_THREADS
|
| 2704 | ui_update(); |
| 2705 | #endif
|
| 2706 | return 0; |
| 2707 | } |
| 2708 | |
| 2709 | static gint imap_get_uncached_messages_func(IMAPSession *session, gpointer data)
|
| 2710 | {
|
| 2711 | gchar *tmp; |
| 2712 | GSList *newlist = NULL;
|
| 2713 | GSList *llast = NULL;
|
| 2714 | GString *str; |
| 2715 | MsgInfo *msginfo; |
| 2716 | gint count = 1;
|
| 2717 | GTimeVal tv_prev, tv_cur; |
| 2718 | IMAPGetData *get_data = (IMAPGetData *)data; |
| 2719 | FolderItem *item = get_data->item; |
| 2720 | gint exists = get_data->exists; |
| 2721 | gboolean update_count = get_data->update_count; |
| 2722 | |
| 2723 | g_get_current_time(&tv_prev); |
| 2724 | #ifndef USE_THREADS
|
| 2725 | ui_update(); |
| 2726 | #endif
|
| 2727 | |
| 2728 | #if USE_THREADS
|
| 2729 | ((IMAPRealSession *)session)->prog_total = exists; |
| 2730 | #endif
|
| 2731 | |
| 2732 | str = g_string_new(NULL);
|
| 2733 | |
| 2734 | for (;;) {
|
| 2735 | if (exists > 0 && count <= exists) { |
| 2736 | g_get_current_time(&tv_cur); |
| 2737 | if (tv_cur.tv_sec > tv_prev.tv_sec ||
|
| 2738 | tv_cur.tv_usec - tv_prev.tv_usec > |
| 2739 | PROGRESS_UPDATE_INTERVAL * 1000) {
|
| 2740 | #if USE_THREADS
|
| 2741 | ((IMAPRealSession *)session)->prog_count = count; |
| 2742 | g_main_context_wakeup(NULL);
|
| 2743 | #else
|
| 2744 | imap_get_uncached_messages_progress_func |
| 2745 | (session, count, exists, data); |
| 2746 | #endif
|
| 2747 | tv_prev = tv_cur; |
| 2748 | } |
| 2749 | } |
| 2750 | ++count; |
| 2751 | |
| 2752 | if (sock_getline(SESSION(session)->sock, &tmp) < 0) { |
| 2753 | log_warning(_("error occurred while getting envelope.\n"));
|
| 2754 | g_string_free(str, TRUE); |
| 2755 | return IMAP_SOCKET;
|
| 2756 | } |
| 2757 | strretchomp(tmp); |
| 2758 | if (tmp[0] != '*' || tmp[1] != ' ') { |
| 2759 | log_print("IMAP4< %s\n", tmp);
|
| 2760 | g_free(tmp); |
| 2761 | break;
|
| 2762 | } |
| 2763 | if (strstr(tmp, "FETCH") == NULL) { |
| 2764 | log_print("IMAP4< %s\n", tmp);
|
| 2765 | g_free(tmp); |
| 2766 | continue;
|
| 2767 | } |
| 2768 | log_print("IMAP4< %s\n", tmp);
|
| 2769 | g_string_assign(str, tmp); |
| 2770 | g_free(tmp); |
| 2771 | |
| 2772 | msginfo = imap_parse_envelope(session, item, str); |
| 2773 | if (!msginfo) {
|
| 2774 | log_warning(_("can't parse envelope: %s\n"), str->str);
|
| 2775 | continue;
|
| 2776 | } |
| 2777 | if (update_count) {
|
| 2778 | if (MSG_IS_NEW(msginfo->flags))
|
| 2779 | item->new++; |
| 2780 | if (MSG_IS_UNREAD(msginfo->flags))
|
| 2781 | item->unread++; |
| 2782 | } |
| 2783 | if (item->stype == F_QUEUE) {
|
| 2784 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_QUEUED); |
| 2785 | } else if (item->stype == F_DRAFT) { |
| 2786 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_DRAFT); |
| 2787 | } |
| 2788 | |
| 2789 | msginfo->folder = item; |
| 2790 | |
| 2791 | if (!newlist)
|
| 2792 | llast = newlist = g_slist_append(newlist, msginfo); |
| 2793 | else {
|
| 2794 | llast = g_slist_append(llast, msginfo); |
| 2795 | llast = llast->next; |
| 2796 | } |
| 2797 | |
| 2798 | if (update_count)
|
| 2799 | item->total++; |
| 2800 | } |
| 2801 | |
| 2802 | g_string_free(str, TRUE); |
| 2803 | |
| 2804 | session_set_access_time(SESSION(session)); |
| 2805 | |
| 2806 | get_data->newlist = newlist; |
| 2807 | return IMAP_SUCCESS;
|
| 2808 | } |
| 2809 | |
| 2810 | static GSList *imap_get_uncached_messages(IMAPSession *session,
|
| 2811 | FolderItem *item, |
| 2812 | guint32 first_uid, guint32 last_uid, |
| 2813 | gint exists, gboolean update_count) |
| 2814 | {
|
| 2815 | IMAPGetData get_data = {item, exists, update_count, NULL};
|
| 2816 | gchar seq_set[22];
|
| 2817 | gint ok; |
| 2818 | |
| 2819 | g_return_val_if_fail(session != NULL, NULL); |
| 2820 | g_return_val_if_fail(item != NULL, NULL); |
| 2821 | g_return_val_if_fail(item->folder != NULL, NULL); |
| 2822 | g_return_val_if_fail(FOLDER_TYPE(item->folder) == F_IMAP, NULL);
|
| 2823 | g_return_val_if_fail(first_uid <= last_uid, NULL);
|
| 2824 | |
| 2825 | if (first_uid == 0 && last_uid == 0) |
| 2826 | strcpy(seq_set, "1:*");
|
| 2827 | else
|
| 2828 | g_snprintf(seq_set, sizeof(seq_set), "%u:%u", |
| 2829 | first_uid, last_uid); |
| 2830 | if (imap_cmd_envelope(session, seq_set) != IMAP_SUCCESS) {
|
| 2831 | log_warning(_("can't get envelope\n"));
|
| 2832 | return NULL; |
| 2833 | } |
| 2834 | |
| 2835 | #if USE_THREADS
|
| 2836 | ok = imap_thread_run_progress(session, imap_get_uncached_messages_func, |
| 2837 | imap_get_uncached_messages_progress_func, |
| 2838 | &get_data); |
| 2839 | #else
|
| 2840 | ok = imap_get_uncached_messages_func(session, &get_data); |
| 2841 | #endif
|
| 2842 | |
| 2843 | progress_show(0, 0); |
| 2844 | return get_data.newlist;
|
| 2845 | } |
| 2846 | |
| 2847 | static void imap_delete_cached_message(FolderItem *item, guint32 uid) |
| 2848 | {
|
| 2849 | gchar *dir; |
| 2850 | gchar *file; |
| 2851 | |
| 2852 | g_return_if_fail(item != NULL);
|
| 2853 | g_return_if_fail(item->folder != NULL);
|
| 2854 | g_return_if_fail(FOLDER_TYPE(item->folder) == F_IMAP); |
| 2855 | |
| 2856 | dir = folder_item_get_path(item); |
| 2857 | file = g_strdup_printf("%s%c%u", dir, G_DIR_SEPARATOR, uid);
|
| 2858 | |
| 2859 | debug_print("Deleting cached message: %s\n", file);
|
| 2860 | |
| 2861 | g_unlink(file); |
| 2862 | |
| 2863 | g_free(file); |
| 2864 | g_free(dir); |
| 2865 | } |
| 2866 | |
| 2867 | static GSList *imap_delete_cached_messages(GSList *mlist, FolderItem *item,
|
| 2868 | guint32 first_uid, guint32 last_uid) |
| 2869 | {
|
| 2870 | GSList *cur, *next; |
| 2871 | MsgInfo *msginfo; |
| 2872 | gchar *dir; |
| 2873 | |
| 2874 | g_return_val_if_fail(item != NULL, mlist);
|
| 2875 | g_return_val_if_fail(item->folder != NULL, mlist);
|
| 2876 | g_return_val_if_fail(FOLDER_TYPE(item->folder) == F_IMAP, mlist); |
| 2877 | |
| 2878 | if (first_uid == 0 && last_uid == 0) |
| 2879 | return mlist;
|
| 2880 | |
| 2881 | debug_print("Deleting cached messages %u - %u ... ",
|
| 2882 | first_uid, last_uid); |
| 2883 | |
| 2884 | dir = folder_item_get_path(item); |
| 2885 | if (is_dir_exist(dir))
|
| 2886 | remove_numbered_files(dir, first_uid, last_uid); |
| 2887 | g_free(dir); |
| 2888 | |
| 2889 | for (cur = mlist; cur != NULL; ) { |
| 2890 | next = cur->next; |
| 2891 | |
| 2892 | msginfo = (MsgInfo *)cur->data; |
| 2893 | if (msginfo != NULL && first_uid <= msginfo->msgnum && |
| 2894 | msginfo->msgnum <= last_uid) {
|
| 2895 | procmsg_msginfo_free(msginfo); |
| 2896 | mlist = g_slist_remove(mlist, msginfo); |
| 2897 | } |
| 2898 | |
| 2899 | cur = next; |
| 2900 | } |
| 2901 | |
| 2902 | debug_print("done.\n");
|
| 2903 | |
| 2904 | return mlist;
|
| 2905 | } |
| 2906 | |
| 2907 | static void imap_delete_all_cached_messages(FolderItem *item) |
| 2908 | {
|
| 2909 | gchar *dir; |
| 2910 | |
| 2911 | g_return_if_fail(item != NULL);
|
| 2912 | g_return_if_fail(item->folder != NULL);
|
| 2913 | g_return_if_fail(FOLDER_TYPE(item->folder) == F_IMAP); |
| 2914 | |
| 2915 | debug_print("Deleting all cached messages... ");
|
| 2916 | |
| 2917 | dir = folder_item_get_path(item); |
| 2918 | if (is_dir_exist(dir))
|
| 2919 | remove_all_numbered_files(dir); |
| 2920 | g_free(dir); |
| 2921 | |
| 2922 | debug_print("done.\n");
|
| 2923 | } |
| 2924 | |
| 2925 | #if USE_SSL
|
| 2926 | static SockInfo *imap_open(const gchar *server, gushort port, |
| 2927 | SocksInfo *socks_info, SSLType ssl_type) |
| 2928 | #else
|
| 2929 | static SockInfo *imap_open(const gchar *server, gushort port, |
| 2930 | SocksInfo *socks_info) |
| 2931 | #endif
|
| 2932 | {
|
| 2933 | SockInfo *sock = NULL;
|
| 2934 | const gchar *server_;
|
| 2935 | gushort port_; |
| 2936 | #if USE_THREADS
|
| 2937 | gint conn_id; |
| 2938 | #endif
|
| 2939 | |
| 2940 | if (socks_info) {
|
| 2941 | server_ = socks_info->proxy_host; |
| 2942 | port_ = socks_info->proxy_port; |
| 2943 | } else {
|
| 2944 | server_ = server; |
| 2945 | port_ = port; |
| 2946 | } |
| 2947 | |
| 2948 | #if USE_THREADS
|
| 2949 | if ((conn_id = sock_connect_async_thread(server_, port_)) < 0 || |
| 2950 | sock_connect_async_thread_wait(conn_id, &sock) < 0) {
|
| 2951 | log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
|
| 2952 | server, port); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | #else
|
| 2956 | if ((sock = sock_connect(server_, port_)) == NULL) { |
| 2957 | log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
|
| 2958 | server, port); |
| 2959 | return NULL; |
| 2960 | } |
| 2961 | #endif
|
| 2962 | |
| 2963 | if (socks_info) {
|
| 2964 | if (socks_connect(sock, server, port, socks_info) < 0) { |
| 2965 | log_warning("Can't establish SOCKS connection: %s:%d\n",
|
| 2966 | server, port); |
| 2967 | sock_close(sock); |
| 2968 | return NULL; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | #if USE_SSL
|
| 2973 | if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
|
| 2974 | log_warning(_("Can't establish IMAP4 session with: %s:%d\n"),
|
| 2975 | server, port); |
| 2976 | sock_close(sock); |
| 2977 | return NULL; |
| 2978 | } |
| 2979 | #endif
|
| 2980 | |
| 2981 | return sock;
|
| 2982 | } |
| 2983 | |
| 2984 | static GList *imap_parse_namespace_str(gchar *str)
|
| 2985 | {
|
| 2986 | gchar *p = str; |
| 2987 | gchar *name; |
| 2988 | gchar *separator; |
| 2989 | IMAPNameSpace *namespace; |
| 2990 | GList *ns_list = NULL;
|
| 2991 | |
| 2992 | while (*p != '\0') { |
| 2993 | /* parse ("#foo" "/") */
|
| 2994 | |
| 2995 | while (*p && *p != '(') p++; |
| 2996 | if (*p == '\0') break; |
| 2997 | p++; |
| 2998 | |
| 2999 | while (*p && *p != '"') p++; |
| 3000 | if (*p == '\0') break; |
| 3001 | p++; |
| 3002 | name = p; |
| 3003 | |
| 3004 | while (*p && *p != '"') p++; |
| 3005 | if (*p == '\0') break; |
| 3006 | *p = '\0';
|
| 3007 | p++; |
| 3008 | |
| 3009 | while (*p && g_ascii_isspace(*p)) p++;
|
| 3010 | if (*p == '\0') break; |
| 3011 | if (strncmp(p, "NIL", 3) == 0) |
| 3012 | separator = NULL;
|
| 3013 | else if (*p == '"') { |
| 3014 | p++; |
| 3015 | separator = p; |
| 3016 | while (*p && *p != '"') p++; |
| 3017 | if (*p == '\0') break; |
| 3018 | *p = '\0';
|
| 3019 | p++; |
| 3020 | } else break; |
| 3021 | |
| 3022 | while (*p && *p != ')') p++; |
| 3023 | if (*p == '\0') break; |
| 3024 | p++; |
| 3025 | |
| 3026 | namespace = g_new(IMAPNameSpace, 1);
|
| 3027 | namespace->name = g_strdup(name); |
| 3028 | namespace->separator = separator ? separator[0] : '\0'; |
| 3029 | ns_list = g_list_append(ns_list, namespace); |
| 3030 | } |
| 3031 | |
| 3032 | return ns_list;
|
| 3033 | } |
| 3034 | |
| 3035 | static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder) |
| 3036 | {
|
| 3037 | gchar *ns_str = NULL;
|
| 3038 | gchar **str_array; |
| 3039 | |
| 3040 | g_return_if_fail(session != NULL);
|
| 3041 | g_return_if_fail(folder != NULL);
|
| 3042 | |
| 3043 | if (folder->ns_personal != NULL || |
| 3044 | folder->ns_others != NULL ||
|
| 3045 | folder->ns_shared != NULL)
|
| 3046 | return;
|
| 3047 | |
| 3048 | if (imap_cmd_namespace(session, &ns_str) != IMAP_SUCCESS) {
|
| 3049 | log_warning(_("can't get namespace\n"));
|
| 3050 | imap_get_namespace_by_list(session, folder); |
| 3051 | return;
|
| 3052 | } |
| 3053 | |
| 3054 | str_array = strsplit_parenthesis(ns_str, '(', ')', 3); |
| 3055 | if (str_array[0]) |
| 3056 | folder->ns_personal = imap_parse_namespace_str(str_array[0]);
|
| 3057 | if (str_array[0] && str_array[1]) |
| 3058 | folder->ns_others = imap_parse_namespace_str(str_array[1]);
|
| 3059 | if (str_array[0] && str_array[1] && str_array[2]) |
| 3060 | folder->ns_shared = imap_parse_namespace_str(str_array[2]);
|
| 3061 | g_strfreev(str_array); |
| 3062 | g_free(ns_str); |
| 3063 | } |
| 3064 | |
| 3065 | static void imap_get_namespace_by_list(IMAPSession *session, IMAPFolder *folder) |
| 3066 | {
|
| 3067 | GSList *item_list, *cur; |
| 3068 | gchar separator = '\0';
|
| 3069 | IMAPNameSpace *namespace; |
| 3070 | |
| 3071 | g_return_if_fail(session != NULL);
|
| 3072 | g_return_if_fail(folder != NULL);
|
| 3073 | |
| 3074 | if (folder->ns_personal != NULL || |
| 3075 | folder->ns_others != NULL ||
|
| 3076 | folder->ns_shared != NULL)
|
| 3077 | return;
|
| 3078 | |
| 3079 | if (imap_cmd_gen_send(session, "LIST \"\" \"\"") != IMAP_SUCCESS) |
| 3080 | return;
|
| 3081 | item_list = imap_parse_list(session, "", &separator);
|
| 3082 | for (cur = item_list; cur != NULL; cur = cur->next) |
| 3083 | folder_item_destroy(FOLDER_ITEM(cur->data)); |
| 3084 | g_slist_free(item_list); |
| 3085 | |
| 3086 | namespace = g_new(IMAPNameSpace, 1);
|
| 3087 | namespace->name = g_strdup("");
|
| 3088 | namespace->separator = separator; |
| 3089 | folder->ns_personal = g_list_append(NULL, namespace);
|
| 3090 | } |
| 3091 | |
| 3092 | static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
|
| 3093 | const gchar *path)
|
| 3094 | {
|
| 3095 | IMAPNameSpace *namespace = NULL;
|
| 3096 | gchar *tmp_path, *name; |
| 3097 | |
| 3098 | if (!path) path = ""; |
| 3099 | |
| 3100 | for (; ns_list != NULL; ns_list = ns_list->next) { |
| 3101 | IMAPNameSpace *tmp_ns = ns_list->data; |
| 3102 | |
| 3103 | Xstrcat_a(tmp_path, path, "/", return namespace); |
| 3104 | Xstrdup_a(name, tmp_ns->name, return namespace);
|
| 3105 | if (tmp_ns->separator && tmp_ns->separator != '/') { |
| 3106 | subst_char(tmp_path, tmp_ns->separator, '/');
|
| 3107 | subst_char(name, tmp_ns->separator, '/');
|
| 3108 | } |
| 3109 | if (strncmp(tmp_path, name, strlen(name)) == 0) |
| 3110 | namespace = tmp_ns; |
| 3111 | } |
| 3112 | |
| 3113 | return namespace;
|
| 3114 | } |
| 3115 | |
| 3116 | static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
|
| 3117 | const gchar *path)
|
| 3118 | {
|
| 3119 | IMAPNameSpace *namespace; |
| 3120 | |
| 3121 | g_return_val_if_fail(folder != NULL, NULL); |
| 3122 | |
| 3123 | namespace = imap_find_namespace_from_list(folder->ns_personal, path); |
| 3124 | if (namespace) return namespace; |
| 3125 | namespace = imap_find_namespace_from_list(folder->ns_others, path); |
| 3126 | if (namespace) return namespace; |
| 3127 | namespace = imap_find_namespace_from_list(folder->ns_shared, path); |
| 3128 | if (namespace) return namespace; |
| 3129 | |
| 3130 | return NULL; |
| 3131 | } |
| 3132 | |
| 3133 | static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path) |
| 3134 | {
|
| 3135 | IMAPNameSpace *namespace; |
| 3136 | gchar separator = '/';
|
| 3137 | |
| 3138 | namespace = imap_find_namespace(folder, path); |
| 3139 | if (namespace && namespace->separator)
|
| 3140 | separator = namespace->separator; |
| 3141 | |
| 3142 | return separator;
|
| 3143 | } |
| 3144 | |
| 3145 | static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path) |
| 3146 | {
|
| 3147 | gchar *real_path; |
| 3148 | gchar separator; |
| 3149 | |
| 3150 | g_return_val_if_fail(folder != NULL, NULL); |
| 3151 | g_return_val_if_fail(path != NULL, NULL); |
| 3152 | |
| 3153 | real_path = imap_utf8_to_modified_utf7(path); |
| 3154 | separator = imap_get_path_separator(folder, path); |
| 3155 | imap_path_separator_subst(real_path, separator); |
| 3156 | |
| 3157 | return real_path;
|
| 3158 | } |
| 3159 | |
| 3160 | static gchar *imap_parse_atom(IMAPSession *session, gchar *src,
|
| 3161 | gchar *dest, gint dest_len, GString *str) |
| 3162 | {
|
| 3163 | gchar *cur_pos = src; |
| 3164 | gchar *nextline; |
| 3165 | |
| 3166 | g_return_val_if_fail(str != NULL, cur_pos);
|
| 3167 | |
| 3168 | /* read the next line if the current response buffer is empty */
|
| 3169 | while (g_ascii_isspace(*cur_pos)) cur_pos++;
|
| 3170 | while (*cur_pos == '\0') { |
| 3171 | if (sock_getline(SESSION(session)->sock, &nextline) < 0) |
| 3172 | return cur_pos;
|
| 3173 | g_string_assign(str, nextline); |
| 3174 | cur_pos = str->str; |
| 3175 | strretchomp(nextline); |
| 3176 | /* log_print("IMAP4< %s\n", nextline); */
|
| 3177 | debug_print("IMAP4< %s\n", nextline);
|
| 3178 | g_free(nextline); |
| 3179 | |
| 3180 | while (g_ascii_isspace(*cur_pos)) cur_pos++;
|
| 3181 | } |
| 3182 | |
| 3183 | if (*cur_pos == '~' && *(cur_pos + 1) == '{') |
| 3184 | cur_pos++; |
| 3185 | |
| 3186 | if (!strncmp(cur_pos, "NIL", 3)) { |
| 3187 | *dest = '\0';
|
| 3188 | cur_pos += 3;
|
| 3189 | } else if (*cur_pos == '\"') { |
| 3190 | gchar *p; |
| 3191 | |
| 3192 | p = get_quoted(cur_pos, '\"', dest, dest_len);
|
| 3193 | cur_pos = p ? p : cur_pos + 2;
|
| 3194 | } else if (*cur_pos == '{') { |
| 3195 | gchar buf[32];
|
| 3196 | gint len; |
| 3197 | gint block_len = 0;
|
| 3198 | |
| 3199 | cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf)); |
| 3200 | len = atoi(buf); |
| 3201 | g_return_val_if_fail(len >= 0, cur_pos);
|
| 3202 | |
| 3203 | g_string_truncate(str, 0);
|
| 3204 | cur_pos = str->str; |
| 3205 | |
| 3206 | do {
|
| 3207 | gint cur_len; |
| 3208 | |
| 3209 | cur_len = sock_getline(SESSION(session)->sock, |
| 3210 | &nextline); |
| 3211 | if (cur_len < 0) |
| 3212 | return cur_pos;
|
| 3213 | block_len += cur_len; |
| 3214 | subst_null(nextline, cur_len, ' ');
|
| 3215 | g_string_append(str, nextline); |
| 3216 | cur_pos = str->str; |
| 3217 | strretchomp(nextline); |
| 3218 | /* log_print("IMAP4< %s\n", nextline); */
|
| 3219 | debug_print("IMAP4< %s\n", nextline);
|
| 3220 | g_free(nextline); |
| 3221 | } while (block_len < len);
|
| 3222 | |
| 3223 | memcpy(dest, cur_pos, MIN(len, dest_len - 1));
|
| 3224 | dest[MIN(len, dest_len - 1)] = '\0'; |
| 3225 | cur_pos += len; |
| 3226 | } |
| 3227 | |
| 3228 | return cur_pos;
|
| 3229 | } |
| 3230 | |
| 3231 | static gchar *imap_get_header(IMAPSession *session, gchar *cur_pos,
|
| 3232 | gchar **headers, GString *str) |
| 3233 | {
|
| 3234 | gchar *nextline; |
| 3235 | gchar buf[IMAPBUFSIZE]; |
| 3236 | gint len; |
| 3237 | gint block_len = 0;
|
| 3238 | |
| 3239 | *headers = NULL;
|
| 3240 | |
| 3241 | g_return_val_if_fail(str != NULL, cur_pos);
|
| 3242 | |
| 3243 | while (g_ascii_isspace(*cur_pos)) cur_pos++;
|
| 3244 | if (*cur_pos == '~' && *(cur_pos + 1) == '{') |
| 3245 | cur_pos++; |
| 3246 | |
| 3247 | if (*cur_pos == '"') { |
| 3248 | cur_pos = strchr_cpy(cur_pos + 1, '"', buf, sizeof(buf)); |
| 3249 | if (!cur_pos)
|
| 3250 | return NULL; |
| 3251 | len = strlen(buf); |
| 3252 | *headers = g_strdup(buf); |
| 3253 | while (g_ascii_isspace(*cur_pos)) cur_pos++;
|
| 3254 | return cur_pos;
|
| 3255 | } |
| 3256 | |
| 3257 | g_return_val_if_fail(*cur_pos == '{', cur_pos);
|
| 3258 | |
| 3259 | cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf)); |
| 3260 | len = atoi(buf); |
| 3261 | g_return_val_if_fail(len >= 0, cur_pos);
|
| 3262 | |
| 3263 | g_string_truncate(str, 0);
|
| 3264 | cur_pos = str->str; |
| 3265 | |
| 3266 | do {
|
| 3267 | gint cur_len; |
| 3268 | |
| 3269 | cur_len = sock_getline(SESSION(session)->sock, &nextline); |
| 3270 | if (cur_len < 0) |
| 3271 | return cur_pos;
|
| 3272 | block_len += cur_len; |
| 3273 | subst_null(nextline, cur_len, ' ');
|
| 3274 | g_string_append(str, nextline); |
| 3275 | cur_pos = str->str; |
| 3276 | /* strretchomp(nextline); */
|
| 3277 | /* debug_print("IMAP4< %s\n", nextline); */
|
| 3278 | g_free(nextline); |
| 3279 | } while (block_len < len);
|
| 3280 | |
| 3281 | debug_print("IMAP4< [contents of RFC822.HEADER]\n");
|
| 3282 | |
| 3283 | *headers = g_strndup(cur_pos, len); |
| 3284 | cur_pos += len; |
| 3285 | |
| 3286 | while (g_ascii_isspace(*cur_pos)) cur_pos++;
|
| 3287 | while (*cur_pos == '\0') { |
| 3288 | if (sock_getline(SESSION(session)->sock, &nextline) < 0) |
| 3289 | return cur_pos;
|
| 3290 | g_string_assign(str, nextline); |
| 3291 | cur_pos = str->str; |
| 3292 | strretchomp(nextline); |
| 3293 | debug_print("IMAP4< %s\n", nextline);
|
| 3294 | g_free(nextline); |
| 3295 | |
| 3296 | while (g_ascii_isspace(*cur_pos)) cur_pos++;
|
| 3297 | } |
| 3298 | |
| 3299 | return cur_pos;
|
| 3300 | } |
| 3301 | |
| 3302 | static MsgFlags imap_parse_flags(const gchar *flag_str) |
| 3303 | {
|
| 3304 | const gchar *p = flag_str;
|
| 3305 | MsgFlags flags = {0, 0};
|
| 3306 | |
| 3307 | flags.perm_flags = MSG_UNREAD; |
| 3308 | |
| 3309 | while (*p != '\0') { |
| 3310 | if (g_ascii_strncasecmp(p, "\\Recent", 7) == 0 && |
| 3311 | MSG_IS_UNREAD(flags)) {
|
| 3312 | MSG_SET_PERM_FLAGS(flags, MSG_NEW); |
| 3313 | } else if (g_ascii_strncasecmp(p, "\\Seen", 5) == 0) { |
| 3314 | MSG_UNSET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD); |
| 3315 | } else if (g_ascii_strncasecmp(p, "\\Deleted", 8) == 0) { |
| 3316 | MSG_SET_PERM_FLAGS(flags, MSG_DELETED); |
| 3317 | } else if (g_ascii_strncasecmp(p, "\\Flagged", 8) == 0) { |
| 3318 | MSG_SET_PERM_FLAGS(flags, MSG_MARKED); |
| 3319 | } else if (g_ascii_strncasecmp(p, "\\Answered", 9) == 0) { |
| 3320 | MSG_SET_PERM_FLAGS(flags, MSG_REPLIED); |
| 3321 | } else if (g_ascii_strncasecmp(p, "$label", 6) == 0) { |
| 3322 | /* color labels */
|
| 3323 | if (*(p + 6) >= '1' && *(p + 6) <= '7') { |
| 3324 | guint color = *(p + 6) - '1' + 1; |
| 3325 | MSG_UNSET_PERM_FLAGS(flags, |
| 3326 | MSG_CLABEL_FLAG_MASK); |
| 3327 | MSG_SET_COLORLABEL_VALUE(flags, color); |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | while (*p && !g_ascii_isspace(*p)) p++;
|
| 3332 | while (g_ascii_isspace(*p)) p++;
|
| 3333 | } |
| 3334 | |
| 3335 | return flags;
|
| 3336 | } |
| 3337 | |
| 3338 | static IMAPFlags imap_parse_imap_flags(const gchar *flag_str) |
| 3339 | {
|
| 3340 | const gchar *p = flag_str;
|
| 3341 | IMAPFlags flags = 0;
|
| 3342 | |
| 3343 | while (*p != '\0') { |
| 3344 | if (g_ascii_strncasecmp(p, "\\Seen", 5) == 0) { |
| 3345 | flags |= IMAP_FLAG_SEEN; |
| 3346 | } else if (g_ascii_strncasecmp(p, "\\Deleted", 8) == 0) { |
| 3347 | flags |= IMAP_FLAG_DELETED; |
| 3348 | } else if (g_ascii_strncasecmp(p, "\\Flagged", 8) == 0) { |
| 3349 | flags |= IMAP_FLAG_FLAGGED; |
| 3350 | } else if (g_ascii_strncasecmp(p, "\\Answered", 9) == 0) { |
| 3351 | flags |= IMAP_FLAG_ANSWERED; |
| 3352 | } else if (g_ascii_strncasecmp(p, "$label", 6) == 0) { |
| 3353 | /* color labels */
|
| 3354 | if (*(p + 6) >= '1' && *(p + 6) <= '7') { |
| 3355 | guint color = *(p + 6) - '1' + 1; |
| 3356 | MSG_UNSET_FLAGS(flags, MSG_CLABEL_FLAG_MASK); |
| 3357 | IMAP_SET_COLORLABEL_VALUE(flags, color); |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | while (*p && !g_ascii_isspace(*p)) p++;
|
| 3362 | while (g_ascii_isspace(*p)) p++;
|
| 3363 | } |
| 3364 | |
| 3365 | return flags;
|
| 3366 | } |
| 3367 | |
| 3368 | static MsgInfo *imap_parse_envelope(IMAPSession *session, FolderItem *item,
|
| 3369 | GString *line_str) |
| 3370 | {
|
| 3371 | gchar buf[IMAPBUFSIZE]; |
| 3372 | MsgInfo *msginfo = NULL;
|
| 3373 | gchar *cur_pos; |
| 3374 | gint msgnum; |
| 3375 | guint32 uid = 0;
|
| 3376 | size_t size = 0;
|
| 3377 | MsgFlags flags = {0, 0}, imap_flags = {0, 0};
|
| 3378 | |
| 3379 | g_return_val_if_fail(line_str != NULL, NULL); |
| 3380 | g_return_val_if_fail(line_str->str[0] == '*' && |
| 3381 | line_str->str[1] == ' ', NULL); |
| 3382 | |
| 3383 | MSG_SET_TMP_FLAGS(flags, MSG_IMAP); |
| 3384 | if (item->stype == F_QUEUE) {
|
| 3385 | MSG_SET_TMP_FLAGS(flags, MSG_QUEUED); |
| 3386 | } else if (item->stype == F_DRAFT) { |
| 3387 | MSG_SET_TMP_FLAGS(flags, MSG_DRAFT); |
| 3388 | } |
| 3389 | |
| 3390 | cur_pos = line_str->str + 2;
|
| 3391 | |
| 3392 | #define PARSE_ONE_ELEMENT(ch) \
|
| 3393 | { \
|
| 3394 | cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf)); \
|
| 3395 | if (cur_pos == NULL) { \ |
| 3396 | g_warning("cur_pos == NULL\n"); \
|
| 3397 | procmsg_msginfo_free(msginfo); \ |
| 3398 | return NULL; \ |
| 3399 | } \ |
| 3400 | } |
| 3401 | |
| 3402 | PARSE_ONE_ELEMENT(' ');
|
| 3403 | msgnum = atoi(buf); |
| 3404 | |
| 3405 | PARSE_ONE_ELEMENT(' ');
|
| 3406 | g_return_val_if_fail(!strcmp(buf, "FETCH"), NULL); |
| 3407 | |
| 3408 | g_return_val_if_fail(*cur_pos == '(', NULL); |
| 3409 | cur_pos++; |
| 3410 | |
| 3411 | while (*cur_pos != '\0' && *cur_pos != ')') { |
| 3412 | while (*cur_pos == ' ') cur_pos++; |
| 3413 | |
| 3414 | if (!strncmp(cur_pos, "UID ", 4)) { |
| 3415 | cur_pos += 4;
|
| 3416 | uid = strtoul(cur_pos, &cur_pos, 10);
|
| 3417 | } else if (!strncmp(cur_pos, "FLAGS ", 6)) { |
| 3418 | cur_pos += 6;
|
| 3419 | if (*cur_pos != '(') { |
| 3420 | g_warning("FLAGS: *cur_pos != '('\n");
|
| 3421 | procmsg_msginfo_free(msginfo); |
| 3422 | return NULL; |
| 3423 | } |
| 3424 | cur_pos++; |
| 3425 | PARSE_ONE_ELEMENT(')');
|
| 3426 | imap_flags = imap_parse_flags(buf); |
| 3427 | } else if (!strncmp(cur_pos, "RFC822.SIZE ", 12)) { |
| 3428 | cur_pos += 12;
|
| 3429 | size = strtol(cur_pos, &cur_pos, 10);
|
| 3430 | } else if (!strncmp(cur_pos, "RFC822.HEADER", 13)) { |
| 3431 | gchar *headers; |
| 3432 | |
| 3433 | cur_pos += 13;
|
| 3434 | cur_pos = imap_get_header(session, cur_pos, &headers, |
| 3435 | line_str); |
| 3436 | if (cur_pos == NULL) { |
| 3437 | g_warning("RFC822.HEADER: cur_pos == NULL\n");
|
| 3438 | procmsg_msginfo_free(msginfo); |
| 3439 | return NULL; |
| 3440 | } |
| 3441 | if (!msginfo)
|
| 3442 | msginfo = procheader_parse_str(headers, flags, FALSE); |
| 3443 | g_free(headers); |
| 3444 | } else {
|
| 3445 | g_warning("invalid FETCH response: %s\n", cur_pos);
|
| 3446 | break;
|
| 3447 | } |
| 3448 | } |
| 3449 | |
| 3450 | #undef PARSE_ONE_ELEMENT
|
| 3451 | |
| 3452 | if (msginfo) {
|
| 3453 | msginfo->msgnum = uid; |
| 3454 | msginfo->size = size; |
| 3455 | msginfo->flags.tmp_flags |= imap_flags.tmp_flags; |
| 3456 | msginfo->flags.perm_flags = imap_flags.perm_flags; |
| 3457 | } |
| 3458 | |
| 3459 | return msginfo;
|
| 3460 | } |
| 3461 | |
| 3462 | static gint imap_msg_list_change_perm_flags(GSList *msglist, MsgPermFlags flags,
|
| 3463 | gboolean is_set) |
| 3464 | {
|
| 3465 | Folder *folder; |
| 3466 | IMAPSession *session; |
| 3467 | IMAPFlags iflags = 0;
|
| 3468 | MsgInfo *msginfo; |
| 3469 | GSList *seq_list, *cur; |
| 3470 | gint ok = IMAP_SUCCESS; |
| 3471 | |
| 3472 | if (msglist == NULL) return IMAP_SUCCESS; |
| 3473 | |
| 3474 | msginfo = (MsgInfo *)msglist->data; |
| 3475 | g_return_val_if_fail(msginfo != NULL, -1); |
| 3476 | |
| 3477 | g_return_val_if_fail(MSG_IS_IMAP(msginfo->flags), -1);
|
| 3478 | g_return_val_if_fail(msginfo->folder != NULL, -1); |
| 3479 | g_return_val_if_fail(msginfo->folder->folder != NULL, -1); |
| 3480 | |
| 3481 | folder = msginfo->folder->folder; |
| 3482 | g_return_val_if_fail(FOLDER_TYPE(folder) == F_IMAP, -1);
|
| 3483 | |
| 3484 | session = imap_session_get(folder); |
| 3485 | if (!session) return -1; |
| 3486 | |
| 3487 | ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path, |
| 3488 | NULL, NULL, NULL, NULL); |
| 3489 | if (ok != IMAP_SUCCESS)
|
| 3490 | return ok;
|
| 3491 | |
| 3492 | seq_list = imap_get_seq_set_from_msglist(msglist, 0);
|
| 3493 | |
| 3494 | if (flags & MSG_MARKED) iflags |= IMAP_FLAG_FLAGGED;
|
| 3495 | if (flags & MSG_REPLIED) iflags |= IMAP_FLAG_ANSWERED;
|
| 3496 | |
| 3497 | for (cur = seq_list; cur != NULL; cur = cur->next) { |
| 3498 | gchar *seq_set = (gchar *)cur->data; |
| 3499 | |
| 3500 | if (iflags) {
|
| 3501 | ok = imap_set_message_flags(session, seq_set, iflags, |
| 3502 | is_set); |
| 3503 | if (ok != IMAP_SUCCESS) break; |
| 3504 | } |
| 3505 | |
| 3506 | if (flags & MSG_UNREAD) {
|
| 3507 | ok = imap_set_message_flags(session, seq_set, |
| 3508 | IMAP_FLAG_SEEN, !is_set); |
| 3509 | if (ok != IMAP_SUCCESS) break; |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | imap_seq_set_free(seq_list); |
| 3514 | |
| 3515 | return ok;
|
| 3516 | } |
| 3517 | |
| 3518 | gint imap_msg_set_perm_flags(MsgInfo *msginfo, MsgPermFlags flags) |
| 3519 | {
|
| 3520 | GSList msglist; |
| 3521 | |
| 3522 | msglist.data = msginfo; |
| 3523 | msglist.next = NULL;
|
| 3524 | |
| 3525 | return imap_msg_list_change_perm_flags(&msglist, flags, TRUE);
|
| 3526 | } |
| 3527 | |
| 3528 | gint imap_msg_unset_perm_flags(MsgInfo *msginfo, MsgPermFlags flags) |
| 3529 | {
|
| 3530 | GSList msglist; |
| 3531 | |
| 3532 | msglist.data = msginfo; |
| 3533 | msglist.next = NULL;
|
| 3534 | |
| 3535 | return imap_msg_list_change_perm_flags(&msglist, flags, FALSE);
|
| 3536 | } |
| 3537 | |
| 3538 | gint imap_msg_list_set_perm_flags(GSList *msglist, MsgPermFlags flags) |
| 3539 | {
|
| 3540 | return imap_msg_list_change_perm_flags(msglist, flags, TRUE);
|
| 3541 | } |
| 3542 | |
| 3543 | gint imap_msg_list_unset_perm_flags(GSList *msglist, MsgPermFlags flags) |
| 3544 | {
|
| 3545 | return imap_msg_list_change_perm_flags(msglist, flags, FALSE);
|
| 3546 | } |
| 3547 | |
| 3548 | gint imap_msg_list_set_colorlabel_flags(GSList *msglist, guint color) |
| 3549 | {
|
| 3550 | Folder *folder; |
| 3551 | IMAPSession *session; |
| 3552 | IMAPFlags iflags = 0;
|
| 3553 | MsgInfo *msginfo; |
| 3554 | GSList *seq_list, *cur; |
| 3555 | gint ok = IMAP_SUCCESS; |
| 3556 | |
| 3557 | if (msglist == NULL) return IMAP_SUCCESS; |
| 3558 | |
| 3559 | msginfo = (MsgInfo *)msglist->data; |
| 3560 | g_return_val_if_fail(msginfo != NULL, -1); |
| 3561 | |
| 3562 | g_return_val_if_fail(MSG_IS_IMAP(msginfo->flags), -1);
|
| 3563 | g_return_val_if_fail(msginfo->folder != NULL, -1); |
| 3564 | g_return_val_if_fail(msginfo->folder->folder != NULL, -1); |
| 3565 | |
| 3566 | folder = msginfo->folder->folder; |
| 3567 | g_return_val_if_fail(FOLDER_TYPE(folder) == F_IMAP, -1);
|
| 3568 | |
| 3569 | session = imap_session_get(folder); |
| 3570 | if (!session) return -1; |
| 3571 | |
| 3572 | ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path, |
| 3573 | NULL, NULL, NULL, NULL); |
| 3574 | if (ok != IMAP_SUCCESS)
|
| 3575 | return ok;
|
| 3576 | |
| 3577 | seq_list = imap_get_seq_set_from_msglist(msglist, 0);
|
| 3578 | |
| 3579 | IMAP_SET_COLORLABEL_VALUE(iflags, color); |
| 3580 | |
| 3581 | for (cur = seq_list; cur != NULL; cur = cur->next) { |
| 3582 | gchar *seq_set = (gchar *)cur->data; |
| 3583 | |
| 3584 | ok = imap_cmd_store(session, seq_set, |
| 3585 | "-FLAGS.SILENT ($label1 $label2 $label3 $label4 $label5 $label6 $label7)");
|
| 3586 | if (ok != IMAP_SUCCESS) break; |
| 3587 | |
| 3588 | if (iflags) {
|
| 3589 | ok = imap_set_message_flags(session, seq_set, iflags, |
| 3590 | TRUE); |
| 3591 | if (ok != IMAP_SUCCESS) break; |
| 3592 | } |
| 3593 | } |
| 3594 | |
| 3595 | imap_seq_set_free(seq_list); |
| 3596 | |
| 3597 | return ok;
|
| 3598 | } |
| 3599 | |
| 3600 | static gchar *imap_get_flag_str(IMAPFlags flags)
|
| 3601 | {
|
| 3602 | GString *str; |
| 3603 | gchar *ret; |
| 3604 | guint color; |
| 3605 | |
| 3606 | str = g_string_new(NULL);
|
| 3607 | |
| 3608 | if (IMAP_IS_SEEN(flags)) g_string_append(str, "\\Seen "); |
| 3609 | if (IMAP_IS_ANSWERED(flags)) g_string_append(str, "\\Answered "); |
| 3610 | if (IMAP_IS_FLAGGED(flags)) g_string_append(str, "\\Flagged "); |
| 3611 | if (IMAP_IS_DELETED(flags)) g_string_append(str, "\\Deleted "); |
| 3612 | if (IMAP_IS_DRAFT(flags)) g_string_append(str, "\\Draft "); |
| 3613 | |
| 3614 | if ((color = IMAP_GET_COLORLABEL_VALUE(flags)) != 0) { |
| 3615 | g_string_append_printf(str, "$label%u", color);
|
| 3616 | } |
| 3617 | |
| 3618 | if (str->len > 0 && str->str[str->len - 1] == ' ') |
| 3619 | g_string_truncate(str, str->len - 1);
|
| 3620 | |
| 3621 | ret = str->str; |
| 3622 | g_string_free(str, FALSE); |
| 3623 | |
| 3624 | return ret;
|
| 3625 | } |
| 3626 | |
| 3627 | static gint imap_set_message_flags(IMAPSession *session,
|
| 3628 | const gchar *seq_set,
|
| 3629 | IMAPFlags flags, |
| 3630 | gboolean is_set) |
| 3631 | {
|
| 3632 | gchar *cmd; |
| 3633 | gchar *flag_str; |
| 3634 | gint ok; |
| 3635 | |
| 3636 | flag_str = imap_get_flag_str(flags); |
| 3637 | cmd = g_strconcat(is_set ? "+FLAGS.SILENT (" : "-FLAGS.SILENT (", |
| 3638 | flag_str, ")", NULL); |
| 3639 | g_free(flag_str); |
| 3640 | |
| 3641 | ok = imap_cmd_store(session, seq_set, cmd); |
| 3642 | g_free(cmd); |
| 3643 | |
| 3644 | return ok;
|
| 3645 | } |
| 3646 | |
| 3647 | static gint imap_select(IMAPSession *session, IMAPFolder *folder,
|
| 3648 | const gchar *path,
|
| 3649 | gint *exists, gint *recent, gint *unseen, |
| 3650 | guint32 *uid_validity) |
| 3651 | {
|
| 3652 | gchar *real_path; |
| 3653 | gint ok; |
| 3654 | gint exists_, recent_, unseen_; |
| 3655 | guint32 uid_validity_; |
| 3656 | |
| 3657 | if (!exists || !recent || !unseen || !uid_validity) {
|
| 3658 | if (session->mbox && strcmp(session->mbox, path) == 0) |
| 3659 | return IMAP_SUCCESS;
|
| 3660 | exists = &exists_; |
| 3661 | recent = &recent_; |
| 3662 | unseen = &unseen_; |
| 3663 | uid_validity = &uid_validity_; |
| 3664 | } |
| 3665 | |
| 3666 | g_free(session->mbox); |
| 3667 | session->mbox = NULL;
|
| 3668 | |
| 3669 | real_path = imap_get_real_path(folder, path); |
| 3670 | ok = imap_cmd_select(session, real_path, |
| 3671 | exists, recent, unseen, uid_validity); |
| 3672 | if (ok != IMAP_SUCCESS)
|
| 3673 | log_warning(_("can't select folder: %s\n"), real_path);
|
| 3674 | else
|
| 3675 | session->mbox = g_strdup(path); |
| 3676 | g_free(real_path); |
| 3677 | |
| 3678 | return ok;
|
| 3679 | } |
| 3680 | |
| 3681 | #define THROW(err) { ok = err; goto catch; } |
| 3682 | |
| 3683 | static gint imap_status(IMAPSession *session, IMAPFolder *folder,
|
| 3684 | const gchar *path,
|
| 3685 | gint *messages, gint *recent, |
| 3686 | guint32 *uid_next, guint32 *uid_validity, |
| 3687 | gint *unseen) |
| 3688 | {
|
| 3689 | gchar *real_path; |
| 3690 | gchar *real_path_; |
| 3691 | gint ok; |
| 3692 | GPtrArray *argbuf = NULL;
|
| 3693 | gchar *str; |
| 3694 | |
| 3695 | if (messages && recent && uid_next && uid_validity && unseen) {
|
| 3696 | *messages = *recent = *uid_next = *uid_validity = *unseen = 0;
|
| 3697 | argbuf = g_ptr_array_new(); |
| 3698 | } |
| 3699 | |
| 3700 | real_path = imap_get_real_path(folder, path); |
| 3701 | QUOTE_IF_REQUIRED(real_path_, real_path); |
| 3702 | ok = imap_cmd_gen_send(session, "STATUS %s "
|
| 3703 | "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
|
| 3704 | real_path_); |
| 3705 | if (ok != IMAP_SUCCESS) {
|
| 3706 | log_warning("error on sending imap command: STATUS\n");
|
| 3707 | THROW(ok); |
| 3708 | } |
| 3709 | ok = imap_cmd_ok(session, argbuf); |
| 3710 | if (ok != IMAP_SUCCESS)
|
| 3711 | log_warning(_("error on imap command: STATUS\n"));
|
| 3712 | if (ok != IMAP_SUCCESS || !argbuf) THROW(ok);
|
| 3713 | |
| 3714 | str = search_array_str(argbuf, "STATUS");
|
| 3715 | if (!str) THROW(IMAP_ERROR);
|
| 3716 | |
| 3717 | str = strchr(str, '(');
|
| 3718 | if (!str) THROW(IMAP_ERROR);
|
| 3719 | str++; |
| 3720 | while (*str != '\0' && *str != ')') { |
| 3721 | while (*str == ' ') str++; |
| 3722 | |
| 3723 | if (!strncmp(str, "MESSAGES ", 9)) { |
| 3724 | str += 9;
|
| 3725 | *messages = strtol(str, &str, 10);
|
| 3726 | } else if (!strncmp(str, "RECENT ", 7)) { |
| 3727 | str += 7;
|
| 3728 | *recent = strtol(str, &str, 10);
|
| 3729 | } else if (!strncmp(str, "UIDNEXT ", 8)) { |
| 3730 | str += 8;
|
| 3731 | *uid_next = strtoul(str, &str, 10);
|
| 3732 | } else if (!strncmp(str, "UIDVALIDITY ", 12)) { |
| 3733 | str += 12;
|
| 3734 | *uid_validity = strtoul(str, &str, 10);
|
| 3735 | } else if (!strncmp(str, "UNSEEN ", 7)) { |
| 3736 | str += 7;
|
| 3737 | *unseen = strtol(str, &str, 10);
|
| 3738 | } else {
|
| 3739 | g_warning("invalid STATUS response: %s\n", str);
|
| 3740 | break;
|
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | catch:
|
| 3745 | g_free(real_path); |
| 3746 | if (argbuf) {
|
| 3747 | ptr_array_free_strings(argbuf); |
| 3748 | g_ptr_array_free(argbuf, TRUE); |
| 3749 | } |
| 3750 | |
| 3751 | return ok;
|
| 3752 | } |
| 3753 | |
| 3754 | #undef THROW
|
| 3755 | |
| 3756 | static gboolean imap_has_capability(IMAPSession *session,
|
| 3757 | const gchar *capability)
|
| 3758 | {
|
| 3759 | gchar **p; |
| 3760 | |
| 3761 | for (p = session->capability; *p != NULL; ++p) { |
| 3762 | if (!g_ascii_strcasecmp(*p, capability))
|
| 3763 | return TRUE;
|
| 3764 | } |
| 3765 | |
| 3766 | return FALSE;
|
| 3767 | } |
| 3768 | |
| 3769 | static void imap_capability_free(IMAPSession *session) |
| 3770 | {
|
| 3771 | if (session->capability) {
|
| 3772 | g_strfreev(session->capability); |
| 3773 | session->capability = NULL;
|
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | |
| 3778 | /* low-level IMAP4rev1 commands */
|
| 3779 | |
| 3780 | #define THROW(err) { ok = err; goto catch; } |
| 3781 | |
| 3782 | static gint imap_cmd_capability(IMAPSession *session)
|
| 3783 | {
|
| 3784 | gint ok; |
| 3785 | GPtrArray *argbuf; |
| 3786 | gchar *capability; |
| 3787 | |
| 3788 | argbuf = g_ptr_array_new(); |
| 3789 | |
| 3790 | if ((ok = imap_cmd_gen_send(session, "CAPABILITY")) != IMAP_SUCCESS) |
| 3791 | THROW(ok); |
| 3792 | if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW(ok);
|
| 3793 | |
| 3794 | capability = search_array_str(argbuf, "CAPABILITY ");
|
| 3795 | if (!capability) THROW(IMAP_ERROR);
|
| 3796 | |
| 3797 | capability += strlen("CAPABILITY ");
|
| 3798 | |
| 3799 | imap_capability_free(session); |
| 3800 | session->capability = g_strsplit(capability, " ", -1); |
| 3801 | |
| 3802 | catch:
|
| 3803 | ptr_array_free_strings(argbuf); |
| 3804 | g_ptr_array_free(argbuf, TRUE); |
| 3805 | |
| 3806 | return ok;
|
| 3807 | } |
| 3808 | |
| 3809 | #undef THROW
|
| 3810 | |
| 3811 | static gint imap_cmd_auth_plain(IMAPSession *session, const gchar *user, |
| 3812 | const gchar *pass)
|
| 3813 | {
|
| 3814 | gchar *p; |
| 3815 | gchar *response; |
| 3816 | gchar *response64; |
| 3817 | gint ok; |
| 3818 | |
| 3819 | p = response = g_malloc(strlen(user) * 2 + 2 + strlen(pass) + 1); |
| 3820 | strcpy(p, user); |
| 3821 | p += strlen(user) + 1;
|
| 3822 | strcpy(p, user); |
| 3823 | p += strlen(user) + 1;
|
| 3824 | strcpy(p, pass); |
| 3825 | p += strlen(pass); |
| 3826 | |
| 3827 | response64 = g_malloc((p - response) * 2 + 1); |
| 3828 | base64_encode(response64, (guchar *)response, p - response); |
| 3829 | g_free(response); |
| 3830 | |
| 3831 | log_print("IMAP4> ****************\n");
|
| 3832 | sock_puts(SESSION(session)->sock, response64); |
| 3833 | ok = imap_cmd_ok(session, NULL);
|
| 3834 | if (ok != IMAP_SUCCESS)
|
| 3835 | log_warning(_("IMAP4 authentication failed.\n"));
|
| 3836 | g_free(response64); |
| 3837 | |
| 3838 | return ok;
|
| 3839 | } |
| 3840 | |
| 3841 | static gint imap_cmd_auth_cram_md5(IMAPSession *session, const gchar *user, |
| 3842 | const gchar *pass, const gchar *challenge64) |
| 3843 | {
|
| 3844 | gchar *challenge; |
| 3845 | gint challenge_len; |
| 3846 | gchar hexdigest[33];
|
| 3847 | gchar *response; |
| 3848 | gchar *response64; |
| 3849 | gint ok; |
| 3850 | |
| 3851 | challenge = g_malloc(strlen(challenge64 + 2) + 1); |
| 3852 | challenge_len = base64_decode((guchar *)challenge, challenge64 + 2, -1); |
| 3853 | challenge[challenge_len] = '\0';
|
| 3854 | log_print("IMAP< [Decoded: %s]\n", challenge);
|
| 3855 | |
| 3856 | md5_hex_hmac(hexdigest, (guchar *)challenge, challenge_len, |
| 3857 | (guchar *)pass, strlen(pass)); |
| 3858 | g_free(challenge); |
| 3859 | |
| 3860 | response = g_strdup_printf("%s %s", user, hexdigest);
|
| 3861 | log_print("IMAP> [Encoded: %s]\n", response);
|
| 3862 | response64 = g_malloc((strlen(response) + 3) * 2 + 1); |
| 3863 | base64_encode(response64, (guchar *)response, strlen(response)); |
| 3864 | g_free(response); |
| 3865 | |
| 3866 | log_print("IMAP> %s\n", response64);
|
| 3867 | sock_puts(SESSION(session)->sock, response64); |
| 3868 | ok = imap_cmd_ok(session, NULL);
|
| 3869 | if (ok != IMAP_SUCCESS)
|
| 3870 | log_warning(_("IMAP4 authentication failed.\n"));
|
| 3871 | |
| 3872 | return ok;
|
| 3873 | } |
| 3874 | |
| 3875 | static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user, |
| 3876 | const gchar *pass, IMAPAuthType type)
|
| 3877 | {
|
| 3878 | gchar *auth_type; |
| 3879 | gint ok; |
| 3880 | gchar *buf = NULL;
|
| 3881 | |
| 3882 | g_return_val_if_fail((type == 0 || type == IMAP_AUTH_CRAM_MD5 ||
|
| 3883 | type == IMAP_AUTH_PLAIN), IMAP_ERROR); |
| 3884 | |
| 3885 | if (type == IMAP_AUTH_PLAIN)
|
| 3886 | auth_type = "PLAIN";
|
| 3887 | else
|
| 3888 | auth_type = "CRAM-MD5";
|
| 3889 | |
| 3890 | ok = imap_cmd_gen_send(session, "AUTHENTICATE %s", auth_type);
|
| 3891 | if (ok != IMAP_SUCCESS) {
|
| 3892 | g_free(buf); |
| 3893 | return ok;
|
| 3894 | } |
| 3895 | ok = imap_cmd_gen_recv(session, &buf); |
| 3896 | if (ok != IMAP_SUCCESS || buf[0] != '+') { |
| 3897 | g_free(buf); |
| 3898 | return IMAP_ERROR;
|
| 3899 | } |
| 3900 | |
| 3901 | if (type == IMAP_AUTH_PLAIN)
|
| 3902 | ok = imap_cmd_auth_plain(session, user, pass); |
| 3903 | else
|
| 3904 | ok = imap_cmd_auth_cram_md5(session, user, pass, buf); |
| 3905 | |
| 3906 | g_free(buf); |
| 3907 | |
| 3908 | return ok;
|
| 3909 | } |
| 3910 | |
| 3911 | static gint imap_cmd_login(IMAPSession *session,
|
| 3912 | const gchar *user, const gchar *pass) |
| 3913 | {
|
| 3914 | gchar *user_, *pass_; |
| 3915 | gint ok; |
| 3916 | |
| 3917 | QUOTE_IF_REQUIRED(user_, user); |
| 3918 | QUOTE_IF_REQUIRED(pass_, pass); |
| 3919 | ok = imap_cmd_gen_send(session, "LOGIN %s %s", user_, pass_);
|
| 3920 | if (ok == IMAP_SUCCESS)
|
| 3921 | ok = imap_cmd_ok(session, NULL);
|
| 3922 | if (ok != IMAP_SUCCESS)
|
| 3923 | log_warning(_("IMAP4 login failed.\n"));
|
| 3924 | |
| 3925 | return ok;
|
| 3926 | } |
| 3927 | |
| 3928 | static gint imap_cmd_logout(IMAPSession *session)
|
| 3929 | {
|
| 3930 | if (imap_cmd_gen_send(session, "LOGOUT") != IMAP_SUCCESS) |
| 3931 | return IMAP_ERROR;
|
| 3932 | return imap_cmd_ok(session, NULL); |
| 3933 | } |
| 3934 | |
| 3935 | static gint imap_cmd_noop(IMAPSession *session)
|
| 3936 | {
|
| 3937 | gint ret; |
| 3938 | |
| 3939 | ret = imap_cmd_gen_send(session, "NOOP");
|
| 3940 | if (ret != IMAP_SUCCESS)
|
| 3941 | return ret;
|
| 3942 | return imap_cmd_ok(session, NULL); |
| 3943 | } |
| 3944 | |
| 3945 | #if USE_SSL
|
| 3946 | static gint imap_cmd_starttls(IMAPSession *session)
|
| 3947 | {
|
| 3948 | if (imap_cmd_gen_send(session, "STARTTLS") != IMAP_SUCCESS) |
| 3949 | return IMAP_ERROR;
|
| 3950 | return imap_cmd_ok(session, NULL); |
| 3951 | } |
| 3952 | #endif
|
| 3953 | |
| 3954 | #define THROW(err) { ok = err; goto catch; } |
| 3955 | |
| 3956 | static gint imap_cmd_namespace(IMAPSession *session, gchar **ns_str)
|
| 3957 | {
|
| 3958 | gint ok; |
| 3959 | GPtrArray *argbuf; |
| 3960 | gchar *str; |
| 3961 | |
| 3962 | argbuf = g_ptr_array_new(); |
| 3963 | |
| 3964 | if ((ok = imap_cmd_gen_send(session, "NAMESPACE")) != IMAP_SUCCESS) |
| 3965 | THROW(ok); |
| 3966 | if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW(ok);
|
| 3967 | |
| 3968 | str = search_array_str(argbuf, "NAMESPACE");
|
| 3969 | if (!str) THROW(IMAP_ERROR);
|
| 3970 | |
| 3971 | *ns_str = g_strdup(str); |
| 3972 | |
| 3973 | catch:
|
| 3974 | ptr_array_free_strings(argbuf); |
| 3975 | g_ptr_array_free(argbuf, TRUE); |
| 3976 | |
| 3977 | return ok;
|
| 3978 | } |
| 3979 | |
| 3980 | #undef THROW
|
| 3981 | |
| 3982 | static gint imap_cmd_list(IMAPSession *session, const gchar *ref, |
| 3983 | const gchar *mailbox, GPtrArray *argbuf)
|
| 3984 | {
|
| 3985 | gchar *ref_, *mailbox_; |
| 3986 | |
| 3987 | if (!ref) ref = "\"\""; |
| 3988 | if (!mailbox) mailbox = "\"\""; |
| 3989 | |
| 3990 | QUOTE_IF_REQUIRED(ref_, ref); |
| 3991 | QUOTE_IF_REQUIRED(mailbox_, mailbox); |
| 3992 | if (imap_cmd_gen_send(session, "LIST %s %s", ref_, mailbox_) != IMAP_SUCCESS) |
| 3993 | return IMAP_ERROR;
|
| 3994 | |
| 3995 | return imap_cmd_ok(session, argbuf);
|
| 3996 | } |
| 3997 | |
| 3998 | #define THROW goto catch |
| 3999 | |
| 4000 | static gint imap_cmd_do_select(IMAPSession *session, const gchar *folder, |
| 4001 | gboolean examine, |
| 4002 | gint *exists, gint *recent, gint *unseen, |
| 4003 | guint32 *uid_validity) |
| 4004 | {
|
| 4005 | gint ok; |
| 4006 | gchar *resp_str; |
| 4007 | GPtrArray *argbuf; |
| 4008 | gchar *select_cmd; |
| 4009 | gchar *folder_; |
| 4010 | guint uid_validity_; |
| 4011 | |
| 4012 | *exists = *recent = *unseen = *uid_validity = 0;
|
| 4013 | argbuf = g_ptr_array_new(); |
| 4014 | |
| 4015 | if (examine)
|
| 4016 | select_cmd = "EXAMINE";
|
| 4017 | else
|
| 4018 | select_cmd = "SELECT";
|
| 4019 | |
| 4020 | QUOTE_IF_REQUIRED(folder_, folder); |
| 4021 | if ((ok = imap_cmd_gen_send(session, "%s %s", select_cmd, folder_)) != IMAP_SUCCESS) |
| 4022 | THROW; |
| 4023 | |
| 4024 | if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW;
|
| 4025 | |
| 4026 | resp_str = search_array_contain_str(argbuf, "EXISTS");
|
| 4027 | if (resp_str) {
|
| 4028 | if (sscanf(resp_str,"%d EXISTS", exists) != 1) { |
| 4029 | g_warning("imap_cmd_select(): invalid EXISTS line.\n");
|
| 4030 | THROW; |
| 4031 | } |
| 4032 | } |
| 4033 | |
| 4034 | resp_str = search_array_contain_str(argbuf, "RECENT");
|
| 4035 | if (resp_str) {
|
| 4036 | if (sscanf(resp_str, "%d RECENT", recent) != 1) { |
| 4037 | g_warning("imap_cmd_select(): invalid RECENT line.\n");
|
| 4038 | THROW; |
| 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
|
| 4043 | if (resp_str) {
|
| 4044 | if (sscanf(resp_str, "OK [UIDVALIDITY %u] ", &uid_validity_) |
| 4045 | != 1) {
|
| 4046 | g_warning("imap_cmd_select(): invalid UIDVALIDITY line.\n");
|
| 4047 | THROW; |
| 4048 | } |
| 4049 | *uid_validity = uid_validity_; |
| 4050 | } |
| 4051 | |
| 4052 | resp_str = search_array_contain_str(argbuf, "UNSEEN");
|
| 4053 | if (resp_str) {
|
| 4054 | if (sscanf(resp_str, "OK [UNSEEN %d] ", unseen) != 1) { |
| 4055 | g_warning("imap_cmd_select(): invalid UNSEEN line.\n");
|
| 4056 | THROW; |
| 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | catch:
|
| 4061 | ptr_array_free_strings(argbuf); |
| 4062 | g_ptr_array_free(argbuf, TRUE); |
| 4063 | |
| 4064 | return ok;
|
| 4065 | } |
| 4066 | |
| 4067 | static gint imap_cmd_select(IMAPSession *session, const gchar *folder, |
| 4068 | gint *exists, gint *recent, gint *unseen, |
| 4069 | guint32 *uid_validity) |
| 4070 | {
|
| 4071 | return imap_cmd_do_select(session, folder, FALSE,
|
| 4072 | exists, recent, unseen, uid_validity); |
| 4073 | } |
| 4074 | |
| 4075 | static gint imap_cmd_examine(IMAPSession *session, const gchar *folder, |
| 4076 | gint *exists, gint *recent, gint *unseen, |
| 4077 | guint32 *uid_validity) |
| 4078 | {
|
| 4079 | return imap_cmd_do_select(session, folder, TRUE,
|
| 4080 | exists, recent, unseen, uid_validity); |
| 4081 | } |
| 4082 | |
| 4083 | #undef THROW
|
| 4084 | |
| 4085 | static gint imap_cmd_create(IMAPSession *session, const gchar *folder) |
| 4086 | {
|
| 4087 | gchar *folder_; |
| 4088 | |
| 4089 | QUOTE_IF_REQUIRED(folder_, folder); |
| 4090 | if (imap_cmd_gen_send(session, "CREATE %s", folder_) != IMAP_SUCCESS) |
| 4091 | return IMAP_ERROR;
|
| 4092 | |
| 4093 | return imap_cmd_ok(session, NULL); |
| 4094 | } |
| 4095 | |
| 4096 | static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder, |
| 4097 | const gchar *new_folder)
|
| 4098 | {
|
| 4099 | gchar *old_folder_, *new_folder_; |
| 4100 | |
| 4101 | QUOTE_IF_REQUIRED(old_folder_, old_folder); |
| 4102 | QUOTE_IF_REQUIRED(new_folder_, new_folder); |
| 4103 | if (imap_cmd_gen_send(session, "RENAME %s %s", old_folder_, new_folder_) != IMAP_SUCCESS) |
| 4104 | return IMAP_ERROR;
|
| 4105 | |
| 4106 | return imap_cmd_ok(session, NULL); |
| 4107 | } |
| 4108 | |
| 4109 | static gint imap_cmd_delete(IMAPSession *session, const gchar *folder) |
| 4110 | {
|
| 4111 | gchar *folder_; |
| 4112 | |
| 4113 | QUOTE_IF_REQUIRED(folder_, folder); |
| 4114 | if (imap_cmd_gen_send(session, "DELETE %s", folder_) != IMAP_SUCCESS) |
| 4115 | return IMAP_ERROR;
|
| 4116 | |
| 4117 | return imap_cmd_ok(session, NULL); |
| 4118 | } |
| 4119 | |
| 4120 | #define THROW(err) { ok = err; goto catch; } |
| 4121 | |
| 4122 | static gint imap_cmd_search(IMAPSession *session, const gchar *criteria, |
| 4123 | GArray **result) |
| 4124 | {
|
| 4125 | gint ok; |
| 4126 | GPtrArray *argbuf; |
| 4127 | GArray *array; |
| 4128 | gchar *str; |
| 4129 | gchar *p, *ep; |
| 4130 | gint i; |
| 4131 | guint32 uid; |
| 4132 | |
| 4133 | g_return_val_if_fail(criteria != NULL, IMAP_ERROR);
|
| 4134 | g_return_val_if_fail(result != NULL, IMAP_ERROR);
|
| 4135 | |
| 4136 | argbuf = g_ptr_array_new(); |
| 4137 | |
| 4138 | if ((ok = imap_cmd_gen_send(session, "UID SEARCH %s", criteria)) != IMAP_SUCCESS) |
| 4139 | THROW(ok); |
| 4140 | if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW(ok);
|
| 4141 | |
| 4142 | array = g_array_new(FALSE, FALSE, sizeof(guint32));
|
| 4143 | |
| 4144 | for (i = 0; i < argbuf->len; i++) { |
| 4145 | str = g_ptr_array_index(argbuf, i); |
| 4146 | if (strncmp(str, "SEARCH", 6) != 0) |
| 4147 | continue;
|
| 4148 | |
| 4149 | p = str + 6;
|
| 4150 | while (*p != '\0') { |
| 4151 | uid = strtoul(p, &ep, 10);
|
| 4152 | if (p < ep && uid > 0) { |
| 4153 | g_array_append_val(array, uid); |
| 4154 | p = ep; |
| 4155 | } else
|
| 4156 | break;
|
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | *result = array; |
| 4161 | |
| 4162 | catch:
|
| 4163 | ptr_array_free_strings(argbuf); |
| 4164 | g_ptr_array_free(argbuf, TRUE); |
| 4165 | |
| 4166 | return ok;
|
| 4167 | } |
| 4168 | |
| 4169 | typedef struct _IMAPCmdFetchData |
| 4170 | {
|
| 4171 | guint32 uid; |
| 4172 | const gchar *filename;
|
| 4173 | } IMAPCmdFetchData; |
| 4174 | |
| 4175 | #define THROW(err) { ok = err; goto catch; } |
| 4176 | |
| 4177 | static gint imap_cmd_fetch_func(IMAPSession *session, gpointer data)
|
| 4178 | {
|
| 4179 | const gchar *filename = ((IMAPCmdFetchData *)data)->filename;
|
| 4180 | gint ok; |
| 4181 | gchar *buf; |
| 4182 | gchar *cur_pos; |
| 4183 | gchar size_str[32];
|
| 4184 | glong size_num; |
| 4185 | gint ret; |
| 4186 | |
| 4187 | while ((ok = imap_cmd_gen_recv(session, &buf)) == IMAP_SUCCESS) {
|
| 4188 | if (buf[0] != '*' || buf[1] != ' ') { |
| 4189 | g_free(buf); |
| 4190 | return IMAP_ERROR;
|
| 4191 | } |
| 4192 | if (strstr(buf, "FETCH") != NULL && strstr(buf, "BODY") != NULL) |
| 4193 | break;
|
| 4194 | g_free(buf); |
| 4195 | } |
| 4196 | if (ok != IMAP_SUCCESS)
|
| 4197 | THROW(ok); |
| 4198 | |
| 4199 | #define RETURN_ERROR_IF_FAIL(cond) \
|
| 4200 | if (!(cond)) { \
|
| 4201 | g_free(buf); \ |
| 4202 | ok = imap_cmd_ok_real(session, NULL); \
|
| 4203 | THROW(IMAP_ERROR); \ |
| 4204 | } |
| 4205 | |
| 4206 | cur_pos = strchr(buf, '{');
|
| 4207 | RETURN_ERROR_IF_FAIL(cur_pos != NULL);
|
| 4208 | cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str)); |
| 4209 | RETURN_ERROR_IF_FAIL(cur_pos != NULL);
|
| 4210 | size_num = atol(size_str); |
| 4211 | RETURN_ERROR_IF_FAIL(size_num >= 0);
|
| 4212 | |
| 4213 | RETURN_ERROR_IF_FAIL(*cur_pos == '\0');
|
| 4214 | |
| 4215 | #undef RETURN_ERROR_IF_FAIL
|
| 4216 | |
| 4217 | g_free(buf); |
| 4218 | |
| 4219 | if ((ret = recv_bytes_write_to_file(SESSION(session)->sock,
|
| 4220 | size_num, filename)) != 0) {
|
| 4221 | if (ret == -2) |
| 4222 | THROW(IMAP_SOCKET); |
| 4223 | } |
| 4224 | |
| 4225 | if (imap_cmd_gen_recv(session, &buf) != IMAP_SUCCESS)
|
| 4226 | THROW(IMAP_ERROR); |
| 4227 | |
| 4228 | if (buf[0] == '\0' || buf[strlen(buf) - 1] != ')') { |
| 4229 | g_free(buf); |
| 4230 | THROW(IMAP_ERROR); |
| 4231 | } |
| 4232 | g_free(buf); |
| 4233 | |
| 4234 | ok = imap_cmd_ok_real(session, NULL);
|
| 4235 | |
| 4236 | if (ret != 0) |
| 4237 | THROW(IMAP_ERROR); |
| 4238 | |
| 4239 | catch:
|
| 4240 | return ok;
|
| 4241 | } |
| 4242 | |
| 4243 | #undef THROW
|
| 4244 | |
| 4245 | static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
|
| 4246 | const gchar *filename)
|
| 4247 | {
|
| 4248 | gint ok; |
| 4249 | IMAPCmdFetchData fetch_data = {uid, filename};
|
| 4250 | |
| 4251 | g_return_val_if_fail(filename != NULL, IMAP_ERROR);
|
| 4252 | |
| 4253 | ok = imap_cmd_gen_send(session, "UID FETCH %u BODY.PEEK[]", uid);
|
| 4254 | if (ok != IMAP_SUCCESS)
|
| 4255 | return ok;
|
| 4256 | |
| 4257 | #if USE_THREADS
|
| 4258 | ok = imap_thread_run(session, imap_cmd_fetch_func, &fetch_data); |
| 4259 | #else
|
| 4260 | ok = imap_cmd_fetch_func(session, &fetch_data); |
| 4261 | #endif
|
| 4262 | |
| 4263 | return ok;
|
| 4264 | } |
| 4265 | |
| 4266 | static void imap_get_date_time(gchar *buf, size_t len, time_t timer) |
| 4267 | {
|
| 4268 | static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
| 4269 | struct tm *lt;
|
| 4270 | gchar date_time[64];
|
| 4271 | gchar tz[6];
|
| 4272 | |
| 4273 | lt = localtime(&timer); |
| 4274 | if (lt && lt->tm_mon >= 0 && lt->tm_mon < 12) { |
| 4275 | strftime(date_time, sizeof(date_time), "%d-___-%Y %H:%M:%S", |
| 4276 | lt); |
| 4277 | tzoffset_buf(tz, &timer); |
| 4278 | memcpy(date_time + 3, monthstr + lt->tm_mon * 3, 3); |
| 4279 | g_snprintf(buf, len, "%s %s", date_time, tz);
|
| 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder, |
| 4284 | const gchar *file, IMAPFlags flags,
|
| 4285 | guint32 *new_uid) |
| 4286 | {
|
| 4287 | gint ok; |
| 4288 | MsgInfo *msginfo; |
| 4289 | MsgFlags flags_ = {0, 0};
|
| 4290 | gchar date_time[64] = ""; |
| 4291 | gint size; |
| 4292 | gchar *destfolder_; |
| 4293 | gchar *flag_str; |
| 4294 | guint new_uid_; |
| 4295 | gchar *ret = NULL;
|
| 4296 | gchar buf[BUFFSIZE]; |
| 4297 | FILE *fp; |
| 4298 | FILE *tmp; |
| 4299 | size_t read_len; |
| 4300 | GPtrArray *argbuf; |
| 4301 | gchar *resp_str; |
| 4302 | |
| 4303 | g_return_val_if_fail(file != NULL, IMAP_ERROR);
|
| 4304 | |
| 4305 | if ((fp = g_fopen(file, "rb")) == NULL) { |
| 4306 | FILE_OP_ERROR(file, "fopen");
|
| 4307 | return -1; |
| 4308 | } |
| 4309 | |
| 4310 | /* use Date: header as received date */
|
| 4311 | msginfo = procheader_parse_stream(fp, flags_, FALSE); |
| 4312 | imap_get_date_time(date_time, sizeof(date_time), msginfo->date_t);
|
| 4313 | procmsg_msginfo_free(msginfo); |
| 4314 | |
| 4315 | rewind(fp); |
| 4316 | tmp = canonicalize_file_stream(fp, &size); |
| 4317 | fclose(fp); |
| 4318 | if (!tmp)
|
| 4319 | return -1; |
| 4320 | |
| 4321 | QUOTE_IF_REQUIRED(destfolder_, destfolder); |
| 4322 | flag_str = imap_get_flag_str(flags); |
| 4323 | if (date_time[0]) |
| 4324 | ok = imap_cmd_gen_send(session, "APPEND %s (%s) \"%s\" {%d}",
|
| 4325 | destfolder_, flag_str, date_time, size); |
| 4326 | else
|
| 4327 | ok = imap_cmd_gen_send(session, "APPEND %s (%s) {%d}",
|
| 4328 | destfolder_, flag_str, size); |
| 4329 | g_free(flag_str); |
| 4330 | if (ok != IMAP_SUCCESS) {
|
| 4331 | log_warning(_("can't append %s to %s\n"), file, destfolder_);
|
| 4332 | fclose(tmp); |
| 4333 | return ok;
|
| 4334 | } |
| 4335 | |
| 4336 | ok = imap_cmd_gen_recv(session, &ret); |
| 4337 | if (ok != IMAP_SUCCESS || ret[0] != '+') { |
| 4338 | log_warning(_("can't append %s to %s\n"), file, destfolder_);
|
| 4339 | g_free(ret); |
| 4340 | fclose(tmp); |
| 4341 | return IMAP_ERROR;
|
| 4342 | } |
| 4343 | g_free(ret); |
| 4344 | |
| 4345 | log_print("IMAP4> %s\n", _("(sending file...)")); |
| 4346 | |
| 4347 | while ((read_len = fread(buf, 1, sizeof(buf), tmp)) > 0) { |
| 4348 | if (read_len < sizeof(buf) && ferror(tmp)) |
| 4349 | break;
|
| 4350 | if (sock_write_all(SESSION(session)->sock, buf, read_len) < 0) { |
| 4351 | fclose(tmp); |
| 4352 | return -1; |
| 4353 | } |
| 4354 | } |
| 4355 | |
| 4356 | if (ferror(tmp)) {
|
| 4357 | FILE_OP_ERROR(file, "fread");
|
| 4358 | fclose(tmp); |
| 4359 | return -1; |
| 4360 | } |
| 4361 | |
| 4362 | sock_puts(SESSION(session)->sock, "");
|
| 4363 | |
| 4364 | fclose(tmp); |
| 4365 | |
| 4366 | if (new_uid != NULL) |
| 4367 | *new_uid = 0;
|
| 4368 | |
| 4369 | if (new_uid != NULL && session->uidplus) { |
| 4370 | argbuf = g_ptr_array_new(); |
| 4371 | |
| 4372 | ok = imap_cmd_ok(session, argbuf); |
| 4373 | if (ok != IMAP_SUCCESS)
|
| 4374 | log_warning(_("can't append message to %s\n"),
|
| 4375 | destfolder_); |
| 4376 | else if (argbuf->len > 0) { |
| 4377 | resp_str = g_ptr_array_index(argbuf, argbuf->len - 1);
|
| 4378 | if (resp_str &&
|
| 4379 | sscanf(resp_str, "%*u OK [APPENDUID %*u %u]",
|
| 4380 | &new_uid_) == 1) {
|
| 4381 | *new_uid = new_uid_; |
| 4382 | } |
| 4383 | } |
| 4384 | |
| 4385 | ptr_array_free_strings(argbuf); |
| 4386 | g_ptr_array_free(argbuf, TRUE); |
| 4387 | } else
|
| 4388 | ok = imap_cmd_ok(session, NULL);
|
| 4389 | |
| 4390 | return ok;
|
| 4391 | } |
| 4392 | |
| 4393 | static gint imap_cmd_copy(IMAPSession *session, const gchar *seq_set, |
| 4394 | const gchar *destfolder)
|
| 4395 | {
|
| 4396 | gint ok; |
| 4397 | gchar *destfolder_; |
| 4398 | |
| 4399 | g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
|
| 4400 | |
| 4401 | QUOTE_IF_REQUIRED(destfolder_, destfolder); |
| 4402 | ok = imap_cmd_gen_send(session, "UID COPY %s %s", seq_set, destfolder_);
|
| 4403 | if (ok == IMAP_SUCCESS)
|
| 4404 | ok = imap_cmd_ok(session, NULL);
|
| 4405 | if (ok != IMAP_SUCCESS) {
|
| 4406 | log_warning(_("can't copy %s to %s\n"), seq_set, destfolder_);
|
| 4407 | return -1; |
| 4408 | } |
| 4409 | |
| 4410 | return ok;
|
| 4411 | } |
| 4412 | |
| 4413 | gint imap_cmd_envelope(IMAPSession *session, const gchar *seq_set)
|
| 4414 | {
|
| 4415 | return imap_cmd_gen_send
|
| 4416 | (session, "UID FETCH %s (UID FLAGS RFC822.SIZE RFC822.HEADER)",
|
| 4417 | seq_set); |
| 4418 | } |
| 4419 | |
| 4420 | static gint imap_cmd_store(IMAPSession *session, const gchar *seq_set, |
| 4421 | const gchar *sub_cmd)
|
| 4422 | {
|
| 4423 | gint ok; |
| 4424 | |
| 4425 | ok = imap_cmd_gen_send(session, "UID STORE %s %s", seq_set, sub_cmd);
|
| 4426 | if (ok == IMAP_SUCCESS)
|
| 4427 | ok = imap_cmd_ok(session, NULL);
|
| 4428 | if (ok != IMAP_SUCCESS) {
|
| 4429 | log_warning(_("error while imap command: STORE %s %s\n"),
|
| 4430 | seq_set, sub_cmd); |
| 4431 | return ok;
|
| 4432 | } |
| 4433 | |
| 4434 | return IMAP_SUCCESS;
|
| 4435 | } |
| 4436 | |
| 4437 | static gint imap_cmd_expunge(IMAPSession *session)
|
| 4438 | {
|
| 4439 | gint ok; |
| 4440 | |
| 4441 | ok = imap_cmd_gen_send(session, "EXPUNGE");
|
| 4442 | if (ok == IMAP_SUCCESS)
|
| 4443 | ok = imap_cmd_ok(session, NULL);
|
| 4444 | if (ok != IMAP_SUCCESS) {
|
| 4445 | log_warning(_("error while imap command: EXPUNGE\n"));
|
| 4446 | return ok;
|
| 4447 | } |
| 4448 | |
| 4449 | return IMAP_SUCCESS;
|
| 4450 | } |
| 4451 | |
| 4452 | static gint imap_cmd_close(IMAPSession *session)
|
| 4453 | {
|
| 4454 | gint ok; |
| 4455 | |
| 4456 | ok = imap_cmd_gen_send(session, "CLOSE");
|
| 4457 | if (ok == IMAP_SUCCESS)
|
| 4458 | ok = imap_cmd_ok(session, NULL);
|
| 4459 | if (ok != IMAP_SUCCESS)
|
| 4460 | log_warning(_("error while imap command: CLOSE\n"));
|
| 4461 | |
| 4462 | return ok;
|
| 4463 | } |
| 4464 | |
| 4465 | static gint imap_cmd_ok_real(IMAPSession *session, GPtrArray *argbuf)
|
| 4466 | {
|
| 4467 | gint ok; |
| 4468 | gchar *buf; |
| 4469 | gint cmd_num; |
| 4470 | gchar cmd_status[IMAPBUFSIZE + 1];
|
| 4471 | GString *str; |
| 4472 | gchar *p; |
| 4473 | gchar obuf[32];
|
| 4474 | gint len; |
| 4475 | gchar *literal; |
| 4476 | |
| 4477 | str = g_string_sized_new(256);
|
| 4478 | |
| 4479 | //g_usleep(800000);
|
| 4480 | while ((ok = imap_cmd_gen_recv(session, &buf)) == IMAP_SUCCESS) {
|
| 4481 | g_string_append(str, buf); |
| 4482 | |
| 4483 | if ((p = strrchr_with_skip_quote(buf, '"', '{'))) { |
| 4484 | /* literal */
|
| 4485 | p = strchr_cpy(p + 1, '}', obuf, sizeof(obuf)); |
| 4486 | len = atoi(obuf); |
| 4487 | if (len < 0 || p == NULL || *p != '\0') { |
| 4488 | g_free(buf); |
| 4489 | ok = IMAP_ERROR; |
| 4490 | break;
|
| 4491 | } |
| 4492 | |
| 4493 | literal = recv_bytes(SESSION(session)->sock, len); |
| 4494 | if (!literal) {
|
| 4495 | g_free(buf); |
| 4496 | ok = IMAP_SOCKET; |
| 4497 | break;
|
| 4498 | } |
| 4499 | if (memchr(literal, '\n', len)) |
| 4500 | log_print("IMAP4< (literal: %d bytes)\n", len);
|
| 4501 | else
|
| 4502 | log_print("IMAP4< %s\n", literal);
|
| 4503 | |
| 4504 | g_string_append(str, "\r\n");
|
| 4505 | g_string_append_len(str, literal, len); |
| 4506 | g_free(literal); |
| 4507 | g_free(buf); |
| 4508 | continue;
|
| 4509 | } |
| 4510 | |
| 4511 | g_free(buf); |
| 4512 | |
| 4513 | if (str->str[0] == '*' && str->str[1] == ' ') { |
| 4514 | if (argbuf)
|
| 4515 | g_ptr_array_add(argbuf, g_strdup(str->str + 2));
|
| 4516 | |
| 4517 | g_string_truncate(str, 0);
|
| 4518 | continue;
|
| 4519 | } else if (sscanf(str->str, "%d %" Xstr(IMAPBUFSIZE) "s", |
| 4520 | &cmd_num, cmd_status) < 2) {
|
| 4521 | ok = IMAP_ERROR; |
| 4522 | } else if (cmd_num == session->cmd_count && |
| 4523 | !strcmp(cmd_status, "OK")) {
|
| 4524 | if (argbuf)
|
| 4525 | g_ptr_array_add(argbuf, g_strdup(str->str)); |
| 4526 | } else {
|
| 4527 | ok = IMAP_ERROR; |
| 4528 | } |
| 4529 | |
| 4530 | break;
|
| 4531 | } |
| 4532 | |
| 4533 | g_string_free(str, TRUE); |
| 4534 | return ok;
|
| 4535 | } |
| 4536 | |
| 4537 | #if USE_THREADS
|
| 4538 | static gint imap_cmd_ok_func(IMAPSession *session, gpointer data)
|
| 4539 | {
|
| 4540 | GPtrArray *argbuf = (GPtrArray *)data; |
| 4541 | gint ok; |
| 4542 | |
| 4543 | ok = imap_cmd_ok_real(session, argbuf); |
| 4544 | return ok;
|
| 4545 | } |
| 4546 | #endif
|
| 4547 | |
| 4548 | static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
|
| 4549 | {
|
| 4550 | #if USE_THREADS
|
| 4551 | return imap_thread_run(session, imap_cmd_ok_func, argbuf);
|
| 4552 | #else
|
| 4553 | return imap_cmd_ok_real(session, argbuf);
|
| 4554 | #endif
|
| 4555 | } |
| 4556 | |
| 4557 | static gint imap_cmd_gen_send(IMAPSession *session, const gchar *format, ...) |
| 4558 | {
|
| 4559 | IMAPRealSession *real = (IMAPRealSession *)session; |
| 4560 | gchar buf[IMAPBUFSIZE]; |
| 4561 | gchar tmp[IMAPBUFSIZE]; |
| 4562 | gchar *p; |
| 4563 | va_list args; |
| 4564 | |
| 4565 | va_start(args, format); |
| 4566 | g_vsnprintf(tmp, sizeof(tmp), format, args);
|
| 4567 | va_end(args); |
| 4568 | |
| 4569 | #if USE_THREADS
|
| 4570 | if (real->is_running) {
|
| 4571 | g_warning("imap_cmd_gen_send: cannot send command because another command is already running.");
|
| 4572 | return IMAP_EAGAIN;
|
| 4573 | } |
| 4574 | #endif
|
| 4575 | |
| 4576 | session->cmd_count++; |
| 4577 | |
| 4578 | g_snprintf(buf, sizeof(buf), "%d %s\r\n", session->cmd_count, tmp); |
| 4579 | if (!g_ascii_strncasecmp(tmp, "LOGIN ", 6) && |
| 4580 | (p = strchr(tmp + 6, ' '))) { |
| 4581 | *p = '\0';
|
| 4582 | log_print("IMAP4> %d %s ********\n", session->cmd_count, tmp);
|
| 4583 | } else
|
| 4584 | log_print("IMAP4> %d %s\n", session->cmd_count, tmp);
|
| 4585 | |
| 4586 | sock_write_all(SESSION(session)->sock, buf, strlen(buf)); |
| 4587 | |
| 4588 | return IMAP_SUCCESS;
|
| 4589 | } |
| 4590 | |
| 4591 | static gint imap_cmd_gen_recv(IMAPSession *session, gchar **ret)
|
| 4592 | {
|
| 4593 | gint len; |
| 4594 | |
| 4595 | if ((len = sock_getline(SESSION(session)->sock, ret)) < 0) |
| 4596 | return IMAP_SOCKET;
|
| 4597 | |
| 4598 | strretchomp(*ret); |
| 4599 | |
| 4600 | if (len > 1000) { |
| 4601 | gchar *str; |
| 4602 | |
| 4603 | str = trim_string(*ret, 1000);
|
| 4604 | log_print("IMAP4< %s\n", str);
|
| 4605 | g_free(str); |
| 4606 | } else
|
| 4607 | log_print("IMAP4< %s\n", *ret);
|
| 4608 | |
| 4609 | session_set_access_time(SESSION(session)); |
| 4610 | |
| 4611 | return IMAP_SUCCESS;
|
| 4612 | } |
| 4613 | |
| 4614 | static gint imap_cmd_gen_recv_silent(IMAPSession *session, gchar **ret)
|
| 4615 | {
|
| 4616 | gint len; |
| 4617 | |
| 4618 | if ((len = sock_getline(SESSION(session)->sock, ret)) < 0) |
| 4619 | return IMAP_SOCKET;
|
| 4620 | |
| 4621 | strretchomp(*ret); |
| 4622 | |
| 4623 | session_set_access_time(SESSION(session)); |
| 4624 | |
| 4625 | return IMAP_SUCCESS;
|
| 4626 | } |
| 4627 | |
| 4628 | |
| 4629 | /* misc utility functions */
|
| 4630 | |
| 4631 | static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len) |
| 4632 | {
|
| 4633 | gchar *tmp; |
| 4634 | |
| 4635 | dest[0] = '\0'; |
| 4636 | tmp = strchr(src, ch); |
| 4637 | if (!tmp)
|
| 4638 | return NULL; |
| 4639 | |
| 4640 | memcpy(dest, src, MIN(tmp - src, len - 1));
|
| 4641 | dest[MIN(tmp - src, len - 1)] = '\0'; |
| 4642 | |
| 4643 | return tmp + 1; |
| 4644 | } |
| 4645 | |
| 4646 | static gchar *get_quoted(const gchar *src, gchar ch, gchar *dest, gint len) |
| 4647 | {
|
| 4648 | const gchar *p = src;
|
| 4649 | gint n = 0;
|
| 4650 | |
| 4651 | g_return_val_if_fail(*p == ch, NULL);
|
| 4652 | |
| 4653 | *dest = '\0';
|
| 4654 | p++; |
| 4655 | |
| 4656 | while (*p != '\0' && *p != ch) { |
| 4657 | if (n < len - 1) { |
| 4658 | if (*p == '\\' && *(p + 1) != '\0') |
| 4659 | p++; |
| 4660 | *dest++ = *p++; |
| 4661 | } else
|
| 4662 | p++; |
| 4663 | n++; |
| 4664 | } |
| 4665 | |
| 4666 | *dest = '\0';
|
| 4667 | return (gchar *)(*p == ch ? p + 1 : p); |
| 4668 | } |
| 4669 | |
| 4670 | static gchar *search_array_contain_str(GPtrArray *array, gchar *str)
|
| 4671 | {
|
| 4672 | gint i; |
| 4673 | |
| 4674 | for (i = 0; i < array->len; i++) { |
| 4675 | gchar *tmp; |
| 4676 | |
| 4677 | tmp = g_ptr_array_index(array, i); |
| 4678 | if (strstr(tmp, str) != NULL) |
| 4679 | return tmp;
|
| 4680 | } |
| 4681 | |
| 4682 | return NULL; |
| 4683 | } |
| 4684 | |
| 4685 | static gchar *search_array_str(GPtrArray *array, gchar *str)
|
| 4686 | {
|
| 4687 | gint i; |
| 4688 | gint len; |
| 4689 | |
| 4690 | len = strlen(str); |
| 4691 | |
| 4692 | for (i = 0; i < array->len; i++) { |
| 4693 | gchar *tmp; |
| 4694 | |
| 4695 | tmp = g_ptr_array_index(array, i); |
| 4696 | if (!strncmp(tmp, str, len))
|
| 4697 | return tmp;
|
| 4698 | } |
| 4699 | |
| 4700 | return NULL; |
| 4701 | } |
| 4702 | |
| 4703 | static void imap_path_separator_subst(gchar *str, gchar separator) |
| 4704 | {
|
| 4705 | gchar *p; |
| 4706 | gboolean in_escape = FALSE; |
| 4707 | |
| 4708 | if (!separator || separator == '/') return; |
| 4709 | |
| 4710 | for (p = str; *p != '\0'; p++) { |
| 4711 | if (*p == '/' && !in_escape) |
| 4712 | *p = separator; |
| 4713 | else if (*p == '&' && *(p + 1) != '-' && !in_escape) |
| 4714 | in_escape = TRUE; |
| 4715 | else if (*p == '-' && in_escape) |
| 4716 | in_escape = FALSE; |
| 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str) |
| 4721 | {
|
| 4722 | static iconv_t cd = (iconv_t)-1; |
| 4723 | static gboolean iconv_ok = TRUE;
|
| 4724 | GString *norm_utf7; |
| 4725 | gchar *norm_utf7_p; |
| 4726 | size_t norm_utf7_len; |
| 4727 | const gchar *p;
|
| 4728 | gchar *to_str, *to_p; |
| 4729 | size_t to_len; |
| 4730 | gboolean in_escape = FALSE; |
| 4731 | |
| 4732 | if (!iconv_ok) return g_strdup(mutf7_str); |
| 4733 | |
| 4734 | if (cd == (iconv_t)-1) { |
| 4735 | cd = iconv_open(CS_INTERNAL, CS_UTF_7); |
| 4736 | if (cd == (iconv_t)-1) { |
| 4737 | g_warning("iconv cannot convert UTF-7 to %s\n",
|
| 4738 | CS_INTERNAL); |
| 4739 | iconv_ok = FALSE; |
| 4740 | return g_strdup(mutf7_str);
|
| 4741 | } |
| 4742 | } |
| 4743 | |
| 4744 | /* modified UTF-7 to normal UTF-7 conversion */
|
| 4745 | norm_utf7 = g_string_new(NULL);
|
| 4746 | |
| 4747 | for (p = mutf7_str; *p != '\0'; p++) { |
| 4748 | /* replace: '&' -> '+',
|
| 4749 | "&-" -> '&', |
| 4750 | "+" -> "+-", |
| 4751 | escaped ',' -> '/' */ |
| 4752 | if (!in_escape && *p == '&') { |
| 4753 | if (*(p + 1) != '-') { |
| 4754 | g_string_append_c(norm_utf7, '+');
|
| 4755 | in_escape = TRUE; |
| 4756 | } else {
|
| 4757 | g_string_append_c(norm_utf7, '&');
|
| 4758 | p++; |
| 4759 | } |
| 4760 | } else if (!in_escape && *p == '+') { |
| 4761 | g_string_append(norm_utf7, "+-");
|
| 4762 | } else if (in_escape && *p == ',') { |
| 4763 | g_string_append_c(norm_utf7, '/');
|
| 4764 | } else if (in_escape && *p == '-') { |
| 4765 | g_string_append_c(norm_utf7, '-');
|
| 4766 | in_escape = FALSE; |
| 4767 | } else {
|
| 4768 | g_string_append_c(norm_utf7, *p); |
| 4769 | } |
| 4770 | } |
| 4771 | |
| 4772 | /* somehow iconv() returns error when the last of the string is "+-" */
|
| 4773 | g_string_append_c(norm_utf7, '\n');
|
| 4774 | norm_utf7_p = norm_utf7->str; |
| 4775 | norm_utf7_len = norm_utf7->len; |
| 4776 | to_len = strlen(mutf7_str) * 5;
|
| 4777 | to_p = to_str = g_malloc(to_len + 1);
|
| 4778 | |
| 4779 | if (iconv(cd, (ICONV_CONST gchar **)&norm_utf7_p, &norm_utf7_len,
|
| 4780 | &to_p, &to_len) == -1) {
|
| 4781 | g_warning(_("iconv cannot convert UTF-7 to %s\n"), CS_INTERNAL);
|
| 4782 | g_string_free(norm_utf7, TRUE); |
| 4783 | g_free(to_str); |
| 4784 | return g_strdup(mutf7_str);
|
| 4785 | } |
| 4786 | |
| 4787 | /* second iconv() call for flushing */
|
| 4788 | iconv(cd, NULL, NULL, &to_p, &to_len); |
| 4789 | g_string_free(norm_utf7, TRUE); |
| 4790 | *to_p = '\0';
|
| 4791 | strretchomp(to_str); |
| 4792 | |
| 4793 | return to_str;
|
| 4794 | } |
| 4795 | |
| 4796 | static gchar *imap_utf8_to_modified_utf7(const gchar *from) |
| 4797 | {
|
| 4798 | static iconv_t cd = (iconv_t)-1; |
| 4799 | static gboolean iconv_ok = TRUE;
|
| 4800 | gchar *norm_utf7, *norm_utf7_p; |
| 4801 | size_t from_len, norm_utf7_len; |
| 4802 | GString *to_str; |
| 4803 | const gchar *from_tmp;
|
| 4804 | const gchar *p;
|
| 4805 | gchar *to; |
| 4806 | gboolean in_escape = FALSE; |
| 4807 | |
| 4808 | if (!iconv_ok) return g_strdup(from); |
| 4809 | |
| 4810 | if (cd == (iconv_t)-1) { |
| 4811 | cd = iconv_open(CS_UTF_7, CS_INTERNAL); |
| 4812 | if (cd == (iconv_t)-1) { |
| 4813 | g_warning(_("iconv cannot convert %s to UTF-7\n"),
|
| 4814 | CS_INTERNAL); |
| 4815 | iconv_ok = FALSE; |
| 4816 | return g_strdup(from);
|
| 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | /* UTF-8 to normal UTF-7 conversion */
|
| 4821 | from_tmp = from; |
| 4822 | from_len = strlen(from); |
| 4823 | norm_utf7_len = from_len * 5;
|
| 4824 | norm_utf7 = g_malloc(norm_utf7_len + 1);
|
| 4825 | norm_utf7_p = norm_utf7; |
| 4826 | |
| 4827 | while (from_len > 0) { |
| 4828 | if (*from_tmp == '+') { |
| 4829 | *norm_utf7_p++ = '+';
|
| 4830 | *norm_utf7_p++ = '-';
|
| 4831 | norm_utf7_len -= 2;
|
| 4832 | from_tmp++; |
| 4833 | from_len--; |
| 4834 | } else if (g_ascii_isprint(*from_tmp)) { |
| 4835 | /* printable ascii char */
|
| 4836 | *norm_utf7_p = *from_tmp; |
| 4837 | norm_utf7_p++; |
| 4838 | norm_utf7_len--; |
| 4839 | from_tmp++; |
| 4840 | from_len--; |
| 4841 | } else {
|
| 4842 | size_t conv_len = 0;
|
| 4843 | |
| 4844 | /* unprintable char: convert to UTF-7 */
|
| 4845 | p = from_tmp; |
| 4846 | while (!g_ascii_isprint(*p) && conv_len < from_len) {
|
| 4847 | conv_len += g_utf8_skip[*(guchar *)p]; |
| 4848 | p += g_utf8_skip[*(guchar *)p]; |
| 4849 | } |
| 4850 | |
| 4851 | from_len -= conv_len; |
| 4852 | if (iconv(cd, (ICONV_CONST gchar **)&from_tmp,
|
| 4853 | &conv_len, |
| 4854 | &norm_utf7_p, &norm_utf7_len) == -1) {
|
| 4855 | g_warning("iconv cannot convert %s to UTF-7\n",
|
| 4856 | CS_INTERNAL); |
| 4857 | g_free(norm_utf7); |
| 4858 | return g_strdup(from);
|
| 4859 | } |
| 4860 | |
| 4861 | /* second iconv() call for flushing */
|
| 4862 | iconv(cd, NULL, NULL, &norm_utf7_p, &norm_utf7_len); |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | *norm_utf7_p = '\0';
|
| 4867 | to_str = g_string_new(NULL);
|
| 4868 | for (p = norm_utf7; p < norm_utf7_p; p++) {
|
| 4869 | /* replace: '&' -> "&-",
|
| 4870 | '+' -> '&', |
| 4871 | "+-" -> '+', |
| 4872 | BASE64 '/' -> ',' */ |
| 4873 | if (!in_escape && *p == '&') { |
| 4874 | g_string_append(to_str, "&-");
|
| 4875 | } else if (!in_escape && *p == '+') { |
| 4876 | if (*(p + 1) == '-') { |
| 4877 | g_string_append_c(to_str, '+');
|
| 4878 | p++; |
| 4879 | } else {
|
| 4880 | g_string_append_c(to_str, '&');
|
| 4881 | in_escape = TRUE; |
| 4882 | } |
| 4883 | } else if (in_escape && *p == '/') { |
| 4884 | g_string_append_c(to_str, ',');
|
| 4885 | } else if (in_escape && *p == '-') { |
| 4886 | g_string_append_c(to_str, '-');
|
| 4887 | in_escape = FALSE; |
| 4888 | } else {
|
| 4889 | g_string_append_c(to_str, *p); |
| 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | if (in_escape) {
|
| 4894 | in_escape = FALSE; |
| 4895 | g_string_append_c(to_str, '-');
|
| 4896 | } |
| 4897 | |
| 4898 | to = g_string_free(to_str, FALSE); |
| 4899 | g_free(norm_utf7); |
| 4900 | |
| 4901 | return to;
|
| 4902 | } |
| 4903 | |
| 4904 | static GSList *imap_get_seq_set_from_msglist(GSList *msglist, gint limit)
|
| 4905 | {
|
| 4906 | GString *str; |
| 4907 | GSList *sorted_list, *cur; |
| 4908 | guint first, last, next; |
| 4909 | gchar *ret_str; |
| 4910 | GSList *ret_list = NULL;
|
| 4911 | gint count = 0;
|
| 4912 | |
| 4913 | if (msglist == NULL) |
| 4914 | return NULL; |
| 4915 | |
| 4916 | str = g_string_sized_new(256);
|
| 4917 | |
| 4918 | sorted_list = g_slist_copy(msglist); |
| 4919 | sorted_list = procmsg_sort_msg_list(sorted_list, SORT_BY_NUMBER, |
| 4920 | SORT_ASCENDING); |
| 4921 | |
| 4922 | first = ((MsgInfo *)sorted_list->data)->msgnum; |
| 4923 | |
| 4924 | for (cur = sorted_list; cur != NULL; cur = cur->next) { |
| 4925 | ++count; |
| 4926 | last = ((MsgInfo *)cur->data)->msgnum; |
| 4927 | if (cur->next)
|
| 4928 | next = ((MsgInfo *)cur->next->data)->msgnum; |
| 4929 | else
|
| 4930 | next = 0;
|
| 4931 | |
| 4932 | if (limit > 0 && count >= limit) { |
| 4933 | if (str->len > 0) |
| 4934 | g_string_append_c(str, ',');
|
| 4935 | if (first == last)
|
| 4936 | g_string_sprintfa(str, "%u", first);
|
| 4937 | else
|
| 4938 | g_string_sprintfa(str, "%u:%u", first, last);
|
| 4939 | |
| 4940 | first = next; |
| 4941 | |
| 4942 | ret_str = g_strdup(str->str); |
| 4943 | ret_list = g_slist_append(ret_list, ret_str); |
| 4944 | g_string_truncate(str, 0);
|
| 4945 | count = 0;
|
| 4946 | continue;
|
| 4947 | } |
| 4948 | |
| 4949 | if (last + 1 != next || next == 0) { |
| 4950 | if (str->len > 0) |
| 4951 | g_string_append_c(str, ',');
|
| 4952 | if (first == last)
|
| 4953 | g_string_sprintfa(str, "%u", first);
|
| 4954 | else
|
| 4955 | g_string_sprintfa(str, "%u:%u", first, last);
|
| 4956 | |
| 4957 | first = next; |
| 4958 | |
| 4959 | if (str->len > IMAP_CMD_LIMIT) {
|
| 4960 | ret_str = g_strdup(str->str); |
| 4961 | ret_list = g_slist_append(ret_list, ret_str); |
| 4962 | g_string_truncate(str, 0);
|
| 4963 | } |
| 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | if (str->len > 0) { |
| 4968 | ret_str = g_strdup(str->str); |
| 4969 | ret_list = g_slist_append(ret_list, ret_str); |
| 4970 | } |
| 4971 | |
| 4972 | g_slist_free(sorted_list); |
| 4973 | g_string_free(str, TRUE); |
| 4974 | |
| 4975 | return ret_list;
|
| 4976 | } |
| 4977 | |
| 4978 | static gint imap_seq_set_get_count(const gchar *seq_set) |
| 4979 | {
|
| 4980 | gint count = 0;
|
| 4981 | guint first, last; |
| 4982 | gchar *tmp, *p, *q; |
| 4983 | |
| 4984 | p = q = tmp = g_strdup(seq_set); |
| 4985 | |
| 4986 | while (*p) {
|
| 4987 | if (*p == ',') { |
| 4988 | *p = '\0';
|
| 4989 | if (sscanf(q, "%u:%u", &first, &last) == 2) |
| 4990 | count += last - first + 1;
|
| 4991 | else if (sscanf(q, "%u", &first) == 1) |
| 4992 | count++; |
| 4993 | q = ++p; |
| 4994 | } else
|
| 4995 | ++p; |
| 4996 | } |
| 4997 | if (q != p) {
|
| 4998 | if (sscanf(q, "%u:%u", &first, &last) == 2) |
| 4999 | count += last - first + 1;
|
| 5000 | else if (sscanf(q, "%u", &first) == 1) |
| 5001 | count++; |
| 5002 | } |
| 5003 | |
| 5004 | g_free(tmp); |
| 5005 | |
| 5006 | return count;
|
| 5007 | } |
| 5008 | |
| 5009 | static void imap_seq_set_free(GSList *seq_list) |
| 5010 | {
|
| 5011 | slist_free_strings(seq_list); |
| 5012 | g_slist_free(seq_list); |
| 5013 | } |
| 5014 | |
| 5015 | static GHashTable *imap_get_uid_table(GArray *array)
|
| 5016 | {
|
| 5017 | GHashTable *table; |
| 5018 | gint i; |
| 5019 | guint32 uid; |
| 5020 | |
| 5021 | g_return_val_if_fail(array != NULL, NULL); |
| 5022 | |
| 5023 | table = g_hash_table_new(NULL, g_direct_equal);
|
| 5024 | |
| 5025 | for (i = 0; i < array->len; i++) { |
| 5026 | uid = g_array_index(array, guint32, i); |
| 5027 | g_hash_table_insert(table, GUINT_TO_POINTER(uid), |
| 5028 | GINT_TO_POINTER(i + 1));
|
| 5029 | } |
| 5030 | |
| 5031 | return table;
|
| 5032 | } |
| 5033 | |
| 5034 | static gboolean imap_rename_folder_func(GNode *node, gpointer data)
|
| 5035 | {
|
| 5036 | FolderItem *item = node->data; |
| 5037 | gchar **paths = data; |
| 5038 | const gchar *oldpath = paths[0]; |
| 5039 | const gchar *newpath = paths[1]; |
| 5040 | gchar *base; |
| 5041 | gchar *new_itempath; |
| 5042 | gint oldpathlen; |
| 5043 | |
| 5044 | oldpathlen = strlen(oldpath); |
| 5045 | if (strncmp(oldpath, item->path, oldpathlen) != 0) { |
| 5046 | g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
|
| 5047 | return TRUE;
|
| 5048 | } |
| 5049 | |
| 5050 | base = item->path + oldpathlen; |
| 5051 | while (*base == '/') base++; |
| 5052 | if (*base == '\0') |
| 5053 | new_itempath = g_strdup(newpath); |
| 5054 | else
|
| 5055 | new_itempath = g_strconcat(newpath, "/", base, NULL); |
| 5056 | g_free(item->path); |
| 5057 | item->path = new_itempath; |
| 5058 | |
| 5059 | return FALSE;
|
| 5060 | } |
| 5061 | |
| 5062 | #if USE_THREADS
|
| 5063 | static void imap_thread_run_proxy(gpointer push_data, gpointer data) |
| 5064 | {
|
| 5065 | IMAPRealSession *real = (IMAPRealSession *)data; |
| 5066 | |
| 5067 | debug_print("imap_thread_run_proxy (%p): calling thread_func\n", g_thread_self());
|
| 5068 | real->retval = real->thread_func(IMAP_SESSION(real), real->thread_data); |
| 5069 | g_atomic_int_set(&real->flag, 1);
|
| 5070 | g_main_context_wakeup(NULL);
|
| 5071 | debug_print("imap_thread_run_proxy (%p): thread_func done\n", g_thread_self());
|
| 5072 | } |
| 5073 | |
| 5074 | static gint imap_thread_run(IMAPSession *session, IMAPThreadFunc func,
|
| 5075 | gpointer data) |
| 5076 | {
|
| 5077 | IMAPRealSession *real = (IMAPRealSession *)session; |
| 5078 | gint ret; |
| 5079 | |
| 5080 | if (real->is_running) {
|
| 5081 | g_warning("imap_thread_run: thread is already running");
|
| 5082 | return IMAP_ERROR;
|
| 5083 | } |
| 5084 | |
| 5085 | if (!real->pool) {
|
| 5086 | real->pool = g_thread_pool_new(imap_thread_run_proxy, real, |
| 5087 | -1, FALSE, NULL); |
| 5088 | if (!real->pool)
|
| 5089 | return IMAP_ERROR;
|
| 5090 | } |
| 5091 | |
| 5092 | real->is_running = TRUE; |
| 5093 | real->thread_func = func; |
| 5094 | real->thread_data = data; |
| 5095 | real->flag = 0;
|
| 5096 | real->retval = 0;
|
| 5097 | |
| 5098 | g_thread_pool_push(real->pool, real, NULL);
|
| 5099 | |
| 5100 | while (g_atomic_int_get(&real->flag) == 0) |
| 5101 | event_loop_iterate(); |
| 5102 | |
| 5103 | real->is_running = FALSE; |
| 5104 | real->thread_func = NULL;
|
| 5105 | real->thread_data = NULL;
|
| 5106 | real->flag = 0;
|
| 5107 | ret = real->retval; |
| 5108 | real->retval = 0;
|
| 5109 | log_flush(); |
| 5110 | |
| 5111 | return ret;
|
| 5112 | } |
| 5113 | |
| 5114 | static gint imap_thread_run_progress(IMAPSession *session, IMAPThreadFunc func,
|
| 5115 | IMAPProgressFunc progress_func, |
| 5116 | gpointer data) |
| 5117 | {
|
| 5118 | IMAPRealSession *real = (IMAPRealSession *)session; |
| 5119 | gint prev_count = 0;
|
| 5120 | gint ret; |
| 5121 | |
| 5122 | if (real->is_running) {
|
| 5123 | g_warning("imap_thread_run: thread is already running");
|
| 5124 | return IMAP_ERROR;
|
| 5125 | } |
| 5126 | |
| 5127 | if (!real->pool) {
|
| 5128 | real->pool = g_thread_pool_new(imap_thread_run_proxy, real, |
| 5129 | -1, FALSE, NULL); |
| 5130 | if (!real->pool)
|
| 5131 | return IMAP_ERROR;
|
| 5132 | } |
| 5133 | |
| 5134 | real->is_running = TRUE; |
| 5135 | real->thread_func = func; |
| 5136 | real->thread_data = data; |
| 5137 | real->flag = 0;
|
| 5138 | real->retval = 0;
|
| 5139 | real->prog_count = 0;
|
| 5140 | real->prog_total = 0;
|
| 5141 | |
| 5142 | g_thread_pool_push(real->pool, real, NULL);
|
| 5143 | |
| 5144 | while (g_atomic_int_get(&real->flag) == 0) { |
| 5145 | event_loop_iterate(); |
| 5146 | if (prev_count != real->prog_count && real->prog_total > 0) { |
| 5147 | progress_func(session, real->prog_count, |
| 5148 | real->prog_total, data); |
| 5149 | prev_count = real->prog_count; |
| 5150 | } |
| 5151 | } |
| 5152 | |
| 5153 | real->is_running = FALSE; |
| 5154 | real->thread_func = NULL;
|
| 5155 | real->thread_data = NULL;
|
| 5156 | real->flag = 0;
|
| 5157 | ret = real->retval; |
| 5158 | real->retval = 0;
|
| 5159 | real->prog_count = 0;
|
| 5160 | real->prog_total = 0;
|
| 5161 | log_flush(); |
| 5162 | |
| 5163 | return ret;
|
| 5164 | } |
| 5165 | #endif /* USE_THREADS */ |
| 5166 | |
| 5167 | gboolean imap_is_session_active(IMAPFolder *folder) |
| 5168 | {
|
| 5169 | #if USE_THREADS
|
| 5170 | IMAPRealSession *real; |
| 5171 | |
| 5172 | g_return_val_if_fail(folder != NULL, FALSE);
|
| 5173 | |
| 5174 | real = (IMAPRealSession *)(REMOTE_FOLDER(folder)->session); |
| 5175 | if (!real)
|
| 5176 | return FALSE;
|
| 5177 | |
| 5178 | return real->is_running;
|
| 5179 | #else
|
| 5180 | return FALSE;
|
| 5181 | #endif
|
| 5182 | } |