]> andersk Git - moira.git/blob - regtape/employee.dc
translate to SQL
[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 EXEC SQL INCLUDE sqlca;
11
12
13 #define WHO 11859               /* root */
14 #define PROG "emp-tape"
15
16 #define MAX_ID_VALUE    31999
17 #define MIN_ID_VALUE    101
18
19 /* File format is:
20
21 0-8     id number
22 9-38    name
23 39-62   office address
24 63-74   phone1
25 75-86   phone2
26 87-106  dept
27 107-156 title
28 157-186 username
29 187-241 host
30
31 */
32
33 #define LOC_ID 0
34 #define LOC_NAME 9
35 #define LOC_OFFICE 39
36 #define LOC_PHONE 63
37 #define LOC_PHONE2 75
38 #define LOC_DEPT 87
39 #define LOC_TITLE 107
40 #define LOC_USERNAME 157
41 #define LOC_HOST 187
42
43 #define LEN_ID 9
44 #define LEN_NAME 30
45 #define LEN_OFFICE 24
46 #define LEN_PHONE 12
47 #define LEN_PHONE2 12
48 #define LEN_DEPT 20
49 #define LEN_TITLE 50
50 #define LEN_USERNAME 30
51 #define LEN_HOST 55
52
53
54 struct entry {
55     char *name;
56     char *last;
57     char *first;
58     char *middle;
59     char *title;
60     char *class;
61     char *id;
62     char *eid;
63     char *dept;
64     char *address;
65     char *phone;
66     char *phone2;
67     char *email;
68 };
69
70
71 char *whoami;
72 int newfinger = 0;
73
74
75 main(argc, argv)
76 int argc;
77 char **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 (file != NULL)
98           fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
99         else
100           file = argv[i];
101     }
102
103     in = fopen(file, "r");
104     if (in == NULL) {
105         fprintf(stderr, "Unable to open %s for input\n", file);
106         exit(1);
107     }
108
109     setlinebuf(stdout);
110     setlinebuf(stderr);
111
112     EXEC SQL CONNECT moira;
113
114     while (e = get_next_entry(in)) {
115         process_entry(e);
116         EXEC SQL COMMIT;
117         if (wait) {
118             printf("Next");
119             fflush(stdout);
120             gets(buf);
121         }
122     }
123
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, ends_ii, ends_v;
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 = ends_ii = ends_v = 0;
190     LookForSt(e.last);
191     LookForO(e.last);
192     LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
193                     &ends_ii, &ends_v);
194     LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
195                     &ends_ii, &ends_v);
196     FixCase(e.last);
197     FixCase(e.first);
198     FixCase(e.middle);
199
200     e.id = id;
201     e.eid = eid;
202     EncryptID(e.eid, e.id, e.first, e.last);
203
204     e.address = strtrim(office);
205     e.phone = strtrim(phone);
206     e.phone2 = strtrim(phone2);
207     e.dept = strtrim(dept);
208     e.title = strtrim(title);
209
210     e.class = "MITS";
211     if (!strcmp(e.dept, "PROJECT ATHENA"))
212       e.class = "STAFF";
213     else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
214       e.class = "FACULTY";
215     else if (!strcmp(e.title, "VISITING SCIENTIST"))
216       e.class = "VSCIENTI";
217
218     strcpy(email, strtrim(username));
219     if (host[0] == '@')
220       strncat(email, strtrim(host));
221     e.email = email;
222
223     return(&e);
224 }
225
226
227 process_entry(e)
228 struct entry *e;
229 {
230     int changed, nochange;
231     char buf[BUFSIZ], *from, *to;
232     EXEC SQL BEGIN DECLARE SECTION;
233     char *first, *last, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
234     char *raddr, *rhphone, *rophone;
235     char class[9], oaddr[25], ophone[17], dept[128];
236     int id, status;
237     EXEC SQL END DECLARE SECTION;
238
239     first = e->first;
240     if (strlen(first) > 16)
241       first[16] = 0;
242     last = e->last;
243     if (strlen(last) > 16)
244       last[16] = 0;
245     eid = e->eid;
246     sid = e->id;
247     id = 0;
248
249     /* Get user info */
250     EXEC SQL SELECT users_id, mit_year, office_addr, office_phone, mit_dept, status
251       INTO :id, :class, :oaddr, :ophone, :dept, :status
252       FROM users
253       WHERE last = :last and first = :first and clearid = :sid;
254     if (id == 0) {
255         EXEC SQL SELECT users_id, mit_year, office_addr, office_phone, mit_dept, status
256           INTO :id, :class, :oaddr, :ophone, :dept, :status
257           FROM users
258           WHERE last = :last and first = :first and clearid = :eid;
259         if (id == 0) {
260             newuser(e);
261             return;
262         }
263     }
264
265     if (strcmp(e->class, strtrim(class)) &&
266         strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
267         com_err(whoami, 0, "updating class for %s %s from %s to %s",
268                 first, last, class, e->class);
269         if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
270           status = US_NO_LOGIN_YET;
271         if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
272           status = US_ENROLLED;
273         strcpy(class, e->class);
274         EXEC SQL UPDATE users
275           SET type = :class, status = :status, modtime = 'now',
276                 modby = WHO, modwith = PROG
277           WHERE users_id = :id;
278     }
279
280     changed = nochange = 0;
281     strcpy(buf, e->address);
282     while (to = index(buf, ','))
283       *to = ';';
284     while (to = index(buf, ':'))
285       *to = ';';
286     if (newfinger) {
287         if (oaddr[0] == ' ' && buf[0]) {
288             strncpy(oaddr, buf, 16);
289             oaddr[16] = 0;
290             changed++;
291         } else if (strncmp(strtrim(oaddr), buf, 15))
292           nochange++;
293     } else {
294         if (strncmp(strtrim(oaddr), buf, 15))
295           changed++;
296         strncpy(oaddr, buf, 16);
297         oaddr[16] = 0;
298     }
299     from = e->phone;
300     to = buf;
301     while (*from) {
302         if (isdigit(*from))
303           *to++ = *from;
304         from++;
305     }
306     *to = 0;
307     if (newfinger) {
308         if (ophone[0] == ' ') {
309             strncpy(ophone, buf, 16);
310             ophone[16] = 0;
311         } else if (strncmp(strtrim(ophone), buf, 11))
312           nochange++;
313     } else {
314         if (strncmp(strtrim(ophone), buf, 11))
315           changed++;
316         strncpy(ophone, buf, 16);
317         ophone[16] = 0;
318     }
319     FixCase(e->dept);
320     FixCase(e->title);
321     if (newfinger) {
322         if (dept[0] == ' ') {
323             strncpy(dept, e->dept, 12);
324             dept[12] = 0;
325         } else if (strncmp(strtrim(dept), e->dept, 11))
326           nochange++;
327     } else {
328         if (strncmp(strtrim(dept), e->dept, 11))
329           changed++;
330         strncpy(dept, e->dept, 12);
331         dept[12] = 0;
332     }
333     sid = e->id;
334     name = e->name;
335     rdept = e->dept;
336     rtitle = e->title;
337     raddr = e->address;
338     rhphone = e->phone;
339     rophone = e->phone2;
340     if (changed) {
341         com_err(whoami, 0, "updating finger for %s %s", first, last);
342         EXEC SQL REPEATED UPDATE users
343           SET office_addr = :oaddr, office_phone = :ophone, mit_dept = :dept,
344             fmodtime = 'now', fmodby = WHO, fmodwith = PROG,
345             xname = :name, xdept = :rdept, xtitle = :rtitle,
346             xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
347             xmodtime = 'now', mit_id = :sid
348           WHERE users_id = :id;
349     } else {
350         EXEC SQL REPEATED UPDATE users
351           SET xname = :name, xdept = :rdept, xtitle = :rtitle,
352             xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
353             xmodtime = 'now', mit_id = :sid
354           WHERE users_id = :id;
355     }
356 }
357
358
359 newuser(e)
360 struct entry *e;
361 {
362     char *from, *to;
363     EXEC SQL BEGIN DECLARE SECTION;
364     int id, uid, st, who;
365     char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
366     char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
367     char *rdept, *rhphone, *rophone;
368     EXEC SQL END DECLARE SECTION;
369
370     who = WHO;
371     prog = PROG;
372     strncpy(oaddr, e->address, 16);
373     oaddr[16] = 0;
374     while (to = index(oaddr, ','))
375       *to = ';';
376     while (to = index(oaddr, ':'))
377       *to = ';';
378     from = e->phone;
379     to = ophone;
380     while (*from) {
381         if (isdigit(*from))
382           *to++ = *from;
383         from++;
384     }
385     *to = 0;
386     FixCase(e->dept);
387     strncpy(dept, e->dept, 12);
388     dept[12] = 0;
389     
390     id = set_next_object_id("users_id", 0);
391     uid = set_next_object_id("uid", 1);
392     sprintf(login, "#%d", uid);
393     last = e->last;
394     first = e->first;
395     middle = e->middle;
396     class = e->class;
397     if (*middle)
398       sprintf(fullname, "%s %s %s", first, middle, last);
399     else
400       sprintf(fullname, "%s %s", first, last);
401     st = US_NOT_ALLOWED;
402     if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
403       st = US_NO_LOGIN_YET;
404
405     sid = e->id;
406     name = e->name;
407     rdept = e->dept;
408     title = e->title;
409     rhphone = e->phone;
410     rophone = e->phone2;
411
412     EXEC SQL INSERT INTO users
413       (login, users_id, uid, shell, last, first, middle, status,
414        clearid, type, modtime, modby, modwith, fullname, office_addr,
415        office_phone, department, fmodtime, fmodby, fmodwith,
416        potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
417       VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
418               :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
419               :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
420               :title, :oaddr, :rhphone, :rophone, 'now');
421 }
422
423
424 set_next_object_id(object, limit)
425     char *object;
426     int limit;
427 {
428     EXEC SQL BEGIN DECLARE SECTION;
429     char *name;
430     int rowcount, flag, value;
431     EXEC SQL END DECLARE SECTION;
432
433     name = object;
434     EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :name;
435     if (sqlca.sqlerrd[2] != 1) {
436         EXEC SQL ROLLBACK;
437         com_err(whoami, MR_INTERNAL, "values table inconsistancy");
438         exit(1);
439     }
440
441     flag = 0;
442     EXEC SQL SELECT :name INTO :flag FROM users WHERE :name = :value;
443     if (sqlca.sqlerrd[2] == 0)
444       flag = 0;
445     while (flag) {
446         value++;
447         if (limit && value > MAX_ID_VALUE)
448             value = MIN_ID_VALUE;
449         flag = 0;
450         EXEC SQL SELECT :name INTO :flag FROM users WHERE :name = :value;
451         if (sqlca.sqlerrd[2] == 0)
452           flag = 0;
453     }
454
455     value++;
456     if (limit && value > MAX_ID_VALUE)
457       value = MIN_ID_VALUE;
458     EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = :name;
459     return(value);
460 }
This page took 0.070468 seconds and 5 git commands to generate.