unixnt.c
上传用户:ycwykj01
上传日期:2007-01-04
资源大小:1819k
文件大小:56k
- /*
- * Program: UNIX mail routines
- *
- * Author: Mark Crispin
- * Networks and Distributed Computing
- * Computing & Communications
- * University of Washington
- * Administration Building, AG-44
- * Seattle, WA 98195
- * Internet: MRC@CAC.Washington.EDU
- *
- * Date: 20 December 1989
- * Last Edited: 22 September 1999
- *
- * Copyright 1999 by the University of Washington
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appears in all copies and that both the
- * above copyright notice and this permission notice appear in supporting
- * documentation, and that the name of the University of Washington not be
- * used in advertising or publicity pertaining to distribution of the software
- * without specific, written prior permission. This software is made
- * available "as is", and
- * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
- * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
- * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
- * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
- * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
- * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- */
- /* DEDICATION
- *
- * This file is dedicated to my dog, Unix, also known as Yun-chan and
- * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix
- * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
- * a two-month bout with cirrhosis of the liver.
- *
- * He was a dear friend, and I miss him terribly.
- *
- * Lift a leg, Yunie. Luv ya forever!!!!
- */
- #include <stdio.h>
- #include <ctype.h>
- #include <errno.h>
- extern int errno; /* just in case */
- #include "mail.h"
- #include "osdep.h"
- #include <time.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/utime.h>
- #include "unixnt.h"
- #include "pseudo.h"
- #include "fdstring.h"
- #include "misc.h"
- #include "dummy.h"
- /* UNIX mail routines */
- /* Driver dispatch used by MAIL */
- DRIVER unixdriver = {
- "unix", /* driver name */
- DR_LOCAL|DR_MAIL, /* driver flags */
- (DRIVER *) NIL, /* next driver */
- unix_valid, /* mailbox is valid for us */
- unix_parameters, /* manipulate parameters */
- unix_scan, /* scan mailboxes */
- unix_list, /* list mailboxes */
- unix_lsub, /* list subscribed mailboxes */
- NIL, /* subscribe to mailbox */
- NIL, /* unsubscribe from mailbox */
- unix_create, /* create mailbox */
- unix_delete, /* delete mailbox */
- unix_rename, /* rename mailbox */
- NIL, /* status of mailbox */
- unix_open, /* open mailbox */
- unix_close, /* close mailbox */
- NIL, /* fetch message "fast" attributes */
- NIL, /* fetch message flags */
- NIL, /* fetch overview */
- NIL, /* fetch message envelopes */
- unix_header, /* fetch message header */
- unix_text, /* fetch message text */
- NIL, /* fetch partial message text */
- NIL, /* unique identifier */
- NIL, /* message number */
- NIL, /* modify flags */
- unix_flagmsg, /* per-message modify flags */
- NIL, /* search for message based on criteria */
- NIL, /* sort messages */
- NIL, /* thread messages */
- unix_ping, /* ping mailbox to see if still alive */
- unix_check, /* check for new messages */
- unix_expunge, /* expunge deleted messages */
- unix_copy, /* copy messages to another mailbox */
- unix_append, /* append string message to mailbox */
- NIL /* garbage collect stream */
- };
- /* prototype stream */
- MAILSTREAM unixproto = {&unixdriver};
- /* driver parameters */
- static long unix_fromwidget = T;
- /* UNIX mail validate mailbox
- * Accepts: mailbox name
- * Returns: our driver if name is valid, NIL otherwise
- */
- DRIVER *unix_valid (char *name)
- {
- int fd;
- DRIVER *ret = NIL;
- int c,r;
- char tmp[MAILTMPLEN],file[MAILTMPLEN],*s,*t;
- struct stat sbuf;
- struct utimbuf times;
- errno = EINVAL; /* assume invalid argument */
- /* must be non-empty file */
- if ((t = dummy_file (file,name)) && !stat (t,&sbuf) &&
- ((sbuf.st_mode & S_IFMT) == S_IFREG)) {
- if (!sbuf.st_size)errno = 0;/* empty file */
- else if ((fd = open (file,O_BINARY|O_RDONLY,NIL)) >= 0) {
- memset (tmp,' ',MAILTMPLEN);
- if (read (fd,tmp,MAILTMPLEN-1) <= 0) errno = -1;
- else { /* ignore leading whitespace */
- for (s = tmp,c = ' 12';
- (*s == ' 15') || (*s == ' 12') || (*s == ' ') || (*s == 't');
- c = *s++);
- if (c == ' 12') { /* at start of a line? */
- VALID (s,t,r,c); /* yes, validate format */
- if (r) ret = &unixdriver;
- else errno = -1; /* invalid format */
- }
- }
- close (fd); /* close the file */
- /* preserve atime and mtime */
- times.actime = sbuf.st_atime;
- times.modtime = sbuf.st_mtime;
- utime (file,×); /* set the times */
- }
- }
- /* in case INBOX but not unix format */
- else if ((errno == ENOENT) && ((name[0] == 'I') || (name[0] == 'i')) &&
- ((name[1] == 'N') || (name[1] == 'n')) &&
- ((name[2] == 'B') || (name[2] == 'b')) &&
- ((name[3] == 'O') || (name[3] == 'o')) &&
- ((name[4] == 'X') || (name[4] == 'x')) && !name[5]) errno = -1;
- return ret; /* return what we should */
- }
- /* UNIX manipulate driver parameters
- * Accepts: function code
- * function-dependent value
- * Returns: function-dependent return value
- */
- void *unix_parameters (long function,void *value)
- {
- switch ((int) function) {
- case SET_FROMWIDGET:
- unix_fromwidget = (long) value;
- break;
- case GET_FROMWIDGET:
- value = (void *) unix_fromwidget;
- break;
- default:
- value = NIL; /* error case */
- break;
- }
- return value;
- }
- /* UNIX mail scan mailboxes
- * Accepts: mail stream
- * reference
- * pattern to search
- * string to scan
- */
- void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
- {
- if (stream) dummy_scan (NIL,ref,pat,contents);
- }
- /* UNIX mail list mailboxes
- * Accepts: mail stream
- * reference
- * pattern to search
- */
- void unix_list (MAILSTREAM *stream,char *ref,char *pat)
- {
- if (stream) dummy_list (NIL,ref,pat);
- }
- /* UNIX mail list subscribed mailboxes
- * Accepts: mail stream
- * reference
- * pattern to search
- */
- void unix_lsub (MAILSTREAM *stream,char *ref,char *pat)
- {
- if (stream) dummy_lsub (NIL,ref,pat);
- }
- /* UNIX mail create mailbox
- * Accepts: MAIL stream
- * mailbox name to create
- * Returns: T on success, NIL on failure
- */
- long unix_create (MAILSTREAM *stream,char *mailbox)
- {
- char *s,mbx[MAILTMPLEN],tmp[MAILTMPLEN];
- long ret = NIL;
- int fd;
- time_t ti = time (0);
- if (!(s = dummy_file (mbx,mailbox))) {
- sprintf (tmp,"Can't create %.80s: invalid name",mailbox);
- mm_log (tmp,ERROR);
- }
- /* create underlying file */
- else if (dummy_create_path (stream,s)) {
- /* done if made directory */
- if ((s = strrchr (s,'\')) && !s[1]) ret = T;
- else if ((fd = open (mbx,O_WRONLY,
- (int) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) {
- sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
- mm_log (tmp,ERROR);
- unlink (mbx); /* delete the file */
- }
- else { /* initialize header */
- memset (tmp,' ',MAILTMPLEN);
- sprintf (tmp,"From %s %sDate: ",pseudo_from,ctime (&ti));
- rfc822_date (s = tmp + strlen (tmp));
- sprintf (s += strlen (s), /* write the pseudo-header */
- "