imapext-2007
diff src/osdep/unix/mx.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/unix/mx.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,1287 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2008 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: MX mail routines 1.19 + * 1.20 + * Author(s): 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: 3 May 1996 1.29 + * Last Edited: 6 January 2008 1.30 + */ 1.31 + 1.32 + 1.33 +#include <stdio.h> 1.34 +#include <ctype.h> 1.35 +#include <errno.h> 1.36 +extern int errno; /* just in case */ 1.37 +#include "mail.h" 1.38 +#include "osdep.h" 1.39 +#include <pwd.h> 1.40 +#include <sys/stat.h> 1.41 +#include <sys/time.h> 1.42 +#include "misc.h" 1.43 +#include "dummy.h" 1.44 +#include "fdstring.h" 1.45 + 1.46 +/* Index file */ 1.47 + 1.48 +#define MXINDEXNAME "/.mxindex" 1.49 +#define MXINDEX(d,s) strcat (mx_file (d,s),MXINDEXNAME) 1.50 + 1.51 + 1.52 +/* MX I/O stream local data */ 1.53 + 1.54 +typedef struct mx_local { 1.55 + int fd; /* file descriptor of open index */ 1.56 + unsigned char *buf; /* temporary buffer */ 1.57 + unsigned long buflen; /* current size of temporary buffer */ 1.58 + unsigned long cachedtexts; /* total size of all cached texts */ 1.59 + time_t scantime; /* last time directory scanned */ 1.60 +} MXLOCAL; 1.61 + 1.62 + 1.63 +/* Convenient access to local data */ 1.64 + 1.65 +#define LOCAL ((MXLOCAL *) stream->local) 1.66 + 1.67 + 1.68 +/* Function prototypes */ 1.69 + 1.70 +DRIVER *mx_valid (char *name); 1.71 +int mx_isvalid (char *name,char *tmp); 1.72 +int mx_namevalid (char *name); 1.73 +void *mx_parameters (long function,void *value); 1.74 +long mx_dirfmttest (char *name); 1.75 +void mx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents); 1.76 +long mx_scan_contents (char *name,char *contents,unsigned long csiz, 1.77 + unsigned long fsiz); 1.78 +void mx_list (MAILSTREAM *stream,char *ref,char *pat); 1.79 +void mx_lsub (MAILSTREAM *stream,char *ref,char *pat); 1.80 +long mx_subscribe (MAILSTREAM *stream,char *mailbox); 1.81 +long mx_unsubscribe (MAILSTREAM *stream,char *mailbox); 1.82 +long mx_create (MAILSTREAM *stream,char *mailbox); 1.83 +long mx_delete (MAILSTREAM *stream,char *mailbox); 1.84 +long mx_rename (MAILSTREAM *stream,char *old,char *newname); 1.85 +int mx_rename_work (char *src,size_t srcl,char *dst,size_t dstl,char *name); 1.86 +MAILSTREAM *mx_open (MAILSTREAM *stream); 1.87 +void mx_close (MAILSTREAM *stream,long options); 1.88 +void mx_fast (MAILSTREAM *stream,char *sequence,long flags); 1.89 +char *mx_fast_work (MAILSTREAM *stream,MESSAGECACHE *elt); 1.90 +char *mx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length, 1.91 + long flags); 1.92 +long mx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags); 1.93 +void mx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags); 1.94 +void mx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt); 1.95 +long mx_ping (MAILSTREAM *stream); 1.96 +void mx_check (MAILSTREAM *stream); 1.97 +long mx_expunge (MAILSTREAM *stream,char *sequence,long options); 1.98 +long mx_copy (MAILSTREAM *stream,char *sequence,char *mailbox, 1.99 + long options); 1.100 +long mx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data); 1.101 +long mx_append_msg (MAILSTREAM *stream,char *flags,MESSAGECACHE *elt, 1.102 + STRING *st,SEARCHSET *set); 1.103 + 1.104 +int mx_select (struct direct *name); 1.105 +int mx_numsort (const void *d1,const void *d2); 1.106 +char *mx_file (char *dst,char *name); 1.107 +long mx_lockindex (MAILSTREAM *stream); 1.108 +void mx_unlockindex (MAILSTREAM *stream); 1.109 +void mx_setdate (char *file,MESSAGECACHE *elt); 1.110 + 1.111 + 1.112 +/* MX mail routines */ 1.113 + 1.114 + 1.115 +/* Driver dispatch used by MAIL */ 1.116 + 1.117 +DRIVER mxdriver = { 1.118 + "mx", /* driver name */ 1.119 + /* driver flags */ 1.120 + DR_MAIL|DR_LOCAL|DR_NOFAST|DR_CRLF|DR_LOCKING|DR_DIRFMT, 1.121 + (DRIVER *) NIL, /* next driver */ 1.122 + mx_valid, /* mailbox is valid for us */ 1.123 + mx_parameters, /* manipulate parameters */ 1.124 + mx_scan, /* scan mailboxes */ 1.125 + mx_list, /* find mailboxes */ 1.126 + mx_lsub, /* find subscribed mailboxes */ 1.127 + mx_subscribe, /* subscribe to mailbox */ 1.128 + mx_unsubscribe, /* unsubscribe from mailbox */ 1.129 + mx_create, /* create mailbox */ 1.130 + mx_delete, /* delete mailbox */ 1.131 + mx_rename, /* rename mailbox */ 1.132 + mail_status_default, /* status of mailbox */ 1.133 + mx_open, /* open mailbox */ 1.134 + mx_close, /* close mailbox */ 1.135 + mx_fast, /* fetch message "fast" attributes */ 1.136 + NIL, /* fetch message flags */ 1.137 + NIL, /* fetch overview */ 1.138 + NIL, /* fetch message envelopes */ 1.139 + mx_header, /* fetch message header only */ 1.140 + mx_text, /* fetch message body only */ 1.141 + NIL, /* fetch partial message test */ 1.142 + NIL, /* unique identifier */ 1.143 + NIL, /* message number */ 1.144 + mx_flag, /* modify flags */ 1.145 + mx_flagmsg, /* per-message modify flags */ 1.146 + NIL, /* search for message based on criteria */ 1.147 + NIL, /* sort messages */ 1.148 + NIL, /* thread messages */ 1.149 + mx_ping, /* ping mailbox to see if still alive */ 1.150 + mx_check, /* check for new messages */ 1.151 + mx_expunge, /* expunge deleted messages */ 1.152 + mx_copy, /* copy messages to another mailbox */ 1.153 + mx_append, /* append string message to mailbox */ 1.154 + NIL /* garbage collect stream */ 1.155 +}; 1.156 + 1.157 + /* prototype stream */ 1.158 +MAILSTREAM mxproto = {&mxdriver}; 1.159 + 1.160 +/* MX mail validate mailbox 1.161 + * Accepts: mailbox name 1.162 + * Returns: our driver if name is valid, NIL otherwise 1.163 + */ 1.164 + 1.165 +DRIVER *mx_valid (char *name) 1.166 +{ 1.167 + char tmp[MAILTMPLEN]; 1.168 + return mx_isvalid (name,tmp) ? &mxdriver : NIL; 1.169 +} 1.170 + 1.171 + 1.172 +/* MX mail test for valid mailbox 1.173 + * Accepts: mailbox name 1.174 + * temporary buffer to use 1.175 + * Returns: T if valid, NIL otherwise with errno holding dir stat error 1.176 + */ 1.177 + 1.178 +int mx_isvalid (char *name,char *tmp) 1.179 +{ 1.180 + struct stat sbuf; 1.181 + errno = NIL; /* zap error */ 1.182 + if ((strlen (name) <= NETMAXMBX) && *mx_file (tmp,name) && 1.183 + !stat (tmp,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFDIR)) { 1.184 + /* name is directory; is it mx? */ 1.185 + if (!stat (MXINDEX (tmp,name),&sbuf) && 1.186 + ((sbuf.st_mode & S_IFMT) == S_IFREG)) return T; 1.187 + errno = NIL; /* directory but not mx */ 1.188 + } 1.189 + else if (!compare_cstring (name,"INBOX")) errno = NIL; 1.190 + return NIL; 1.191 +} 1.192 + 1.193 + 1.194 +/* MX mail test for valid mailbox 1.195 + * Accepts: mailbox name 1.196 + * Returns: T if valid, NIL otherwise 1.197 + */ 1.198 + 1.199 +int mx_namevalid (char *name) 1.200 +{ 1.201 + char *s = (*name == '/') ? name + 1 : name; 1.202 + while (s && *s) { /* make sure valid name */ 1.203 + if (isdigit (*s)) s++; /* digit, check this node further... */ 1.204 + else if (*s == '/') break; /* all digit node, barf */ 1.205 + /* non-digit, skip to next node or return */ 1.206 + else if (!((s = strchr (s+1,'/')) && *++s)) return T; 1.207 + } 1.208 + return NIL; /* all numeric or empty node */ 1.209 +} 1.210 + 1.211 +/* MX manipulate driver parameters 1.212 + * Accepts: function code 1.213 + * function-dependent value 1.214 + * Returns: function-dependent return value 1.215 + */ 1.216 + 1.217 +void *mx_parameters (long function,void *value) 1.218 +{ 1.219 + void *ret = NIL; 1.220 + switch ((int) function) { 1.221 + case GET_INBOXPATH: 1.222 + if (value) ret = mailboxfile ((char *) value,"~/INBOX"); 1.223 + break; 1.224 + case GET_DIRFMTTEST: 1.225 + ret = (void *) mx_dirfmttest; 1.226 + break; 1.227 + case GET_SCANCONTENTS: 1.228 + ret = (void *) mx_scan_contents; 1.229 + break; 1.230 + } 1.231 + return ret; 1.232 +} 1.233 + 1.234 + 1.235 +/* MX test for directory format internal node 1.236 + * Accepts: candidate node name 1.237 + * Returns: T if internal name, NIL otherwise 1.238 + */ 1.239 + 1.240 +long mx_dirfmttest (char *name) 1.241 +{ 1.242 + int c; 1.243 + /* success if index name or all-numberic */ 1.244 + if (strcmp (name,MXINDEXNAME+1)) 1.245 + while (c = *name++) if (!isdigit (c)) return NIL; 1.246 + return LONGT; 1.247 +} 1.248 + 1.249 +/* MX scan mailboxes 1.250 + * Accepts: mail stream 1.251 + * reference 1.252 + * pattern to search 1.253 + * string to scan 1.254 + */ 1.255 + 1.256 +void mx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents) 1.257 +{ 1.258 + if (stream) dummy_scan (NIL,ref,pat,contents); 1.259 +} 1.260 + 1.261 + 1.262 +/* MX scan mailbox for contents 1.263 + * Accepts: mailbox name 1.264 + * desired contents 1.265 + * contents size 1.266 + * file size (ignored) 1.267 + * Returns: NIL if contents not found, T if found 1.268 + */ 1.269 + 1.270 +long mx_scan_contents (char *name,char *contents,unsigned long csiz, 1.271 + unsigned long fsiz) 1.272 +{ 1.273 + long i,nfiles; 1.274 + void *a; 1.275 + char *s; 1.276 + long ret = NIL; 1.277 + size_t namelen = strlen (name); 1.278 + struct stat sbuf; 1.279 + struct direct **names = NIL; 1.280 + if ((nfiles = scandir (name,&names,mx_select,mx_numsort)) > 0) 1.281 + for (i = 0; i < nfiles; ++i) { 1.282 + if (!ret) { 1.283 + sprintf (s = (char *) fs_get (namelen + strlen (names[i]->d_name) + 2), 1.284 + "%s/%s",name,names[i]->d_name); 1.285 + if (!stat (s,&sbuf) && (csiz <= sbuf.st_size)) 1.286 + ret = dummy_scan_contents (s,contents,csiz,sbuf.st_size); 1.287 + fs_give ((void **) &s); 1.288 + } 1.289 + fs_give ((void **) &names[i]); 1.290 + } 1.291 + /* free directory list */ 1.292 + if (a = (void *) names) fs_give ((void **) &a); 1.293 + return ret; 1.294 +} 1.295 + 1.296 +/* MX list mailboxes 1.297 + * Accepts: mail stream 1.298 + * reference 1.299 + * pattern to search 1.300 + */ 1.301 + 1.302 +void mx_list (MAILSTREAM *stream,char *ref,char *pat) 1.303 +{ 1.304 + if (stream) dummy_list (NIL,ref,pat); 1.305 +} 1.306 + 1.307 + 1.308 +/* MX list subscribed mailboxes 1.309 + * Accepts: mail stream 1.310 + * reference 1.311 + * pattern to search 1.312 + */ 1.313 + 1.314 +void mx_lsub (MAILSTREAM *stream,char *ref,char *pat) 1.315 +{ 1.316 + if (stream) dummy_lsub (NIL,ref,pat); 1.317 +} 1.318 + 1.319 +/* MX mail subscribe to mailbox 1.320 + * Accepts: mail stream 1.321 + * mailbox to add to subscription list 1.322 + * Returns: T on success, NIL on failure 1.323 + */ 1.324 + 1.325 +long mx_subscribe (MAILSTREAM *stream,char *mailbox) 1.326 +{ 1.327 + return sm_subscribe (mailbox); 1.328 +} 1.329 + 1.330 + 1.331 +/* MX mail unsubscribe to mailbox 1.332 + * Accepts: mail stream 1.333 + * mailbox to delete from subscription list 1.334 + * Returns: T on success, NIL on failure 1.335 + */ 1.336 + 1.337 +long mx_unsubscribe (MAILSTREAM *stream,char *mailbox) 1.338 +{ 1.339 + return sm_unsubscribe (mailbox); 1.340 +} 1.341 + 1.342 +/* MX mail create mailbox 1.343 + * Accepts: mail stream 1.344 + * mailbox name to create 1.345 + * Returns: T on success, NIL on failure 1.346 + */ 1.347 + 1.348 +long mx_create (MAILSTREAM *stream,char *mailbox) 1.349 +{ 1.350 + DRIVER *test; 1.351 + int fd; 1.352 + char *s,tmp[MAILTMPLEN]; 1.353 + int mask = umask (0); 1.354 + long ret = NIL; 1.355 + if (!mx_namevalid (mailbox)) /* validate name */ 1.356 + sprintf (tmp,"Can't create mailbox %.80s: invalid MX-format name",mailbox); 1.357 + /* must not already exist */ 1.358 + else if ((test = mail_valid (NIL,mailbox,NIL)) && 1.359 + strcmp (test->name,"dummy")) 1.360 + sprintf (tmp,"Can't create mailbox %.80s: mailbox already exists",mailbox); 1.361 + /* create directory */ 1.362 + else if (!dummy_create_path (stream,MXINDEX (tmp,mailbox), 1.363 + get_dir_protection (mailbox))) 1.364 + sprintf (tmp,"Can't create mailbox %.80s: %s",mailbox,strerror (errno)); 1.365 + else { /* success */ 1.366 + /* set index protection */ 1.367 + set_mbx_protections (mailbox,tmp); 1.368 + /* tie off directory name */ 1.369 + *(s = strrchr (tmp,'/') + 1) = '\0'; 1.370 + /* set directory protection */ 1.371 + set_mbx_protections (mailbox,tmp); 1.372 + ret = LONGT; 1.373 + } 1.374 + umask (mask); /* restore mask */ 1.375 + if (!ret) MM_LOG (tmp,ERROR); /* some error */ 1.376 + return ret; 1.377 +} 1.378 + 1.379 +/* MX mail delete mailbox 1.380 + * mailbox name to delete 1.381 + * Returns: T on success, NIL on failure 1.382 + */ 1.383 + 1.384 +long mx_delete (MAILSTREAM *stream,char *mailbox) 1.385 +{ 1.386 + DIR *dirp; 1.387 + struct direct *d; 1.388 + char *s; 1.389 + char tmp[MAILTMPLEN]; 1.390 + if (!mx_isvalid (mailbox,tmp)) 1.391 + sprintf (tmp,"Can't delete mailbox %.80s: no such mailbox",mailbox); 1.392 + /* delete index */ 1.393 + else if (unlink (MXINDEX (tmp,mailbox))) 1.394 + sprintf (tmp,"Can't delete mailbox %.80s index: %s", 1.395 + mailbox,strerror (errno)); 1.396 + else { /* get directory name */ 1.397 + *(s = strrchr (tmp,'/')) = '\0'; 1.398 + if (dirp = opendir (tmp)) { /* open directory */ 1.399 + *s++ = '/'; /* restore delimiter */ 1.400 + /* massacre messages */ 1.401 + while (d = readdir (dirp)) if (mx_select (d)) { 1.402 + strcpy (s,d->d_name); /* make path */ 1.403 + unlink (tmp); /* sayonara */ 1.404 + } 1.405 + closedir (dirp); /* flush directory */ 1.406 + *(s = strrchr (tmp,'/')) = '\0'; 1.407 + if (rmdir (tmp)) { /* try to remove the directory */ 1.408 + sprintf (tmp,"Can't delete name %.80s: %s",mailbox,strerror (errno)); 1.409 + MM_LOG (tmp,WARN); 1.410 + } 1.411 + } 1.412 + return T; /* always success */ 1.413 + } 1.414 + MM_LOG (tmp,ERROR); /* something failed */ 1.415 + return NIL; 1.416 +} 1.417 + 1.418 +/* MX mail rename mailbox 1.419 + * Accepts: MX mail stream 1.420 + * old mailbox name 1.421 + * new mailbox name 1.422 + * Returns: T on success, NIL on failure 1.423 + */ 1.424 + 1.425 +long mx_rename (MAILSTREAM *stream,char *old,char *newname) 1.426 +{ 1.427 + char c,*s,tmp[MAILTMPLEN],tmp1[MAILTMPLEN]; 1.428 + struct stat sbuf; 1.429 + if (!mx_isvalid (old,tmp)) 1.430 + sprintf (tmp,"Can't rename mailbox %.80s: no such mailbox",old); 1.431 + else if (!mx_namevalid (newname)) 1.432 + sprintf (tmp,"Can't rename to mailbox %.80s: invalid MX-format name", 1.433 + newname); 1.434 + /* new mailbox name must not be valid */ 1.435 + else if (mx_isvalid (newname,tmp)) 1.436 + sprintf (tmp,"Can't rename to mailbox %.80s: destination already exists", 1.437 + newname); 1.438 + else { 1.439 + mx_file (tmp,old); /* build old directory name */ 1.440 + mx_file (tmp1,newname); /* and new directory name */ 1.441 + /* easy if not INBOX */ 1.442 + if (compare_cstring (old,"INBOX")) { 1.443 + /* found superior to destination name? */ 1.444 + if (s = strrchr (mx_file (tmp1,newname),'/')) { 1.445 + c = *++s; /* remember first character of inferior */ 1.446 + *s = '\0'; /* tie off to get just superior */ 1.447 + /* name doesn't exist, create it */ 1.448 + if ((stat (tmp1,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) && 1.449 + !dummy_create_path (stream,tmp1,get_dir_protection (newname))) 1.450 + return NIL; 1.451 + *s = c; /* restore full name */ 1.452 + } 1.453 + if (!rename (tmp,tmp1)) return LONGT; 1.454 + } 1.455 + 1.456 + /* RFC 3501 requires this */ 1.457 + else if (dummy_create_path (stream,strcat (tmp1,"/"), 1.458 + get_dir_protection (newname))) { 1.459 + void *a; 1.460 + int i,n,lasterror; 1.461 + struct direct **names = NIL; 1.462 + size_t srcl = strlen (tmp); 1.463 + size_t dstl = strlen (tmp1); 1.464 + /* rename each mx file to new directory */ 1.465 + for (i = lasterror = 0,n = scandir (tmp,&names,mx_select,mx_numsort); 1.466 + i < n; ++i) { 1.467 + if (mx_rename_work (tmp,srcl,tmp1,dstl,names[i]->d_name)) 1.468 + lasterror = errno; 1.469 + fs_give ((void **) &names[i]); 1.470 + } 1.471 + /* free directory list */ 1.472 + if (a = (void *) names) fs_give ((void **) &a); 1.473 + if (lasterror || mx_rename_work (tmp,srcl,tmp1,dstl,MXINDEXNAME+1)) 1.474 + errno = lasterror; 1.475 + else return mx_create (NIL,"INBOX"); 1.476 + } 1.477 + sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s", 1.478 + old,newname,strerror (errno)); 1.479 + } 1.480 + MM_LOG (tmp,ERROR); /* something failed */ 1.481 + return NIL; 1.482 +} 1.483 + 1.484 + 1.485 +/* MX rename worker routine 1.486 + * Accepts: source directory name 1.487 + * source directory name length 1.488 + * destination directory name 1.489 + * destination directory name length 1.490 + * name of node to move 1.491 + * Returns: zero if success, non-zero if error 1.492 + */ 1.493 + 1.494 +int mx_rename_work (char *src,size_t srcl,char *dst,size_t dstl,char *name) 1.495 +{ 1.496 + int ret; 1.497 + size_t len = strlen (name); 1.498 + char *s = (char *) fs_get (srcl + len + 2); 1.499 + char *d = (char *) fs_get (dstl + len + 1); 1.500 + sprintf (s,"%s/%s",src,name); 1.501 + sprintf (d,"%s%s",dst,name); 1.502 + ret = rename (s,d); 1.503 + fs_give ((void **) &s); 1.504 + fs_give ((void **) &d); 1.505 + return ret; 1.506 +} 1.507 + 1.508 +/* MX mail open 1.509 + * Accepts: stream to open 1.510 + * Returns: stream on success, NIL on failure 1.511 + */ 1.512 + 1.513 +MAILSTREAM *mx_open (MAILSTREAM *stream) 1.514 +{ 1.515 + char tmp[MAILTMPLEN]; 1.516 + /* return prototype for OP_PROTOTYPE call */ 1.517 + if (!stream) return user_flags (&mxproto); 1.518 + if (stream->local) fatal ("mx recycle stream"); 1.519 + stream->local = fs_get (sizeof (MXLOCAL)); 1.520 + /* note if an INBOX or not */ 1.521 + stream->inbox = !compare_cstring (stream->mailbox,"INBOX"); 1.522 + mx_file (tmp,stream->mailbox);/* get directory name */ 1.523 + /* canonicalize mailbox name */ 1.524 + fs_give ((void **) &stream->mailbox); 1.525 + stream->mailbox = cpystr (tmp); 1.526 + /* make temporary buffer */ 1.527 + LOCAL->buf = (char *) fs_get (CHUNKSIZE); 1.528 + LOCAL->buflen = CHUNKSIZE - 1; 1.529 + LOCAL->scantime = 0; /* not scanned yet */ 1.530 + LOCAL->fd = -1; /* no index yet */ 1.531 + LOCAL->cachedtexts = 0; /* no cached texts */ 1.532 + stream->sequence++; /* bump sequence number */ 1.533 + /* parse mailbox */ 1.534 + stream->nmsgs = stream->recent = 0; 1.535 + if (mx_ping (stream) && !(stream->nmsgs || stream->silent)) 1.536 + MM_LOG ("Mailbox is empty",(long) NIL); 1.537 + stream->perm_seen = stream->perm_deleted = stream->perm_flagged = 1.538 + stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T; 1.539 + stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff; 1.540 + stream->kwd_create = (stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ? 1.541 + NIL : T; /* can we create new user flags? */ 1.542 + return stream; /* return stream to caller */ 1.543 +} 1.544 + 1.545 +/* MX mail close 1.546 + * Accepts: MAIL stream 1.547 + * close options 1.548 + */ 1.549 + 1.550 +void mx_close (MAILSTREAM *stream,long options) 1.551 +{ 1.552 + if (LOCAL) { /* only if a file is open */ 1.553 + int silent = stream->silent; 1.554 + stream->silent = T; /* note this stream is dying */ 1.555 + if (options & CL_EXPUNGE) mx_expunge (stream,NIL,NIL); 1.556 + /* free local scratch buffer */ 1.557 + if (LOCAL->buf) fs_give ((void **) &LOCAL->buf); 1.558 + /* nuke the local data */ 1.559 + fs_give ((void **) &stream->local); 1.560 + stream->dtb = NIL; /* log out the DTB */ 1.561 + stream->silent = silent; /* reset silent state */ 1.562 + } 1.563 +} 1.564 + 1.565 +/* MX mail fetch fast information 1.566 + * Accepts: MAIL stream 1.567 + * sequence 1.568 + * option flags 1.569 + */ 1.570 + 1.571 +void mx_fast (MAILSTREAM *stream,char *sequence,long flags) 1.572 +{ 1.573 + unsigned long i; 1.574 + MESSAGECACHE *elt; 1.575 + if (stream && LOCAL && 1.576 + ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) : 1.577 + mail_sequence (stream,sequence))) 1.578 + for (i = 1; i <= stream->nmsgs; i++) 1.579 + if ((elt = mail_elt (stream,i))->sequence) mx_fast_work (stream,elt); 1.580 +} 1.581 + 1.582 + 1.583 +/* MX mail fetch fast information 1.584 + * Accepts: MAIL stream 1.585 + * message cache element 1.586 + * Returns: name of message file 1.587 + */ 1.588 + 1.589 +char *mx_fast_work (MAILSTREAM *stream,MESSAGECACHE *elt) 1.590 +{ 1.591 + struct stat sbuf; 1.592 + struct tm *tm; 1.593 + /* build message file name */ 1.594 + sprintf (LOCAL->buf,"%s/%lu",stream->mailbox,elt->private.uid); 1.595 + /* have size yet? */ 1.596 + if (!elt->rfc822_size && !stat (LOCAL->buf,&sbuf)) { 1.597 + /* make plausible IMAPish date string */ 1.598 + tm = gmtime (&sbuf.st_mtime); 1.599 + elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1; 1.600 + elt->year = tm->tm_year + 1900 - BASEYEAR; 1.601 + elt->hours = tm->tm_hour; elt->minutes = tm->tm_min; 1.602 + elt->seconds = tm->tm_sec; 1.603 + elt->zhours = 0; elt->zminutes = 0; elt->zoccident = 0; 1.604 + elt->rfc822_size = sbuf.st_size; 1.605 + } 1.606 + return (char *) LOCAL->buf; /* return file name */ 1.607 +} 1.608 + 1.609 +/* MX mail fetch message header 1.610 + * Accepts: MAIL stream 1.611 + * message # to fetch 1.612 + * pointer to returned header text length 1.613 + * option flags 1.614 + * Returns: message header in RFC822 format 1.615 + */ 1.616 + 1.617 +char *mx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length, 1.618 + long flags) 1.619 +{ 1.620 + unsigned long i; 1.621 + int fd; 1.622 + MESSAGECACHE *elt; 1.623 + *length = 0; /* default to empty */ 1.624 + if (flags & FT_UID) return "";/* UID call "impossible" */ 1.625 + elt = mail_elt (stream,msgno);/* get elt */ 1.626 + if (!elt->private.msg.header.text.data) { 1.627 + /* purge cache if too big */ 1.628 + if (LOCAL->cachedtexts > max (stream->nmsgs * 4096,2097152)) { 1.629 + mail_gc (stream,GC_TEXTS);/* just can't keep that much */ 1.630 + LOCAL->cachedtexts = 0; 1.631 + } 1.632 + if ((fd = open (mx_fast_work (stream,elt),O_RDONLY,NIL)) < 0) return ""; 1.633 + /* is buffer big enough? */ 1.634 + if (elt->rfc822_size > LOCAL->buflen) { 1.635 + fs_give ((void **) &LOCAL->buf); 1.636 + LOCAL->buf = (char *) fs_get ((LOCAL->buflen = elt->rfc822_size) + 1); 1.637 + } 1.638 + /* slurp message */ 1.639 + read (fd,LOCAL->buf,elt->rfc822_size); 1.640 + /* tie off file */ 1.641 + LOCAL->buf[elt->rfc822_size] = '\0'; 1.642 + close (fd); /* flush message file */ 1.643 + /* find end of header */ 1.644 + if (elt->rfc822_size < 4) i = 0; 1.645 + else for (i = 4; (i < elt->rfc822_size) && 1.646 + !((LOCAL->buf[i - 4] == '\015') && 1.647 + (LOCAL->buf[i - 3] == '\012') && 1.648 + (LOCAL->buf[i - 2] == '\015') && 1.649 + (LOCAL->buf[i - 1] == '\012')); i++); 1.650 + /* copy header */ 1.651 + cpytxt (&elt->private.msg.header.text,LOCAL->buf,i); 1.652 + cpytxt (&elt->private.msg.text.text,LOCAL->buf+i,elt->rfc822_size - i); 1.653 + /* add to cached size */ 1.654 + LOCAL->cachedtexts += elt->rfc822_size; 1.655 + } 1.656 + *length = elt->private.msg.header.text.size; 1.657 + return (char *) elt->private.msg.header.text.data; 1.658 +} 1.659 + 1.660 +/* MX mail fetch message text (body only) 1.661 + * Accepts: MAIL stream 1.662 + * message # to fetch 1.663 + * pointer to returned stringstruct 1.664 + * option flags 1.665 + * Returns: T on success, NIL on failure 1.666 + */ 1.667 + 1.668 +long mx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags) 1.669 +{ 1.670 + unsigned long i; 1.671 + MESSAGECACHE *elt; 1.672 + /* UID call "impossible" */ 1.673 + if (flags & FT_UID) return NIL; 1.674 + elt = mail_elt (stream,msgno); 1.675 + /* snarf message if don't have it yet */ 1.676 + if (!elt->private.msg.text.text.data) { 1.677 + mx_header (stream,msgno,&i,flags); 1.678 + if (!elt->private.msg.text.text.data) return NIL; 1.679 + } 1.680 + /* mark as seen */ 1.681 + if (!(flags & FT_PEEK) && mx_lockindex (stream)) { 1.682 + elt->seen = T; 1.683 + mx_unlockindex (stream); 1.684 + MM_FLAGS (stream,msgno); 1.685 + } 1.686 + INIT (bs,mail_string,elt->private.msg.text.text.data, 1.687 + elt->private.msg.text.text.size); 1.688 + return T; 1.689 +} 1.690 + 1.691 +/* MX mail modify flags 1.692 + * Accepts: MAIL stream 1.693 + * sequence 1.694 + * flag(s) 1.695 + * option flags 1.696 + */ 1.697 + 1.698 +void mx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags) 1.699 +{ 1.700 + mx_unlockindex (stream); /* finished with index */ 1.701 +} 1.702 + 1.703 + 1.704 +/* MX per-message modify flags 1.705 + * Accepts: MAIL stream 1.706 + * message cache element 1.707 + */ 1.708 + 1.709 +void mx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt) 1.710 +{ 1.711 + mx_lockindex (stream); /* lock index if not already locked */ 1.712 +} 1.713 + 1.714 +/* MX mail ping mailbox 1.715 + * Accepts: MAIL stream 1.716 + * Returns: T if stream alive, else NIL 1.717 + */ 1.718 + 1.719 +long mx_ping (MAILSTREAM *stream) 1.720 +{ 1.721 + MAILSTREAM *sysibx = NIL; 1.722 + MESSAGECACHE *elt,*selt; 1.723 + struct stat sbuf; 1.724 + char *s,tmp[MAILTMPLEN]; 1.725 + int fd; 1.726 + unsigned long i,j,r,old; 1.727 + long nmsgs = stream->nmsgs; 1.728 + long recent = stream->recent; 1.729 + int silent = stream->silent; 1.730 + if (stat (stream->mailbox,&sbuf)) return NIL; 1.731 + stream->silent = T; /* don't pass up exists events yet */ 1.732 + if (sbuf.st_ctime != LOCAL->scantime) { 1.733 + struct direct **names = NIL; 1.734 + long nfiles = scandir (stream->mailbox,&names,mx_select,mx_numsort); 1.735 + if (nfiles < 0) nfiles = 0; /* in case error */ 1.736 + old = stream->uid_last; 1.737 + /* note scanned now */ 1.738 + LOCAL->scantime = sbuf.st_ctime; 1.739 + /* scan directory */ 1.740 + for (i = 0; i < nfiles; ++i) { 1.741 + /* if newly seen, add to list */ 1.742 + if ((j = atoi (names[i]->d_name)) > old) { 1.743 + /* swell the cache */ 1.744 + mail_exists (stream,++nmsgs); 1.745 + stream->uid_last = (elt = mail_elt (stream,nmsgs))->private.uid = j; 1.746 + elt->valid = T; /* note valid flags */ 1.747 + if (old) { /* other than the first pass? */ 1.748 + elt->recent = T; /* yup, mark as recent */ 1.749 + recent++; /* bump recent count */ 1.750 + } 1.751 + } 1.752 + fs_give ((void **) &names[i]); 1.753 + } 1.754 + /* free directory */ 1.755 + if (s = (void *) names) fs_give ((void **) &s); 1.756 + } 1.757 + stream->nmsgs = nmsgs; /* don't upset mail_uid() */ 1.758 + 1.759 + /* if INBOX, snarf from system INBOX */ 1.760 + if (mx_lockindex (stream) && stream->inbox && 1.761 + !strcmp (sysinbox (),stream->mailbox)) { 1.762 + old = stream->uid_last; 1.763 + MM_CRITICAL (stream); /* go critical */ 1.764 + /* see if anything in system inbox */ 1.765 + if (!stat (sysinbox (),&sbuf) && sbuf.st_size && 1.766 + (sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) && 1.767 + !sysibx->rdonly && (r = sysibx->nmsgs)) { 1.768 + for (i = 1; i <= r; ++i) {/* for each message in sysinbox mailbox */ 1.769 + /* build file name we will use */ 1.770 + sprintf (LOCAL->buf,"%s/%lu",stream->mailbox,++old); 1.771 + /* snarf message from Berkeley mailbox */ 1.772 + selt = mail_elt (sysibx,i); 1.773 + if (((fd = open (LOCAL->buf,O_WRONLY|O_CREAT|O_EXCL, 1.774 + (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) 1.775 + >= 0) && 1.776 + (s = mail_fetchheader_full (sysibx,i,NIL,&j,FT_PEEK)) && 1.777 + (write (fd,s,j) == j) && 1.778 + (s = mail_fetchtext_full (sysibx,i,&j,FT_PEEK)) && 1.779 + (write (fd,s,j) == j) && !fsync (fd) && !close (fd)) { 1.780 + /* swell the cache */ 1.781 + mail_exists (stream,++nmsgs); 1.782 + stream->uid_last = /* create new elt, note its file number */ 1.783 + (elt = mail_elt (stream,nmsgs))->private.uid = old; 1.784 + recent++; /* bump recent count */ 1.785 + /* set up initial flags and date */ 1.786 + elt->valid = elt->recent = T; 1.787 + elt->seen = selt->seen; 1.788 + elt->deleted = selt->deleted; 1.789 + elt->flagged = selt->flagged; 1.790 + elt->answered = selt->answered; 1.791 + elt->draft = selt->draft; 1.792 + elt->day = selt->day;elt->month = selt->month;elt->year = selt->year; 1.793 + elt->hours = selt->hours;elt->minutes = selt->minutes; 1.794 + elt->seconds = selt->seconds; 1.795 + elt->zhours = selt->zhours; elt->zminutes = selt->zminutes; 1.796 + elt->zoccident = selt->zoccident; 1.797 + mx_setdate (LOCAL->buf,elt); 1.798 + sprintf (tmp,"%lu",i);/* delete it from the sysinbox */ 1.799 + mail_flag (sysibx,tmp,"\\Deleted",ST_SET); 1.800 + } 1.801 + else { /* failed to snarf */ 1.802 + if (fd) { /* did it ever get opened? */ 1.803 + close (fd); /* close descriptor */ 1.804 + unlink (LOCAL->buf);/* flush this file */ 1.805 + } 1.806 + sprintf (tmp,"Message copy to MX mailbox failed: %.80s", 1.807 + s,strerror (errno)); 1.808 + MM_LOG (tmp,ERROR); 1.809 + r = 0; /* stop the snarf in its tracks */ 1.810 + } 1.811 + } 1.812 + /* update scan time */ 1.813 + if (!stat (stream->mailbox,&sbuf)) LOCAL->scantime = sbuf.st_ctime; 1.814 + mail_expunge (sysibx); /* now expunge all those messages */ 1.815 + } 1.816 + if (sysibx) mail_close (sysibx); 1.817 + MM_NOCRITICAL (stream); /* release critical */ 1.818 + } 1.819 + mx_unlockindex (stream); /* done with index */ 1.820 + stream->silent = silent; /* can pass up events now */ 1.821 + mail_exists (stream,nmsgs); /* notify upper level of mailbox size */ 1.822 + mail_recent (stream,recent); 1.823 + return T; /* return that we are alive */ 1.824 +} 1.825 + 1.826 +/* MX mail check mailbox 1.827 + * Accepts: MAIL stream 1.828 + */ 1.829 + 1.830 +void mx_check (MAILSTREAM *stream) 1.831 +{ 1.832 + if (mx_ping (stream)) MM_LOG ("Check completed",(long) NIL); 1.833 +} 1.834 + 1.835 + 1.836 +/* MX mail expunge mailbox 1.837 + * Accepts: MAIL stream 1.838 + * sequence to expunge if non-NIL 1.839 + * expunge options 1.840 + * Returns: T, always 1.841 + */ 1.842 + 1.843 +long mx_expunge (MAILSTREAM *stream,char *sequence,long options) 1.844 +{ 1.845 + long ret; 1.846 + MESSAGECACHE *elt; 1.847 + unsigned long i = 1; 1.848 + unsigned long n = 0; 1.849 + unsigned long recent = stream->recent; 1.850 + if (ret = (sequence ? ((options & EX_UID) ? 1.851 + mail_uid_sequence (stream,sequence) : 1.852 + mail_sequence (stream,sequence)) : LONGT) && 1.853 + mx_lockindex (stream)) { /* lock the index */ 1.854 + MM_CRITICAL (stream); /* go critical */ 1.855 + while (i <= stream->nmsgs) {/* for each message */ 1.856 + elt = mail_elt (stream,i);/* if deleted, need to trash it */ 1.857 + if (elt->deleted && (sequence ? elt->sequence : T)) { 1.858 + sprintf (LOCAL->buf,"%s/%lu",stream->mailbox,elt->private.uid); 1.859 + if (unlink (LOCAL->buf)) {/* try to delete the message */ 1.860 + sprintf (LOCAL->buf,"Expunge of message %lu failed, aborted: %s",i, 1.861 + strerror (errno)); 1.862 + MM_LOG (LOCAL->buf,(long) NIL); 1.863 + break; 1.864 + } 1.865 + /* note uncached */ 1.866 + LOCAL->cachedtexts -= ((elt->private.msg.header.text.data ? 1.867 + elt->private.msg.header.text.size : 0) + 1.868 + (elt->private.msg.text.text.data ? 1.869 + elt->private.msg.text.text.size : 0)); 1.870 + mail_gc_msg (&elt->private.msg,GC_ENV | GC_TEXTS); 1.871 + if(elt->recent)--recent;/* if recent, note one less recent message */ 1.872 + mail_expunged(stream,i);/* notify upper levels */ 1.873 + n++; /* count up one more expunged message */ 1.874 + } 1.875 + else i++; /* otherwise try next message */ 1.876 + } 1.877 + if (n) { /* output the news if any expunged */ 1.878 + sprintf (LOCAL->buf,"Expunged %lu messages",n); 1.879 + MM_LOG (LOCAL->buf,(long) NIL); 1.880 + } 1.881 + else MM_LOG ("No messages deleted, so no update needed",(long) NIL); 1.882 + MM_NOCRITICAL (stream); /* release critical */ 1.883 + mx_unlockindex (stream); /* finished with index */ 1.884 + /* notify upper level of new mailbox size */ 1.885 + mail_exists (stream,stream->nmsgs); 1.886 + mail_recent (stream,recent); 1.887 + } 1.888 + return ret; 1.889 +} 1.890 + 1.891 +/* MX mail copy message(s) 1.892 + * Accepts: MAIL stream 1.893 + * sequence 1.894 + * destination mailbox 1.895 + * copy options 1.896 + * Returns: T if copy successful, else NIL 1.897 + */ 1.898 + 1.899 +long mx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options) 1.900 +{ 1.901 + FDDATA d; 1.902 + STRING st; 1.903 + MESSAGECACHE *elt; 1.904 + MAILSTREAM *astream; 1.905 + struct stat sbuf; 1.906 + int fd; 1.907 + unsigned long i,j,uid,uidv; 1.908 + char *t,tmp[MAILTMPLEN]; 1.909 + long ret; 1.910 + mailproxycopy_t pc = 1.911 + (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL); 1.912 + /* make sure valid mailbox */ 1.913 + if (!mx_valid (mailbox)) switch (errno) { 1.914 + case NIL: /* no error in stat() */ 1.915 + if (pc) return (*pc) (stream,sequence,mailbox,options); 1.916 + sprintf (LOCAL->buf,"Not a MX-format mailbox: %.80s",mailbox); 1.917 + MM_LOG (LOCAL->buf,ERROR); 1.918 + return NIL; 1.919 + default: /* some stat() error */ 1.920 + MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL); 1.921 + return NIL; 1.922 + } 1.923 + /* copy the messages */ 1.924 + if (!(ret = ((options & CP_UID) ? mail_uid_sequence (stream,sequence) : 1.925 + mail_sequence (stream,sequence)))); 1.926 + /* acquire stream to append to */ 1.927 + else if (!(astream = mail_open (NIL,mailbox,OP_SILENT))) { 1.928 + MM_LOG ("Can't open copy mailbox",ERROR); 1.929 + ret = NIL; 1.930 + } 1.931 + else { 1.932 + MM_CRITICAL (stream); /* go critical */ 1.933 + if (!(ret = mx_lockindex (astream))) 1.934 + MM_LOG ("Message copy failed: unable to lock index",ERROR); 1.935 + else { 1.936 + 1.937 + copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL); 1.938 + SEARCHSET *source = cu ? mail_newsearchset () : NIL; 1.939 + SEARCHSET *dest = cu ? mail_newsearchset () : NIL; 1.940 + for (i = 1,uid = uidv = 0; ret && (i <= stream->nmsgs); i++) 1.941 + if ((elt = mail_elt (stream,i))->sequence) { 1.942 + if (ret = ((fd = open (mx_fast_work (stream,elt),O_RDONLY,NIL)) 1.943 + >= 0)) { 1.944 + fstat (fd,&sbuf); /* get size of message */ 1.945 + d.fd = fd; /* set up file descriptor */ 1.946 + d.pos = 0; /* start of file */ 1.947 + d.chunk = LOCAL->buf; 1.948 + d.chunksize = CHUNKSIZE; 1.949 + INIT (&st,fd_string,&d,sbuf.st_size); 1.950 + /* init flag string */ 1.951 + tmp[0] = tmp[1] = '\0'; 1.952 + if (j = elt->user_flags) do 1.953 + if (t = stream->user_flags[find_rightmost_bit (&j)]) 1.954 + strcat (strcat (tmp," "),t); 1.955 + while (j); 1.956 + if (elt->seen) strcat (tmp," \\Seen"); 1.957 + if (elt->deleted) strcat (tmp," \\Deleted"); 1.958 + if (elt->flagged) strcat (tmp," \\Flagged"); 1.959 + if (elt->answered) strcat (tmp," \\Answered"); 1.960 + if (elt->draft) strcat (tmp," \\Draft"); 1.961 + tmp[0] = '('; /* open list */ 1.962 + strcat (tmp,")"); /* close list */ 1.963 + if (ret = mx_append_msg (astream,tmp,elt,&st,dest)) { 1.964 + /* add to source set if needed */ 1.965 + if (source) mail_append_set (source,mail_uid (stream,i)); 1.966 + /* delete if doing a move */ 1.967 + if (options & CP_MOVE) elt->deleted = T; 1.968 + } 1.969 + } 1.970 + } 1.971 + /* return sets if doing COPYUID */ 1.972 + if (cu && ret) (*cu) (stream,mailbox,astream->uid_validity,source,dest); 1.973 + else { /* flush any sets we may have built */ 1.974 + mail_free_searchset (&source); 1.975 + mail_free_searchset (&dest); 1.976 + } 1.977 + mx_unlockindex (astream); /* unlock index */ 1.978 + } 1.979 + MM_NOCRITICAL (stream); 1.980 + mail_close (astream); /* finished with append stream */ 1.981 + } 1.982 + return ret; /* return success */ 1.983 +} 1.984 + 1.985 +/* MX mail append message from stringstruct 1.986 + * Accepts: MAIL stream 1.987 + * destination mailbox 1.988 + * append callback 1.989 + * data for callback 1.990 + * Returns: T if append successful, else NIL 1.991 + */ 1.992 + 1.993 +long mx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data) 1.994 +{ 1.995 + MESSAGECACHE elt; 1.996 + MAILSTREAM *astream; 1.997 + char *flags,*date,tmp[MAILTMPLEN]; 1.998 + STRING *message; 1.999 + long ret = LONGT; 1.1000 + /* default stream to prototype */ 1.1001 + if (!stream) stream = user_flags (&mxproto); 1.1002 + /* N.B.: can't use LOCAL->buf for tmp */ 1.1003 + /* make sure valid mailbox */ 1.1004 + if (!mx_isvalid (mailbox,tmp)) switch (errno) { 1.1005 + case ENOENT: /* no such file? */ 1.1006 + if (!compare_cstring (mailbox,"INBOX")) mx_create (NIL,"INBOX"); 1.1007 + else { 1.1008 + MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL); 1.1009 + return NIL; 1.1010 + } 1.1011 + /* falls through */ 1.1012 + case 0: /* merely empty file? */ 1.1013 + break; 1.1014 + case EINVAL: 1.1015 + sprintf (tmp,"Invalid MX-format mailbox name: %.80s",mailbox); 1.1016 + MM_LOG (tmp,ERROR); 1.1017 + return NIL; 1.1018 + default: 1.1019 + sprintf (tmp,"Not a MX-format mailbox: %.80s",mailbox); 1.1020 + MM_LOG (tmp,ERROR); 1.1021 + return NIL; 1.1022 + } 1.1023 + 1.1024 + /* get first message */ 1.1025 + if (!MM_APPEND (af) (stream,data,&flags,&date,&message)) return NIL; 1.1026 + if (!(astream = mail_open (NIL,mailbox,OP_SILENT))) { 1.1027 + MM_LOG ("Can't open append mailbox",ERROR); 1.1028 + return NIL; 1.1029 + } 1.1030 + MM_CRITICAL (astream); /* go critical */ 1.1031 + /* lock the index */ 1.1032 + if (!(ret = mx_lockindex (astream))) 1.1033 + MM_LOG ("Message append failed: unable to lock index",ERROR); 1.1034 + else { 1.1035 + appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL); 1.1036 + SEARCHSET *dst = au ? mail_newsearchset () : NIL; 1.1037 + do { 1.1038 + /* guard against zero-length */ 1.1039 + if (!(ret = SIZE (message))) 1.1040 + MM_LOG ("Append of zero-length message",ERROR); 1.1041 + else if (date && !(ret = mail_parse_date (&elt,date))) { 1.1042 + sprintf (tmp,"Bad date in append: %.80s",date); 1.1043 + MM_LOG (tmp,ERROR); 1.1044 + } 1.1045 + else ret = mx_append_msg (astream,flags,date ? &elt : NIL,message,dst)&& 1.1046 + MM_APPEND (af) (stream,data,&flags,&date,&message); 1.1047 + } while (ret && message); 1.1048 + /* return sets if doing APPENDUID */ 1.1049 + if (au && ret) (*au) (mailbox,astream->uid_validity,dst); 1.1050 + else mail_free_searchset (&dst); 1.1051 + mx_unlockindex (astream); /* unlock index */ 1.1052 + } 1.1053 + MM_NOCRITICAL (astream); /* release critical */ 1.1054 + mail_close (astream); 1.1055 + return ret; 1.1056 +} 1.1057 + 1.1058 +/* MX mail append single message 1.1059 + * Accepts: MAIL stream 1.1060 + * flags for new message if non-NIL 1.1061 + * elt with source date if non-NIL 1.1062 + * stringstruct of message text 1.1063 + * searchset to place UID 1.1064 + * Returns: T if success, NIL if failure 1.1065 + */ 1.1066 + 1.1067 +long mx_append_msg (MAILSTREAM *stream,char *flags,MESSAGECACHE *elt, 1.1068 + STRING *st,SEARCHSET *set) 1.1069 +{ 1.1070 + char tmp[MAILTMPLEN]; 1.1071 + int fd; 1.1072 + unsigned long uf; 1.1073 + long f = mail_parse_flags (stream,flags,&uf); 1.1074 + /* make message file name */ 1.1075 + sprintf (tmp,"%s/%lu",stream->mailbox,++stream->uid_last); 1.1076 + if ((fd = open (tmp,O_WRONLY|O_CREAT|O_EXCL, 1.1077 + (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) { 1.1078 + sprintf (tmp,"Can't create append message: %s",strerror (errno)); 1.1079 + MM_LOG (tmp,ERROR); 1.1080 + return NIL; 1.1081 + } 1.1082 + while (SIZE (st)) { /* copy the file */ 1.1083 + if (st->cursize && (write (fd,st->curpos,st->cursize) < 0)) { 1.1084 + unlink (tmp); /* delete file */ 1.1085 + close (fd); /* close the file */ 1.1086 + sprintf (tmp,"Message append failed: %s",strerror (errno)); 1.1087 + MM_LOG (tmp,ERROR); 1.1088 + return NIL; 1.1089 + } 1.1090 + SETPOS (st,GETPOS (st) + st->cursize); 1.1091 + } 1.1092 + close (fd); /* close the file */ 1.1093 + if (elt) mx_setdate (tmp,elt);/* set file date */ 1.1094 + /* swell the cache */ 1.1095 + mail_exists (stream,++stream->nmsgs); 1.1096 + /* copy flags */ 1.1097 + mail_append_set (set,(elt = mail_elt (stream,stream->nmsgs))->private.uid = 1.1098 + stream->uid_last); 1.1099 + if (f&fSEEN) elt->seen = T; 1.1100 + if (f&fDELETED) elt->deleted = T; 1.1101 + if (f&fFLAGGED) elt->flagged = T; 1.1102 + if (f&fANSWERED) elt->answered = T; 1.1103 + if (f&fDRAFT) elt->draft = T; 1.1104 + elt->user_flags |= uf; 1.1105 + return LONGT; 1.1106 +} 1.1107 + 1.1108 +/* Internal routines */ 1.1109 + 1.1110 + 1.1111 +/* MX file name selection test 1.1112 + * Accepts: candidate directory entry 1.1113 + * Returns: T to use file name, NIL to skip it 1.1114 + */ 1.1115 + 1.1116 +int mx_select (struct direct *name) 1.1117 +{ 1.1118 + char c; 1.1119 + char *s = name->d_name; 1.1120 + while (c = *s++) if (!isdigit (c)) return NIL; 1.1121 + return T; 1.1122 +} 1.1123 + 1.1124 + 1.1125 +/* MX file name comparision 1.1126 + * Accepts: first candidate directory entry 1.1127 + * second candidate directory entry 1.1128 + * Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2 1.1129 + */ 1.1130 + 1.1131 +int mx_numsort (const void *d1,const void *d2) 1.1132 +{ 1.1133 + return atoi ((*(struct direct **) d1)->d_name) - 1.1134 + atoi ((*(struct direct **) d2)->d_name); 1.1135 +} 1.1136 + 1.1137 + 1.1138 +/* MX mail build file name 1.1139 + * Accepts: destination string 1.1140 + * source 1.1141 + * Returns: destination 1.1142 + */ 1.1143 + 1.1144 +char *mx_file (char *dst,char *name) 1.1145 +{ 1.1146 + char *s; 1.1147 + /* empty string if mailboxfile fails */ 1.1148 + if (!mailboxfile (dst,name)) *dst = '\0'; 1.1149 + /* driver-selected INBOX */ 1.1150 + else if (!*dst) mailboxfile (dst,"~/INBOX"); 1.1151 + /* tie off unnecessary trailing / */ 1.1152 + else if ((s = strrchr (dst,'/')) && !s[1]) *s = '\0'; 1.1153 + return dst; 1.1154 +} 1.1155 + 1.1156 +/* MX read and lock index 1.1157 + * Accepts: MAIL stream 1.1158 + * Returns: T if success, NIL if failure 1.1159 + */ 1.1160 + 1.1161 +long mx_lockindex (MAILSTREAM *stream) 1.1162 +{ 1.1163 + unsigned long uf,sf,uid; 1.1164 + int k = 0; 1.1165 + unsigned long msgno = 1; 1.1166 + struct stat sbuf; 1.1167 + char *s,*t,*idx,tmp[2*MAILTMPLEN]; 1.1168 + MESSAGECACHE *elt; 1.1169 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.1170 + if ((LOCAL->fd < 0) && /* get index file, no-op if already have it */ 1.1171 + (LOCAL->fd = open (strcat (strcpy (tmp,stream->mailbox),MXINDEXNAME), 1.1172 + O_RDWR|O_CREAT, 1.1173 + (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) 1.1174 + >= 0) { 1.1175 + (*bn) (BLOCK_FILELOCK,NIL); 1.1176 + flock (LOCAL->fd,LOCK_EX); /* get exclusive lock */ 1.1177 + (*bn) (BLOCK_NONE,NIL); 1.1178 + fstat (LOCAL->fd,&sbuf); /* get size of index */ 1.1179 + /* slurp index */ 1.1180 + read (LOCAL->fd,s = idx = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size); 1.1181 + idx[sbuf.st_size] = '\0'; /* tie off index */ 1.1182 + /* parse index */ 1.1183 + if (sbuf.st_size) while (s && *s) switch (*s) { 1.1184 + case 'V': /* UID validity record */ 1.1185 + stream->uid_validity = strtoul (s+1,&s,16); 1.1186 + break; 1.1187 + case 'L': /* UID last record */ 1.1188 + stream->uid_last = strtoul (s+1,&s,16); 1.1189 + break; 1.1190 + case 'K': /* keyword */ 1.1191 + /* find end of keyword */ 1.1192 + if (s = strchr (t = ++s,'\n')) { 1.1193 + *s++ = '\0'; /* tie off keyword */ 1.1194 + /* copy keyword */ 1.1195 + if ((k < NUSERFLAGS) && !stream->user_flags[k] && 1.1196 + (strlen (t) <= MAXUSERFLAG)) stream->user_flags[k] = cpystr (t); 1.1197 + k++; /* one more keyword */ 1.1198 + } 1.1199 + break; 1.1200 + 1.1201 + case 'M': /* message status record */ 1.1202 + uid = strtoul (s+1,&s,16);/* get UID for this message */ 1.1203 + if (*s == ';') { /* get user flags */ 1.1204 + uf = strtoul (s+1,&s,16); 1.1205 + if (*s == '.') { /* get system flags */ 1.1206 + sf = strtoul (s+1,&s,16); 1.1207 + while ((msgno <= stream->nmsgs) && (mail_uid (stream,msgno) < uid)) 1.1208 + msgno++; /* find message number for this UID */ 1.1209 + if ((msgno <= stream->nmsgs) && (mail_uid (stream,msgno) == uid)) { 1.1210 + (elt = mail_elt (stream,msgno))->valid = T; 1.1211 + elt->user_flags=uf; /* set user and system flags in elt */ 1.1212 + if (sf & fSEEN) elt->seen = T; 1.1213 + if (sf & fDELETED) elt->deleted = T; 1.1214 + if (sf & fFLAGGED) elt->flagged = T; 1.1215 + if (sf & fANSWERED) elt->answered = T; 1.1216 + if (sf & fDRAFT) elt->draft = T; 1.1217 + } 1.1218 + break; 1.1219 + } 1.1220 + } 1.1221 + default: /* bad news */ 1.1222 + sprintf (tmp,"Error in index: %.80s",s); 1.1223 + MM_LOG (tmp,ERROR); 1.1224 + *s = NIL; /* ignore remainder of index */ 1.1225 + } 1.1226 + else { /* new index */ 1.1227 + stream->uid_validity = time (0); 1.1228 + user_flags (stream); /* init stream with default user flags */ 1.1229 + } 1.1230 + fs_give ((void **) &idx); /* flush index */ 1.1231 + } 1.1232 + return (LOCAL->fd >= 0) ? T : NIL; 1.1233 +} 1.1234 + 1.1235 +/* MX write and unlock index 1.1236 + * Accepts: MAIL stream 1.1237 + */ 1.1238 + 1.1239 +#define MXIXBUFLEN 2048 1.1240 + 1.1241 +void mx_unlockindex (MAILSTREAM *stream) 1.1242 +{ 1.1243 + unsigned long i,j; 1.1244 + off_t size = 0; 1.1245 + char *s,tmp[MXIXBUFLEN + 64]; 1.1246 + MESSAGECACHE *elt; 1.1247 + if (LOCAL->fd >= 0) { 1.1248 + lseek (LOCAL->fd,0,L_SET); /* rewind file */ 1.1249 + /* write header */ 1.1250 + sprintf (s = tmp,"V%08lxL%08lx",stream->uid_validity,stream->uid_last); 1.1251 + for (i = 0; (i < NUSERFLAGS) && stream->user_flags[i]; ++i) 1.1252 + sprintf (s += strlen (s),"K%s\n",stream->user_flags[i]); 1.1253 + /* write messages */ 1.1254 + for (i = 1; i <= stream->nmsgs; i++) { 1.1255 + /* filled buffer? */ 1.1256 + if (((s += strlen (s)) - tmp) > MXIXBUFLEN) { 1.1257 + write (LOCAL->fd,tmp,j = s - tmp); 1.1258 + size += j; 1.1259 + *(s = tmp) = '\0'; /* dump out and restart buffer */ 1.1260 + } 1.1261 + elt = mail_elt (stream,i); 1.1262 + sprintf(s,"M%08lx;%08lx.%04x",elt->private.uid,elt->user_flags,(unsigned) 1.1263 + ((fSEEN * elt->seen) + (fDELETED * elt->deleted) + 1.1264 + (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) + 1.1265 + (fDRAFT * elt->draft))); 1.1266 + } 1.1267 + /* write tail end of buffer */ 1.1268 + if ((s += strlen (s)) != tmp) { 1.1269 + write (LOCAL->fd,tmp,j = s - tmp); 1.1270 + size += j; 1.1271 + } 1.1272 + ftruncate (LOCAL->fd,size); 1.1273 + flock (LOCAL->fd,LOCK_UN); /* unlock the index */ 1.1274 + close (LOCAL->fd); /* finished with file */ 1.1275 + LOCAL->fd = -1; /* no index now */ 1.1276 + } 1.1277 +} 1.1278 + 1.1279 +/* Set date for message 1.1280 + * Accepts: file name 1.1281 + * elt containing date 1.1282 + */ 1.1283 + 1.1284 +void mx_setdate (char *file,MESSAGECACHE *elt) 1.1285 +{ 1.1286 + time_t tp[2]; 1.1287 + tp[0] = time (0); /* atime is now */ 1.1288 + tp[1] = mail_longdate (elt); /* modification time */ 1.1289 + utime (file,tp); /* set the times */ 1.1290 +}