⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtx.c

📁 广泛使用的邮件服务器!同时
💻 C
📖 第 1 页 / 共 4 页
字号:
    for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));    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;%010lo%02lo\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 || !MM_APPEND (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 */    if (errno) {      sprintf (tmp,"Message append failed: %s",strerror (errno));      MM_LOG (tmp,ERROR);    }    ret = NIL;  }  if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */				/* 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 */  unlockfd (ld,lock);		/* release exclusive parse/append permission */  MM_NOCRITICAL (stream);	/* release critical */  if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))    MM_LOG ("Can not return meaningful APPENDUID with this mailbox format",	    WARN);  return ret;}/* Internal routines *//* MTX mail generate file string * Accepts: temporary buffer to write into *	    mailbox name string * Returns: local file string or NIL if failure */char *mtx_file (char *dst,char *name){  char tmp[MAILTMPLEN];  char *s = mailboxfile (dst,name);				/* return our standard inbox */  return (s && !*s) ? mailboxfile (dst,mtx_isvalid ("~/INBOX",tmp) ?				   "~/INBOX" : "INBOX.MTX") : s;}/* MTX mail parse mailbox * Accepts: MAIL stream * Returns: T if parse OK *	    NIL if failure, stream aborted */long mtx_parse (MAILSTREAM *stream){  struct stat sbuf;  MESSAGECACHE *elt = NIL;  unsigned char c,*s,*t,*x;  char tmp[MAILTMPLEN];  unsigned long i,j;  long curpos = LOCAL->filesize;  long nmsgs = stream->nmsgs;  long recent = stream->recent;  short added = NIL;  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);    mtx_close (stream,NIL);    return NIL;  }  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);      mtx_close (stream,NIL);      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: %s",	       (unsigned long) curpos,i,(char *) LOCAL->buf);      MM_LOG (tmp,ERROR);      mtx_close (stream,NIL);      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: %s",	       (unsigned long) curpos,(char *) LOCAL->buf);      MM_LOG (tmp,ERROR);      mtx_close (stream,NIL);      return NIL;    }    *s++ = '\0'; *t++ = '\0';	/* tie off fields */    added = T;			/* note that a new message was added */				/* swell the cache */    mail_exists (stream,++nmsgs);				/* instantiate an elt for this message */    (elt = mail_elt (stream,nmsgs))->valid = T;    elt->private.uid = ++stream->uid_last;				/* note file offset of header */    elt->private.special.offset = curpos;				/* in case error */    elt->private.special.text.size = 0;				/* header size not known yet */    elt->private.msg.header.text.size = 0;    x = s;			/* parse the header components */    if (mail_parse_date (elt,LOCAL->buf) &&	(elt->rfc822_size = strtoul (s,(char **) &s,10)) && (!(s && *s)) &&	isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&	isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&	isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&	isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])      elt->private.special.text.size = i;    else {			/* oops */      sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",	       curpos,(char *) LOCAL->buf,(char *) x,(char *) t);      MM_LOG (tmp,ERROR);      mtx_close (stream,NIL);      return NIL;    }				/* make sure didn't run off end of file */    if ((curpos += (elt->rfc822_size + i)) > sbuf.st_size) {      sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",	       elt->private.special.offset,(unsigned long) curpos,	       (unsigned long) sbuf.st_size);      MM_LOG (tmp,ERROR);      mtx_close (stream,NIL);      return NIL;    }    c = t[10];			/* remember first system flags byte */    t[10] = '\0';		/* tie off flags */    j = strtoul (t,NIL,8);	/* get user flags value */    t[10] = c;			/* restore first system flags byte */				/* set up all valid user flags (reversed!) */    while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&		  stream->user_flags[i]) elt->user_flags |= 1 << i;				/* calculate system flags */    if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;    if (j & fDELETED) elt->deleted = T;    if (j & fFLAGGED) elt->flagged = T;    if (j & fANSWERED) elt->answered = T;    if (j & fDRAFT) elt->draft = T;    if (!(j & fOLD)) {		/* newly arrived message? */      elt->recent = T;      recent++;			/* count up a new recent message */				/* mark it as old */      mtx_update_status (stream,nmsgs,NIL);    }  }  fsync (LOCAL->fd);		/* make sure all the fOLD flags 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 */}/* MTX get cache element with status updating from file * Accepts: MAIL stream *	    message number * Returns: cache element */MESSAGECACHE *mtx_elt (MAILSTREAM *stream,unsigned long msgno){  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;  mtx_read_flags (stream,elt);  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;}/* MTX read flags from file * Accepts: MAIL stream * Returns: cache element */void mtx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt){  unsigned long i,j;				/* noop if readonly and have valid flags */  if (stream->rdonly && elt->valid) return;				/* set the seek pointer */  lseek (LOCAL->fd,(off_t) elt->private.special.offset +	 elt->private.special.text.size - 14,L_SET);				/* read the new flags */  if (read (LOCAL->fd,LOCAL->buf,12) < 0) {    sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));    fatal (LOCAL->buf);  }				/* calculate system flags */  i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');  elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;  elt->flagged = i & fFLAGGED ? T : NIL;  elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;  LOCAL->buf[10] = '\0';	/* tie off flags */  j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */				/* set up all valid user flags (reversed!) */  while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&		stream->user_flags[i]) elt->user_flags |= 1 << i;  elt->valid = T;		/* have valid flags now */}/* MTX update status string * Accepts: MAIL stream *	    message number *	    flag saying whether or not to sync */void mtx_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag){  time_t tp[2];  struct stat sbuf;  MESSAGECACHE *elt = mail_elt (stream,msgno);  unsigned long j,k = 0;				/* readonly */  if (stream->rdonly || !elt->valid) mtx_read_flags (stream,elt);  else {			/* readwrite */    j = elt->user_flags;	/* get user flags */				/* reverse bits (dontcha wish we had CIRC?) */    while (j) k |= 1 << (29 - find_rightmost_bit (&j));				/* print new flag string */    sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)	     (fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +	      (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +	      (fDRAFT * elt->draft)));				/* get to that place in the file */    lseek (LOCAL->fd,(off_t) elt->private.special.offset +	   elt->private.special.text.size - 14,L_SET);				/* write new flags */    write (LOCAL->fd,LOCAL->buf,12);    if (syncflag) {		/* sync if requested */      fsync (LOCAL->fd);      fstat (LOCAL->fd,&sbuf);	/* get new write time */      tp[1] = LOCAL->filetime = sbuf.st_mtime;      tp[0] = time (0);		/* make sure read is later */      utime (stream->mailbox,tp);    }  }}/* MTX locate header for a message * Accepts: MAIL stream *	    message number *	    pointer to returned header size * Returns: position of header in file */unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno,			  unsigned long *size){  unsigned long siz;  long i = 0;  int q = 0;  char *s,tmp[MAILTMPLEN];  MESSAGECACHE *elt = mtx_elt (stream,msgno);  unsigned long ret = elt->private.special.offset +    elt->private.special.text.size;				/* is header size known? */  if (!(*size = elt->private.msg.header.text.size)) {    lseek (LOCAL->fd,ret,L_SET);/* get to header position */				/* search message for CRLF CRLF */    for (siz = 1,s = tmp; siz <= elt->rfc822_size; siz++) {				/* read another buffer as necessary */      if ((--i <= 0) &&		/* buffer empty? */	  (read (LOCAL->fd,s = tmp,		 i = min (elt->rfc822_size - siz,(long) MAILTMPLEN)) < 0))	return ret;		/* I/O error? */      switch (q) {		/* sniff at buffer */      case 0:			/* first character */	q = (*s++ == '\015') ? 1 : 0;	break;      case 1:			/* second character */	q = (*s++ == '\012') ? 2 : 0;	break;      case 2:			/* third character */	q = (*s++ == '\015') ? 3 : 0;	break;      case 3:			/* fourth character */	if (*s++ == '\012') {	/* have the sequence? */				/* yes, note for later */	  elt->private.msg.header.text.size = *size = siz;	  return ret;	}	q = 0;			/* lost... */	break;      }    }				/* header consumes entire message */    elt->private.msg.header.text.size = *size = elt->rfc822_size;  }  return ret;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -