]> andersk Git - moira.git/blame_incremental - regtape/employee.dc
don't range limit users_id's when assigning new ones
[moira.git] / regtape / employee.dc
... / ...
CommitLineData
1/* $Header$
2 */
3
4#include <stdio.h>
5#include <strings.h>
6#include <ctype.h>
7#include <sys/time.h>
8#include <moira.h>
9#include <moira_site.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
200-8 id number
219-38 name
2239-62 office address
2363-74 phone1
2475-86 phone2
2587-106 dept
26107-156 title
27157-186 username
28187-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
44#define LEN_OFFICE 24
45#define LEN_PHONE 12
46#define LEN_PHONE2 12
47#define LEN_DEPT 20
48#define LEN_TITLE 50
49#define LEN_USERNAME 30
50#define LEN_HOST 55
51
52
53struct entry {
54 char *name;
55 char *last;
56 char *first;
57 char *middle;
58 char *title;
59 char *class;
60 char *id;
61 char *eid;
62 char *dept;
63 char *address;
64 char *phone;
65 char *phone2;
66 char *email;
67};
68
69
70char *whoami;
71int newfinger = 0;
72int addxuser = 0;
73
74
75main(argc, argv)
76int argc;
77char **argv;
78##{
79 FILE *in;
80 struct entry *e, *get_next_entry();
81 int i, wait = 0;
82 char buf[BUFSIZ], *file = NULL;
83
84 whoami = rindex(argv[0], '/');
85 if (whoami)
86 whoami++;
87 else
88 whoami = argv[0];
89
90 for (i = 1; i < argc; i++) {
91 if (!strcmp(argv[i], "-w"))
92 wait++;
93 else if (!strcmp(argv[i], "-D"))
94 setenv("ING_SET", "set printqry");
95 else if (!strcmp(argv[i], "-n"))
96 newfinger++;
97 else if (!strcmp(argv[i], "-u"))
98 addxuser++;
99 else if (file != NULL)
100 fprintf(stderr, "Usage: %s [-w] [-D] [-n] [-u] inputfile\n", whoami);
101 else
102 file = argv[i];
103 }
104
105 in = fopen(file, "r");
106 if (in == NULL) {
107 fprintf(stderr, "Unable to open %s for input\n", file);
108 exit(1);
109 }
110
111## ingres sms
112## range of u is users
113
114 while (e = get_next_entry(in)) {
115 process_entry(e);
116 if (wait) {
117 printf("Next");
118 fflush(stdout);
119 gets(buf);
120 }
121 }
122
123## exit
124 exit(0);
125##}
126
127
128char *substr(buf, key)
129char *buf;
130char *key;
131{
132 int l;
133
134 for (l = strlen(key); *buf; buf++)
135 if (!strncmp(buf, key, l))
136 return(buf);
137 return(NULL);
138}
139
140
141struct entry *get_next_entry(in)
142FILE *in;
143{
144 static struct entry e;
145 static char buf[BUFSIZ], mid[16], eid[16], email[256];
146 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
147 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
148 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
149 static char host[LEN_HOST+1];
150 int ends_sr, ends_jr, ends_iii, ends_iv;
151 char *p;
152
153 if (fgets(buf, sizeof(buf), in) == NULL)
154 return((struct entry *)NULL);
155
156 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
157 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
158 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
159 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
160 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
161 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
162 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
163 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
164 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
165
166 strcpy(sname, name);
167 e.name = strtrim(sname);
168 p = index(name, ',');
169 if (p)
170 *p = 0;
171 e.last = strtrim(name);
172 if (p) {
173 p++;
174 while (isspace(*p))
175 p++;
176 e.first = p;
177 if (p = index(e.first, ' ')) {
178 *p = 0;
179 e.first = strtrim(e.first);
180 e.middle = strtrim(p + 1);
181 } else {
182 e.first = strtrim(e.first);
183 e.middle = "";
184 }
185 } else {
186 e.first = "";
187 e.middle = "";
188 }
189 ends_sr = ends_jr = ends_iii = ends_iv = 0;
190 LookForSt(e.last);
191 LookForO(e.last);
192 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
193 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
194 FixCase(e.last);
195 FixCase(e.first);
196 FixCase(e.middle);
197
198 e.id = id;
199 e.eid = eid;
200 EncryptID(e.eid, e.id, e.first, e.last);
201
202 e.address = strtrim(office);
203 e.phone = strtrim(phone);
204 e.phone2 = strtrim(phone2);
205 e.dept = strtrim(dept);
206 e.title = strtrim(title);
207
208 e.class = "MITS";
209 if (!strcmp(e.dept, "PROJECT ATHENA"))
210 e.class = "STAFF";
211 else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
212 e.class = "FACULTY";
213 else if (!strcmp(e.title, "VISITING SCIENTIST"))
214 e.class = "VSCIENTI";
215
216 strcpy(email, strtrim(username));
217 if (host[0] == '@')
218 strncat(email, strtrim(host));
219 e.email = email;
220
221 return(&e);
222}
223
224
225process_entry(e)
226struct entry *e;
227##{
228 int changed, nochange;
229 char buf[BUFSIZ], *from, *to;
230## char *first, *last, *eid, *sid, *name, *title, *phone2;
231## char class[9], oaddr[25], ophone[17], dept[128];
232## int id, status;
233
234 first = e->first;
235 if (strlen(first) > 16)
236 first[16] = 0;
237 last = e->last;
238 if (strlen(last) > 16)
239 last[16] = 0;
240 eid = e->eid;
241 id = 0;
242## repeat retrieve (id = u.users_id, class = u.mit_year, oaddr = u.office_addr,
243## ophone = u.office_phone, dept = u.mit_dept, status = u.#status)
244## where u.#last = @last and u.#first = @first and u.mit_id = @eid
245 if (id == 0) {
246 newuser(e);
247 return;
248 }
249 if (strcmp(e->class, strtrim(class)) &&
250 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
251 com_err(whoami, 0, "updating class for %s %s from %s to %s",
252 first, last, class, e->class);
253 if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
254 status = US_NO_LOGIN_YET;
255 if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
256 status = US_ENROLLED;
257 strcpy(class, e->class);
258## repeat replace u (mit_year = @class, #status = @status, ugdefault = 1,
259## modtime = "now", modby = WHO, modwith = PROG)
260## where u.users_id = @id
261 }
262 changed = nochange = 0;
263 strcpy(buf, e->address);
264 while (to = index(buf, ','))
265 *to = ';';
266 while (to = index(buf, ':'))
267 *to = ';';
268 if (newfinger) {
269 if (oaddr[0] == ' ' && buf[0]) {
270 strncpy(oaddr, buf, 16);
271 oaddr[16] = 0;
272 changed++;
273 } else if (strncmp(strtrim(oaddr), buf, 15))
274 nochange++;
275 } else {
276 if (strncmp(strtrim(oaddr), buf, 15))
277 changed++;
278 strncpy(oaddr, buf, 16);
279 oaddr[16] = 0;
280 }
281 from = e->phone;
282 to = buf;
283 while (*from) {
284 if (isdigit(*from))
285 *to++ = *from;
286 from++;
287 }
288 *to = 0;
289 if (newfinger) {
290 if (ophone[0] == ' ') {
291 strncpy(ophone, buf, 16);
292 ophone[16] = 0;
293 } else if (strncmp(strtrim(ophone), buf, 11))
294 nochange++;
295 } else {
296 if (strncmp(strtrim(ophone), buf, 11))
297 changed++;
298 strncpy(ophone, buf, 16);
299 ophone[16] = 0;
300 }
301 FixCase(e->dept);
302 FixCase(e->title);
303 if (newfinger) {
304 if (dept[0] == ' ') {
305 strncpy(dept, e->dept, 12);
306 dept[12] = 0;
307 } else if (strncmp(strtrim(dept), e->dept, 11))
308 nochange++;
309 } else {
310 if (strncmp(strtrim(dept), e->dept, 11))
311 changed++;
312 strncpy(dept, e->dept, 12);
313 dept[12] = 0;
314 }
315 if (changed) {
316 com_err(whoami, 0, "updating finger for %s %s", first, last);
317## repeat replace u (office_addr = @oaddr, ugdefault = 1,
318## office_phone = @ophone, #mit_dept = @dept,
319## fmodtime = "now", fmodby = WHO, fmodwith = PROG)
320## where u.users_id = @id
321 } /* else if (nochange)
322 com_err(whoami, 0, "NOT updating finger for %s %s", first, last);
323 */
324 if (!changed) {
325## repeat replace u (ugdefault = 1) where u.users_id = @id
326 }
327 if (addxuser) {
328 sid = e->id;
329 name = e->name;
330 strcpy(dept, e->dept);
331 title = e->title;
332 strcpy(oaddr, e->address);
333 phone2 = e->phone2;
334
335 status = 1;
336## repeat retrieve (status = any(xuser.users_id
337## where xuser.users_id = @id))
338 if (!status) {
339## append xuser (users_id = id, #id = sid, #name = name, #dept = dept,
340## #title = title, address = oaddr, #phone1 = ophone,
341## #phone2 = phone2, modtime = "now")
342 }
343 }
344##}
345
346
347newuser(e)
348struct entry *e;
349##{
350 char *from, *to;
351## int id, uid, st;
352## char *last, *first, *class, *middle, login[9], *eid, fullname[65];
353## char oaddr[81], ophone[17], dept[128], *sid, *name, *title, phone2[17];
354
355
356 strncpy(oaddr, e->address, 16);
357 oaddr[16] = 0;
358 while (to = index(oaddr, ','))
359 *to = ';';
360 while (to = index(oaddr, ':'))
361 *to = ';';
362 from = e->phone;
363 to = ophone;
364 while (*from) {
365 if (isdigit(*from))
366 *to++ = *from;
367 from++;
368 }
369 *to = 0;
370 FixCase(e->dept);
371 strncpy(dept, e->dept, 12);
372 dept[12] = 0;
373
374 id = set_next_object_id("users_id", 0);
375 uid = set_next_object_id("uid", 1);
376 sprintf(login, "#%d", uid);
377 last = e->last;
378 first = e->first;
379 middle = e->middle;
380 eid = e->eid;
381 class = e->class;
382 if (*middle)
383 sprintf(fullname, "%s %s %s", first, middle, last);
384 else
385 sprintf(fullname, "%s %s", first, last);
386 st = US_NOT_ALLOWED;
387 if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
388 st = US_NO_LOGIN_YET;
389
390## append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
391## #last = last, #first = first, #middle = middle, status = st,
392## #mit_id = eid, #mit_year = class, ugdefault = 1,
393## modtime = "now", modby = WHO, modwith = PROG,
394## #fullname = fullname, office_addr = oaddr,
395## office_phone = ophone, #mit_dept = dept,
396## fmodtime = "now", fmodby = WHO, fmodwith = PROG,
397## potype = "NONE")
398
399 sid = e->id;
400 name = e->name;
401 strcpy(dept, e->dept);
402 title = e->title;
403 FixCase(title);
404 strcpy(oaddr, e->address);
405 FixCase(oaddr);
406 from = e->phone2;
407 to = phone2;
408 while (*from) {
409 if (isdigit(*from))
410 *to++ = *from;
411 from++;
412 }
413 *to = 0;
414
415 if (addxuser) {
416## append xuser (users_id = id, #id = sid, #name = fullname, #dept = dept,
417## #title = title, address = oaddr, #phone1 = ophone,
418## #phone2 = phone2, modtime = "now")
419 }
420 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
421##}
422
423
424set_next_object_id(object, limit)
425 char *object;
426 int limit;
427##{
428## char *name;
429## int rowcount, exists, value;
430
431 name = object;
432## begin transaction
433## repeat retrieve (value = values.#value) where values.#name = @name
434## inquire_equel(rowcount = "rowcount")
435 if (rowcount != 1) {
436## abort
437 return(0);
438 }
439
440## retrieve (exists = any(users.name where users.name = value))
441## inquire_equel(rowcount = "rowcount")
442 if (rowcount != 1) {
443## abort
444 return(0);
445 }
446 while (exists) {
447 value++;
448 if (limit && value > MAX_ID_VALUE)
449 value = MIN_ID_VALUE;
450## retrieve (exists = any(users.name where users.name = value))
451 }
452
453## repeat replace values (#value = @value) where values.#name = @name
454## end transaction
455 return(value);
456##}
This page took 0.036988 seconds and 5 git commands to generate.