]> andersk Git - moira.git/blob - regtape/employee.dc
punt xuser stuff and add to users relation. Also, linebuf output
[moira.git] / regtape / employee.dc
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
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
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
53 struct 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
70 char *whoami;
71 int newfinger = 0;
72
73
74 main(argc, argv)
75 int argc;
76 char **argv;
77 ##{
78     FILE *in;
79     struct entry *e, *get_next_entry();
80     int i, wait = 0;
81     char buf[BUFSIZ], *file = NULL;
82
83     whoami = rindex(argv[0], '/');
84     if (whoami)
85       whoami++;
86     else
87       whoami = argv[0];
88
89     for (i = 1; i < argc; i++) {
90         if (!strcmp(argv[i], "-w"))
91           wait++;
92         else if (!strcmp(argv[i], "-D"))
93           setenv("ING_SET", "set printqry");
94         else if (!strcmp(argv[i], "-n"))
95           newfinger++;
96         else if (file != NULL)
97           fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
98         else
99           file = argv[i];
100     }
101
102     in = fopen(file, "r");
103     if (in == NULL) {
104         fprintf(stderr, "Unable to open %s for input\n", file);
105         exit(1);
106     }
107
108     setlinebuf(stdout);
109     setlinebuf(stderr);
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
128 char *substr(buf, key)
129 char *buf;
130 char *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
141 struct entry *get_next_entry(in)
142 FILE *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
225 process_entry(e)
226 struct entry *e;
227 ##{
228     int changed, nochange;
229     char buf[BUFSIZ], *from, *to;
230 ##  char *first, *last, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
231 ##  char *raddr, *rhphone, *rophone;
232 ##  char class[9], oaddr[25], ophone[17], dept[128];
233 ##  int id, status;
234
235     first = e->first;
236     if (strlen(first) > 16)
237       first[16] = 0;
238     last = e->last;
239     if (strlen(last) > 16)
240       last[16] = 0;
241     eid = e->eid;
242     sid = e->id;
243     id = 0;
244
245     /* Get user info */
246 ##  repeat retrieve (id = u.users_id, class = u.mit_year, oaddr = u.office_addr,
247 ##            ophone = u.office_phone, dept = u.mit_dept, status = u.#status)
248 ##      where u.#last = @last and u.#first = @first and u.mit_id = @sid
249     if (id == 0) {
250 ##      repeat retrieve (id = u.users_id, class = u.mit_year, 
251 ##                       oaddr = u.office_addr, ophone = u.office_phone,
252 ##                       dept = u.mit_dept, status = u.#status)
253 ##              where u.#last = @last and u.#first = @first and u.mit_id = @eid
254         if (id == 0) {
255             newuser(e);
256             return;
257         }
258     }
259
260     if (strcmp(e->class, strtrim(class)) &&
261         strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
262         com_err(whoami, 0, "updating class for %s %s from %s to %s",
263                 first, last, class, e->class);
264         if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
265           status = US_NO_LOGIN_YET;
266         if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
267           status = US_ENROLLED;
268         strcpy(class, e->class);
269 ##      repeat replace u (mit_year = @class, #status = @status,
270 ##                 modtime = "now", modby = WHO, modwith = PROG)
271 ##          where u.users_id = @id
272     }
273
274     changed = nochange = 0;
275     strcpy(buf, e->address);
276     while (to = index(buf, ','))
277       *to = ';';
278     while (to = index(buf, ':'))
279       *to = ';';
280     if (newfinger) {
281         if (oaddr[0] == ' ' && buf[0]) {
282             strncpy(oaddr, buf, 16);
283             oaddr[16] = 0;
284             changed++;
285         } else if (strncmp(strtrim(oaddr), buf, 15))
286           nochange++;
287     } else {
288         if (strncmp(strtrim(oaddr), buf, 15))
289           changed++;
290         strncpy(oaddr, buf, 16);
291         oaddr[16] = 0;
292     }
293     from = e->phone;
294     to = buf;
295     while (*from) {
296         if (isdigit(*from))
297           *to++ = *from;
298         from++;
299     }
300     *to = 0;
301     if (newfinger) {
302         if (ophone[0] == ' ') {
303             strncpy(ophone, buf, 16);
304             ophone[16] = 0;
305         } else if (strncmp(strtrim(ophone), buf, 11))
306           nochange++;
307     } else {
308         if (strncmp(strtrim(ophone), buf, 11))
309           changed++;
310         strncpy(ophone, buf, 16);
311         ophone[16] = 0;
312     }
313     FixCase(e->dept);
314     FixCase(e->title);
315     if (newfinger) {
316         if (dept[0] == ' ') {
317             strncpy(dept, e->dept, 12);
318             dept[12] = 0;
319         } else if (strncmp(strtrim(dept), e->dept, 11))
320           nochange++;
321     } else {
322         if (strncmp(strtrim(dept), e->dept, 11))
323           changed++;
324         strncpy(dept, e->dept, 12);
325         dept[12] = 0;
326     }
327     sid = e->id;
328     name = e->name;
329     rdept = e->dept;
330     rtitle = e->title;
331     raddr = e->address;
332     rhphone = e->phone;
333     rophone = e->phone2;
334     if (changed) {
335         com_err(whoami, 0, "updating finger for %s %s", first, last);
336 ##      repeat replace u (office_addr = @oaddr,
337 ##                 office_phone = @ophone, #mit_dept = @dept,
338 ##                 fmodtime = "now", fmodby = WHO, fmodwith = PROG,
339 ##                 xname = @name, xdept = @rdept, xtitle = @rtitle,
340 ##                 xaddress = @raddr, xphone1 = @rhphone, xphone2 = @rophone,
341 ##                 xmodtime = date("now"), mit_id = @sid)
342 ##          where u.users_id = @id
343     } else {
344 ##      repeat replace u (xname = @name, xdept = @rdept, xtitle = @rtitle,
345 ##                 xaddress = @raddr, xphone1 = @rhphone, xphone2 = @rophone,
346 ##                 xmodtime = date("now"), mit_id = @sid)
347 ##          where u.users_id = @id
348     }
349 ##}
350
351
352 newuser(e)
353 struct entry *e;
354 ##{
355     char *from, *to;
356 ##  int id, uid, st;
357 ##  char *last, *first, *class, *middle, login[9], *sid, fullname[65];
358 ##  char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
359 ##  char *rdept, *rhphone, *rophone;
360
361     strncpy(oaddr, e->address, 16);
362     oaddr[16] = 0;
363     while (to = index(oaddr, ','))
364       *to = ';';
365     while (to = index(oaddr, ':'))
366       *to = ';';
367     from = e->phone;
368     to = ophone;
369     while (*from) {
370         if (isdigit(*from))
371           *to++ = *from;
372         from++;
373     }
374     *to = 0;
375     FixCase(e->dept);
376     strncpy(dept, e->dept, 12);
377     dept[12] = 0;
378     
379     id = set_next_object_id("users_id", 0);
380     uid = set_next_object_id("uid", 1);
381     sprintf(login, "#%d", uid);
382     last = e->last;
383     first = e->first;
384     middle = e->middle;
385     class = e->class;
386     if (*middle)
387       sprintf(fullname, "%s %s %s", first, middle, last);
388     else
389       sprintf(fullname, "%s %s", first, last);
390     st = US_NOT_ALLOWED;
391     if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
392       st = US_NO_LOGIN_YET;
393
394     sid = e->id;
395     name = e->name;
396     rdept = e->dept;
397     title = e->title;
398     rhphone = e->phone;
399     rophone = e->phone2;
400
401
402 ##  append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
403 ##                #last = last, #first = first, #middle = middle, status = st,
404 ##                #mit_id = sid, #mit_year = class,
405 ##                modtime = "now", modby = WHO, modwith = PROG,
406 ##                #fullname = fullname, office_addr = oaddr,
407 ##                office_phone = ophone, #mit_dept = dept,
408 ##                fmodtime = "now", fmodby = WHO, fmodwith = PROG,
409 ##                potype = "NONE",
410 ##                xname = name, xdept = rdept, xtitle = title,
411 ##                xaddress = oaddr, xphone1 = rhphone, xphone2 = rophone,
412 ##                xmodtime = date("now"))
413
414 ##}
415
416
417 set_next_object_id(object, limit)
418     char *object;
419     int limit;
420 ##{
421 ##  char *name;
422 ##  int rowcount, exists, value;
423
424     name = object;
425 ##  begin transaction
426 ##  repeat retrieve (value = values.#value) where values.#name = @name
427 ##  inquire_equel(rowcount = "rowcount")
428     if (rowcount != 1) {
429 ##      abort
430         return(0);
431     }
432
433 ##  retrieve (exists = any(users.name where users.name = value))
434 ##  inquire_equel(rowcount = "rowcount")
435     if (rowcount != 1) {
436 ##      abort
437         return(0);
438     }
439     while (exists) {
440         value++;
441         if (limit && value > MAX_ID_VALUE)
442             value = MIN_ID_VALUE;
443 ##      retrieve (exists = any(users.name where users.name = value))
444     }
445
446 ##  repeat replace values (#value = @value) where values.#name = @name
447 ##  end transaction
448     return(value);
449 ##}
This page took 0.298319 seconds and 5 git commands to generate.