📄 mbx.c
字号:
SEARCHSET *dst = au ? mail_newsearchset () : NIL; /* make sure valid mailbox */ if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock, au ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0) switch (errno) { case ENOENT: /* no such file? */ if (compare_cstring (mailbox,"INBOX")) { MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL); return NIL; } /* can create INBOX here */ mbx_create (dstream = stream ? stream : user_flags (&mbxproto),"INBOX"); if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock, au ? MBXISVALIDUID : MBXISVALIDNOUID)) >= 0) break; case EACCES: /* file protected */ sprintf (tmp,"Can't access destination: %.80s",mailbox); MM_LOG (tmp,ERROR); return NIL; case EINVAL: sprintf (tmp,"Invalid MBX-format mailbox name: %.80s",mailbox); MM_LOG (tmp,ERROR); return NIL; default: sprintf (tmp,"Not a MBX-format mailbox: %.80s",mailbox); MM_LOG (tmp,ERROR); return NIL; } /* get first message */ if (!MM_APPEND (af) (dstream,data,&flags,&date,&message)) close (fd); else if (!(df = fdopen (fd,"r+b"))) { MM_LOG ("Unable to reopen append mailbox",ERROR); close (fd); } else { MM_CRITICAL (dstream); /* go critical */ fstat (fd,&sbuf); /* get current file size */ fseek (df,sbuf.st_size,SEEK_SET); errno = 0; for (ret = LONGT; ret && message; ) { if (!SIZE (message)) { /* guard against zero-length */ MM_LOG ("Append of zero-length message",ERROR); ret = NIL; break; } f = mail_parse_flags (dstream,flags,&uf); if (date) { /* parse date if given */ if (!mail_parse_date (&elt,date)) { sprintf (tmp,"Bad date in append: %.80s",date); MM_LOG (tmp,ERROR); ret = NIL; /* mark failure */ break; } mail_date (tmp,&elt); /* write preseved date */ } else internal_date (tmp); /* get current date in IMAP format */ /* write header */ if (fprintf (df,"%s,%lu;%08lx%04lx-%08lx\015\012",tmp,i = SIZE (message), uf,(unsigned long) f,au ? ++dstream->uid_last : 0) < 0) ret = NIL; else { /* write message */ size_t j; if (!message->cursize) SETPOS (message,GETPOS (message)); while (i && (j = fwrite (message->curpos,1,message->cursize,df))) { i -= j; SETPOS (message,GETPOS (message) + j); } /* get next message */ if (i || !MM_APPEND (af) (dstream,data,&flags,&date,&message)) ret = NIL; else if (au) mail_append_set (dst,dstream->uid_last); } } /* if error... */ if (!ret || (fflush (df) == EOF)) { /* revert file */ ftruncate (fd,sbuf.st_size); close (fd); /* make sure fclose() doesn't corrupt us */ if (errno) { sprintf (tmp,"Message append failed: %s",strerror (errno)); MM_LOG (tmp,ERROR); } ret = NIL; } if (au && ret) { /* return sets if doing APPENDUID */ (*au) (mailbox,dstream->uid_validity,dst); fseek (df,15,SEEK_SET); /* update UIDLAST */ fprintf (df,"%08lx",dstream->uid_last); } else mail_free_searchset (&dst); /* set atime to now-1 if successful copy */ if (ret) tp[0] = time (0) - 1; /* else preserve \Marked status */ else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0); tp[1] = sbuf.st_mtime; /* preserve mtime */ utime (file,tp); /* set the times */ fclose (df); /* close the file */ MM_NOCRITICAL (dstream); /* release critical */ } unlockfd (ld,lock); /* release exclusive parse/append permission */ if (dstream != stream) mail_close (dstream); return ret;}/* Internal routines *//* MBX mail generate file string * Accepts: temporary buffer to write into * mailbox name string * Returns: local file string or NIL if failure */char *mbx_file (char *dst,char *name){ char *s = mailboxfile (dst,name); return (s && !*s) ? mailboxfile (dst,"~/INBOX") : s;}/* MBX mail parse mailbox * Accepts: MAIL stream * Returns: T if parse OK * NIL if failure, stream aborted */long mbx_parse (MAILSTREAM *stream){ struct stat sbuf; MESSAGECACHE *elt = NIL; unsigned char c,*s,*t,*x; char tmp[MAILTMPLEN]; unsigned long i,j,k,m; off_t curpos = LOCAL->filesize; unsigned long nmsgs = stream->nmsgs; unsigned long recent = stream->recent; unsigned long lastuid = 0; short dirty = NIL; short added = NIL; short silent = stream->silent; short uidwarn = T; fstat (LOCAL->fd,&sbuf); /* get status */ if (sbuf.st_size < curpos) { /* sanity check */ sprintf (tmp,"Mailbox shrank from %lu to %lu!", (unsigned long) curpos,(unsigned long) sbuf.st_size); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } lseek (LOCAL->fd,0,L_SET); /* rewind file */ /* read internal header */ read (LOCAL->fd,LOCAL->buf,HDRSIZE); LOCAL->buf[HDRSIZE] = '\0'; /* tie off header */ c = LOCAL->buf[15]; /* save first character of last UID */ LOCAL->buf[15] = '\0'; /* parse UID validity */ stream->uid_validity = strtoul (LOCAL->buf + 7,NIL,16); LOCAL->buf[15] = c; /* restore first character of last UID */ /* parse last UID */ i = strtoul (LOCAL->buf + 15,NIL,16); stream->uid_last = stream->rdonly ? max (i,stream->uid_last) : i; /* parse user flags */ for (i = 0, s = LOCAL->buf + 25; (i < NUSERFLAGS) && (t = strchr (s,'\015')) && (t - s); i++, s = t + 2) { *t = '\0'; /* tie off flag */ if (!stream->user_flags[i] && (strlen (s) <= MAXUSERFLAG)) stream->user_flags[i] = cpystr (s); } LOCAL->ffuserflag = (int) i; /* first free user flag */ /* get current last flag updater PID */ i = (isxdigit (LOCAL->buf[HDRSIZE-10]) && isxdigit (LOCAL->buf[HDRSIZE-9]) && isxdigit (LOCAL->buf[HDRSIZE-8]) && isxdigit (LOCAL->buf[HDRSIZE-7]) && isxdigit (LOCAL->buf[HDRSIZE-6]) && isxdigit (LOCAL->buf[HDRSIZE-5]) && isxdigit (LOCAL->buf[HDRSIZE-4]) && isxdigit (LOCAL->buf[HDRSIZE-3]) && (LOCAL->buf[HDRSIZE-2] == '\015') && (LOCAL->buf[HDRSIZE-1] == '\012'))? strtoul (LOCAL->buf + HDRSIZE - 8,NIL,16) : 0; /* set flagcheck if lastpid changed */ if (LOCAL->lastpid && (LOCAL->lastpid != i)) LOCAL->flagcheck = T; LOCAL->lastpid = i; /* set as last PID */ stream->silent = T; /* don't pass up exists events yet */ while (sbuf.st_size - curpos){/* while there is stuff to parse */ /* get to that position in the file */ lseek (LOCAL->fd,curpos,L_SET); if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) { sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s", (unsigned long) curpos,(unsigned long) sbuf.st_size, i ? strerror (errno) : "no data read"); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } LOCAL->buf[i] = '\0'; /* tie off buffer just in case */ if (!((s = strchr (LOCAL->buf,'\015')) && (s[1] == '\012'))) { sprintf (tmp,"Unable to find CRLF at %lu in %lu bytes, text: %.80s", (unsigned long) curpos,i,(char *) LOCAL->buf); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } *s = '\0'; /* tie off header line */ i = (s + 2) - LOCAL->buf; /* note start of text offset */ if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) { sprintf (tmp,"Unable to parse internal header at %lu: %.80s", (unsigned long) curpos,(char *) LOCAL->buf); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } if (!(isxdigit (t[1]) && isxdigit (t[2]) && isxdigit (t[3]) && isxdigit (t[4]) && isxdigit (t[5]) && isxdigit (t[6]) && isxdigit (t[7]) && isxdigit (t[8]) && isxdigit (t[9]) && isxdigit (t[10]) && isxdigit (t[11]) && isxdigit (t[12]))) { sprintf (tmp,"Unable to parse message flags at %lu: %.80s", (unsigned long) curpos,(char *) LOCAL->buf); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } if ((t[13] != '-') || t[22] || !(isxdigit (t[14]) && isxdigit (t[15]) && isxdigit (t[16]) && isxdigit (t[17]) && isxdigit (t[18]) && isxdigit (t[19]) && isxdigit (t[20]) && isxdigit (t[21]))) { sprintf (tmp,"Unable to parse message UID at %lu: %.80s", (unsigned long) curpos,(char *) LOCAL->buf); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } *s++ = '\0'; *t++ = '\0'; /* break up fields */ /* get message size */ if (!(j = strtoul (s,(char **) &x,10)) && (!(x && *x))) { sprintf (tmp,"Unable to parse message size at %lu: %.80s,%.80s;%.80s", (unsigned long) curpos,(char *) LOCAL->buf,(char *) s, (char *) t); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } /* make sure didn't run off end of file */ if (((off_t) (curpos + i + j)) > sbuf.st_size) { sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)", (unsigned long) curpos,(unsigned long) (curpos + i + j), (unsigned long) sbuf.st_size); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } /* parse UID */ if ((m = strtoul (t+13,NIL,16)) && ((m <= lastuid) || (m > stream->uid_last))) { if (uidwarn) { sprintf (tmp,"Invalid UID %08lx in message %lu, rebuilding UIDs", m,nmsgs+1); MM_LOG (tmp,WARN); uidwarn = NIL; /* restart UID validity */ stream->uid_validity = time (0); } m = 0; /* lose this UID */ dirty = T; /* mark dirty, set new lastuid */ stream->uid_last = lastuid; } t[12] = '\0'; /* parse system flags */ if ((k = strtoul (t+8,NIL,16)) & fEXPUNGED) { if (m) lastuid = m; /* expunge message, update last UID seen */ else { /* no UID assigned? */ lastuid = ++stream->uid_last; dirty = T; } } else { /* not expunged, swell the cache */ added = T; /* note that a new message was added */ mail_exists (stream,++nmsgs); /* instantiate an elt for this message */ (elt = mail_elt (stream,nmsgs))->valid = T; /* parse the date */ if (!mail_parse_date (elt,LOCAL->buf)) { sprintf (tmp,"Unable to parse message date at %lu: %.80s", (unsigned long) curpos,(char *) LOCAL->buf); MM_LOG (tmp,ERROR); mbx_abort (stream); return NIL; } /* note file offset of header */ elt->private.special.offset = curpos; /* and internal header size */ elt->private.special.text.size = i; /* header size not known yet */ elt->private.msg.header.text.size = 0; elt->rfc822_size = j; /* note message size */ /* calculate system flags */ if (k & fSEEN) elt->seen = T; if (k & fDELETED) elt->deleted = T; if (k & fFLAGGED) elt->flagged = T; if (k & fANSWERED) elt->answered = T; if (k & fDRAFT) elt->draft = T; t[8] = '\0'; /* get user flags value */ elt->user_flags = strtoul (t,NIL,16); /* UID already assigned? */ if (!(elt->private.uid = m) || !(k & fOLD)) { elt->recent = T; /* no, mark as recent */ ++recent; /* count up a new recent message */ dirty = T; /* and must rewrite header */ /* assign new UID */ if (!elt->private.uid) elt->private.uid = ++stream->uid_last; mbx_update_status (stream,elt->msgno,NIL); } /* update last parsed UID */ lastuid = elt->private.uid; } curpos += i + j; /* update position */ } if (dirty && !stream->rdonly){/* update header */ mbx_update_header (stream); fsync (LOCAL->fd); /* make sure all the UID updates take */ } /* update parsed file size and time */ LOCAL->filesize = sbuf.st_size; fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */ LOCAL->filetime = sbuf.st_mtime; if (added && !stream->rdonly){/* make sure atime updated */ time_t tp[2]; tp[0] = time (0); tp[1] = LOCAL->filetime; utime (stream->mailbox,tp); } stream->silent = silent; /* can pass up events now */ mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */ mail_recent (stream,recent); /* and of change in recent messages */ return LONGT; /* return the winnage */}/* MBX get cache element with status updating from file * Accepts: MAIL stream * message number * expunge OK flag * Returns: cache element */MESSAGECACHE *mbx_elt (MAILSTREAM *stream,unsigned long msgno,long expok){ MESSAGECACHE *elt = mail_elt (stream,msgno); struct { /* old flags */ unsigned int seen : 1; unsigned int deleted : 1; unsigned int flagged : 1; unsigned int answered : 1; unsigned int draft : 1; unsigned long user_flags; } old; old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged; old.answered = elt->answered; old.draft = elt->draft; old.user_flags = elt->user_flags; /* get new flags */ if (mbx_read_flags (stream,elt) && expok) { mail_expunged (stream,elt->msgno); return NIL; /* return this message was expunged */ } if ((old.seen != elt->seen) || (old.deleted != elt->deleted) || (old.flagged != elt->flagged) || (old.answered != elt->answered) || (old.draft != elt->draft) || (old.user_flags != elt->user_flags)) MM_FLAGS (stream,msgno); /* let top level know */ return elt;}/* MBX read flags from file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -