imapext-2007

annotate src/osdep/amiga/unix.c @ 0:ada5e610ab86

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

UW-IMAP'd extensions by yuuji