]>
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> | |
932028de | 8 | #include <moira.h> |
9 | #include <moira_site.h> | |
469f80df | 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 | |
1efc94dc | 43 | #define LEN_NAME 29 |
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) { | |
1efc94dc | 152 | e.first = p + 1; |
153 | while (*(e.first) && isspace(*(e.first))) | |
154 | (e.first)++; | |
469f80df | 155 | if (p = index(e.first, ' ')) { |
156 | *p = 0; | |
157 | e.middle = strtrim(p + 1); | |
158 | } else | |
159 | e.middle = ""; | |
160 | } else | |
161 | e.first = ""; | |
162 | e.first = strtrim(e.first); | |
4763be9c | 163 | ends_sr = ends_jr = ends_iii = ends_iv = 0; |
469f80df | 164 | LookForSt(e.last); |
165 | LookForO(e.last); | |
4763be9c | 166 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv); |
167 | LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv); | |
469f80df | 168 | FixCase(e.last); |
169 | FixCase(e.first); | |
170 | FixCase(e.middle); | |
171 | ||
172 | strncpy(mid, buf, LEN_ID); | |
173 | e.id = mid; | |
174 | e.eid = eid; | |
175 | EncryptID(e.eid, e.id, e.first, e.last); | |
176 | ||
177 | e.address = &buf[LOC_OFFICE]; | |
178 | e.address[LEN_OFFICE] = 0; | |
179 | e.address = strtrim(e.address); | |
180 | e.phone = phone; | |
181 | strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); | |
182 | e.phone = strtrim(e.phone); | |
183 | e.dept = &buf[LOC_DEPT]; | |
184 | e.dept[LEN_DEPT] = 0; | |
185 | e.dept = strtrim(e.dept); | |
186 | e.title = &buf[LOC_TITLE]; | |
187 | e.title[LEN_TITLE] = 0; | |
188 | e.title = strtrim(e.title); | |
189 | ||
190 | e.class = "MITS"; | |
191 | if (!strcmp(e.dept, "PROJECT ATHENA")) | |
192 | e.class = "STAFF"; | |
e1695a1c | 193 | else if (substr(e.title, "PROF") || substr(e.title, "LECTURE")) |
469f80df | 194 | e.class = "FACULTY"; |
195 | else if (!strcmp(e.title, "VISITING SCIENTIST")) | |
196 | e.class = "VSCIENTI"; | |
197 | ||
198 | strncpy(email, &buf[LOC_USERNAME], LEN_USERNAME); | |
199 | strtrim(email); | |
200 | if (buf[LOC_HOST] == '@') | |
201 | strncat(email, &buf[LOC_HOST], LEN_HOST); | |
202 | e.email = strtrim(email); | |
203 | ||
204 | return(&e); | |
205 | } | |
206 | ||
207 | ||
208 | process_entry(e) | |
209 | struct entry *e; | |
210 | ##{ | |
211 | int changed, nochange; | |
212 | char buf[BUFSIZ], *from, *to; | |
213 | ## char *first, *last, *eid; | |
214 | ## char class[9], oaddr[17], ophone[17], dept[17]; | |
215 | ## int id; | |
216 | ||
217 | first = e->first; | |
9e4455c2 | 218 | if (strlen(first) > 16) |
219 | first[16] = 0; | |
469f80df | 220 | last = e->last; |
9e4455c2 | 221 | if (strlen(last) > 16) |
222 | last[16] = 0; | |
469f80df | 223 | eid = e->eid; |
224 | id = 0; | |
225 | ## repeat retrieve (id = u.users_id, class = u.mit_year, oaddr = u.office_addr, | |
226 | ## ophone = u.office_phone, dept = u.mit_dept) | |
227 | ## where u.#last = @last and u.#first = @first and u.mit_id = @eid | |
228 | if (id == 0) { | |
229 | newuser(e); | |
230 | return; | |
231 | } | |
e1695a1c | 232 | if (strcmp(e->class, strtrim(class)) && |
233 | strcmp(class, "STAFF") && strcmp(class, "SIPB")) { | |
469f80df | 234 | com_err(whoami, 0, "updating class for %s %s from %s to %s", |
235 | first, last, class, e->class); | |
236 | strcpy(class, e->class); | |
237 | ## repeat replace u (mit_year = @class, | |
238 | ## modtime = "now", modby = WHO, modwith = PROG) | |
4763be9c | 239 | ## where u.users_id = @id |
469f80df | 240 | } |
241 | changed = nochange = 0; | |
242 | strcpy(buf, e->address); | |
243 | while (to = index(buf, ',')) | |
244 | *to = ';'; | |
245 | while (to = index(buf, ':')) | |
246 | *to = ';'; | |
247 | if (newfinger) { | |
248 | if (oaddr[0] == ' ') { | |
249 | strncpy(oaddr, buf, 16); | |
250 | oaddr[16] = 0; | |
251 | changed++; | |
6eb764c5 | 252 | } else if (strncmp(strtrim(oaddr), buf, 15)) |
469f80df | 253 | nochange++; |
254 | } else { | |
255 | if (strncmp(strtrim(oaddr), buf, 15)) | |
256 | changed++; | |
257 | strncpy(oaddr, buf, 16); | |
258 | oaddr[16] = 0; | |
259 | } | |
260 | from = e->phone; | |
261 | to = buf; | |
262 | while (*from) { | |
263 | if (isdigit(*from)) | |
264 | *to++ = *from; | |
265 | from++; | |
266 | } | |
267 | *to = 0; | |
268 | if (newfinger) { | |
269 | if (ophone[0] == ' ') { | |
270 | strncpy(ophone, buf, 16); | |
271 | ophone[16] = 0; | |
6eb764c5 | 272 | } else if (strncmp(strtrim(ophone), buf, 11)) |
469f80df | 273 | nochange++; |
274 | } else { | |
275 | if (strncmp(strtrim(ophone), buf, 11)) | |
276 | changed++; | |
277 | strncpy(ophone, buf, 16); | |
278 | ophone[16] = 0; | |
279 | } | |
280 | FixCase(e->dept); | |
281 | if (newfinger) { | |
282 | if (dept[0] == ' ') { | |
283 | strncpy(dept, e->dept, 12); | |
284 | dept[12] = 0; | |
6eb764c5 | 285 | } else if (strncmp(strtrim(dept), e->dept, 11)) |
469f80df | 286 | nochange++; |
287 | } else { | |
288 | if (strncmp(strtrim(dept), e->dept, 11)) | |
289 | changed++; | |
290 | strncpy(dept, e->dept, 12); | |
291 | dept[12] = 0; | |
292 | } | |
293 | if (changed) { | |
294 | com_err(whoami, 0, "updating finger for %s %s", first, last); | |
295 | ## repeat replace u (office_addr = @oaddr, | |
296 | ## office_phone = @ophone, #mit_dept = @dept, | |
297 | ## fmodtime = "now", fmodby = WHO, fmodwith = PROG) | |
298 | ## where u.users_id = @id | |
299 | } else if (nochange) | |
300 | com_err(whoami, 0, "NOT updating finger for %s %s", first, last); | |
301 | ##} | |
302 | ||
303 | ||
304 | newuser(e) | |
305 | struct entry *e; | |
306 | ##{ | |
307 | char *from, *to; | |
308 | ## int id, uid, st; | |
309 | ## char *last, *first, *class, *middle, login[9], *eid, fullname[65]; | |
310 | ## char oaddr[81], ophone[13], dept[13]; | |
311 | ||
312 | ||
313 | strncpy(oaddr, e->address, 16); | |
314 | while (to = index(oaddr, ',')) | |
315 | *to = ';'; | |
316 | while (to = index(oaddr, ':')) | |
317 | *to = ';'; | |
318 | from = e->phone; | |
319 | to = ophone; | |
320 | while (*from) { | |
321 | if (isdigit(*from)) | |
322 | *to++ = *from; | |
323 | from++; | |
324 | } | |
325 | *to = 0; | |
326 | strncpy(dept, e->dept, 12); | |
327 | ||
328 | id = set_next_object_id("users_id"); | |
329 | uid = set_next_object_id("uid"); | |
330 | sprintf(login, "#%d", uid); | |
331 | last = e->last; | |
332 | first = e->first; | |
333 | middle = e->middle; | |
334 | eid = e->eid; | |
335 | class = e->class; | |
336 | if (*middle) | |
337 | sprintf(fullname, "%s %s %s", first, middle, last); | |
338 | else | |
339 | sprintf(fullname, "%s %s", first, last); | |
340 | st = US_NOT_ALLOWED; | |
e1695a1c | 341 | if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF")) |
469f80df | 342 | st = US_NO_LOGIN_YET; |
343 | ||
344 | ## append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh", | |
345 | ## #last = last, #first = first, #middle = middle, status = st, | |
346 | ## #mit_id = eid, #mit_year = class, | |
347 | ## modtime = "now", modby = WHO, modwith = PROG, | |
348 | ## #fullname = fullname, office_addr = oaddr, | |
349 | ## office_phone = ophone, #mit_dept = dept, | |
350 | ## fmodtime = "now", fmodby = WHO, fmodwith = PROG, | |
351 | ## potype = "NONE") | |
352 | com_err(whoami, 0, "adding user %s %s", e->first, e->last); | |
353 | ##} | |
354 | ||
355 | ||
356 | set_next_object_id(object) | |
357 | char *object; | |
358 | ##{ | |
359 | ## char *name; | |
360 | ## int rowcount, exists, value; | |
361 | ||
362 | name = object; | |
363 | ## begin transaction | |
364 | ## repeat retrieve (value = values.#value) where values.#name = @name | |
365 | ## inquire_equel(rowcount = "rowcount") | |
366 | if (rowcount != 1) { | |
367 | ## abort | |
368 | return(0); | |
369 | } | |
370 | ||
371 | ## retrieve (exists = any(users.name where users.name = value)) | |
372 | ## inquire_equel(rowcount = "rowcount") | |
373 | if (rowcount != 1) { | |
374 | ## abort | |
375 | return(0); | |
376 | } | |
377 | while (exists) { | |
378 | value++; | |
379 | if (value > MAX_ID_VALUE) | |
380 | value = MIN_ID_VALUE; | |
381 | ## retrieve (exists = any(users.name where users.name = value)) | |
382 | } | |
383 | ||
384 | ## repeat replace values (#value = @value) where values.#name = @name | |
385 | ## end transaction | |
386 | return(value); | |
387 | ##} |