summaryrefslogtreecommitdiff
path: root/src/qmail-pop3d.c
blob: 2d26c1624a469824c09e25b9bd5d06e860c489b1 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "commands.h"
#include "sig.h"
#include "getln.h"
#include "stralloc.h"
#include "buffer.h"
#include "alloc.h"
#include "open.h"
#include "prioq.h"
#include "scan.h"
#include "fmt.h"
#include "str.h"
#include "exit.h"
#include "maildir.h"
#include "timeout.h"

#define FDIN 0
#define FDOUT 1

int rename(const char *,const char *); // stdio.h

void die() { _exit(0); }

ssize_t saferead(int fd,char *buf,int len)
{
  int r;
  r = timeoutread(1200,fd,buf,len);
  if (r <= 0) die();
  return r;
}

ssize_t safewrite(int fd,char *buf,int len)
{
  int r;
  r = timeoutwrite(1200,fd,buf,len);
  if (r <= 0) die();
  return r;
}

char outbuf[1024];
buffer bo = BUFFER_INIT(safewrite,FDOUT,outbuf,sizeof(outbuf));

char inbuf[128];
buffer bi = BUFFER_INIT(saferead,FDIN,inbuf,sizeof(inbuf));

void out(char *buf,int len)
{
  buffer_put(&bo,buf,len);
}
void outs(char *s)
{
  buffer_puts(&bo,s);
}
void flush()
{
  buffer_flush(&bo);
}
void err(char *s)
{
  outs("-ERR ");
  outs(s);
  outs("\r\n");
  flush();
}

void die_nomem() { err("out of memory"); die(); }
void die_nomaildir() { err("this user has no $HOME/Maildir"); die(); }
void die_scan() { err("unable to scan $HOME/Maildir"); die(); }

void err_syntax() { err("syntax error"); }
void err_unimpl() { err("unimplemented"); }
void err_deleted() { err("already deleted"); }
void err_nozero() { err("messages are counted from 1"); }
void err_toobig() { err("not that many messages"); }
void err_nosuch() { err("unable to open that message"); }
void err_nounlink() { err("unable to unlink all deleted messages"); }

void okay() { outs("+OK \r\n"); flush(); }

void printfn(char *fn)
{
  fn += 4;
  out(fn,str_chr(fn,':'));
}

char strnum[FMT_ULONG];
stralloc line = {0};

void blast(buffer *bf,unsigned long limit)
{
  int match;
  int inheaders = 1;
 
  for (;;) {
    if (getln(bf,&line,&match,'\n') != 0) die();
    if (!match && !line.len) break;
    if (match) --line.len; /* no way to pass this info over POP */
    if (limit) if (!inheaders) if (!--limit) break;
    if (!line.len)
      inheaders = 0;
    else
      if (line.s[0] == '.')
        out(".",1);
    out(line.s,line.len);
    out("\r\n",2);
    
    if (!match) break;
  }
  out("\r\n.\r\n",5);
  flush();
}

stralloc filenames = {0};
prioq pq = {0};

struct message {
  int flagdeleted;
  unsigned long size;
  char *fn;
} *m;

int numm;
int last = 0;

void getlist()
{
  struct prioq_elt pe;
  struct stat st;
  int i;
 
  maildir_clean(&line);
  if (maildir_scan(&pq,&filenames,1,1) == -1) die_scan();
 
  numm = pq.p ? pq.len : 0;
  m = (struct message *) alloc(numm * sizeof(struct message));
  if (!m) die_nomem();
 
  for (i = 0; i < numm; ++i) {
    if (!prioq_min(&pq,&pe)) { numm = i; break; }
    prioq_delmin(&pq);
    m[i].fn = filenames.s + pe.id;
    m[i].flagdeleted = 0;
    if (stat(m[i].fn,&st) == -1)
      m[i].size = 0;
    else
      m[i].size = st.st_size;
  }
}

void pop3_stat()
{
  int i;
  unsigned long total;
 
  total = 0;
  for (i = 0; i < numm; ++i) 
    if (!m[i].flagdeleted) total += m[i].size;

  outs("+OK ");
  out(strnum,fmt_uint(strnum,numm));
  outs(" ");
  out(strnum,fmt_ulong(strnum,total));
  outs("\r\n");
  flush();
}

void pop3_rset()
{
  int i;

  for (i = 0; i < numm; ++i) 
    m[i].flagdeleted = 0;
  last = 0;
  okay();
}

void pop3_last()
{
  outs("+OK ");
  out(strnum,fmt_uint(strnum,last));
  outs("\r\n");
  flush();
}

void pop3_quit()
{
  int i;

  for (i = 0; i < numm; ++i)
    if (m[i].flagdeleted) {
      if (unlink(m[i].fn) == -1) err_nounlink();
    } else {
      if (str_start(m[i].fn,"new/")) {
        if (!stralloc_copys(&line,"cur/")) die_nomem();
        if (!stralloc_cats(&line,m[i].fn + 4)) die_nomem();
        if (!stralloc_cats(&line,":2,")) die_nomem();
        if (!stralloc_0(&line)) die_nomem();
        rename(m[i].fn,line.s); /* if it fails, bummer */
      }
    }
  okay();
  die();
}

int msgno(char *arg)
{
  unsigned long u;

  if (!scan_ulong(arg,&u)) { err_syntax(); return -1; }
  if (!u) { err_nozero(); return -1; }
  --u;
  if (u >= numm) { err_toobig(); return -1; }
  if (m[u].flagdeleted) { err_deleted(); return -1; }
  return u;
}

void pop3_dele(char *arg)
{
  int i;

  i = msgno(arg);
  if (i == -1) return;
  m[i].flagdeleted = 1;
  if (i + 1 > last) last = i + 1;
  okay();
}

void list(int i,int flaguidl)
{
  out(strnum,fmt_uint(strnum,i + 1));
  outs(" ");
  if (flaguidl) printfn(m[i].fn);
  else out(strnum,fmt_ulong(strnum,m[i].size));
  outs("\r\n");
}

void dolisting(char *arg,int flaguidl)
{
  unsigned int i;

  if (*arg) {
    i = msgno(arg);
    if (i == -1) return;

    outs("+OK ");
    list(i,flaguidl);
  } else {
    okay();
    for (i = 0; i < numm; ++i)
      if (!m[i].flagdeleted) list(i,flaguidl);
    outs(".\r\n");
  }
  flush();
}

void pop3_uidl(char *arg) { dolisting(arg,1); }
void pop3_list(char *arg) { dolisting(arg,0); }

char msgbuf[1024];
buffer bm; 

void pop3_top(char *arg)
{
  int i;
  unsigned long limit;
  int fd;
 
  i = msgno(arg);
  if (i == -1) return;
 
  arg += scan_ulong(arg,&limit);
  while (*arg == ' ') ++arg;
  if (scan_ulong(arg,&limit)) ++limit; 
  else limit = 0;
 
  fd = open_read(m[i].fn);
  if (fd == -1) { err_nosuch(); return; }
  okay();
  buffer_init(&bm,read,fd,msgbuf,sizeof(msgbuf));
  blast(&bm,limit);
  close(fd);
}

struct commands pop3commands[] = {
  { "quit", pop3_quit, 0 }
, { "stat", pop3_stat, 0 }
, { "list", pop3_list, 0 }
, { "uidl", pop3_uidl, 0 }
, { "dele", pop3_dele, 0 }
, { "retr", pop3_top, 0 }
, { "rset", pop3_rset, 0 }
, { "last", pop3_last, 0 }
, { "top", pop3_top, 0 }
, { "noop", okay, 0 }
, { 0, err_unimpl, 0 }
} ;

int main(int argc,char **argv)
{
  sig_alarmcatch(die);
  sig_pipeignore();
 
  if (!argv[1]) die_nomaildir();
  if (chdir(argv[1]) == -1) die_nomaildir();
 
  getlist();

  okay();
  commands(&bi,pop3commands);
  die();
}