]> andersk Git - moira.git/blob - regtape/students.dc
new option to add xuser records
[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    32766
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 159
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 14
53 #define LEN_DPHONE 9
54 #define LEN_MPHONE 9
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 int addxuser = 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 (!strcmp(argv[i], "-u"))
104           addxuser++;
105         else if (file != NULL)
106           fprintf(stderr, "Usage: %s [-w] [-D] [-n] [-u] 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 ##  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;
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     } else {
192         e.first = "";
193         e.middle = "";
194     }
195     ends_jr = ends_iii = ends_iv = ends_sr = 0;
196     LookForSt(e.last);
197     LookForO(e.last);
198     LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
199     LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
200     FixCase(e.last);
201     FixCase(e.first);
202     FixCase(e.middle);
203
204     e.id = id;
205     e.id[LEN_ID] = 0;
206     e.eid = eid;
207     EncryptID(e.eid, e.id, e.first, e.last);
208
209     e.year = strtrim(year);
210     e.title = title;
211     if (e.year[0] == 'G') {
212         e.class = "G";
213         sprintf(title, "Grad Student");
214     } else {
215         e.class = classbuf;
216         sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
217         sprintf(title, "Undergrad (class of %s)", classbuf);
218     }
219
220     e.course = strtrim(course);
221     e.address = strtrim(address);
222     e.dorm = strtrim(dorm_room);
223     e.city = strtrim(city);
224     e.state = strtrim(state);
225     e.dphone = strtrim(dphone);
226     e.mphone = strtrim(mphone);
227     return(&e);
228 }
229
230
231 process_entry(e)
232 struct entry *e;
233 ##{
234     int changed, nochange;
235     char buf[BUFSIZ], *from, *to;
236 ##  char *first, *last, *eid, *title, *sid, *name;
237 ##  char class[9], haddr[128], hphone[17], ophone[13], dept[24];
238 ##  int id, status;
239
240     first = e->first;
241     if (strlen(first) > 16)
242       first[16] = 0;
243     last = e->last;
244     if (strlen(last) > 16)
245       last[16] = 0;
246     eid = e->eid;
247     id = 0;
248 ##  repeat retrieve (id = u.users_id, class = u.mit_year, haddr = u.home_addr,
249 ##            hphone = u.home_phone, ophone = u.office_phone,
250 ##            status = u.#status, dept = u.mit_dept)
251 ##      where u.#last = @last and u.#first = @first and u.mit_id = @eid
252     if (id == 0) {
253         newuser(e);
254         return;
255     }
256     if (strcmp(e->class, strtrim(class)) &&
257         strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
258         com_err(whoami, 0, "updating class for user %s %s from %s to %s",
259                 first, last, class, e->class);
260         if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
261         if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
262         strcpy(class, e->class);
263 ##      repeat replace u (mit_year = @class, #status = @status, ugdefault = 1,
264 ##                 modtime = "now", modby = WHO, modwith = PROG)
265 ##          where u.users_id = @id
266     }
267     changed = nochange = 0;
268     strcpy(buf, e->address);
269     if (*e->dorm) {
270         strcat(buf, " ");
271         strcat(buf, e->dorm);
272     }
273     if (*e->city) {
274         strcat(buf, " ");
275         strcat(buf, e->city);
276     }
277     FixCase(buf);
278     if (*e->state) {
279         strcat(buf, " ");
280         strcat(buf, e->state);
281     }
282     while (to = index(buf, ','))
283       *to = ';';
284     while (to = index(buf, ':'))
285       *to = ';';
286     if (newfinger) {
287         if (haddr[0] == ' ') {
288             strncpy(haddr, buf, 80);
289             haddr[80] = 0;
290             changed++;
291         } else if (strncmp(strtrim(haddr), buf, 80))
292           nochange++;
293     } else {
294         if (strncmp(strtrim(haddr), buf, 80))
295           changed++;
296         strncpy(haddr, buf, 80);
297         haddr[80] = 0;
298     }
299     from = e->dphone;
300     to = buf;
301     while (*from) {
302         if (isdigit(*from))
303           *to++ = *from;
304         from++;
305     }
306     *to = 0;
307     if (newfinger) {
308         if (hphone[0] == ' ') {
309             strncpy(hphone, buf, 16);
310             hphone[16] = 0;
311         } else if (strncmp(strtrim(hphone), buf, 16))
312           nochange++;
313     } else {
314         if (strncmp(strtrim(hphone), buf, 16))
315           changed++;
316         strncpy(hphone, buf, 16);
317         hphone[16] = 0;
318     }
319     from = e->mphone;
320     to = buf;
321     while (*from) {
322         if (isdigit(*from))
323           *to++ = *from;
324         from++;
325     }
326     *to = 0;
327     if (newfinger) {
328         if (ophone[0] == ' ') {
329             strncpy(ophone, buf, 12);
330             ophone[12] = 0;
331         } else if (strncmp(strtrim(ophone), buf, 12))
332           nochange++;
333     } else {
334         if (strncmp(strtrim(ophone), buf, 12))
335           changed++;
336         strncpy(ophone, buf, 12);
337         ophone[12] = 0;
338     }
339     e->course = e->course;
340     if (newfinger) {
341         if (dept[0] == ' ') {
342             strncpy(dept, e->course, 12);
343             dept[12] = 0;
344         } else if (strncmp(strtrim(dept), e->course, 11))
345           nochange++;
346     } else {
347         if (strncmp(strtrim(dept), e->course, 11))
348           changed++;
349         strncpy(dept, e->course, 12);
350         dept[12] = 0;
351     }
352     if (changed) {
353         com_err(whoami, 0, "updating finger for %s %s", first, last);
354 ##      repeat replace u (home_addr = @haddr, home_phone = @hphone,
355 ##                 office_phone = @ophone, #mit_dept = @dept, ugdefault = 1,
356 ##                 fmodtime = "now", fmodby = WHO, fmodwith = PROG)
357 ##          where u.users_id = @id
358     } /* else if (nochange)
359       com_err(whoami, 0, "NOT updating finger for %s %s", first, last);
360        */
361     if (!changed && !strcmp(e->class, class)) {
362 ##      repeat replace u (ugdefault = 1) where u.users_id = @id
363     }
364     if (addxuser) {
365         sid = e->id;
366         name = e->name;
367         strcpy(dept, e->course);
368         title = e->title;
369         strcpy(haddr, e->address);
370         if (*e->dorm) {
371             strcat(haddr, " ");
372             strcat(haddr, e->dorm);
373         }
374         if (*e->city) {
375             strcat(haddr, " ");
376             strcat(haddr, e->city);
377         }
378         FixCase(haddr);
379         if (*e->state) {
380             strcat(haddr, " ");
381             strcat(haddr, e->state);
382         }
383         
384 ##      append xuser (users_id = id, #id = sid, #name = name, #dept = dept,
385 ##                    #title = title, address = haddr, #phone1 = hphone,
386 ##                    #phone2 = ophone, modtime = "now")
387     }
388 ##}
389
390
391 newuser(e)
392 struct entry *e;
393 ##{
394     char buf[512], *from, *to;
395 ##  int id, uid;
396 ##  char *last, *first, *class, *middle, login[9], *eid, fullname[65];
397 ##  char haddr[81], hphone[17], ophone[13], dept[24], *sid, *title;
398
399
400     strcpy(buf, e->address);
401     if (*e->dorm) {
402         strcat(buf, " ");
403         strcat(buf, e->dorm);
404     }
405     if (*e->city) {
406         strcat(buf, " ");
407         strcat(buf, e->city);
408     }
409     if (*e->state) {
410         strcat(buf, " ");
411         strcat(buf, e->state);
412     }
413     strncpy(haddr, buf, 80);
414     from = e->dphone;
415     to = buf;
416     while (*from) {
417         if (isdigit(*from))
418           *to++ = *from;
419         from++;
420     }
421     *to = 0;
422     strncpy(hphone, buf, 16);
423     from = e->mphone;
424     to = buf;
425     while (*from) {
426         if (isdigit(*from))
427           *to++ = *from;
428         from++;
429     }
430     *to = 0;
431     strncpy(ophone, buf, 12);
432     strncpy(dept, e->course, 12);
433     
434     id = set_next_object_id("users_id");
435     uid = set_next_object_id("uid");
436     sprintf(login, "#%d", uid);
437     last = e->last;
438     first = e->first;
439     middle = e->middle;
440     eid = e->eid;
441     class = e->class;
442     if (*middle)
443       sprintf(fullname, "%s %s %s", first, middle, last);
444     else
445       sprintf(fullname, "%s %s", first, last);
446     
447 ##  append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
448 ##                #last = last, #first = first, #middle = middle, status = 0,
449 ##                #mit_id = eid, #mit_year = class, ugdefault = 1,
450 ##                modtime = "now", modby = WHO, modwith = PROG,
451 ##                #fullname = fullname, home_addr = haddr, home_phone = hphone,
452 ##                office_phone = ophone, #mit_dept = dept,
453 ##                fmodtime = "now", fmodby = WHO, fmodwith = PROG,
454 ##                potype = "NONE")
455
456     sid = e->id;
457     strcpy(fullname, e->name);
458     strcpy(dept, e->course);
459     title = e->title;
460     strcpy(haddr, e->address);
461     if (*e->dorm) {
462         strcat(haddr, " ");
463         strcat(haddr, e->dorm);
464     }
465     if (*e->city) {
466         strcat(haddr, " ");
467         strcat(haddr, e->city);
468     }
469     FixCase(haddr);
470     if (*e->state) {
471         strcat(haddr, " ");
472         strcat(haddr, e->state);
473     }
474
475     if (addxuser) {
476 ##      append xuser (users_id = id, #id = sid, #name = fullname, #dept = dept,
477 ##                #title = title, address = haddr, #phone1 = hphone,
478 ##                #phone2 = ophone, modtime = "now")
479     }
480     com_err(whoami, 0, "adding user %s %s", e->first, e->last);
481 ##}
482
483
484 set_next_object_id(object)
485     char *object;
486 ##{
487 ##  char *name;
488 ##  int rowcount, exists, value;
489
490     name = object;
491 ##  begin transaction
492 ##  repeat retrieve (value = values.#value) where values.#name = @name
493 ##  inquire_equel(rowcount = "rowcount")
494     if (rowcount != 1) {
495 ##      abort
496         return(0);
497     }
498
499 ##  retrieve (exists = any(users.name where users.name = value))
500 ##  inquire_equel(rowcount = "rowcount")
501     if (rowcount != 1) {
502 ##      abort
503         return(0);
504     }
505     while (exists) {
506         value++;
507         if (value > MAX_ID_VALUE)
508             value = MIN_ID_VALUE;
509 ##      retrieve (exists = any(users.name where users.name = value))
510     }
511
512 ##  repeat replace values (#value = @value) where values.#name = @name
513 ##  end transaction
514     return(value);
515 ##}
516
517
This page took 0.096661 seconds and 5 git commands to generate.