unixnt.c

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

C
1,867
字号
    if (LOCAL->line) fs_give ((void **) &LOCAL->line);				/* nuke the local data */    fs_give ((void **) &stream->local);    stream->dtb = NIL;		/* log out the DTB */  }}/* UNIX open and lock mailbox * Accepts: file name to open/lock *	    file open mode *	    destination buffer for lock file name *	    type of locking operation (LOCK_SH or LOCK_EX) */int unix_lock (char *file,int flags,int mode,char *lock,int op){  int fd,ld,j;  int i = LOCKTIMEOUT * 60 - 1;  char tmp[MAILTMPLEN];  time_t t;  struct stat sb;  sprintf (lock,"%s.lock",file);/* build lock filename */  do {				/* until OK or out of tries */    t = time (0);		/* get the time now */				/* try to get the lock */    if ((ld = open(lock,O_BINARY|O_WRONLY|O_CREAT|O_EXCL,S_IREAD|S_IWRITE))>=0)      close (ld);		/* got it, close the lock file! */    else if (errno != EEXIST) {	/* miscellaneous error */      sprintf (tmp,"Error creating %.80s: %s",lock,strerror (errno));      if (!(i%15)) mm_log (tmp,WARN);    }				/* lock exists, still active? */    else if (!stat (lock,&sb) && (t > sb.st_ctime + LOCKTIMEOUT * 60) &&	     ((ld = open(lock,O_BINARY|O_WRONLY|O_CREAT,S_IREAD|S_IWRITE))>=0))      close (ld);		/* got timed-out lock file */    else {			/* active lock, try again */      if (!(i%15)) {	sprintf (tmp,"Mailbox %.80s is locked, will override in %d seconds...",		 file,i);	mm_log (tmp,WARN);      }      sleep (1);		/* wait a second before next retry */    }  } while (*lock && ld < 0 && i--);				/* open file */  if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);  else {			/* open failed */    j = errno;			/* preserve error code */    if (*lock) unlink (lock);	/* flush the lock file if any */    errno = j;			/* restore error code */  }  return fd;}/* UNIX unlock and close mailbox * Accepts: file descriptor *	    (optional) mailbox stream to check atime/mtime *	    (optional) lock file name */void unix_unlock (int fd,MAILSTREAM *stream,char *lock){  if (stream) {			/* need to muck with times? */    struct stat sbuf;    struct utimbuf times;    time_t now = time (0);    fstat (fd,&sbuf);		/* get file times */    if (LOCAL->ld >= 0) {	/* yes, readwrite session? */      times.actime = now;	/* set atime to now */				/* set mtime to (now - 1) if necessary */      times.modtime = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;    }    else if (stream->recent) {	/* readonly with recent messages */      if ((sbuf.st_atime >= sbuf.st_mtime) ||	  (sbuf.st_atime >= sbuf.st_ctime))				/* keep past mtime, whack back atime */	times.actime = (times.modtime = (sbuf.st_mtime < now) ?			sbuf.st_mtime : now) - 1;      else now = 0;		/* no time change needed */    }				/* readonly with no recent messages */    else if ((sbuf.st_atime < sbuf.st_mtime) ||	     (sbuf.st_atime < sbuf.st_ctime)) {      times.actime = now;	/* set atime to now */				/* set mtime to (now - 1) if necessary */      times.modtime = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;    }    else now = 0;		/* no time change needed */				/* set the times, note change */    if (now && !utime (stream->mailbox,&times))      LOCAL->filetime = times.modtime;  }  flock (fd,LOCK_UN);		/* release flock'ers */  if (!stream) close (fd);	/* close the file if no stream */				/* flush the lock file if any */  if (lock && *lock) unlink (lock);}/* UNIX mail parse and lock mailbox * Accepts: MAIL stream *	    space to write lock file name *	    type of locking operation * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure */int unix_parse (MAILSTREAM *stream,char *lock,int op){  int zn;  unsigned long i,j,k,m;  unsigned char c,*s,*t,*u,tmp[MAILTMPLEN],date[30];  int ti = 0,retain = T;  unsigned long nmsgs = stream->nmsgs;  unsigned long prevuid = nmsgs ? mail_elt (stream,nmsgs)->private.uid : 0;  unsigned long recent = stream->recent;  unsigned long oldnmsgs = stream->nmsgs;  short silent = stream->silent;  short pseudoseen = NIL;  struct stat sbuf;  STRING bs;  FDDATA d;  MESSAGECACHE *elt;  mail_lock (stream);		/* guard against recursion or pingers */				/* toss out previous descriptor */  if (LOCAL->fd >= 0) close (LOCAL->fd);  mm_critical (stream);		/* open and lock mailbox (shared OK) */  if ((LOCAL->fd = unix_lock (stream->mailbox,			      O_BINARY + ((LOCAL->ld >= 0) ? O_RDWR:O_RDONLY),			      NIL,lock,op)) < 0) {    sprintf (tmp,"Mailbox open failed, aborted: %s",strerror (errno));    mm_log (tmp,ERROR);    unix_abort (stream);    mail_unlock (stream);    mm_nocritical (stream);	/* done with critical */    return NIL;  }  fstat (LOCAL->fd,&sbuf);	/* get status */				/* validate change in size */  if (sbuf.st_size < LOCAL->filesize) {    sprintf (tmp,"Mailbox shrank from %lu to %lu bytes, aborted",	     (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);    mm_log (tmp,ERROR);		/* this is pretty bad */    unix_unlock (LOCAL->fd,stream,lock);    unix_abort (stream);    mail_unlock (stream);    mm_nocritical (stream);	/* done with critical */    return NIL;  }				/* new data? */  else if (i = sbuf.st_size - LOCAL->filesize) {    d.fd = LOCAL->fd;		/* yes, set up file descriptor */    d.pos = LOCAL->filesize;	/* get to that position in the file */    d.chunk = LOCAL->buf;	/* initial buffer chunk */    d.chunksize = CHUNK;	/* file chunk size */    INIT (&bs,fd_string,&d,i);	/* initialize stringstruct */				/* skip leading whitespace for broken MTAs */    while (((c = CHR (&bs)) == '\n') || (c == '\r') ||	   (c == ' ') || (c == '\t')) SNX (&bs);    if (SIZE (&bs)) {		/* read new data */				/* remember internal header position */      j = LOCAL->filesize + GETPOS (&bs);      s = unix_mbxline (stream,&bs,&i);      t = NIL,zn = 0;      if (i) VALID (s,t,ti,zn);	/* see if valid From line */      if (!ti) {		/* someone pulled the rug from under us */	sprintf (tmp,"Unexpected changes to mailbox (try restarting): %.20s",		 (char *) s);	mm_log (tmp,ERROR);	unix_unlock (LOCAL->fd,stream,lock);	unix_abort (stream);	mail_unlock (stream);	mm_nocritical (stream);	/* done with critical */	return NIL;      }      stream->silent = T;	/* quell main program new message events */      do {			/* found a message */				/* instantiate first new message */	mail_exists (stream,++nmsgs);	(elt = mail_elt (stream,nmsgs))->valid = T;	recent++;		/* assume recent by default */	elt->recent = T;				/* note position/size of internal header */	elt->private.special.offset = j;	elt->private.msg.header.offset = elt->private.special.text.size = i;				/* generate plausible IMAPish date string */	date[2] = date[6] = date[20] = '-'; date[11] = ' ';	date[14] = date[17] = ':';				/* dd */	date[0] = t[ti - 2]; date[1] = t[ti - 1];				/* mmm */	date[3] = t[ti - 6]; date[4] = t[ti - 5]; date[5] = t[ti - 4];				/* hh */	date[12] = t[ti + 1]; date[13] = t[ti + 2];				/* mm */	date[15] = t[ti + 4]; date[16] = t[ti + 5];	if (t[ti += 6] == ':') {/* ss */	  date[18] = t[++ti]; date[19] = t[++ti];	  ti++;			/* move to space */	}	else date[18] = date[19] = '0';				/* yy -- advance over timezone if necessary */	if (zn == ti) ti += (((t[zn+1] == '+') || (t[zn+1] == '-')) ? 6 : 4);	date[7] = t[ti + 1]; date[8] = t[ti + 2];	date[9] = t[ti + 3]; date[10] = t[ti + 4];				/* zzz */	t = zn ? (t + zn + 1) : (unsigned char *) "LCL";	date[21] = *t++; date[22] = *t++; date[23] = *t++;	if ((date[21] != '+') && (date[21] != '-')) date[24] = '\0';	else {			/* numeric time zone */	  date[24] = *t++; date[25] = *t++;	  date[26] = '\0'; date[20] = ' ';	}				/* set internal date */	if (!mail_parse_date (elt,date)) {	  sprintf (tmp,"Unable to parse internal date: %s",(char *) date);	  mm_log (tmp,WARN);	}	do {			/* look for message body */	  s = t = unix_mbxline (stream,&bs,&i);	  if (i) switch (*s) {	/* check header lines */	  case 'X':		/* possible X-???: line */	    if (s[1] == '-') {	/* must be immediately followed by hyphen */				/* X-Status: becomes Status: in S case */	      if (s[2] == 'S' && s[3] == 't' && s[4] == 'a' && s[5] == 't' &&		  s[6] == 'u' && s[7] == 's' && s[8] == ':') s += 2;				/* possible X-Keywords */	      else if (s[2] == 'K' && s[3] == 'e' && s[4] == 'y' &&		       s[5] == 'w' && s[6] == 'o' && s[7] == 'r' &&		       s[8] == 'd' && s[9] == 's' && s[10] == ':') {		SIZEDTEXT uf;		retain = NIL;	/* don't retain continuation */		s += 11;	/* flush leading whitespace */		while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n'))){		  while (*s == ' ') s++;				/* find end of keyword */		  if (!(u = strpbrk (s," \n\r"))) u = s + strlen (s);				/* got a keyword? */		  if ((k = (u - s)) && (k < MAXUSERFLAG)) {		    uf.data = (unsigned char *) s;		    uf.size = k;		    for (j = 0; (j < NUSERFLAGS) && stream->user_flags[j]; ++j)		      if (!compare_csizedtext (stream->user_flags[j],&uf)) {			elt->user_flags |= ((long) 1) << j;			break;		      }		  }		  s = u;	/* advance to next keyword */		}		break;	      }				/* possible X-IMAP */	      else if ((s[2] == 'I') && (s[3] == 'M') && (s[4] == 'A') &&		       (s[5] == 'P') && ((m = (s[6] == ':')) ||					 ((s[6] == 'b') && (s[7] == 'a') &&					  (s[8] == 's') && (s[9] == 'e') &&					  (s[10] == ':')))) {		retain = NIL;	/* don't retain continuation */		if ((nmsgs == 1) && !stream->uid_validity) {				/* advance to data */		  s += m ? 7 : 11;				/* flush whitespace */		  while (*s == ' ') s++;		  j = 0;	/* slurp UID validity */				/* found a digit? */		  while (isdigit (*s)) {		    j *= 10;	/* yes, add it in */		    j += *s++ - '0';		  }				/* flush whitespace */		  while (*s == ' ') s++;				/* must have valid UID validity and UID last */		  if (j && isdigit (*s)) {				/* pseudo-header seen if X-IMAP */		    if (m) pseudoseen = LOCAL->pseudo = T;				/* save UID validity */		    stream->uid_validity = j;		    j = 0;	/* slurp UID last */		    while (isdigit (*s)) {		      j *= 10;	/* yes, add it in */		      j += *s++ - '0';		    }				/* save UID last */		    stream->uid_last = j;				/* process keywords */		    for (j = 0; (*s != '\n') && ((*s != '\r')||(s[1] != '\n'));			 s = u,j++) {				/* flush leading whitespace */		      while (*s == ' ') s++;		      u = strpbrk (s," \n\r");				/* got a keyword? */		      if ((k = (u - s)) && j < NUSERFLAGS) {			if (stream->user_flags[j])			  fs_give ((void **) &stream->user_flags[j]);			stream->user_flags[j] = (char *) fs_get (k + 1);			strncpy (stream->user_flags[j],s,k);			stream->user_flags[j][k] = '\0';		      }		    }		  }		}		break;	      }				/* possible X-UID */	      else if (s[2] == 'U' && s[3] == 'I' && s[4] == 'D' &&		       s[5] == ':') {		retain = NIL;	/* don't retain continuation */				/* only believe if have a UID validity */		if (stream->uid_validity && ((nmsgs > 1) || !pseudoseen)) {		  s += 6;	/* advance to UID value */				/* flush whitespace */		  while (*s == ' ') s++;		  j = 0;				/* found a digit? */		  while (isdigit (*s)) {		    j *= 10;	/* yes, add it in */		    j += *s++ - '0';		  }				/* flush remainder of line */		  while (*s != '\n') s++;				/* make sure not duplicated */		  if (elt->private.uid)		    sprintf (tmp,"Message %lu UID %lu already has UID %lu",			     pseudoseen ? elt->msgno - 1 : elt->msgno,			     j,elt->private.uid);				/* make sure UID doesn't go backwards */		  else if (j <= prevuid)		    sprintf (tmp,"Message %lu UID %lu less than %lu",			     pseudoseen ? elt->msgno - 1 : elt->msgno,			     j,prevuid + 1);				/* or skip by mailbox's recorded last */		  else if (j > stream->uid_last)		    sprintf (tmp,"Message %lu UID %lu greater than last %lu",			     pseudoseen ? elt->msgno - 1 : elt->msgno,			     j,stream->uid_last);		  else {	/* normal UID case */		    prevuid = elt->private.uid = j;		    break;	/* exit this cruft */		  }		  mm_log (tmp,WARN);				/* invalidate UID validity */		  stream->uid_validity = 0;		  elt->private.uid = 0;		}		break;	      }	    }				/* otherwise fall into S case */	  case 'S':		/* possible Status: line */	    if (s[0] == 'S' && s[1] == 't' && s[2] == 'a' && s[3] == 't' &&		s[4] == 'u' && s[5] == 's' && s[6] == ':') {	      retain = NIL;	/* don't retain continuation */	      s += 6;		/* advance to status flags */	      do switch (*s++) {/* parse flags */	      case 'R':		/* message read */		elt->seen = T;		break;	      case 'O':		/* message old */		if (elt->recent) {		  elt->recent = NIL;		  recent--;	/* it really wasn't recent */		}		break;	      case 'D':		/* message deleted */		elt->deleted = T;		break;	      case 'F':		/* message flagged */		elt->flagged = T;		break;	      case 'A':		/* message answered */

⌨️ 快捷键说明

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