1 /*$Id: ezmlm-limit.c,v 1.2 1999/10/31 18:58:48 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
19 #define FATAL "ezmlm-limit: fatal: "
20 #define INFO "ezmlm-limit: info: "
22 unsigned long deltasecs = LIMSECS; /* interval to test over (seconds) */
23 unsigned long deltanum = LIMMSG; /* max no messages in interval */
24 /* see idx.h. Usually 30 msg/3600 secs*/
25 int flagd = 0; /* =0 create modpost, =1 ignore */
26 /* excess, =2 defer excess */
27 int flagmod; /* list moderated */
29 char *fn = TXT_LOOPNUM;
34 "ezmlm-limit: usage: ezmlm-limit [-f file] [-dDF] [-n messages]"
38 void die_nomem() { strerr_die2x(111,FATAL,ERR_NOMEM); }
40 void die_new() { strerr_die4sys(111,FATAL,ERR_WRITE,fn,": "); }
47 char strnum[FMT_ULONG];
56 unsigned long num, loopnum, when;
57 unsigned long loopwhen = 0L;
58 unsigned long numwhen = 0L;
63 when = (unsigned long) now();
65 while ((opt = getopt(argc,argv,"dDf:Fn:t:")) != opteof)
67 case 'd': flagd = 1; break;
68 case 'D': flagd = 0; break;
69 case 'f': if (optarg && *optarg) fn = optarg; break;
70 case 'F': fn = TXT_LOOPNUM;
73 scan_ulong(optarg,&deltanum);
77 scan_ulong(optarg,&deltasecs);
84 if (!dir) die_usage();
87 strerr_die4sys(111,FATAL,ERR_SWITCH,dir,": ");
90 die_usage(); /* avoid common error of putting options after dir */
91 if ((flagmod = getconf_line(&line,"modpost",0,FATAL,dir)))
92 _exit(0); /* already mod */
93 /* lock for num and for writing loopnum */
94 fdlock = open_append("lock");
96 strerr_die4sys(111,FATAL,ERR_OPEN,dir,"/lock: ");
97 if (lock_ex(fdlock) == -1)
98 strerr_die4sys(111,FATAL,ERR_OBTAIN,dir,"/lock: ");
100 if (!getconf_line(&line,"num",0,FATAL,dir))
101 _exit(99); /* no msgs */
102 if(!stralloc_0(&line)) die_nomem();
103 pos = scan_ulong(line.s,&num); /* current msg */
104 if ((flagloop = getconf_line(&line,fn,0,FATAL,dir))) {
105 if(!stralloc_0(&line)) die_nomem();
106 pos = scan_ulong(line.s,&loopnum); /* msg when written */
107 if (line.s[pos] == ':')
108 scan_ulong(line.s+pos+1,&loopwhen); /* time written */
110 if (!flagloop || loopwhen + deltasecs < when || loopwhen > when) {
111 /* loopnum too old, bad or not there */
112 fd = open_trunc(fn); /* no need to write crash-proof */
113 if (fd == -1) die_new();
114 substdio_fdbuf(&ssnew,write,fd,newbuf,sizeof(newbuf));
115 if (substdio_put(&ssnew,strnum,fmt_ulong(strnum,num)) == -1) die_new();
116 if (substdio_puts(&ssnew,":") == -1) die_new();
117 if (substdio_put(&ssnew,strnum,fmt_ulong(strnum,when)) == -1) die_new();
118 if (substdio_puts(&ssnew,"\n") == -1) die_new();
119 if (substdio_flush(&ssnew) == -1) die_new();
121 } else if (num >= loopnum + deltanum) { /* excess messages */
123 if ((fd = open_append("modpost")) == -1) /* create dir/modpost */
124 strerr_die3sys(111,FATAL,ERR_WRITE,"subpost:");
128 strerr_die2x(0,INFO,ERR_EXCESS_MOD);
131 strerr_die2x(111,FATAL,ERR_EXCESS_DEFER);