]> andersk Git - moira.git/blob - regtape/students.dc
New database and column names for Moira2.
[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
11
12 ##define WHO 11859              /* root */
13 ##define PROG "stu-tape"
14
15 #define MAX_ID_VALUE    31999
16 #define MIN_ID_VALUE    101
17
18 /* File format is:
19
20 0-29    name
21 30-38   id number
22 50-54   school code
23 55-79   year
24 80-109  address
25 110-124 room
26 125-144 city
27 145-158 state
28 159-168 dorm phone
29 169-212 home address
30 213-232 home city
31 243-251 mit phone (?)
32 */
33
34 #define LOC_NAME 0
35 #define LOC_ID 30
36 #define LOC_COURSE 50
37 #define LOC_YEAR 55
38 #define LOC_ADDRESS 80
39 #define LOC_DORM_ROOM 110
40 #define LOC_CITY 125
41 #define LOC_STATE 145
42 #define LOC_DPHONE 155
43 #define LOC_MPHONE 243
44
45 #define LEN_NAME 30
46 #define LEN_ID 9
47 #define LEN_COURSE 5
48 #define LEN_YEAR 25
49 #define LEN_ADDRESS 30
50 #define LEN_DORM_ROOM 15
51 #define LEN_CITY 20
52 #define LEN_STATE 10
53 #define LEN_DPHONE 12
54 #define LEN_MPHONE 12
55
56 struct entry {
57     char *name;
58     char *last;
59     char *first;
60     char *middle;
61     char *title;
62     char *id;
63     char *eid;
64     char *course;
65     char *year;
66     char *address;
67     char *dorm;
68     char *city;
69     char *state;
70     char *dphone;
71     char *mphone;
72     char *class;
73 };
74
75
76 char *whoami;
77 int newfinger = 0;
78
79
80 main(argc, argv)
81 int argc;
82 char **argv;
83 ##{
84     FILE *in;
85     struct entry *e, *get_next_entry();
86     int i, wait = 0;
87     char buf[BUFSIZ], *file = NULL;
88
89     whoami = rindex(argv[0], '/');
90     if (whoami)
91       whoami++;
92     else
93       whoami = argv[0];
94
95     for (i = 1; i < argc; i++) {
96         if (!strcmp(argv[i], "-w"))
97           wait++;
98         else if (!strcmp(argv[i], "-D"))
99           setenv("ING_SET", "set printqry");
100         else if (!strcmp(argv[i], "-n"))
101           newfinger++;
102         else if (file != NULL)
103           fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
104         else
105           file = argv[i];
106     }
107
108     in = fopen(file, "r");
109     if (in == NULL) {
110         fprintf(stderr, "Unable to open %s for input\n", file);
111         exit(1);
112     }
113
114     setlinebuf(stdout);
115     setlinebuf(stderr);
116
117 ##  ingres sms
118 ##  range of u is users
119
120     while (e = get_next_entry(in)) {
121         process_entry(e);
122         if (wait) {
123             printf("Next");
124             fflush(stdout);
125             gets(buf);
126         }
127     }
128
129 ##  exit
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 ##  char *first, *last, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
240 ##  char *rophone, *rhphone;
241 ##  char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
242 ##  int id, status;
243
244     first = e->first;
245     if (strlen(first) > 16)
246       first[16] = 0;
247     last = e->last;
248     if (strlen(last) > 16)
249       last[16] = 0;
250     eid = e->eid;
251     sid = e->id;
252     id = 0;
253     encrypted = 0;
254
255 /* Get user info */
256 ##  repeat retrieve (id = u.users_id, class = u.mit_year, haddr = u.home_addr,
257 ##            hphone = u.home_phone, ophone = u.office_phone,
258 ##            status = u.#status, dept = u.mit_dept)
259 ##      where u.#last = @last and u.#first = @first and u.mit_id = @sid
260     if (id == 0) {
261 ##      repeat retrieve (id = u.users_id, class = u.mit_year, haddr = u.home_addr,
262 ##            hphone = u.home_phone, ophone = u.office_phone,
263 ##            status = u.#status, dept = u.mit_dept)
264 ##      where u.#last = @last and u.#first = @first and u.mit_id = @eid
265         encrypted++;
266         if (id == 0) {
267             newuser(e);
268             return;
269         }
270     }
271
272 /* See if class changed: if it's different, and the value in the database
273  * is not STAFF or SIPB, then update the database.  Since they were on the
274  * students tape, make the account usable.
275  */
276     if (strcmp(e->class, strtrim(class)) &&
277         strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
278         com_err(whoami, 0, "updating class for user %s %s from %s to %s",
279                 first, last, class, e->class);
280         if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
281         if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
282         strcpy(class, e->class);
283 ##      repeat replace u (mit_year = @class, #status = @status,
284 ##                 modtime = "now", modby = WHO, modwith = PROG)
285 ##          where u.users_id = @id
286     }
287
288     /* Deal with updating the finger info if necessary */
289
290     changed = nochange = 0;
291     strcpy(buf, e->address);
292     if (*e->dorm) {
293         strcat(buf, " ");
294         strcat(buf, e->dorm);
295     }
296     if (*e->city) {
297         strcat(buf, " ");
298         strcat(buf, e->city);
299     }
300     FixCase(buf);
301     if (*e->state) {
302         strcat(buf, " ");
303         strcat(buf, e->state);
304     }
305     while (to = index(buf, ','))
306       *to = ';';
307     while (to = index(buf, ':'))
308       *to = ';';
309     if (newfinger) {
310         if (haddr[0] == ' ') {
311             strncpy(haddr, buf, 80);
312             haddr[80] = 0;
313             changed++;
314         } else if (strncmp(strtrim(haddr), buf, 80))
315           nochange++;
316     } else {
317         if (strncmp(strtrim(haddr), buf, 80))
318           changed++;
319         strncpy(haddr, buf, 80);
320         haddr[80] = 0;
321     }
322     from = e->dphone;
323     to = buf;
324     while (*from) {
325         if (isdigit(*from))
326           *to++ = *from;
327         from++;
328     }
329     *to = 0;
330     if (newfinger) {
331         if (hphone[0] == ' ') {
332             strncpy(hphone, buf, 16);
333             hphone[16] = 0;
334         } else if (strncmp(strtrim(hphone), buf, 16))
335           nochange++;
336     } else {
337         if (strncmp(strtrim(hphone), buf, 16))
338           changed++;
339         strncpy(hphone, buf, 16);
340         hphone[16] = 0;
341     }
342     from = e->mphone;
343     to = buf;
344     while (*from) {
345         if (isdigit(*from))
346           *to++ = *from;
347         from++;
348     }
349     *to = 0;
350     if (newfinger) {
351         if (ophone[0] == ' ') {
352             strncpy(ophone, buf, 12);
353             ophone[12] = 0;
354         } else if (strncmp(strtrim(ophone), buf, 12))
355           nochange++;
356     } else {
357         if (strncmp(strtrim(ophone), buf, 12))
358           changed++;
359         strncpy(ophone, buf, 12);
360         ophone[12] = 0;
361     }
362     e->course = e->course;
363     if (newfinger) {
364         if (dept[0] == ' ') {
365             strncpy(dept, e->course, 12);
366             dept[12] = 0;
367         } else if (strncmp(strtrim(dept), e->course, 11))
368           nochange++;
369     } else {
370         if (strncmp(strtrim(dept), e->course, 11))
371           changed++;
372         strncpy(dept, e->course, 12);
373         dept[12] = 0;
374     }
375     sid = e->id;
376     name = e->name;
377     rdept = e->course;
378     rtitle = e->title;
379     rophone = e->mphone;
380     rhphone = e->dphone;
381     strcpy(raddr, e->address);
382     if (*e->dorm) {
383         strcat(raddr, " ");
384         strcat(raddr, e->dorm);
385     }
386     if (*e->city) {
387         strcat(raddr, " ");
388         strcat(raddr, e->city);
389     }
390     FixCase(raddr);
391     if (*e->state) {
392         strcat(raddr, " ");
393         strcat(raddr, e->state);
394     }
395     if (changed) {
396         com_err(whoami, 0, "updating finger for %s %s", first, last);
397 ##      repeat replace u (home_addr = @haddr, home_phone = @hphone,
398 ##                 office_phone = @ophone, #mit_dept = @dept,
399 ##                 fmodtime = "now", fmodby = WHO, fmodwith = PROG,
400 ##                 xname = @name, xdept = @rdept, xtitle = @rtitle,
401 ##                 xaddress = @raddr, xphone1 = @rhphone, xphone2 = @rophone,
402 ##                 xmodtime = date("now"), mit_id = @sid)
403 ##          where u.users_id = @id
404     }  else {
405 ##      repeat replace u (xname = @name, xdept = @rdept, xtitle = @rtitle,
406 ##                 xaddress = @raddr, xphone1 = @rhphone, xphone2 = @rophone,
407 ##                 xmodtime = date("now"), mit_id = @sid)
408 ##          where u.users_id = @id
409     }
410 ##}
411
412
413 newuser(e)
414 struct entry *e;
415 ##{
416     char buf[512], *from, *to;
417 ##  int id, uid;
418 ##  char *last, *first, *class, *middle, login[9], *sid, fullname[65];
419 ##  char haddr[81], hphone[17], ophone[13], dept[24], *title;
420
421
422     strcpy(buf, e->address);
423     if (*e->dorm) {
424         strcat(buf, " ");
425         strcat(buf, e->dorm);
426     }
427     if (*e->city) {
428         strcat(buf, " ");
429         strcat(buf, e->city);
430     }
431     if (*e->state) {
432         strcat(buf, " ");
433         strcat(buf, e->state);
434     }
435     strncpy(haddr, buf, 80);
436     from = e->dphone;
437     to = buf;
438     while (*from) {
439         if (isdigit(*from))
440           *to++ = *from;
441         from++;
442     }
443     *to = 0;
444     strncpy(hphone, buf, 16);
445     from = e->mphone;
446     to = buf;
447     while (*from) {
448         if (isdigit(*from))
449           *to++ = *from;
450         from++;
451     }
452     *to = 0;
453     strncpy(ophone, buf, 12);
454     strncpy(dept, e->course, 12);
455     
456     id = set_next_object_id("users_id", 0);
457     uid = set_next_object_id("uid", 1);
458     sprintf(login, "#%d", uid);
459     last = e->last;
460     first = e->first;
461     middle = e->middle;
462     sid = e->id;
463     class = e->class;
464     title = e->title;
465     if (*middle)
466       sprintf(fullname, "%s %s %s", first, middle, last);
467     else
468       sprintf(fullname, "%s %s", first, last);
469     
470 ##  append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
471 ##                #last = last, #first = first, #middle = middle, status = 0,
472 ##                #mit_id = sid, #mit_year = class,
473 ##                modtime = "now", modby = WHO, modwith = PROG,
474 ##                #fullname = fullname, home_addr = haddr, home_phone = hphone,
475 ##                office_phone = ophone, #mit_dept = dept,
476 ##                fmodtime = "now", fmodby = WHO, fmodwith = PROG,
477 ##                potype = "NONE",
478 ##                xname = fullname, xdept = dept, xtitle = title,
479 ##                xaddress = haddr, xphone1 = hphone, xphone2 = ophone,
480 ##                xmodtime = date("now"))
481
482     com_err(whoami, 0, "adding user %s %s", e->first, e->last);
483 ##}
484
485
486 set_next_object_id(object, limit)
487     char *object;
488     int limit;
489 ##{
490 ##  char *name;
491 ##  int rowcount, exists, value;
492
493     name = object;
494 ##  begin transaction
495 ##  repeat retrieve (value = values.#value) where values.#name = @name
496 ##  inquire_equel(rowcount = "rowcount")
497     if (rowcount != 1) {
498 ##      abort
499         return(0);
500     }
501
502 ##  retrieve (exists = any(users.name where users.name = value))
503 ##  inquire_equel(rowcount = "rowcount")
504     if (rowcount != 1) {
505 ##      abort
506         return(0);
507     }
508     while (exists) {
509         value++;
510         if (limit && value > MAX_ID_VALUE)
511             value = MIN_ID_VALUE;
512 ##      retrieve (exists = any(users.name where users.name = value))
513     }
514
515 ##  repeat replace values (#value = @value) where values.#name = @name
516 ##  end transaction
517     return(value);
518 ##}
519
520
This page took 0.179229 seconds and 5 git commands to generate.