]>
Commit | Line | Data |
---|---|---|
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[MAXPATHLEN]; | |
33 | struct stat sb; | |
34 | int flag, i; | |
35 | EXEC SQL BEGIN DECLARE SECTION; | |
36 | char login[USERS_LOGIN_SIZE], id[USERS_CLEARID_SIZE]; | |
37 | char string[STRINGS_STRING_SIZE]; | |
38 | EXEC SQL END DECLARE SECTION; | |
39 | ||
40 | EXEC SQL CONNECT :db; | |
41 | ||
42 | if (argc == 2) | |
43 | { | |
44 | if (stat(argv[1], &sb) == 0) | |
45 | { | |
46 | if (ModDiff(&flag, "users", sb.st_mtime)) | |
47 | exit(MR_DATE); | |
48 | if (flag < 0) | |
49 | { | |
50 | fprintf(stderr, "File %s does not need to be rebuilt.\n", | |
51 | argv[1]); | |
52 | exit(MR_NO_CHANGE); | |
53 | } | |
54 | } | |
55 | outf = argv[1]; | |
56 | sprintf(outft, "%s~", outf); | |
57 | if (!(out = fopen(outft, "w"))) | |
58 | { | |
59 | fprintf(stderr, "unable to open %s for output\n", outf); | |
60 | exit(MR_OCONFIG); | |
61 | } | |
62 | } | |
63 | else if (argc != 1) | |
64 | { | |
65 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
66 | exit(MR_ARGS); | |
67 | } | |
68 | else | |
69 | outf = NULL; | |
70 | ||
71 | /* The following is declarative, not executed, | |
72 | * and so is dependent on where it is in the file, | |
73 | * not in the order of execution of statements. | |
74 | */ | |
75 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
76 | ||
77 | EXEC SQL DECLARE x CURSOR FOR SELECT | |
78 | clearid, login | |
79 | FROM users WHERE status != 3 and potype = 'POP'; | |
80 | EXEC SQL OPEN x; | |
81 | while (1) | |
82 | { | |
83 | EXEC SQL FETCH x INTO :id, :login; | |
84 | if (sqlca.sqlcode) | |
85 | break; | |
86 | strtrim(login); | |
87 | strtrim(id); | |
88 | if (isdigit(id[0])) | |
89 | fprintf(out, "%s %s@MIT.EDU\n", id, login); | |
90 | } | |
91 | ||
92 | EXEC SQL CLOSE x; | |
93 | ||
94 | EXEC SQL DECLARE y CURSOR FOR SELECT | |
95 | u.clearid, s.string | |
96 | FROM users u, strings s | |
97 | WHERE u.status != 3 and u.potype = 'SMTP' and u.box_id = s.string_id; | |
98 | EXEC SQL OPEN y; | |
99 | while (1) | |
100 | { | |
101 | EXEC SQL FETCH y INTO :id, :string; | |
102 | if (sqlca.sqlcode) | |
103 | break; | |
104 | strtrim(string); | |
105 | strtrim(id); | |
106 | if (isdigit(id[0])) | |
107 | { | |
108 | if ((i = strlen(string)) > 7 && | |
109 | !strcasecmp(".local", string + i - 6)) | |
110 | { | |
111 | string[i - 6] = '\0'; | |
112 | fprintf(out, "%s %s.mit.edu\n", id, string); | |
113 | } | |
114 | else if (!strchr(string, '@')) | |
115 | fprintf(out, "%s %s@mit.edu\n", id, string); | |
116 | else | |
117 | fprintf(out, "%s %s\n", id, string); | |
118 | } | |
119 | } | |
120 | ||
121 | EXEC SQL CLOSE y; | |
122 | ||
123 | EXEC SQL COMMIT; | |
124 | ||
125 | if (fclose(out)) | |
126 | { | |
127 | perror("close failed"); | |
128 | exit(MR_CCONFIG); | |
129 | } | |
130 | if (outf) | |
131 | fix_file(outf); | |
132 | exit(MR_SUCCESS); | |
133 | ||
134 | sqlerr: | |
135 | db_error(sqlca.sqlcode); | |
136 | exit(MR_DBMS_ERR); | |
137 | } |