]> andersk Git - moira.git/blob - regtape/students.dc
don't change away from SIPB either
[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, ends_sr;
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     if (fgets(buf, sizeof(buf), in) == NULL)
151       return((struct entry *)NULL);
152     buf[LEN_NAME] = 0;
153     p = index(&buf[LOC_NAME], ',');
154     if (p)
155       *p = 0;
156     e.last = strtrim(&buf[LOC_NAME]);
157     if (p) {
158         e.first = p + 2;
159         if (p = index(e.first, ' ')) {
160             *p = 0;
161             e.middle = strtrim(p + 1);
162         } else
163           e.middle = "";
164     } else
165       e.first = "";
166     e.first = strtrim(e.first);
167     ends_jr = ends_iii = ends_iv = ends_sr = 0;
168     LookForSt(e.last);
169     LookForO(e.last);
170     LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
171     LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
172     FixCase(e.last);
173     FixCase(e.first);
174     FixCase(e.middle);
175     e.title = titlebuf;
176     titlebuf[0] = 0;
177     AppendJrOrIII(titlebuf, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
178
179     e.id = &buf[LOC_ID];
180     e.id[LEN_ID] = 0;
181     e.eid = eid;
182     EncryptID(e.eid, e.id, e.first, e.last);
183
184     e.course = &buf[LOC_COURSE];
185     e.course[LEN_COURSE] = 0;
186     e.year = &buf[LOC_YEAR];
187     e.year[LEN_YEAR] = 0;
188     if (e.year[0] == 'G')
189       e.class = "G";
190     else {
191         e.class = classbuf;
192         sprintf(classbuf, "%d", year + 4 - atoi(e.year) + 1900);
193     }
194     e.address = &buf[LOC_ADDRESS];
195     e.address[LEN_ADDRESS] = 0;
196     e.dorm = &buf[LOC_DORM_ROOM];
197     e.dorm[LEN_DORM_ROOM] = 0;
198     e.city = &buf[LOC_CITY];
199     e.city[LEN_CITY] = 0;
200     e.state = &buf[LOC_STATE];
201     e.state[LEN_STATE] = 0;
202     e.dphone = &buf[LOC_DORM_PHONE];
203     e.dphone[LEN_DPHONE] = 0;
204     e.mphone = &buf[LOC_MIT_PHONE1];
205     e.mphone[LEN_MPHONE] = 0;
206     return(&e);
207 }
208
209
210 process_entry(e)
211 struct entry *e;
212 ##{
213     int changed, nochange;
214     char buf[BUFSIZ], *from, *to;
215 ##  char *first, *last, *eid;
216 ##  char class[9], haddr[81], hphone[17], ophone[13], dept[13];
217 ##  int id;
218
219     first = e->first;
220     last = e->last;
221     eid = e->eid;
222     id = 0;
223 ##  repeat retrieve (id = u.users_id, class = u.mit_year, haddr = u.home_addr,
224 ##            hphone = u.home_phone, ophone = u.office_phone,
225 ##            dept = u.mit_dept)
226 ##      where u.#last = @last and u.#first = @first and u.mit_id = @eid
227     if (id == 0) {
228         newuser(e);
229         return;
230     }
231     if (strcmp(e->class, strtrim(class)) &&
232         strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
233         com_err(whoami, 0, "updating class for user %s %s from %s to %s",
234                 first, last, class, e->class);
235         strcpy(class, e->class);
236 ##      repeat replace u (mit_year = @class,
237 ##                 modtime = "now", modby = WHO, modwith = PROG)
238 ##          where u.users_id = @id
239     }
240     changed = nochange = 0;
241     strcpy(buf, strtrim(e->address));
242     e->dorm = strtrim(e->dorm);
243     if (*e->dorm) {
244         strcat(buf, " ");
245         strcat(buf, e->dorm);
246     }
247     e->city = strtrim(e->city);
248     if (*e->city) {
249         strcat(buf, " ");
250         strcat(buf, e->city);
251     }
252     FixCase(buf);
253     e->state = strtrim(e->state);
254     if (*e->state) {
255         strcat(buf, " ");
256         strcat(buf, e->state);
257     }
258     while (to = index(buf, ','))
259       *to = ';';
260     while (to = index(buf, ':'))
261       *to = ';';
262     if (newfinger) {
263         if (haddr[0] == ' ') {
264             strncpy(haddr, buf, 80);
265             haddr[80] = 0;
266             changed++;
267         } else if (strncmp(strtrim(haddr), buf, 80))
268           nochange++;
269     } else {
270         if (strncmp(strtrim(haddr), buf, 80))
271           changed++;
272         strncpy(haddr, buf, 80);
273         haddr[80] = 0;
274     }
275     from = e->dphone;
276     to = buf;
277     while (*from) {
278         if (isdigit(*from))
279           *to++ = *from;
280         from++;
281     }
282     *to = 0;
283     if (newfinger) {
284         if (hphone[0] == ' ') {
285             strncpy(hphone, buf, 16);
286             hphone[16] = 0;
287         } else if (strncmp(strtrim(hphone), buf, 16))
288           nochange++;
289     } else {
290         if (strncmp(strtrim(hphone), buf, 16))
291           changed++;
292         strncpy(hphone, buf, 16);
293         hphone[16] = 0;
294     }
295     from = e->mphone;
296     to = buf;
297     while (*from) {
298         if (isdigit(*from))
299           *to++ = *from;
300         from++;
301     }
302     *to = 0;
303     if (newfinger) {
304         if (ophone[0] == ' ') {
305             strncpy(ophone, buf, 12);
306             ophone[12] = 0;
307         } else if (strncmp(strtrim(ophone), buf, 12))
308           nochange++;
309     } else {
310         if (strncmp(strtrim(ophone), buf, 12))
311           changed++;
312         strncpy(ophone, buf, 12);
313         ophone[12] = 0;
314     }
315     e->course = strtrim(e->course);
316     if (newfinger) {
317         if (dept[0] == ' ') {
318             strncpy(dept, e->course, 12);
319             dept[12] = 0;
320         } else if (strncmp(strtrim(dept), e->course, 11))
321           nochange++;
322     } else {
323         if (strncmp(strtrim(dept), e->course, 11))
324           changed++;
325         strncpy(dept, e->course, 12);
326         dept[12] = 0;
327     }
328     if (changed) {
329         com_err(whoami, 0, "updating finger for %s %s", first, last);
330 ##      repeat replace u (home_addr = @haddr, home_phone = @hphone,
331 ##                 office_phone = @ophone, #mit_dept = @dept,
332 ##                 fmodtime = "now", fmodby = WHO, fmodwith = PROG)
333 ##          where u.users_id = @id
334     } else if (nochange)
335       com_err(whoami, 0, "NOT updating finger for %s %s", first, last);
336 ##}
337
338
339 newuser(e)
340 struct entry *e;
341 ##{
342     char buf[512], *from, *to;
343 ##  int id, uid;
344 ##  char *last, *first, *class, *middle, login[9], *eid, fullname[65];
345 ##  char haddr[81], hphone[17], ophone[13], dept[13];
346
347
348     strcpy(buf, strtrim(e->address));
349     if (*e->dorm) {
350         strcat(buf, " ");
351         strcat(buf, strtrim(e->dorm));
352     }
353     if (*e->city) {
354         strcat(buf, " ");
355         strcat(buf, strtrim(e->city));
356     }
357     if (*e->state) {
358         strcat(buf, " ");
359         strcat(buf, strtrim(e->state));
360     }
361     strncpy(haddr, buf, 80);
362     from = e->dphone;
363     to = buf;
364     while (*from) {
365         if (isdigit(*from))
366           *to++ = *from;
367         from++;
368     }
369     *to = 0;
370     strncpy(hphone, buf, 16);
371     from = e->mphone;
372     to = buf;
373     while (*from) {
374         if (isdigit(*from))
375           *to++ = *from;
376         from++;
377     }
378     *to = 0;
379     strncpy(ophone, buf, 12);
380     e->course = strtrim(e->course);
381     strncpy(dept, e->course, 12);
382
383     
384     id = set_next_object_id("users_id");
385     uid = set_next_object_id("uid");
386     sprintf(login, "#%d", uid);
387     last = e->last;
388     first = e->first;
389     middle = e->middle;
390     eid = e->eid;
391     class = e->class;
392     if (*middle)
393       sprintf(fullname, "%s %s %s", first, middle, last);
394     else
395       sprintf(fullname, "%s %s", first, last);
396     
397 ##  append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
398 ##                #last = last, #first = first, #middle = middle, status = 0,
399 ##                #mit_id = eid, #mit_year = class,
400 ##                modtime = "now", modby = WHO, modwith = PROG,
401 ##                #fullname = fullname, home_addr = haddr, home_phone = hphone,
402 ##                office_phone = ophone, #mit_dept = dept,
403 ##                fmodtime = "now", fmodby = WHO, fmodwith = PROG,
404 ##                potype = "NONE")
405     com_err(whoami, 0, "adding user %s %s", e->first, e->last);
406 ##}
407
408
409 set_next_object_id(object)
410     char *object;
411 ##{
412 ##  char *name;
413 ##  int rowcount, exists, value;
414
415     name = object;
416 ##  begin transaction
417 ##  repeat retrieve (value = values.#value) where values.#name = @name
418 ##  inquire_equel(rowcount = "rowcount")
419     if (rowcount != 1) {
420 ##      abort
421         return(0);
422     }
423
424 ##  retrieve (exists = any(users.name where users.name = value))
425 ##  inquire_equel(rowcount = "rowcount")
426     if (rowcount != 1) {
427 ##      abort
428         return(0);
429     }
430     while (exists) {
431         value++;
432         if (value > MAX_ID_VALUE)
433             value = MIN_ID_VALUE;
434 ##      retrieve (exists = any(users.name where users.name = value))
435     }
436
437 ##  repeat replace values (#value = @value) where values.#name = @name
438 ##  end transaction
439     return(value);
440 ##}
441
442
This page took 0.147421 seconds and 5 git commands to generate.