📄 unixnt.c
字号:
/* * 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: 1 September 2000 * * Copyright 2000 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>#define DOTLOCK char#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,'\0',MAILTMPLEN); if (read (fd,tmp,MAILTMPLEN-1) <= 0) errno = -1; else { /* ignore leading whitespace */ for (s = tmp,c = '\n'; (*s == '\r') || (*s == '\n') || (*s == ' ') || (*s == '\t'); c = *s++); if (c == '\n') { /* 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){ void *ret = NIL; switch ((int) function) { case SET_FROMWIDGET: unix_fromwidget = (long) value; case GET_FROMWIDGET: ret = (void *) unix_fromwidget; break; } return ret;}/* 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], ctmp[MAILTMPLEN]; long ret = NIL; int fd, ctmplen; 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]) return T; if ((fd = open (mbx,O_WRONLY|O_BINARY, (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,'\0',MAILTMPLEN); strcpy(ctmp, ctime(&ti)); ctmplen = strlen(ctmp); if(ctmp[ctmplen-2] == '\r') ctmp[ctmplen-2] = '\0'; else if(ctmp[ctmplen-1] == '\n') ctmp[ctmplen-1] = '\0'; sprintf (tmp,"From %s %s\r\nDate: ",pseudo_from,ctmp); rfc822_fixed_date (s = tmp + strlen (tmp)); sprintf (s += strlen (s), /* write the pseudo-header */ "\r\nFrom: %s <%s@%s>\r\nSubject: %s\r\nX-IMAP: %010lu 0000000000\r\nStatus: RO\r\n\r\n%s\r\n\r\n", pseudo_name,pseudo_from,mylocalhost (),pseudo_subject, (unsigned long) ti,pseudo_msg); if ((write (fd,tmp,strlen (tmp)) < 0) || close (fd)) { sprintf (tmp,"Can't initialize mailbox node %.80s: %s",mbx, strerror (errno)); mm_log (tmp,ERROR); unlink (mbx); /* delete the file */ } else ret = T; /* success */ } close (fd); /* close file */ } return ret;}/* UNIX mail delete mailbox * Accepts: MAIL stream * mailbox name to delete * Returns: T on success, NIL on failure */long unix_delete (MAILSTREAM *stream,char *mailbox){ return unix_rename (stream,mailbox,NIL);}/* UNIX mail rename mailbox * Accepts: MAIL stream * old mailbox name * new mailbox name (or NIL for delete) * Returns: T on success, NIL on failure */long unix_rename (MAILSTREAM *stream,char *old,char *newname){ long ret = NIL; char c,*s = NIL; char tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN],lockx[MAILTMPLEN]; int fd,ld; struct stat sbuf; mm_critical (stream); /* get the c-client lock */ if (newname && (!((s = dummy_file (tmp,newname)) && *s) || ((s = strrchr (s,'\\')) && !s[1]))) sprintf (tmp,"Can't rename mailbox %.80s to %.80s: invalid name", old,newname); else if ((ld = lockname (lock,dummy_file (file,old),NIL)) < 0) sprintf (tmp,"Can't get lock for mailbox %.80s",old); else { /* lock out other c-clients */ if (flock (ld,LOCK_EX|LOCK_NB)) { close (ld); /* couldn't lock, give up on it then */ sprintf (tmp,"Mailbox %.80s is in use by another process",old); } /* lock out non c-client applications */ else if ((fd = unix_lock (file,O_BINARY|O_RDWR,S_IREAD|S_IWRITE,lockx, LOCK_EX)) < 0) sprintf (tmp,"Can't lock mailbox %.80s: %s",old,strerror (errno)); else { unix_unlock(fd,NIL,lockx);/* pacify evil NTFS */ if (newname) { /* want rename? */ /* found superior to destination name? */ if (s && (s != tmp) && ((tmp[1] != ':') || (s != tmp + 2))) { c = s[1]; /* remember character after delimiter */ *s = s[1] = '\0'; /* tie off name at delimiter */ /* name doesn't exist, create it */ if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) { *s = '\\'; /* restore delimiter */ if (!dummy_create (stream,newname)) { flock (ld,LOCK_UN); close (ld); /* close c-client lock */ unlink (lock); /* and delete it */ mm_nocritical (stream); return NIL; /* couldn't create superior */ } } else *s = '\\'; /* restore delimiter */ s[1] = c; /* restore character after delimiter */ } if (rename (file,tmp)) sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname, strerror (errno)); else ret = T; /* set success */ } else if (unlink (file)) /* want delete */ sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno)); else ret = T; /* set success */ flock (ld,LOCK_UN); /* release c-client lock */ close (ld); /* close c-client lock */ unlink (lock); /* and delete it */ } } mm_nocritical (stream); /* no longer critical */ if (!ret) mm_log (tmp,ERROR); /* log error */ return ret; /* return success or failure */}/* UNIX mail open * Accepts: Stream to open * Returns: Stream on success, NIL on failure */MAILSTREAM *unix_open (MAILSTREAM *stream){ int fd; char tmp[MAILTMPLEN]; /* return prototype for OP_PROTOTYPE call */ if (!stream) return &unixproto; if (stream->local) fatal ("unix recycle stream"); stream->local = memset (fs_get (sizeof (UNIXLOCAL)),0,sizeof (UNIXLOCAL)); /* note if an INBOX or not */ stream->inbox = !strcmp (ucase (strcpy (tmp,stream->mailbox)),"INBOX"); /* canonicalize the stream mailbox name */ dummy_file (tmp,stream->mailbox); /* flush old name */ fs_give ((void **) &stream->mailbox); /* save canonical name */ stream->mailbox = cpystr (tmp); LOCAL->fd = LOCAL->ld = -1; /* no file or state locking yet */ LOCAL->buf = (char *) fs_get ((LOCAL->buflen = CHUNK) + 1); stream->sequence++; /* bump sequence number */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -