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;
44 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
47 EXEC SQL DATABASE moira;
51 if (stat(argv[1], &sb) == 0) {
52 if (ModDiff (&flag, "users", sb.st_mtime))
55 fprintf(stderr, "File %s does not need to be rebuilt.\n",
61 sprintf(outft, "%s~", outf);
62 if ((out = fopen(outft, "w")) == NULL) {
63 fprintf(stderr, "unable to open %s for output\n", outf);
66 } else if (argc != 1) {
67 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
75 /* The following is declarative, not executed,
76 * and so is dependent on where it is in the file,
77 * not in the order of execution of statements.
79 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
81 EXEC SQL SELECT secure INTO :lastrun FROM users WHERE users_id = 0;
83 gettimeofday(&tv, NULL);
85 EXEC SQL DECLARE x CURSOR FOR SELECT
86 login, first, last, xaddress, secure
87 FROM users WHERE secure > :lastrun;
90 EXEC SQL FETCH x INTO :login, :first, :last, :addr, :when;
91 if (sqlca.sqlcode != 0) break;
95 sprintf(fullname, "%s %s", first, last);
96 print_letter(out, login, fullname, strtrim(addr), when);
102 EXEC SQL UPDATE users SET secure = :lastrun WHERE users_id = 0;
108 EXEC SQL CLOSE DATABASE;
112 perror("close failed");
121 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
122 critical_alert("DCM", "Letter build encountered INGRES ERROR %d",
131 /* Read the template into a buffer */
139 sprintf(filename, "%s/%s", DCM_DIR, LETTER);
140 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
141 com_err(whoami, MR_OCONFIG, " opening letter template %s", filename);
144 if (fstat(fd, &stbuf)) {
145 com_err(whoami, MR_OCONFIG, " stating letter template %s", filename);
148 if ((letter_text = (char *)malloc(stbuf.st_size + 1)) == NULL) {
149 com_err(whoami, MR_NO_MEM, " reading letter template");
152 if (read(fd, letter_text, stbuf.st_size) < stbuf.st_size) {
153 com_err(whoami, MR_OCONFIG, " unable to read all of letter");
156 letter_text[stbuf.st_size] = 0;
161 /* Print out a copy of the letter */
163 print_letter(out, login, fullname, addr, when)
165 char *login, *fullname, *addr;
169 char buf[256], *a1, *a2, *d;
170 char *rindex(), *index();
172 /* split address into two lines.
173 * First determine if postal or interdepartmental address.
174 * Postal addreses end in state & zip code, as in "MA02139"
175 * Anything else, assume interdepartmental.
179 if (a2 = index(buf, '|')) {
182 a2 = rindex(buf, ' '); /* before state & zip */
183 if (a2 && isupper(a2[1]) && isupper(a2[2]) &&
184 isdigit(a2[3]) && isdigit(a2[4]) && isdigit(a2[5]) &&
185 isdigit(a2[6]) && isdigit(a2[7])) {
186 /* Must be postal address. */
187 /* missing space between state & zip, fix */
188 for (p = a2; *p; p++); /* find end of string */
189 for (p--; isdigit(*p); p--)
193 /* Backup to previous space, start of state */
194 for (; *a2 != ' '; a2--);
195 /* Backup to previous space, start of city */
196 for (a2--; *a2 != ' '; a2--);
197 /* terminate first line, leave a2 pointing at second */
200 /* Must be interdepartmental */
201 a2 = "MIT INTERDEPARTMENTAL MAIL";
208 for (p = letter_text; *p; p++) {
216 fputs(fullname, out);