]>
Commit | Line | Data |
---|---|---|
1 | /* $Header$ | |
2 | * | |
3 | * This generates confirmation letters to people who recently set | |
4 | * their secure instance passwords. | |
5 | * | |
6 | * (c) Copyright 1992 by the Massachusetts Institute of Technology. | |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
9 | */ | |
10 | ||
11 | #include <mit-copyright.h> | |
12 | #include <stdio.h> | |
13 | #include <moira.h> | |
14 | #include <moira_site.h> | |
15 | #include <ctype.h> | |
16 | #include <sys/types.h> | |
17 | #include <sys/stat.h> | |
18 | #include <sys/time.h> | |
19 | #include <sys/file.h> | |
20 | EXEC SQL INCLUDE sqlca; | |
21 | ||
22 | #define LETTER "letter.template" | |
23 | ||
24 | extern int errno; | |
25 | char *whoami = "letter.gen"; | |
26 | ||
27 | ||
28 | main(argc, argv) | |
29 | int argc; | |
30 | char **argv; | |
31 | { | |
32 | FILE *out = stdout; | |
33 | char *outf = NULL, outft[64]; | |
34 | struct stat sb; | |
35 | int flag; | |
36 | struct timeval tv; | |
37 | EXEC SQL BEGIN DECLARE SECTION; | |
38 | char login[10], first[17], last[17], fullname[34], addr[84]; | |
39 | int lastrun, when; | |
40 | EXEC SQL END DECLARE SECTION; | |
41 | ||
42 | #ifsql INGRES | |
43 | EXEC SQL CONNECT sms; | |
44 | #endsql | |
45 | #ifsql INFORMIX | |
46 | EXEC SQL DATABASE sms; | |
47 | #endsql | |
48 | ||
49 | if (argc == 2) { | |
50 | if (stat(argv[1], &sb) == 0) { | |
51 | if (ModDiff (&flag, "users", sb.st_mtime)) | |
52 | exit(MR_DATE); | |
53 | if (flag < 0) { | |
54 | fprintf(stderr, "File %s does not need to be rebuilt.\n", | |
55 | argv[1]); | |
56 | exit(MR_NO_CHANGE); | |
57 | } | |
58 | } | |
59 | outf = argv[1]; | |
60 | sprintf(outft, "%s~", outf); | |
61 | if ((out = fopen(outft, "w")) == NULL) { | |
62 | fprintf(stderr, "unable to open %s for output\n", outf); | |
63 | exit(MR_OCONFIG); | |
64 | } | |
65 | } else if (argc != 1) { | |
66 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
67 | exit(MR_ARGS); | |
68 | } else { | |
69 | outf = NULL; | |
70 | } | |
71 | ||
72 | init_letter(); | |
73 | ||
74 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
75 | ||
76 | EXEC SQL SELECT secure INTO :lastrun FROM users WHERE users_id = 0; | |
77 | ||
78 | gettimeofday(&tv, NULL); | |
79 | ||
80 | EXEC SQL DECLARE x CURSOR FOR SELECT | |
81 | login, first, last, xaddress, secure | |
82 | FROM users WHERE secure > :lastrun; | |
83 | EXEC SQL OPEN x; | |
84 | while (1) { | |
85 | EXEC SQL FETCH x INTO :login, :first, :last, :addr, :when; | |
86 | if (sqlca.sqlcode != 0) break; | |
87 | strtrim(login); | |
88 | strtrim(first); | |
89 | strtrim(last); | |
90 | sprintf(fullname, "%s %s", first, last); | |
91 | print_letter(out, login, fullname, strtrim(addr), when); | |
92 | } | |
93 | ||
94 | EXEC SQL CLOSE x; | |
95 | ||
96 | lastrun = tv.tv_sec; | |
97 | EXEC SQL UPDATE users SET secure = :lastrun WHERE users_id = 0; | |
98 | ||
99 | #ifsql INGRES | |
100 | EXEC SQL DISCONNECT; | |
101 | #endsql | |
102 | #ifsql INFORMIX | |
103 | EXEC SQL CLOSE DATABASE; | |
104 | #endsql | |
105 | ||
106 | if (fclose(out)) { | |
107 | perror("close failed"); | |
108 | exit(MR_CCONFIG); | |
109 | } | |
110 | if (outf) | |
111 | fix_file(outf); | |
112 | exit(MR_SUCCESS); | |
113 | ||
114 | sqlerr: | |
115 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
116 | critical_alert("DCM", "Letter build encountered INGRES ERROR %d", | |
117 | sqlca.sqlcode); | |
118 | exit(MR_INGRES_ERR); | |
119 | } | |
120 | ||
121 | ||
122 | char *letter_text; | |
123 | ||
124 | ||
125 | /* Read the template into a buffer */ | |
126 | ||
127 | init_letter() | |
128 | { | |
129 | int fd; | |
130 | char filename[256]; | |
131 | struct stat stbuf; | |
132 | ||
133 | sprintf(filename, "%s/%s", DCM_DIR, LETTER); | |
134 | if ((fd = open(filename, O_RDONLY, 0)) < 0) { | |
135 | com_err(whoami, MR_OCONFIG, " opening letter template %s", filename); | |
136 | exit(MR_OCONFIG); | |
137 | } | |
138 | if (fstat(fd, &stbuf)) { | |
139 | com_err(whoami, MR_OCONFIG, " stating letter template %s", filename); | |
140 | exit(MR_OCONFIG); | |
141 | } | |
142 | if ((letter_text = (char *)malloc(stbuf.st_size + 1)) == NULL) { | |
143 | com_err(whoami, MR_NO_MEM, " reading letter template"); | |
144 | exit(MR_NO_MEM); | |
145 | } | |
146 | if (read(fd, letter_text, stbuf.st_size) < stbuf.st_size) { | |
147 | com_err(whoami, MR_OCONFIG, " unable to read all of letter"); | |
148 | exit(MR_OCONFIG); | |
149 | } | |
150 | letter_text[stbuf.st_size] = 0; | |
151 | close(fd); | |
152 | } | |
153 | ||
154 | ||
155 | /* Print out a copy of the letter */ | |
156 | ||
157 | print_letter(out, login, fullname, addr, when) | |
158 | FILE *out; | |
159 | char *login, *fullname, *addr; | |
160 | int when; | |
161 | { | |
162 | register char *p; | |
163 | char buf[256], *a1, *a2, *d; | |
164 | char *rindex(); | |
165 | ||
166 | /* split address into two lines. | |
167 | * First determine if postal or interdepartmental address. | |
168 | * Postal addreses end in state & zip code, as in "MA02139" | |
169 | * Anything else, assume interdepartmental. | |
170 | */ | |
171 | strcpy(buf, addr); | |
172 | a1 = buf; | |
173 | a2 = rindex(buf, ' '); /* before state & zip */ | |
174 | if (a2 && isupper(a2[1]) && isupper(a2[2]) && | |
175 | isdigit(a2[3]) && isdigit(a2[4]) && isdigit(a2[5]) && | |
176 | isdigit(a2[6]) && isdigit(a2[7])) { | |
177 | /* Must be postal address. */ | |
178 | /* missing space between state & zip, fix */ | |
179 | for (p = a2; *p; p++); /* find end of string */ | |
180 | for (p--; isdigit(*p); p--) | |
181 | p[2] = p[1]; | |
182 | p[2] = p[1]; | |
183 | p[1] = ' '; | |
184 | /* Backup to previous space, start of state */ | |
185 | for (; *a2 != ' '; a2--); | |
186 | /* Backup to previous space, start of city */ | |
187 | for (a2--; *a2 != ' '; a2--); | |
188 | /* terminate first line, leave a2 pointing at second */ | |
189 | *a2++ = 0; | |
190 | } else { | |
191 | /* Must be interdepartmental */ | |
192 | a2 = "MIT INTERDEPARTMENTAL MAIL"; | |
193 | } | |
194 | d = ctime(&when); | |
195 | d[strlen(d)-1] = 0; | |
196 | ||
197 | for (p = letter_text; *p; p++) { | |
198 | if (*p == '%') { | |
199 | p++; | |
200 | switch (*p) { | |
201 | case 'l': | |
202 | fputs(login, out); | |
203 | break; | |
204 | case 'n': | |
205 | fputs(fullname, out); | |
206 | break; | |
207 | case 'A': | |
208 | fputs(addr, out); | |
209 | break; | |
210 | case 'a': | |
211 | p++; | |
212 | switch (*p) { | |
213 | case '1': | |
214 | fputs(a1, out); | |
215 | break; | |
216 | case '2': | |
217 | fputs(a2, out); | |
218 | break; | |
219 | default: | |
220 | fputs(addr, out); | |
221 | } | |
222 | break; | |
223 | case 'd': | |
224 | fputs(d, out); | |
225 | break; | |
226 | default: | |
227 | putc('%', out); | |
228 | putc(*p, out); | |
229 | } | |
230 | } else | |
231 | putc(*p, out); | |
232 | } | |
233 | fflush(out); | |
234 | } |