util.c
4.13 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
/* 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"
extern int ifile;
extern int ofile;
int
util_out (char *seq, int tag, char *f, ...)
{
char *buf = NULL;
int len = 0;
va_list ap;
va_start (ap, f);
len = vasprintf (&buf, f, ap);
if (tag == TAG_SEQ)
{
write (ofile, seq, strlen (seq));
write (ofile, " ", 1);
}
else if (tag == TAG_NONE)
write (ofile, "* ", 2);
write (ofile, buf, len);
write (ofile, "\r\n", 2);
free (buf);
return 0;
}
int
util_finish (int argc, char **argv, int resp, char *rc, char *f, ...)
{
char *buf = NULL, *buf2 = NULL, *code = NULL;
/* FIXME: No argc maybe 1, then it will sigsegv. */
/* struct imap4d_command *cmd = util_getcommand (argv[1]); */
int len = 0;
va_list ap;
va_start (ap, f);
switch (resp)
{
case RESP_OK:
code = strdup ("OK");
/* set state (cmd->success); */
break;
case RESP_BAD:
code = strdup ("BAD");
break;
case RESP_NO:
code = strdup ("NO");
/* set state (cmd->failure); */
break;
default:
code = strdup ("X-BUG");
}
vasprintf (&buf, f, ap);
if (rc != NULL)
len = asprintf (&buf, "%s %s %s %s %s\r\n",
argv[0], code, rc, (argc > 1) ? argv[1] : "", buf);
else
len = asprintf (&buf2, "%s %s %s %s\r\n",
argv[0], code, (argc > 1) ? argv[1] : "", buf);
write (ofile, buf2, len);
free (buf);
free (buf2);
free (code);
free (rc);
argcv_free (argc, argv);
return resp;
}
char *
util_getline (void)
{
fd_set rfds;
struct timeval tv;
char buf[1024], *ret = NULL;
int fd = ifile, len = 0;
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
tv.tv_sec = 30;
tv.tv_usec = 0;
memset (buf, '\0', 1024);
do
{
if (!select (fd + 1, &rfds, NULL, NULL, &tv))
return NULL;
else if (read (fd, buf, 1024) < 1)
exit (1); /* FIXME: dead socket */
else if (ret == NULL)
{
ret = malloc ((strlen (buf + 1) * sizeof (char)));
strcpy (ret, buf);
}
else
{
ret = realloc (ret, (strlen (ret) + strlen (buf) + 1) *
sizeof (char));
strcat (ret, buf);
}
}
while (strchr (buf, '\n') == NULL);
for (len = strlen (ret); len > 0; len--)
if (ret[len] == '\r' || ret[len] == '\n')
ret[len] = '\0';
return ret;
}
int
util_do_command (char *cmd)
{
int argc = 0, i = 0;
char **argv = NULL;
struct imap4d_command *command = NULL;
if (cmd == NULL)
return 0;
if (argcv_get (cmd, &argc, &argv) != 0)
{
argcv_free (argc, argv);
return 1;
}
util_start (argv[0]);
if (argc < 2)
return util_finish (argc, argv, RESP_BAD, NULL, "No space after tag");
command = util_getcommand (argv[1]);
for (i=0; i < strlen (argv[1]); i++)
argv[1][i] = toupper (argv[1][i]);
if (command == NULL)
return util_finish (argc, argv, RESP_BAD, NULL, "Invalid command");
else if (! (command->states & util_getstate ()))
return util_finish (argc, argv, RESP_BAD, NULL, "Incorrect state");
return command->func (argc, argv);
}
int
util_start (char *seq)
{
return 0;
}
int
util_getstate (void)
{
return STATE_NONAUTH;
}
struct imap4d_command *
util_getcommand (char *cmd)
{
int i = 0, len = strlen (cmd);
for (i = 0; imap4d_command_table[i].name != 0; i++)
{
if (strlen (imap4d_command_table[i].name) == len &&
!strcasecmp (imap4d_command_table[i].name, cmd))
return &imap4d_command_table[i];
}
return NULL;
}