imapext-2007

annotate src/osdep/unix/unix.c @ 4:d741b3ecc917

imapext-2007f
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 30 Oct 2014 00:03:05 +0900
parents 2366b362676d
children
rev   line source
yuuji@0 1 /* ========================================================================
yuuji@0 2 * Copyright 1988-2008 University of Washington
yuuji@0 3 *
yuuji@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
yuuji@0 5 * you may not use this file except in compliance with the License.
yuuji@0 6 * You may obtain a copy of the License at
yuuji@0 7 *
yuuji@0 8 * http://www.apache.org/licenses/LICENSE-2.0
yuuji@0 9 *
yuuji@0 10 *
yuuji@0 11 * ========================================================================
yuuji@0 12 */
yuuji@0 13
yuuji@0 14 /*
yuuji@0 15 * Program: UNIX mail routines
yuuji@0 16 *
yuuji@0 17 * Author: Mark Crispin
yuuji@0 18 * UW Technology
yuuji@0 19 * University of Washington
yuuji@0 20 * Seattle, WA 98195
yuuji@0 21 * Internet: MRC@Washington.EDU
yuuji@0 22 *
yuuji@0 23 * Date: 20 December 1989
yuuji@0 24 * Last Edited: 27 March 2008
yuuji@0 25 */
yuuji@0 26
yuuji@0 27
yuuji@0 28 /* DEDICATION
yuuji@0 29 *
yuuji@0 30 * This file is dedicated to my dog, Unix, also known as Yun-chan and
yuuji@0 31 * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix
yuuji@0 32 * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
yuuji@0 33 * a two-month bout with cirrhosis of the liver.
yuuji@0 34 *
yuuji@0 35 * He was a dear friend, and I miss him terribly.
yuuji@0 36 *
yuuji@0 37 * Lift a leg, Yunie. Luv ya forever!!!!
yuuji@0 38 */
yuuji@0 39
yuuji@0 40 #include <stdio.h>
yuuji@0 41 #include <ctype.h>
yuuji@0 42 #include <errno.h>
yuuji@0 43 extern int errno; /* just in case */
yuuji@0 44 #include <signal.h>
yuuji@0 45 #include "mail.h"
yuuji@0 46 #include "osdep.h"
yuuji@0 47 #include <time.h>
yuuji@0 48 #include <sys/stat.h>
yuuji@0 49 #include "unix.h"
yuuji@0 50 #include "pseudo.h"
yuuji@0 51 #include "fdstring.h"
yuuji@0 52 #include "misc.h"
yuuji@0 53 #include "dummy.h"
yuuji@0 54
yuuji@0 55 /* UNIX I/O stream local data */
yuuji@0 56
yuuji@0 57 typedef struct unix_local {
yuuji@0 58 unsigned int dirty : 1; /* disk copy needs updating */
yuuji@0 59 unsigned int ddirty : 1; /* double-dirty, ping becomes checkpoint */
yuuji@0 60 unsigned int pseudo : 1; /* uses a pseudo message */
yuuji@0 61 unsigned int appending : 1; /* don't mark new messages as old */
yuuji@0 62 int fd; /* mailbox file descriptor */
yuuji@0 63 int ld; /* lock file descriptor */
yuuji@0 64 char *lname; /* lock file name */
yuuji@0 65 off_t filesize; /* file size parsed */
yuuji@0 66 time_t filetime; /* last file time */
yuuji@0 67 time_t lastsnarf; /* last snarf time (for mbox driver) */
yuuji@0 68 unsigned char *buf; /* temporary buffer */
yuuji@0 69 unsigned long buflen; /* current size of temporary buffer */
yuuji@0 70 unsigned long uid; /* current text uid */
yuuji@0 71 SIZEDTEXT text; /* current text */
yuuji@0 72 unsigned long textlen; /* current text length */
yuuji@0 73 char *line; /* returned line */
yuuji@0 74 char *linebuf; /* line readin buffer */
yuuji@0 75 unsigned long linebuflen; /* current line readin buffer length */
yuuji@0 76 } UNIXLOCAL;
yuuji@0 77
yuuji@0 78
yuuji@0 79 /* Convenient access to local data */
yuuji@0 80
yuuji@0 81 #define LOCAL ((UNIXLOCAL *) stream->local)
yuuji@0 82
yuuji@0 83
yuuji@0 84 /* UNIX protected file structure */
yuuji@0 85
yuuji@0 86 typedef struct unix_file {
yuuji@0 87 MAILSTREAM *stream; /* current stream */
yuuji@0 88 off_t curpos; /* current file position */
yuuji@0 89 off_t protect; /* protected position */
yuuji@0 90 off_t filepos; /* current last written file position */
yuuji@0 91 char *buf; /* overflow buffer */
yuuji@0 92 size_t buflen; /* current overflow buffer length */
yuuji@0 93 char *bufpos; /* current buffer position */
yuuji@0 94 } UNIXFILE;
yuuji@0 95
yuuji@0 96 /* Function prototypes */
yuuji@0 97
yuuji@0 98 DRIVER *unix_valid (char *name);
yuuji@0 99 long unix_isvalid_fd (int fd);
yuuji@0 100 void *unix_parameters (long function,void *value);
yuuji@0 101 void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
yuuji@0 102 void unix_list (MAILSTREAM *stream,char *ref,char *pat);
yuuji@0 103 void unix_lsub (MAILSTREAM *stream,char *ref,char *pat);
yuuji@0 104 long unix_create (MAILSTREAM *stream,char *mailbox);
yuuji@0 105 long unix_delete (MAILSTREAM *stream,char *mailbox);
yuuji@0 106 long unix_rename (MAILSTREAM *stream,char *old,char *newname);
yuuji@0 107 MAILSTREAM *unix_open (MAILSTREAM *stream);
yuuji@0 108 void unix_close (MAILSTREAM *stream,long options);
yuuji@0 109 char *unix_header (MAILSTREAM *stream,unsigned long msgno,
yuuji@0 110 unsigned long *length,long flags);
yuuji@0 111 long unix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
yuuji@0 112 char *unix_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
yuuji@0 113 unsigned long *length,long flags);
yuuji@0 114 void unix_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
yuuji@0 115 long unix_ping (MAILSTREAM *stream);
yuuji@0 116 void unix_check (MAILSTREAM *stream);
yuuji@0 117 long unix_expunge (MAILSTREAM *stream,char *sequence,long options);
yuuji@0 118 long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
yuuji@0 119 long unix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
yuuji@0 120 int unix_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
yuuji@0 121 STRING *msg);
yuuji@0 122 int unix_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set);
yuuji@0 123
yuuji@0 124 void unix_abort (MAILSTREAM *stream);
yuuji@0 125 char *unix_file (char *dst,char *name);
yuuji@0 126 int unix_lock (char *file,int flags,int mode,DOTLOCK *lock,int op);
yuuji@0 127 void unix_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock);
yuuji@0 128 int unix_parse (MAILSTREAM *stream,DOTLOCK *lock,int op);
yuuji@0 129 char *unix_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size);
yuuji@0 130 unsigned long unix_pseudo (MAILSTREAM *stream,char *hdr);
yuuji@0 131 unsigned long unix_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
yuuji@0 132 unsigned long uid,long flag);
yuuji@0 133 long unix_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
yuuji@0 134 long flags);
yuuji@0 135 long unix_extend (MAILSTREAM *stream,unsigned long size);
yuuji@0 136 void unix_write (UNIXFILE *f,char *s,unsigned long i);
yuuji@0 137 void unix_phys_write (UNIXFILE *f,char *buf,size_t size);
yuuji@0 138
yuuji@0 139 /* mbox mail routines */
yuuji@0 140
yuuji@0 141 /* Function prototypes */
yuuji@0 142
yuuji@0 143 DRIVER *mbox_valid (char *name);
yuuji@0 144 long mbox_create (MAILSTREAM *stream,char *mailbox);
yuuji@0 145 long mbox_delete (MAILSTREAM *stream,char *mailbox);
yuuji@0 146 long mbox_rename (MAILSTREAM *stream,char *old,char *newname);
yuuji@0 147 long mbox_status (MAILSTREAM *stream,char *mbx,long flags);
yuuji@0 148 MAILSTREAM *mbox_open (MAILSTREAM *stream);
yuuji@0 149 long mbox_ping (MAILSTREAM *stream);
yuuji@0 150 void mbox_check (MAILSTREAM *stream);
yuuji@0 151 long mbox_expunge (MAILSTREAM *stream,char *sequence,long options);
yuuji@0 152 long mbox_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
yuuji@0 153
yuuji@0 154
yuuji@0 155 /* UNIX mail routines */
yuuji@0 156
yuuji@0 157
yuuji@0 158 /* Driver dispatch used by MAIL */
yuuji@0 159
yuuji@0 160 DRIVER unixdriver = {
yuuji@0 161 "unix", /* driver name */
yuuji@0 162 /* driver flags */
yuuji@0 163 DR_LOCAL|DR_MAIL|DR_LOCKING|DR_NONEWMAILRONLY|DR_XPOINT,
yuuji@0 164 (DRIVER *) NIL, /* next driver */
yuuji@0 165 unix_valid, /* mailbox is valid for us */
yuuji@0 166 unix_parameters, /* manipulate parameters */
yuuji@0 167 unix_scan, /* scan mailboxes */
yuuji@0 168 unix_list, /* list mailboxes */
yuuji@0 169 unix_lsub, /* list subscribed mailboxes */
yuuji@0 170 NIL, /* subscribe to mailbox */
yuuji@0 171 NIL, /* unsubscribe from mailbox */
yuuji@0 172 unix_create, /* create mailbox */
yuuji@0 173 unix_delete, /* delete mailbox */
yuuji@0 174 unix_rename, /* rename mailbox */
yuuji@0 175 mail_status_default, /* status of mailbox */
yuuji@0 176 unix_open, /* open mailbox */
yuuji@0 177 unix_close, /* close mailbox */
yuuji@0 178 NIL, /* fetch message "fast" attributes */
yuuji@0 179 NIL, /* fetch message flags */
yuuji@0 180 NIL, /* fetch overview */
yuuji@0 181 NIL, /* fetch message envelopes */
yuuji@0 182 unix_header, /* fetch message header */
yuuji@0 183 unix_text, /* fetch message text */
yuuji@0 184 NIL, /* fetch partial message text */
yuuji@0 185 NIL, /* unique identifier */
yuuji@0 186 NIL, /* message number */
yuuji@0 187 NIL, /* modify flags */
yuuji@0 188 unix_flagmsg, /* per-message modify flags */
yuuji@0 189 NIL, /* search for message based on criteria */
yuuji@0 190 NIL, /* sort messages */
yuuji@0 191 NIL, /* thread messages */
yuuji@0 192 unix_ping, /* ping mailbox to see if still alive */
yuuji@0 193 unix_check, /* check for new messages */
yuuji@0 194 unix_expunge, /* expunge deleted messages */
yuuji@0 195 unix_copy, /* copy messages to another mailbox */
yuuji@0 196 unix_append, /* append string message to mailbox */
yuuji@0 197 NIL /* garbage collect stream */
yuuji@0 198 };
yuuji@0 199
yuuji@0 200 /* prototype stream */
yuuji@0 201 MAILSTREAM unixproto = {&unixdriver};
yuuji@0 202
yuuji@0 203 /* driver parameters */
yuuji@0 204 static long unix_fromwidget = T;
yuuji@4 205 char *myMailSuffix =NIL; /* home directory name */
yuuji@0 206
yuuji@0 207 /* UNIX mail validate mailbox
yuuji@0 208 * Accepts: mailbox name
yuuji@0 209 * Returns: our driver if name is valid, NIL otherwise
yuuji@0 210 */
yuuji@0 211
yuuji@0 212 DRIVER *unix_valid (char *name)
yuuji@0 213 {
yuuji@0 214 int fd;
yuuji@0 215 DRIVER *ret = NIL;
yuuji@0 216 char *t,file[MAILTMPLEN];
yuuji@0 217 struct stat sbuf;
yuuji@0 218 time_t tp[2];
yuuji@0 219 errno = EINVAL; /* assume invalid argument */
yuuji@0 220 /* must be non-empty file */
yuuji@0 221 if ((t = dummy_file (file,name)) && !stat (t,&sbuf)) {
yuuji@0 222 if (!sbuf.st_size)errno = 0;/* empty file */
yuuji@0 223 else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
yuuji@0 224 /* OK if mailbox format good */
yuuji@0 225 if (unix_isvalid_fd (fd)) ret = &unixdriver;
yuuji@0 226 else errno = -1; /* invalid format */
yuuji@0 227 close (fd); /* close the file */
yuuji@0 228 /* \Marked status? */
yuuji@0 229 if ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) {
yuuji@0 230 tp[0] = sbuf.st_atime; /* yes, preserve atime and mtime */
yuuji@0 231 tp[1] = sbuf.st_mtime;
yuuji@0 232 utime (file,tp); /* set the times */
yuuji@0 233 }
yuuji@0 234 }
yuuji@0 235 }
yuuji@0 236 return ret; /* return what we should */
yuuji@0 237 }
yuuji@0 238
yuuji@0 239 /* UNIX mail test for valid mailbox
yuuji@0 240 * Accepts: file descriptor
yuuji@0 241 * scratch buffer
yuuji@0 242 * Returns: T if valid, NIL otherwise
yuuji@0 243 */
yuuji@0 244
yuuji@0 245 long unix_isvalid_fd (int fd)
yuuji@0 246 {
yuuji@0 247 int zn;
yuuji@0 248 int ret = NIL;
yuuji@0 249 char tmp[MAILTMPLEN],*s,*t,c = '\n';
yuuji@0 250 memset (tmp,'\0',MAILTMPLEN);
yuuji@0 251 if (read (fd,tmp,MAILTMPLEN-1) >= 0) {
yuuji@0 252 for (s = tmp; (*s == '\r') || (*s == '\n') || (*s == ' ') || (*s == '\t');)
yuuji@0 253 c = *s++;
yuuji@0 254 if (c == '\n') VALID (s,t,ret,zn);
yuuji@0 255 }
yuuji@0 256 return ret; /* return what we should */
yuuji@0 257 }
yuuji@0 258
yuuji@0 259
yuuji@0 260 /* UNIX manipulate driver parameters
yuuji@0 261 * Accepts: function code
yuuji@0 262 * function-dependent value
yuuji@0 263 * Returns: function-dependent return value
yuuji@0 264 */
yuuji@0 265
yuuji@0 266 void *unix_parameters (long function,void *value)
yuuji@0 267 {
yuuji@0 268 void *ret = NIL;
yuuji@0 269 switch ((int) function) {
yuuji@0 270 case GET_INBOXPATH:
yuuji@0 271 if (value) ret = dummy_file ((char *) value,"INBOX");
yuuji@0 272 break;
yuuji@0 273 case SET_FROMWIDGET:
yuuji@0 274 unix_fromwidget = (long) value;
yuuji@0 275 case GET_FROMWIDGET:
yuuji@0 276 ret = (void *) unix_fromwidget;
yuuji@0 277 break;
yuuji@0 278 }
yuuji@0 279 return ret;
yuuji@0 280 }
yuuji@0 281
yuuji@0 282 /* UNIX mail scan mailboxes
yuuji@0 283 * Accepts: mail stream
yuuji@0 284 * reference
yuuji@0 285 * pattern to search
yuuji@0 286 * string to scan
yuuji@0 287 */
yuuji@0 288
yuuji@0 289 void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
yuuji@0 290 {
yuuji@0 291 if (stream) dummy_scan (NIL,ref,pat,contents);
yuuji@0 292 }
yuuji@0 293
yuuji@0 294
yuuji@0 295 /* UNIX mail list mailboxes
yuuji@0 296 * Accepts: mail stream
yuuji@0 297 * reference
yuuji@0 298 * pattern to search
yuuji@0 299 */
yuuji@0 300
yuuji@0 301 void unix_list (MAILSTREAM *stream,char *ref,char *pat)
yuuji@0 302 {
yuuji@0 303 if (stream) dummy_list (NIL,ref,pat);
yuuji@0 304 }
yuuji@0 305
yuuji@0 306
yuuji@0 307 /* UNIX mail list subscribed mailboxes
yuuji@0 308 * Accepts: mail stream
yuuji@0 309 * reference
yuuji@0 310 * pattern to search
yuuji@0 311 */
yuuji@0 312
yuuji@0 313 void unix_lsub (MAILSTREAM *stream,char *ref,char *pat)
yuuji@0 314 {
yuuji@0 315 if (stream) dummy_lsub (NIL,ref,pat);
yuuji@0 316 }
yuuji@0 317
yuuji@0 318 /* UNIX mail create mailbox
yuuji@0 319 * Accepts: MAIL stream
yuuji@0 320 * mailbox name to create
yuuji@0 321 * Returns: T on success, NIL on failure
yuuji@0 322 */
yuuji@0 323
yuuji@0 324 long unix_create (MAILSTREAM *stream,char *mailbox)
yuuji@0 325 {
yuuji@0 326 char *s,mbx[MAILTMPLEN],tmp[MAILTMPLEN];
yuuji@0 327 long ret = NIL;
yuuji@0 328 int i,fd;
yuuji@0 329 time_t ti = time (0);
yuuji@0 330 if (!(s = dummy_file (mbx,mailbox))) {
yuuji@0 331 sprintf (tmp,"Can't create %.80s: invalid name",mailbox);
yuuji@0 332 MM_LOG (tmp,ERROR);
yuuji@0 333 }
yuuji@0 334 /* create underlying file */
yuuji@0 335 else if (dummy_create_path (stream,s,get_dir_protection (mailbox))) {
yuuji@0 336 /* done if dir-only or whiner */
yuuji@0 337 if (((s = strrchr (s,'/')) && !s[1]) ||
yuuji@0 338 mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) ret = T;
yuuji@0 339 else if ((fd = open (mbx,O_WRONLY,
yuuji@0 340 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) {
yuuji@0 341 sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
yuuji@0 342 MM_LOG (tmp,ERROR);
yuuji@0 343 unlink (mbx); /* delete the file */
yuuji@0 344 }
yuuji@0 345 else { /* initialize header */
yuuji@0 346 memset (tmp,'\0',MAILTMPLEN);
yuuji@0 347 sprintf (tmp,"From %s %sDate: ",pseudo_from,ctime (&ti));
yuuji@0 348 rfc822_fixed_date (s = tmp + strlen (tmp));
yuuji@0 349 /* write the pseudo-header */
yuuji@0 350 sprintf (s += strlen (s),
yuuji@0 351 "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000",
yuuji@0 352 pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
yuuji@0 353 (unsigned long) ti);
yuuji@0 354 for (i = 0; i < NUSERFLAGS; ++i) if (default_user_flag (i))
yuuji@0 355 sprintf (s += strlen (s)," %s",default_user_flag (i));
yuuji@0 356 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n\n",pseudo_msg);
yuuji@0 357 if (write (fd,tmp,strlen (tmp)) > 0) ret = T;
yuuji@0 358 else {
yuuji@0 359 sprintf (tmp,"Can't initialize mailbox node %.80s: %s",mbx,
yuuji@0 360 strerror (errno));
yuuji@0 361 MM_LOG (tmp,ERROR);
yuuji@0 362 unlink (mbx); /* delete the file */
yuuji@0 363 }
yuuji@0 364 close (fd); /* close file */
yuuji@0 365 }
yuuji@0 366 }
yuuji@0 367 /* set proper protections */
yuuji@0 368 return ret ? set_mbx_protections (mailbox,mbx) : NIL;
yuuji@0 369 }
yuuji@0 370
yuuji@0 371 /* UNIX mail delete mailbox
yuuji@0 372 * Accepts: MAIL stream
yuuji@0 373 * mailbox name to delete
yuuji@0 374 * Returns: T on success, NIL on failure
yuuji@0 375 */
yuuji@0 376
yuuji@0 377 long unix_delete (MAILSTREAM *stream,char *mailbox)
yuuji@0 378 {
yuuji@0 379 return unix_rename (stream,mailbox,NIL);
yuuji@0 380 }
yuuji@0 381
yuuji@0 382
yuuji@0 383 /* UNIX mail rename mailbox
yuuji@0 384 * Accepts: MAIL stream
yuuji@0 385 * old mailbox name
yuuji@0 386 * new mailbox name (or NIL for delete)
yuuji@0 387 * Returns: T on success, NIL on failure
yuuji@0 388 */
yuuji@0 389
yuuji@0 390 long unix_rename (MAILSTREAM *stream,char *old,char *newname)
yuuji@0 391 {
yuuji@0 392 long ret = NIL;
yuuji@0 393 char c,*s = NIL;
yuuji@0 394 char tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
yuuji@0 395 DOTLOCK lockx;
yuuji@0 396 int fd,ld;
yuuji@0 397 long i;
yuuji@0 398 struct stat sbuf;
yuuji@0 399 MM_CRITICAL (stream); /* get the c-client lock */
yuuji@0 400 if (!dummy_file (file,old) ||
yuuji@0 401 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
yuuji@0 402 ((s = strrchr (tmp,'/')) && !s[1]))))
yuuji@0 403 sprintf (tmp,newname ?
yuuji@0 404 "Can't rename mailbox %.80s to %.80s: invalid name" :
yuuji@0 405 "Can't delete mailbox %.80s: invalid name",
yuuji@0 406 old,newname);
yuuji@0 407 /* lock out other c-clients */
yuuji@0 408 else if ((ld = lockname (lock,file,LOCK_EX|LOCK_NB,&i)) < 0)
yuuji@0 409 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
yuuji@0 410
yuuji@0 411 else {
yuuji@0 412 if ((fd = unix_lock (file,O_RDWR,
yuuji@0 413 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
yuuji@0 414 &lockx,LOCK_EX)) < 0)
yuuji@0 415 sprintf (tmp,"Can't lock mailbox %.80s: %s",old,strerror (errno));
yuuji@0 416 else {
yuuji@0 417 if (newname) { /* want rename? */
yuuji@0 418 /* found superior to destination name? */
yuuji@0 419 if (s = strrchr (s,'/')) {
yuuji@0 420 c = *++s; /* remember first character of inferior */
yuuji@0 421 *s = '\0'; /* tie off to get just superior */
yuuji@0 422 /* name doesn't exist, create it */
yuuji@0 423 if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
yuuji@0 424 !dummy_create_path (stream,tmp,get_dir_protection (newname))) {
yuuji@0 425 unix_unlock (fd,NIL,&lockx);
yuuji@0 426 unix_unlock (ld,NIL,NIL);
yuuji@0 427 unlink (lock);
yuuji@0 428 MM_NOCRITICAL (stream);
yuuji@0 429 return ret; /* return success or failure */
yuuji@0 430 }
yuuji@0 431 *s = c; /* restore full name */
yuuji@0 432 }
yuuji@0 433 if (rename (file,tmp))
yuuji@0 434 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
yuuji@0 435 strerror (errno));
yuuji@0 436 else ret = T; /* set success */
yuuji@0 437 }
yuuji@0 438 else if (unlink (file))
yuuji@0 439 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
yuuji@0 440 else ret = T; /* set success */
yuuji@0 441 unix_unlock (fd,NIL,&lockx);
yuuji@0 442 }
yuuji@0 443 unix_unlock (ld,NIL,NIL); /* flush the lock */
yuuji@0 444 unlink (lock);
yuuji@0 445 }
yuuji@0 446 MM_NOCRITICAL (stream); /* no longer critical */
yuuji@0 447 if (!ret) MM_LOG (tmp,ERROR); /* log error */
yuuji@0 448 return ret; /* return success or failure */
yuuji@0 449 }
yuuji@0 450
yuuji@0 451 /* UNIX mail open
yuuji@0 452 * Accepts: Stream to open
yuuji@0 453 * Returns: Stream on success, NIL on failure
yuuji@0 454 */
yuuji@0 455
yuuji@0 456 MAILSTREAM *unix_open (MAILSTREAM *stream)
yuuji@0 457 {
yuuji@0 458 long i;
yuuji@0 459 int fd;
yuuji@0 460 char tmp[MAILTMPLEN];
yuuji@0 461 DOTLOCK lock;
yuuji@0 462 long retry;
yuuji@0 463 /* return prototype for OP_PROTOTYPE call */
yuuji@0 464 if (!stream) return user_flags (&unixproto);
yuuji@0 465 retry = stream->silent ? 1 : KODRETRY;
yuuji@0 466 if (stream->local) fatal ("unix recycle stream");
yuuji@0 467 stream->local = memset (fs_get (sizeof (UNIXLOCAL)),0,sizeof (UNIXLOCAL));
yuuji@0 468 /* note if an INBOX or not */
yuuji@0 469 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
yuuji@0 470 /* canonicalize the stream mailbox name */
yuuji@0 471 if (!dummy_file (tmp,stream->mailbox)) {
yuuji@0 472 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
yuuji@0 473 MM_LOG (tmp,ERROR);
yuuji@0 474 return NIL;
yuuji@0 475 }
yuuji@0 476 /* flush old name */
yuuji@0 477 fs_give ((void **) &stream->mailbox);
yuuji@0 478 /* save canonical name */
yuuji@0 479 stream->mailbox = cpystr (tmp);
yuuji@0 480 LOCAL->fd = LOCAL->ld = -1; /* no file or state locking yet */
yuuji@0 481 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
yuuji@0 482 LOCAL->buflen = CHUNKSIZE - 1;
yuuji@0 483 LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
yuuji@0 484 LOCAL->text.size = CHUNKSIZE - 1;
yuuji@0 485 LOCAL->linebuf = (char *) fs_get (CHUNKSIZE);
yuuji@0 486 LOCAL->linebuflen = CHUNKSIZE - 1;
yuuji@0 487 stream->sequence++; /* bump sequence number */
yuuji@0 488
yuuji@0 489 /* make lock for read/write access */
yuuji@0 490 if (!stream->rdonly) while (retry) {
yuuji@0 491 /* try to lock file */
yuuji@0 492 if ((fd = lockname (tmp,stream->mailbox,LOCK_EX|LOCK_NB,&i)) < 0) {
yuuji@0 493 /* suppressing kiss-of-death? */
yuuji@0 494 if (stream->nokod) retry = 0;
yuuji@0 495 /* no, first time through? */
yuuji@0 496 else if (retry-- == KODRETRY) {
yuuji@0 497 /* learned other guy's PID and can signal? */
yuuji@0 498 if (i && !kill ((int) i,SIGUSR2)) {
yuuji@0 499 sprintf (tmp,"Trying to get mailbox lock from process %ld",i);
yuuji@0 500 MM_LOG (tmp,WARN);
yuuji@0 501 }
yuuji@0 502 else retry = 0; /* give up */
yuuji@0 503 }
yuuji@0 504 if (!stream->silent) { /* nothing if silent stream */
yuuji@0 505 if (retry) sleep (1); /* wait a second before trying again */
yuuji@0 506 else MM_LOG ("Mailbox is open by another process, access is readonly",
yuuji@0 507 WARN);
yuuji@0 508 }
yuuji@0 509 }
yuuji@0 510 else { /* got the lock, nobody else can alter state */
yuuji@0 511 LOCAL->ld = fd; /* note lock's fd and name */
yuuji@0 512 LOCAL->lname = cpystr (tmp);
yuuji@0 513 /* make sure mode OK (don't use fchmod()) */
yuuji@0 514 chmod (LOCAL->lname,(long) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
yuuji@0 515 if (stream->silent) i = 0;/* silent streams won't accept KOD */
yuuji@0 516 else { /* note our PID in the lock */
yuuji@0 517 sprintf (tmp,"%d",getpid ());
yuuji@0 518 write (fd,tmp,(i = strlen (tmp))+1);
yuuji@0 519 }
yuuji@0 520 ftruncate (fd,i); /* make sure tied off */
yuuji@0 521 fsync (fd); /* make sure it's available */
yuuji@0 522 retry = 0; /* no more need to try */
yuuji@0 523 }
yuuji@0 524 }
yuuji@0 525
yuuji@0 526 /* parse mailbox */
yuuji@0 527 stream->nmsgs = stream->recent = 0;
yuuji@0 528 /* will we be able to get write access? */
yuuji@0 529 if ((LOCAL->ld >= 0) && access (stream->mailbox,W_OK) && (errno == EACCES)) {
yuuji@0 530 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
yuuji@0 531 flock (LOCAL->ld,LOCK_UN); /* release the lock */
yuuji@0 532 close (LOCAL->ld); /* close the lock file */
yuuji@0 533 LOCAL->ld = -1; /* no more lock fd */
yuuji@0 534 unlink (LOCAL->lname); /* delete it */
yuuji@0 535 }
yuuji@0 536 /* reset UID validity */
yuuji@0 537 stream->uid_validity = stream->uid_last = 0;
yuuji@0 538 if (stream->silent && !stream->rdonly && (LOCAL->ld < 0))
yuuji@0 539 unix_abort (stream); /* abort if can't get RW silent stream */
yuuji@0 540 /* parse mailbox */
yuuji@0 541 else if (unix_parse (stream,&lock,LOCK_SH)) {
yuuji@0 542 unix_unlock (LOCAL->fd,stream,&lock);
yuuji@0 543 mail_unlock (stream);
yuuji@0 544 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 545 }
yuuji@0 546 if (!LOCAL) return NIL; /* failure if stream died */
yuuji@0 547 /* make sure upper level knows readonly */
yuuji@0 548 stream->rdonly = (LOCAL->ld < 0);
yuuji@0 549 /* notify about empty mailbox */
yuuji@0 550 if (!(stream->nmsgs || stream->silent)) MM_LOG ("Mailbox is empty",NIL);
yuuji@0 551 if (!stream->rdonly) { /* flags stick if readwrite */
yuuji@0 552 stream->perm_seen = stream->perm_deleted =
yuuji@0 553 stream->perm_flagged = stream->perm_answered = stream->perm_draft = T;
yuuji@0 554 if (!stream->uid_nosticky) {/* users with lives get permanent keywords */
yuuji@0 555 stream->perm_user_flags = 0xffffffff;
yuuji@0 556 /* and maybe can create them too! */
yuuji@0 557 stream->kwd_create = stream->user_flags[NUSERFLAGS-1] ? NIL : T;
yuuji@0 558 }
yuuji@0 559 }
yuuji@0 560 return stream; /* return stream alive to caller */
yuuji@0 561 }
yuuji@0 562
yuuji@0 563
yuuji@0 564 /* UNIX mail close
yuuji@0 565 * Accepts: MAIL stream
yuuji@0 566 * close options
yuuji@0 567 */
yuuji@0 568
yuuji@0 569 void unix_close (MAILSTREAM *stream,long options)
yuuji@0 570 {
yuuji@0 571 int silent = stream->silent;
yuuji@0 572 stream->silent = T; /* go silent */
yuuji@0 573 /* expunge if requested */
yuuji@0 574 if (options & CL_EXPUNGE) unix_expunge (stream,NIL,NIL);
yuuji@0 575 /* else dump final checkpoint */
yuuji@0 576 else if (LOCAL->dirty) unix_check (stream);
yuuji@0 577 stream->silent = silent; /* restore old silence state */
yuuji@0 578 unix_abort (stream); /* now punt the file and local data */
yuuji@0 579 }
yuuji@0 580
yuuji@0 581 /* UNIX mail fetch message header
yuuji@0 582 * Accepts: MAIL stream
yuuji@0 583 * message # to fetch
yuuji@0 584 * pointer to returned header text length
yuuji@0 585 * option flags
yuuji@0 586 * Returns: message header in RFC822 format
yuuji@0 587 */
yuuji@0 588
yuuji@0 589 /* lines to filter from header */
yuuji@0 590 static STRINGLIST *unix_hlines = NIL;
yuuji@0 591
yuuji@0 592 char *unix_header (MAILSTREAM *stream,unsigned long msgno,
yuuji@0 593 unsigned long *length,long flags)
yuuji@0 594 {
yuuji@0 595 MESSAGECACHE *elt;
yuuji@0 596 unsigned char *s,*t,*tl;
yuuji@0 597 *length = 0; /* default to empty */
yuuji@0 598 if (flags & FT_UID) return "";/* UID call "impossible" */
yuuji@0 599 elt = mail_elt (stream,msgno);/* get cache */
yuuji@0 600 if (!unix_hlines) { /* once only code */
yuuji@0 601 STRINGLIST *lines = unix_hlines = mail_newstringlist ();
yuuji@0 602 lines->text.size = strlen ((char *) (lines->text.data =
yuuji@0 603 (unsigned char *) "Status"));
yuuji@0 604 lines = lines->next = mail_newstringlist ();
yuuji@0 605 lines->text.size = strlen ((char *) (lines->text.data =
yuuji@0 606 (unsigned char *) "X-Status"));
yuuji@0 607 lines = lines->next = mail_newstringlist ();
yuuji@0 608 lines->text.size = strlen ((char *) (lines->text.data =
yuuji@0 609 (unsigned char *) "X-Keywords"));
yuuji@0 610 lines = lines->next = mail_newstringlist ();
yuuji@0 611 lines->text.size = strlen ((char *) (lines->text.data =
yuuji@0 612 (unsigned char *) "X-UID"));
yuuji@0 613 lines = lines->next = mail_newstringlist ();
yuuji@0 614 lines->text.size = strlen ((char *) (lines->text.data =
yuuji@0 615 (unsigned char *) "X-IMAP"));
yuuji@0 616 lines = lines->next = mail_newstringlist ();
yuuji@0 617 lines->text.size = strlen ((char *) (lines->text.data =
yuuji@0 618 (unsigned char *) "X-IMAPbase"));
yuuji@0 619 }
yuuji@0 620 /* go to header position */
yuuji@0 621 lseek (LOCAL->fd,elt->private.special.offset +
yuuji@0 622 elt->private.msg.header.offset,L_SET);
yuuji@0 623
yuuji@0 624 if (flags & FT_INTERNAL) { /* initial data OK? */
yuuji@0 625 if (elt->private.msg.header.text.size > LOCAL->buflen) {
yuuji@0 626 fs_give ((void **) &LOCAL->buf);
yuuji@0 627 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
yuuji@0 628 elt->private.msg.header.text.size) + 1);
yuuji@0 629 }
yuuji@0 630 /* read message */
yuuji@0 631 read (LOCAL->fd,LOCAL->buf,elt->private.msg.header.text.size);
yuuji@0 632 /* got text, tie off string */
yuuji@0 633 LOCAL->buf[*length = elt->private.msg.header.text.size] = '\0';
yuuji@0 634 /* squeeze out CRs (in case from PC) */
yuuji@0 635 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
yuuji@0 636 if (*t != '\r') *s++ = *t;
yuuji@0 637 *s = '\0';
yuuji@0 638 *length = s - LOCAL->buf; /* adjust length */
yuuji@0 639 }
yuuji@0 640 else { /* need to make a CRLF version */
yuuji@0 641 read (LOCAL->fd,s = (char *) fs_get (elt->private.msg.header.text.size+1),
yuuji@0 642 elt->private.msg.header.text.size);
yuuji@0 643 /* tie off string, and convert to CRLF */
yuuji@0 644 s[elt->private.msg.header.text.size] = '\0';
yuuji@0 645 *length = strcrlfcpy (&LOCAL->buf,&LOCAL->buflen,s,
yuuji@0 646 elt->private.msg.header.text.size);
yuuji@0 647 fs_give ((void **) &s); /* free readin buffer */
yuuji@0 648 /* squeeze out spurious CRs */
yuuji@0 649 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
yuuji@0 650 if ((*t != '\r') || (t[1] == '\n')) *s++ = *t;
yuuji@0 651 *s = '\0';
yuuji@0 652 *length = s - LOCAL->buf; /* adjust length */
yuuji@0 653 }
yuuji@0 654 *length = mail_filter (LOCAL->buf,*length,unix_hlines,FT_NOT);
yuuji@0 655 return (char *) LOCAL->buf; /* return processed copy */
yuuji@0 656 }
yuuji@0 657
yuuji@0 658 /* UNIX mail fetch message text
yuuji@0 659 * Accepts: MAIL stream
yuuji@0 660 * message # to fetch
yuuji@0 661 * pointer to returned stringstruct
yuuji@0 662 * option flags
yuuji@0 663 * Returns: T on success, NIL if failure
yuuji@0 664 */
yuuji@0 665
yuuji@0 666 long unix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
yuuji@0 667 {
yuuji@0 668 char *s;
yuuji@0 669 unsigned long i;
yuuji@0 670 MESSAGECACHE *elt;
yuuji@0 671 /* UID call "impossible" */
yuuji@0 672 if (flags & FT_UID) return NIL;
yuuji@0 673 elt = mail_elt (stream,msgno);/* get cache element */
yuuji@0 674 /* if message not seen */
yuuji@0 675 if (!(flags & FT_PEEK) && !elt->seen) {
yuuji@0 676 /* mark message seen and dirty */
yuuji@0 677 elt->seen = elt->private.dirty = LOCAL->dirty = T;
yuuji@0 678 MM_FLAGS (stream,msgno);
yuuji@0 679 }
yuuji@0 680 s = unix_text_work (stream,elt,&i,flags);
yuuji@0 681 INIT (bs,mail_string,s,i); /* set up stringstruct */
yuuji@0 682 return T; /* success */
yuuji@0 683 }
yuuji@0 684
yuuji@0 685 /* UNIX mail fetch message text worker routine
yuuji@0 686 * Accepts: MAIL stream
yuuji@0 687 * message cache element
yuuji@0 688 * pointer to returned header text length
yuuji@0 689 * option flags
yuuji@0 690 */
yuuji@0 691
yuuji@0 692 char *unix_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
yuuji@0 693 unsigned long *length,long flags)
yuuji@0 694 {
yuuji@0 695 FDDATA d;
yuuji@0 696 STRING bs;
yuuji@0 697 unsigned char c,*s,*t,*tl,tmp[CHUNKSIZE];
yuuji@0 698 /* go to text position */
yuuji@0 699 lseek (LOCAL->fd,elt->private.special.offset +
yuuji@0 700 elt->private.msg.text.offset,L_SET);
yuuji@0 701 if (flags & FT_INTERNAL) { /* initial data OK? */
yuuji@0 702 if (elt->private.msg.text.text.size > LOCAL->buflen) {
yuuji@0 703 fs_give ((void **) &LOCAL->buf);
yuuji@0 704 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
yuuji@0 705 elt->private.msg.text.text.size) + 1);
yuuji@0 706 }
yuuji@0 707 /* read message */
yuuji@0 708 read (LOCAL->fd,LOCAL->buf,elt->private.msg.text.text.size);
yuuji@0 709 /* got text, tie off string */
yuuji@0 710 LOCAL->buf[*length = elt->private.msg.text.text.size] = '\0';
yuuji@0 711 /* squeeze out CRs (in case from PC) */
yuuji@0 712 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
yuuji@0 713 if (*t != '\r') *s++ = *t;
yuuji@0 714 *s = '\0';
yuuji@0 715 *length = s - LOCAL->buf; /* adjust length */
yuuji@0 716 return (char *) LOCAL->buf;
yuuji@0 717 }
yuuji@0 718
yuuji@0 719 /* have it cached already? */
yuuji@0 720 if (elt->private.uid != LOCAL->uid) {
yuuji@0 721 /* not cached, cache it now */
yuuji@0 722 LOCAL->uid = elt->private.uid;
yuuji@0 723 /* is buffer big enough? */
yuuji@0 724 if (elt->rfc822_size > LOCAL->text.size) {
yuuji@0 725 /* excessively conservative, but the right thing is too hard to do */
yuuji@0 726 fs_give ((void **) &LOCAL->text.data);
yuuji@0 727 LOCAL->text.data = (unsigned char *)
yuuji@0 728 fs_get ((LOCAL->text.size = elt->rfc822_size) + 1);
yuuji@0 729 }
yuuji@0 730 d.fd = LOCAL->fd; /* yes, set up file descriptor */
yuuji@0 731 d.pos = elt->private.special.offset + elt->private.msg.text.offset;
yuuji@0 732 d.chunk = tmp; /* initial buffer chunk */
yuuji@0 733 d.chunksize = CHUNKSIZE; /* file chunk size */
yuuji@0 734 INIT (&bs,fd_string,&d,elt->private.msg.text.text.size);
yuuji@0 735 for (s = (char *) LOCAL->text.data; SIZE (&bs);) switch (c = SNX (&bs)) {
yuuji@0 736 case '\r': /* carriage return seen */
yuuji@0 737 break;
yuuji@0 738 case '\n':
yuuji@0 739 *s++ = '\r'; /* insert a CR */
yuuji@0 740 default:
yuuji@0 741 *s++ = c; /* copy characters */
yuuji@0 742 }
yuuji@0 743 *s = '\0'; /* tie off buffer */
yuuji@0 744 /* calculate length of cached data */
yuuji@0 745 LOCAL->textlen = s - LOCAL->text.data;
yuuji@0 746 }
yuuji@0 747 *length = LOCAL->textlen; /* return from cache */
yuuji@0 748 return (char *) LOCAL->text.data;
yuuji@0 749 }
yuuji@0 750
yuuji@0 751 /* UNIX per-message modify flag
yuuji@0 752 * Accepts: MAIL stream
yuuji@0 753 * message cache element
yuuji@0 754 */
yuuji@0 755
yuuji@0 756 void unix_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
yuuji@0 757 {
yuuji@0 758 /* only after finishing */
yuuji@0 759 if (elt->valid) elt->private.dirty = LOCAL->dirty = T;
yuuji@0 760 }
yuuji@0 761
yuuji@0 762
yuuji@0 763 /* UNIX mail ping mailbox
yuuji@0 764 * Accepts: MAIL stream
yuuji@0 765 * Returns: T if stream alive, else NIL
yuuji@0 766 */
yuuji@0 767
yuuji@0 768 long unix_ping (MAILSTREAM *stream)
yuuji@0 769 {
yuuji@0 770 DOTLOCK lock;
yuuji@0 771 struct stat sbuf;
yuuji@0 772 long reparse;
yuuji@0 773 /* big no-op if not readwrite */
yuuji@0 774 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock) {
yuuji@0 775 if (stream->rdonly) { /* does he want to give up readwrite? */
yuuji@0 776 /* checkpoint if we changed something */
yuuji@0 777 if (LOCAL->dirty) unix_check (stream);
yuuji@0 778 flock (LOCAL->ld,LOCK_UN);/* release readwrite lock */
yuuji@0 779 close (LOCAL->ld); /* close the readwrite lock file */
yuuji@0 780 LOCAL->ld = -1; /* no more readwrite lock fd */
yuuji@0 781 unlink (LOCAL->lname); /* delete the readwrite lock file */
yuuji@0 782 }
yuuji@0 783 else { /* see if need to reparse */
yuuji@0 784 if (!(reparse = (long) mail_parameters (NIL,GET_NETFSSTATBUG,NIL))) {
yuuji@0 785 /* get current mailbox size */
yuuji@0 786 if (LOCAL->fd >= 0) fstat (LOCAL->fd,&sbuf);
yuuji@0 787 else if (stat (stream->mailbox,&sbuf)) {
yuuji@0 788 sprintf (LOCAL->buf,"Mailbox stat failed, aborted: %s",
yuuji@0 789 strerror (errno));
yuuji@0 790 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 791 unix_abort (stream);
yuuji@0 792 return NIL;
yuuji@0 793 }
yuuji@0 794 reparse = (sbuf.st_size != LOCAL->filesize);
yuuji@0 795 }
yuuji@0 796 /* parse if mailbox changed */
yuuji@0 797 if ((LOCAL->ddirty || reparse) && unix_parse (stream,&lock,LOCK_EX)) {
yuuji@0 798 /* force checkpoint if double-dirty */
yuuji@0 799 if (LOCAL->ddirty) unix_rewrite (stream,NIL,&lock,NIL);
yuuji@0 800 /* unlock mailbox */
yuuji@0 801 else unix_unlock (LOCAL->fd,stream,&lock);
yuuji@0 802 mail_unlock (stream); /* and stream */
yuuji@0 803 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 804 }
yuuji@0 805 }
yuuji@0 806 }
yuuji@0 807 return LOCAL ? LONGT : NIL; /* return if still alive */
yuuji@0 808 }
yuuji@0 809
yuuji@0 810 /* UNIX mail check mailbox
yuuji@0 811 * Accepts: MAIL stream
yuuji@0 812 */
yuuji@0 813
yuuji@0 814 void unix_check (MAILSTREAM *stream)
yuuji@0 815 {
yuuji@0 816 DOTLOCK lock;
yuuji@0 817 /* parse and lock mailbox */
yuuji@0 818 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
yuuji@0 819 unix_parse (stream,&lock,LOCK_EX)) {
yuuji@0 820 /* any unsaved changes? */
yuuji@0 821 if (LOCAL->dirty && unix_rewrite (stream,NIL,&lock,NIL)) {
yuuji@0 822 if (!stream->silent) MM_LOG ("Checkpoint completed",NIL);
yuuji@0 823 }
yuuji@0 824 /* no checkpoint needed, just unlock */
yuuji@0 825 else unix_unlock (LOCAL->fd,stream,&lock);
yuuji@0 826 mail_unlock (stream); /* unlock the stream */
yuuji@0 827 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 828 }
yuuji@0 829 }
yuuji@0 830
yuuji@0 831
yuuji@0 832 /* UNIX mail expunge mailbox
yuuji@0 833 * Accepts: MAIL stream
yuuji@0 834 * sequence to expunge if non-NIL
yuuji@0 835 * expunge options
yuuji@0 836 * Returns: T, always
yuuji@0 837 */
yuuji@0 838
yuuji@0 839 long unix_expunge (MAILSTREAM *stream,char *sequence,long options)
yuuji@0 840 {
yuuji@0 841 long ret;
yuuji@0 842 unsigned long i;
yuuji@0 843 DOTLOCK lock;
yuuji@0 844 char *msg = NIL;
yuuji@0 845 /* parse and lock mailbox */
yuuji@0 846 if (ret = (sequence ? ((options & EX_UID) ?
yuuji@0 847 mail_uid_sequence (stream,sequence) :
yuuji@0 848 mail_sequence (stream,sequence)) : LONGT) &&
yuuji@0 849 LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
yuuji@0 850 unix_parse (stream,&lock,LOCK_EX)) {
yuuji@0 851 /* check expunged messages if not dirty */
yuuji@0 852 for (i = 1; !LOCAL->dirty && (i <= stream->nmsgs); i++) {
yuuji@0 853 MESSAGECACHE *elt = mail_elt (stream,i);
yuuji@0 854 if (mail_elt (stream,i)->deleted) LOCAL->dirty = T;
yuuji@0 855 }
yuuji@0 856 if (!LOCAL->dirty) { /* not dirty and no expunged messages */
yuuji@0 857 unix_unlock (LOCAL->fd,stream,&lock);
yuuji@0 858 msg = "No messages deleted, so no update needed";
yuuji@0 859 }
yuuji@0 860 else if (unix_rewrite (stream,&i,&lock,sequence ? LONGT : NIL)) {
yuuji@0 861 if (i) sprintf (msg = LOCAL->buf,"Expunged %lu messages",i);
yuuji@0 862 else msg = "Mailbox checkpointed, but no messages expunged";
yuuji@0 863 }
yuuji@0 864 /* rewrite failed */
yuuji@0 865 else unix_unlock (LOCAL->fd,stream,&lock);
yuuji@0 866 mail_unlock (stream); /* unlock the stream */
yuuji@0 867 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 868 if (msg && !stream->silent) MM_LOG (msg,NIL);
yuuji@0 869 }
yuuji@0 870 else if (!stream->silent) MM_LOG("Expunge ignored on readonly mailbox",WARN);
yuuji@0 871 return ret;
yuuji@0 872 }
yuuji@0 873
yuuji@0 874 /* UNIX mail copy message(s)
yuuji@0 875 * Accepts: MAIL stream
yuuji@0 876 * sequence
yuuji@0 877 * destination mailbox
yuuji@0 878 * copy options
yuuji@0 879 * Returns: T if copy successful, else NIL
yuuji@0 880 */
yuuji@0 881
yuuji@0 882 long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
yuuji@0 883 {
yuuji@0 884 struct stat sbuf;
yuuji@0 885 int fd;
yuuji@0 886 char *s,file[MAILTMPLEN];
yuuji@0 887 DOTLOCK lock;
yuuji@0 888 time_t tp[2];
yuuji@0 889 unsigned long i,j;
yuuji@0 890 MESSAGECACHE *elt;
yuuji@0 891 long ret = T;
yuuji@0 892 mailproxycopy_t pc =
yuuji@0 893 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
yuuji@0 894 copyuid_t cu = (copyuid_t) (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ?
yuuji@0 895 NIL : mail_parameters (NIL,GET_COPYUID,NIL));
yuuji@0 896 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
yuuji@0 897 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
yuuji@0 898 MAILSTREAM *tstream = NIL;
yuuji@0 899 DRIVER *d;
yuuji@0 900 for (d = (DRIVER *) mail_parameters (NIL,GET_DRIVERS,NIL);
yuuji@0 901 (d && strcmp (d->name,"mbox") && !(d->flags & DR_DISABLE));
yuuji@0 902 d = d->next); /* see if mbox driver active */
yuuji@0 903 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
yuuji@0 904 mail_sequence (stream,sequence))) return NIL;
yuuji@0 905 /* make sure destination is valid */
yuuji@0 906 if (!((d && mbox_valid (mailbox) && (mailbox = "mbox")) ||
yuuji@0 907 unix_valid (mailbox) || !errno))
yuuji@0 908 switch (errno) {
yuuji@0 909 case ENOENT: /* no such file? */
yuuji@0 910 if (compare_cstring (mailbox,"INBOX")) {
yuuji@0 911 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
yuuji@0 912 return NIL;
yuuji@0 913 }
yuuji@0 914 if (pc) return (*pc) (stream,sequence,mailbox,options);
yuuji@0 915 unix_create (NIL,"INBOX");/* create empty INBOX */
yuuji@0 916 case EACCES: /* file protected */
yuuji@0 917 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
yuuji@0 918 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 919 return NIL;
yuuji@0 920 case EINVAL:
yuuji@0 921 if (pc) return (*pc) (stream,sequence,mailbox,options);
yuuji@0 922 sprintf (LOCAL->buf,"Invalid UNIX-format mailbox name: %.80s",mailbox);
yuuji@0 923 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 924 return NIL;
yuuji@0 925 default:
yuuji@0 926 if (pc) return (*pc) (stream,sequence,mailbox,options);
yuuji@0 927 sprintf (LOCAL->buf,"Not a UNIX-format mailbox: %.80s",mailbox);
yuuji@0 928 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 929 return NIL;
yuuji@0 930 }
yuuji@0 931
yuuji@0 932 /* try to open rewrite for UIDPLUS */
yuuji@0 933 if ((tstream = mail_open_work (&unixdriver,NIL,mailbox,
yuuji@0 934 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
yuuji@0 935 tstream = mail_close (tstream);
yuuji@0 936 if (cu && !tstream) { /* wanted a COPYUID? */
yuuji@0 937 sprintf (LOCAL->buf,"Unable to write-open mailbox for COPYUID: %.80s",
yuuji@0 938 mailbox);
yuuji@0 939 MM_LOG (LOCAL->buf,WARN);
yuuji@0 940 cu = NIL; /* don't try to do COPYUID */
yuuji@0 941 }
yuuji@0 942 LOCAL->buf[0] = '\0';
yuuji@0 943 MM_CRITICAL (stream); /* go critical */
yuuji@0 944 if ((fd = unix_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
yuuji@0 945 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
yuuji@0 946 &lock,LOCK_EX)) < 0) {
yuuji@0 947 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 948 sprintf (LOCAL->buf,"Can't open destination mailbox: %s",strerror (errno));
yuuji@0 949 MM_LOG (LOCAL->buf,ERROR);/* log the error */
yuuji@0 950 return NIL; /* failed */
yuuji@0 951 }
yuuji@0 952 fstat (fd,&sbuf); /* get current file size */
yuuji@0 953 /* write all requested messages to mailbox */
yuuji@0 954 for (i = 1; ret && (i <= stream->nmsgs); i++)
yuuji@0 955 if ((elt = mail_elt (stream,i))->sequence) {
yuuji@0 956 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
yuuji@0 957 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
yuuji@0 958 if (write (fd,LOCAL->buf,elt->private.special.text.size) < 0) ret = NIL;
yuuji@0 959 else { /* internal header succeeded */
yuuji@0 960 s = unix_header (stream,i,&j,FT_INTERNAL);
yuuji@0 961 /* header size, sans trailing newline */
yuuji@0 962 if (j && (s[j - 2] == '\n')) j--;
yuuji@0 963 if (write (fd,s,j) < 0) ret = NIL;
yuuji@0 964 else { /* message header succeeded */
yuuji@0 965 j = tstream ? /* write UIDPLUS data if have readwrite */
yuuji@0 966 unix_xstatus (stream,LOCAL->buf,elt,++(tstream->uid_last),LONGT) :
yuuji@0 967 unix_xstatus (stream,LOCAL->buf,elt,NIL,NIL);
yuuji@0 968 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
yuuji@0 969 else { /* message status succeeded */
yuuji@0 970 s = unix_text_work (stream,elt,&j,FT_INTERNAL);
yuuji@0 971 if ((write (fd,s,j) < 0) || (write (fd,"\n",1) < 0)) ret = NIL;
yuuji@0 972 else if (cu) { /* need to pass back new UID? */
yuuji@0 973 mail_append_set (source,mail_uid (stream,i));
yuuji@0 974 mail_append_set (dest,tstream->uid_last);
yuuji@0 975 }
yuuji@0 976 }
yuuji@0 977 }
yuuji@0 978 }
yuuji@0 979 }
yuuji@0 980
yuuji@0 981 if (!ret || fsync (fd)) { /* force out the update */
yuuji@0 982 sprintf (LOCAL->buf,"Message copy failed: %s",strerror (errno));
yuuji@0 983 ftruncate (fd,sbuf.st_size);
yuuji@0 984 ret = NIL;
yuuji@0 985 }
yuuji@0 986 /* force UIDVALIDITY assignment now */
yuuji@0 987 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
yuuji@0 988 /* return sets if doing COPYUID */
yuuji@0 989 if (cu && ret) (*cu) (stream,mailbox,tstream->uid_validity,source,dest);
yuuji@0 990 else { /* flush any sets we may have built */
yuuji@0 991 mail_free_searchset (&source);
yuuji@0 992 mail_free_searchset (&dest);
yuuji@0 993 }
yuuji@0 994 tp[1] = time (0); /* set mtime to now */
yuuji@0 995 if (ret) tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
yuuji@0 996 else tp[0] = /* else preserve \Marked status */
yuuji@0 997 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
yuuji@0 998 sbuf.st_atime : tp[1];
yuuji@0 999 utime (file,tp); /* set the times */
yuuji@0 1000 unix_unlock (fd,NIL,&lock); /* unlock and close mailbox */
yuuji@0 1001 if (tstream) { /* update last UID if we can */
yuuji@0 1002 UNIXLOCAL *local = (UNIXLOCAL *) tstream->local;
yuuji@0 1003 local->dirty = T; /* do a rewrite */
yuuji@0 1004 local->appending = T; /* but not at the cost of marking as old */
yuuji@0 1005 tstream = mail_close (tstream);
yuuji@0 1006 }
yuuji@0 1007 /* log the error */
yuuji@0 1008 if (!ret) MM_LOG (LOCAL->buf,ERROR);
yuuji@0 1009 /* delete if requested message */
yuuji@0 1010 else if (options & CP_MOVE) for (i = 1; i <= stream->nmsgs; i++)
yuuji@0 1011 if ((elt = mail_elt (stream,i))->sequence)
yuuji@0 1012 elt->deleted = elt->private.dirty = LOCAL->dirty = T;
yuuji@0 1013 MM_NOCRITICAL (stream); /* release critical */
yuuji@0 1014 return ret;
yuuji@0 1015 }
yuuji@0 1016
yuuji@0 1017 /* UNIX mail append message from stringstruct
yuuji@0 1018 * Accepts: MAIL stream
yuuji@0 1019 * destination mailbox
yuuji@0 1020 * append callback
yuuji@0 1021 * data for callback
yuuji@0 1022 * Returns: T if append successful, else NIL
yuuji@0 1023 */
yuuji@0 1024
yuuji@0 1025 #define BUFLEN 8*MAILTMPLEN
yuuji@0 1026
yuuji@0 1027 long unix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
yuuji@0 1028 {
yuuji@0 1029 struct stat sbuf;
yuuji@0 1030 int fd;
yuuji@0 1031 unsigned long i;
yuuji@0 1032 char *flags,*date,buf[BUFLEN],tmp[MAILTMPLEN],file[MAILTMPLEN];
yuuji@0 1033 time_t tp[2];
yuuji@0 1034 FILE *sf,*df;
yuuji@0 1035 MESSAGECACHE elt;
yuuji@0 1036 DOTLOCK lock;
yuuji@0 1037 STRING *message;
yuuji@0 1038 unsigned long uidlocation = 0;
yuuji@0 1039 appenduid_t au = (appenduid_t)
yuuji@0 1040 (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ? NIL :
yuuji@0 1041 mail_parameters (NIL,GET_APPENDUID,NIL));
yuuji@0 1042 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
yuuji@0 1043 long ret = LONGT;
yuuji@0 1044 MAILSTREAM *tstream = NIL;
yuuji@0 1045 if (!stream) { /* stream specified? */
yuuji@0 1046 stream = &unixproto; /* no, default stream to prototype */
yuuji@0 1047 for (i = 0; i < NUSERFLAGS && stream->user_flags[i]; ++i)
yuuji@0 1048 fs_give ((void **) &stream->user_flags[i]);
yuuji@0 1049 }
yuuji@0 1050 if (!unix_valid (mailbox)) switch (errno) {
yuuji@0 1051 case ENOENT: /* no such file? */
yuuji@0 1052 if (compare_cstring (mailbox,"INBOX")) {
yuuji@0 1053 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
yuuji@0 1054 return NIL;
yuuji@0 1055 }
yuuji@0 1056 unix_create (NIL,"INBOX"); /* create empty INBOX */
yuuji@0 1057 case 0: /* merely empty file? */
yuuji@0 1058 tstream = stream;
yuuji@0 1059 break;
yuuji@0 1060 case EACCES: /* file protected */
yuuji@0 1061 sprintf (tmp,"Can't access destination: %.80s",mailbox);
yuuji@0 1062 MM_LOG (tmp,ERROR);
yuuji@0 1063 return NIL;
yuuji@0 1064 case EINVAL:
yuuji@0 1065 sprintf (tmp,"Invalid UNIX-format mailbox name: %.80s",mailbox);
yuuji@0 1066 MM_LOG (tmp,ERROR);
yuuji@0 1067 return NIL;
yuuji@0 1068 default:
yuuji@0 1069 sprintf (tmp,"Not a UNIX-format mailbox: %.80s",mailbox);
yuuji@0 1070 MM_LOG (tmp,ERROR);
yuuji@0 1071 return NIL;
yuuji@0 1072 }
yuuji@0 1073 /* get sniffing stream for keywords */
yuuji@0 1074 else if (!(tstream = mail_open (NIL,mailbox,
yuuji@0 1075 OP_READONLY|OP_SILENT|OP_NOKOD|OP_SNIFF))) {
yuuji@0 1076 sprintf (tmp,"Unable to examine mailbox for APPEND: %.80s",mailbox);
yuuji@0 1077 MM_LOG (tmp,ERROR);
yuuji@0 1078 return NIL;
yuuji@0 1079 }
yuuji@0 1080
yuuji@0 1081 /* get first message */
yuuji@0 1082 if (!MM_APPEND (af) (tstream,data,&flags,&date,&message)) return NIL;
yuuji@0 1083 if (!(sf = tmpfile ())) { /* must have scratch file */
yuuji@0 1084 sprintf (tmp,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
yuuji@0 1085 if (!stat (tmp,&sbuf) || !(sf = fopen (tmp,"wb+"))) {
yuuji@0 1086 sprintf (tmp,"Unable to create scratch file: %.80s",strerror (errno));
yuuji@0 1087 MM_LOG (tmp,ERROR);
yuuji@0 1088 return NIL;
yuuji@0 1089 }
yuuji@0 1090 unlink (tmp);
yuuji@0 1091 }
yuuji@0 1092 do { /* parse date */
yuuji@0 1093 if (!date) rfc822_date (date = tmp);
yuuji@0 1094 if (!mail_parse_date (&elt,date)) {
yuuji@0 1095 sprintf (tmp,"Bad date in append: %.80s",date);
yuuji@0 1096 MM_LOG (tmp,ERROR);
yuuji@0 1097 }
yuuji@0 1098 else { /* user wants to suppress time zones? */
yuuji@0 1099 if (mail_parameters (NIL,GET_NOTIMEZONES,NIL)) {
yuuji@0 1100 time_t when = mail_longdate (&elt);
yuuji@0 1101 date = ctime (&when); /* use traditional date */
yuuji@0 1102 }
yuuji@0 1103 /* use POSIX-style date */
yuuji@0 1104 else date = mail_cdate (tmp,&elt);
yuuji@0 1105 if (!SIZE (message)) MM_LOG ("Append of zero-length message",ERROR);
yuuji@0 1106 else if (!unix_collect_msg (tstream,sf,flags,date,message)) {
yuuji@0 1107 sprintf (tmp,"Error writing scratch file: %.80s",strerror (errno));
yuuji@0 1108 MM_LOG (tmp,ERROR);
yuuji@0 1109 }
yuuji@0 1110 /* get next message */
yuuji@0 1111 else if (MM_APPEND (af) (tstream,data,&flags,&date,&message)) continue;
yuuji@0 1112 }
yuuji@0 1113 fclose (sf); /* punt scratch file */
yuuji@0 1114 return NIL; /* give up */
yuuji@0 1115 } while (message); /* until no more messages */
yuuji@0 1116 if (fflush (sf)) {
yuuji@0 1117 sprintf (tmp,"Error finishing scratch file: %.80s",strerror (errno));
yuuji@0 1118 MM_LOG (tmp,ERROR);
yuuji@0 1119 fclose (sf); /* punt scratch file */
yuuji@0 1120 return NIL; /* give up */
yuuji@0 1121 }
yuuji@0 1122 i = ftell (sf); /* size of scratch file */
yuuji@0 1123 /* close sniffing stream */
yuuji@0 1124 if (tstream != stream) tstream = mail_close (tstream);
yuuji@0 1125
yuuji@0 1126 MM_CRITICAL (stream); /* go critical */
yuuji@0 1127 /* try to open readwrite for UIDPLUS */
yuuji@0 1128 if ((tstream = mail_open_work (&unixdriver,NIL,mailbox,
yuuji@0 1129 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
yuuji@0 1130 tstream = mail_close (tstream);
yuuji@0 1131 if (au && !tstream) { /* wanted an APPENDUID? */
yuuji@0 1132 sprintf (tmp,"Unable to re-open mailbox for APPENDUID: %.80s",mailbox);
yuuji@0 1133 MM_LOG (tmp,WARN);
yuuji@0 1134 au = NIL;
yuuji@0 1135 }
yuuji@0 1136 if (((fd = unix_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
yuuji@0 1137 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
yuuji@0 1138 &lock,LOCK_EX)) < 0) ||
yuuji@0 1139 !(df = fdopen (fd,"ab"))) {
yuuji@0 1140 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 1141 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
yuuji@0 1142 MM_LOG (tmp,ERROR);
yuuji@0 1143 return NIL;
yuuji@0 1144 }
yuuji@0 1145 fstat (fd,&sbuf); /* get current file size */
yuuji@0 1146 rewind (sf);
yuuji@0 1147 tp[1] = time (0); /* set mtime to now */
yuuji@0 1148 /* write all messages */
yuuji@0 1149 if (!unix_append_msgs (tstream,sf,df,au ? dst : NIL) ||
yuuji@0 1150 (fflush (df) == EOF) || fsync (fd)) {
yuuji@0 1151 sprintf (buf,"Message append failed: %s",strerror (errno));
yuuji@0 1152 MM_LOG (buf,ERROR);
yuuji@0 1153 ftruncate (fd,sbuf.st_size);
yuuji@0 1154 tp[0] = /* preserve \Marked status */
yuuji@0 1155 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
yuuji@0 1156 sbuf.st_atime : tp[1];
yuuji@0 1157 ret = NIL; /* return error */
yuuji@0 1158 }
yuuji@0 1159 else tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
yuuji@0 1160 utime (file,tp); /* set the times */
yuuji@0 1161 fclose (sf); /* done with scratch file */
yuuji@0 1162 /* force UIDVALIDITY assignment now */
yuuji@0 1163 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
yuuji@0 1164 /* return sets if doing APPENDUID */
yuuji@0 1165 if (au && ret) (*au) (mailbox,tstream->uid_validity,dst);
yuuji@0 1166 else mail_free_searchset (&dst);
yuuji@0 1167 unix_unlock (fd,NIL,&lock); /* unlock and close mailbox */
yuuji@0 1168 fclose (df); /* note that unix_unlock() released the fd */
yuuji@0 1169 if (tstream) { /* update last UID if we can */
yuuji@0 1170 UNIXLOCAL *local = (UNIXLOCAL *) tstream->local;
yuuji@0 1171 local->dirty = T; /* do a rewrite */
yuuji@0 1172 local->appending = T; /* but not at the cost of marking as old */
yuuji@0 1173 tstream = mail_close (tstream);
yuuji@0 1174 }
yuuji@0 1175 MM_NOCRITICAL (stream); /* release critical */
yuuji@0 1176 return ret;
yuuji@0 1177 }
yuuji@0 1178
yuuji@0 1179 /* Collect and write single message to append scratch file
yuuji@0 1180 * Accepts: MAIL stream
yuuji@0 1181 * scratch file
yuuji@0 1182 * flags
yuuji@0 1183 * date
yuuji@0 1184 * message stringstruct
yuuji@0 1185 * Returns: NIL if write error, else T
yuuji@0 1186 */
yuuji@0 1187
yuuji@0 1188 int unix_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
yuuji@0 1189 STRING *msg)
yuuji@0 1190 {
yuuji@0 1191 unsigned char *s,*t;
yuuji@0 1192 unsigned long uf;
yuuji@0 1193 long f = mail_parse_flags (stream,flags,&uf);
yuuji@0 1194 /* write metadata, note date ends with NL */
yuuji@0 1195 if (fprintf (sf,"%ld %lu %s",f,SIZE (msg) + 1,date) < 0) return NIL;
yuuji@0 1196 while (uf) /* write user flags */
yuuji@0 1197 if ((s = stream->user_flags[find_rightmost_bit (&uf)]) &&
yuuji@0 1198 (fprintf (sf," %s",s) < 0)) return NIL;
yuuji@0 1199 if (putc ('\n',sf) == EOF) return NIL;
yuuji@0 1200 while (SIZE (msg)) { /* copy text to scratch file */
yuuji@0 1201 for (s = (unsigned char *) msg->curpos, t = s + msg->cursize; s < t; ++s)
yuuji@0 1202 if (!*s) *s = 0x80; /* disallow NUL */
yuuji@0 1203 /* write buffered text */
yuuji@0 1204 if (fwrite (msg->curpos,1,msg->cursize,sf) == msg->cursize)
yuuji@0 1205 SETPOS (msg,GETPOS (msg) + msg->cursize);
yuuji@0 1206 else return NIL; /* failed */
yuuji@0 1207 }
yuuji@0 1208 /* write trailing newline and return */
yuuji@0 1209 return (putc ('\n',sf) == EOF) ? NIL : T;
yuuji@0 1210 }
yuuji@0 1211
yuuji@0 1212 /* Append messages from scratch file to mailbox
yuuji@0 1213 * Accepts: MAIL stream
yuuji@0 1214 * source file
yuuji@0 1215 * destination file
yuuji@0 1216 * uidset to update if non-NIL
yuuji@0 1217 * Returns: T if success, NIL if failure
yuuji@0 1218 */
yuuji@0 1219
yuuji@0 1220 int unix_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set)
yuuji@0 1221 {
yuuji@0 1222 int ti,zn,c;
yuuji@0 1223 long f;
yuuji@0 1224 unsigned long i,j;
yuuji@0 1225 char *x,tmp[MAILTMPLEN];
yuuji@0 1226 int hdrp = T;
yuuji@0 1227 /* get message metadata line */
yuuji@0 1228 while (fgets (tmp,MAILTMPLEN,sf)) {
yuuji@0 1229 if (!(isdigit (tmp[0]) && strchr (tmp,'\n'))) return NIL;
yuuji@0 1230 f = strtol (tmp,&x,10); /* get flags */
yuuji@0 1231 if (!((*x++ == ' ') && isdigit (*x))) return NIL;
yuuji@0 1232 i = strtoul (x,&x,10); /* get message size */
yuuji@0 1233 if ((*x++ != ' ') || /* build initial header */
yuuji@0 1234 (fprintf (df,"From %s@%s %sStatus: ",myusername(),mylocalhost(),x)<0)||
yuuji@0 1235 (f&fSEEN && (putc ('R',df) == EOF)) ||
yuuji@0 1236 (fputs ("\nX-Status: ",df) == EOF) ||
yuuji@0 1237 (f&fDELETED && (putc ('D',df) == EOF)) ||
yuuji@0 1238 (f&fFLAGGED && (putc ('F',df) == EOF)) ||
yuuji@0 1239 (f&fANSWERED && (putc ('A',df) == EOF)) ||
yuuji@0 1240 (f&fDRAFT && (putc ('T',df) == EOF)) ||
yuuji@0 1241 (fputs ("\nX-Keywords:",df) == EOF)) return NIL;
yuuji@0 1242 /* copy keywords */
yuuji@0 1243 while ((c = getc (sf)) != '\n') switch (c) {
yuuji@0 1244 case EOF:
yuuji@0 1245 return NIL;
yuuji@0 1246 default:
yuuji@0 1247 if (putc (c,df) == EOF) return NIL;
yuuji@0 1248 }
yuuji@0 1249 if ((putc ('\n',df) == EOF) ||
yuuji@0 1250 (set && (fprintf (df,"X-UID: %lu\n",++(stream->uid_last)) < 0)))
yuuji@0 1251 return NIL;
yuuji@0 1252
yuuji@0 1253 for (c = '\n'; i && fgets (tmp,MAILTMPLEN,sf); c = tmp[j-1]) {
yuuji@0 1254 /* get read line length */
yuuji@0 1255 if (i < (j = strlen (tmp))) fatal ("unix_append_msgs overrun");
yuuji@0 1256 i -= j; /* number of bytes left */
yuuji@0 1257 /* squish out CRs (note also copies NUL) */
yuuji@0 1258 for (x = tmp; x = strchr (x,'\r'); --j) memmove (x,x+1,j-(x-tmp));
yuuji@0 1259 if (!j) continue; /* do nothing if line emptied */
yuuji@0 1260 /* start of line? */
yuuji@0 1261 if ((c == '\n')) switch (tmp[0]) {
yuuji@0 1262 case 'F': /* possible "From " (case counts here) */
yuuji@0 1263 if ((j > 4) && (tmp[0] == 'F') && (tmp[1] == 'r') && (tmp[2] == 'o') &&
yuuji@0 1264 (tmp[3] == 'm') && (tmp[4] == ' ')) {
yuuji@0 1265 if (!unix_fromwidget) {
yuuji@0 1266 VALID (tmp,x,ti,zn);/* conditional, only write widget if */
yuuji@0 1267 if (!ti) break; /* it looks like a valid header */
yuuji@0 1268 } /* write the widget */
yuuji@0 1269 if (putc ('>',df) == EOF) return NIL;
yuuji@0 1270 }
yuuji@0 1271 break;
yuuji@0 1272 case 'S': case 's': /* possible "Status:" */
yuuji@0 1273 if (hdrp && (j > 6) && ((tmp[1] == 't') || (tmp[1] == 'T')) &&
yuuji@0 1274 ((tmp[2] == 'a') || (tmp[2] == 'A')) &&
yuuji@0 1275 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
yuuji@0 1276 ((tmp[4] == 'u') || (tmp[4] == 'U')) &&
yuuji@0 1277 ((tmp[5] == 's') || (tmp[5] == 'S')) && (tmp[6] == ':') &&
yuuji@0 1278 (fputs ("X-Original-",df) == EOF)) return NIL;
yuuji@0 1279 break;
yuuji@0 1280 case 'X': case 'x': /* possible X-??? header */
yuuji@0 1281 if (hdrp && (tmp[1] == '-') &&
yuuji@0 1282 /* possible X-UID: */
yuuji@0 1283 (((j > 5) && ((tmp[2] == 'U') || (tmp[2] == 'u')) &&
yuuji@0 1284 ((tmp[3] == 'I') || (tmp[3] == 'i')) &&
yuuji@0 1285 ((tmp[4] == 'D') || (tmp[4] == 'd')) && (tmp[5] == ':')) ||
yuuji@0 1286 /* possible X-IMAP: */
yuuji@0 1287 ((j > 6) && ((tmp[2] == 'I') || (tmp[2] == 'i')) &&
yuuji@0 1288 ((tmp[3] == 'M') || (tmp[3] == 'm')) &&
yuuji@0 1289 ((tmp[4] == 'A') || (tmp[4] == 'a')) &&
yuuji@0 1290 ((tmp[5] == 'P') || (tmp[5] == 'p')) &&
yuuji@0 1291 ((tmp[6] == ':') ||
yuuji@0 1292 /* or X-IMAPbase: */
yuuji@0 1293 ((j > 10) && ((tmp[6] == 'b') || (tmp[6] == 'B')) &&
yuuji@0 1294 ((tmp[7] == 'a') || (tmp[7] == 'A')) &&
yuuji@0 1295 ((tmp[8] == 's') || (tmp[8] == 'S')) &&
yuuji@0 1296 ((tmp[9] == 'e') || (tmp[9] == 'E')) && (tmp[10] == ':')))) ||
yuuji@0 1297 /* possible X-Status: */
yuuji@0 1298 ((j > 8) && ((tmp[2] == 'S') || (tmp[2] == 's')) &&
yuuji@0 1299 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
yuuji@0 1300 ((tmp[4] == 'a') || (tmp[4] == 'A')) &&
yuuji@0 1301 ((tmp[5] == 't') || (tmp[5] == 'T')) &&
yuuji@0 1302 ((tmp[6] == 'u') || (tmp[6] == 'U')) &&
yuuji@0 1303 ((tmp[7] == 's') || (tmp[7] == 'S')) && (tmp[8] == ':')) ||
yuuji@0 1304 /* possible X-Keywords: */
yuuji@0 1305 ((j > 10) && ((tmp[2] == 'K') || (tmp[2] == 'k')) &&
yuuji@0 1306 ((tmp[3] == 'e') || (tmp[3] == 'E')) &&
yuuji@0 1307 ((tmp[4] == 'y') || (tmp[4] == 'Y')) &&
yuuji@0 1308 ((tmp[5] == 'w') || (tmp[5] == 'W')) &&
yuuji@0 1309 ((tmp[6] == 'o') || (tmp[6] == 'O')) &&
yuuji@0 1310 ((tmp[7] == 'r') || (tmp[7] == 'R')) &&
yuuji@0 1311 ((tmp[8] == 'd') || (tmp[8] == 'D')) &&
yuuji@0 1312 ((tmp[9] == 's') || (tmp[9] == 'S')) && (tmp[10] == ':'))) &&
yuuji@0 1313 (fputs ("X-Original-",df) == EOF)) return NIL;
yuuji@0 1314 case '\n': /* blank line */
yuuji@0 1315 hdrp = NIL;
yuuji@0 1316 break;
yuuji@0 1317 default: /* nothing to do */
yuuji@0 1318 break;
yuuji@0 1319 }
yuuji@0 1320 /* just write the line */
yuuji@0 1321 if (fwrite (tmp,1,j,df) != j) return NIL;
yuuji@0 1322 }
yuuji@0 1323 if (i) return NIL; /* didn't read entire message */
yuuji@0 1324 /* update set */
yuuji@0 1325 if (stream) mail_append_set (set,stream->uid_last);
yuuji@0 1326 }
yuuji@0 1327 return T;
yuuji@0 1328 }
yuuji@0 1329
yuuji@0 1330 /* Internal routines */
yuuji@0 1331
yuuji@0 1332
yuuji@0 1333 /* UNIX mail abort stream
yuuji@0 1334 * Accepts: MAIL stream
yuuji@0 1335 */
yuuji@0 1336
yuuji@0 1337 void unix_abort (MAILSTREAM *stream)
yuuji@0 1338 {
yuuji@0 1339 if (LOCAL) { /* only if a file is open */
yuuji@0 1340 if (LOCAL->fd >= 0) close (LOCAL->fd);
yuuji@0 1341 if (LOCAL->ld >= 0) { /* have a mailbox lock? */
yuuji@0 1342 flock (LOCAL->ld,LOCK_UN);/* yes, release the lock */
yuuji@0 1343 close (LOCAL->ld); /* close the lock file */
yuuji@0 1344 unlink (LOCAL->lname); /* and delete it */
yuuji@0 1345 }
yuuji@0 1346 if (LOCAL->lname) fs_give ((void **) &LOCAL->lname);
yuuji@0 1347 /* free local text buffers */
yuuji@0 1348 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
yuuji@0 1349 if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
yuuji@0 1350 if (LOCAL->linebuf) fs_give ((void **) &LOCAL->linebuf);
yuuji@0 1351 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
yuuji@0 1352 /* nuke the local data */
yuuji@0 1353 fs_give ((void **) &stream->local);
yuuji@0 1354 stream->dtb = NIL; /* log out the DTB */
yuuji@0 1355 }
yuuji@0 1356 }
yuuji@0 1357
yuuji@0 1358 /* UNIX open and lock mailbox
yuuji@0 1359 * Accepts: file name to open/lock
yuuji@0 1360 * file open mode
yuuji@0 1361 * destination buffer for lock file name
yuuji@0 1362 * type of locking operation (LOCK_SH or LOCK_EX)
yuuji@0 1363 */
yuuji@0 1364
yuuji@0 1365 int unix_lock (char *file,int flags,int mode,DOTLOCK *lock,int op)
yuuji@0 1366 {
yuuji@0 1367 int fd;
yuuji@0 1368 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
yuuji@0 1369 (*bn) (BLOCK_FILELOCK,NIL);
yuuji@0 1370 /* try locking the easy way */
yuuji@0 1371 if (dotlock_lock (file,lock,-1)) {
yuuji@0 1372 /* got dotlock file, easy open */
yuuji@0 1373 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
yuuji@0 1374 else dotlock_unlock (lock); /* open failed, free the dotlock */
yuuji@0 1375 }
yuuji@0 1376 /* no dot lock file, open file now */
yuuji@0 1377 else if ((fd = open (file,flags,mode)) >= 0) {
yuuji@0 1378 /* try paranoid way to make a dot lock file */
yuuji@0 1379 if (dotlock_lock (file,lock,fd)) {
yuuji@0 1380 close (fd); /* get fresh fd in case of timing race */
yuuji@0 1381 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
yuuji@0 1382 /* open failed, free the dotlock */
yuuji@0 1383 else dotlock_unlock (lock);
yuuji@0 1384 }
yuuji@0 1385 else flock (fd,op); /* paranoid way failed, just flock() it */
yuuji@0 1386 }
yuuji@0 1387 (*bn) (BLOCK_NONE,NIL);
yuuji@0 1388 return fd;
yuuji@0 1389 }
yuuji@0 1390
yuuji@0 1391 /* UNIX unlock and close mailbox
yuuji@0 1392 * Accepts: file descriptor
yuuji@0 1393 * (optional) mailbox stream to check atime/mtime
yuuji@0 1394 * (optional) lock file name
yuuji@0 1395 */
yuuji@0 1396
yuuji@0 1397 void unix_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock)
yuuji@0 1398 {
yuuji@0 1399 if (stream) { /* need to muck with times? */
yuuji@0 1400 struct stat sbuf;
yuuji@0 1401 time_t tp[2];
yuuji@0 1402 time_t now = time (0);
yuuji@0 1403 fstat (fd,&sbuf); /* get file times */
yuuji@0 1404 if (LOCAL->ld >= 0) { /* yes, readwrite session? */
yuuji@0 1405 tp[0] = now; /* set atime to now */
yuuji@0 1406 /* set mtime to (now - 1) if necessary */
yuuji@0 1407 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
yuuji@0 1408 }
yuuji@0 1409 else if (stream->recent) { /* readonly with recent messages */
yuuji@0 1410 if ((sbuf.st_atime >= sbuf.st_mtime) ||
yuuji@0 1411 (sbuf.st_atime >= sbuf.st_ctime))
yuuji@0 1412 /* keep past mtime, whack back atime */
yuuji@0 1413 tp[0] = (tp[1] = (sbuf.st_mtime < now) ? sbuf.st_mtime : now) - 1;
yuuji@0 1414 else now = 0; /* no time change needed */
yuuji@0 1415 }
yuuji@0 1416 /* readonly with no recent messages */
yuuji@0 1417 else if ((sbuf.st_atime < sbuf.st_mtime) ||
yuuji@0 1418 (sbuf.st_atime < sbuf.st_ctime)) {
yuuji@0 1419 tp[0] = now; /* set atime to now */
yuuji@0 1420 /* set mtime to (now - 1) if necessary */
yuuji@0 1421 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
yuuji@0 1422 }
yuuji@0 1423 else now = 0; /* no time change needed */
yuuji@0 1424 /* set the times, note change */
yuuji@0 1425 if (now && !utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
yuuji@0 1426 }
yuuji@0 1427 flock (fd,LOCK_UN); /* release flock'ers */
yuuji@0 1428 if (!stream) close (fd); /* close the file if no stream */
yuuji@0 1429 dotlock_unlock (lock); /* flush the lock file if any */
yuuji@0 1430 }
yuuji@0 1431
yuuji@0 1432 /* UNIX mail parse and lock mailbox
yuuji@0 1433 * Accepts: MAIL stream
yuuji@0 1434 * space to write lock file name
yuuji@0 1435 * type of locking operation
yuuji@0 1436 * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure
yuuji@0 1437 */
yuuji@0 1438
yuuji@0 1439 int unix_parse (MAILSTREAM *stream,DOTLOCK *lock,int op)
yuuji@0 1440 {
yuuji@0 1441 int zn;
yuuji@0 1442 unsigned long i,j,k,m;
yuuji@0 1443 unsigned char c,*s,*t,*u,tmp[MAILTMPLEN],date[30];
yuuji@0 1444 int ti = 0,retain = T;
yuuji@0 1445 unsigned long nmsgs = stream->nmsgs;
yuuji@0 1446 unsigned long prevuid = nmsgs ? mail_elt (stream,nmsgs)->private.uid : 0;
yuuji@0 1447 unsigned long recent = stream->recent;
yuuji@0 1448 unsigned long oldnmsgs = stream->nmsgs;
yuuji@0 1449 short silent = stream->silent;
yuuji@0 1450 short pseudoseen = NIL;
yuuji@0 1451 struct stat sbuf;
yuuji@0 1452 STRING bs;
yuuji@0 1453 FDDATA d;
yuuji@0 1454 MESSAGECACHE *elt;
yuuji@0 1455 mail_lock (stream); /* guard against recursion or pingers */
yuuji@0 1456 /* toss out previous descriptor */
yuuji@0 1457 if (LOCAL->fd >= 0) close (LOCAL->fd);
yuuji@0 1458 MM_CRITICAL (stream); /* open and lock mailbox (shared OK) */
yuuji@0 1459 if ((LOCAL->fd = unix_lock (stream->mailbox,(LOCAL->ld >= 0) ?
yuuji@0 1460 O_RDWR : O_RDONLY,
yuuji@0 1461 (long)mail_parameters(NIL,GET_MBXPROTECTION,NIL),
yuuji@0 1462 lock,op)) < 0) {
yuuji@0 1463 sprintf (tmp,"Mailbox open failed, aborted: %s",strerror (errno));
yuuji@0 1464 MM_LOG (tmp,ERROR);
yuuji@0 1465 unix_abort (stream);
yuuji@0 1466 mail_unlock (stream);
yuuji@0 1467 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 1468 return NIL;
yuuji@0 1469 }
yuuji@0 1470 fstat (LOCAL->fd,&sbuf); /* get status */
yuuji@0 1471 /* validate change in size */
yuuji@0 1472 if (sbuf.st_size < LOCAL->filesize) {
yuuji@0 1473 sprintf (tmp,"Mailbox shrank from %lu to %lu bytes, aborted",
yuuji@0 1474 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
yuuji@0 1475 MM_LOG (tmp,ERROR); /* this is pretty bad */
yuuji@0 1476 unix_unlock (LOCAL->fd,stream,lock);
yuuji@0 1477 unix_abort (stream);
yuuji@0 1478 mail_unlock (stream);
yuuji@0 1479 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 1480 return NIL;
yuuji@0 1481 }
yuuji@0 1482
yuuji@0 1483 /* new data? */
yuuji@0 1484 else if (i = sbuf.st_size - LOCAL->filesize) {
yuuji@0 1485 d.fd = LOCAL->fd; /* yes, set up file descriptor */
yuuji@0 1486 d.pos = LOCAL->filesize; /* get to that position in the file */
yuuji@0 1487 d.chunk = LOCAL->buf; /* initial buffer chunk */
yuuji@0 1488 d.chunksize = CHUNKSIZE; /* file chunk size */
yuuji@0 1489 INIT (&bs,fd_string,&d,i); /* initialize stringstruct */
yuuji@0 1490 /* skip leading whitespace for broken MTAs */
yuuji@0 1491 while (((c = CHR (&bs)) == '\n') || (c == '\r') ||
yuuji@0 1492 (c == ' ') || (c == '\t')) SNX (&bs);
yuuji@0 1493 if (SIZE (&bs)) { /* read new data */
yuuji@0 1494 /* remember internal header position */
yuuji@0 1495 j = LOCAL->filesize + GETPOS (&bs);
yuuji@0 1496 s = unix_mbxline (stream,&bs,&i);
yuuji@0 1497 t = NIL,zn = 0;
yuuji@0 1498 if (i) VALID (s,t,ti,zn); /* see if valid From line */
yuuji@0 1499 if (!ti) { /* someone pulled the rug from under us */
yuuji@0 1500 sprintf (tmp,"Unexpected changes to mailbox (try restarting): %.20s",
yuuji@0 1501 (char *) s);
yuuji@0 1502 MM_LOG (tmp,ERROR);
yuuji@0 1503 unix_unlock (LOCAL->fd,stream,lock);
yuuji@0 1504 unix_abort (stream);
yuuji@0 1505 mail_unlock (stream);
yuuji@0 1506 /* done with critical */
yuuji@0 1507 MM_NOCRITICAL (stream);
yuuji@0 1508 return NIL;
yuuji@0 1509 }
yuuji@0 1510 stream->silent = T; /* quell main program new message events */
yuuji@0 1511 do { /* found a message */
yuuji@0 1512 /* instantiate first new message */
yuuji@0 1513 mail_exists (stream,++nmsgs);
yuuji@0 1514 (elt = mail_elt (stream,nmsgs))->valid = T;
yuuji@0 1515 recent++; /* assume recent by default */
yuuji@0 1516 elt->recent = T;
yuuji@0 1517 /* note position/size of internal header */
yuuji@0 1518 elt->private.special.offset = j;
yuuji@0 1519 elt->private.msg.header.offset = elt->private.special.text.size = i;
yuuji@0 1520
yuuji@0 1521 /* generate plausible IMAPish date string */
yuuji@0 1522 date[2] = date[6] = date[20] = '-'; date[11] = ' ';
yuuji@0 1523 date[14] = date[17] = ':';
yuuji@0 1524 /* dd */
yuuji@0 1525 date[0] = t[ti - 2]; date[1] = t[ti - 1];
yuuji@0 1526 /* mmm */
yuuji@0 1527 date[3] = t[ti - 6]; date[4] = t[ti - 5]; date[5] = t[ti - 4];
yuuji@0 1528 /* hh */
yuuji@0 1529 date[12] = t[ti + 1]; date[13] = t[ti + 2];
yuuji@0 1530 /* mm */
yuuji@0 1531 date[15] = t[ti + 4]; date[16] = t[ti + 5];
yuuji@0 1532 if (t[ti += 6] == ':') {/* ss */
yuuji@0 1533 date[18] = t[++ti]; date[19] = t[++ti];
yuuji@0 1534 ti++; /* move to space */
yuuji@0 1535 }
yuuji@0 1536 else date[18] = date[19] = '0';
yuuji@0 1537 /* yy -- advance over timezone if necessary */
yuuji@0 1538 if (zn == ti) ti += (((t[zn+1] == '+') || (t[zn+1] == '-')) ? 6 : 4);
yuuji@0 1539 date[7] = t[ti + 1]; date[8] = t[ti + 2];
yuuji@0 1540 date[9] = t[ti + 3]; date[10] = t[ti + 4];
yuuji@0 1541 /* zzz */
yuuji@0 1542 t = zn ? (t + zn + 1) : (unsigned char *) "LCL";
yuuji@0 1543 date[21] = *t++; date[22] = *t++; date[23] = *t++;
yuuji@0 1544 if ((date[21] != '+') && (date[21] != '-')) date[24] = '\0';
yuuji@0 1545 else { /* numeric time zone */
yuuji@0 1546 date[24] = *t++; date[25] = *t++;
yuuji@0 1547 date[26] = '\0'; date[20] = ' ';
yuuji@0 1548 }
yuuji@0 1549 /* set internal date */
yuuji@0 1550 if (!mail_parse_date (elt,date)) {
yuuji@0 1551 sprintf (tmp,"Unable to parse internal date: %s",(char *) date);
yuuji@0 1552 MM_LOG (tmp,WARN);
yuuji@0 1553 }
yuuji@0 1554
yuuji@0 1555 do { /* look for message body */
yuuji@0 1556 s = t = unix_mbxline (stream,&bs,&i);
yuuji@0 1557 if (i) switch (*s) { /* check header lines */
yuuji@0 1558 case 'X': /* possible X-???: line */
yuuji@0 1559 if (s[1] == '-') { /* must be immediately followed by hyphen */
yuuji@0 1560 /* X-Status: becomes Status: in S case */
yuuji@0 1561 if (s[2] == 'S' && s[3] == 't' && s[4] == 'a' && s[5] == 't' &&
yuuji@0 1562 s[6] == 'u' && s[7] == 's' && s[8] == ':') s += 2;
yuuji@0 1563 /* possible X-Keywords */
yuuji@0 1564 else if (s[2] == 'K' && s[3] == 'e' && s[4] == 'y' &&
yuuji@0 1565 s[5] == 'w' && s[6] == 'o' && s[7] == 'r' &&
yuuji@0 1566 s[8] == 'd' && s[9] == 's' && s[10] == ':') {
yuuji@0 1567 SIZEDTEXT uf;
yuuji@0 1568 retain = NIL; /* don't retain continuation */
yuuji@0 1569 s += 11; /* flush leading whitespace */
yuuji@0 1570 while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n'))){
yuuji@0 1571 while (*s == ' ') s++;
yuuji@0 1572 /* find end of keyword */
yuuji@0 1573 if (!(u = strpbrk (s," \n\r"))) u = s + strlen (s);
yuuji@0 1574 /* got a keyword? */
yuuji@0 1575 if ((k = (u - s)) && (k <= MAXUSERFLAG)) {
yuuji@0 1576 uf.data = (unsigned char *) s;
yuuji@0 1577 uf.size = k;
yuuji@0 1578 for (j = 0; (j < NUSERFLAGS) && stream->user_flags[j]; ++j)
yuuji@0 1579 if (!compare_csizedtext (stream->user_flags[j],&uf)) {
yuuji@0 1580 elt->user_flags |= ((long) 1) << j;
yuuji@0 1581 break;
yuuji@0 1582 }
yuuji@0 1583 }
yuuji@0 1584 s = u; /* advance to next keyword */
yuuji@0 1585 }
yuuji@0 1586 break;
yuuji@0 1587 }
yuuji@0 1588
yuuji@0 1589 /* possible X-IMAP */
yuuji@0 1590 else if ((s[2] == 'I') && (s[3] == 'M') && (s[4] == 'A') &&
yuuji@0 1591 (s[5] == 'P') && ((m = (s[6] == ':')) ||
yuuji@0 1592 ((s[6] == 'b') && (s[7] == 'a') &&
yuuji@0 1593 (s[8] == 's') && (s[9] == 'e') &&
yuuji@0 1594 (s[10] == ':')))) {
yuuji@0 1595 retain = NIL; /* don't retain continuation */
yuuji@0 1596 if ((nmsgs == 1) && !stream->uid_validity) {
yuuji@0 1597 /* advance to data */
yuuji@0 1598 s += m ? 7 : 11;
yuuji@0 1599 /* flush whitespace */
yuuji@0 1600 while (*s == ' ') s++;
yuuji@0 1601 j = 0; /* slurp UID validity */
yuuji@0 1602 /* found a digit? */
yuuji@0 1603 while (isdigit (*s)) {
yuuji@0 1604 j *= 10; /* yes, add it in */
yuuji@0 1605 j += *s++ - '0';
yuuji@0 1606 }
yuuji@0 1607 /* flush whitespace */
yuuji@0 1608 while (*s == ' ') s++;
yuuji@0 1609 /* must have valid UID validity and UID last */
yuuji@0 1610 if (j && isdigit (*s)) {
yuuji@0 1611 /* pseudo-header seen if X-IMAP */
yuuji@0 1612 if (m) pseudoseen = LOCAL->pseudo = T;
yuuji@0 1613 /* save UID validity */
yuuji@0 1614 stream->uid_validity = j;
yuuji@0 1615 j = 0; /* slurp UID last */
yuuji@0 1616 while (isdigit (*s)) {
yuuji@0 1617 j *= 10; /* yes, add it in */
yuuji@0 1618 j += *s++ - '0';
yuuji@0 1619 }
yuuji@0 1620 /* save UID last */
yuuji@0 1621 stream->uid_last = j;
yuuji@0 1622 /* process keywords */
yuuji@0 1623 for (j = 0; (*s != '\n') && ((*s != '\r')||(s[1] != '\n'));
yuuji@0 1624 s = u,j++) {
yuuji@0 1625 /* flush leading whitespace */
yuuji@0 1626 while (*s == ' ') s++;
yuuji@0 1627 u = strpbrk (s," \n\r");
yuuji@0 1628 /* got a keyword? */
yuuji@0 1629 if ((j < NUSERFLAGS) && (k = (u - s)) &&
yuuji@0 1630 (k <= MAXUSERFLAG)) {
yuuji@0 1631 if (stream->user_flags[j])
yuuji@0 1632 fs_give ((void **) &stream->user_flags[j]);
yuuji@0 1633 stream->user_flags[j] = (char *) fs_get (k + 1);
yuuji@0 1634 strncpy (stream->user_flags[j],s,k);
yuuji@0 1635 stream->user_flags[j][k] = '\0';
yuuji@0 1636 }
yuuji@0 1637 }
yuuji@0 1638 }
yuuji@0 1639 }
yuuji@0 1640 break;
yuuji@0 1641 }
yuuji@0 1642
yuuji@0 1643 /* possible X-UID */
yuuji@0 1644 else if (s[2] == 'U' && s[3] == 'I' && s[4] == 'D' &&
yuuji@0 1645 s[5] == ':') {
yuuji@0 1646 retain = NIL; /* don't retain continuation */
yuuji@0 1647 /* only believe if have a UID validity */
yuuji@0 1648 if (stream->uid_validity && ((nmsgs > 1) || !pseudoseen)) {
yuuji@0 1649 s += 6; /* advance to UID value */
yuuji@0 1650 /* flush whitespace */
yuuji@0 1651 while (*s == ' ') s++;
yuuji@0 1652 j = 0;
yuuji@0 1653 /* found a digit? */
yuuji@0 1654 while (isdigit (*s)) {
yuuji@0 1655 j *= 10; /* yes, add it in */
yuuji@0 1656 j += *s++ - '0';
yuuji@0 1657 }
yuuji@0 1658 /* flush remainder of line */
yuuji@0 1659 while (*s != '\n') s++;
yuuji@0 1660 /* make sure not duplicated */
yuuji@0 1661 if (elt->private.uid)
yuuji@0 1662 sprintf (tmp,"Message %lu UID %lu already has UID %lu",
yuuji@0 1663 pseudoseen ? elt->msgno - 1 : elt->msgno,
yuuji@0 1664 j,elt->private.uid);
yuuji@0 1665 /* make sure UID doesn't go backwards */
yuuji@0 1666 else if (j <= prevuid)
yuuji@0 1667 sprintf (tmp,"Message %lu UID %lu less than %lu",
yuuji@0 1668 pseudoseen ? elt->msgno - 1 : elt->msgno,
yuuji@0 1669 j,prevuid + 1);
yuuji@0 1670 #if 0 /* this is currently broken by UIDPLUS */
yuuji@0 1671 /* or skip by mailbox's recorded last */
yuuji@0 1672 else if (j > stream->uid_last)
yuuji@0 1673 sprintf (tmp,"Message %lu UID %lu greater than last %lu",
yuuji@0 1674 pseudoseen ? elt->msgno - 1 : elt->msgno,
yuuji@0 1675 j,stream->uid_last);
yuuji@0 1676 #endif
yuuji@0 1677 else { /* normal UID case */
yuuji@0 1678 prevuid = elt->private.uid = j;
yuuji@0 1679 #if 1 /* temporary kludge for UIDPLUS */
yuuji@0 1680 if (prevuid > stream->uid_last) {
yuuji@0 1681 stream->uid_last = prevuid;
yuuji@0 1682 LOCAL->ddirty = LOCAL->dirty = T;
yuuji@0 1683 }
yuuji@0 1684 #endif
yuuji@0 1685 break; /* exit this cruft */
yuuji@0 1686 }
yuuji@0 1687 MM_LOG (tmp,WARN);
yuuji@0 1688 /* invalidate UID validity */
yuuji@0 1689 stream->uid_validity = 0;
yuuji@0 1690 elt->private.uid = 0;
yuuji@0 1691 }
yuuji@0 1692 break;
yuuji@0 1693 }
yuuji@0 1694 }
yuuji@0 1695 /* otherwise fall into S case */
yuuji@0 1696
yuuji@0 1697 case 'S': /* possible Status: line */
yuuji@0 1698 if (s[0] == 'S' && s[1] == 't' && s[2] == 'a' && s[3] == 't' &&
yuuji@0 1699 s[4] == 'u' && s[5] == 's' && s[6] == ':') {
yuuji@0 1700 retain = NIL; /* don't retain continuation */
yuuji@0 1701 s += 6; /* advance to status flags */
yuuji@0 1702 do switch (*s++) {/* parse flags */
yuuji@0 1703 case 'R': /* message read */
yuuji@0 1704 elt->seen = T;
yuuji@0 1705 break;
yuuji@0 1706 case 'O': /* message old */
yuuji@0 1707 if (elt->recent) {
yuuji@0 1708 elt->recent = NIL;
yuuji@0 1709 recent--; /* it really wasn't recent */
yuuji@0 1710 }
yuuji@0 1711 break;
yuuji@0 1712 case 'D': /* message deleted */
yuuji@0 1713 elt->deleted = T;
yuuji@0 1714 break;
yuuji@0 1715 case 'F': /* message flagged */
yuuji@0 1716 elt->flagged = T;
yuuji@0 1717 break;
yuuji@0 1718 case 'A': /* message answered */
yuuji@0 1719 elt->answered = T;
yuuji@0 1720 break;
yuuji@0 1721 case 'T': /* message is a draft */
yuuji@0 1722 elt->draft = T;
yuuji@0 1723 break;
yuuji@0 1724 default: /* some other crap */
yuuji@0 1725 break;
yuuji@0 1726 } while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n')));
yuuji@0 1727 break; /* all done */
yuuji@0 1728 }
yuuji@0 1729 /* otherwise fall into default case */
yuuji@0 1730
yuuji@0 1731 default: /* ordinary header line */
yuuji@0 1732 if ((*s == 'S') || (*s == 's') ||
yuuji@0 1733 (((*s == 'X') || (*s == 'x')) && (s[1] == '-'))) {
yuuji@0 1734 unsigned char *e,*v;
yuuji@0 1735 /* must match what mail_filter() does */
yuuji@0 1736 for (u = s,v = tmp,e = u + min (i,MAILTMPLEN - 1);
yuuji@0 1737 (u < e) && ((c = (*u ? *u : (*u = ' '))) != ':') &&
yuuji@0 1738 ((c > ' ') || ((c != ' ') && (c != '\t') &&
yuuji@0 1739 (c != '\r') && (c != '\n')));
yuuji@0 1740 *v++ = *u++);
yuuji@0 1741 *v = '\0'; /* tie off */
yuuji@0 1742 /* matches internal header? */
yuuji@0 1743 if (!compare_cstring (tmp,"STATUS") ||
yuuji@0 1744 !compare_cstring (tmp,"X-STATUS") ||
yuuji@0 1745 !compare_cstring (tmp,"X-KEYWORDS") ||
yuuji@0 1746 !compare_cstring (tmp,"X-UID") ||
yuuji@0 1747 !compare_cstring (tmp,"X-IMAP") ||
yuuji@0 1748 !compare_cstring (tmp,"X-IMAPBASE")) {
yuuji@0 1749 char err[MAILTMPLEN];
yuuji@0 1750 sprintf (err,"Discarding bogus %s header in message %lu",
yuuji@0 1751 (char *) tmp,elt->msgno);
yuuji@0 1752 MM_LOG (err,WARN);
yuuji@0 1753 retain = NIL; /* don't retain continuation */
yuuji@0 1754 break; /* different case or something */
yuuji@0 1755 }
yuuji@0 1756 }
yuuji@0 1757 /* retain or non-continuation? */
yuuji@0 1758 if (retain || ((*s != ' ') && (*s != '\t'))) {
yuuji@0 1759 retain = T; /* retaining continuation now */
yuuji@0 1760 /* line length in LF format newline */
yuuji@0 1761 for (j = k = 0; j < i; ++j) if (s[j] != '\r') ++k;
yuuji@0 1762 /* "internal" header size */
yuuji@0 1763 elt->private.spare.data += k;
yuuji@0 1764 /* message size */
yuuji@0 1765 elt->rfc822_size += k + 1;
yuuji@0 1766 }
yuuji@0 1767 else {
yuuji@0 1768 char err[MAILTMPLEN];
yuuji@0 1769 sprintf (err,"Discarding bogus continuation in msg %lu: %.80s",
yuuji@0 1770 elt->msgno,(char *) s);
yuuji@0 1771 if (u = strpbrk (err,"\r\n")) *u = '\0';
yuuji@0 1772 MM_LOG (err,WARN);
yuuji@0 1773 break; /* different case or something */
yuuji@0 1774 }
yuuji@0 1775 break;
yuuji@0 1776 }
yuuji@0 1777 } while (i && (*t != '\n') && ((*t != '\r') || (t[1] != '\n')));
yuuji@0 1778 /* "internal" header sans trailing newline */
yuuji@0 1779 if (i) elt->private.spare.data--;
yuuji@0 1780 /* assign a UID if none found */
yuuji@0 1781 if (((nmsgs > 1) || !pseudoseen) && !elt->private.uid) {
yuuji@0 1782 prevuid = elt->private.uid = ++stream->uid_last;
yuuji@0 1783 elt->private.dirty = T;
yuuji@0 1784 LOCAL->ddirty = T; /* force update */
yuuji@0 1785 }
yuuji@0 1786 else elt->private.dirty = elt->recent;
yuuji@0 1787
yuuji@0 1788 /* note size of header, location of text */
yuuji@0 1789 elt->private.msg.header.text.size =
yuuji@0 1790 (elt->private.msg.text.offset =
yuuji@0 1791 (LOCAL->filesize + GETPOS (&bs)) - elt->private.special.offset) -
yuuji@0 1792 elt->private.special.text.size;
yuuji@0 1793 k = m = 0; /* no previous line size yet */
yuuji@0 1794 /* note current position */
yuuji@0 1795 j = LOCAL->filesize + GETPOS (&bs);
yuuji@0 1796 if (i) do { /* look for next message */
yuuji@0 1797 s = unix_mbxline (stream,&bs,&i);
yuuji@0 1798 if (i) { /* got new data? */
yuuji@0 1799 VALID (s,t,ti,zn); /* yes, parse line */
yuuji@0 1800 if (!ti) { /* not a header line, add it to message */
yuuji@0 1801 elt->rfc822_size += i;
yuuji@0 1802 for (j = 0; j < i; ++j) switch (s[j]) {
yuuji@0 1803 case '\r': /* squeeze out CRs */
yuuji@0 1804 elt->rfc822_size -= 1;
yuuji@0 1805 break;
yuuji@0 1806 case '\n': /* LF becomes CRLF */
yuuji@0 1807 elt->rfc822_size += 1;
yuuji@0 1808 break;
yuuji@0 1809 default:
yuuji@0 1810 break;
yuuji@0 1811 }
yuuji@0 1812 if ((i == 1) && (*s == '\n')) {
yuuji@0 1813 k = 2;
yuuji@0 1814 m = 1;
yuuji@0 1815 }
yuuji@0 1816 else if ((i == 2) && (*s == '\r') && (s[1] == '\n'))
yuuji@0 1817 k = m = 2;
yuuji@0 1818 else k = m = 0; /* file does not end with newline! */
yuuji@0 1819 /* update current position */
yuuji@0 1820 j = LOCAL->filesize + GETPOS (&bs);
yuuji@0 1821 }
yuuji@0 1822 }
yuuji@0 1823 } while (i && !ti); /* until found a header */
yuuji@0 1824 elt->private.msg.text.text.size = j -
yuuji@0 1825 (elt->private.special.offset + elt->private.msg.text.offset);
yuuji@0 1826 /* flush ending blank line */
yuuji@0 1827 elt->private.msg.text.text.size -= m;
yuuji@0 1828 elt->rfc822_size -= k;
yuuji@0 1829 /* until end of buffer */
yuuji@0 1830 } while (!stream->sniff && i);
yuuji@0 1831 if (pseudoseen) { /* flush pseudo-message if present */
yuuji@0 1832 /* decrement recent count */
yuuji@0 1833 if (mail_elt (stream,1)->recent) recent--;
yuuji@0 1834 /* and the exists count */
yuuji@0 1835 mail_exists (stream,nmsgs--);
yuuji@0 1836 mail_expunged(stream,1);/* fake an expunge of that message */
yuuji@0 1837 }
yuuji@0 1838 /* need to start a new UID validity? */
yuuji@0 1839 if (!stream->uid_validity) {
yuuji@0 1840 stream->uid_validity = time (0);
yuuji@0 1841 /* in case a whiner with no life */
yuuji@0 1842 if (mail_parameters (NIL,GET_USERHASNOLIFE,NIL))
yuuji@0 1843 stream->uid_nosticky = T;
yuuji@0 1844 else if (nmsgs) { /* don't bother if empty file */
yuuji@0 1845 /* make dirty to restart UID epoch */
yuuji@0 1846 LOCAL->ddirty = LOCAL->dirty = T;
yuuji@0 1847 /* need to rewrite msg 1 if not pseudo */
yuuji@0 1848 if (!LOCAL->pseudo) mail_elt (stream,1)->private.dirty = T;
yuuji@0 1849 MM_LOG ("Assigning new unique identifiers to all messages",NIL);
yuuji@0 1850 }
yuuji@0 1851 }
yuuji@0 1852 stream->nmsgs = oldnmsgs; /* whack it back down */
yuuji@0 1853 stream->silent = silent; /* restore old silent setting */
yuuji@0 1854 /* notify upper level of new mailbox sizes */
yuuji@0 1855 mail_exists (stream,nmsgs);
yuuji@0 1856 mail_recent (stream,recent);
yuuji@0 1857 /* mark dirty so O flags are set */
yuuji@0 1858 if (recent) LOCAL->dirty = T;
yuuji@0 1859 }
yuuji@0 1860 }
yuuji@0 1861 /* no change, don't babble if never got time */
yuuji@0 1862 else if (LOCAL->filetime && LOCAL->filetime != sbuf.st_mtime)
yuuji@0 1863 MM_LOG ("New mailbox modification time but apparently no changes",WARN);
yuuji@0 1864 /* update parsed file size and time */
yuuji@0 1865 LOCAL->filesize = sbuf.st_size;
yuuji@0 1866 LOCAL->filetime = sbuf.st_mtime;
yuuji@0 1867 return T; /* return the winnage */
yuuji@0 1868 }
yuuji@0 1869
yuuji@0 1870 /* UNIX read line from mailbox
yuuji@0 1871 * Accepts: mail stream
yuuji@0 1872 * stringstruct
yuuji@0 1873 * pointer to line size
yuuji@0 1874 * Returns: pointer to input line
yuuji@0 1875 */
yuuji@0 1876
yuuji@0 1877 char *unix_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size)
yuuji@0 1878 {
yuuji@0 1879 unsigned long i,j,k,m;
yuuji@0 1880 char *s,*t,*te;
yuuji@0 1881 char *ret = "";
yuuji@0 1882 /* flush old buffer */
yuuji@0 1883 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
yuuji@0 1884 /* if buffer needs refreshing */
yuuji@0 1885 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
yuuji@0 1886 if (SIZE (bs)) { /* find newline */
yuuji@0 1887 /* end of fast scan */
yuuji@0 1888 te = (t = (s = bs->curpos) + bs->cursize) - 12;
yuuji@0 1889 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
yuuji@0 1890 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
yuuji@0 1891 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
yuuji@0 1892 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
yuuji@0 1893 --s; /* back up */
yuuji@0 1894 break; /* exit loop */
yuuji@0 1895 }
yuuji@0 1896 /* final character-at-a-time scan */
yuuji@0 1897 while ((s < t) && (*s != '\n')) ++s;
yuuji@0 1898 /* difficult case if line spans buffer */
yuuji@0 1899 if ((i = s - bs->curpos) == bs->cursize) {
yuuji@0 1900 /* have space in line buffer? */
yuuji@0 1901 if (i > LOCAL->linebuflen) {
yuuji@0 1902 fs_give ((void **) &LOCAL->linebuf);
yuuji@0 1903 LOCAL->linebuf = (char *) fs_get (LOCAL->linebuflen = i);
yuuji@0 1904 }
yuuji@0 1905 /* remember what we have so far */
yuuji@0 1906 memcpy (LOCAL->linebuf,bs->curpos,i);
yuuji@0 1907 /* load next buffer */
yuuji@0 1908 SETPOS (bs,k = GETPOS (bs) + i);
yuuji@0 1909 /* end of fast scan */
yuuji@0 1910 te = (t = (s = bs->curpos) + bs->cursize) - 12;
yuuji@0 1911 /* fast scan in overlap buffer */
yuuji@0 1912 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
yuuji@0 1913 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
yuuji@0 1914 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
yuuji@0 1915 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
yuuji@0 1916 --s; /* back up */
yuuji@0 1917 break; /* exit loop */
yuuji@0 1918 }
yuuji@0 1919
yuuji@0 1920 /* final character-at-a-time scan */
yuuji@0 1921 while ((s < t) && (*s != '\n')) ++s;
yuuji@0 1922 /* huge line? */
yuuji@0 1923 if ((j = s - bs->curpos) == bs->cursize) {
yuuji@0 1924 SETPOS (bs,GETPOS (bs) + j);
yuuji@0 1925 /* look for end of line (s-l-o-w!!) */
yuuji@0 1926 for (m = SIZE (bs); m && (SNX (bs) != '\n'); --m,++j);
yuuji@0 1927 SETPOS (bs,k); /* go back to where it started */
yuuji@0 1928 }
yuuji@0 1929 /* got size of data, make buffer for return */
yuuji@0 1930 ret = LOCAL->line = (char *) fs_get (i + j + 2);
yuuji@0 1931 /* copy first chunk */
yuuji@0 1932 memcpy (ret,LOCAL->linebuf,i);
yuuji@0 1933 while (j) { /* copy remainder */
yuuji@0 1934 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
yuuji@0 1935 memcpy (ret + i,bs->curpos,k = min (j,bs->cursize));
yuuji@0 1936 i += k; /* account for this much read in */
yuuji@0 1937 j -= k;
yuuji@0 1938 bs->curpos += k; /* increment new position */
yuuji@0 1939 bs->cursize -= k; /* eat that many bytes */
yuuji@0 1940 }
yuuji@0 1941 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
yuuji@0 1942 /* read newline at end */
yuuji@0 1943 if (SIZE (bs)) ret[i++] = SNX (bs);
yuuji@0 1944 ret[i] = '\0'; /* makes debugging easier */
yuuji@0 1945 }
yuuji@0 1946 else { /* this is easy */
yuuji@0 1947 ret = bs->curpos; /* string it at this position */
yuuji@0 1948 bs->curpos += ++i; /* increment new position */
yuuji@0 1949 bs->cursize -= i; /* eat that many bytes */
yuuji@0 1950 }
yuuji@0 1951 *size = i; /* return that to user */
yuuji@0 1952 }
yuuji@0 1953 else *size = 0; /* end of data, return empty */
yuuji@0 1954 return ret;
yuuji@0 1955 }
yuuji@0 1956
yuuji@0 1957 /* UNIX make pseudo-header
yuuji@0 1958 * Accepts: MAIL stream
yuuji@0 1959 * buffer to write pseudo-header
yuuji@0 1960 * Returns: length of pseudo-header
yuuji@0 1961 */
yuuji@0 1962
yuuji@0 1963 unsigned long unix_pseudo (MAILSTREAM *stream,char *hdr)
yuuji@0 1964 {
yuuji@0 1965 int i;
yuuji@0 1966 char *s,tmp[MAILTMPLEN];
yuuji@0 1967 time_t now = time (0);
yuuji@0 1968 rfc822_fixed_date (tmp);
yuuji@0 1969 sprintf (hdr,"From %s %.24s\nDate: %s\nFrom: %s <%s@%.80s>\nSubject: %s\nMessage-ID: <%lu@%.80s>\nX-IMAP: %010lu %010lu",
yuuji@0 1970 pseudo_from,ctime (&now),
yuuji@0 1971 tmp,pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
yuuji@0 1972 (unsigned long) now,mylocalhost (),stream->uid_validity,
yuuji@0 1973 stream->uid_last);
yuuji@0 1974 for (s = hdr + strlen (hdr),i = 0; i < NUSERFLAGS; ++i)
yuuji@0 1975 if (stream->user_flags[i])
yuuji@0 1976 sprintf (s += strlen (s)," %s",stream->user_flags[i]);
yuuji@0 1977 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n\n",pseudo_msg);
yuuji@0 1978 return strlen (hdr); /* return header length */
yuuji@0 1979 }
yuuji@0 1980
yuuji@0 1981 /* UNIX make status string
yuuji@0 1982 * Accepts: MAIL stream
yuuji@0 1983 * destination string to write
yuuji@0 1984 * message cache entry
yuuji@0 1985 * UID to write if non-zero (else use elt->private.uid)
yuuji@0 1986 * non-zero flag to write UID (.LT. 0 to write UID base info too)
yuuji@0 1987 * Returns: length of string
yuuji@0 1988 */
yuuji@0 1989
yuuji@0 1990 unsigned long unix_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
yuuji@0 1991 unsigned long uid,long flag)
yuuji@0 1992 {
yuuji@0 1993 char *t,stack[64];
yuuji@0 1994 char *s = status;
yuuji@0 1995 unsigned long n;
yuuji@0 1996 int pad = 50;
yuuji@0 1997 int sticky = uid ? T : !stream->uid_nosticky;
yuuji@0 1998 /* This used to use sprintf(), but thanks to certain cretinous C libraries
yuuji@0 1999 with horribly slow implementations of sprintf() I had to change it to this
yuuji@0 2000 mess. At least it should be fast. */
yuuji@0 2001 if ((flag < 0) && sticky) { /* need to write X-IMAPbase: header? */
yuuji@0 2002 *s++ = 'X'; *s++ = '-'; *s++ = 'I'; *s++ = 'M'; *s++ = 'A'; *s++ = 'P';
yuuji@0 2003 *s++ = 'b'; *s++ = 'a'; *s++ = 's'; *s++ = 'e'; *s++ = ':'; *s++ = ' ';
yuuji@0 2004 t = stack;
yuuji@0 2005 n = stream->uid_validity; /* push UID validity digits on the stack */
yuuji@0 2006 do *t++ = (char) (n % 10) + '0';
yuuji@0 2007 while (n /= 10);
yuuji@0 2008 /* pop UID validity digits from stack */
yuuji@0 2009 while (t > stack) *s++ = *--t;
yuuji@0 2010 *s++ = ' ';
yuuji@0 2011 n = stream->uid_last; /* push UID last digits on the stack */
yuuji@0 2012 do *t++ = (char) (n % 10) + '0';
yuuji@0 2013 while (n /= 10);
yuuji@0 2014 /* pop UID last digits from stack */
yuuji@0 2015 while (t > stack) *s++ = *--t;
yuuji@0 2016 for (n = 0; n < NUSERFLAGS; ++n) if (t = stream->user_flags[n])
yuuji@0 2017 for (*s++ = ' '; *t; *s++ = *t++);
yuuji@0 2018 *s++ = '\n';
yuuji@0 2019 pad += 30; /* increased padding if have IMAPbase */
yuuji@0 2020 }
yuuji@0 2021 *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't'; *s++ = 'u'; *s++ = 's';
yuuji@0 2022 *s++ = ':'; *s++ = ' ';
yuuji@0 2023 if (elt->seen) *s++ = 'R';
yuuji@0 2024 /* only write O if have a UID */
yuuji@0 2025 if (flag && (!elt->recent || !LOCAL->appending)) *s++ = 'O';
yuuji@0 2026 *s++ = '\n';
yuuji@0 2027 *s++ = 'X'; *s++ = '-'; *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't';
yuuji@0 2028 *s++ = 'u'; *s++ = 's'; *s++ = ':'; *s++ = ' ';
yuuji@0 2029 if (elt->deleted) *s++ = 'D';
yuuji@0 2030 if (elt->flagged) *s++ = 'F';
yuuji@0 2031 if (elt->answered) *s++ = 'A';
yuuji@0 2032 if (elt->draft) *s++ = 'T';
yuuji@0 2033 *s++ = '\n';
yuuji@0 2034
yuuji@0 2035 if (sticky) { /* only do this if UIDs sticky */
yuuji@0 2036 *s++ = 'X'; *s++ = '-'; *s++ = 'K'; *s++ = 'e'; *s++ = 'y'; *s++ = 'w';
yuuji@0 2037 *s++ = 'o'; *s++ = 'r'; *s++ = 'd'; *s++ = 's'; *s++ = ':';
yuuji@0 2038 if (n = elt->user_flags) do {
yuuji@0 2039 *s++ = ' ';
yuuji@0 2040 for (t = stream->user_flags[find_rightmost_bit (&n)]; *t; *s++ = *t++);
yuuji@0 2041 } while (n);
yuuji@0 2042 n = s - status; /* get size of stuff so far */
yuuji@0 2043 /* pad X-Keywords to make size constant */
yuuji@0 2044 if (n < pad) for (n = pad - n; n > 0; --n) *s++ = ' ';
yuuji@0 2045 *s++ = '\n';
yuuji@0 2046 if (flag) { /* want to include UID? */
yuuji@0 2047 t = stack;
yuuji@0 2048 /* push UID digits on the stack */
yuuji@0 2049 n = uid ? uid : elt->private.uid;
yuuji@0 2050 do *t++ = (char) (n % 10) + '0';
yuuji@0 2051 while (n /= 10);
yuuji@0 2052 *s++ = 'X'; *s++ = '-'; *s++ = 'U'; *s++ = 'I'; *s++ = 'D'; *s++ = ':';
yuuji@0 2053 *s++ = ' ';
yuuji@0 2054 /* pop UID from stack */
yuuji@0 2055 while (t > stack) *s++ = *--t;
yuuji@0 2056 *s++ = '\n';
yuuji@0 2057 }
yuuji@0 2058 }
yuuji@0 2059 *s++ = '\n'; *s = '\0'; /* end of extended message status */
yuuji@0 2060 return s - status; /* return size of resulting string */
yuuji@0 2061 }
yuuji@0 2062
yuuji@0 2063 /* Rewrite mailbox file
yuuji@0 2064 * Accepts: MAIL stream, must be critical and locked
yuuji@0 2065 * return pointer to number of expunged messages if want expunge
yuuji@0 2066 * lock file name
yuuji@0 2067 * expunge sequence, not deleted flag
yuuji@0 2068 * Returns: T if success and mailbox unlocked, NIL if failure
yuuji@0 2069 */
yuuji@0 2070
yuuji@0 2071 #define OVERFLOWBUFLEN 8192 /* initial overflow buffer length */
yuuji@0 2072
yuuji@0 2073 long unix_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
yuuji@0 2074 long flags)
yuuji@0 2075 {
yuuji@0 2076 MESSAGECACHE *elt;
yuuji@0 2077 UNIXFILE f;
yuuji@0 2078 char *s;
yuuji@0 2079 time_t tp[2];
yuuji@0 2080 long ret,flag;
yuuji@0 2081 unsigned long i,j;
yuuji@0 2082 unsigned long recent = stream->recent;
yuuji@0 2083 unsigned long size = LOCAL->pseudo ? unix_pseudo (stream,LOCAL->buf) : 0;
yuuji@0 2084 if (nexp) *nexp = 0; /* initially nothing expunged */
yuuji@0 2085 /* calculate size of mailbox after rewrite */
yuuji@0 2086 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs; i++) {
yuuji@0 2087 elt = mail_elt (stream,i); /* get cache */
yuuji@0 2088 if (!(nexp && elt->deleted && (flags ? elt->sequence : T))) {
yuuji@0 2089 /* add RFC822 size of this message */
yuuji@0 2090 size += elt->private.special.text.size + elt->private.spare.data +
yuuji@0 2091 unix_xstatus (stream,LOCAL->buf,elt,NIL,flag) +
yuuji@0 2092 elt->private.msg.text.text.size + 1;
yuuji@0 2093 flag = 1; /* only count X-IMAPbase once */
yuuji@0 2094 }
yuuji@0 2095 }
yuuji@0 2096 /* no messages, has a life, and no pseudo */
yuuji@0 2097 if (!size && !mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) {
yuuji@0 2098 LOCAL->pseudo = T; /* so make a pseudo-message now */
yuuji@0 2099 size = unix_pseudo (stream,LOCAL->buf);
yuuji@0 2100 }
yuuji@0 2101 /* extend the file as necessary */
yuuji@0 2102 if (ret = unix_extend (stream,size)) {
yuuji@0 2103 /* Set up buffered I/O file structure
yuuji@0 2104 * curpos current position being written through buffering
yuuji@0 2105 * filepos current position being written physically to the disk
yuuji@0 2106 * bufpos current position being written in the buffer
yuuji@0 2107 * protect current maximum position that can be written to the disk
yuuji@0 2108 * before buffering is forced
yuuji@0 2109 * The code tries to buffer so that that disk is written in multiples of
yuuji@0 2110 * OVERBLOWBUFLEN bytes.
yuuji@0 2111 */
yuuji@0 2112 f.stream = stream; /* note mail stream */
yuuji@0 2113 f.curpos = f.filepos = 0; /* start of file */
yuuji@0 2114 f.protect = stream->nmsgs ? /* initial protection pointer */
yuuji@0 2115 mail_elt (stream,1)->private.special.offset : 8192;
yuuji@0 2116 f.bufpos = f.buf = (char *) fs_get (f.buflen = OVERFLOWBUFLEN);
yuuji@0 2117
yuuji@0 2118 if (LOCAL->pseudo) /* update pseudo-header */
yuuji@0 2119 unix_write (&f,LOCAL->buf,unix_pseudo (stream,LOCAL->buf));
yuuji@0 2120 /* loop through all messages */
yuuji@0 2121 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs;) {
yuuji@0 2122 elt = mail_elt (stream,i);/* get cache */
yuuji@0 2123 /* expunge this message? */
yuuji@0 2124 if (nexp && elt->deleted && (flags ? elt->sequence : T)) {
yuuji@0 2125 /* one less recent message */
yuuji@0 2126 if (elt->recent) --recent;
yuuji@0 2127 mail_expunged(stream,i);/* notify upper levels */
yuuji@0 2128 ++*nexp; /* count up one more expunged message */
yuuji@0 2129 }
yuuji@0 2130 else { /* preserve this message */
yuuji@0 2131 i++; /* advance to next message */
yuuji@0 2132 if ((flag < 0) || /* need to rewrite message? */
yuuji@0 2133 elt->private.dirty || (f.curpos != elt->private.special.offset) ||
yuuji@0 2134 (elt->private.msg.header.text.size !=
yuuji@0 2135 (elt->private.spare.data +
yuuji@0 2136 unix_xstatus (stream,LOCAL->buf,elt,NIL,flag)))) {
yuuji@0 2137 unsigned long newoffset = f.curpos;
yuuji@0 2138 /* yes, seek to internal header */
yuuji@0 2139 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
yuuji@0 2140 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
yuuji@0 2141 /* see if need to squeeze out a CR */
yuuji@0 2142 if (LOCAL->buf[elt->private.special.text.size - 2] == '\r') {
yuuji@0 2143 LOCAL->buf[--elt->private.special.text.size - 1] = '\n';
yuuji@0 2144 --size; /* squeezed out a CR from PC */
yuuji@0 2145 }
yuuji@0 2146 /* protection pointer moves to RFC822 header */
yuuji@0 2147 f.protect = elt->private.special.offset +
yuuji@0 2148 elt->private.msg.header.offset;
yuuji@0 2149 /* write internal header */
yuuji@0 2150 unix_write (&f,LOCAL->buf,elt->private.special.text.size);
yuuji@0 2151 /* get RFC822 header */
yuuji@0 2152 s = unix_header (stream,elt->msgno,&j,FT_INTERNAL);
yuuji@0 2153 /* in case this got decremented */
yuuji@0 2154 elt->private.msg.header.offset = elt->private.special.text.size;
yuuji@0 2155 /* header size, sans trailing newline */
yuuji@0 2156 if ((j < 2) || (s[j - 2] == '\n')) j--;
yuuji@0 2157 /* this can happen if CRs were squeezed */
yuuji@0 2158 if (j < elt->private.spare.data) {
yuuji@0 2159 /* so fix up counts */
yuuji@0 2160 size -= elt->private.spare.data - j;
yuuji@0 2161 elt->private.spare.data = j;
yuuji@0 2162 }
yuuji@0 2163 else if (j != elt->private.spare.data)
yuuji@0 2164 fatal ("header size inconsistent");
yuuji@0 2165 /* protection pointer moves to RFC822 text */
yuuji@0 2166 f.protect = elt->private.special.offset +
yuuji@0 2167 elt->private.msg.text.offset;
yuuji@0 2168 unix_write (&f,s,j); /* write RFC822 header */
yuuji@0 2169 /* write status and UID */
yuuji@0 2170 unix_write (&f,LOCAL->buf,
yuuji@0 2171 j = unix_xstatus (stream,LOCAL->buf,elt,NIL,flag));
yuuji@0 2172 flag = 1; /* only write X-IMAPbase once */
yuuji@0 2173 /* new file header size */
yuuji@0 2174 elt->private.msg.header.text.size = elt->private.spare.data + j;
yuuji@0 2175
yuuji@0 2176 /* did text move? */
yuuji@0 2177 if (f.curpos != f.protect) {
yuuji@0 2178 /* get message text */
yuuji@0 2179 s = unix_text_work (stream,elt,&j,FT_INTERNAL);
yuuji@0 2180 /* this can happen if CRs were squeezed */
yuuji@0 2181 if (j < elt->private.msg.text.text.size) {
yuuji@0 2182 /* so fix up counts */
yuuji@0 2183 size -= elt->private.msg.text.text.size - j;
yuuji@0 2184 elt->private.msg.text.text.size = j;
yuuji@0 2185 }
yuuji@0 2186 /* can't happen it says here */
yuuji@0 2187 else if (j > elt->private.msg.text.text.size)
yuuji@0 2188 fatal ("text size inconsistent");
yuuji@0 2189 /* new text offset, status/UID may change it */
yuuji@0 2190 elt->private.msg.text.offset = f.curpos - newoffset;
yuuji@0 2191 /* protection pointer moves to next message */
yuuji@0 2192 f.protect = (i <= stream->nmsgs) ?
yuuji@0 2193 mail_elt (stream,i)->private.special.offset : (f.curpos + j + 1);
yuuji@0 2194 unix_write (&f,s,j);/* write text */
yuuji@0 2195 /* write trailing newline */
yuuji@0 2196 unix_write (&f,"\n",1);
yuuji@0 2197 }
yuuji@0 2198 else { /* tie off header and status */
yuuji@0 2199 unix_write (&f,NIL,NIL);
yuuji@0 2200 /* protection pointer moves to next message */
yuuji@0 2201 f.protect = (i <= stream->nmsgs) ?
yuuji@0 2202 mail_elt (stream,i)->private.special.offset : size;
yuuji@0 2203 /* locate end of message text */
yuuji@0 2204 j = f.filepos + elt->private.msg.text.text.size;
yuuji@0 2205 /* trailing newline already there? */
yuuji@0 2206 if (f.protect == (j + 1)) f.curpos = f.filepos = f.protect;
yuuji@0 2207 else { /* trailing newline missing, write it */
yuuji@0 2208 f.curpos = f.filepos = j;
yuuji@0 2209 unix_write (&f,"\n",1);
yuuji@0 2210 }
yuuji@0 2211 }
yuuji@0 2212 /* new internal header offset */
yuuji@0 2213 elt->private.special.offset = newoffset;
yuuji@0 2214 elt->private.dirty =NIL;/* message is now clean */
yuuji@0 2215 }
yuuji@0 2216 else { /* no need to rewrite this message */
yuuji@0 2217 /* tie off previous message if needed */
yuuji@0 2218 unix_write (&f,NIL,NIL);
yuuji@0 2219 /* protection pointer moves to next message */
yuuji@0 2220 f.protect = (i <= stream->nmsgs) ?
yuuji@0 2221 mail_elt (stream,i)->private.special.offset : size;
yuuji@0 2222 /* locate end of message text */
yuuji@0 2223 j = f.filepos + elt->private.special.text.size +
yuuji@0 2224 elt->private.msg.header.text.size +
yuuji@0 2225 elt->private.msg.text.text.size;
yuuji@0 2226 /* trailing newline already there? */
yuuji@0 2227 if (f.protect == (j + 1)) f.curpos = f.filepos = f.protect;
yuuji@0 2228 else { /* trailing newline missing, write it */
yuuji@0 2229 f.curpos = f.filepos = j;
yuuji@0 2230 unix_write (&f,"\n",1);
yuuji@0 2231 }
yuuji@0 2232 }
yuuji@0 2233 }
yuuji@0 2234 }
yuuji@0 2235
yuuji@0 2236 unix_write (&f,NIL,NIL); /* tie off final message */
yuuji@0 2237 if (size != f.filepos) fatal ("file size inconsistent");
yuuji@0 2238 fs_give ((void **) &f.buf); /* free buffer */
yuuji@0 2239 /* make sure tied off */
yuuji@0 2240 ftruncate (LOCAL->fd,LOCAL->filesize = size);
yuuji@0 2241 fsync (LOCAL->fd); /* make sure the updates take */
yuuji@0 2242 if (size && (flag < 0)) fatal ("lost UID base information");
yuuji@0 2243 /* no longer dirty */
yuuji@0 2244 LOCAL->ddirty = LOCAL->dirty = NIL;
yuuji@0 2245 /* notify upper level of new mailbox sizes */
yuuji@0 2246 mail_exists (stream,stream->nmsgs);
yuuji@0 2247 mail_recent (stream,recent);
yuuji@0 2248 /* set atime to now, mtime a second earlier */
yuuji@0 2249 tp[1] = (tp[0] = time (0)) - 1;
yuuji@0 2250 /* set the times, note change */
yuuji@0 2251 if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
yuuji@0 2252 close (LOCAL->fd); /* close and reopen file */
yuuji@0 2253 if ((LOCAL->fd = open (stream->mailbox,O_RDWR,
yuuji@0 2254 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
yuuji@0 2255 < 0) {
yuuji@0 2256 sprintf (LOCAL->buf,"Mailbox open failed, aborted: %s",strerror (errno));
yuuji@0 2257 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 2258 unix_abort (stream);
yuuji@0 2259 }
yuuji@0 2260 dotlock_unlock (lock); /* flush the lock file */
yuuji@0 2261 }
yuuji@0 2262 return ret; /* return state from algorithm */
yuuji@0 2263 }
yuuji@0 2264
yuuji@0 2265 /* Extend UNIX mailbox file
yuuji@0 2266 * Accepts: MAIL stream
yuuji@0 2267 * new desired size
yuuji@0 2268 * Return: T if success, else NIL
yuuji@0 2269 */
yuuji@0 2270
yuuji@0 2271 long unix_extend (MAILSTREAM *stream,unsigned long size)
yuuji@0 2272 {
yuuji@0 2273 unsigned long i = (size > LOCAL->filesize) ? size - LOCAL->filesize : 0;
yuuji@0 2274 if (i) { /* does the mailbox need to grow? */
yuuji@0 2275 if (i > LOCAL->buflen) { /* make sure have enough space */
yuuji@0 2276 /* this user won the lottery all right */
yuuji@0 2277 fs_give ((void **) &LOCAL->buf);
yuuji@0 2278 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = i) + 1);
yuuji@0 2279 }
yuuji@0 2280 memset (LOCAL->buf,'\0',i); /* get a block of nulls */
yuuji@0 2281 while (T) { /* until write successful or punt */
yuuji@0 2282 lseek (LOCAL->fd,LOCAL->filesize,L_SET);
yuuji@0 2283 if ((write (LOCAL->fd,LOCAL->buf,i) >= 0) && !fsync (LOCAL->fd)) break;
yuuji@0 2284 else {
yuuji@0 2285 long e = errno; /* note error before doing ftruncate */
yuuji@0 2286 ftruncate (LOCAL->fd,LOCAL->filesize);
yuuji@0 2287 if (MM_DISKERROR (stream,e,NIL)) {
yuuji@0 2288 fsync (LOCAL->fd); /* user chose to punt */
yuuji@0 2289 sprintf (LOCAL->buf,"Unable to extend mailbox: %s",strerror (e));
yuuji@0 2290 if (!stream->silent) MM_LOG (LOCAL->buf,ERROR);
yuuji@0 2291 return NIL;
yuuji@0 2292 }
yuuji@0 2293 }
yuuji@0 2294 }
yuuji@0 2295 }
yuuji@0 2296 return LONGT;
yuuji@0 2297 }
yuuji@0 2298
yuuji@0 2299 /* Write data to buffered file
yuuji@0 2300 * Accepts: buffered file pointer
yuuji@0 2301 * file data or NIL to indicate "flush buffer"
yuuji@0 2302 * date size (ignored for "flush buffer")
yuuji@0 2303 * Does not return until success
yuuji@0 2304 */
yuuji@0 2305
yuuji@0 2306 void unix_write (UNIXFILE *f,char *buf,unsigned long size)
yuuji@0 2307 {
yuuji@0 2308 unsigned long i,j,k;
yuuji@0 2309 if (buf) { /* doing buffered write? */
yuuji@0 2310 i = f->bufpos - f->buf; /* yes, get size of current buffer data */
yuuji@0 2311 /* yes, have space in current buffer chunk? */
yuuji@0 2312 if (j = i ? ((f->buflen - i) % OVERFLOWBUFLEN) : f->buflen) {
yuuji@0 2313 /* yes, fill up buffer as much as we can */
yuuji@0 2314 memcpy (f->bufpos,buf,k = min (j,size));
yuuji@0 2315 f->bufpos += k; /* new buffer position */
yuuji@0 2316 f->curpos += k; /* new current position */
yuuji@0 2317 if (j -= k) return; /* all done if still have buffer free space */
yuuji@0 2318 buf += k; /* full, get new unwritten data pointer */
yuuji@0 2319 size -= k; /* new data size */
yuuji@0 2320 i += k; /* new buffer data size */
yuuji@0 2321 }
yuuji@0 2322 /* This chunk of the buffer is full. See if can make some space by
yuuji@0 2323 * writing to the disk, if there's enough unprotected space to do so.
yuuji@0 2324 * Try to fill out any unaligned chunk, along with any subsequent full
yuuji@0 2325 * chunks that will fit in unprotected space.
yuuji@0 2326 */
yuuji@0 2327 /* any unprotected space we can write to? */
yuuji@0 2328 if (j = min (i,f->protect - f->filepos)) {
yuuji@0 2329 /* yes, filepos not at chunk boundary? */
yuuji@0 2330 if ((k = f->filepos % OVERFLOWBUFLEN) && ((k = OVERFLOWBUFLEN - k) < j))
yuuji@0 2331 j -= k; /* yes, and can write out partial chunk */
yuuji@0 2332 else k = 0; /* no partial chunk to write */
yuuji@0 2333 /* if at least a chunk free, write that too */
yuuji@0 2334 if (j > OVERFLOWBUFLEN) k += j - (j % OVERFLOWBUFLEN);
yuuji@0 2335 if (k) { /* write data if there is anything we can */
yuuji@0 2336 unix_phys_write (f,f->buf,k);
yuuji@0 2337 /* slide buffer */
yuuji@0 2338 if (i -= k) memmove (f->buf,f->buf + k,i);
yuuji@0 2339 f->bufpos = f->buf + i; /* new end of buffer */
yuuji@0 2340 }
yuuji@0 2341 }
yuuji@0 2342
yuuji@0 2343 /* Have flushed the buffer as best as possible. All done if no more
yuuji@0 2344 * data to write. Otherwise, if the buffer is empty AND if the unwritten
yuuji@0 2345 * data is larger than a chunk AND the unprotected space is also larger
yuuji@0 2346 * than a chunk, then write as many chunks as we can directly from the
yuuji@0 2347 * data. Buffer the rest, expanding the buffer as needed.
yuuji@0 2348 */
yuuji@0 2349 if (size) { /* have more data that we need to buffer? */
yuuji@0 2350 /* can write any of it to disk instead? */
yuuji@0 2351 if ((f->bufpos == f->buf) &&
yuuji@0 2352 ((j = min (f->protect - f->filepos,size)) > OVERFLOWBUFLEN)) {
yuuji@0 2353 /* write as much as we can right now */
yuuji@0 2354 unix_phys_write (f,buf,j -= (j % OVERFLOWBUFLEN));
yuuji@0 2355 buf += j; /* new data pointer */
yuuji@0 2356 size -= j; /* new data size */
yuuji@0 2357 f->curpos += j; /* advance current pointer */
yuuji@0 2358 }
yuuji@0 2359 if (size) { /* still have data that we need to buffer? */
yuuji@0 2360 /* yes, need to expand the buffer? */
yuuji@0 2361 if ((i = ((f->bufpos + size) - f->buf)) > f->buflen) {
yuuji@0 2362 /* note current position in buffer */
yuuji@0 2363 j = f->bufpos - f->buf;
yuuji@0 2364 i += OVERFLOWBUFLEN; /* yes, grow another chunk */
yuuji@0 2365 fs_resize ((void **) &f->buf,f->buflen = i - (i % OVERFLOWBUFLEN));
yuuji@0 2366 /* in case buffer relocated */
yuuji@0 2367 f->bufpos = f->buf + j;
yuuji@0 2368 }
yuuji@0 2369 /* buffer remaining data */
yuuji@0 2370 memcpy (f->bufpos,buf,size);
yuuji@0 2371 f->bufpos += size; /* new end of buffer */
yuuji@0 2372 f->curpos += size; /* advance current pointer */
yuuji@0 2373 }
yuuji@0 2374 }
yuuji@0 2375 }
yuuji@0 2376 else { /* flush buffer to disk */
yuuji@0 2377 unix_phys_write (f,f->buf,i = f->bufpos - f->buf);
yuuji@0 2378 f->bufpos = f->buf; /* reset buffer */
yuuji@0 2379 /* update positions */
yuuji@0 2380 f->curpos = f->protect = f->filepos;
yuuji@0 2381 }
yuuji@0 2382 }
yuuji@0 2383
yuuji@0 2384 /* Physical disk write
yuuji@0 2385 * Accepts: buffered file pointer
yuuji@0 2386 * buffer address
yuuji@0 2387 * buffer size
yuuji@0 2388 * Does not return until success
yuuji@0 2389 */
yuuji@0 2390
yuuji@0 2391 void unix_phys_write (UNIXFILE *f,char *buf,size_t size)
yuuji@0 2392 {
yuuji@0 2393 MAILSTREAM *stream = f->stream;
yuuji@0 2394 /* write data at desired position */
yuuji@0 2395 while (size && ((lseek (LOCAL->fd,f->filepos,L_SET) < 0) ||
yuuji@0 2396 (write (LOCAL->fd,buf,size) < 0))) {
yuuji@0 2397 int e;
yuuji@0 2398 char tmp[MAILTMPLEN];
yuuji@0 2399 sprintf (tmp,"Unable to write to mailbox: %s",strerror (e = errno));
yuuji@0 2400 MM_LOG (tmp,ERROR);
yuuji@0 2401 MM_DISKERROR (NIL,e,T); /* serious problem, must retry */
yuuji@0 2402 }
yuuji@0 2403 f->filepos += size; /* update file position */
yuuji@0 2404 }
yuuji@4 2405
yuuji@4 2406 /* Return my mail suffix(of qmail or Postfix)
yuuji@4 2407 * Returns: my mail address extensional suffix
yuuji@4 2408 */
yuuji@4 2409
yuuji@4 2410 char *mymailsuffix ()
yuuji@4 2411 {
yuuji@4 2412 return myMailSuffix ? myMailSuffix : "";
yuuji@4 2413 }
yuuji@0 2414
yuuji@0 2415 /* MBOX mail routines */
yuuji@0 2416
yuuji@0 2417
yuuji@0 2418 /* Driver dispatch used by MAIL */
yuuji@0 2419
yuuji@0 2420 DRIVER mboxdriver = {
yuuji@0 2421 "mbox", /* driver name */
yuuji@0 2422 /* driver flags */
yuuji@0 2423 DR_LOCAL|DR_MAIL|DR_LOCKING|DR_NONEWMAILRONLY,
yuuji@0 2424 (DRIVER *) NIL, /* next driver */
yuuji@0 2425 mbox_valid, /* mailbox is valid for us */
yuuji@0 2426 unix_parameters, /* manipulate parameters */
yuuji@0 2427 unix_scan, /* scan mailboxes */
yuuji@0 2428 unix_list, /* find mailboxes */
yuuji@0 2429 unix_lsub, /* find subscribed mailboxes */
yuuji@0 2430 NIL, /* subscribe to mailbox */
yuuji@0 2431 NIL, /* unsubscribe from mailbox */
yuuji@0 2432 mbox_create, /* create mailbox */
yuuji@0 2433 mbox_delete, /* delete mailbox */
yuuji@0 2434 mbox_rename, /* rename mailbox */
yuuji@0 2435 mbox_status, /* status of mailbox */
yuuji@0 2436 mbox_open, /* open mailbox */
yuuji@0 2437 unix_close, /* close mailbox */
yuuji@0 2438 NIL, /* fetch message "fast" attributes */
yuuji@0 2439 NIL, /* fetch message flags */
yuuji@0 2440 NIL, /* fetch overview */
yuuji@0 2441 NIL, /* fetch message structure */
yuuji@0 2442 unix_header, /* fetch message header */
yuuji@0 2443 unix_text, /* fetch message body */
yuuji@0 2444 NIL, /* fetch partial message text */
yuuji@0 2445 NIL, /* unique identifier */
yuuji@0 2446 NIL, /* message number */
yuuji@0 2447 NIL, /* modify flags */
yuuji@0 2448 unix_flagmsg, /* per-message modify flags */
yuuji@0 2449 NIL, /* search for message based on criteria */
yuuji@0 2450 NIL, /* sort messages */
yuuji@0 2451 NIL, /* thread messages */
yuuji@0 2452 mbox_ping, /* ping mailbox to see if still alive */
yuuji@0 2453 mbox_check, /* check for new messages */
yuuji@0 2454 mbox_expunge, /* expunge deleted messages */
yuuji@0 2455 unix_copy, /* copy messages to another mailbox */
yuuji@0 2456 mbox_append, /* append string message to mailbox */
yuuji@0 2457 NIL /* garbage collect stream */
yuuji@0 2458 };
yuuji@0 2459
yuuji@0 2460 /* prototype stream */
yuuji@0 2461 MAILSTREAM mboxproto = {&mboxdriver};
yuuji@0 2462
yuuji@0 2463 /* MBOX mail validate mailbox
yuuji@0 2464 * Accepts: mailbox name
yuuji@0 2465 * Returns: our driver if name is valid, NIL otherwise
yuuji@0 2466 */
yuuji@0 2467
yuuji@0 2468 DRIVER *mbox_valid (char *name)
yuuji@0 2469 {
yuuji@0 2470 /* only INBOX, mbox must exist */
yuuji@0 2471 if (!compare_cstring (name,"INBOX") && (unix_valid ("mbox") || !errno) &&
yuuji@0 2472 (unix_valid (sysinbox()) || !errno || (errno == ENOENT)))
yuuji@0 2473 return &mboxdriver;
yuuji@0 2474 return NIL; /* can't win (yet, anyway) */
yuuji@0 2475 }
yuuji@0 2476
yuuji@0 2477 /* MBOX mail create mailbox
yuuji@0 2478 * Accepts: MAIL stream
yuuji@0 2479 * mailbox name to create
yuuji@0 2480 * Returns: T on success, NIL on failure
yuuji@0 2481 */
yuuji@0 2482
yuuji@0 2483 long mbox_create (MAILSTREAM *stream,char *mailbox)
yuuji@0 2484 {
yuuji@0 2485 char tmp[MAILTMPLEN];
yuuji@0 2486 if (!compare_cstring (mailbox,"INBOX")) return unix_create (NIL,"mbox");
yuuji@0 2487 sprintf (tmp,"Can't create non-INBOX name as mbox: %.80s",mailbox);
yuuji@0 2488 MM_LOG (tmp,ERROR);
yuuji@0 2489 return NIL;
yuuji@0 2490 }
yuuji@0 2491
yuuji@0 2492
yuuji@0 2493 /* MBOX mail delete mailbox
yuuji@0 2494 * Accepts: MAIL stream
yuuji@0 2495 * mailbox name to delete
yuuji@0 2496 * Returns: T on success, NIL on failure
yuuji@0 2497 */
yuuji@0 2498
yuuji@0 2499 long mbox_delete (MAILSTREAM *stream,char *mailbox)
yuuji@0 2500 {
yuuji@0 2501 return mbox_rename (stream,mailbox,NIL);
yuuji@0 2502 }
yuuji@0 2503
yuuji@0 2504
yuuji@0 2505 /* MBOX mail rename mailbox
yuuji@0 2506 * Accepts: MAIL stream
yuuji@0 2507 * old mailbox name
yuuji@0 2508 * new mailbox name (or NIL for delete)
yuuji@0 2509 * Returns: T on success, NIL on failure
yuuji@0 2510 */
yuuji@0 2511
yuuji@0 2512 long mbox_rename (MAILSTREAM *stream,char *old,char *newname)
yuuji@0 2513 {
yuuji@0 2514 char tmp[MAILTMPLEN];
yuuji@0 2515 long ret = unix_rename (stream,"~/mbox",newname);
yuuji@0 2516 /* recreate file if renamed INBOX */
yuuji@0 2517 if (ret) unix_create (NIL,"mbox");
yuuji@0 2518 else MM_LOG (tmp,ERROR); /* log error */
yuuji@0 2519 return ret; /* return success */
yuuji@0 2520 }
yuuji@0 2521
yuuji@0 2522 /* MBOX Mail status
yuuji@0 2523 * Accepts: mail stream
yuuji@0 2524 * mailbox name
yuuji@0 2525 * status flags
yuuji@0 2526 * Returns: T on success, NIL on failure
yuuji@0 2527 */
yuuji@0 2528
yuuji@0 2529 long mbox_status (MAILSTREAM *stream,char *mbx,long flags)
yuuji@0 2530 {
yuuji@0 2531 MAILSTATUS status;
yuuji@0 2532 unsigned long i;
yuuji@0 2533 MAILSTREAM *tstream = NIL;
yuuji@0 2534 MAILSTREAM *systream = NIL;
yuuji@0 2535 /* make temporary stream (unless this mbx) */
yuuji@0 2536 if (!stream && !(stream = tstream =
yuuji@0 2537 mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) return NIL;
yuuji@0 2538 status.flags = flags; /* return status values */
yuuji@0 2539 status.messages = stream->nmsgs;
yuuji@0 2540 status.recent = stream->recent;
yuuji@0 2541 if (flags & SA_UNSEEN) /* must search to get unseen messages */
yuuji@0 2542 for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
yuuji@0 2543 if (!mail_elt (stream,i)->seen) status.unseen++;
yuuji@0 2544 status.uidnext = stream->uid_last + 1;
yuuji@0 2545 status.uidvalidity = stream->uid_validity;
yuuji@0 2546 if (!status.recent && /* calculate post-snarf results */
yuuji@0 2547 (systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) {
yuuji@0 2548 status.messages += systream->nmsgs;
yuuji@0 2549 status.recent += systream->recent;
yuuji@0 2550 if (flags & SA_UNSEEN) /* must search to get unseen messages */
yuuji@0 2551 for (i = 1; i <= systream->nmsgs; i++)
yuuji@0 2552 if (!mail_elt (systream,i)->seen) status.unseen++;
yuuji@0 2553 /* kludge but probably good enough */
yuuji@0 2554 status.uidnext += systream->nmsgs;
yuuji@0 2555 }
yuuji@0 2556 MM_STATUS(stream,mbx,&status);/* pass status to main program */
yuuji@0 2557 if (tstream) mail_close (tstream);
yuuji@0 2558 if (systream) mail_close (systream);
yuuji@0 2559 return T; /* success */
yuuji@0 2560 }
yuuji@0 2561
yuuji@0 2562 /* MBOX mail open
yuuji@0 2563 * Accepts: stream to open
yuuji@0 2564 * Returns: stream on success, NIL on failure
yuuji@0 2565 */
yuuji@0 2566
yuuji@0 2567 MAILSTREAM *mbox_open (MAILSTREAM *stream)
yuuji@0 2568 {
yuuji@0 2569 unsigned long i = 1;
yuuji@0 2570 unsigned long recent = 0;
yuuji@0 2571 /* return prototype for OP_PROTOTYPE call */
yuuji@0 2572 if (!stream) return &mboxproto;
yuuji@0 2573 /* change mailbox file name */
yuuji@0 2574 fs_give ((void **) &stream->mailbox);
yuuji@0 2575 stream->mailbox = cpystr ("mbox");
yuuji@0 2576 /* open mailbox, snarf new mail */
yuuji@0 2577 if (!(unix_open (stream) && mbox_ping (stream))) return NIL;
yuuji@0 2578 stream->inbox = T; /* mark that this is an INBOX */
yuuji@0 2579 /* notify upper level of mailbox sizes */
yuuji@0 2580 mail_exists (stream,stream->nmsgs);
yuuji@0 2581 while (i <= stream->nmsgs) if (mail_elt (stream,i++)->recent) ++recent;
yuuji@0 2582 mail_recent (stream,recent); /* including recent messages */
yuuji@0 2583 return stream;
yuuji@0 2584 }
yuuji@0 2585
yuuji@0 2586 /* MBOX mail ping mailbox
yuuji@0 2587 * Accepts: MAIL stream
yuuji@0 2588 * Returns: T if stream alive, else NIL
yuuji@0 2589 * No-op for readonly files, since read/writer can expunge it from under us!
yuuji@0 2590 */
yuuji@0 2591
yuuji@0 2592 static int snarfed = 0; /* number of snarfs */
yuuji@0 2593
yuuji@0 2594 long mbox_ping (MAILSTREAM *stream)
yuuji@0 2595 {
yuuji@0 2596 int sfd;
yuuji@0 2597 unsigned long size;
yuuji@0 2598 struct stat sbuf;
yuuji@0 2599 char *s;
yuuji@0 2600 DOTLOCK lock,lockx;
yuuji@0 2601 /* time to try snarf and sysinbox non-empty? */
yuuji@0 2602 if (LOCAL && !stream->rdonly && !stream->lock &&
yuuji@0 2603 (time (0) >= (LOCAL->lastsnarf +
yuuji@0 2604 (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) &&
yuuji@0 2605 !stat (sysinbox (),&sbuf) && sbuf.st_size) {
yuuji@0 2606 MM_CRITICAL (stream); /* yes, go critical */
yuuji@0 2607 /* open and lock sysinbox */
yuuji@0 2608 if ((sfd = unix_lock (sysinbox (),O_RDWR,
yuuji@0 2609 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
yuuji@0 2610 &lockx,LOCK_EX)) >= 0) {
yuuji@0 2611 /* locked sysinbox in good format? */
yuuji@0 2612 if (fstat (sfd,&sbuf) || !(size = sbuf.st_size) ||
yuuji@0 2613 !unix_isvalid_fd (sfd)) {
yuuji@0 2614 sprintf (LOCAL->buf,"Mail drop %s is not in standard Unix format",
yuuji@0 2615 sysinbox ());
yuuji@0 2616 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 2617 }
yuuji@0 2618 /* sysinbox good, parse and excl-lock mbox */
yuuji@0 2619 else if (unix_parse (stream,&lock,LOCK_EX)) {
yuuji@0 2620 lseek (sfd,0,L_SET); /* read entire sysinbox into memory */
yuuji@0 2621 read (sfd,s = (char *) fs_get (size + 1),size);
yuuji@0 2622 s[size] = '\0'; /* tie it off */
yuuji@0 2623 /* append to end of mbox */
yuuji@0 2624 lseek (LOCAL->fd,LOCAL->filesize,L_SET);
yuuji@0 2625
yuuji@0 2626 /* copy to mbox */
yuuji@0 2627 if ((write (LOCAL->fd,s,size) < 0) || fsync (LOCAL->fd)) {
yuuji@0 2628 sprintf (LOCAL->buf,"New mail move failed: %s",strerror (errno));
yuuji@0 2629 MM_LOG (LOCAL->buf,WARN);
yuuji@0 2630 /* revert mbox to previous size */
yuuji@0 2631 ftruncate (LOCAL->fd,LOCAL->filesize);
yuuji@0 2632 }
yuuji@0 2633 /* sysinbox better not have changed */
yuuji@0 2634 else if (fstat (sfd,&sbuf) || (size != sbuf.st_size)) {
yuuji@0 2635 sprintf (LOCAL->buf,"Mail drop %s lock failure, old=%lu now=%lu",
yuuji@0 2636 sysinbox (),size,(unsigned long) sbuf.st_size);
yuuji@0 2637 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 2638 /* revert mbox to previous size */
yuuji@0 2639 ftruncate (LOCAL->fd,LOCAL->filesize);
yuuji@0 2640 /* Believe it or not, a Singaporean government system actually had
yuuji@0 2641 * symlinks from /var/mail/user to ~user/mbox. To compound this
yuuji@0 2642 * error, they used an SVR4 system; BSD and OSF locks would have
yuuji@0 2643 * prevented it but not SVR4 locks.
yuuji@0 2644 */
yuuji@0 2645 if (!fstat (sfd,&sbuf) && (size == sbuf.st_size))
yuuji@0 2646 syslog (LOG_ALERT,"File %s and %s are the same file!",
yuuji@0 2647 sysinbox (),stream->mailbox);
yuuji@0 2648 }
yuuji@0 2649 else { /* data copied OK */
yuuji@0 2650 ftruncate (sfd,0); /* truncate sysinbox to zero bytes */
yuuji@0 2651 if (!snarfed++) { /* have we snarfed before? */
yuuji@0 2652 /* syslog if server, else user log */
yuuji@0 2653 sprintf (LOCAL->buf,"Moved %lu bytes of new mail to %s from %s",
yuuji@0 2654 size,stream->mailbox,sysinbox ());
yuuji@0 2655 if (strcmp ((char *) mail_parameters (NIL,GET_SERVICENAME,NIL),
yuuji@0 2656 "unknown"))
yuuji@0 2657 syslog (LOG_INFO,"%s host= %s",LOCAL->buf,tcp_clienthost ());
yuuji@0 2658 else MM_LOG (LOCAL->buf,WARN);
yuuji@0 2659 }
yuuji@0 2660 }
yuuji@0 2661 /* done with sysinbox text */
yuuji@0 2662 fs_give ((void **) &s);
yuuji@0 2663 /* all done with mbox */
yuuji@0 2664 unix_unlock (LOCAL->fd,stream,&lock);
yuuji@0 2665 mail_unlock (stream); /* unlock the stream */
yuuji@0 2666 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 2667 }
yuuji@0 2668 /* all done with sysinbox */
yuuji@0 2669 unix_unlock (sfd,NIL,&lockx);
yuuji@0 2670 }
yuuji@0 2671 MM_NOCRITICAL (stream); /* done with critical */
yuuji@0 2672 LOCAL->lastsnarf = time (0);/* note time of last snarf */
yuuji@0 2673 }
yuuji@0 2674 return unix_ping (stream); /* do the unix routine now */
yuuji@0 2675 }
yuuji@0 2676
yuuji@0 2677 /* MBOX mail check mailbox
yuuji@0 2678 * Accepts: MAIL stream
yuuji@0 2679 */
yuuji@0 2680
yuuji@0 2681 void mbox_check (MAILSTREAM *stream)
yuuji@0 2682 {
yuuji@0 2683 /* do local ping, then do unix routine */
yuuji@0 2684 if (mbox_ping (stream)) unix_check (stream);
yuuji@0 2685 }
yuuji@0 2686
yuuji@0 2687
yuuji@0 2688 /* MBOX mail expunge mailbox
yuuji@0 2689 * Accepts: MAIL stream
yuuji@0 2690 * sequence to expunge if non-NIL
yuuji@0 2691 * expunge options
yuuji@0 2692 * Returns: T, always
yuuji@0 2693 */
yuuji@0 2694
yuuji@0 2695 long mbox_expunge (MAILSTREAM *stream,char *sequence,long options)
yuuji@0 2696 {
yuuji@0 2697 long ret = unix_expunge (stream,sequence,options);
yuuji@0 2698 mbox_ping (stream); /* do local ping */
yuuji@0 2699 return ret;
yuuji@0 2700 }
yuuji@0 2701
yuuji@0 2702
yuuji@0 2703 /* MBOX mail append message from stringstruct
yuuji@0 2704 * Accepts: MAIL stream
yuuji@0 2705 * destination mailbox
yuuji@0 2706 * append callback
yuuji@0 2707 * data for callback
yuuji@0 2708 * Returns: T if append successful, else NIL
yuuji@0 2709 */
yuuji@0 2710
yuuji@0 2711 long mbox_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
yuuji@0 2712 {
yuuji@0 2713 char tmp[MAILTMPLEN];
yuuji@0 2714 if (mbox_valid (mailbox)) return unix_append (stream,"mbox",af,data);
yuuji@0 2715 sprintf (tmp,"Can't append to that name: %.80s",mailbox);
yuuji@0 2716 MM_LOG (tmp,ERROR);
yuuji@0 2717 return NIL;
yuuji@0 2718 }

UW-IMAP'd extensions by yuuji