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