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