]>
Commit | Line | Data |
---|---|---|
469f80df | 1 | /* $Header$ |
2 | */ | |
3 | ||
4 | #include <stdio.h> | |
5 | #include <strings.h> | |
6 | #include <ctype.h> | |
7 | #include <sys/time.h> | |
8 | #include <sms.h> | |
9 | #include <sms_app.h> | |
10 | ||
11 | ||
12 | ##define WHO 11859 /* root */ | |
13 | ##define PROG "emp-tape" | |
14 | ||
15 | #define MAX_ID_VALUE 32766 | |
16 | #define MIN_ID_VALUE 101 | |
17 | ||
18 | /* File format is: | |
19 | ||
20 | 0-8 id number | |
21 | 9-38 name | |
22 | 39-62 office address | |
23 | 63-74 phone1 | |
24 | 75-86 phone2 | |
25 | 87-106 dept | |
26 | 107-156 title | |
27 | 157-186 username | |
28 | 187-241 host | |
29 | ||
30 | */ | |
31 | ||
32 | #define LOC_ID 0 | |
33 | #define LOC_NAME 9 | |
34 | #define LOC_OFFICE 39 | |
35 | #define LOC_PHONE 63 | |
36 | #define LOC_PHONE2 75 | |
37 | #define LOC_DEPT 87 | |
38 | #define LOC_TITLE 107 | |
39 | #define LOC_USERNAME 157 | |
40 | #define LOC_HOST 187 | |
41 | ||
42 | #define LEN_ID 9 | |
43 | #define LEN_NAME 30 | |
4763be9c | 44 | #define LEN_OFFICE 12 |
469f80df | 45 | #define LEN_PHONE 12 |
46 | #define LEN_PHONE2 12 | |
47 | #define LEN_DEPT 19 | |
48 | #define LEN_TITLE 49 | |
49 | #define LEN_USERNAME 29 | |
50 | #define LEN_HOST 55 | |
51 | ||
52 | ||
53 | struct entry { | |
54 | char *last; | |
55 | char *first; | |
56 | char *middle; | |
57 | char *title; | |
58 | char *class; | |
59 | char *id; | |
60 | char *eid; | |
61 | char *dept; | |
62 | char *address; | |
63 | char *phone; | |
64 | char *email; | |
65 | }; | |
66 | ||
67 | ||
68 | char *whoami; | |
69 | int newfinger = 0; | |
70 | ||
71 | ||
72 | main(argc, argv) | |
73 | int argc; | |
74 | char **argv; | |
75 | ##{ | |
76 | FILE *in; | |
77 | struct entry *e, *get_next_entry(); | |
78 | int i, wait = 0; | |
79 | char buf[BUFSIZ], *file = NULL; | |
80 | ||
81 | whoami = rindex(argv[0], '/'); | |
82 | if (whoami) | |
83 | whoami++; | |
84 | else | |
85 | whoami = argv[0]; | |
86 | ||
87 | for (i = 1; i < argc; i++) { | |
88 | if (!strcmp(argv[i], "-w")) | |
89 | wait++; | |
90 | else if (!strcmp(argv[i], "-D")) | |
91 | setenv("ING_SET", "set printqry"); | |
92 | else if (!strcmp(argv[i], "-n")) | |
93 | newfinger++; | |
94 | else if (file != NULL) | |
95 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami); | |
96 | else | |
97 | file = argv[i]; | |
98 | } | |
99 | ||
100 | in = fopen(file, "r"); | |
101 | if (in == NULL) { | |
102 | fprintf(stderr, "Unable to open %s for input\n", file); | |
103 | exit(1); | |
104 | } | |
105 | ||
106 | ## ingres sms | |
107 | ## range of u is users | |
108 | ||
109 | while (e = get_next_entry(in)) { | |
110 | process_entry(e); | |
111 | if (wait) { | |
112 | printf("Next"); | |
113 | fflush(stdout); | |
114 | gets(buf); | |
115 | } | |
116 | } | |
117 | ||
118 | ## exit | |
119 | exit(0); | |
120 | ##} | |
121 | ||
122 | ||
123 | char *substr(buf, key) | |
124 | char *buf; | |
125 | char *key; | |
126 | { | |
127 | int l; | |
128 | ||
129 | for (l = strlen(key); *buf; buf++) | |
130 | if (!strncmp(buf, key, l)) | |
131 | return(buf); | |
132 | return(NULL); | |
133 | } | |
134 | ||
135 | ||
136 | struct entry *get_next_entry(in) | |
137 | FILE *in; | |
138 | { | |
139 | static struct entry e; | |
140 | static char buf[BUFSIZ], mid[16], eid[16], email[256], phone[13]; | |
4763be9c | 141 | int ends_sr, ends_jr, ends_iii, ends_iv; |
469f80df | 142 | char *p; |
143 | ||
4763be9c | 144 | if (fgets(buf, sizeof(buf), in) == NULL) |
145 | return((struct entry *)NULL); | |
e1695a1c | 146 | buf[LOC_NAME + LEN_NAME] = 0; |
469f80df | 147 | p = index(&buf[LOC_NAME], ','); |
148 | if (p) | |
149 | *p = 0; | |
150 | e.last = strtrim(&buf[LOC_NAME]); | |
151 | if (p) { | |
152 | e.first = p + 3; | |
153 | if (p = index(e.first, ' ')) { | |
154 | *p = 0; | |
155 | e.middle = strtrim(p + 1); | |
156 | } else | |
157 | e.middle = ""; | |
158 | } else | |
159 | e.first = ""; | |
160 | e.first = strtrim(e.first); | |
4763be9c | 161 | ends_sr = ends_jr = ends_iii = ends_iv = 0; |
469f80df | 162 | LookForSt(e.last); |
163 | LookForO(e.last); | |
4763be9c | 164 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv); |
165 | LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv); | |
469f80df | 166 | FixCase(e.last); |
167 | FixCase(e.first); | |
168 | FixCase(e.middle); | |
169 | ||
170 | strncpy(mid, buf, LEN_ID); | |
171 | e.id = mid; | |
172 | e.eid = eid; | |
173 | EncryptID(e.eid, e.id, e.first, e.last); | |
174 | ||
175 | e.address = &buf[LOC_OFFICE]; | |
176 | e.address[LEN_OFFICE] = 0; | |
177 | e.address = strtrim(e.address); | |
178 | e.phone = phone; | |
179 | strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); | |
180 | e.phone = strtrim(e.phone); | |
181 | e.dept = &buf[LOC_DEPT]; | |
182 | e.dept[LEN_DEPT] = 0; | |
183 | e.dept = strtrim(e.dept); | |
184 | e.title = &buf[LOC_TITLE]; | |
185 | e.title[LEN_TITLE] = 0; | |
186 | e.title = strtrim(e.title); | |
187 | ||
188 | e.class = "MITS"; | |
189 | if (!strcmp(e.dept, "PROJECT ATHENA")) | |
190 | e.class = "STAFF"; | |
e1695a1c | 191 | else if (substr(e.title, "PROF") || substr(e.title, "LECTURE")) |
469f80df | 192 | e.class = "FACULTY"; |
193 | else if (!strcmp(e.title, "VISITING SCIENTIST")) | |
194 | e.class = "VSCIENTI"; | |
195 | ||
196 | strncpy(email, &buf[LOC_USERNAME], LEN_USERNAME); | |
197 | strtrim(email); | |
198 | if (buf[LOC_HOST] == '@') | |
199 | strncat(email, &buf[LOC_HOST], LEN_HOST); | |
200 | e.email = strtrim(email); | |
201 | ||
202 | return(&e); | |
203 | } | |
204 | ||
205 | ||
206 | process_entry(e) | |
207 | struct entry *e; | |
208 | ##{ | |
209 | int changed, nochange; | |
210 | char buf[BUFSIZ], *from, *to; | |
211 | ## char *first, *last, *eid; | |
212 | ## char class[9], oaddr[17], ophone[17], dept[17]; | |
213 | ## int id; | |
214 | ||
215 | first = e->first; | |
9e4455c2 | 216 | if (strlen(first) > 16) |
217 | first[16] = 0; | |
469f80df | 218 | last = e->last; |
9e4455c2 | 219 | if (strlen(last) > 16) |
220 | last[16] = 0; | |
469f80df | 221 | eid = e->eid; |
222 | id = 0; | |
223 | ## repeat retrieve (id = u.users_id, class = u.mit_year, oaddr = u.office_addr, | |
224 | ## ophone = u.office_phone, dept = u.mit_dept) | |
225 | ## where u.#last = @last and u.#first = @first and u.mit_id = @eid | |
226 | if (id == 0) { | |
227 | newuser(e); | |
228 | return; | |
229 | } | |
e1695a1c | 230 | if (strcmp(e->class, strtrim(class)) && |
231 | strcmp(class, "STAFF") && strcmp(class, "SIPB")) { | |
469f80df | 232 | com_err(whoami, 0, "updating class for %s %s from %s to %s", |
233 | first, last, class, e->class); | |
234 | strcpy(class, e->class); | |
235 | ## repeat replace u (mit_year = @class, | |
236 | ## modtime = "now", modby = WHO, modwith = PROG) | |
4763be9c | 237 | ## where u.users_id = @id |
469f80df | 238 | } |
239 | changed = nochange = 0; | |
240 | strcpy(buf, e->address); | |
241 | while (to = index(buf, ',')) | |
242 | *to = ';'; | |
243 | while (to = index(buf, ':')) | |
244 | *to = ';'; | |
245 | if (newfinger) { | |
246 | if (oaddr[0] == ' ') { | |
247 | strncpy(oaddr, buf, 16); | |
248 | oaddr[16] = 0; | |
249 | changed++; | |
6eb764c5 | 250 | } else if (strncmp(strtrim(oaddr), buf, 15)) |
469f80df | 251 | nochange++; |
252 | } else { | |
253 | if (strncmp(strtrim(oaddr), buf, 15)) | |
254 | changed++; | |
255 | strncpy(oaddr, buf, 16); | |
256 | oaddr[16] = 0; | |
257 | } | |
258 | from = e->phone; | |
259 | to = buf; | |
260 | while (*from) { | |
261 | if (isdigit(*from)) | |
262 | *to++ = *from; | |
263 | from++; | |
264 | } | |
265 | *to = 0; | |
266 | if (newfinger) { | |
267 | if (ophone[0] == ' ') { | |
268 | strncpy(ophone, buf, 16); | |
269 | ophone[16] = 0; | |
6eb764c5 | 270 | } else if (strncmp(strtrim(ophone), buf, 11)) |
469f80df | 271 | nochange++; |
272 | } else { | |
273 | if (strncmp(strtrim(ophone), buf, 11)) | |
274 | changed++; | |
275 | strncpy(ophone, buf, 16); | |
276 | ophone[16] = 0; | |
277 | } | |
278 | FixCase(e->dept); | |
279 | if (newfinger) { | |
280 | if (dept[0] == ' ') { | |
281 | strncpy(dept, e->dept, 12); | |
282 | dept[12] = 0; | |
6eb764c5 | 283 | } else if (strncmp(strtrim(dept), e->dept, 11)) |
469f80df | 284 | nochange++; |
285 | } else { | |
286 | if (strncmp(strtrim(dept), e->dept, 11)) | |
287 | changed++; | |
288 | strncpy(dept, e->dept, 12); | |
289 | dept[12] = 0; | |
290 | } | |
291 | if (changed) { | |
292 | com_err(whoami, 0, "updating finger for %s %s", first, last); | |
293 | ## repeat replace u (office_addr = @oaddr, | |
294 | ## office_phone = @ophone, #mit_dept = @dept, | |
295 | ## fmodtime = "now", fmodby = WHO, fmodwith = PROG) | |
296 | ## where u.users_id = @id | |
297 | } else if (nochange) | |
298 | com_err(whoami, 0, "NOT updating finger for %s %s", first, last); | |
299 | ##} | |
300 | ||
301 | ||
302 | newuser(e) | |
303 | struct entry *e; | |
304 | ##{ | |
305 | char *from, *to; | |
306 | ## int id, uid, st; | |
307 | ## char *last, *first, *class, *middle, login[9], *eid, fullname[65]; | |
308 | ## char oaddr[81], ophone[13], dept[13]; | |
309 | ||
310 | ||
311 | strncpy(oaddr, e->address, 16); | |
312 | while (to = index(oaddr, ',')) | |
313 | *to = ';'; | |
314 | while (to = index(oaddr, ':')) | |
315 | *to = ';'; | |
316 | from = e->phone; | |
317 | to = ophone; | |
318 | while (*from) { | |
319 | if (isdigit(*from)) | |
320 | *to++ = *from; | |
321 | from++; | |
322 | } | |
323 | *to = 0; | |
324 | strncpy(dept, e->dept, 12); | |
325 | ||
326 | id = set_next_object_id("users_id"); | |
327 | uid = set_next_object_id("uid"); | |
328 | sprintf(login, "#%d", uid); | |
329 | last = e->last; | |
330 | first = e->first; | |
331 | middle = e->middle; | |
332 | eid = e->eid; | |
333 | class = e->class; | |
334 | if (*middle) | |
335 | sprintf(fullname, "%s %s %s", first, middle, last); | |
336 | else | |
337 | sprintf(fullname, "%s %s", first, last); | |
338 | st = US_NOT_ALLOWED; | |
e1695a1c | 339 | if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF")) |
469f80df | 340 | st = US_NO_LOGIN_YET; |
341 | ||
342 | ## append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh", | |
343 | ## #last = last, #first = first, #middle = middle, status = st, | |
344 | ## #mit_id = eid, #mit_year = class, | |
345 | ## modtime = "now", modby = WHO, modwith = PROG, | |
346 | ## #fullname = fullname, office_addr = oaddr, | |
347 | ## office_phone = ophone, #mit_dept = dept, | |
348 | ## fmodtime = "now", fmodby = WHO, fmodwith = PROG, | |
349 | ## potype = "NONE") | |
350 | com_err(whoami, 0, "adding user %s %s", e->first, e->last); | |
351 | ##} | |
352 | ||
353 | ||
354 | set_next_object_id(object) | |
355 | char *object; | |
356 | ##{ | |
357 | ## char *name; | |
358 | ## int rowcount, exists, value; | |
359 | ||
360 | name = object; | |
361 | ## begin transaction | |
362 | ## repeat retrieve (value = values.#value) where values.#name = @name | |
363 | ## inquire_equel(rowcount = "rowcount") | |
364 | if (rowcount != 1) { | |
365 | ## abort | |
366 | return(0); | |
367 | } | |
368 | ||
369 | ## retrieve (exists = any(users.name where users.name = value)) | |
370 | ## inquire_equel(rowcount = "rowcount") | |
371 | if (rowcount != 1) { | |
372 | ## abort | |
373 | return(0); | |
374 | } | |
375 | while (exists) { | |
376 | value++; | |
377 | if (value > MAX_ID_VALUE) | |
378 | value = MIN_ID_VALUE; | |
379 | ## retrieve (exists = any(users.name where users.name = value)) | |
380 | } | |
381 | ||
382 | ## repeat replace values (#value = @value) where values.#name = @name | |
383 | ## end transaction | |
384 | return(value); | |
385 | ##} |