mbx.c

来自「这是用C编写IMAP源代码」· C语言 代码 · 共 1,740 行 · 第 1/4 页

C
1,740
字号
		   mail_open (NIL,mbx,OP_READONLY|OP_SILENT)))    return NIL;  status.flags = flags;		/* return status values */  status.messages = stream->nmsgs;  status.recent = stream->recent;  if (flags & SA_UNSEEN)	/* must search to get unseen messages */    for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)      if (!mail_elt (stream,i)->seen) status.unseen++;  status.uidnext = stream->uid_last + 1;  status.uidvalidity = stream->uid_validity;				/* calculate post-snarf results */  if (!status.recent && stream->inbox &&      (systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) {    status.messages += systream->nmsgs;    status.recent += systream->recent;    if (flags & SA_UNSEEN)	/* must search to get unseen messages */      for (i = 1; i <= systream->nmsgs; i++)	if (!mail_elt (systream,i)->seen) status.unseen++;				/* kludge but probably good enough */    status.uidnext += systream->nmsgs;  }  MM_STATUS(stream,mbx,&status);/* pass status to main program */  if (tstream) mail_close (tstream);  if (systream) mail_close (systream);  return T;			/* success */}/* MBX mail open * Accepts: stream to open * Returns: stream on success, NIL on failure */MAILSTREAM *mbx_open (MAILSTREAM *stream){  int fd,ld;  short silent;  char tmp[MAILTMPLEN];  blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);				/* return prototype for OP_PROTOTYPE call */  if (!stream) return user_flags (&mbxproto);  if (stream->local) fatal ("mbx recycle stream");				/* canonicalize the mailbox name */  if (!mbx_file (tmp,stream->mailbox)) {    sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);    mm_log (tmp,ERROR);  }  if (stream->rdonly ||      (fd = open (tmp,O_RDWR|O_BINARY,NIL)) < 0) {    if ((fd = open (tmp,O_RDONLY|O_BINARY,NIL)) < 0) {      sprintf (tmp,"Can't open mailbox: %s",strerror (errno));      MM_LOG (tmp,ERROR);      return NIL;    }    else if (!stream->rdonly) {	/* got it, but readonly */      MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);      stream->rdonly = T;    }  }  stream->local = memset (fs_get (sizeof (MBXLOCAL)),NIL,sizeof (MBXLOCAL));  LOCAL->fd = fd;		/* bind the file */  LOCAL->ld = -1;		/* no flaglock */  LOCAL->buf = (char *) fs_get (MAXMESSAGESIZE + 1);  LOCAL->buflen = MAXMESSAGESIZE;  LOCAL->text.data = (unsigned char *)    fs_get ((LOCAL->text.size = MAXMESSAGESIZE) + 1);				/* note if an INBOX or not */  stream->inbox = !compare_cstring (stream->mailbox,"INBOX");  fs_give ((void **) &stream->mailbox);  stream->mailbox = cpystr (tmp);				/* get parse/append permission */  if ((ld = lockfd (LOCAL->fd,tmp,LOCK_EX)) < 0) {    MM_LOG ("Unable to lock open mailbox",ERROR);    return NIL;  }  (*bn) (BLOCK_FILELOCK,NIL);  flock (LOCAL->fd,LOCK_SH);	/* lock the file */  (*bn) (BLOCK_NONE,NIL);  unlockfd (ld,tmp);		/* release shared parse permission */  LOCAL->filesize = HDRSIZE;	/* initialize parsed file size */				/* time not set up yet */  LOCAL->lastsnarf = LOCAL->filetime = 0;  LOCAL->expok = LOCAL->flagcheck = NIL;  stream->sequence++;		/* bump sequence number */				/* parse mailbox */  stream->nmsgs = stream->recent = 0;  silent = stream->silent;	/* defer events */  stream->silent = T;  if (mbx_ping (stream) && !stream->nmsgs)    MM_LOG ("Mailbox is empty",(long) NIL);  stream->silent = silent;	/* now notify upper level */  mail_exists (stream,stream->nmsgs);  mail_recent (stream,stream->recent);  if (!LOCAL) return NIL;	/* failure if stream died */  stream->perm_seen = stream->perm_deleted = stream->perm_flagged =    stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T;  stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;  stream->kwd_create = (stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ?    NIL : T;			/* can we create new user flags? */  return stream;		/* return stream to caller */}/* MBX mail close * Accepts: MAIL stream *	    close options */void mbx_close (MAILSTREAM *stream,long options){  if (stream && LOCAL) {	/* only if a file is open */    int silent = stream->silent;    stream->silent = T;		/* note this stream is dying */				/* do an expunge if requested */    if (options & CL_EXPUNGE) mbx_expunge (stream);    else {			/* otherwise do a checkpoint to purge */      LOCAL->expok = T;		/*  possible expunged messages */      mbx_ping (stream);    }    stream->silent = silent;	/* restore previous status */    mbx_abort (stream);  }}/* MBX mail abort stream * Accepts: MAIL stream */void mbx_abort (MAILSTREAM *stream){  if (stream && LOCAL) {	/* only if a file is open */    flock (LOCAL->fd,LOCK_UN);	/* unlock local file */    close (LOCAL->fd);		/* close the local file */				/* free local text buffer */    if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);    if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);				/* nuke the local data */    fs_give ((void **) &stream->local);    stream->dtb = NIL;		/* log out the DTB */  }}/* MBX mail fetch flags * Accepts: MAIL stream *	    sequence *	    option flags * Sniffs at file to see if some other process changed the flags */void mbx_flags (MAILSTREAM *stream,char *sequence,long flags){  MESSAGECACHE *elt;  unsigned long i;  if (mbx_ping (stream) && 	/* ping mailbox, get new status for messages */      ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :       mail_sequence (stream,sequence)))    for (i = 1; i <= stream->nmsgs; i++)       if ((elt = mail_elt (stream,i))->sequence && !elt->valid)	mbx_elt (stream,i,NIL);}/* MBX 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 *mbx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,		  long flags){  unsigned long i;  char *s;  *length = 0;			/* default to empty */  if (flags & FT_UID) return "";/* UID call "impossible" */				/* get header position, possibly header */  i = mbx_hdrpos (stream,msgno,length,&s);  if (!s) {			/* mbx_hdrpos() returned header? */    lseek (LOCAL->fd,i,L_SET);	/* no, get to header position */				/* is buffer big enough? */    if (*length > LOCAL->buflen) {      fs_give ((void **) &LOCAL->buf);      LOCAL->buf = (char *) fs_get ((LOCAL->buflen = *length) + 1);    }				/* slurp the data */    read (LOCAL->fd,s = LOCAL->buf,*length);  }  s[*length] = '\0';		/* tie off string */  return s;}/* MBX mail fetch message text (body only) * Accepts: MAIL stream *	    message # to fetch *	    pointer to returned header text length *	    option flags * Returns: T, always */long mbx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags){  unsigned long i,j;  char *s = LOCAL->text.data;  MESSAGECACHE *elt;				/* UID call "impossible" */  if (flags & FT_UID) return NIL;				/* get message status */  elt = mbx_elt (stream,msgno,NIL);				/* if message not seen */  if (!(flags & FT_PEEK) && !elt->seen && mbx_flaglock (stream)) {    elt->seen = T;		/* mark message as seen */				/* recalculate status */    mbx_update_status (stream,msgno,NIL);    MM_FLAGS (stream,msgno);				/* update flags */    mbx_flag (stream,NIL,NIL,NIL);  }  if (!LOCAL) i = 0;		/* mbx_flaglock() could have aborted */				/* in case previous text cached */  if (elt->private.uid == LOCAL->uid)    i = elt->rfc822_size - elt->private.msg.header.text.size;  else {			/* not cached, cache it now */    LOCAL->uid = elt->private.uid;				/* find header position */    i = mbx_hdrpos (stream,msgno,&j,NIL);				/* go to text position */    lseek (LOCAL->fd,i + j,L_SET);				/* is buffer big enough? */    if ((i = elt->rfc822_size - j) > LOCAL->text.size) {      fs_give ((void **) &LOCAL->text.data);      LOCAL->text.data = (unsigned char *) fs_get ((LOCAL->text.size = i) + 1);    }				/* slurp the data */    read (LOCAL->fd,s = LOCAL->text.data,i);    LOCAL->text.data[i] = '\0';	/* tie off string */  }  INIT (bs,mail_string,s,i);	/* set up stringstruct */  return T;			/* success */}/* MBX mail modify flags * Accepts: MAIL stream *	    sequence *	    flag(s) *	    option flags */void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags){  time_t tp[2];  struct stat sbuf;  unsigned long oldpid = LOCAL->lastpid;				/* make sure the update takes */  if (!stream->rdonly && LOCAL && (LOCAL->fd >= 0) && (LOCAL->ld >= 0)) {    fsync (LOCAL->fd);    fstat (LOCAL->fd,&sbuf);	/* get current write time */    tp[1] = LOCAL->filetime = sbuf.st_mtime;				/* we are the last flag updater */    LOCAL->lastpid = (unsigned long) getpid ();				/* update header if needed */    if (((LOCAL->ffuserflag < NUSERFLAGS) &&	 stream->user_flags[LOCAL->ffuserflag]) || (oldpid != LOCAL->lastpid))      mbx_update_header (stream);    tp[0] = time (0);		/* make sure read comes after all that */    utime (stream->mailbox,tp);    unlockfd (LOCAL->ld,LOCAL->lock);    LOCAL->ld = -1;  }}/* MBX mail per-message modify flags * Accepts: MAIL stream *	    message cache element */void mbx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt){  if (mbx_flaglock (stream)) mbx_update_status (stream,elt->msgno,NIL);}/* MBX mail ping mailbox * Accepts: MAIL stream * Returns: T if stream still alive, NIL if not */long mbx_ping (MAILSTREAM *stream){  unsigned long i,pos;  long ret = NIL;  int ld;  char lock[MAILTMPLEN];  MESSAGECACHE *elt;  struct stat sbuf;  if (stream && LOCAL) {	/* only if stream already open */    int snarf = stream->inbox && !stream->rdonly;    ret = LONGT;		/* assume OK */    fstat (LOCAL->fd,&sbuf);	/* get current file poop */				/* allow expunge if permitted at ping */    if (mail_parameters (NIL,GET_EXPUNGEATPING,NIL)) LOCAL->expok = T;				/* if external modification */    if (LOCAL->filetime && (LOCAL->filetime < sbuf.st_mtime))      LOCAL->flagcheck = T;	/* upgrade to flag checking */				/* new mail or flagcheck handling needed? */    if (((i = (sbuf.st_size - LOCAL->filesize)) || LOCAL->flagcheck ||	 !stream->nmsgs || snarf) &&	((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) >= 0)) {      if (LOCAL->flagcheck) {	/* sweep mailbox for changed message status */	if (ret = mbx_parse (stream)) {	  LOCAL->filetime = sbuf.st_mtime;	  for (i = 1; i <= stream->nmsgs; )	    if (mbx_elt (stream,i,LOCAL->expok)) ++i;	  LOCAL->flagcheck =NIL;/* got all the updates */	}      }      else if (i) ret = mbx_parse (stream);      if (ret && snarf) {	/* snarf new messages if still OK */	mbx_snarf (stream);				/* parse snarfed messages */	ret = mbx_parse (stream);      }      unlockfd (ld,lock);	/* release shared parse/append permission */    }    if (ret) {			/* must still be alive */      if (!LOCAL->expunged)	/* look for holes if none known yet */	for (i = 1, pos = HDRSIZE;	     !LOCAL->expunged && (i <= stream->nmsgs);	     i++, pos += elt->private.special.text.size + elt->rfc822_size)	  if ((elt = mail_elt (stream,i))->private.special.offset != pos)	    LOCAL->expunged = T;/* found a hole */				/* burp any holes */      if (LOCAL->expunged && !stream->rdonly) {	if (mbx_rewrite (stream,&i,NIL)) fatal ("expunge on check");	if (i) {		/* any space reclaimed? */	  LOCAL->expunged = NIL;/* no more pending expunge */	  sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",i);	  MM_LOG (LOCAL->buf,(long) NIL);	}      }      LOCAL->expok = NIL;	/* no more expok */    }  }  return ret;			/* return result of the parse */}/* MBX mail check mailbox (reparses status too) * Accepts: MAIL stream */void mbx_check (MAILSTREAM *stream){  if (LOCAL) LOCAL->expok = T;	/* mark that a check is desired */  if (mbx_ping (stream)) MM_LOG ("Check completed",(long) NIL);}/* MBX mail expunge mailbox * Accepts: MAIL stream */void mbx_expunge (MAILSTREAM *stream){  unsigned long nexp,reclaimed;  if (!mbx_ping (stream));	/* do nothing if stream dead */  else if (stream->rdonly)	/* won't do on readonly files! */    MM_LOG ("Expunge ignored on readonly mailbox",WARN);				/* if expunged any messages */  else if (nexp = mbx_rewrite (stream,&reclaimed,T)) {    sprintf (LOCAL->buf,"Expunged %lu messages",nexp);    MM_LOG (LOCAL->buf,(long) NIL);  }  else if (reclaimed) {		/* or if any prior expunged space reclaimed */    sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",reclaimed);    MM_LOG (LOCAL->buf,(long) NIL);  }  else MM_LOG ("No messages deleted, so no update needed",(long) NIL);}/* MBX mail snarf messages from system inbox * Accepts: MAIL stream, already locked */void mbx_snarf (MAILSTREAM *stream){  unsigned long i = 0;  unsigned long j,r,hdrlen,txtlen;  struct stat sbuf;  char *hdr,*txt,tmp[MAILTMPLEN];  MESSAGECACHE *elt;  MAILSTREAM *sysibx = NIL;				/* give up if can't get exclusive permission */  if ((time (0) >= (LOCAL->lastsnarf +		    (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) &&      strcmp (sysinbox (),stream->mailbox)) {    MM_CRITICAL (stream);	/* go critical */				/* sizes match and anything in sysinbox? */    if (!stat (sysinbox (),&sbuf) && sbuf.st_size &&	!fstat (LOCAL->fd,&sbuf) && (sbuf.st_size == LOCAL->filesize) && 	(sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&	(!sysibx->rdonly) && (r = sysibx->nmsgs)) {				/* yes, go to end of file in our mailbox */      lseek (LOCAL->fd,sbuf.st_size,L_SET);				/* for each message in sysibx mailbox */      while (r && (++i <= sysibx->nmsgs)) {				/* snarf message from system INBOX */	hdr = cpystr (mail_fetchheader_full (sysibx,i,NIL,&hdrlen,NIL));	txt = mail_fetchtext_full (sysibx,i,&txtlen,FT_PEEK);				/* if have a message */	if (j = hdrlen + txtlen) {				/* build header line */	  mail_date (LOCAL->buf,elt = mail_elt (sysibx,i));	  sprintf (LOCAL->buf + strlen (LOCAL->buf),		   ",%lu;00000000%04x-00000000\015\012",j,(unsigned)		   ((fSEEN * elt->seen) +		    (fDELETED * elt->deleted) + (fFLAGGED * elt->flagged) +		    (fANSWERED * elt->answered) + (fDRAFT * elt->draft)));				/* copy message */	  if ((write (LOCAL->fd,LOCAL->buf,strlen (LOCAL->buf)) < 0) ||	      (write (LOCAL->fd,hdr,hdrlen) < 0) ||	      (write (LOCAL->fd,txt,txtlen) < 0)) r = 0;	}	fs_give ((void **) &hdr);      }				/* make sure all the updates take */      if (fsync (LOCAL->fd)) r = 0;      if (r) {			/* delete all the messages we copied */	if (r == 1) strcpy (tmp,"1");	else sprintf (tmp,"1:%lu",r);	mail_setflag (sysibx,tmp,"\\Deleted");	mail_expunge (sysibx);	/* now expunge all those messages */      }      else {

⌨️ 快捷键说明

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