📄 mbxnt.c
字号:
* Returns: T if success, NIL if failed */long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options){ struct stat sbuf; struct utimbuf times; MESSAGECACHE *elt; unsigned long i,j,k,m; long ret = LONGT; int fd,ld; char *s,*t,file[MAILTMPLEN],lock[MAILTMPLEN]; mailproxycopy_t pc = (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL); copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL); SEARCHSET *source = cu ? mail_newsearchset () : NIL; SEARCHSET *dest = cu ? mail_newsearchset () : NIL; MAILSTREAM *dstream = NIL; if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence))) return NIL; /* make sure valid mailbox */ if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock, cu ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0) switch (errno) { case ENOENT: /* no such file? */ mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL); return NIL; case EACCES: /* file protected */ sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox); MM_LOG (LOCAL->buf,ERROR); return NIL; case EINVAL: if (pc) return (*pc) (stream,sequence,mailbox,options); sprintf (LOCAL->buf,"Invalid MBX-format mailbox name: %.80s",mailbox); mm_log (LOCAL->buf,ERROR); return NIL; default: if (pc) return (*pc) (stream,sequence,mailbox,options); sprintf (LOCAL->buf,"Not a MBX-format mailbox: %.80s",mailbox); mm_log (LOCAL->buf,ERROR); return NIL; } /* got file? */ if ((fd = open (dummy_file (file,mailbox),O_RDWR|O_CREAT|O_BINARY, S_IREAD|S_IWRITE)) < 0) { sprintf (LOCAL->buf,"Unable to open copy mailbox: %s",strerror (errno)); mm_log (LOCAL->buf,ERROR); return NIL; } mm_critical (stream); /* go critical */ fstat (fd,&sbuf); /* get current file size */ lseek (fd,sbuf.st_size,L_SET);/* move to end of file */ /* for each requested message */ for (i = 1; ret && (i <= stream->nmsgs); i++) if ((elt = mail_elt (stream,i))->sequence) { lseek (LOCAL->fd,elt->private.special.offset + elt->private.special.text.size,L_SET); mail_date(LOCAL->buf,elt);/* build target header */ /* get target keyword mask */ for (j = elt->user_flags, k = 0; j; ) if (s = stream->user_flags[find_rightmost_bit (&j)]) for (m = 0; (m < NUSERFLAGS) && (t = dstream->user_flags[m]); m++) if (!compare_cstring (s,t) && (k |= 1 << m)) break; sprintf (LOCAL->buf+strlen(LOCAL->buf),",%lu;%08lx%04x-%08lx\015\012", elt->rfc822_size,k,(unsigned) ((fSEEN * elt->seen) + (fDELETED * elt->deleted) + (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) + (fDRAFT * elt->draft)),cu ? ++dstream->uid_last : 0); /* write target header */ if (ret = (write (fd,LOCAL->buf,strlen (LOCAL->buf)) > 0)) { for (k = elt->rfc822_size; ret && (j = min (k,LOCAL->buflen)); k -= j){ read (LOCAL->fd,LOCAL->buf,j); ret = write (fd,LOCAL->buf,j) >= 0; } if (cu) { /* need to pass back new UID? */ mail_append_set (source,mail_uid (stream,i)); mail_append_set (dest,dstream->uid_last); } } } /* make sure all the updates take */ if (!(ret && (ret = !fsync (fd)))) { sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno)); mm_log (LOCAL->buf,ERROR); ftruncate (fd,sbuf.st_size); } if (cu && ret) { /* return sets if doing COPYUID */ (*cu) (stream,mailbox,dstream->uid_validity,source,dest); lseek (fd,15,L_SET); /* update UIDLAST */ sprintf (LOCAL->buf,"%08lx",dstream->uid_last); write (fd,LOCAL->buf,8); } else { /* flush any sets we may have built */ mail_free_searchset (&source); mail_free_searchset (&dest); } /* set atime to now-1 if successful copy */ if (ret) times.actime = time (0) - 1; /* else preserved \Marked status */ else times.actime = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time (0); times.modtime = sbuf.st_mtime;/* preserve mtime */ utime (file,×); /* set the times */ close (fd); /* close the file */ mm_nocritical (stream); /* release critical */ unlockfd (ld,lock); /* release exclusive parse/append permission */ /* delete all requested messages */ if (ret && (options & CP_MOVE) && mbx_flaglock (stream)) { for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->sequence) { /* mark message deleted */ mbx_elt (stream,i,NIL)->deleted = T; /* recalculate status */ mbx_update_status (stream,i,NIL); } /* update flags */ mbx_flag (stream,NIL,NIL,NIL); } if (dstream != stream) mail_close (dstream); return ret;}/* MBX mail append message from stringstruct * Accepts: MAIL stream * destination mailbox * append callback * data for callback * Returns: T if append successful, else NIL */long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data){ struct stat sbuf; int fd,ld; char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN]; struct utimbuf times; FILE *df; MESSAGECACHE elt; long f; unsigned long i,uf; STRING *message; long ret = NIL; MAILSTREAM *dstream = NIL; appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL); SEARCHSET *dst = au ? mail_newsearchset () : NIL; /* make sure valid mailbox */ /* 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 : &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 (!(*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 || !(*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); if (ret) times.actime = time (0) - 1; /* else preserve \Marked status */ else times.actime = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time (0); /* preserve mtime */ times.modtime = sbuf.st_mtime; utime (file,×); /* 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 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 */ stream->silent = T; /* don't pass up mm_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 = (unsigned long) 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -