imapext-2007
diff src/osdep/dos/mtxdos.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/dos/mtxdos.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,875 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2006 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: MTX mail routines 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 24 June 1992 1.29 + * Last Edited: 30 August 2006 1.30 + */ 1.31 + 1.32 + 1.33 +#include <ctype.h> 1.34 +#include <errno.h> 1.35 +#include <fcntl.h> 1.36 +#include "mail.h" 1.37 +#include "osdep.h" 1.38 +#include <time.h> 1.39 +#include <sys\stat.h> 1.40 +#include <dos.h> 1.41 +#include "rfc822.h" 1.42 +#include "dummy.h" 1.43 +#include "misc.h" 1.44 +#include "fdstring.h" 1.45 + 1.46 +/* MTX I/O stream local data */ 1.47 + 1.48 +typedef struct mtx_local { 1.49 + int fd; /* file descriptor for I/O */ 1.50 + off_t filesize; /* file size parsed */ 1.51 + unsigned char *buf; /* temporary buffer */ 1.52 +} MTXLOCAL; 1.53 + 1.54 + 1.55 +/* Drive-dependent data passed to init method */ 1.56 + 1.57 +typedef struct mtx_data { 1.58 + int fd; /* file data */ 1.59 + unsigned long pos; /* initial position */ 1.60 +} MTXDATA; 1.61 + 1.62 + 1.63 +/* Convenient access to local data */ 1.64 + 1.65 +#define LOCAL ((MTXLOCAL *) stream->local) 1.66 + 1.67 + 1.68 +/* Function prototypes */ 1.69 + 1.70 +DRIVER *mtx_valid (char *name); 1.71 +long mtx_isvalid (char *name,char *tmp); 1.72 +void *mtx_parameters (long function,void *value); 1.73 +void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents); 1.74 +void mtx_list (MAILSTREAM *stream,char *ref,char *pat); 1.75 +void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat); 1.76 +long mtx_create (MAILSTREAM *stream,char *mailbox); 1.77 +long mtx_delete (MAILSTREAM *stream,char *mailbox); 1.78 +long mtx_rename (MAILSTREAM *stream,char *old,char *newname); 1.79 +MAILSTREAM *mtx_open (MAILSTREAM *stream); 1.80 +void mtx_close (MAILSTREAM *stream,long options); 1.81 +char *mtx_header (MAILSTREAM *stream,unsigned long msgno, 1.82 + unsigned long *length,long flags); 1.83 +long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags); 1.84 +void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt); 1.85 +long mtx_ping (MAILSTREAM *stream); 1.86 +void mtx_check (MAILSTREAM *stream); 1.87 +long mtx_expunge (MAILSTREAM *stream,char *sequence,long options); 1.88 +long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options); 1.89 +long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data); 1.90 + 1.91 +char *mtx_file (char *dst,char *name); 1.92 +long mtx_badname (char *tmp,char *s); 1.93 +long mtx_parse (MAILSTREAM *stream); 1.94 +void mtx_update_status (MAILSTREAM *stream,unsigned long msgno); 1.95 +unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno, 1.96 + unsigned long *size); 1.97 + 1.98 +/* MTX mail routines */ 1.99 + 1.100 + 1.101 +/* Driver dispatch used by MAIL */ 1.102 + 1.103 +DRIVER mtxdriver = { 1.104 + "mtx", /* driver name */ 1.105 + /* driver flags */ 1.106 + DR_LOCAL|DR_MAIL|DR_LOWMEM|DR_CRLF|DR_NOSTICKY, 1.107 + (DRIVER *) NIL, /* next driver */ 1.108 + mtx_valid, /* mailbox is valid for us */ 1.109 + mtx_parameters, /* manipulate parameters */ 1.110 + mtx_scan, /* scan mailboxes */ 1.111 + mtx_list, /* list mailboxes */ 1.112 + mtx_lsub, /* list subscribed mailboxes */ 1.113 + NIL, /* subscribe to mailbox */ 1.114 + NIL, /* unsubscribe from mailbox */ 1.115 + mtx_create, /* create mailbox */ 1.116 + mtx_delete, /* delete mailbox */ 1.117 + mtx_rename, /* rename mailbox */ 1.118 + mail_status_default, /* status of mailbox */ 1.119 + mtx_open, /* open mailbox */ 1.120 + mtx_close, /* close mailbox */ 1.121 + NIL, /* fetch message "fast" attributes */ 1.122 + NIL, /* fetch message flags */ 1.123 + NIL, /* fetch overview */ 1.124 + NIL, /* fetch message envelopes */ 1.125 + mtx_header, /* fetch message header */ 1.126 + mtx_text, /* fetch message body */ 1.127 + NIL, /* fetch partial message text */ 1.128 + NIL, /* unique identifier */ 1.129 + NIL, /* message number */ 1.130 + NIL, /* modify flags */ 1.131 + mtx_flagmsg, /* per-message modify flags */ 1.132 + NIL, /* search for message based on criteria */ 1.133 + NIL, /* sort messages */ 1.134 + NIL, /* thread messages */ 1.135 + mtx_ping, /* ping mailbox to see if still alive */ 1.136 + mtx_check, /* check for new messages */ 1.137 + mtx_expunge, /* expunge deleted messages */ 1.138 + mtx_copy, /* copy messages to another mailbox */ 1.139 + mtx_append, /* append string message to mailbox */ 1.140 + NIL /* garbage collect stream */ 1.141 +}; 1.142 + 1.143 + /* prototype stream */ 1.144 +MAILSTREAM mtxproto = {&mtxdriver}; 1.145 + 1.146 +/* MTX mail validate mailbox 1.147 + * Accepts: mailbox name 1.148 + * Returns: our driver if name is valid, NIL otherwise 1.149 + */ 1.150 + 1.151 +DRIVER *mtx_valid (char *name) 1.152 +{ 1.153 + char tmp[MAILTMPLEN]; 1.154 + return mtx_isvalid (name,tmp) ? &mtxdriver : (DRIVER *) NIL; 1.155 +} 1.156 + 1.157 + 1.158 +/* MTX mail test for valid mailbox 1.159 + * Accepts: mailbox name 1.160 + * Returns: T if valid, NIL otherwise 1.161 + */ 1.162 + 1.163 +long mtx_isvalid (char *name,char *tmp) 1.164 +{ 1.165 + int fd; 1.166 + long ret = NIL; 1.167 + char *s; 1.168 + struct stat sbuf; 1.169 + errno = EINVAL; /* assume invalid argument */ 1.170 + /* if file, get its status */ 1.171 + if ((*name != '{') && mailboxfile (tmp,name) && !stat (tmp,&sbuf)) { 1.172 + if (!sbuf.st_size)errno = 0;/* empty file */ 1.173 + else if ((fd = open (tmp,O_BINARY|O_RDONLY,NIL)) >= 0) { 1.174 + memset (tmp,'\0',MAILTMPLEN); 1.175 + if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\015')) && 1.176 + (s[1] == '\012')) { /* valid format? */ 1.177 + *s = '\0'; /* tie off header */ 1.178 + /* must begin with dd-mmm-yy" */ 1.179 + ret = (((tmp[2] == '-' && tmp[6] == '-') || 1.180 + (tmp[1] == '-' && tmp[5] == '-')) && 1.181 + (s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL; 1.182 + } 1.183 + else errno = -1; /* bogus format */ 1.184 + close (fd); /* close the file */ 1.185 + } 1.186 + } 1.187 + /* in case INBOX but not mtx format */ 1.188 + else if ((errno == ENOENT) && ((name[0] == 'I') || (name[0] == 'i')) && 1.189 + ((name[1] == 'N') || (name[1] == 'n')) && 1.190 + ((name[2] == 'B') || (name[2] == 'b')) && 1.191 + ((name[3] == 'O') || (name[3] == 'o')) && 1.192 + ((name[4] == 'X') || (name[4] == 'x')) && !name[5]) errno = -1; 1.193 + return ret; /* return what we should */ 1.194 +} 1.195 + 1.196 + 1.197 +/* MTX manipulate driver parameters 1.198 + * Accepts: function code 1.199 + * function-dependent value 1.200 + * Returns: function-dependent return value 1.201 + */ 1.202 + 1.203 +void *mtx_parameters (long function,void *value) 1.204 +{ 1.205 + return NIL; 1.206 +} 1.207 + 1.208 +/* MTX mail scan mailboxes 1.209 + * Accepts: mail stream 1.210 + * reference 1.211 + * pattern to search 1.212 + * string to scan 1.213 + */ 1.214 + 1.215 +void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents) 1.216 +{ 1.217 + if (stream) dummy_scan (NIL,ref,pat,contents); 1.218 +} 1.219 + 1.220 + 1.221 +/* MTX mail list mailboxes 1.222 + * Accepts: mail stream 1.223 + * reference 1.224 + * pattern to search 1.225 + */ 1.226 + 1.227 +void mtx_list (MAILSTREAM *stream,char *ref,char *pat) 1.228 +{ 1.229 + if (stream) dummy_list (stream,ref,pat); 1.230 +} 1.231 + 1.232 + 1.233 +/* MTX mail list subscribed mailboxes 1.234 + * Accepts: mail stream 1.235 + * reference 1.236 + * pattern to search 1.237 + */ 1.238 + 1.239 +void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat) 1.240 +{ 1.241 + if (stream) dummy_lsub (stream,ref,pat); 1.242 +} 1.243 + 1.244 +/* MTX mail create mailbox 1.245 + * Accepts: MAIL stream 1.246 + * mailbox name to create 1.247 + * Returns: T on success, NIL on failure 1.248 + */ 1.249 + 1.250 +long mtx_create (MAILSTREAM *stream,char *mailbox) 1.251 +{ 1.252 + return dummy_create (stream,mailbox); 1.253 +} 1.254 + 1.255 + 1.256 +/* MTX mail delete mailbox 1.257 + * Accepts: MAIL stream 1.258 + * mailbox name to delete 1.259 + * Returns: T on success, NIL on failure 1.260 + */ 1.261 + 1.262 +long mtx_delete (MAILSTREAM *stream,char *mailbox) 1.263 +{ 1.264 + return dummy_delete (stream,mailbox); 1.265 +} 1.266 + 1.267 + 1.268 +/* MTX mail rename mailbox 1.269 + * Accepts: MAIL stream 1.270 + * old mailbox name 1.271 + * new mailbox name (or NIL for delete) 1.272 + * Returns: T on success, NIL on failure 1.273 + */ 1.274 + 1.275 +long mtx_rename (MAILSTREAM *stream,char *old,char *newname) 1.276 +{ 1.277 + return dummy_rename (stream,old,newname); 1.278 +} 1.279 + 1.280 +/* MTX mail open 1.281 + * Accepts: stream to open 1.282 + * Returns: stream on success, NIL on failure 1.283 + */ 1.284 + 1.285 +MAILSTREAM *mtx_open (MAILSTREAM *stream) 1.286 +{ 1.287 + long i; 1.288 + int fd; 1.289 + char *s; 1.290 + char tmp[MAILTMPLEN]; 1.291 + /* return prototype for OP_PROTOTYPE call */ 1.292 + if (!stream) return &mtxproto; 1.293 + if (stream->local) fatal ("mtx recycle stream"); 1.294 + if (!mailboxfile (tmp,stream->mailbox)) 1.295 + return (MAILSTREAM *) mtx_badname (tmp,stream->mailbox); 1.296 + /* open, possibly creating INBOX */ 1.297 + if (((fd = open (tmp,O_BINARY|(stream->rdonly ? O_RDONLY:O_RDWR),NIL)) < 0)&& 1.298 + (compare_cstring (stream->mailbox,"INBOX") || 1.299 + ((fd = open (tmp,O_BINARY|O_RDWR|O_CREAT|O_EXCL,S_IREAD|S_IWRITE))<0))){ 1.300 + sprintf (tmp,"Can't open mailbox: %s",strerror (errno)); 1.301 + mm_log (tmp,ERROR); 1.302 + return NIL; 1.303 + } 1.304 + stream->local = fs_get (sizeof (MTXLOCAL)); 1.305 + /* canonicalize the stream mailbox name */ 1.306 + fs_give ((void **) &stream->mailbox); 1.307 + if (s = strchr ((s = strrchr (tmp,'\\')) ? s : tmp,'.')) *s = '\0'; 1.308 + stream->mailbox = cpystr (tmp); 1.309 + LOCAL->fd = fd; /* note the file */ 1.310 + LOCAL->filesize = 0; /* initialize parsed file size */ 1.311 + LOCAL->buf = NIL; /* initially no local buffer */ 1.312 + stream->sequence++; /* bump sequence number */ 1.313 + stream->uid_validity = time (0); 1.314 + /* parse mailbox */ 1.315 + stream->nmsgs = stream->recent = 0; 1.316 + if (!mtx_ping (stream)) return NIL; 1.317 + if (!stream->nmsgs) mm_log ("Mailbox is empty",(long) NIL); 1.318 + stream->perm_seen = stream->perm_deleted = 1.319 + stream->perm_flagged = stream->perm_answered = stream->perm_draft = 1.320 + stream->rdonly ? NIL : T; 1.321 + stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff; 1.322 + return stream; /* return stream to caller */ 1.323 +} 1.324 + 1.325 +/* MTX mail close 1.326 + * Accepts: MAIL stream 1.327 + * close options 1.328 + */ 1.329 + 1.330 +void mtx_close (MAILSTREAM *stream,long options) 1.331 +{ 1.332 + if (stream && LOCAL) { /* only if a file is open */ 1.333 + int silent = stream->silent; 1.334 + stream->silent = T; 1.335 + if (options & CL_EXPUNGE) mtx_expunge (stream,NIL,NIL); 1.336 + close (LOCAL->fd); /* close the local file */ 1.337 + if (LOCAL->buf) fs_give ((void **) &LOCAL->buf); 1.338 + /* nuke the local data */ 1.339 + fs_give ((void **) &stream->local); 1.340 + stream->dtb = NIL; /* log out the DTB */ 1.341 + } 1.342 +} 1.343 + 1.344 +/* MTX mail fetch message header 1.345 + * Accepts: MAIL stream 1.346 + * message # to fetch 1.347 + * pointer to returned header text length 1.348 + * option flags 1.349 + * Returns: message header in RFC822 format 1.350 + */ 1.351 + 1.352 +char *mtx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length, 1.353 + long flags) 1.354 +{ 1.355 + *length = 0; /* default to empty */ 1.356 + if (flags & FT_UID) return "";/* UID call "impossible" */ 1.357 + /* get to header position */ 1.358 + lseek (LOCAL->fd,mtx_hdrpos (stream,msgno,length),L_SET); 1.359 + /* is buffer big enough? */ 1.360 + if (LOCAL->buf) fs_give ((void **) &LOCAL->buf); 1.361 + LOCAL->buf = (char *) fs_get ((size_t) *length + 1); 1.362 + LOCAL->buf[*length] = '\0'; /* tie off string */ 1.363 + /* slurp the data */ 1.364 + read (LOCAL->fd,LOCAL->buf,(size_t) *length); 1.365 + return LOCAL->buf; 1.366 +} 1.367 + 1.368 + 1.369 +/* MTX mail fetch message text (body only) 1.370 + * Accepts: MAIL stream 1.371 + * message # to fetch 1.372 + * pointer to returned header text length 1.373 + * option flags 1.374 + * Returns: T, always 1.375 + */ 1.376 + 1.377 +long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags) 1.378 +{ 1.379 + MESSAGECACHE *elt; 1.380 + FDDATA d; 1.381 + unsigned long hdrsize,hdrpos; 1.382 + /* UID call "impossible" */ 1.383 + if (flags & FT_UID) return NIL; 1.384 + elt = mail_elt (stream,msgno);/* if message not seen */ 1.385 + if (elt->seen && !(flags & FT_PEEK)) { 1.386 + elt->seen = T; /* mark message as seen */ 1.387 + /* recalculate status */ 1.388 + mtx_update_status (stream,msgno); 1.389 + mm_flags (stream,msgno); 1.390 + } 1.391 + /* get location of text data */ 1.392 + hdrpos = mtx_hdrpos (stream,msgno,&hdrsize); 1.393 + d.fd = LOCAL->fd; /* set initial stringstruct */ 1.394 + d.pos = hdrpos + hdrsize; 1.395 + /* flush old buffer */ 1.396 + if (LOCAL->buf) fs_give ((void **) &LOCAL->buf); 1.397 + d.chunk = LOCAL->buf = (char *) fs_get ((size_t) d.chunksize = CHUNKSIZE); 1.398 + INIT (bs,fd_string,(void *) &d,elt->rfc822_size - hdrsize); 1.399 + return T; /* success */ 1.400 +} 1.401 + 1.402 +/* MTX mail per-message modify flags 1.403 + * Accepts: MAIL stream 1.404 + * message cache element 1.405 + */ 1.406 + 1.407 +void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt) 1.408 +{ 1.409 + /* recalculate status */ 1.410 + mtx_update_status (stream,elt->msgno); 1.411 +} 1.412 + 1.413 + 1.414 +/* MTX mail ping mailbox 1.415 + * Accepts: MAIL stream 1.416 + * Returns: T if stream still alive, NIL if not 1.417 + */ 1.418 + 1.419 +long mtx_ping (MAILSTREAM *stream) 1.420 +{ 1.421 + /* punt if stream no longer alive */ 1.422 + if (!(stream && LOCAL)) return NIL; 1.423 + /* parse mailbox, punt if parse dies */ 1.424 + return (mtx_parse (stream)) ? T : NIL; 1.425 +} 1.426 + 1.427 + 1.428 +/* MTX mail check mailbox (reparses status too) 1.429 + * Accepts: MAIL stream 1.430 + */ 1.431 + 1.432 +void mtx_check (MAILSTREAM *stream) 1.433 +{ 1.434 + unsigned long i = 1; 1.435 + if (mtx_ping (stream)) { /* ping mailbox */ 1.436 + /* get new message status */ 1.437 + while (i <= stream->nmsgs) mail_elt (stream,i++); 1.438 + mm_log ("Check completed",(long) NIL); 1.439 + } 1.440 +} 1.441 + 1.442 +/* MTX mail expunge mailbox 1.443 + * Accepts: MAIL stream 1.444 + * sequence to expunge if non-NIL 1.445 + * expunge options 1.446 + * Returns: T, always 1.447 + */ 1.448 + 1.449 +long mtx_expunge (MAILSTREAM *stream,char *sequence,long options) 1.450 +{ 1.451 + long ret; 1.452 + unsigned long i = 1; 1.453 + unsigned long j,k,m,recent; 1.454 + unsigned long n = 0; 1.455 + unsigned long delta = 0; 1.456 + MESSAGECACHE *elt; 1.457 + char tmp[MAILTMPLEN]; 1.458 + if (!(ret = (sequence ? ((options & EX_UID) ? 1.459 + mail_uid_sequence (stream,sequence) : 1.460 + mail_sequence (stream,sequence)) : LONGT) && 1.461 + mtx_ping (stream))); /* parse sequence if given, ping stream */ 1.462 + else if (stream->rdonly) mm_log ("Expunge ignored on readonly mailbox",WARN); 1.463 + else { 1.464 + mm_critical (stream); /* go critical */ 1.465 + recent = stream->recent; /* get recent now that pinged */ 1.466 + while (i <= stream->nmsgs) {/* for each message */ 1.467 + elt = mail_elt (stream,i);/* get cache element */ 1.468 + /* number of bytes to smash or preserve */ 1.469 + k = elt->private.special.text.size + elt->rfc822_size; 1.470 + /* if need to expunge this message */ 1.471 + if (elt->deleted && (sequence ? elt->sequence : T)) { 1.472 + /* if recent, note one less recent message */ 1.473 + if (elt->recent) --recent; 1.474 + delta += k; /* number of bytes to delete */ 1.475 + /* notify upper levels */ 1.476 + mail_expunged (stream,i); 1.477 + n++; /* count up one more deleted message */ 1.478 + } 1.479 + else if (i++ && delta) { /* preserved message */ 1.480 + /* first byte to preserve */ 1.481 + j = elt->private.special.offset; 1.482 + do { /* read from source position */ 1.483 + m = min (k,(unsigned long) MAILTMPLEN); 1.484 + lseek (LOCAL->fd,j,SEEK_SET); 1.485 + read (LOCAL->fd,tmp,(size_t) m); 1.486 + /* write to destination position */ 1.487 + lseek (LOCAL->fd,j - delta,SEEK_SET); 1.488 + write (LOCAL->fd,tmp,(size_t) m); 1.489 + j += m; /* next chunk, perhaps */ 1.490 + } while (k -= m); /* until done */ 1.491 + elt->private.special.offset -= delta; 1.492 + } 1.493 + } 1.494 + if (n) { /* truncate file after last message */ 1.495 + chsize (LOCAL->fd,LOCAL->filesize -= delta); 1.496 + sprintf (tmp,"Expunged %ld messages",n); 1.497 + mm_log (tmp,(long) NIL); /* output the news */ 1.498 + } 1.499 + else mm_log ("No messages deleted, so no update needed",(long) NIL); 1.500 + mm_nocritical (stream); /* release critical */ 1.501 + /* notify upper level of new mailbox size */ 1.502 + mail_exists (stream,stream->nmsgs); 1.503 + mail_recent (stream,recent); 1.504 + } 1.505 + return ret; 1.506 +} 1.507 + 1.508 +/* MTX mail copy message(s) 1.509 + * Accepts: MAIL stream 1.510 + * sequence 1.511 + * destination mailbox 1.512 + * copy options 1.513 + * Returns: T if success, NIL if failed 1.514 + */ 1.515 + 1.516 +long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options) 1.517 +{ 1.518 + char tmp[MAILTMPLEN]; 1.519 + struct stat sbuf; 1.520 + MESSAGECACHE *elt; 1.521 + unsigned long i,j,k; 1.522 + long ret = LONGT; 1.523 + int fd; 1.524 + mailproxycopy_t pc = 1.525 + (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL); 1.526 + if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) : 1.527 + mail_sequence (stream,sequence))) return NIL; 1.528 + if (!mtx_isvalid (mailbox,tmp)) switch (errno) { 1.529 + case ENOENT: /* no such file? */ 1.530 + mm_notify (stream,"[TRYCREATE] Must create mailbox before append", 1.531 + (long) NIL); 1.532 + return NIL; 1.533 + case 0: /* merely empty file? */ 1.534 + break; 1.535 + case EINVAL: /* name is bogus */ 1.536 + if (pc) return (*pc) (stream,sequence,mailbox,options); 1.537 + return mtx_badname (tmp,mailbox); 1.538 + default: /* file exists, but not valid format */ 1.539 + if (pc) return (*pc) (stream,sequence,mailbox,options); 1.540 + sprintf (tmp,"Not a MTX-format mailbox: %s",mailbox); 1.541 + mm_log (tmp,ERROR); 1.542 + return NIL; 1.543 + } 1.544 + /* open the destination */ 1.545 + if (!mailboxfile (tmp,mailbox) || 1.546 + (fd = open (tmp,O_BINARY|O_WRONLY|O_APPEND|O_CREAT, 1.547 + S_IREAD|S_IWRITE)) < 0) { 1.548 + sprintf (tmp,"Unable to open copy mailbox: %s",strerror (errno)); 1.549 + mm_log (tmp,ERROR); 1.550 + return NIL; 1.551 + } 1.552 + 1.553 + mm_critical (stream); /* go critical */ 1.554 + fstat (fd,&sbuf); /* get current file size */ 1.555 + /* for each requested message */ 1.556 + for (i = 1; ret && (i <= stream->nmsgs); i++) 1.557 + if ((elt = mail_elt (stream,i))->sequence) { 1.558 + lseek (LOCAL->fd,elt->private.special.offset,SEEK_SET); 1.559 + /* number of bytes to copy */ 1.560 + k = elt->private.special.text.size + elt->rfc822_size; 1.561 + do { /* read from source position */ 1.562 + j = min (k,(long) MAILTMPLEN); 1.563 + read (LOCAL->fd,tmp,(size_t) j); 1.564 + if (write (fd,tmp,(size_t) j) < 0) { 1.565 + sprintf (tmp,"Unable to write message: %s",strerror (errno)); 1.566 + mm_log (tmp,ERROR); 1.567 + chsize (fd,sbuf.st_size); 1.568 + j = k; 1.569 + ret = NIL; /* note error */ 1.570 + break; 1.571 + } 1.572 + } while (k -= j); /* until done */ 1.573 + } 1.574 + close (fd); /* close the file */ 1.575 + mm_nocritical (stream); /* release critical */ 1.576 + /* delete all requested messages */ 1.577 + if (ret && (options & CP_MOVE)) for (i = 1; i <= stream->nmsgs; i++) 1.578 + if ((elt = mail_elt (stream,i))->sequence) { 1.579 + elt->deleted = T; /* mark message deleted */ 1.580 + /* recalculate status */ 1.581 + mtx_update_status (stream,i); 1.582 + } 1.583 + if (ret && mail_parameters (NIL,GET_COPYUID,NIL)) 1.584 + mm_log ("Can not return meaningful COPYUID with this mailbox format",WARN); 1.585 + return ret; 1.586 +} 1.587 + 1.588 +/* MTX mail append message from stringstruct 1.589 + * Accepts: MAIL stream 1.590 + * destination mailbox 1.591 + * append callback 1.592 + * data for callback 1.593 + * Returns: T if append successful, else NIL 1.594 + */ 1.595 + 1.596 +long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data) 1.597 +{ 1.598 + struct stat sbuf; 1.599 + int fd,ld,c; 1.600 + char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN]; 1.601 + FILE *df; 1.602 + MESSAGECACHE elt; 1.603 + long f; 1.604 + unsigned long i,uf; 1.605 + STRING *message; 1.606 + long ret = LONGT; 1.607 + /* default stream to prototype */ 1.608 + if (!stream) stream = &mtxproto; 1.609 + /* make sure valid mailbox */ 1.610 + if (!mtx_isvalid (mailbox,tmp)) switch (errno) { 1.611 + case ENOENT: /* no such file? */ 1.612 + if (((mailbox[0] == 'I') || (mailbox[0] == 'i')) && 1.613 + ((mailbox[1] == 'N') || (mailbox[1] == 'n')) && 1.614 + ((mailbox[2] == 'B') || (mailbox[2] == 'b')) && 1.615 + ((mailbox[3] == 'O') || (mailbox[3] == 'o')) && 1.616 + ((mailbox[4] == 'X') || (mailbox[4] == 'x')) && !mailbox[5]) 1.617 + dummy_create (NIL,"INBOX.MTX"); 1.618 + else { 1.619 + mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL); 1.620 + return NIL; 1.621 + } 1.622 + /* falls through */ 1.623 + case 0: /* merely empty file? */ 1.624 + break; 1.625 + case EINVAL: 1.626 + return mtx_badname (tmp,mailbox); 1.627 + default: 1.628 + sprintf (tmp,"Not a MTX-format mailbox: %.80s",mailbox); 1.629 + mm_log (tmp,ERROR); 1.630 + return NIL; 1.631 + } 1.632 + /* get first message */ 1.633 + if (!(*af) (stream,data,&flags,&date,&message)) return NIL; 1.634 + /* open destination mailbox */ 1.635 + if (!mailboxfile (file,mailbox) || 1.636 + ((fd = open (file,O_BINARY|O_WRONLY|O_APPEND|O_CREAT, 1.637 + S_IREAD|S_IWRITE)) < 0) || !(df = fdopen (fd,"ab"))) { 1.638 + sprintf (tmp,"Can't open append mailbox: %s",strerror (errno)); 1.639 + mm_log (tmp,ERROR); 1.640 + return NIL; 1.641 + } 1.642 + mm_critical (stream); /* go critical */ 1.643 + fstat (fd,&sbuf); /* get current file size */ 1.644 + 1.645 + errno = 0; 1.646 + do { /* parse flags */ 1.647 + if (!SIZE (message)) { /* guard against zero-length */ 1.648 + mm_log ("Append of zero-length message",ERROR); 1.649 + ret = NIL; 1.650 + break; 1.651 + } 1.652 + f = mail_parse_flags (stream,flags,&i); 1.653 + /* reverse bits (dontcha wish we had CIRC?) */ 1.654 + for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i))); 1.655 + if (date) { /* parse date if given */ 1.656 + if (!mail_parse_date (&elt,date)) { 1.657 + sprintf (tmp,"Bad date in append: %.80s",date); 1.658 + mm_log (tmp,ERROR); 1.659 + ret = NIL; /* mark failure */ 1.660 + break; 1.661 + } 1.662 + mail_date (tmp,&elt); /* write preseved date */ 1.663 + } 1.664 + else internal_date (tmp); /* get current date in IMAP format */ 1.665 + /* write header */ 1.666 + if (fprintf (df,"%s,%lu;%010lo%02lo\015\012",tmp,i = SIZE (message),uf, 1.667 + (unsigned long) f) < 0) ret = NIL; 1.668 + else { /* write message */ 1.669 + if (i) do c = 0xff & SNX (message); 1.670 + while ((putc (c,df) != EOF) && --i); 1.671 + /* get next message */ 1.672 + if (i || !(*af) (stream,data,&flags,&date,&message)) ret = NIL; 1.673 + } 1.674 + } while (ret && message); 1.675 + /* revert file if failure */ 1.676 + if (!ret || (fflush (df) == EOF)) { 1.677 + chsize (fd,sbuf.st_size); /* revert file */ 1.678 + close (fd); /* make sure fclose() doesn't corrupt us */ 1.679 + if (errno) { 1.680 + sprintf (tmp,"Message append failed: %s",strerror (errno)); 1.681 + mm_log (tmp,ERROR); 1.682 + } 1.683 + ret = NIL; 1.684 + } 1.685 + fclose (df); /* close the file */ 1.686 + mm_nocritical (stream); /* release critical */ 1.687 + if (ret && mail_parameters (NIL,GET_APPENDUID,NIL)) 1.688 + mm_log ("Can not return meaningful APPENDUID with this mailbox format", 1.689 + WARN); 1.690 + return ret; 1.691 +} 1.692 + 1.693 + 1.694 +/* Return bad file name error message 1.695 + * Accepts: temporary buffer 1.696 + * file name 1.697 + * Returns: long NIL always 1.698 + */ 1.699 + 1.700 +long mtx_badname (char *tmp,char *s) 1.701 +{ 1.702 + sprintf (tmp,"Invalid mailbox name: %s",s); 1.703 + mm_log (tmp,ERROR); 1.704 + return (long) NIL; 1.705 +} 1.706 + 1.707 +/* Parse mailbox 1.708 + * Accepts: MAIL stream 1.709 + * Returns: T if parse OK 1.710 + * NIL if failure, stream aborted 1.711 + */ 1.712 + 1.713 +long mtx_parse (MAILSTREAM *stream) 1.714 +{ 1.715 + struct stat sbuf; 1.716 + MESSAGECACHE *elt = NIL; 1.717 + unsigned char *s,*t,*x,lbuf[65]; 1.718 + char tmp[MAILTMPLEN]; 1.719 + long i; 1.720 + long curpos = LOCAL->filesize; 1.721 + long nmsgs = stream->nmsgs; 1.722 + long recent = stream->recent; 1.723 + fstat (LOCAL->fd,&sbuf); /* get status */ 1.724 + if (sbuf.st_size < curpos) { /* sanity check */ 1.725 + sprintf (tmp,"Mailbox shrank from %ld to %ld!",curpos,sbuf.st_size); 1.726 + mm_log (tmp,ERROR); 1.727 + mtx_close (stream,NIL); 1.728 + return NIL; 1.729 + } 1.730 + /* while there is stuff to parse */ 1.731 + while (i = sbuf.st_size - curpos) { 1.732 + /* get to that position in the file */ 1.733 + lseek (LOCAL->fd,curpos,SEEK_SET); 1.734 + if ((i = read (LOCAL->fd,lbuf,64)) <= 0) { 1.735 + sprintf (tmp,"Unable to read internal header at %ld, size = %ld: %s", 1.736 + curpos,sbuf.st_size,i ? strerror (errno) : "no data read"); 1.737 + mm_log (tmp,ERROR); 1.738 + mtx_close (stream,NIL); 1.739 + return NIL; 1.740 + } 1.741 + lbuf[i] = '\0'; /* tie off buffer just in case */ 1.742 + if (!((s = strchr (lbuf,'\015')) && (s[1] == '\012'))) { 1.743 + sprintf (tmp,"Unable to find end of line at %ld in %ld bytes, text: %s", 1.744 + curpos,i,(char *) lbuf); 1.745 + mm_log (tmp,ERROR); 1.746 + mtx_close (stream,NIL); 1.747 + return NIL; 1.748 + } 1.749 + *s = '\0'; /* tie off header line */ 1.750 + i = (s + 2) - lbuf; /* note start of text offset */ 1.751 + if (!((s = strchr (lbuf,',')) && (t = strchr (s+1,';')))) { 1.752 + sprintf (tmp,"Unable to parse internal header at %ld: %s",curpos, 1.753 + (char *) lbuf); 1.754 + mm_log (tmp,ERROR); 1.755 + mtx_close (stream,NIL); 1.756 + return NIL; 1.757 + } 1.758 + 1.759 + *s++ = '\0'; *t++ = '\0'; /* tie off fields */ 1.760 + /* intantiate an elt for this message */ 1.761 + (elt = mail_elt (stream,++nmsgs))->valid = T; 1.762 + elt->private.uid = ++stream->uid_last; 1.763 + /* note file offset of header */ 1.764 + elt->private.special.offset = curpos; 1.765 + /* as well as offset from header of message */ 1.766 + elt->private.special.text.size = i; 1.767 + /* header size not known yet */ 1.768 + elt->private.msg.header.text.size = 0; 1.769 + /* parse the header components */ 1.770 + if (!(mail_parse_date (elt,lbuf) && 1.771 + (elt->rfc822_size = strtol (x = s,(char **) &s,10)) && (!(s && *s))&& 1.772 + isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) && 1.773 + isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) && 1.774 + isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) && 1.775 + isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])) { 1.776 + sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s", 1.777 + curpos,(char *) lbuf,(char *) x,(char *) t); 1.778 + mtx_close (stream,NIL); 1.779 + return NIL; 1.780 + } 1.781 + /* update current position to next header */ 1.782 + curpos += i + elt->rfc822_size; 1.783 + /* calculate system flags */ 1.784 + if ((i = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T; 1.785 + if (i & fDELETED) elt->deleted = T; 1.786 + if (i & fFLAGGED) elt->flagged = T; 1.787 + if (i & fANSWERED) elt->answered = T; 1.788 + if (i & fDRAFT) elt->draft = T; 1.789 + if (curpos > sbuf.st_size) { 1.790 + sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)", 1.791 + elt->private.special.offset,curpos,sbuf.st_size); 1.792 + mm_log (tmp,ERROR); 1.793 + mtx_close (stream,NIL); 1.794 + return NIL; 1.795 + } 1.796 + } 1.797 + /* update parsed file size */ 1.798 + LOCAL->filesize = sbuf.st_size; 1.799 + mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */ 1.800 + mail_recent (stream,recent); /* and of change in recent messages */ 1.801 + return T; /* return the winnage */ 1.802 +} 1.803 + 1.804 +/* Update status string 1.805 + * Accepts: MAIL stream 1.806 + * message number 1.807 + */ 1.808 + 1.809 +void mtx_update_status (MAILSTREAM *stream,unsigned long msgno) 1.810 +{ 1.811 + char tmp[MAILTMPLEN]; 1.812 + MESSAGECACHE *elt = mail_elt (stream,msgno); 1.813 + unsigned long j,k = 0; 1.814 + /* not if readonly you don't */ 1.815 + if (stream->rdonly || !elt->valid) return; 1.816 + j = elt->user_flags; /* get user flags */ 1.817 + /* reverse bits (dontcha wish we had CIRC?) */ 1.818 + while (j) k |= 1 << 29 - find_rightmost_bit (&j); 1.819 + sprintf (tmp,"%010lo%02o",k, /* print new flag string */ 1.820 + fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) + 1.821 + (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) + 1.822 + (fDRAFT * elt->draft)); 1.823 + /* get to that place in the file */ 1.824 + lseek (LOCAL->fd,(off_t) elt->private.special.offset + 1.825 + elt->private.special.text.size - 14,SEEK_SET); 1.826 + write (LOCAL->fd,tmp,12); /* write new flags */ 1.827 +} 1.828 + 1.829 +/* MTX locate header for a message 1.830 + * Accepts: MAIL stream 1.831 + * message number 1.832 + * pointer to returned header size 1.833 + * Returns: position of header in file 1.834 + */ 1.835 + 1.836 +unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno, 1.837 + unsigned long *size) 1.838 +{ 1.839 + unsigned long siz; 1.840 + size_t i = 0; 1.841 + int q = 0; 1.842 + char *s,tmp[MAILTMPLEN]; 1.843 + MESSAGECACHE *elt = mail_elt (stream,msgno); 1.844 + long pos = elt->private.special.offset + elt->private.special.text.size; 1.845 + /* is header size known? */ 1.846 + if (!(*size = elt->private.msg.header.text.size)) { 1.847 + /* get to header position */ 1.848 + lseek (LOCAL->fd,pos,SEEK_SET); 1.849 + /* search message for CRLF CRLF */ 1.850 + for (siz = 1; siz <= elt->rfc822_size; siz++) { 1.851 + if (!i && /* buffer empty? */ 1.852 + (read (LOCAL->fd,s = tmp, 1.853 + i = (size_t) min(elt->rfc822_size-siz,(long)MAILTMPLEN))<= 0)) 1.854 + return pos; 1.855 + else i--; 1.856 + switch (q) { /* sniff at buffer */ 1.857 + case 0: /* first character */ 1.858 + q = (*s++ == '\015') ? 1 : 0; 1.859 + break; 1.860 + case 1: /* second character */ 1.861 + q = (*s++ == '\012') ? 2 : 0; 1.862 + break; 1.863 + case 2: /* third character */ 1.864 + q = (*s++ == '\015') ? 3 : 0; 1.865 + break; 1.866 + case 3: /* fourth character */ 1.867 + if (*s++ == '\012') { /* have the sequence? */ 1.868 + /* yes, note for later */ 1.869 + elt->private.msg.header.text.size = (*size = siz); 1.870 + return pos; /* return to caller */ 1.871 + } 1.872 + q = 0; /* lost... */ 1.873 + break; 1.874 + } 1.875 + } 1.876 + } 1.877 + return pos; /* have position */ 1.878 +}