id.c
4.33 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2008, 2009 Free Software Foundation, Inc.
GNU Mailutils 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 3, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
/* Implementation of ID extension (RFC 2971) */
#include "imap4d.h"
#include <sys/utsname.h>
mu_list_t imap4d_id_list;
static int
eat_args (imap4d_tokbuf_t tok)
{
int n = IMAP4_ARG_1;
char *p;
p = imap4d_tokbuf_getarg (tok, n++);
if (!p)
return RESP_BAD;
if (mu_c_strcasecmp (p, "NIL") == 0)
{
if (imap4d_tokbuf_getarg (tok, n))
return RESP_BAD;
return RESP_OK;
}
else if (p[0] != '(')
return RESP_BAD;
/* Collect arguments */
while ((p = imap4d_tokbuf_getarg (tok, n++)))
{
if (p[0] == ')')
{
if (imap4d_tokbuf_getarg (tok, n))
return RESP_BAD;
return RESP_OK;
}
}
return RESP_BAD;
}
struct id_value
{
char *name;
char *value;
const char *(*fun) (struct id_value *idv);
};
static const char *
get_os (struct id_value *idv)
{
struct utsname uts;
uname(&uts);
return idv->value = mu_strdup (uts.sysname);
}
static const char *
get_os_version (struct id_value *idv)
{
struct utsname uts;
uname(&uts);
return idv->value = mu_strdup (uts.version);
}
static const char *
get_command (struct id_value *idv MU_ARG_UNUSED)
{
return mu_program_name;
}
static char *
build_str (char **argv)
{
size_t size = 0;
int i, j;
char *buf, *p;
for (j = 0; argv[j]; j++)
{
size_t len = strlen (argv[j]);
if (size + len + 1 > 1024)
break;
size += len + 1;
}
buf = mu_alloc (size);
for (i = 0, p = buf; argv[i];)
{
strcpy (p, argv[i]);
p += strlen (p);
if (++i < j)
*p++ = ' ';
else
break;
}
*p = 0;
return buf;
}
static const char *
get_arguments (struct id_value *idv)
{
return idv->value = build_str (imap4d_argv + 1);
}
static const char *
get_environment (struct id_value *idv)
{
extern char **environ;
return idv->value = build_str (environ);
}
static struct id_value id_tab[] = {
{ "name", PACKAGE_NAME },
{ "version", PACKAGE_VERSION },
{ "vendor", "GNU" },
{ "support-url", "http://www.gnu.org/software/mailutils" },
{ "address",
"51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA" },
#if 0
/* FIXME */
{ "date", NULL },
#endif
{ "os", NULL, get_os },
{ "os-version", NULL, get_os_version },
{ "command", NULL, get_command },
{ "arguments", NULL, get_arguments },
{ "environment", NULL, get_environment },
{ NULL }
};
static const char *
get_id_value (const char *name)
{
struct id_value *idv;
const char *val = NULL;
for (idv = id_tab; idv->name; idv++)
{
if (strcmp (idv->name, name) == 0)
{
if (idv->value)
val = idv->value;
else if (idv->fun)
val = idv->fun (idv);
break;
}
}
return val;
}
int
imap4d_id (struct imap4d_command *command, imap4d_tokbuf_t tok)
{
int rc = eat_args (tok);
if (rc != RESP_OK)
return util_finish (command, rc, "Syntax error");
if (imap4d_id_list)
{
mu_iterator_t itr;
mu_list_get_iterator (imap4d_id_list, &itr);
int i;
int outcnt = 0;
for (i = 0, mu_iterator_first (itr);
i < 30 && !mu_iterator_is_done (itr);
i++, mu_iterator_next (itr))
{
const char *p, *q;
size_t len;
mu_iterator_current (itr, (void**)&p);
len = strcspn (p, "=");
if (p[len])
q = p + len + 1;
else
q = get_id_value (p);
if (q)
{
if (outcnt++ == 0)
util_send ("* ID (");
else
util_send (" ");
util_send ("\"%*.*s\" \"%s\"", (int) len, (int) len, p, q);
}
}
mu_iterator_destroy (&itr);
if (outcnt)
util_send (")\r\n");
}
return util_finish (command, RESP_OK, "Completed");
}