3 * This generates confirmation letters to people who recently set
4 * their secure instance passwords.
6 * (c) Copyright 1992 by the Massachusetts Institute of Technology.
7 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
14 #include <moira_site.h>
16 #include <sys/types.h>
20 EXEC SQL INCLUDE sqlca;
22 #define LETTER "letter.template"
25 char *whoami = "letter.gen";
33 char *outf = NULL, outft[64];
37 EXEC SQL BEGIN DECLARE SECTION;
38 char login[10], first[17], last[17], fullname[34], addr[84];
40 EXEC SQL END DECLARE SECTION;
43 EXEC SQL CONNECT moira;
46 EXEC SQL DATABASE moira;
50 if (stat(argv[1], &sb) == 0) {
51 if (ModDiff (&flag, "users", sb.st_mtime))
54 fprintf(stderr, "File %s does not need to be rebuilt.\n",
60 sprintf(outft, "%s~", outf);
61 if ((out = fopen(outft, "w")) == NULL) {
62 fprintf(stderr, "unable to open %s for output\n", outf);
65 } else if (argc != 1) {
66 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
74 /* The following is declarative, not executed,
75 * and so is dependent on where it is in the file,
76 * not in the order of execution of statements.
78 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
80 EXEC SQL SELECT secure INTO :lastrun FROM users WHERE users_id = 0;
82 gettimeofday(&tv, NULL);
84 EXEC SQL DECLARE x CURSOR FOR SELECT
85 login, first, last, xaddress, secure
86 FROM users WHERE secure > :lastrun;
89 EXEC SQL FETCH x INTO :login, :first, :last, :addr, :when;
90 if (sqlca.sqlcode != 0) break;
94 sprintf(fullname, "%s %s", first, last);
95 print_letter(out, login, fullname, strtrim(addr), when);
101 EXEC SQL UPDATE users SET secure = :lastrun WHERE users_id = 0;
107 EXEC SQL CLOSE DATABASE;
111 perror("close failed");
119 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
120 critical_alert("DCM", "Letter build encountered INGRES ERROR %d",
129 /* Read the template into a buffer */
137 sprintf(filename, "%s/%s", DCM_DIR, LETTER);
138 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
139 com_err(whoami, MR_OCONFIG, " opening letter template %s", filename);
142 if (fstat(fd, &stbuf)) {
143 com_err(whoami, MR_OCONFIG, " stating letter template %s", filename);
146 if ((letter_text = (char *)malloc(stbuf.st_size + 1)) == NULL) {
147 com_err(whoami, MR_NO_MEM, " reading letter template");
150 if (read(fd, letter_text, stbuf.st_size) < stbuf.st_size) {
151 com_err(whoami, MR_OCONFIG, " unable to read all of letter");
154 letter_text[stbuf.st_size] = 0;
159 /* Print out a copy of the letter */
161 print_letter(out, login, fullname, addr, when)
163 char *login, *fullname, *addr;
167 char buf[256], *a1, *a2, *d;
170 /* split address into two lines.
171 * First determine if postal or interdepartmental address.
172 * Postal addreses end in state & zip code, as in "MA02139"
173 * Anything else, assume interdepartmental.
177 a2 = rindex(buf, ' '); /* before state & zip */
178 if (a2 && isupper(a2[1]) && isupper(a2[2]) &&
179 isdigit(a2[3]) && isdigit(a2[4]) && isdigit(a2[5]) &&
180 isdigit(a2[6]) && isdigit(a2[7])) {
181 /* Must be postal address. */
182 /* missing space between state & zip, fix */
183 for (p = a2; *p; p++); /* find end of string */
184 for (p--; isdigit(*p); p--)
188 /* Backup to previous space, start of state */
189 for (; *a2 != ' '; a2--);
190 /* Backup to previous space, start of city */
191 for (a2--; *a2 != ' '; a2--);
192 /* terminate first line, leave a2 pointing at second */
195 /* Must be interdepartmental */
196 a2 = "MIT INTERDEPARTMENTAL MAIL";
201 for (p = letter_text; *p; p++) {
209 fputs(fullname, out);