~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to imap/src/osdep/nt/tenexnt.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ========================================================================
 
2
 * Copyright 1988-2006 University of Washington
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * 
 
11
 * ========================================================================
 
12
 */
 
13
 
 
14
/*
 
15
 * Program:     Tenex mail routines
 
16
 *
 
17
 * Author:      Mark Crispin
 
18
 *              Networks and Distributed Computing
 
19
 *              Computing & Communications
 
20
 *              University of Washington
 
21
 *              Administration Building, AG-44
 
22
 *              Seattle, WA  98195
 
23
 *              Internet: MRC@CAC.Washington.EDU
 
24
 *
 
25
 * Date:        22 May 1990
 
26
 * Last Edited: 30 August 2006
 
27
 */
 
28
 
 
29
 
 
30
/*                              FILE TIME SEMANTICS
 
31
 *
 
32
 * The atime is the last read time of the file.
 
33
 * The mtime is the last flags update time of the file.
 
34
 * The ctime is the last write time of the file.
 
35
 *
 
36
 *                              TEXT SIZE SEMANTICS
 
37
 *
 
38
 * Most of the text sizes are in internal (LF-only) form, except for the
 
39
 * msg.text size.  Beware.
 
40
 */
 
41
 
 
42
#include <stdio.h>
 
43
#include <ctype.h>
 
44
#include <errno.h>
 
45
extern int errno;               /* just in case */
 
46
#include "mail.h"
 
47
#include "osdep.h"
 
48
#include <fcntl.h>
 
49
#include <time.h>
 
50
#include <sys/stat.h>
 
51
#include <sys/utime.h>
 
52
#include "misc.h"
 
53
#include "dummy.h"
 
54
 
 
55
/* TENEX I/O stream local data */
 
56
        
 
57
typedef struct tenex_local {
 
58
  unsigned int shouldcheck: 1;  /* if ping should do a check instead */
 
59
  unsigned int mustcheck: 1;    /* if ping must do a check instead */
 
60
  int fd;                       /* file descriptor for I/O */
 
61
  off_t filesize;               /* file size parsed */
 
62
  time_t filetime;              /* last file time */
 
63
  time_t lastsnarf;             /* local snarf time */
 
64
  unsigned char *buf;           /* temporary buffer */
 
65
  unsigned long buflen;         /* current size of temporary buffer */
 
66
  unsigned long uid;            /* current text uid */
 
67
  SIZEDTEXT text;               /* current text */
 
68
} TENEXLOCAL;
 
69
 
 
70
 
 
71
/* Convenient access to local data */
 
72
 
 
73
#define LOCAL ((TENEXLOCAL *) stream->local)
 
74
 
 
75
 
 
76
/* Function prototypes */
 
77
 
 
78
DRIVER *tenex_valid (char *name);
 
79
int tenex_isvalid (char *name,char *file);
 
80
void *tenex_parameters (long function,void *value);
 
81
void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
 
82
void tenex_list (MAILSTREAM *stream,char *ref,char *pat);
 
83
void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat);
 
84
long tenex_create (MAILSTREAM *stream,char *mailbox);
 
85
long tenex_delete (MAILSTREAM *stream,char *mailbox);
 
86
long tenex_rename (MAILSTREAM *stream,char *old,char *newname);
 
87
long tenex_status (MAILSTREAM *stream,char *mbx,long flags);
 
88
MAILSTREAM *tenex_open (MAILSTREAM *stream);
 
89
void tenex_close (MAILSTREAM *stream,long options);
 
90
void tenex_fast (MAILSTREAM *stream,char *sequence,long flags);
 
91
void tenex_flags (MAILSTREAM *stream,char *sequence,long flags);
 
92
char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
 
93
                    unsigned long *length,long flags);
 
94
long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
 
95
void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
 
96
void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
 
97
long tenex_ping (MAILSTREAM *stream);
 
98
void tenex_check (MAILSTREAM *stream);
 
99
void tenex_snarf (MAILSTREAM *stream);
 
100
long tenex_expunge (MAILSTREAM *stream,char *sequence,long options);
 
101
long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
 
102
long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
 
103
 
 
104
unsigned long tenex_size (MAILSTREAM *stream,unsigned long m);
 
105
long tenex_parse (MAILSTREAM *stream);
 
106
MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno);
 
107
void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
 
108
void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,
 
109
                          long syncflag);
 
110
unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
 
111
                            unsigned long *size);
 
112
 
 
113
 
 
114
/* Tenex mail routines */
 
115
 
 
116
 
 
117
/* Driver dispatch used by MAIL */
 
118
 
 
119
DRIVER tenexdriver = {
 
120
  "tenex",                      /* driver name */
 
121
  DR_LOCAL|DR_MAIL,             /* driver flags */
 
122
  (DRIVER *) NIL,               /* next driver */
 
123
  tenex_valid,                  /* mailbox is valid for us */
 
124
  tenex_parameters,             /* manipulate parameters */
 
125
  tenex_scan,                   /* scan mailboxes */
 
126
  tenex_list,                   /* list mailboxes */
 
127
  tenex_lsub,                   /* list subscribed mailboxes */
 
128
  NIL,                          /* subscribe to mailbox */
 
129
  NIL,                          /* unsubscribe from mailbox */
 
130
  tenex_create,                 /* create mailbox */
 
131
  tenex_delete,                 /* delete mailbox */
 
132
  tenex_rename,                 /* rename mailbox */
 
133
  mail_status_default,          /* status of mailbox */
 
134
  tenex_open,                   /* open mailbox */
 
135
  tenex_close,                  /* close mailbox */
 
136
  tenex_flags,                  /* fetch message "fast" attributes */
 
137
  tenex_flags,                  /* fetch message flags */
 
138
  NIL,                          /* fetch overview */
 
139
  NIL,                          /* fetch message envelopes */
 
140
  tenex_header,                 /* fetch message header */
 
141
  tenex_text,                   /* fetch message body */
 
142
  NIL,                          /* fetch partial message text */
 
143
  NIL,                          /* unique identifier */
 
144
  NIL,                          /* message number */
 
145
  tenex_flag,                   /* modify flags */
 
146
  tenex_flagmsg,                /* per-message modify flags */
 
147
  NIL,                          /* search for message based on criteria */
 
148
  NIL,                          /* sort messages */
 
149
  NIL,                          /* thread messages */
 
150
  tenex_ping,                   /* ping mailbox to see if still alive */
 
151
  tenex_check,                  /* check for new messages */
 
152
  tenex_expunge,                /* expunge deleted messages */
 
153
  tenex_copy,                   /* copy messages to another mailbox */
 
154
  tenex_append,                 /* append string message to mailbox */
 
155
  NIL                           /* garbage collect stream */
 
156
};
 
157
 
 
158
                                /* prototype stream */
 
159
MAILSTREAM tenexproto = {&tenexdriver};
 
160
 
 
161
/* Tenex mail validate mailbox
 
162
 * Accepts: mailbox name
 
163
 * Returns: our driver if name is valid, NIL otherwise
 
164
 */
 
165
 
 
166
DRIVER *tenex_valid (char *name)
 
167
{
 
168
  char tmp[MAILTMPLEN];
 
169
  return tenex_isvalid (name,tmp) ? &tenexdriver : NIL;
 
170
}
 
171
 
 
172
 
 
173
/* Tenex mail test for valid mailbox
 
174
 * Accepts: mailbox name
 
175
 *          buffer to return file name
 
176
 * Returns: T if valid, NIL otherwise
 
177
 */
 
178
 
 
179
int tenex_isvalid (char *name,char *file)
 
180
{
 
181
  int fd;
 
182
  int ret = NIL;
 
183
  char *s,tmp[MAILTMPLEN];
 
184
  struct stat sbuf;
 
185
  struct utimbuf times;
 
186
  errno = EINVAL;               /* assume invalid argument */
 
187
                                /* if file, get its status */
 
188
  if ((s = dummy_file (file,name)) && !stat (s,&sbuf) &&
 
189
      ((sbuf.st_mode & S_IFMT) == S_IFREG)) {
 
190
    if (!sbuf.st_size)errno = 0;/* empty file */
 
191
    else if ((fd = open (file,O_BINARY|O_RDONLY,NIL)) >= 0) {
 
192
      memset (tmp,'\0',MAILTMPLEN);
 
193
      if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\012')) &&
 
194
          (s[-1] != '\015')) {  /* valid format? */
 
195
        *s = '\0';              /* tie off header */
 
196
                                /* must begin with dd-mmm-yy" */
 
197
        ret = (((tmp[2] == '-' && tmp[6] == '-') ||
 
198
                (tmp[1] == '-' && tmp[5] == '-')) &&
 
199
               (s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL;
 
200
      }
 
201
      else errno = -1;          /* bogus format */
 
202
      close (fd);               /* close the file */
 
203
                                /* \Marked status? */
 
204
      if (sbuf.st_ctime > sbuf.st_atime) {
 
205
                                /* preserve atime and mtime */
 
206
        times.actime = sbuf.st_atime;
 
207
        times.modtime = sbuf.st_mtime;
 
208
        utime (file,&times);    /* set the times */
 
209
      }
 
210
    }
 
211
  }
 
212
                                /* in case INBOX but not tenex format */
 
213
  else if ((errno == ENOENT) && !compare_cstring (name,"INBOX")) errno = -1;
 
214
  return ret;                   /* return what we should */
 
215
}
 
216
 
 
217
 
 
218
/* Tenex manipulate driver parameters
 
219
 * Accepts: function code
 
220
 *          function-dependent value
 
221
 * Returns: function-dependent return value
 
222
 */
 
223
 
 
224
void *tenex_parameters (long function,void *value)
 
225
{
 
226
  return NIL;
 
227
}
 
228
 
 
229
/* Tenex mail scan mailboxes
 
230
 * Accepts: mail stream
 
231
 *          reference
 
232
 *          pattern to search
 
233
 *          string to scan
 
234
 */
 
235
 
 
236
void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
 
237
{
 
238
  if (stream) dummy_scan (NIL,ref,pat,contents);
 
239
}
 
240
 
 
241
 
 
242
/* Tenex mail list mailboxes
 
243
 * Accepts: mail stream
 
244
 *          reference
 
245
 *          pattern to search
 
246
 */
 
247
 
 
248
void tenex_list (MAILSTREAM *stream,char *ref,char *pat)
 
249
{
 
250
  if (stream) dummy_list (NIL,ref,pat);
 
251
}
 
252
 
 
253
 
 
254
/* Tenex mail list subscribed mailboxes
 
255
 * Accepts: mail stream
 
256
 *          reference
 
257
 *          pattern to search
 
258
 */
 
259
 
 
260
void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat)
 
261
{
 
262
  if (stream) dummy_lsub (NIL,ref,pat);
 
263
}
 
264
 
 
265
/* Tenex mail create mailbox
 
266
 * Accepts: MAIL stream
 
267
 *          mailbox name to create
 
268
 * Returns: T on success, NIL on failure
 
269
 */
 
270
 
 
271
long tenex_create (MAILSTREAM *stream,char *mailbox)
 
272
{
 
273
  char *s,mbx[MAILTMPLEN];
 
274
  if (s = dummy_file (mbx,mailbox)) return dummy_create (stream,s);
 
275
  sprintf (mbx,"Can't create %.80s: invalid name",mailbox);
 
276
  mm_log (mbx,ERROR);
 
277
  return NIL;
 
278
}
 
279
 
 
280
 
 
281
/* Tenex mail delete mailbox
 
282
 * Accepts: MAIL stream
 
283
 *          mailbox name to delete
 
284
 * Returns: T on success, NIL on failure
 
285
 */
 
286
 
 
287
long tenex_delete (MAILSTREAM *stream,char *mailbox)
 
288
{
 
289
  return tenex_rename (stream,mailbox,NIL);
 
290
}
 
291
 
 
292
/* Tenex mail rename mailbox
 
293
 * Accepts: MAIL stream
 
294
 *          old mailbox name
 
295
 *          new mailbox name (or NIL for delete)
 
296
 * Returns: T on success, NIL on failure
 
297
 */
 
298
 
 
299
long tenex_rename (MAILSTREAM *stream,char *old,char *newname)
 
300
{
 
301
  long ret = LONGT;
 
302
  char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
 
303
  int fd,ld;
 
304
  struct stat sbuf;
 
305
  if (!dummy_file (file,old) ||
 
306
      (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
 
307
                   ((s = strrchr (tmp,'\\')) && !s[1])))) {
 
308
    sprintf (tmp,newname ?
 
309
             "Can't rename mailbox %.80s to %.80s: invalid name" :
 
310
             "Can't delete mailbox %.80s: invalid name",
 
311
             old,newname);
 
312
    mm_log (tmp,ERROR);
 
313
    return NIL;
 
314
  }
 
315
  else if ((fd = open (file,O_BINARY|O_RDWR,NIL)) < 0) {
 
316
    sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
 
317
    mm_log (tmp,ERROR);
 
318
    return NIL;
 
319
  }
 
320
                                /* get exclusive parse/append permission */
 
321
  if ((ld = lockname (lock,file,LOCK_EX)) < 0) {
 
322
    mm_log ("Unable to lock rename mailbox",ERROR);
 
323
    return NIL;
 
324
  }
 
325
                                /* lock out other users */
 
326
  if (flock (fd,LOCK_EX|LOCK_NB)) {
 
327
    close (fd);                 /* couldn't lock, give up on it then */
 
328
    sprintf (tmp,"Mailbox %.80s is in use by another process",old);
 
329
    mm_log (tmp,ERROR);
 
330
    unlockfd (ld,lock);         /* release exclusive parse/append permission */
 
331
    return NIL;
 
332
  }
 
333
 
 
334
  if (newname) {                /* want rename? */
 
335
                                /* found superior to destination name? */
 
336
    if ((s = strrchr (tmp,'\\')) && (s != tmp) &&
 
337
        ((tmp[1] != ':') || (s != tmp + 2))) {
 
338
      c = s[1];                 /* remember character after delimiter */
 
339
      *s = s[1] = '\0';         /* tie off name at delimiter */
 
340
                                /* name doesn't exist, create it */
 
341
      if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) {
 
342
        *s = '\\';              /* restore delimiter */
 
343
        if (!dummy_create (stream,tmp)) ret = NIL;
 
344
      }
 
345
      else *s = '\\';           /* restore delimiter */
 
346
      s[1] = c;                 /* restore character after delimiter */
 
347
    }
 
348
    flock (fd,LOCK_UN);         /* release lock on the file */
 
349
    close (fd);                 /* pacify NTFS */
 
350
                                /* rename the file */
 
351
    if (ret && rename (file,tmp)) {
 
352
      sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
 
353
               strerror (errno));
 
354
      mm_log (tmp,ERROR);
 
355
      ret = NIL;                /* set failure */
 
356
    }
 
357
  }
 
358
  else {
 
359
    flock (fd,LOCK_UN);         /* release lock on the file */
 
360
    close (fd);                 /* pacify NTFS */
 
361
    if (unlink (file)) {
 
362
      sprintf (tmp,"Can't delete mailbox %.80s: %.80s",old,strerror (errno));
 
363
      mm_log (tmp,ERROR);
 
364
      ret = NIL;                /* set failure */
 
365
    }
 
366
  }
 
367
  unlockfd (ld,lock);           /* release exclusive parse/append permission */
 
368
  return ret;                   /* return success */
 
369
}
 
370
 
 
371
/* Tenex mail open
 
372
 * Accepts: stream to open
 
373
 * Returns: stream on success, NIL on failure
 
374
 */
 
375
 
 
376
MAILSTREAM *tenex_open (MAILSTREAM *stream)
 
377
{
 
378
  int fd,ld;
 
379
  char tmp[MAILTMPLEN];
 
380
                                /* return prototype for OP_PROTOTYPE call */
 
381
  if (!stream) return &tenexproto;
 
382
  if (stream->local) fatal ("tenex recycle stream");
 
383
                                /* canonicalize the mailbox name */
 
384
  if (!dummy_file (tmp,stream->mailbox)) {
 
385
    sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
 
386
    mm_log (tmp,ERROR);
 
387
  }
 
388
  if (stream->rdonly ||
 
389
      (fd = open (tmp,O_BINARY|O_RDWR,NIL)) < 0) {
 
390
    if ((fd = open (tmp,O_BINARY|O_RDONLY,NIL)) < 0) {
 
391
      sprintf (tmp,"Can't open mailbox: %.80s",strerror (errno));
 
392
      mm_log (tmp,ERROR);
 
393
      return NIL;
 
394
    }
 
395
    else if (!stream->rdonly) { /* got it, but readonly */
 
396
      mm_log ("Can't get write access to mailbox, access is readonly",WARN);
 
397
      stream->rdonly = T;
 
398
    }
 
399
  }
 
400
  stream->local = fs_get (sizeof (TENEXLOCAL));
 
401
  LOCAL->fd = fd;               /* bind the file */
 
402
  LOCAL->buf = (char *) fs_get (CHUNKSIZE);
 
403
  LOCAL->buflen = CHUNKSIZE - 1;
 
404
  LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
 
405
  LOCAL->text.size = CHUNKSIZE - 1;
 
406
                                /* note if an INBOX or not */
 
407
  stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
 
408
  fs_give ((void **) &stream->mailbox);
 
409
  stream->mailbox = cpystr (tmp);
 
410
                                /* get shared parse permission */
 
411
  if ((ld = lockname (tmp,stream->mailbox,LOCK_SH)) < 0) {
 
412
    mm_log ("Unable to lock open mailbox",ERROR);
 
413
    return NIL;
 
414
  }
 
415
  flock (LOCAL->fd,LOCK_SH);    /* lock the file */
 
416
  unlockfd (ld,tmp);            /* release shared parse permission */
 
417
  LOCAL->filesize = 0;          /* initialize parsed file size */
 
418
  LOCAL->filetime = 0;          /* time not set up yet */
 
419
  LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
 
420
  stream->sequence++;           /* bump sequence number */
 
421
  stream->uid_validity = (unsigned long) time (0);
 
422
                                /* parse mailbox */
 
423
  stream->nmsgs = stream->recent = 0;
 
424
  if (tenex_ping (stream) && !stream->nmsgs)
 
425
    mm_log ("Mailbox is empty",(long) NIL);
 
426
  if (!LOCAL) return NIL;       /* failure if stream died */
 
427
  stream->perm_seen = stream->perm_deleted =
 
428
    stream->perm_flagged = stream->perm_answered = stream->perm_draft =
 
429
      stream->rdonly ? NIL : T;
 
430
  stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
 
431
  return stream;                /* return stream to caller */
 
432
}
 
433
 
 
434
/* Tenex mail close
 
435
 * Accepts: MAIL stream
 
436
 *          close options
 
437
 */
 
438
 
 
439
void tenex_close (MAILSTREAM *stream,long options)
 
440
{
 
441
  if (stream && LOCAL) {        /* only if a file is open */
 
442
    int silent = stream->silent;
 
443
    stream->silent = T;         /* note this stream is dying */
 
444
    if (options & CL_EXPUNGE) tenex_expunge (stream,NIL,NIL);
 
445
    stream->silent = silent;    /* restore previous status */
 
446
    flock (LOCAL->fd,LOCK_UN);  /* unlock local file */
 
447
    close (LOCAL->fd);          /* close the local file */
 
448
                                /* free local text buffer */
 
449
    if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
 
450
    if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
 
451
                                /* nuke the local data */
 
452
    fs_give ((void **) &stream->local);
 
453
    stream->dtb = NIL;          /* log out the DTB */
 
454
  }
 
455
}
 
456
 
 
457
/* Tenex mail fetch flags
 
458
 * Accepts: MAIL stream
 
459
 *          sequence
 
460
 *          option flags
 
461
 * Sniffs at file to get flags
 
462
 */
 
463
 
 
464
void tenex_flags (MAILSTREAM *stream,char *sequence,long flags)
 
465
{
 
466
  STRING bs;
 
467
  MESSAGECACHE *elt;
 
468
  unsigned long i;
 
469
  if (stream && LOCAL &&
 
470
      ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
 
471
       mail_sequence (stream,sequence)))
 
472
    for (i = 1; i <= stream->nmsgs; i++)
 
473
      if ((elt = mail_elt (stream,i))->sequence) {
 
474
        if (!elt->rfc822_size) { /* have header size yet? */
 
475
          lseek (LOCAL->fd,elt->private.special.offset +
 
476
                 elt->private.special.text.size,L_SET);
 
477
                                /* resize bigbuf if necessary */
 
478
          if (LOCAL->buflen < elt->private.msg.full.text.size) {
 
479
            fs_give ((void **) &LOCAL->buf);
 
480
            LOCAL->buflen = elt->private.msg.full.text.size;
 
481
            LOCAL->buf = (char *) fs_get (LOCAL->buflen + 1);
 
482
          }
 
483
                                /* tie off string */
 
484
          LOCAL->buf[elt->private.msg.full.text.size] = '\0';
 
485
                                /* read in the message */
 
486
          read (LOCAL->fd,LOCAL->buf,elt->private.msg.full.text.size);
 
487
          INIT (&bs,mail_string,(void *) LOCAL->buf,
 
488
                elt->private.msg.full.text.size);
 
489
                                /* calculate its CRLF size */
 
490
          elt->rfc822_size = unix_crlflen (&bs);
 
491
        }
 
492
        tenex_elt (stream,i);   /* get current flags from file */
 
493
      }
 
494
}
 
495
 
 
496
/* TENEX mail fetch message header
 
497
 * Accepts: MAIL stream
 
498
 *          message # to fetch
 
499
 *          pointer to returned header text length
 
500
 *          option flags
 
501
 * Returns: message header in RFC822 format
 
502
 */
 
503
 
 
504
char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
 
505
                    unsigned long *length,long flags)
 
506
{
 
507
  char *s;
 
508
  unsigned long i;
 
509
  *length = 0;                  /* default to empty */
 
510
  if (flags & FT_UID) return "";/* UID call "impossible" */
 
511
                                /* get to header position */
 
512
  lseek (LOCAL->fd,tenex_hdrpos (stream,msgno,&i),L_SET);
 
513
  if (flags & FT_INTERNAL) {
 
514
    if (i > LOCAL->buflen) {    /* resize if not enough space */
 
515
      fs_give ((void **) &LOCAL->buf);
 
516
      LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
 
517
    }
 
518
                                /* slurp the data */
 
519
    read (LOCAL->fd,LOCAL->buf,*length = i);
 
520
  }
 
521
  else {
 
522
    s = (char *) fs_get (i + 1);/* get readin buffer */
 
523
    s[i] = '\0';                /* tie off string */
 
524
    read (LOCAL->fd,s,i);       /* slurp the data */
 
525
                                /* make CRLF copy of string */
 
526
    *length = unix_crlfcpy (&LOCAL->buf,&LOCAL->buflen,s,i);
 
527
    fs_give ((void **) &s);     /* free readin buffer */
 
528
  }
 
529
  return LOCAL->buf;
 
530
}
 
531
 
 
532
/* TENEX mail fetch message text (body only)
 
533
 * Accepts: MAIL stream
 
534
 *          message # to fetch
 
535
 *          pointer to returned stringstruct
 
536
 *          option flags
 
537
 * Returns: T, always
 
538
 */
 
539
 
 
540
long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
 
541
{
 
542
  char *s;
 
543
  unsigned long i,j;
 
544
  MESSAGECACHE *elt;
 
545
                                /* UID call "impossible" */
 
546
  if (flags & FT_UID) return NIL;
 
547
                                /* get message status */
 
548
  elt = tenex_elt (stream,msgno);
 
549
                                /* if message not seen */
 
550
  if (!(flags & FT_PEEK) && !elt->seen) {
 
551
    elt->seen = T;              /* mark message as seen */
 
552
                                /* recalculate status */
 
553
    tenex_update_status (stream,msgno,T);
 
554
    mm_flags (stream,msgno);
 
555
  }
 
556
  if (flags & FT_INTERNAL) {    /* if internal representation wanted */
 
557
                                /* find header position */
 
558
    i = tenex_hdrpos (stream,msgno,&j);
 
559
    if (i > LOCAL->buflen) {    /* resize if not enough space */
 
560
      fs_give ((void **) &LOCAL->buf);
 
561
      LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
 
562
    }
 
563
                                /* go to text position */
 
564
    lseek (LOCAL->fd,i + j,L_SET);
 
565
                                /* slurp the data */
 
566
    if (read (LOCAL->fd,LOCAL->buf,i) != (long) i) return NIL;
 
567
                                /* set up stringstruct for internal */
 
568
    INIT (bs,mail_string,LOCAL->buf,i);
 
569
  }
 
570
  else {                        /* normal form, previous text cached? */
 
571
    if (elt->private.uid == LOCAL->uid)
 
572
      i = elt->private.msg.text.text.size;
 
573
    else {                      /* not cached, cache it now */
 
574
      LOCAL->uid = elt->private.uid;
 
575
                                /* find header position */
 
576
      i = tenex_hdrpos (stream,msgno,&j);
 
577
                                /* go to text position */
 
578
      lseek (LOCAL->fd,i + j,L_SET);
 
579
      s = (char *) fs_get ((i = tenex_size (stream,msgno) - j) + 1);
 
580
      s[i] = '\0';              /* tie off string */
 
581
      read (LOCAL->fd,s,i);     /* slurp the data */
 
582
                                /* make CRLF copy of string */
 
583
      i = elt->private.msg.text.text.size =
 
584
        strcrlfcpy (&LOCAL->text.data,&LOCAL->text.size,s,i);
 
585
      fs_give ((void **) &s);   /* free readin buffer */
 
586
    }
 
587
                                /* set up stringstruct */
 
588
    INIT (bs,mail_string,LOCAL->text.data,i);
 
589
  }
 
590
  return T;                     /* success */
 
591
}
 
592
 
 
593
/* Tenex mail modify flags
 
594
 * Accepts: MAIL stream
 
595
 *          sequence
 
596
 *          flag(s)
 
597
 *          option flags
 
598
 */
 
599
 
 
600
void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
 
601
{
 
602
  struct utimbuf times;
 
603
  struct stat sbuf;
 
604
  if (!stream->rdonly) {        /* make sure the update takes */
 
605
    fsync (LOCAL->fd);
 
606
    fstat (LOCAL->fd,&sbuf);    /* get current write time */
 
607
    times.modtime = LOCAL->filetime = sbuf.st_mtime;
 
608
    times.actime = time (0);    /* make sure read comes after all that */
 
609
    utime (stream->mailbox,&times);
 
610
  }
 
611
}
 
612
 
 
613
 
 
614
/* Tenex mail per-message modify flags
 
615
 * Accepts: MAIL stream
 
616
 *          message cache element
 
617
 */
 
618
 
 
619
void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
 
620
{
 
621
  struct stat sbuf;
 
622
                                /* maybe need to do a checkpoint? */
 
623
  if (LOCAL->filetime && !LOCAL->shouldcheck) {
 
624
    fstat (LOCAL->fd,&sbuf);    /* get current write time */
 
625
    if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
 
626
    LOCAL->filetime = 0;        /* don't do this test for any other messages */
 
627
  }
 
628
                                /* recalculate status */
 
629
  tenex_update_status (stream,elt->msgno,NIL);
 
630
}
 
631
 
 
632
/* Tenex mail ping mailbox
 
633
 * Accepts: MAIL stream
 
634
 * Returns: T if stream still alive, NIL if not
 
635
 */
 
636
 
 
637
long tenex_ping (MAILSTREAM *stream)
 
638
{
 
639
  unsigned long i = 1;
 
640
  long r = T;
 
641
  int ld;
 
642
  char lock[MAILTMPLEN];
 
643
  struct stat sbuf;
 
644
  if (stream && LOCAL) {        /* only if stream already open */
 
645
    fstat (LOCAL->fd,&sbuf);    /* get current file poop */
 
646
    if (LOCAL->filetime && !(LOCAL->mustcheck || LOCAL->shouldcheck) &&
 
647
        (LOCAL->filetime < sbuf.st_mtime)) LOCAL->shouldcheck = T;
 
648
                                /* check for changed message status */
 
649
    if (LOCAL->mustcheck || LOCAL->shouldcheck) {
 
650
      LOCAL->filetime = sbuf.st_mtime;
 
651
      if (LOCAL->shouldcheck)   /* babble when we do this unilaterally */
 
652
        mm_notify (stream,"[CHECK] Checking for flag updates",NIL);
 
653
      while (i <= stream->nmsgs) tenex_elt (stream,i++);
 
654
      LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
 
655
    }
 
656
                                /* get shared parse/append permission */
 
657
    if ((sbuf.st_size != LOCAL->filesize) &&
 
658
        ((ld = lockname (lock,stream->mailbox,LOCK_SH)) >= 0)) {
 
659
                                /* parse resulting mailbox */
 
660
      r = (tenex_parse (stream)) ? T : NIL;
 
661
      unlockfd (ld,lock);       /* release shared parse/append permission */
 
662
    }
 
663
  }
 
664
  return r;                     /* return result of the parse */
 
665
}
 
666
 
 
667
 
 
668
/* Tenex mail check mailbox (reparses status too)
 
669
 * Accepts: MAIL stream
 
670
 */
 
671
 
 
672
void tenex_check (MAILSTREAM *stream)
 
673
{
 
674
                                /* mark that a check is desired */
 
675
  if (LOCAL) LOCAL->mustcheck = T;
 
676
  if (tenex_ping (stream)) mm_log ("Check completed",(long) NIL);
 
677
}
 
678
 
 
679
/* Tenex mail expunge mailbox
 
680
 *          sequence to expunge if non-NIL
 
681
 *          expunge options
 
682
 * Returns: T, always
 
683
 */
 
684
 
 
685
long tenex_expunge (MAILSTREAM *stream,char *sequence,long options)
 
686
{
 
687
  long ret;
 
688
  struct utimbuf times;
 
689
  struct stat sbuf;
 
690
  off_t pos = 0;
 
691
  int ld;
 
692
  unsigned long i = 1;
 
693
  unsigned long j,k,m,recent;
 
694
  unsigned long n = 0;
 
695
  unsigned long delta = 0;
 
696
  char lock[MAILTMPLEN];
 
697
  MESSAGECACHE *elt;
 
698
  if (!(ret = (sequence ? ((options & EX_UID) ?
 
699
                           mail_uid_sequence (stream,sequence) :
 
700
                           mail_sequence (stream,sequence)) : LONGT) &&
 
701
        tenex_ping (stream)));  /* parse sequence if given, ping stream */
 
702
  else if (stream->rdonly) mm_log ("Expunge ignored on readonly mailbox",WARN);
 
703
  else {
 
704
    if (LOCAL->filetime && !LOCAL->shouldcheck) {
 
705
      fstat (LOCAL->fd,&sbuf);  /* get current write time */
 
706
      if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
 
707
    }
 
708
                                /* get exclusive access */
 
709
    if ((ld = lockname (lock,stream->mailbox,LOCK_EX)) < 0)
 
710
      mm_log ("Unable to lock expunge mailbox",ERROR);
 
711
                                /* make sure see any newly-arrived messages */
 
712
    else if (!tenex_parse (stream));
 
713
                                /* get exclusive access */
 
714
    else if (flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
 
715
      flock (LOCAL->fd,LOCK_SH);/* recover previous lock */
 
716
      mm_log ("Can't expunge because mailbox is in use by another process",
 
717
              ERROR);
 
718
      unlockfd (ld,lock);       /* release exclusive parse/append permission */
 
719
    }
 
720
 
 
721
    else {
 
722
      mm_critical (stream);     /* go critical */
 
723
      recent = stream->recent;  /* get recent now that pinged and locked */
 
724
                                /* for each message */
 
725
      while (i <= stream->nmsgs) {
 
726
                                /* get cache element */
 
727
        elt = tenex_elt (stream,i);
 
728
                                /* number of bytes to smash or preserve */
 
729
        k = elt->private.special.text.size + tenex_size (stream,i);
 
730
                                /* if need to expunge this message */
 
731
        if (elt->deleted && (sequence ? elt->sequence : T)) {
 
732
                                /* if recent, note one less recent message */
 
733
          if (elt->recent) --recent;
 
734
          delta += k;           /* number of bytes to delete */
 
735
                                /* notify upper levels */
 
736
          mail_expunged (stream,i);
 
737
          n++;                  /* count up one more expunged message */
 
738
        }
 
739
        else if (i++ && delta) {/* preserved message */
 
740
                                /* first byte to preserve */
 
741
          j = elt->private.special.offset;
 
742
          do {                  /* read from source position */
 
743
            m = min (k,LOCAL->buflen);
 
744
            lseek (LOCAL->fd,j,L_SET);
 
745
            read (LOCAL->fd,LOCAL->buf,m);
 
746
            pos = j - delta;    /* write to destination position */
 
747
            while (T) {
 
748
              lseek (LOCAL->fd,pos,L_SET);
 
749
              if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
 
750
              mm_notify (stream,strerror (errno),WARN);
 
751
              mm_diskerror (stream,errno,T);
 
752
            }
 
753
            pos += m;           /* new position */
 
754
            j += m;             /* next chunk, perhaps */
 
755
          } while (k -= m);     /* until done */
 
756
                                /* note the new address of this text */
 
757
          elt->private.special.offset -= delta;
 
758
        }
 
759
                                /* preserved but no deleted messages */
 
760
        else pos = elt->private.special.offset + k;
 
761
      }
 
762
 
 
763
      if (n) {                  /* truncate file after last message */
 
764
        if (pos != (LOCAL->filesize -= delta)) {
 
765
          sprintf (LOCAL->buf,
 
766
                   "Calculated size mismatch %lu != %lu, delta = %lu",
 
767
                   (unsigned long) pos,(unsigned long) LOCAL->filesize,delta);
 
768
          mm_log (LOCAL->buf,WARN);
 
769
          LOCAL->filesize = pos;/* fix it then */
 
770
        }
 
771
        ftruncate (LOCAL->fd,LOCAL->filesize);
 
772
        sprintf (LOCAL->buf,"Expunged %lu messages",n);
 
773
                                /* output the news */
 
774
        mm_log (LOCAL->buf,(long) NIL);
 
775
      }
 
776
      else mm_log ("No messages deleted, so no update needed",(long) NIL);
 
777
      fsync (LOCAL->fd);        /* force disk update */
 
778
      fstat (LOCAL->fd,&sbuf);  /* get new write time */
 
779
      times.modtime = LOCAL->filetime = sbuf.st_mtime;
 
780
      times.actime = time (0);  /* reset atime to now */
 
781
      utime (stream->mailbox,&times);
 
782
      mm_nocritical (stream);   /* release critical */
 
783
                                /* notify upper level of new mailbox size */
 
784
      mail_exists (stream,stream->nmsgs);
 
785
      mail_recent (stream,recent);
 
786
      flock (LOCAL->fd,LOCK_SH);/* allow sharers again */
 
787
      unlockfd (ld,lock);       /* release exclusive parse/append permission */
 
788
    }
 
789
  }
 
790
  return ret;
 
791
}
 
792
 
 
793
/* Tenex mail copy message(s)
 
794
 * Accepts: MAIL stream
 
795
 *          sequence
 
796
 *          destination mailbox
 
797
 *          copy options
 
798
 * Returns: T if success, NIL if failed
 
799
 */
 
800
 
 
801
long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
 
802
{
 
803
  struct stat sbuf;
 
804
  struct utimbuf times;
 
805
  MESSAGECACHE *elt;
 
806
  unsigned long i,j,k;
 
807
  long ret = LONGT;
 
808
  int fd,ld;
 
809
  char file[MAILTMPLEN],lock[MAILTMPLEN];
 
810
  mailproxycopy_t pc =
 
811
    (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
 
812
                                /* make sure valid mailbox */
 
813
  if (!tenex_isvalid (mailbox,file)) switch (errno) {
 
814
  case ENOENT:                  /* no such file? */
 
815
    mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
 
816
    return NIL;
 
817
  case 0:                       /* merely empty file? */
 
818
    break;
 
819
  case EINVAL:
 
820
    if (pc) return (*pc) (stream,sequence,mailbox,options);
 
821
    sprintf (LOCAL->buf,"Invalid Tenex-format mailbox name: %.80s",mailbox);
 
822
    mm_log (LOCAL->buf,ERROR);
 
823
    return NIL;
 
824
  default:
 
825
    if (pc) return (*pc) (stream,sequence,mailbox,options);
 
826
    sprintf (LOCAL->buf,"Not a Tenex-format mailbox: %.80s",mailbox);
 
827
    mm_log (LOCAL->buf,ERROR);
 
828
    return NIL;
 
829
  }
 
830
  if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
 
831
        mail_sequence (stream,sequence))) return NIL;
 
832
                                /* got file? */  
 
833
  if ((fd = open (file,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)) < 0) {
 
834
    sprintf (LOCAL->buf,"Unable to open copy mailbox: %.80s",strerror (errno));
 
835
    mm_log (LOCAL->buf,ERROR);
 
836
    return NIL;
 
837
  }
 
838
  mm_critical (stream);         /* go critical */
 
839
                                /* get exclusive parse/append permission */
 
840
  if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
 
841
    mm_log ("Unable to lock copy mailbox",ERROR);
 
842
    mm_nocritical (stream);
 
843
    return NIL;
 
844
  }
 
845
  fstat (fd,&sbuf);             /* get current file size */
 
846
  lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
 
847
 
 
848
                                /* for each requested message */
 
849
  for (i = 1; ret && (i <= stream->nmsgs); i++) 
 
850
    if ((elt = mail_elt (stream,i))->sequence) {
 
851
      lseek (LOCAL->fd,elt->private.special.offset,L_SET);
 
852
                                /* number of bytes to copy */
 
853
      k = elt->private.special.text.size + tenex_size (stream,i);
 
854
      do {                      /* read from source position */
 
855
        j = min (k,LOCAL->buflen);
 
856
        read (LOCAL->fd,LOCAL->buf,j);
 
857
        if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
 
858
      } while (ret && (k -= j));/* until done */
 
859
    }
 
860
                                /* delete all requested messages */
 
861
  if (ret && (options & CP_MOVE)) {
 
862
    sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
 
863
    mm_log (LOCAL->buf,ERROR);
 
864
    ftruncate (fd,sbuf.st_size);
 
865
  }
 
866
                                /* set atime to now-1 if successful copy */
 
867
  if (ret) times.actime = time (0) - 1;
 
868
                                /* else preserved \Marked status */
 
869
  else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
 
870
         sbuf.st_atime : time (0);
 
871
  times.modtime = sbuf.st_mtime;/* preserve mtime */
 
872
  utime (file,&times);          /* set the times */
 
873
  unlockfd (ld,lock);           /* release exclusive parse/append permission */
 
874
  close (fd);                   /* close the file */
 
875
  mm_nocritical (stream);       /* release critical */
 
876
                                /* delete all requested messages */
 
877
  if (ret && (options & CP_MOVE)) {
 
878
    for (i = 1; i <= stream->nmsgs; i++)
 
879
      if ((elt = tenex_elt (stream,i))->sequence) {
 
880
        elt->deleted = T;       /* mark message deleted */
 
881
                                /* recalculate status */
 
882
        tenex_update_status (stream,i,NIL);
 
883
      }
 
884
    if (!stream->rdonly) {      /* make sure the update takes */
 
885
      fsync (LOCAL->fd);
 
886
      fstat (LOCAL->fd,&sbuf);  /* get current write time */
 
887
      times.modtime = LOCAL->filetime = sbuf.st_mtime;
 
888
      times.actime = time (0);  /* make sure atime remains greater */
 
889
      utime (stream->mailbox,&times);
 
890
    }
 
891
  }
 
892
  if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
 
893
    mm_log ("Can not return meaningful COPYUID with this mailbox format",WARN);
 
894
  return ret;
 
895
}
 
896
 
 
897
/* Tenex mail append message from stringstruct
 
898
 * Accepts: MAIL stream
 
899
 *          destination mailbox
 
900
 *          append callback
 
901
 *          data for callback
 
902
 * Returns: T if append successful, else NIL
 
903
 */
 
904
 
 
905
long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
 
906
{
 
907
  struct stat sbuf;
 
908
  int fd,ld,c;
 
909
  char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
 
910
  struct utimbuf times;
 
911
  FILE *df;
 
912
  MESSAGECACHE elt;
 
913
  long f;
 
914
  unsigned long i,j,uf,size;
 
915
  STRING *message;
 
916
  long ret = LONGT;
 
917
                                /* default stream to prototype */
 
918
  if (!stream) stream = &tenexproto;
 
919
                                /* make sure valid mailbox */
 
920
  if (!tenex_isvalid (mailbox,file)) switch (errno) {
 
921
  case ENOENT:                  /* no such file? */
 
922
    if (!compare_cstring (mailbox,"INBOX")) tenex_create (NIL,"INBOX");
 
923
    else {
 
924
      mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
 
925
      return NIL;
 
926
    }
 
927
                                /* falls through */
 
928
  case 0:                       /* merely empty file? */
 
929
    break;
 
930
  case EINVAL:
 
931
    sprintf (tmp,"Invalid TENEX-format mailbox name: %.80s",mailbox);
 
932
    mm_log (tmp,ERROR);
 
933
    return NIL;
 
934
  default:
 
935
    sprintf (tmp,"Not a TENEX-format mailbox: %.80s",mailbox);
 
936
    mm_log (tmp,ERROR);
 
937
    return NIL;
 
938
  }
 
939
                                /* get first message */
 
940
  if (!(*af) (stream,data,&flags,&date,&message)) return NIL;
 
941
 
 
942
                                /* open destination mailbox */
 
943
  if (((fd = open (file,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IREAD|S_IWRITE))
 
944
       < 0) || !(df = fdopen (fd,"ab"))) {
 
945
    sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
 
946
    mm_log (tmp,ERROR);
 
947
    return NIL;
 
948
  }
 
949
                                /* get parse/append permission */
 
950
  if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
 
951
    mm_log ("Unable to lock append mailbox",ERROR);
 
952
    close (fd);
 
953
    return NIL;
 
954
  }
 
955
  mm_critical (stream);         /* go critical */
 
956
  fstat (fd,&sbuf);             /* get current file size */
 
957
  errno = 0;
 
958
  do {                          /* parse flags */
 
959
    if (!SIZE (message)) {      /* guard against zero-length */
 
960
      mm_log ("Append of zero-length message",ERROR);
 
961
      ret = NIL;
 
962
      break;
 
963
    }
 
964
    f = mail_parse_flags (stream,flags,&i);
 
965
                                /* reverse bits (dontcha wish we had CIRC?) */
 
966
    for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
 
967
    if (date) {                 /* parse date if given */
 
968
      if (!mail_parse_date (&elt,date)) {
 
969
        sprintf (tmp,"Bad date in append: %.80s",date);
 
970
        mm_log (tmp,ERROR);
 
971
        ret = NIL;              /* mark failure */
 
972
        break;
 
973
      }
 
974
      mail_date (tmp,&elt);     /* write preseved date */
 
975
    }
 
976
    else internal_date (tmp);   /* get current date in IMAP format */
 
977
    i = GETPOS (message);       /* remember current position */
 
978
    for (j = SIZE (message), size = 0; j; --j)
 
979
      if (SNX (message) != '\015') ++size;
 
980
    SETPOS (message,i);         /* restore position */
 
981
                                /* write header */
 
982
    if (fprintf (df,"%s,%lu;%010lo%02lo\n",tmp,size,uf,(unsigned long) f) < 0)
 
983
      ret = NIL;
 
984
    else {                      /* write message */
 
985
      while (size) if ((c = 0xff & SNX (message)) != '\015') {
 
986
        if (putc (c,df) != EOF) --size;
 
987
        else break;
 
988
      }
 
989
                                /* get next message */
 
990
      if (size || !(*af) (stream,data,&flags,&date,&message)) ret = NIL;
 
991
    }
 
992
  } while (ret && message);
 
993
                                /* if error... */
 
994
  if (!ret || (fflush (df) == EOF)) {
 
995
    ftruncate (fd,sbuf.st_size);/* revert file */
 
996
    close (fd);                 /* make sure fclose() doesn't corrupt us */
 
997
    if (errno) {
 
998
      sprintf (tmp,"Message append failed: %s",strerror (errno));
 
999
      mm_log (tmp,ERROR);
 
1000
    }
 
1001
    ret = NIL;
 
1002
  }
 
1003
  if (ret) times.actime = time (0) - 1;
 
1004
                                /* else preserved \Marked status */
 
1005
  else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
 
1006
         sbuf.st_atime : time (0);
 
1007
  times.modtime = sbuf.st_mtime;/* preserve mtime */
 
1008
  utime (file,&times);          /* set the times */
 
1009
  fclose (df);                  /* close the file */
 
1010
  unlockfd (ld,lock);           /* release exclusive parse/append permission */
 
1011
  mm_nocritical (stream);       /* release critical */
 
1012
  if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
 
1013
    mm_log ("Can not return meaningful APPENDUID with this mailbox format",
 
1014
            WARN);
 
1015
  return ret;
 
1016
}
 
1017
 
 
1018
/* Internal routines */
 
1019
 
 
1020
 
 
1021
/* Tenex mail return internal message size in bytes
 
1022
 * Accepts: MAIL stream
 
1023
 *          message #
 
1024
 * Returns: internal size of message
 
1025
 */
 
1026
 
 
1027
unsigned long tenex_size (MAILSTREAM *stream,unsigned long m)
 
1028
{
 
1029
  MESSAGECACHE *elt = mail_elt (stream,m);
 
1030
  return ((m < stream->nmsgs) ? mail_elt (stream,m+1)->private.special.offset :
 
1031
          LOCAL->filesize) -
 
1032
            (elt->private.special.offset + elt->private.special.text.size);
 
1033
}
 
1034
 
 
1035
/* Tenex mail parse mailbox
 
1036
 * Accepts: MAIL stream
 
1037
 * Returns: T if parse OK
 
1038
 *          NIL if failure, stream aborted
 
1039
 */
 
1040
 
 
1041
long tenex_parse (MAILSTREAM *stream)
 
1042
{
 
1043
  struct stat sbuf;
 
1044
  MESSAGECACHE *elt = NIL;
 
1045
  unsigned char c,*s,*t,*x;
 
1046
  char tmp[MAILTMPLEN];
 
1047
  unsigned long i,j;
 
1048
  long curpos = LOCAL->filesize;
 
1049
  long nmsgs = stream->nmsgs;
 
1050
  long recent = stream->recent;
 
1051
  short added = NIL;
 
1052
  short silent = stream->silent;
 
1053
  fstat (LOCAL->fd,&sbuf);      /* get status */
 
1054
  if (sbuf.st_size < curpos) {  /* sanity check */
 
1055
    sprintf (tmp,"Mailbox shrank from %ld to %ld!",curpos,sbuf.st_size);
 
1056
    mm_log (tmp,ERROR);
 
1057
    tenex_close (stream,NIL);
 
1058
    return NIL;
 
1059
  }
 
1060
  stream->silent = T;           /* don't pass up mm_exists() events yet */
 
1061
  while (sbuf.st_size - curpos){/* while there is stuff to parse */
 
1062
                                /* get to that position in the file */
 
1063
    lseek (LOCAL->fd,curpos,L_SET);
 
1064
    if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
 
1065
      sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
 
1066
               (unsigned long) curpos,(unsigned long) sbuf.st_size,
 
1067
               i ? strerror (errno) : "no data read");
 
1068
      mm_log (tmp,ERROR);
 
1069
      tenex_close (stream,NIL);
 
1070
      return NIL;
 
1071
    }
 
1072
    LOCAL->buf[i] = '\0';       /* tie off buffer just in case */
 
1073
    if (!(s = strchr (LOCAL->buf,'\012'))) {
 
1074
      sprintf (tmp,"Unable to find newline at %lu in %lu bytes, text: %s",
 
1075
               (unsigned long) curpos,i,(char *) LOCAL->buf);
 
1076
      mm_log (tmp,ERROR);
 
1077
      tenex_close (stream,NIL);
 
1078
      return NIL;
 
1079
    }
 
1080
    *s = '\0';                  /* tie off header line */
 
1081
    i = (s + 1) - LOCAL->buf;   /* note start of text offset */
 
1082
    if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
 
1083
      sprintf (tmp,"Unable to parse internal header at %lu: %s",
 
1084
               (unsigned long) curpos,(char *) LOCAL->buf);
 
1085
      mm_log (tmp,ERROR);
 
1086
      tenex_close (stream,NIL);
 
1087
      return NIL;
 
1088
    }
 
1089
    *s++ = '\0'; *t++ = '\0';   /* tie off fields */
 
1090
 
 
1091
    added = T;                  /* note that a new message was added */
 
1092
                                /* swell the cache */
 
1093
    mail_exists (stream,++nmsgs);
 
1094
                                /* instantiate an elt for this message */
 
1095
    (elt = mail_elt (stream,nmsgs))->valid = T;
 
1096
    elt->private.uid = ++stream->uid_last;
 
1097
                                /* note file offset of header */
 
1098
    elt->private.special.offset = curpos;
 
1099
                                /* in case error */
 
1100
    elt->private.special.text.size = 0;
 
1101
                                /* header size not known yet */
 
1102
    elt->private.msg.header.text.size = 0;
 
1103
    x = s;                      /* parse the header components */
 
1104
    if (mail_parse_date (elt,LOCAL->buf) &&
 
1105
        (elt->private.msg.full.text.size = strtoul (s,(char **) &s,10)) &&
 
1106
        (!(s && *s)) && isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
 
1107
        isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
 
1108
        isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
 
1109
        isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
 
1110
      elt->private.special.text.size = i;
 
1111
    else {                      /* oops */
 
1112
      sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
 
1113
               curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
 
1114
      mm_log (tmp,ERROR);
 
1115
      tenex_close (stream,NIL);
 
1116
      return NIL;
 
1117
    }
 
1118
                                /* make sure didn't run off end of file */
 
1119
    if ((curpos += (elt->private.msg.full.text.size + i)) > sbuf.st_size) {
 
1120
      sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
 
1121
               elt->private.special.offset,(unsigned long) curpos,
 
1122
               (unsigned long) sbuf.st_size);
 
1123
      mm_log (tmp,ERROR);
 
1124
      tenex_close (stream,NIL);
 
1125
      return NIL;
 
1126
    }
 
1127
    c = t[10];                  /* remember first system flags byte */
 
1128
    t[10] = '\0';               /* tie off flags */
 
1129
    j = strtoul (t,NIL,8);      /* get user flags value */
 
1130
    t[10] = c;                  /* restore first system flags byte */
 
1131
                                /* set up all valid user flags (reversed!) */
 
1132
    while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
 
1133
                  stream->user_flags[i]) elt->user_flags |= 1 << i;
 
1134
                                /* calculate system flags */
 
1135
    if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
 
1136
    if (j & fDELETED) elt->deleted = T;
 
1137
    if (j & fFLAGGED) elt->flagged = T;
 
1138
    if (j & fANSWERED) elt->answered = T;
 
1139
    if (j & fDRAFT) elt->draft = T;
 
1140
    if (!(j & fOLD)) {          /* newly arrived message? */
 
1141
      elt->recent = T;
 
1142
      recent++;                 /* count up a new recent message */
 
1143
                                /* mark it as old */
 
1144
      tenex_update_status (stream,nmsgs,NIL);
 
1145
    }
 
1146
  }
 
1147
  fsync (LOCAL->fd);            /* make sure all the fOLD flags take */
 
1148
                                /* update parsed file size and time */
 
1149
  LOCAL->filesize = sbuf.st_size;
 
1150
  fstat (LOCAL->fd,&sbuf);      /* get status again to ensure time is right */
 
1151
  LOCAL->filetime = sbuf.st_mtime;
 
1152
  if (added && !stream->rdonly){/* make sure atime updated */
 
1153
    struct utimbuf times;
 
1154
    times.actime = time (0);
 
1155
    times.modtime = LOCAL->filetime;
 
1156
    utime (stream->mailbox,&times);
 
1157
  }
 
1158
  stream->silent = silent;      /* can pass up events now */
 
1159
  mail_exists (stream,nmsgs);   /* notify upper level of new mailbox size */
 
1160
  mail_recent (stream,recent);  /* and of change in recent messages */
 
1161
  return LONGT;                 /* return the winnage */
 
1162
}
 
1163
 
 
1164
/* Tenex get cache element with status updating from file
 
1165
 * Accepts: MAIL stream
 
1166
 *          message number
 
1167
 * Returns: cache element
 
1168
 */
 
1169
 
 
1170
MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno)
 
1171
{
 
1172
  MESSAGECACHE *elt = mail_elt (stream,msgno);
 
1173
  struct {                      /* old flags */
 
1174
    unsigned int seen : 1;
 
1175
    unsigned int deleted : 1;
 
1176
    unsigned int flagged : 1;
 
1177
    unsigned int answered : 1;
 
1178
    unsigned int draft : 1;
 
1179
    unsigned long user_flags;
 
1180
  } old;
 
1181
  old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
 
1182
  old.answered = elt->answered; old.draft = elt->draft;
 
1183
  old.user_flags = elt->user_flags;
 
1184
  tenex_read_flags (stream,elt);
 
1185
  if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
 
1186
      (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
 
1187
      (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
 
1188
    mm_flags (stream,msgno);    /* let top level know */
 
1189
  return elt;
 
1190
}
 
1191
 
 
1192
 
 
1193
/* Tenex read flags from file
 
1194
 * Accepts: MAIL stream
 
1195
 * Returns: cache element
 
1196
 */
 
1197
 
 
1198
void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
 
1199
{
 
1200
  unsigned long i,j;
 
1201
                                /* noop if readonly and have valid flags */
 
1202
  if (stream->rdonly && elt->valid) return;
 
1203
                                /* set the seek pointer */
 
1204
  lseek (LOCAL->fd,(off_t) elt->private.special.offset +
 
1205
         elt->private.special.text.size - 13,L_SET);
 
1206
                                /* read the new flags */
 
1207
  if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
 
1208
    sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
 
1209
    fatal (LOCAL->buf);
 
1210
  }
 
1211
                                /* calculate system flags */
 
1212
  i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
 
1213
  elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
 
1214
  elt->flagged = i & fFLAGGED ? T : NIL;
 
1215
  elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
 
1216
  LOCAL->buf[10] = '\0';        /* tie off flags */
 
1217
  j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
 
1218
                                /* set up all valid user flags (reversed!) */
 
1219
  while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
 
1220
                stream->user_flags[i]) elt->user_flags |= 1 << i;
 
1221
  elt->valid = T;               /* have valid flags now */
 
1222
}
 
1223
 
 
1224
/* Tenex update status string
 
1225
 * Accepts: MAIL stream
 
1226
 *          message number
 
1227
 *          flag saying whether or not to sync
 
1228
 */
 
1229
 
 
1230
void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
 
1231
{
 
1232
  struct utimbuf times;
 
1233
  struct stat sbuf;
 
1234
  MESSAGECACHE *elt = mail_elt (stream,msgno);
 
1235
  unsigned long j,k = 0;
 
1236
                                /* readonly */
 
1237
  if (stream->rdonly || !elt->valid) tenex_read_flags (stream,elt);
 
1238
  else {                        /* readwrite */
 
1239
    j = elt->user_flags;        /* get user flags */
 
1240
                                /* reverse bits (dontcha wish we had CIRC?) */
 
1241
    while (j) k |= 1 << (29 - find_rightmost_bit (&j));
 
1242
                                /* print new flag string */
 
1243
    sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
 
1244
             (fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
 
1245
              (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
 
1246
              (fDRAFT * elt->draft)));
 
1247
    while (T) {                 /* get to that place in the file */
 
1248
      lseek (LOCAL->fd,(off_t) elt->private.special.offset +
 
1249
             elt->private.special.text.size - 13,L_SET);
 
1250
                                /* write new flags */
 
1251
      if (write (LOCAL->fd,LOCAL->buf,12) > 0) break;
 
1252
      mm_notify (stream,strerror (errno),WARN);
 
1253
      mm_diskerror (stream,errno,T);
 
1254
    }
 
1255
    if (syncflag) {             /* sync if requested */
 
1256
      fsync (LOCAL->fd);
 
1257
      fstat (LOCAL->fd,&sbuf);  /* get new write time */
 
1258
      times.modtime = LOCAL->filetime = sbuf.st_mtime;
 
1259
      times.actime = time (0);  /* make sure read is later */
 
1260
      utime (stream->mailbox,&times);
 
1261
    }
 
1262
  }
 
1263
}
 
1264
 
 
1265
/* Tenex locate header for a message
 
1266
 * Accepts: MAIL stream
 
1267
 *          message number
 
1268
 *          pointer to returned header size
 
1269
 * Returns: position of header in file
 
1270
 */
 
1271
 
 
1272
unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
 
1273
                            unsigned long *size)
 
1274
{
 
1275
  unsigned long siz;
 
1276
  long i = 0;
 
1277
  char c = '\0';
 
1278
  char *s = NIL;
 
1279
  MESSAGECACHE *elt = tenex_elt (stream,msgno);
 
1280
  unsigned long ret = elt->private.special.offset +
 
1281
    elt->private.special.text.size;
 
1282
  unsigned long msiz = tenex_size (stream,msgno);
 
1283
                                /* is header size known? */
 
1284
  if (!(*size = elt->private.msg.header.text.size)) {
 
1285
    lseek (LOCAL->fd,ret,L_SET);/* get to header position */
 
1286
                                /* search message for LF LF */
 
1287
    for (siz = 0; siz < msiz; siz++) {
 
1288
      if (--i <= 0)             /* read another buffer as necessary */
 
1289
        read (LOCAL->fd,s = LOCAL->buf,i = min (msiz-siz,(long) MAILTMPLEN));
 
1290
                                /* two newline sequence? */
 
1291
      if ((c == '\012') && (*s == '\012')) {
 
1292
                                /* yes, note for later */
 
1293
        elt->private.msg.header.text.size = (*size = siz + 1);
 
1294
        return ret;             /* return to caller */
 
1295
      }
 
1296
      else c = *s++;            /* next character */
 
1297
    }
 
1298
                                /* header consumes entire message */
 
1299
    elt->private.msg.header.text.size = *size = msiz;
 
1300
  }
 
1301
  return ret;
 
1302
}