]> andersk Git - moira.git/blob - gen/pobox.pc
c164080c7e2c7b984c0d134fc6f0ff2b264aa046
[moira.git] / gen / pobox.pc
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 <errno.h>
12 #include <stdio.h>
13 #include <moira.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 #include <ctype.h>
18 EXEC SQL INCLUDE sqlca;
19
20 char *whoami = "pobox.gen";
21 char *db = "moira/moira";
22
23 int main(int argc, char **argv)
24 {
25   FILE *out = stdout;
26   char *outf = NULL, outft[64];
27   struct stat sb;
28   int flag, i;
29   EXEC SQL BEGIN DECLARE SECTION;
30   char login[9], id[18], string[257];
31   EXEC SQL END DECLARE SECTION;
32
33   EXEC SQL CONNECT :db;
34
35   if (argc == 2)
36     {
37       if (stat(argv[1], &sb) == 0)
38         {
39           if (ModDiff(&flag, "users", sb.st_mtime))
40             exit(MR_DATE);
41           if (flag < 0)
42             {
43               fprintf(stderr, "File %s does not need to be rebuilt.\n",
44                       argv[1]);
45               exit(MR_NO_CHANGE);
46             }
47         }
48       outf = argv[1];
49       sprintf(outft, "%s~", outf);
50       if (!(out = fopen(outft, "w")))
51         {
52           fprintf(stderr, "unable to open %s for output\n", outf);
53           exit(MR_OCONFIG);
54         }
55     }
56   else if (argc != 1)
57     {
58       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
59       exit(MR_ARGS);
60     }
61   else
62     outf = NULL;
63
64   /* The following is declarative, not executed,
65    * and so is dependent on where it is in the file,
66    * not in the order of execution of statements.
67    */
68   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
69
70   EXEC SQL DECLARE x CURSOR FOR SELECT
71     clearid, login
72     FROM users WHERE status != 3 and potype = 'POP';
73   EXEC SQL OPEN x;
74   while (1)
75     {
76       EXEC SQL FETCH x INTO :id, :login;
77       if (sqlca.sqlcode)
78         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     {
94       EXEC SQL FETCH y INTO :id, :string;
95       if (sqlca.sqlcode)
96         break;
97       strtrim(string);
98       strtrim(id);
99       if (isdigit(id[0]))
100         {
101           if ((i = strlen(string)) > 7 &&
102               !strcasecmp(".local", string + i - 6))
103             {
104               string[i - 6] = '\0';
105               fprintf(out, "%s %s.mit.edu\n", id, string);
106             }
107           else if (!strchr(string, '@'))
108             fprintf(out, "%s %s@mit.edu\n", id, string);
109           else
110             fprintf(out, "%s %s\n", id, string);
111         }
112     }
113
114   EXEC SQL CLOSE y;
115
116   EXEC SQL COMMIT;
117
118   if (fclose(out))
119     {
120       perror("close failed");
121       exit(MR_CCONFIG);
122     }
123   if (outf)
124     fix_file(outf);
125   exit(MR_SUCCESS);
126
127 sqlerr:
128   db_error(sqlca.sqlcode);
129   exit(MR_DBMS_ERR);
130 }
This page took 0.035396 seconds and 3 git commands to generate.