comparison src/do_all_the_voodoo_here.c @ 748:dfc9ab125fd4

Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
author Chris Webb
date Sun, 03 Oct 2010 21:40:42 +1100
parents src/do_all_the_voodo_here.c@84189f529aee
children 1c13e314e5b9
comparison
equal deleted inserted replaced
747:fd77dd58bde1 748:dfc9ab125fd4
1 /* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
2 *
3 * $Id$
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "mlmmj.h"
29 #include "mygetline.h"
30 #include "gethdrline.h"
31 #include "strgen.h"
32 #include "chomp.h"
33 #include "ctrlvalue.h"
34 #include "do_all_the_voodoo_here.h"
35 #include "log_error.h"
36 #include "wrappers.h"
37 #include "memory.h"
38
39 int findit(const char *line, const char **headers)
40 {
41 int i = 0;
42 size_t len;
43
44 while(headers[i]) {
45 len = strlen(headers[i]);
46 if(strncasecmp(line, headers[i], len) == 0)
47 return 1;
48 i++;
49 }
50
51 return 0;
52 }
53
54 void getinfo(const char *line, struct mailhdr *readhdrs)
55 {
56 int i = 0;
57 size_t tokenlen, linelen, valuelen;
58
59 while(readhdrs[i].token) {
60 tokenlen = strlen(readhdrs[i].token);
61 linelen = strlen(line);
62 if(strncasecmp(line, readhdrs[i].token, tokenlen) == 0) {
63 readhdrs[i].valuecount++;
64 valuelen = linelen - tokenlen + 1;
65 readhdrs[i].values =
66 (char **)myrealloc(readhdrs[i].values,
67 readhdrs[i].valuecount * sizeof(char *));
68 readhdrs[i].values[readhdrs[i].valuecount - 1] =
69 (char *)mymalloc(valuelen + 1);
70 strncpy(readhdrs[i].values[readhdrs[i].valuecount - 1],
71 line+tokenlen, valuelen);
72 chomp(readhdrs[i].values[readhdrs[i].valuecount - 1]);
73 }
74 i++;
75 }
76 }
77
78 int do_all_the_voodoo_here(int infd, int outfd, int hdrfd, int footfd,
79 const char **delhdrs, struct mailhdr *readhdrs,
80 struct strlist *allhdrs, const char *prefix)
81 {
82 char *hdrline, *subject, *unqp;
83 int hdrsadded = 0;
84 int subject_present = 0;
85
86 allhdrs->count = 0;
87 allhdrs->strs = NULL;
88
89 while((hdrline = gethdrline(infd))) {
90 /* Done with headers? Then add extra if wanted*/
91 if((strncasecmp(hdrline, "mime", 4) == 0) ||
92 ((strlen(hdrline) == 1) && (hdrline[0] == '\n'))){
93
94 /* add extra headers */
95 if(!hdrsadded && hdrfd >= 0) {
96 if(dumpfd2fd(hdrfd, outfd) < 0) {
97 log_error(LOG_ARGS, "Could not "
98 "add extra headers");
99 myfree(hdrline);
100 return -1;
101 } else
102 hdrsadded = 1;
103 }
104
105 fsync(outfd);
106
107 /* end of headers, write single LF */
108 if(hdrline[0] == '\n') {
109 /* but first add Subject if none is present
110 * and a prefix is defined */
111 if (prefix && !subject_present)
112 {
113 subject = concatstr(3, "Subject: ",
114 prefix, "\n");
115 writen(outfd, subject, strlen(subject));
116 myfree(subject);
117 subject_present = 1;
118 }
119
120 if(writen(outfd, hdrline, strlen(hdrline))
121 < 0) {
122 myfree(hdrline);
123 log_error(LOG_ARGS,
124 "Error writing hdrs.");
125 return -1;
126 }
127 myfree(hdrline);
128 break;
129 }
130 }
131 /* Do we want info from hdrs? Get it before it's gone */
132 if(readhdrs)
133 getinfo(hdrline, readhdrs);
134
135 /* Snatch a copy of the header */
136 allhdrs->count++;
137 allhdrs->strs = myrealloc(allhdrs->strs,
138 sizeof(char *) * (allhdrs->count + 1));
139 allhdrs->strs[allhdrs->count-1] = mystrdup(hdrline);
140 allhdrs->strs[allhdrs->count] = NULL; /* XXX why, why, why? */
141
142 /* Add Subject: prefix if wanted */
143 if(prefix) {
144 if(strncasecmp(hdrline, "Subject:", 8) == 0) {
145 subject_present = 1;
146 unqp = cleanquotedp(hdrline + 8);
147 if(strstr(hdrline + 8, prefix) == NULL &&
148 strstr(unqp, prefix) == NULL) {
149 subject = concatstr(3,
150 "Subject: ", prefix,
151 hdrline + 8);
152 writen(outfd, subject,
153 strlen(subject));
154 myfree(subject);
155 myfree(hdrline);
156 myfree(unqp);
157 continue;
158 }
159 myfree(unqp);
160 }
161 }
162
163 /* Should it be stripped? */
164 if(delhdrs) {
165 if(!findit(hdrline, delhdrs))
166 writen(outfd, hdrline, strlen(hdrline));
167 } else
168 writen(outfd, hdrline, strlen(hdrline));
169
170
171 myfree(hdrline);
172 }
173
174 /* Just print the rest of the mail */
175 if(dumpfd2fd(infd, outfd) < 0) {
176 log_error(LOG_ARGS, "Error when dumping rest of mail");
177 return -1;
178 }
179
180 /* No more, lets add the footer if one */
181 if(footfd >= 0)
182 if(dumpfd2fd(footfd, outfd) < 0) {
183 log_error(LOG_ARGS, "Error when adding footer");
184 return -1;
185 }
186
187 fsync(outfd);
188
189 return 0;
190 }