]> andersk Git - moira.git/blob - gen/aliases.qc
Initial revision
[moira.git] / gen / aliases.qc
1 /* $Header$
2  *
3  * This generates the /usr/lib/aliases mail aliases file for the mailhub.
4  * The aliases file will contain:
5  *      user pobox entries
6  *      maillist expansions
7  *      sublists of maillists
8  *      mail alias entries
9  */
10
11
12 #include <stdio.h>
13 #include <sms.h>
14 #include <sms_app.h>
15
16 #define ML_WID  72
17 #define AL_MAX_WID 896
18
19 char *divide = "########################################################################";
20 extern int errno;
21
22
23 main(argc, argv)
24 int argc;
25 char **argv;
26 {
27     long tm = time(NULL);
28     FILE *out= stdout;
29 ##  int error;
30
31     if (argc == 2) {
32         if ((out = fopen(argv[1], "w")) == NULL) {
33             fprintf(stderr, "unable to open %s for output\n", argv[1]);
34             exit(errno);
35         }
36     } else if (argc != 1) {
37         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
38         exit(-1);
39     }
40
41 ##  ingres sms
42 ##  set lockmode session where readlock = nolock
43
44     
45     fprintf(out, "%s\n# Aliases File Extract of %s", divide, ctime(&tm));
46     fprintf(out, "# This file is automatically generated, do not edit it directly.\n%s\n\n", divide);
47
48     do_mlists(out);
49     do_poboxes(out);
50     do_aliases(out);
51
52     fprintf(out, "\n%s\n# End of aliases file\n%s\n", divide, divide);
53
54 ##  inquire_equel(error = "errorno")
55     if (error)  {
56         fprintf(out, "Ingres error %d\n", error);
57         exit(error);
58     }
59
60 ##  exit
61
62     if (fclose(out)) {
63         perror("close failed");
64         exit(errno);
65     }
66     exit(0);
67 }
68
69
70 /* Extract mailing lists.  First dump all real mailing lists.  While doing
71  * this, make a list of all mailing list IDs and all sub-list IDs.  Next,
72  * as long as there are sub-lists that aren't mailing lists, extract them
73  * and add them to the list of mailing lists.  If further sublists are 
74  * encountered, repeat...
75  */
76
77 int lwid, bol;
78
79 do_mlists(out)
80 FILE *out;
81 ##{
82 ##  char name[257], desc[257], own_type[9], owner_name[33];
83 ##  int id, own_id, member_id;
84     struct save_queue *sq, *sq_create();
85
86     sq = sq_create();
87     fprintf(out, "\n%s\n# Mailing lists\n%s\n", divide, divide);
88
89 ##  range of l is list
90 ##  retrieve (id = l.list_id) where l.maillist != 0 {
91       sq_save_unique_data(sq, id);
92 ##  }
93
94 ##  range of m is members
95 ##  range of u is users
96 ##  range of s is strings
97     while (sq_get_data(sq, &id)) {
98 ##      repeat retrieve (name= l.#name, desc = l.#desc,
99 ##                own_type = l.acl_type, own_id = l.acl_id)
100 ##              where l.list_id = @id
101         trim(name);
102         trim(desc);
103         trim(own_type);
104         put_fill(out, desc);
105         if (!strcmp(own_type, "LIST")) {
106 ##          repeat retrieve (owner_name = l.#name) where l.list_id = @own_id
107             trim(owner_name);
108             fprintf(out, "owner-%s: %s\n", name, owner_name);
109             sq_save_unique_data(sq, own_id);
110         } else if (!strcmp(own_type, "USER")) {
111 ##          repeat retrieve (owner_name = u.#login) where u.users_id = @own_id
112             trim(owner_name);
113             fprintf(out, "owner-%s: %s\n", name, owner_name);
114         }
115         fprintf(out, "%s: ", name);
116         lwid = strlen(name) + 2;
117         bol = 1;
118 ##      repeat retrieve (name = u.#login) where u.users_id = m.#member_id and
119 ##              m.list_id = @id and m.member_type = "USER" {
120             do_member(out, name);
121 ##      }
122 ##      repeat retrieve (name = l.#name, member_id = m.#member_id)
123 ##              where l.list_id = m.#member_id and
124 ##                    m.list_id = @id and m.member_type = "LIST" {
125             do_member(out, name);
126             sq_save_unique_data(sq, member_id);
127 ##      }
128 ##      repeat retrieve (name = s.#string) where s.string_id = m.#member_id and
129 ##              m.list_id = @id and m.member_type = "STRING" {
130             do_member(out, name);
131 ##      }
132         fprintf(out, "\n\n");
133     }
134
135     sq_destroy(sq);
136 ##}
137
138
139 /* print out strings separated by commas, doing line breaks as appropriate */
140
141 do_member(out, s)
142 FILE *out;
143 register char *s;
144 {
145     register wwid;
146     static int awid;
147     static int cont = 1;
148
149     trim(s);
150     wwid = strlen(s);
151
152     if (awid + wwid + 2 > AL_MAX_WID) {
153         fprintf(out, ",\n\tcontinuation-%d\ncontinuation-%d: ", cont, cont);
154         cont++;
155         awid = lwid = bol = 17;
156     }
157
158     if (bol) {
159         lwid += wwid;
160         awid = lwid;
161         fprintf(out, "%s", s);
162         bol = 0;
163         return;
164     }
165     if (lwid + wwid + 2 > ML_WID) {
166         fprintf(out, ",\n\t%s", s);
167         awid = lwid + wwid + 2;
168         lwid = wwid + 8;
169         return;
170     }
171     lwid += wwid + 2;
172     fprintf(out, ", %s", s);
173 }
174
175
176 /* Extract user poboxes.  First do POP boxes, where the name matches the
177  * login name and the machine name is in the pop_id.  Then do SMTP boxes,
178  * where the expansion is stored in the strings table.  The remaining boxes
179  * are of type NONE and should be skipped.
180  */
181
182 do_poboxes(out)
183 FILE *out;
184 ##{
185 ##  char login[9], name[33], box[129];
186
187     fprintf(out, "\n%s\n# User Poboxes\n%s\n", divide, divide);
188
189 ##  range of u is users
190 ##  range of m is machine
191 ##  retrieve (login = u.#login, name = m.#name)
192 ##      where u.potype = "POP" and m.mach_id = u.pop_id {
193       trim(login, name);
194       fprintf(out, "%s: %s@%s\n", login, login, name);
195 ##  }
196
197     fprintf(out, "\n# User Forwarding\n");
198
199 ##  range of s is strings
200 ##  retrieve (login = u.#login, box = s.#string)
201 ##      where u.potype = "SMTP" and u.box_id = s.string_id {
202       trim(login);
203       trim(box);
204       fprintf(out, "%s: %s\n", login, box);
205 ##  }
206
207 ##}
208
209
210 do_aliases(out)
211 FILE *out;
212 ##{
213 ##  char name[33], trans[129];
214
215     fprintf(out, "\n%s\n# Aliases\n%s\n", divide, divide);
216
217 ##  range of a is alias
218 ##  retrieve (name = a.#name, trans = a.#trans) where a.type = "MAIL" {
219         trim(name);
220         trim(trans);
221         fprintf(out, "%s: %s\n", name, trans);
222 ##  }
223 ##}
224
225
226 put_fill(aliases, string)
227 FILE *aliases;
228 register char *string;
229 {
230     register char *c;
231     register int lwid;
232     register int wwid;
233
234     if (*string == 0) return;
235     fputs("#  ", aliases);
236     lwid = 3;
237
238     while (1) {
239         while (*string && *string == ' ') string++;
240         c = (char *)index(string, ' ');
241         if (c == 0) {
242             wwid = strlen(string);
243         } else {
244             wwid = c - string;
245             *c = 0;
246         }
247
248         if ((lwid + wwid) > ML_WID) {
249             fputs("\n#  ", aliases);
250             lwid = 3;
251             fputs(string, aliases);
252         } else {
253             fputs(string, aliases);
254         }
255
256         if (c == (char *)0) break;
257         /* add a space after the word */
258         (void) fputc(' ', aliases);
259         wwid++;
260         lwid += wwid;
261         string += wwid;
262         /* add another if after a period */
263         if (*--c == '.') {
264             (void) fputc(' ', aliases);
265             lwid++;
266         }
267     }
268
269     (void) fputc('\n', aliases);
270 }
271
272
273 trim(s)
274 register char *s;
275 {
276     register char *p;
277
278     for (p = s; *s; s++)
279       if (*s != ' ')
280         p = s;
281     if (p != s) {
282         if (*p == ' ')
283           *p = 0;
284         else
285           p[1] = 0;
286     }
287 }
This page took 2.2843 seconds and 5 git commands to generate.