|
|
1.1 root 1: #include "decl.h"
2:
3: /* udaemon: do the processing for a single user */
4:
5: /* Does this string represent the name of a status file? */
6: static int
7: isstat (String s)
8: {
9: if (s.length() <= 2)
10: return 0;
11: return String (s (s.length()-2, 2)) == ".s";
12: }
13:
14: /* Return the data file name corresponding to this string */
15: static String
16: datafile (String s)
17: {
18: return String (s(0,s.length()-2)) + ".d";
19: }
20:
21: /* Read a status file and return its contents */
22: static map
23: readstatus (String s)
24: {
25: map m;
26: char *buf = new char[s.length() + 1];
27: s.dump (buf);
28: filebuf fb;
29: if (fb.open (buf, input)) {
30: istream i (&fb);
31: i >> m;
32: }
33: delete buf;
34: return m;
35: }
36:
37: /* Rewrite a map into a status file */
38: static void
39: writestatus (Path file, map m)
40: {
41: String tempfile = file.rmlast() & "stemp";
42: char *buf = new char[tempfile.length() + 1];
43: tempfile.dump (buf);
44: filebuf fb;
45: if (fb.open (buf, output)) {
46: ostream o (&fb);
47: o << m;
48: o.flush();
49: if (o) {
50: unlink (String(file));
51: link (String(tempfile), String(file));
52: unlink (tempfile);
53: }
54: }
55: delete buf;
56: }
57:
58: static int
59: listsearch (String_list sl, String s)
60: {
61: String_list_iterator i (sl);
62: String t;
63: while (i.nextX (t)) {
64: if (s == t)
65: return 1;
66: }
67: return 0;
68: }
69:
70: String_list deadmach;
71:
72: /* Do all the work for a status file */
73: static void
74: dofile (Path sfile, Path dfile)
75: {
76: map statmap = readstatus (sfile);
77: mel_list_iterator mli (statmap.l);
78: mel m;
79: int keepfile = 0, sendfile = 0;
80: while (mli.nextX (m)) {
81: if (m.p.strchr('#') < 0 &&
82: (m.q.length() == 0 || m.s.length() != 0)) {
83: m.q = String_list();
84: if (listsearch (deadmach, m.p)) {
85: keepfile = 1;
86: m.s = "deferred for sequence";
87: mli.replace (m);
88: } else {
89: m.s = domach (m.p, dfile, m.q);
90: if (m.s.length() == 0) {
91: sendfile = 1;
92: if (m.q.length() == 0)
93: mli.remove();
94: else
95: mli.replace (m);
96: } else {
97: keepfile = 1;
98: deadmach += m.p;
99: mli.replace (m);
100: }
101: }
102: }
103: writestatus (sfile, statmap);
104: }
105:
106: int rc = 0;
107:
108: if (sendfile || !keepfile) {
109: rc = system ("mail " + logname() + " <" + String(sfile));
110: }
111:
112: if (!keepfile && rc == 0) {
113: unlink (String(sfile));
114: unlink (String(dfile));
115: }
116: }
117:
118: main()
119: {
120: String ln = logname();
121: Path myspool = spooldir & ln;
122:
123: umask (022);
124: lock (myspool);
125:
126: Path_list files = dircontents (myspool);
127: Path_list_iterator pli (files);
128: Path f;
129:
130: while (pli.nextX (f)) {
131: if (isstat (f))
132: dofile (myspool & f, myspool & datafile(f));
133: }
134: unlock (myspool);
135: return 0;
136: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.