status.c
4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
static int status_messages __P ((mailbox_t));
static int status_recent __P ((mailbox_t));
static int status_uidnext __P ((mailbox_t));
static int status_uidvalidity __P ((mailbox_t));
static int status_unseen __P ((mailbox_t));
int
imap4d_status (struct imap4d_command *command, char *arg)
{
char *sp = NULL;
char *name;
char *mailbox_name;
const char *delim = "/";
mailbox_t smbox = NULL;
int status;
if (! (command->states & state))
return util_finish (command, RESP_BAD, "Wrong state");
name = util_getword (arg, &sp);
util_unquote (&name);
if (!name || *name == '\0' || !sp || *sp == '\0')
return util_finish (command, RESP_BAD, "Too few args");
if (strcasecmp (name, "INBOX") == 0)
{
struct passwd *pw = getpwuid (getuid());
mailbox_name = strdup ((pw) ? pw->pw_name : "");
}
else
mailbox_name = util_getfullpath (name, delim);
status = mailbox_create_default (&smbox, mailbox_name);
if (status == 0)
{
status = mailbox_open (smbox, MU_STREAM_READ);
if (status == 0)
{
char item[32];
util_send ("* STATUS %s (", name);
item[0] = '\0';
/* Get the status item names. */
while (*sp && *sp != ')')
{
int err = 1;
util_token (item, sizeof (item), &sp);
if (strcasecmp (item, "MESSAGES") == 0)
err = status_messages (smbox);
else if (strcasecmp (item, "RECENT") == 0)
err = status_recent (smbox);
else if (strcasecmp (item, "UIDNEXT") == 0)
err = status_uidnext (smbox);
else if (strcasecmp (item, "UIDVALIDITY") == 0)
err = status_uidvalidity (smbox);
else if (strcasecmp (item, "UNSEEN") == 0)
err = status_unseen (smbox);
if (!err)
util_send (" ");
}
util_send (")\r\n");
mailbox_close (smbox);
}
mailbox_destroy (&smbox);
}
free (mailbox_name);
if (status == 0)
return util_finish (command, RESP_OK, "Completed");
return util_finish (command, RESP_NO, "Error opening mailbox");
}
static int
status_messages (mailbox_t smbox)
{
size_t total = 0;
mailbox_messages_count (smbox, &total);
util_send ("MESSAGES %u", total);
return 0;
}
static int
status_recent (mailbox_t smbox)
{
size_t recent = 0;
mailbox_messages_recent (smbox, &recent);
util_send ("RECENT %u", recent);
return 0;
}
static int
status_uidnext (mailbox_t smbox)
{
size_t uidnext = 1;
mailbox_uidnext (smbox, &uidnext);
util_send ("UIDNEXT %u", uidnext);
return 0;
}
static int
status_uidvalidity (mailbox_t smbox)
{
unsigned long uidvalidity = 0;
mailbox_uidvalidity (smbox, &uidvalidity);
util_send ("UIDVALIDITY %u", uidvalidity);
return 0;
}
/* Note that unlike the unseen response code, which indicates the message
number of the first unseen message, the unseeen item in the response the
status command indicates the quantity of unseen messages. */
static int
status_unseen (mailbox_t smbox)
{
size_t total = 0;
size_t unseen = 0;
size_t i;
mailbox_messages_count (smbox, &total);
for (i = 1; i <= total; i++)
{
message_t msg = NULL;
attribute_t attr = NULL;
mailbox_get_message (smbox, i, &msg);
message_get_attribute (msg, &attr);
if (!attribute_is_seen (attr))
unseen++;
}
util_send ("UNSEEN %d", unseen);
return 0;
}