📄 mh.c
字号:
/* MH mail unsubscribe to mailbox * Accepts: mail stream * mailbox to delete from subscription list * Returns: T on success, NIL on failure */long mh_unsubscribe (MAILSTREAM *stream,char *mailbox){ return sm_unsubscribe (mailbox);}/* MH mail create mailbox * Accepts: mail stream * mailbox name to create * Returns: T on success, NIL on failure */long mh_create (MAILSTREAM *stream,char *mailbox){ char *s,tmp[MAILTMPLEN]; /* assume error */ sprintf (tmp,"Can't create mailbox %.80s: invalid MH-format name",mailbox); if (mailbox[0] == '#' && (mailbox[1] == 'm' || mailbox[1] == 'M') && (mailbox[2] == 'h' || mailbox[2] == 'H') && mailbox[3] == '/') /* make sure valid name */ for (s = mailbox + 4; s && *s;) { if (isdigit (*s)) s++; /* digit, check this node further... */ /* all digit node, barf */ else if (*s == '/') s = NIL; /* non-digit in node, skip to next node */ else if (s = strchr (s+1,'/')) s++; else tmp[0] = NIL; /* no more nodes, good name */ } if (tmp[0]) { /* was there an error in the name? */ mm_log (tmp,ERROR); /* yes, log it */ return NIL; } /* must not already exist */ if (mh_isvalid (mailbox,tmp,NIL)) { sprintf (tmp,"Can't create mailbox %.80s: mailbox already exists",mailbox); mm_log (tmp,ERROR); return NIL; } if (!mh_path) return NIL; /* sorry */ if (!(mh_file (tmp,mailbox) &&/* try to make it */ dummy_create_path (stream,strcat (tmp,"/"), get_dir_protection (mailbox)))) { sprintf (tmp,"Can't create mailbox %.80s: %s",mailbox,strerror (errno)); mm_log (tmp,ERROR); return NIL; } return T; /* return success */}/* MH mail delete mailbox * mailbox name to delete * Returns: T on success, NIL on failure */long mh_delete (MAILSTREAM *stream,char *mailbox){ DIR *dirp; struct direct *d; int i; char tmp[MAILTMPLEN]; if (!(mailbox[0] == '#' && (mailbox[1] == 'm' || mailbox[1] == 'M') && (mailbox[2] == 'h' || mailbox[2] == 'H') && mailbox[3] == '/')) { sprintf (tmp,"Can't delete mailbox %.80s: invalid MH-format name",mailbox); mm_log (tmp,ERROR); return NIL; } /* is mailbox valid? */ if (!mh_isvalid (mailbox,tmp,NIL)){ sprintf (tmp,"Can't delete mailbox %.80s: no such mailbox",mailbox); mm_log (tmp,ERROR); return NIL; } /* get name of directory */ i = strlen (mh_file (tmp,mailbox)); if (dirp = opendir (tmp)) { /* open directory */ tmp[i++] = '/'; /* now apply trailing delimiter */ while (d = readdir (dirp)) /* massacre all numeric or comma files */ if (mh_select (d) || (*d->d_name == ',') || !strcmp (d->d_name,MHSEQUENCE)) { strcpy (tmp + i,d->d_name); unlink (tmp); /* sayonara */ } closedir (dirp); /* flush directory */ } /* try to remove the directory */ if (rmdir (mh_file (tmp,mailbox))) { sprintf (tmp,"Can't delete mailbox %.80s: %s",mailbox,strerror (errno)); mm_log (tmp,WARN); } return T; /* return success */}/* MH mail rename mailbox * Accepts: MH mail stream * old mailbox name * new mailbox name * Returns: T on success, NIL on failure */long mh_rename (MAILSTREAM *stream,char *old,char *newname){ char c,*s,tmp[MAILTMPLEN],tmp1[MAILTMPLEN]; struct stat sbuf; if (!(old[0] == '#' && (old[1] == 'm' || old[1] == 'M') && (old[2] == 'h' || old[2] == 'H') && old[3] == '/')) sprintf (tmp,"Can't delete mailbox %.80s: invalid MH-format name",old); /* old mailbox name must be valid */ else if (!mh_isvalid (old,tmp,NIL)) sprintf (tmp,"Can't rename mailbox %.80s: no such mailbox",old); else if (!(newname[0] == '#' && (newname[1] == 'm' || newname[1] == 'M') && (newname[2] == 'h' || newname[2] == 'H') && newname[3] == '/')) sprintf (tmp,"Can't rename to mailbox %.80s: invalid MH-format name", newname); /* new mailbox name must not be valid */ else if (mh_isvalid (newname,tmp,NIL)) sprintf (tmp,"Can't rename to mailbox %.80s: destination already exists", newname); /* success if can rename the directory */ else { /* found superior to destination name? */ if (s = strrchr (mh_file (tmp1,newname),'/')) { c = *++s; /* remember first character of inferior */ *s = '\0'; /* tie off to get just superior */ /* name doesn't exist, create it */ if ((stat (tmp1,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) && !dummy_create_path (stream,tmp1,get_dir_protection (newname))) return NIL; *s = c; /* restore full name */ } if (!rename (mh_file (tmp,old),tmp1)) return T; sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s", old,newname,strerror (errno)); } mm_log (tmp,ERROR); /* something failed */ return NIL;}/* MH mail open * Accepts: stream to open * Returns: stream on success, NIL on failure */MAILSTREAM *mh_open (MAILSTREAM *stream){ char tmp[MAILTMPLEN]; if (!stream) return &mhproto; /* return prototype for OP_PROTOTYPE call */ if (stream->local) fatal ("mh recycle stream"); stream->local = fs_get (sizeof (MHLOCAL)); /* note if an INBOX or not */ stream->inbox = !compare_cstring (stream->mailbox,"#MHINBOX"); mh_file (tmp,stream->mailbox);/* get directory name */ LOCAL->dir = cpystr (tmp); /* copy directory name for later */ /* make temporary buffer */ LOCAL->buf = (char *) fs_get ((LOCAL->buflen = MAXMESSAGESIZE) + 1); LOCAL->scantime = 0; /* not scanned yet */ LOCAL->cachedtexts = 0; /* no cached texts */ stream->sequence++; /* bump sequence number */ /* parse mailbox */ stream->nmsgs = stream->recent = 0; if (!mh_ping (stream)) return NIL; if (!(stream->nmsgs || stream->silent)) mm_log ("Mailbox is empty",(long) NIL); return stream; /* return stream to caller */}/* MH mail close * Accepts: MAIL stream * close options */void mh_close (MAILSTREAM *stream,long options){ if (LOCAL) { /* only if a file is open */ int silent = stream->silent; stream->silent = T; /* note this stream is dying */ if (options & CL_EXPUNGE) mh_expunge (stream); if (LOCAL->dir) fs_give ((void **) &LOCAL->dir); /* free local scratch buffer */ if (LOCAL->buf) fs_give ((void **) &LOCAL->buf); /* nuke the local data */ fs_give ((void **) &stream->local); stream->dtb = NIL; /* log out the DTB */ stream->silent = silent; /* reset silent state */ }}/* MH mail fetch fast information * Accepts: MAIL stream * sequence * option flags */void mh_fast (MAILSTREAM *stream,char *sequence,long flags){ unsigned long i,j; /* ugly and slow */ if (stream && LOCAL && ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence))) for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->sequence) mh_header (stream,i,&j,NIL);}/* MH mail fetch message header * Accepts: MAIL stream * message # to fetch * pointer to returned header text length * option flags * Returns: message header in RFC822 format */char *mh_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length, long flags){ unsigned long i,hdrsize; int fd; unsigned char *t; struct stat sbuf; struct tm *tm; MESSAGECACHE *elt; *length = 0; /* default to empty */ if (flags & FT_UID) return "";/* UID call "impossible" */ elt = mail_elt (stream,msgno);/* get elt */ if (!elt->private.msg.header.text.data) { /* purge cache if too big */ if (LOCAL->cachedtexts > max (stream->nmsgs * 4096,2097152)) { mail_gc (stream,GC_TEXTS);/* just can't keep that much */ LOCAL->cachedtexts = 0; } /* build message file name */ sprintf (LOCAL->buf,"%s/%lu",LOCAL->dir,elt->private.uid); if ((fd = open (LOCAL->buf,O_RDONLY,NIL)) < 0) return ""; fstat (fd,&sbuf); /* get size of message */ /* make plausible IMAPish date string */ tm = gmtime (&sbuf.st_mtime); elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1; elt->year = tm->tm_year + 1900 - BASEYEAR; elt->hours = tm->tm_hour; elt->minutes = tm->tm_min; elt->seconds = tm->tm_sec; elt->zhours = 0; elt->zminutes = 0; /* is buffer big enough? */ if (sbuf.st_size > LOCAL->buflen) { fs_give ((void **) &LOCAL->buf); LOCAL->buf = (char *) fs_get ((LOCAL->buflen = sbuf.st_size) + 1); } /* slurp message */ read (fd,LOCAL->buf,sbuf.st_size); /* tie off file */ LOCAL->buf[sbuf.st_size] = '\0'; close (fd); /* flush message file */ /* find end of header */ for (i = 0,t = LOCAL->buf; *t && !(i && (*t == '\n')); i = (*t++ == '\n')); /* number of header bytes */ hdrsize = (*t ? ++t : t) - LOCAL->buf; elt->rfc822_size = /* size of entire message in CRLF form */ (elt->private.msg.header.text.size = strcrlfcpy (&elt->private.msg.header.text.data,&i,LOCAL->buf, hdrsize)) + (elt->private.msg.text.text.size = strcrlfcpy (&elt->private.msg.text.text.data,&i,t, sbuf.st_size - hdrsize)); /* add to cached size */ LOCAL->cachedtexts += elt->rfc822_size; } *length = elt->private.msg.header.text.size; return (char *) elt->private.msg.header.text.data;}/* MH mail fetch message text (body only) * Accepts: MAIL stream * message # to fetch * pointer to returned stringstruct * option flags * Returns: T on success, NIL on failure */long mh_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags){ unsigned long i; MESSAGECACHE *elt; /* UID call "impossible" */ if (flags & FT_UID) return NIL; elt = mail_elt (stream,msgno);/* get elt */ /* snarf message if don't have it yet */ if (!elt->private.msg.text.text.data) { mh_header (stream,msgno,&i,flags); if (!elt->private.msg.text.text.data) return NIL; } if (!(flags & FT_PEEK)) { /* mark as seen */ mail_elt (stream,msgno)->seen = T; mm_flags (stream,msgno); } if (!elt->private.msg.text.text.data) return NIL; INIT (bs,mail_string,elt->private.msg.text.text.data, elt->private.msg.text.text.size); return T;}/* MH mail ping mailbox * Accepts: MAIL stream * Returns: T if stream alive, else NIL */long mh_ping (MAILSTREAM *stream){ MAILSTREAM *sysibx = NIL; MESSAGECACHE *elt,*selt; struct stat sbuf; char *s,tmp[MAILTMPLEN]; int fd; unsigned long i,j,r,old; long nmsgs = stream->nmsgs; long recent = stream->recent; int silent = stream->silent; if (stat (LOCAL->dir,&sbuf)) { /* directory exists? */ if (stream->inbox) return T; sprintf (tmp,"Can't open mailbox %.80s: no such mailbox",stream->mailbox); mm_log (tmp,ERROR); return NIL; } stream->silent = T; /* don't pass up mm_exists() events yet */ if (sbuf.st_ctime != LOCAL->scantime) { struct direct **names = NIL; long nfiles = scandir (LOCAL->dir,&names,mh_select,mh_numsort); if (nfiles < 0) nfiles = 0; /* in case error */ old = stream->uid_last; /* note scanned now */ LOCAL->scantime = sbuf.st_ctime; /* scan directory */ for (i = 0; i < nfiles; ++i) { /* if newly seen, add to list */ if ((j = atoi (names[i]->d_name)) > old) { mail_exists (stream,++nmsgs); stream->uid_last = (elt = mail_elt (stream,nmsgs))->private.uid = j; elt->valid = T; /* note valid flags */ if (old) { /* other than the first pass? */ elt->recent = T; /* yup, mark as recent */ recent++; /* bump recent count */ } else { /* see if already read */ sprintf (tmp,"%s/%s",LOCAL->dir,names[i]->d_name); stat (tmp,&sbuf); /* get inode poop */ if (sbuf.st_atime > sbuf.st_mtime) elt->seen = T; } } fs_give ((void **) &names[i]); } /* free directory */ if (s = (void *) names) fs_give ((void **) &s); } /* if INBOX, snarf from system INBOX */ if (stream->inbox && strcmp (sysinbox (),stream->mailbox)) { old = stream->uid_last;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -