]> andersk Git - moira.git/blob - gen/pobox.dc
f0fd135a6d338dc6edfa6e8936af5ead6ce820d1
[moira.git] / gen / pobox.dc
1 /* $Header$
2  *
3  * This generates a list of everyone's poboxes for the mitdir.
4  *
5  *  (c) Copyright 1992 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 <stdio.h>
12 #include <moira.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #include <ctype.h>
17 EXEC SQL INCLUDE sqlca;
18
19 extern int errno;
20 char *whoami = "pobox.gen";
21
22
23 main(argc, argv)
24 int argc;
25 char **argv;
26 {
27     FILE *out = stdout;
28     char *outf = NULL, outft[64];
29     struct stat sb;
30     int flag, i;
31     EXEC SQL BEGIN DECLARE SECTION;
32     char login[9], id[18], string[257];
33     EXEC SQL END DECLARE SECTION;
34
35 #ifsql INGRES
36     EXEC SQL CONNECT moira;
37     EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
38 #endsql
39 #ifsql INFORMIX
40     EXEC SQL DATABASE moira;
41 #endsql
42
43     if (argc == 2) {
44         if (stat(argv[1], &sb) == 0) {
45             if (ModDiff (&flag, "users", sb.st_mtime))
46               exit(MR_DATE);
47             if (flag < 0) {
48                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
49                         argv[1]);
50                 exit(MR_NO_CHANGE);
51             }
52         }
53         outf = argv[1];
54         sprintf(outft, "%s~", outf);
55         if ((out = fopen(outft, "w")) == NULL) {
56             fprintf(stderr, "unable to open %s for output\n", outf);
57             exit(MR_OCONFIG);
58         }
59     } else if (argc != 1) {
60         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
61         exit(MR_ARGS);
62     } else {
63         outf = NULL;
64     }
65
66     /* The following is declarative, not executed,
67      * and so is dependent on where it is in the file,
68      * not in the order of execution of statements.
69      */
70     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
71
72     EXEC SQL DECLARE x CURSOR FOR SELECT 
73       clearid, login
74         FROM users WHERE status != 3 and potype='POP';
75     EXEC SQL OPEN x;
76     while (1) {
77         EXEC SQL FETCH x INTO :id, :login;
78         if (sqlca.sqlcode != 0) break;
79         strtrim(login);
80         strtrim(id);
81         if (isdigit(id[0]))
82           fprintf(out, "%s %s@MIT.EDU\n", id, login);
83     }
84
85     EXEC SQL CLOSE x;
86
87     EXEC SQL DECLARE y CURSOR FOR SELECT 
88       u.clearid, s.string
89         FROM users u, strings s
90         WHERE u.status != 3 and u.potype='SMTP' and u.box_id = s.string_id;
91     EXEC SQL OPEN y;
92     while (1) {
93         EXEC SQL FETCH y INTO :id, :string;
94         if (sqlca.sqlcode != 0) break;
95         strtrim(string);
96         strtrim(id);
97         if (isdigit(id[0])) {
98             if ((i = strlen(string)) > 7 &&
99                 !strcasecmp(".local", string + i - 6)) {
100                 string[i-6] = 0;
101                 fprintf(out, "%s %s.mit.edu\n", id, string);
102             } else
103               fprintf(out, "%s %s\n", id, string);
104         }
105     }
106
107     EXEC SQL CLOSE y;
108
109 #ifsql INGRES
110     EXEC SQL DISCONNECT;
111 #endsql
112 #ifsql INFORMIX
113     EXEC SQL CLOSE DATABASE;
114 #endsql
115
116     if (fclose(out)) {
117         perror("close failed");
118         exit(MR_CCONFIG);
119     }
120     if (outf)
121       fix_file(outf);
122     exit(MR_SUCCESS);
123
124  sqlerr:
125     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
126 #ifsql INGRES
127     if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
128       exit(MR_DEADLOCK);
129 #endsql
130     critical_alert("DCM", "Pobox build encountered INGRES ERROR %d",
131                    sqlca.sqlcode);
132     exit(MR_INGRES_ERR);
133 }
This page took 0.085281 seconds and 3 git commands to generate.