dovecot: Moved STATUS handling code to imap-status.[ch]
dovecot at dovecot.org
dovecot at dovecot.org
Sun Jul 15 10:00:09 EEST 2007
details: http://hg.dovecot.org/dovecot/rev/2d815150cf57
changeset: 6016:2d815150cf57
user: Timo Sirainen <tss at iki.fi>
date: Sun Jul 15 09:33:54 2007 +0300
description:
Moved STATUS handling code to imap-status.[ch]
diffstat:
4 files changed, 127 insertions(+), 103 deletions(-)
src/imap/Makefile.am | 2
src/imap/cmd-status.c | 108 ++----------------------------------------------
src/imap/imap-status.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++
src/imap/imap-status.h | 14 ++++++
diffs (291 lines):
diff -r 1208344cc1df -r 2d815150cf57 src/imap/Makefile.am
--- a/src/imap/Makefile.am Sun Jul 15 09:25:12 2007 +0300
+++ b/src/imap/Makefile.am Sun Jul 15 09:33:54 2007 +0300
@@ -81,6 +81,7 @@ imap_SOURCES = \
imap-messageset.c \
imap-search.c \
imap-sort.c \
+ imap-status.c \
imap-sync.c \
imap-thread.c \
mail-storage-callbacks.c \
@@ -97,6 +98,7 @@ headers = \
imap-messageset.h \
imap-search.h \
imap-sort.h \
+ imap-status.h \
imap-sync.h \
imap-thread.h
diff -r 1208344cc1df -r 2d815150cf57 src/imap/cmd-status.c
--- a/src/imap/cmd-status.c Sun Jul 15 09:25:12 2007 +0300
+++ b/src/imap/cmd-status.c Sun Jul 15 09:33:54 2007 +0300
@@ -1,82 +1,9 @@
/* Copyright (C) 2002 Timo Sirainen */
#include "common.h"
-#include "str.h"
-#include "imap-quote.h"
#include "commands.h"
#include "imap-sync.h"
-
-/* Returns status items, or -1 if error */
-static enum mailbox_status_items
-get_status_items(struct client_command_context *cmd,
- const struct imap_arg *args)
-{
- const char *item;
- enum mailbox_status_items items;
-
- items = 0;
- for (; args->type != IMAP_ARG_EOL; args++) {
- if (args->type != IMAP_ARG_ATOM) {
- /* list may contain only atoms */
- client_send_command_error(cmd,
- "Status list contains non-atoms.");
- return -1;
- }
-
- item = t_str_ucase(IMAP_ARG_STR(args));
-
- if (strcmp(item, "MESSAGES") == 0)
- items |= STATUS_MESSAGES;
- else if (strcmp(item, "RECENT") == 0)
- items |= STATUS_RECENT;
- else if (strcmp(item, "UIDNEXT") == 0)
- items |= STATUS_UIDNEXT;
- else if (strcmp(item, "UIDVALIDITY") == 0)
- items |= STATUS_UIDVALIDITY;
- else if (strcmp(item, "UNSEEN") == 0)
- items |= STATUS_UNSEEN;
- else {
- client_send_tagline(cmd, t_strconcat(
- "BAD Invalid status item ", item, NULL));
- return -1;
- }
- }
-
- return items;
-}
-
-static bool
-get_mailbox_status(struct client *client, struct mail_storage *storage,
- const char *mailbox, enum mailbox_status_items items,
- struct mailbox_status *status)
-{
- struct mailbox *box;
- bool failed = FALSE;
-
- if (client->mailbox != NULL &&
- mailbox_equals(client->mailbox, storage, mailbox)) {
- /* this mailbox is selected */
- box = client->mailbox;
- } else {
- /* open the mailbox */
- box = mailbox_open(storage, mailbox, NULL, MAILBOX_OPEN_FAST |
- MAILBOX_OPEN_READONLY |
- MAILBOX_OPEN_KEEP_RECENT);
- if (box == NULL)
- return FALSE;
-
- if (imap_sync_nonselected(box, 0) < 0)
- failed = TRUE;
- }
-
- if (!failed)
- failed = mailbox_get_status(box, items, status) < 0;
-
- if (box != client->mailbox)
- mailbox_close(&box);
-
- return !failed;
-}
+#include "imap-status.h"
bool cmd_status(struct client_command_context *cmd)
{
@@ -86,7 +13,6 @@ bool cmd_status(struct client_command_co
enum mailbox_status_items items;
struct mail_storage *storage;
const char *mailbox, *real_mailbox;
- string_t *str;
/* <mailbox> <status items> */
if (!client_read_args(cmd, 2, 0, &args))
@@ -99,44 +25,20 @@ bool cmd_status(struct client_command_co
}
/* get the items client wants */
- items = get_status_items(cmd, IMAP_ARG_LIST_ARGS(&args[1]));
- if (items == (enum mailbox_status_items)-1) {
- /* error */
+ if (imap_status_parse_items(cmd, IMAP_ARG_LIST_ARGS(&args[1]),
+ &items) < 0)
return TRUE;
- }
storage = client_find_storage(cmd, &real_mailbox);
if (storage == NULL)
return TRUE;
- /* get status */
- if (!get_mailbox_status(client, storage, real_mailbox,
- items, &status)) {
+ if (!imap_status_get(client, storage, real_mailbox, items, &status)) {
client_send_storage_error(cmd, storage);
return TRUE;
}
- str = t_str_new(128);
- str_append(str, "* STATUS ");
- imap_quote_append_string(str, mailbox, FALSE);
- str_append(str, " (");
-
- if (items & STATUS_MESSAGES)
- str_printfa(str, "MESSAGES %u ", status.messages);
- if (items & STATUS_RECENT)
- str_printfa(str, "RECENT %u ", status.recent);
- if (items & STATUS_UIDNEXT)
- str_printfa(str, "UIDNEXT %u ", status.uidnext);
- if (items & STATUS_UIDVALIDITY)
- str_printfa(str, "UIDVALIDITY %u ", status.uidvalidity);
- if (items & STATUS_UNSEEN)
- str_printfa(str, "UNSEEN %u ", status.unseen);
-
- if (items != 0)
- str_truncate(str, str_len(str)-1);
- str_append_c(str, ')');
-
- client_send_line(client, str_c(str));
+ imap_status_send(client, mailbox, items, &status);
client_send_tagline(cmd, "OK Status completed.");
return TRUE;
diff -r 1208344cc1df -r 2d815150cf57 src/imap/imap-status.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/imap/imap-status.c Sun Jul 15 09:33:54 2007 +0300
@@ -0,0 +1,106 @@
+/* Copyright (C) 2002-2007 Timo Sirainen */
+
+#include "common.h"
+#include "str.h"
+#include "imap-quote.h"
+#include "imap-status.h"
+
+int imap_status_parse_items(struct client_command_context *cmd,
+ const struct imap_arg *args,
+ enum mailbox_status_items *items_r)
+{
+ const char *item;
+ enum mailbox_status_items items;
+
+ items = 0;
+ for (; args->type != IMAP_ARG_EOL; args++) {
+ if (args->type != IMAP_ARG_ATOM) {
+ /* list may contain only atoms */
+ client_send_command_error(cmd,
+ "Status list contains non-atoms.");
+ return -1;
+ }
+
+ item = t_str_ucase(IMAP_ARG_STR(args));
+
+ if (strcmp(item, "MESSAGES") == 0)
+ items |= STATUS_MESSAGES;
+ else if (strcmp(item, "RECENT") == 0)
+ items |= STATUS_RECENT;
+ else if (strcmp(item, "UIDNEXT") == 0)
+ items |= STATUS_UIDNEXT;
+ else if (strcmp(item, "UIDVALIDITY") == 0)
+ items |= STATUS_UIDVALIDITY;
+ else if (strcmp(item, "UNSEEN") == 0)
+ items |= STATUS_UNSEEN;
+ else {
+ client_send_tagline(cmd, t_strconcat(
+ "BAD Invalid status item ", item, NULL));
+ return -1;
+ }
+ }
+
+ *items_r = items;
+ return 0;
+}
+
+bool imap_status_get(struct client *client, struct mail_storage *storage,
+ const char *mailbox, enum mailbox_status_items items,
+ struct mailbox_status *status_r)
+{
+ struct mailbox *box;
+ bool failed = FALSE;
+
+ if (client->mailbox != NULL &&
+ mailbox_equals(client->mailbox, storage, mailbox)) {
+ /* this mailbox is selected */
+ box = client->mailbox;
+ } else {
+ /* open the mailbox */
+ box = mailbox_open(storage, mailbox, NULL, MAILBOX_OPEN_FAST |
+ MAILBOX_OPEN_READONLY |
+ MAILBOX_OPEN_KEEP_RECENT);
+ if (box == NULL)
+ return FALSE;
+
+ if (imap_sync_nonselected(box, 0) < 0)
+ failed = TRUE;
+ }
+
+ if (!failed)
+ failed = mailbox_get_status(box, items, status_r) < 0;
+
+ if (box != client->mailbox)
+ mailbox_close(&box);
+
+ return !failed;
+}
+
+void imap_status_send(struct client *client, const char *mailbox,
+ enum mailbox_status_items items,
+ const struct mailbox_status *status)
+{
+ string_t *str;
+
+ str = t_str_new(128);
+ str_append(str, "* STATUS ");
+ imap_quote_append_string(str, mailbox, FALSE);
+ str_append(str, " (");
+
+ if (items & STATUS_MESSAGES)
+ str_printfa(str, "MESSAGES %u ", status->messages);
+ if (items & STATUS_RECENT)
+ str_printfa(str, "RECENT %u ", status->recent);
+ if (items & STATUS_UIDNEXT)
+ str_printfa(str, "UIDNEXT %u ", status->uidnext);
+ if (items & STATUS_UIDVALIDITY)
+ str_printfa(str, "UIDVALIDITY %u ", status->uidvalidity);
+ if (items & STATUS_UNSEEN)
+ str_printfa(str, "UNSEEN %u ", status->unseen);
+
+ if (items != 0)
+ str_truncate(str, str_len(str)-1);
+ str_append_c(str, ')');
+
+ client_send_line(client, str_c(str));
+}
diff -r 1208344cc1df -r 2d815150cf57 src/imap/imap-status.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/imap/imap-status.h Sun Jul 15 09:33:54 2007 +0300
@@ -0,0 +1,14 @@
+#ifndef __IMAP_STATUS_H
+#define __IMAP_STATUS_H
+
+int imap_status_parse_items(struct client_command_context *cmd,
+ const struct imap_arg *args,
+ enum mailbox_status_items *items_r);
+bool imap_status_get(struct client *client, struct mail_storage *storage,
+ const char *mailbox, enum mailbox_status_items items,
+ struct mailbox_status *status_r);
+void imap_status_send(struct client *client, const char *mailbox,
+ enum mailbox_status_items items,
+ const struct mailbox_status *status);
+
+#endif
More information about the dovecot-cvs
mailing list