📄 mbx.c
字号:
mail_setflag (sysibx,tmp,"\\Deleted"); mail_expunge (sysibx); /* now expunge all those messages */ } else { sprintf (LOCAL->buf,"Can't copy new mail: %s",strerror (errno)); mm_log (LOCAL->buf,ERROR); ftruncate (LOCAL->fd,sbuf.st_size); } fstat (LOCAL->fd,&sbuf); /* yes, get current file size */ LOCAL->filetime = sbuf.st_mtime; } if (sysibx) mail_close (sysibx); } mm_nocritical (stream); /* release critical */ LOCAL->lastsnarf = time (0); /* note time of last snarf */}/* MBX mail copy message(s) * Accepts: MAIL stream * sequence * destination mailbox * copy options * Returns: T if success, NIL if failed */long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options){ struct stat sbuf; time_t tp[2]; MESSAGECACHE *elt; unsigned long i,j,k; long ret = LONGT; int fd,ld; char file[MAILTMPLEN],lock[MAILTMPLEN]; mailproxycopy_t pc = (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL); /* make sure valid mailbox */ if (!mbx_isvalid (mailbox,LOCAL->buf)) switch (errno) { case ENOENT: /* no such file? */ mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL); 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; } if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence))) return NIL; /* got file? */ if ((fd=open(mbx_file(file,mailbox),O_RDWR|O_CREAT,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 */ /* get parse/append permission */ if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) { mm_log ("Unable to lock copy mailbox",ERROR); return NIL; } 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 */ sprintf (LOCAL->buf+strlen(LOCAL->buf),",%lu;%08lx%04x-00000000\015\012", elt->rfc822_size,elt->user_flags,(unsigned) ((fSEEN * elt->seen) + (fDELETED * elt->deleted) + (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) + (fDRAFT * elt->draft))); /* 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; } } /* 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); } tp[0] = sbuf.st_atime; /* preserve atime and mtime */ tp[1] = sbuf.st_mtime; utime (file,tp); /* set the times */ close (fd); /* close the file */ unlockfd (ld,lock); /* release exclusive parse/append permission */ mm_nocritical (stream); /* release critical */ /* delete all requested messages */ if (ret && (options & CP_MOVE)) { for (i = 1; i <= stream->nmsgs; ) if (elt = mbx_elt (stream,i,T)) { if (elt->sequence) { /* want to do this message? */ elt->deleted = T; /* mark message deleted */ /* recalculate status */ mbx_update_status (stream,i,NIL); } i++; /* move to next message */ } if (!stream->rdonly) { /* make sure the update takes */ fsync (LOCAL->fd); fstat (LOCAL->fd,&sbuf); /* get current write time */ LOCAL->filetime = sbuf.st_mtime; } } 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,c; char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN]; time_t tp[2]; FILE *df; MESSAGECACHE elt; long f; unsigned long i,uf; STRING *message; long ret = LONGT; /* default stream to prototype */ if (!stream) stream = user_flags (&mbxproto); /* make sure valid mailbox */ if (!mbx_isvalid (mailbox,tmp)) switch (errno) { case ENOENT: /* no such file? */ if (((mailbox[0] == 'I') || (mailbox[0] == 'i')) && ((mailbox[1] == 'N') || (mailbox[1] == 'n')) && ((mailbox[2] == 'B') || (mailbox[2] == 'b')) && ((mailbox[3] == 'O') || (mailbox[3] == 'o')) && ((mailbox[4] == 'X') || (mailbox[4] == 'x')) && !mailbox[5]) mbx_create (NIL,"INBOX"); else { mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL); return NIL; } /* falls through */ case 0: /* merely empty file? */ break; 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) (stream,data,&flags,&date,&message)) return NIL; /* open destination mailbox */ if (((fd = open (mbx_file (file,mailbox),O_WRONLY|O_APPEND|O_CREAT, S_IREAD|S_IWRITE)) < 0) || !(df = fdopen (fd,"ab"))) { sprintf (tmp,"Can't open append mailbox: %s",strerror (errno)); mm_log (tmp,ERROR); return NIL; } /* get parse/append permission */ if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) { mm_log ("Unable to lock append mailbox",ERROR); close (fd); return NIL; } mm_critical (stream); /* go critical */ fstat (fd,&sbuf); /* get current file size */ do { /* parse flags */ f = mail_parse_flags (stream,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-00000000\015\012",tmp, i = SIZE (message),uf,(unsigned long) f) < 0) ret = NIL; else { /* write message */ if (i) do c = 0xff & SNX (message); while ((putc (c,df) != EOF) && --i); /* get next message */ if (i || !(*af) (stream,data,&flags,&date,&message)) ret = NIL; } } while (ret && message); /* if error... */ if (!ret || (fflush (df) == EOF)) { ftruncate (fd,sbuf.st_size);/* revert file */ close (fd); /* make sure fclose() doesn't corrupt us */ sprintf (tmp,"Message append failed: %s",strerror (errno)); mm_log (tmp,ERROR); ret = NIL; } tp[0] = sbuf.st_atime; /* preserve atime and mtime */ tp[1] = sbuf.st_mtime; utime (file,tp); /* set the times */ fclose (df); /* close the file */ unlockfd (ld,lock); /* release exclusive parse/append permission */ mm_nocritical (stream); /* release critical */ 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; 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 silent = stream->silent; 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,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,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,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,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,&x,10)) && (!(x && *x))) { sprintf (tmp,"Unable to parse message size at %lu: %.80s,%.80s;%.80s", (unsigned long) curpos,LOCAL->buf,s,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))) { sprintf (tmp,"Invalid UID %08lx in message %lu, rebuilding UIDs", m,nmsgs+1); mm_log (tmp,WARN); m = 0; /* lose this UID */ /* restart UID validity and last UID */ stream->uid_validity = time (0); stream->uid_last = lastuid; } t[12] = '\0'; /* parse system flags */ if ((k = strtoul (t+8,NIL,16)) & fEXPUNGED) /* expunged message, update last UID */ lastuid = m ? m : ++stream->uid_last; else { /* not expunged, swell the cache */ 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,LOCAL->buf); mm_log (tmp,ERROR);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -