]> andersk Git - moira.git/blob - regtape/students.dc
rewrite ID allocation; match on ID number; update names; detect duplicates
[moira.git] / regtape / students.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 "stu-tape"
15
16 #define MAX_ID_VALUE    31999
17 #define MIN_ID_VALUE    101
18
19 /* File format is:
20
21 0-29    name
22 30-38   id number
23 50-54   school code
24 55-79   year
25 80-109  address
26 110-124 room
27 125-144 city
28 145-158 state
29 159-168 dorm phone
30 169-212 home address
31 213-232 home city
32 243-251 mit phone (?)
33 */
34
35 #define LOC_NAME 0
36 #define LOC_ID 30
37 #define LOC_COURSE 50
38 #define LOC_YEAR 55
39 #define LOC_ADDRESS 80
40 #define LOC_DORM_ROOM 110
41 #define LOC_CITY 125
42 #define LOC_STATE 145
43 #define LOC_DPHONE 155
44 #define LOC_MPHONE 243
45
46 #define LEN_NAME 30
47 #define LEN_ID 9
48 #define LEN_COURSE 5
49 #define LEN_YEAR 25
50 #define LEN_ADDRESS 30
51 #define LEN_DORM_ROOM 15
52 #define LEN_CITY 20
53 #define LEN_STATE 10
54 #define LEN_DPHONE 12
55 #define LEN_MPHONE 12
56
57 struct entry {
58     char *name;
59     char *last;
60     char *first;
61     char *middle;
62     char *title;
63     char *id;
64     char *eid;
65     char *course;
66     char *year;
67     char *address;
68     char *dorm;
69     char *city;
70     char *state;
71     char *dphone;
72     char *mphone;
73     char *class;
74 };
75
76
77 char *whoami;
78 int newfinger = 0;
79
80 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
81 #define SQL_DUPLICATE -40100
82
83 main(argc, argv)
84 int argc;
85 char **argv;
86 {
87     FILE *in;
88     struct entry *e, *get_next_entry();
89     int i, wait = 0;
90     char buf[BUFSIZ], *file = NULL;
91
92     whoami = rindex(argv[0], '/');
93     if (whoami)
94       whoami++;
95     else
96       whoami = argv[0];
97
98     for (i = 1; i < argc; i++) {
99         if (!strcmp(argv[i], "-w"))
100           wait++;
101         else if (!strcmp(argv[i], "-D"))
102           setenv("ING_SET", "set printqry");
103         else if (!strcmp(argv[i], "-n"))
104           newfinger++;
105         else if (file != NULL)
106           fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
107         else
108           file = argv[i];
109     }
110
111     in = fopen(file, "r");
112     if (in == NULL) {
113         fprintf(stderr, "Unable to open %s for input\n", file);
114         exit(1);
115     }
116
117     setlinebuf(stdout);
118     setlinebuf(stderr);
119
120     EXEC SQL CONNECT moira;
121     if (sqlca.sqlcode != 0) {
122         com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
123         exit(1);
124     }
125
126     while (e = get_next_entry(in)) {
127         process_entry(e);
128         EXEC SQL COMMIT WORK;
129         if (sqlca.sqlcode != 0) {
130             com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
131             exit(1);
132         }
133         if (wait) {
134             printf("Next");
135             fflush(stdout);
136             gets(buf);
137         }
138     }
139
140     exit(0);
141 }
142
143
144 struct entry *get_next_entry(in)
145 FILE *in;
146 {
147     static struct entry e;
148     static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
149     static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1];
150     static char year[LEN_YEAR+1], address[LEN_ADDRESS+1];
151     static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1];
152     static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1];
153     static char sname[LEN_NAME+1], title[128];
154     static int nyear = 0;
155     int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
156     char *p;
157
158     if (nyear == 0) {
159         struct tm *tm;
160         struct timeval tv;
161
162         gettimeofday(&tv, NULL);
163         tm = localtime(&tv.tv_sec);
164         nyear = tm->tm_year;
165         if (tm->tm_mon > 5)
166           nyear++;
167     }
168
169     if (fgets(buf, sizeof(buf), in) == NULL)
170       return((struct entry *)NULL);
171
172     strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
173     strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
174     strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0;
175     strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0;
176     strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0;
177     strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0;
178     strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0;
179     strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0;
180     strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0;
181     strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0;
182
183     strcpy(sname, name);
184     e.name = strtrim(sname);
185     p = index(name, ',');
186     if (p)
187       *p = 0;
188     e.last = strtrim(name);
189     if (p) {
190         p++;
191         while (isspace(*p))
192           p++;
193         e.first = p;    
194         if (p = index(e.first, ' ')) {
195             *p = 0;
196             e.first = strtrim(e.first);
197             e.middle = strtrim(p + 1);
198         } else {
199             e.first = strtrim(e.first);
200             e.middle = "";
201         }
202     } else {
203         e.first = "";
204         e.middle = "";
205     }
206     ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
207     LookForSt(e.last);
208     LookForO(e.last);
209     LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
210                     &ends_ii, &ends_v);
211     LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
212                     &ends_ii, &ends_v);
213     FixCase(e.last);
214     FixCase(e.first);
215     FixCase(e.middle);
216
217     e.id = id;
218     e.id[LEN_ID] = 0;
219     e.eid = eid;
220     EncryptID(e.eid, e.id, e.first, e.last);
221
222     e.year = strtrim(year);
223     e.title = title;
224     if (e.year[0] == 'G') {
225         e.class = "G";
226         sprintf(title, "Grad Student");
227     } else {
228         e.class = classbuf;
229         sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
230         sprintf(title, "Undergrad (class of %s)", classbuf);
231     }
232
233     e.course = strtrim(course);
234     e.address = strtrim(address);
235     e.dorm = strtrim(dorm_room);
236     e.city = strtrim(city);
237     e.state = strtrim(state);
238     e.dphone = strtrim(dphone);
239     e.mphone = strtrim(mphone);
240     return(&e);
241 }
242
243
244 process_entry(e)
245 struct entry *e;
246 {
247     int changed, nochange, encrypted;
248     char buf[BUFSIZ], *from, *to;
249     EXEC SQL BEGIN DECLARE SECTION;
250     char *first, *last, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
251     char *rophone, *rhphone, *prog;
252     char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
253     char dfirst[17], dlast[17], dmiddle[17];
254     int id, status, who;
255     EXEC SQL END DECLARE SECTION;
256
257     who = WHO;
258     prog = PROG;
259     first = e->first;
260     if (strlen(first) > 16)
261       first[16] = 0;
262     last = e->last;
263     if (strlen(last) > 16)
264       last[16] = 0;
265     eid = e->eid;
266     sid = e->id;
267     id = 0;
268     encrypted = 0;
269
270 /* Get user info */
271     EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
272       INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
273       FROM users
274       WHERE clearid = :sid;
275     if (sqlfail()) {
276         if (sqlca.sqlcode == SQL_DUPLICATE) {
277             com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
278             return;
279         } else
280           sqlexit();
281     }
282     if (id == 0) {
283         EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
284           INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
285           FROM users
286           WHERE last = :last and first = :first and clearid = :eid;
287         if (sqlfail() && sqlca.sqlcode != SQL_DUPLICATE) sqlexit();
288         encrypted++;
289         if (id == 0) {
290             newuser(e);
291             return;
292         }
293     }
294
295 /* See if class changed: if it's different, and the value in the database
296  * is not STAFF or SIPB, then update the database.  Since they were on the
297  * students tape, make the account usable.
298  */
299     if (strcmp(e->class, strtrim(class)) &&
300         strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
301         com_err(whoami, 0, "updating class for user %s %s from %s to %s",
302                 first, last, class, e->class);
303         if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
304         if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
305         strcpy(class, e->class);
306         EXEC SQL REPEATED UPDATE users
307           SET type = :class, status = :status, modtime = 'now',
308             modby = :who, modwith = :prog
309           WHERE users_id = :id;   
310         if (sqlca.sqlcode != 0) {
311             com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
312             exit(1);
313         }
314     }
315
316     /* Deal with updating the finger info if necessary */
317
318     changed = nochange = 0;
319     if (encrypted) changed++;
320     strcpy(buf, e->address);
321     if (*e->dorm) {
322         strcat(buf, " ");
323         strcat(buf, e->dorm);
324     }
325     if (*e->city) {
326         strcat(buf, " ");
327         strcat(buf, e->city);
328     }
329     FixCase(buf);
330     if (*e->state) {
331         strcat(buf, " ");
332         strcat(buf, e->state);
333     }
334     while (to = index(buf, ','))
335       *to = ';';
336     while (to = index(buf, ':'))
337       *to = ';';
338     if (newfinger) {
339         if (haddr[0] == ' ') {
340             strncpy(haddr, buf, 80);
341             haddr[80] = 0;
342             changed++;
343         } else if (strncmp(strtrim(haddr), buf, 80))
344           nochange++;
345     } else {
346         if (strncmp(strtrim(haddr), buf, 80))
347           changed++;
348         strncpy(haddr, buf, 80);
349         haddr[80] = 0;
350     }
351     from = e->dphone;
352     to = buf;
353     while (*from) {
354         if (isdigit(*from))
355           *to++ = *from;
356         from++;
357     }
358     *to = 0;
359     if (newfinger) {
360         if (hphone[0] == ' ') {
361             strncpy(hphone, buf, 16);
362             hphone[16] = 0;
363         } else if (strncmp(strtrim(hphone), buf, 16))
364           nochange++;
365     } else {
366         if (strncmp(strtrim(hphone), buf, 16))
367           changed++;
368         strncpy(hphone, buf, 16);
369         hphone[16] = 0;
370     }
371     from = e->mphone;
372     to = buf;
373     while (*from) {
374         if (isdigit(*from))
375           *to++ = *from;
376         from++;
377     }
378     *to = 0;
379     if (newfinger) {
380         if (ophone[0] == ' ') {
381             strncpy(ophone, buf, 12);
382             ophone[12] = 0;
383         } else if (strncmp(strtrim(ophone), buf, 12))
384           nochange++;
385     } else {
386         if (strncmp(strtrim(ophone), buf, 12))
387           changed++;
388         strncpy(ophone, buf, 12);
389         ophone[12] = 0;
390     }
391     e->course = e->course;
392     if (newfinger) {
393         if (dept[0] == ' ') {
394             strncpy(dept, e->course, 12);
395             dept[12] = 0;
396         } else if (strncmp(strtrim(dept), e->course, 11))
397           nochange++;
398     } else {
399         if (strncmp(strtrim(dept), e->course, 11))
400           changed++;
401         strncpy(dept, e->course, 12);
402         dept[12] = 0;
403     }
404     sid = e->id;
405     name = e->name;
406     rdept = e->course;
407     rtitle = e->title;
408     rophone = e->mphone;
409     rhphone = e->dphone;
410     strcpy(raddr, e->address);
411     if (*e->dorm) {
412         strcat(raddr, " ");
413         strcat(raddr, e->dorm);
414     }
415     strcat(raddr, "|");
416     if (*e->city) {
417         strcat(raddr, e->city);
418         FixCase(raddr);
419         if (*e->state) {
420             strcat(raddr, " ");
421             if (isupper(e->state[0]) && isupper(e->state[1]) &&
422                 isdigit(e->state[2]) && isdigit(e->state[3]) &&
423                 isdigit(e->state[4]) && isdigit(e->state[5]) &&
424                 isdigit(e->state[6])) {
425                 sprintf(buf, "%c%c  %s", e->state[0],e->state[1],
426                         &(e->state[2]));
427                 strcat(raddr, buf);
428             } else
429               strcat(raddr, e->state);
430         }
431     } else {
432         FixCase(raddr);
433         strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
434     }
435     if (changed) {
436         com_err(whoami, 0, "updating finger for %s %s", first, last);
437         EXEC SQL REPEATED UPDATE users
438           SET home_addr = :haddr, home_phone = :hphone,
439             office_phone = :ophone, department = :dept,
440             fmodtime = 'now', fmodby = :who, fmodwith = :prog,
441             xname = :name, xdept = :rdept, xtitle = :rtitle,
442             xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
443             xmodtime = date('now'), clearid = :sid
444           WHERE users_id = :id;
445         if (sqlca.sqlcode != 0) {
446             com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
447             exit(1);
448         }
449     }  else {
450         EXEC SQL REPEATED UPDATE users
451           SET xname = :name, xdept = :rdept, xtitle = :rtitle,
452             xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
453             xmodtime = date('now'), clearid = :sid
454           WHERE users_id = :id;
455         if (sqlca.sqlcode != 0) {
456             com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
457             exit(1);
458         }
459     }
460 }
461
462
463 newuser(e)
464 struct entry *e;
465 {
466     char buf[512], *from, *to;
467     EXEC SQL BEGIN DECLARE SECTION;
468     int id, uid, who;
469     char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
470     char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
471     EXEC SQL END DECLARE SECTION;
472
473     who = WHO;
474     prog = PROG;
475     strcpy(buf, e->address);
476     if (*e->dorm) {
477         strcat(buf, " ");
478         strcat(buf, e->dorm);
479     }
480     if (*e->city) {
481         strcat(buf, " ");
482         strcat(buf, e->city);
483     }
484     if (*e->state) {
485         strcat(buf, " ");
486         strcat(buf, e->state);
487     }
488     strncpy(haddr, buf, 80);
489     from = e->dphone;
490     to = buf;
491     while (*from) {
492         if (isdigit(*from))
493           *to++ = *from;
494         from++;
495     }
496     *to = 0;
497     strncpy(hphone, buf, 16);
498     from = e->mphone;
499     to = buf;
500     while (*from) {
501         if (isdigit(*from))
502           *to++ = *from;
503         from++;
504     }
505     *to = 0;
506     strncpy(ophone, buf, 12);
507     strncpy(dept, e->course, 12);
508     
509     id = set_next_users_id(0);
510     uid = set_next_uid(1);
511     sprintf(login, "#%d", uid);
512     last = e->last;
513     first = e->first;
514     middle = e->middle;
515     sid = e->id;
516     class = e->class;
517     title = e->title;
518     if (*middle)
519       sprintf(fullname, "%s %s %s", first, middle, last);
520     else
521       sprintf(fullname, "%s %s", first, last);
522     name = e->name;
523     strcpy(raddr, e->address);
524     if (*e->dorm) {
525         strcat(raddr, " ");
526         strcat(raddr, e->dorm);
527     }
528     strcat(raddr, "|");
529     if (*e->city) {
530         strcat(raddr, e->city);
531         FixCase(raddr);
532         if (*e->state) {
533             strcat(raddr, " ");
534             if (isupper(e->state[0]) && isupper(e->state[1]) &&
535                 isdigit(e->state[2]) && isdigit(e->state[3]) &&
536                 isdigit(e->state[4]) && isdigit(e->state[5]) &&
537                 isdigit(e->state[6])) {
538                 sprintf(buf, "%c%c  %s", e->state[0],e->state[1],
539                         &(e->state[2]));
540                 strcat(raddr, buf);
541             } else
542               strcat(raddr, e->state);
543         }
544     } else {
545         FixCase(raddr);
546         strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
547     }
548     EXEC SQL REPEATED INSERT INTO users
549       (login, users_id, uid, shell, last, first, middle, status,
550        clearid, type, modtime, modby, modwith, fullname, home_addr,
551        home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
552        potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
553       VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, 0,
554               :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone,
555               :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept,
556               :title, :raddr, :hphone, :ophone, date('now'));
557     if (sqlca.sqlcode != 0) {
558         com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
559         exit(1);
560     } else
561       com_err(whoami, 0, "adding user %s %s", e->first, e->last);
562 }
563
564
565
566 set_next_users_id(limit)
567     int limit;
568 {
569     EXEC SQL BEGIN DECLARE SECTION;
570     int rowcount, flag, value, retval;
571     EXEC SQL END DECLARE SECTION;
572
573     EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
574       WHERE name = 'users_id';
575     if (sqlfail()) sqlexit();
576     if (sqlca.sqlerrd[2] != 1) {
577         EXEC SQL ROLLBACK;
578         com_err(whoami, MR_INTERNAL, "values table inconsistancy");
579         exit(1);
580     }
581
582     flag = 0;
583     EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
584       WHERE users_id = :value;
585     if (sqlfail()) sqlexit();
586     if (sqlca.sqlerrd[2] == 0)
587       flag = 0;
588     while (flag) {
589         value++;
590         if (limit && value > MAX_ID_VALUE)
591             value = MIN_ID_VALUE;
592         flag = 0;
593         EXEC SQL REPEATED SELECT users_id INTO :flag FROM users 
594           WHERE users_id = :value;
595         if (sqlfail()) sqlexit();
596         if (sqlca.sqlerrd[2] == 0)
597           flag = 0;
598     }
599
600     retval = value++;
601     if (limit && value > MAX_ID_VALUE)
602       value = MIN_ID_VALUE;
603     EXEC SQL REPEATED UPDATE numvalues SET value = :value
604       WHERE name = 'users_id';
605     if (sqlca.sqlcode != 0) {
606         com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
607         exit(1);
608     }
609     return(retval);
610 }
611
612 set_next_uid(limit)
613     int limit;
614 {
615     EXEC SQL BEGIN DECLARE SECTION;
616     int rowcount, flag, value, retval;
617     EXEC SQL END DECLARE SECTION;
618
619     EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
620       WHERE name = 'uid';
621     if (sqlfail()) sqlexit();
622     if (sqlca.sqlerrd[2] != 1) {
623         EXEC SQL ROLLBACK;
624         com_err(whoami, MR_INTERNAL, "values table inconsistancy");
625         exit(1);
626     }
627
628     flag = 0;
629     EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
630     if (sqlfail()) sqlexit();
631     if (sqlca.sqlerrd[2] == 0)
632       flag = 0;
633     while (flag) {
634         value++;
635         if (limit && value > MAX_ID_VALUE)
636             value = MIN_ID_VALUE;
637         flag = 0;
638         EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
639         if (sqlfail()) sqlexit();
640         if (sqlca.sqlerrd[2] == 0)
641           flag = 0;
642     }
643
644     retval = value++;
645     if (limit && value > MAX_ID_VALUE)
646       value = MIN_ID_VALUE;
647     EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
648     if (sqlca.sqlcode != 0) {
649         com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
650         exit(1);
651     }
652     return(retval);
653 }
654
655
656 sqlexit()
657 {
658     com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
659     EXEC SQL ROLLBACK WORK;
660     exit(1);
661 }
This page took 0.09209 seconds and 5 git commands to generate.