]> andersk Git - moira.git/blob - regtape/students.dc
faa8d6f63131eeea4f6c09aa573a12722c90cff4
[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 <sms.h>
9 #include <sms_app.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_DORM_PHONE 159
43 #define LOC_MIT_PHONE1 243
44
45 #define LEN_NAME 29
46 #define LEN_ID 9
47 #define LEN_COURSE 3
48 #define LEN_YEAR 23
49 #define LEN_ADDRESS 26
50 #define LEN_DORM_ROOM 14
51 #define LEN_CITY 19
52 #define LEN_STATE 12
53 #define LEN_DPHONE 9
54 #define LEN_MPHONE 9
55
56 struct entry {
57     char *last;
58     char *first;
59     char *middle;
60     char *title;
61     char *id;
62     char *eid;
63     char *course;
64     char *year;
65     char *address;
66     char *dorm;
67     char *city;
68     char *state;
69     char *dphone;
70     char *mphone;
71     char *class;
72 };
73
74
75 char *whoami;
76 int newfinger = 0;
77
78
79 main(argc, argv)
80 int argc;
81 char **argv;
82 ##{
83     FILE *in;
84     struct entry *e, *get_next_entry();
85     int i, wait = 0;
86     char buf[BUFSIZ], *file = NULL;
87
88     whoami = rindex(argv[0], '/');
89     if (whoami)
90       whoami++;
91     else
92       whoami = argv[0];
93
94     for (i = 1; i < argc; i++) {
95         if (!strcmp(argv[i], "-w"))
96           wait++;
97         else if (!strcmp(argv[i], "-D"))
98           setenv("ING_SET", "set printqry");
99         else if (!strcmp(argv[i], "-n"))
100           newfinger++;
101         else if (file != NULL)
102           fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
103         else
104           file = argv[i];
105     }
106
107     in = fopen(file, "r");
108     if (in == NULL) {
109         fprintf(stderr, "Unable to open %s for input\n", file);
110         exit(1);
111     }
112
113 ##  ingres sms
114 ##  range of u is users
115
116     while (e = get_next_entry(in)) {
117         process_entry(e);
118         if (wait) {
119             printf("Next");
120             fflush(stdout);
121             gets(buf);
122         }
123     }
124
125 ##  exit
126     exit(0);
127 ##}
128
129
130 struct entry *get_next_entry(in)
131 FILE *in;
132 {
133     static struct entry e;
134     static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
135     static int year = 0;
136     int ends_jr, ends_iii, ends_iv;
137     char *p;
138
139     if (year == 0) {
140         struct tm *tm;
141         struct timeval tv;
142
143         gettimeofday(&tv, NULL);
144         tm = localtime(&tv.tv_sec);
145         year = tm->tm_year;
146         if (tm->tm_mon > 5)
147           year++;
148     }
149
150     fgets(buf, sizeof(buf), in);
151     buf[LEN_NAME] = 0;
152     p = index(&buf[LOC_NAME], ',');
153     if (p)
154       *p = 0;
155     e.last = strtrim(&buf[LOC_NAME]);
156     if (p) {
157         e.first = p + 2;
158         if (p = index(e.first, ' ')) {
159             *p = 0;
160             e.middle = strtrim(p + 1);
161         } else
162           e.middle = "";
163     } else
164       e.first = "";
165     e.first = strtrim(e.first);
166     ends_jr = ends_iii = ends_iv = 0;
167     LookForSt(e.last);
168     LookForO(e.last);
169     LookForJrAndIII(e.last, &ends_jr, &ends_iii, &ends_iv);
170     LookForJrAndIII(e.first, &ends_jr, &ends_iii, &ends_iv);
171     FixCase(e.last);
172     FixCase(e.first);
173     FixCase(e.middle);
174     e.title = titlebuf;
175     titlebuf[0] = 0;
176     AppendJrOrIII(titlebuf, &ends_jr, &ends_iii, &ends_iv);
177
178     e.id = &buf[LOC_ID];
179     e.id[LEN_ID] = 0;
180     e.eid = eid;
181     EncryptID(e.eid, e.id, e.first, e.last);
182
183     e.course = &buf[LOC_COURSE];
184     e.course[LEN_COURSE] = 0;
185     e.year = &buf[LOC_YEAR];
186     e.year[LEN_YEAR] = 0;
187     if (e.year[0] == 'G')
188       e.class = "G";
189     else {
190         e.class = classbuf;
191         sprintf(classbuf, "%d", year + 4 - atoi(e.year) + 1900);
192     }
193     e.address = &buf[LOC_ADDRESS];
194     e.address[LEN_ADDRESS] = 0;
195     e.dorm = &buf[LOC_DORM_ROOM];
196     e.dorm[LEN_DORM_ROOM] = 0;
197     e.city = &buf[LOC_CITY];
198     e.city[LEN_CITY] = 0;
199     e.state = &buf[LOC_STATE];
200     e.state[LEN_STATE] = 0;
201     e.dphone = &buf[LOC_DORM_PHONE];
202     e.dphone[LEN_DPHONE] = 0;
203     e.mphone = &buf[LOC_MIT_PHONE1];
204     e.mphone[LEN_MPHONE] = 0;
205     return(&e);
206 }
207
208
209 process_entry(e)
210 struct entry *e;
211 ##{
212     int changed, nochange;
213     char buf[BUFSIZ], *from, *to;
214 ##  char *first, *last, *eid;
215 ##  char class[9], haddr[81], hphone[17], ophone[13], dept[13];
216 ##  int id;
217
218     first = e->first;
219     last = e->last;
220     eid = e->eid;
221     id = 0;
222 ##  repeat retrieve (id = u.users_id, class = u.mit_year, haddr = u.home_addr,
223 ##            hphone = u.home_phone, ophone = u.office_phone,
224 ##            dept = u.mit_dept)
225 ##      where u.#last = @last and u.#first = @first and u.mit_id = @eid
226     if (id == 0) {
227         newuser(e);
228         return;
229     }
230     if (strcmp(e->class, strtrim(class))) {
231         com_err(whoami, 0, "updating class for user %s %s from %s to %s",
232                 first, last, class, e->class);
233         strcpy(class, e->class);
234 ##      repeat replace u (mit_year = @class,
235 ##                 modtime = "now", modby = WHO, modwith = PROG)
236 ##          where u.users_id = @id
237     }
238     changed = nochange = 0;
239     strcpy(buf, strtrim(e->address));
240     e->dorm = strtrim(e->dorm);
241     if (*e->dorm) {
242         strcat(buf, " ");
243         strcat(buf, e->dorm);
244     }
245     e->city = strtrim(e->city);
246     if (*e->city) {
247         strcat(buf, " ");
248         strcat(buf, e->city);
249     }
250     FixCase(buf);
251     e->state = strtrim(e->state);
252     if (*e->state) {
253         strcat(buf, " ");
254         strcat(buf, e->state);
255     }
256     while (to = index(buf, ','))
257       *to = ';';
258     while (to = index(buf, ':'))
259       *to = ';';
260     if (newfinger) {
261         if (haddr[0] == ' ') {
262             strncpy(haddr, buf, 80);
263             haddr[80] = 0;
264             changed++;
265         } else if (strncmp(strtrim(haddr), buf, 80))
266           nochange++;
267     } else {
268         if (strncmp(strtrim(haddr), buf, 80))
269           changed++;
270         strncpy(haddr, buf, 80);
271         haddr[80] = 0;
272     }
273     from = e->dphone;
274     to = buf;
275     while (*from) {
276         if (isdigit(*from))
277           *to++ = *from;
278         from++;
279     }
280     *to = 0;
281     if (newfinger) {
282         if (hphone[0] == ' ') {
283             strncpy(hphone, buf, 16);
284             hphone[16] = 0;
285         } else if (strncmp(strtrim(hphone), buf, 16))
286           nochange++;
287     } else {
288         if (strncmp(strtrim(hphone), buf, 16))
289           changed++;
290         strncpy(hphone, buf, 16);
291         hphone[16] = 0;
292     }
293     from = e->mphone;
294     to = buf;
295     while (*from) {
296         if (isdigit(*from))
297           *to++ = *from;
298         from++;
299     }
300     *to = 0;
301     if (newfinger) {
302         if (ophone[0] == ' ') {
303             strncpy(ophone, buf, 12);
304             ophone[12] = 0;
305         } else if (strncmp(strtrim(ophone), buf, 12))
306           nochange++;
307     } else {
308         if (strncmp(strtrim(ophone), buf, 12))
309           changed++;
310         strncpy(ophone, buf, 12);
311         ophone[12] = 0;
312     }
313     e->course = strtrim(e->course);
314     if (newfinger) {
315         if (dept[0] == ' ') {
316             strncpy(dept, e->course, 12);
317             dept[12] = 0;
318         } else if (strncmp(strtrim(dept), e->course, 11))
319           nochange++;
320     } else {
321         if (strncmp(strtrim(dept), e->course, 11))
322           changed++;
323         strncpy(dept, e->course, 12);
324         dept[12] = 0;
325     }
326     if (changed) {
327         com_err(whoami, 0, "updating finger for %s %s", first, last);
328 ##      repeat replace u (home_addr = @haddr, home_phone = @hphone,
329 ##                 office_phone = @ophone, #mit_dept = @dept,
330 ##                 fmodtime = "now", fmodby = WHO, fmodwith = PROG)
331 ##          where u.users_id = @id
332     } else if (nochange)
333       com_err(whoami, 0, "NOT updating finger for %s %s", first, last);
334 ##}
335
336
337 newuser(e)
338 struct entry *e;
339 ##{
340     char buf[512], *from, *to;
341 ##  int id, uid;
342 ##  char *last, *first, *class, *middle, login[9], *eid, fullname[65];
343 ##  char haddr[81], hphone[17], ophone[13], dept[13];
344
345
346     strcpy(buf, strtrim(e->address));
347     if (*e->dorm) {
348         strcat(buf, " ");
349         strcat(buf, strtrim(e->dorm));
350     }
351     if (*e->city) {
352         strcat(buf, " ");
353         strcat(buf, strtrim(e->city));
354     }
355     if (*e->state) {
356         strcat(buf, " ");
357         strcat(buf, strtrim(e->state));
358     }
359     strncpy(haddr, buf, 80);
360     from = e->dphone;
361     to = buf;
362     while (*from) {
363         if (isdigit(*from))
364           *to++ = *from;
365         from++;
366     }
367     *to = 0;
368     strncpy(hphone, buf, 16);
369     from = e->mphone;
370     to = buf;
371     while (*from) {
372         if (isdigit(*from))
373           *to++ = *from;
374         from++;
375     }
376     *to = 0;
377     strncpy(ophone, buf, 12);
378     e->course = strtrim(e->course);
379     strncpy(dept, e->course, 12);
380
381     
382     id = set_next_object_id("users_id");
383     uid = set_next_object_id("uid");
384     sprintf(login, "#%d", uid);
385     last = e->last;
386     first = e->first;
387     middle = e->middle;
388     eid = e->eid;
389     class = e->class;
390     if (*middle)
391       sprintf(fullname, "%s %s %s", first, middle, last);
392     else
393       sprintf(fullname, "%s %s", first, last);
394     
395 ##  append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
396 ##                #last = last, #first = first, #middle = middle, status = 0,
397 ##                #mit_id = eid, #mit_year = class,
398 ##                modtime = "now", modby = WHO, modwith = PROG,
399 ##                #fullname = fullname, home_addr = haddr, home_phone = hphone,
400 ##                office_phone = ophone, #mit_dept = dept,
401 ##                fmodtime = "now", fmodby = WHO, fmodwith = PROG,
402 ##                potype = "NONE")
403     com_err(whoami, 0, "adding user %s %s", e->first, e->last);
404 ##}
405
406
407 set_next_object_id(object)
408     char *object;
409 ##{
410 ##  char *name;
411 ##  int rowcount, exists, value;
412
413     name = object;
414 ##  begin transaction
415 ##  repeat retrieve (value = values.#value) where values.#name = @name
416 ##  inquire_equel(rowcount = "rowcount")
417     if (rowcount != 1) {
418 ##      abort
419         return(0);
420     }
421
422 ##  retrieve (exists = any(users.name where users.name = value))
423 ##  inquire_equel(rowcount = "rowcount")
424     if (rowcount != 1) {
425 ##      abort
426         return(0);
427     }
428     while (exists) {
429         value++;
430         if (value > MAX_ID_VALUE)
431             value = MIN_ID_VALUE;
432 ##      retrieve (exists = any(users.name where users.name = value))
433     }
434
435 ##  repeat replace values (#value = @value) where values.#name = @name
436 ##  end transaction
437     return(value);
438 ##}
439
440
This page took 0.058117 seconds and 3 git commands to generate.