source: trunk/third/moira/regtape/student.pc @ 26024

Revision 26024, 7.6 KB checked in by jdreed, 12 years ago (diff)
In moira: * Snapshot moira at r4113 to pick up new firewall-related changes
Line 
1/* $id: student.pc 3956 2010-01-05 20:56:56Z zacheiss $
2 *
3 * Load data into Moira from Registrar's Office data file
4 *
5 * Copyright (C) 1990-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 */
9
10#include <mit-copyright.h>
11#include <moira.h>
12#include <moira_site.h>
13#include <moira_schema.h>
14#include "common.h"
15
16#include <ctype.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21
22EXEC SQL INCLUDE sqlca;
23
24RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/regtape/student.pc $ $Id: student.pc 4111 2013-05-22 20:26:51Z zacheiss $");
25
26/* File format is:
27
280-8     MIT ID
299-38    last name
3039-68   first name
3169-98   middle name
3299      year
33100-103 department
34104-133 address
35134-173 city
36174-175 state
37176-184 zip code
38185-204 phone
39205-234 office address
40235-254 office phone
41255-304 registration type
42305-364 email address
43
44*/
45
46#define LOC_ID 0
47#define LEN_ID 9
48#define LOC_LAST (LOC_ID + LEN_ID)
49#define LEN_LAST 30
50#define LOC_FIRST (LOC_LAST + LEN_LAST)
51#define LEN_FIRST 30
52#define LOC_MIDDLE (LOC_FIRST + LEN_FIRST)
53#define LEN_MIDDLE 30
54#define LOC_YEAR (LOC_MIDDLE + LEN_MIDDLE)
55#define LEN_YEAR 1
56#define LOC_COURSE (LOC_YEAR + LEN_YEAR)
57#define LEN_COURSE 4
58#define LOC_ADDRESS (LOC_COURSE + LEN_COURSE)
59#define LEN_ADDRESS 30
60#define LOC_CITY (LOC_ADDRESS + LEN_ADDRESS)
61#define LEN_CITY 40
62#define LOC_STATE (LOC_CITY + LEN_CITY)
63#define LEN_STATE 2
64#define LOC_ZIP (LOC_STATE + LEN_STATE)
65#define LEN_ZIP 9
66#define LOC_PHONE (LOC_ZIP + LEN_ZIP)
67#define LEN_PHONE 20
68#define LOC_OADDR (LOC_PHONE + LEN_PHONE)
69#define LEN_OADDR 30
70#define LOC_OPHONE (LOC_OADDR + LEN_OADDR)
71#define LEN_OPHONE 20
72#define LOC_REG_TYPE (LOC_OPHONE + LEN_OPHONE)
73#define LEN_REG_TYPE 50
74#define LOC_EMAIL_ADDRESS (LOC_REG_TYPE + LEN_REG_TYPE)
75#define LEN_EMAIL_ADDRESS 60
76
77EXEC SQL BEGIN DECLARE SECTION;
78int who;
79char *prog = "stuload";
80EXEC SQL END DECLARE SECTION;
81
82char *whoami;
83
84struct entry *get_next_entry(FILE *in);
85void process_entry(struct entry *e, int secure);
86
87int main(int argc, char **argv)
88{
89  FILE *in;
90  struct entry *e;
91  int i, wait = 0;
92  char buf[80], *file = NULL;
93  EXEC SQL BEGIN DECLARE SECTION;
94  char *db = "moira";
95  EXEC SQL END DECLARE SECTION;
96
97  whoami = strrchr(argv[0], '/');
98  if (whoami)
99    whoami++;
100  else
101    whoami = argv[0];
102
103  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
104  setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
105
106  for (i = 1; i < argc; i++)
107    {
108      if (!strcmp(argv[i], "-w"))
109        wait++;
110      if (file)
111        {
112          fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
113          exit(1);
114        }
115      else
116        file = argv[i];
117    }
118
119  if (!file)
120    {
121      fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
122      exit(1);
123    }
124
125  in = fopen(file, "r");
126  if (!in)
127    {
128      fprintf(stderr, "Unable to open %s for input\n", file);
129      exit(1);
130    }
131
132  initialize_sms_error_table();
133
134  EXEC SQL CONNECT :db IDENTIFIED BY :db;
135  if (sqlca.sqlcode)
136    {
137      dbmserr("connecting", sqlca.sqlcode);
138      exit(1);
139    }
140
141  EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
142
143  while ((e = get_next_entry(in)))
144    {
145      /* Don't require secure registration for cross-registered or
146       * special students.
147       */
148      if (!strcmp(e->reg_type, "Cross-Registered") ||
149          !strcmp(e->reg_type, "Special"))
150        process_entry(e, 0);   
151      else
152        process_entry(e, 1);
153      EXEC SQL COMMIT WORK;
154      if (sqlca.sqlcode)
155        {
156          dbmserr("committing work", sqlca.sqlcode);
157          exit(1);
158        }
159      if (wait)
160        {
161          printf("Next");
162          fflush(stdout);
163          fgets(buf, sizeof(buf), stdin);
164        }
165    }
166
167  exit(0);
168}
169
170
171struct entry *get_next_entry(FILE *in)
172{
173  static struct entry e;
174  static char buf[BUFSIZ], classbuf[10];
175  static char namebuf[LEN_FIRST + LEN_MIDDLE + LEN_LAST + 2];
176  static char addrbuf[USERS_HOME_ADDR_SIZE], xaddrbuf[USERS_XADDRESS_SIZE];
177  static char first[LEN_FIRST + 1], last[LEN_LAST + 1], middle[LEN_MIDDLE + 1];
178  static char id[LEN_ID + 1], course[LEN_COURSE + 1];
179  static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
180  static char city[LEN_CITY + 1];
181  static char state[LEN_STATE + 1], phone[LEN_PHONE + 1];
182  static char ophone[LEN_OPHONE + 1], title[128];
183  static char zip[LEN_ZIP + 1], oaddr[LEN_OADDR + 1];
184  static char reg_type[LEN_REG_TYPE + 1];
185  static char email_address[LEN_EMAIL_ADDRESS + 1];
186  static int nyear = 0;
187  int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
188  char *p;
189
190  if (nyear == 0)
191    {
192      struct tm *tm;
193      struct timeval tv;
194
195      gettimeofday(&tv, NULL);
196      tm = localtime(&tv.tv_sec);
197      nyear = tm->tm_year;
198      if (tm->tm_mon >= 5)
199        nyear++;
200    }
201
202  if (!fgets(buf, sizeof(buf), in))
203    return NULL;
204
205  strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
206  strtrim(id);
207  strlcpy(last, &buf[LOC_LAST], LEN_LAST + 1);
208  strtrim(last);
209  strlcpy(first, &buf[LOC_FIRST], LEN_FIRST + 1);
210  strtrim(first);
211  strlcpy(middle, &buf[LOC_MIDDLE], LEN_MIDDLE + 1);
212  strtrim(middle);
213  strlcpy(year, &buf[LOC_YEAR], LEN_YEAR + 1);
214  strtrim(year);
215  strlcpy(course, &buf[LOC_COURSE], LEN_COURSE + 1);
216  strtrim(course);
217  strlcpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS + 1);
218  strtrim(address);
219  strlcpy(city, &buf[LOC_CITY], LEN_CITY + 1);
220  strtrim(city);
221  strlcpy(state, &buf[LOC_STATE], LEN_STATE + 1);
222  strtrim(state);
223  strlcpy(zip, &buf[LOC_ZIP], LEN_ZIP + 1);
224  strtrim(zip);
225  strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
226  strtrim(phone);
227  strlcpy(oaddr, &buf[LOC_OADDR], LEN_OADDR + 1);
228  strtrim(oaddr);
229  strlcpy(ophone, &buf[LOC_OPHONE], LEN_OPHONE + 1);
230  strtrim(ophone);
231  strlcpy(reg_type, &buf[LOC_REG_TYPE], LEN_REG_TYPE + 1);
232  strtrim(reg_type);
233  strlcpy(email_address, &buf[LOC_EMAIL_ADDRESS], LEN_EMAIL_ADDRESS + 1);
234  strtrim(email_address);
235
236  e.first = first;
237  e.last = last;
238  e.middle = middle;
239  ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
240  LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
241                  &ends_ii, &ends_v);
242  if (middle[1] == '.' && middle[2] == '\0')
243    middle[1] = '\0';
244  e.name = namebuf;
245  if (*middle)
246    sprintf(e.name, "%s %s %s", first, middle, last);
247  else
248    sprintf(e.name, "%s %s", first, last);
249
250  e.id = id;
251
252  e.xtitle = title;
253  if (year[0] == 'G' || year[0] == 'N')
254    {
255      e.type = "G";
256      sprintf(title, "Grad Student");
257      e.affiliation_detailed = "Graduate Student";     
258    }
259  else
260    {
261      e.type = classbuf;
262      sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900);
263      sprintf(title, "Undergrad (class of %s)", classbuf);
264      e.affiliation_detailed = "Undergraduate Student";
265    }
266
267  e.affiliation_basic = "student";
268
269  e.dept = course;
270
271  /* Used to detect cross-registered students. */
272  e.reg_type = reg_type;
273
274  e.email_address = email_address;
275
276  e.oaddr = oaddr;
277  fixaddress(e.oaddr);
278  e.ophone = e.xphone2 = ophone;
279  fixphone(e.ophone);
280
281  e.haddr = addrbuf;
282  strlcpy(e.haddr, address, sizeof(addrbuf));
283  if (*city)
284    {
285      strlcat(e.haddr, " ", sizeof(addrbuf));
286      strlcat(e.haddr, city, sizeof(addrbuf));
287    }
288  if (*state)
289    {
290      strlcat(e.haddr, " ", sizeof(addrbuf));
291      strlcat(e.haddr, state, sizeof(addrbuf));
292      strlcat(e.haddr, zip, sizeof(addrbuf));
293    }
294  fixaddress(e.haddr);
295
296  e.hphone = e.xphone1 = phone;
297  fixphone(e.hphone);
298
299  e.xaddress = xaddrbuf;
300  strlcpy(e.xaddress, address, sizeof(xaddrbuf));
301  strlcat(e.xaddress, " |", sizeof(xaddrbuf));
302  if (*city)
303    {
304      strlcat(e.xaddress, city, sizeof(xaddrbuf));
305      if (*state)
306        {
307          strlcat(e.xaddress, " ", sizeof(xaddrbuf));
308          strlcat(e.xaddress, state, sizeof(xaddrbuf));
309          strlcat(e.xaddress, zip, sizeof(xaddrbuf));
310        }
311    }
312  else
313    strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf));
314
315  return &e;
316}
Note: See TracBrowser for help on using the repository browser.