Mercurial > hg > mlmmj
annotate 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 |
rev | line source |
---|---|
17
b045203f558b
Add GPL info in top of files where it was missing, and remove a file that
mmj
parents:
11
diff
changeset
|
1 /* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk> |
b045203f558b
Add GPL info in top of files where it was missing, and remove a file that
mmj
parents:
11
diff
changeset
|
2 * |
b045203f558b
Add GPL info in top of files where it was missing, and remove a file that
mmj
parents:
11
diff
changeset
|
3 * $Id$ |
b045203f558b
Add GPL info in top of files where it was missing, and remove a file that
mmj
parents:
11
diff
changeset
|
4 * |
225 | 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. | |
17
b045203f558b
Add GPL info in top of files where it was missing, and remove a file that
mmj
parents:
11
diff
changeset
|
22 */ |
b045203f558b
Add GPL info in top of files where it was missing, and remove a file that
mmj
parents:
11
diff
changeset
|
23 |
11 | 24 #include <stdlib.h> |
25 #include <string.h> | |
161 | 26 #include <unistd.h> |
11 | 27 |
28 #include "mlmmj.h" | |
29 #include "mygetline.h" | |
30 #include "gethdrline.h" | |
31 #include "strgen.h" | |
32 #include "chomp.h" | |
89 | 33 #include "ctrlvalue.h" |
748
dfc9ab125fd4
Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
Chris Webb
parents:
501
diff
changeset
|
34 #include "do_all_the_voodoo_here.h" |
161 | 35 #include "log_error.h" |
36 #include "wrappers.h" | |
245 | 37 #include "memory.h" |
11 | 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]); | |
324 | 46 if(strncasecmp(line, headers[i], len) == 0) |
11 | 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); | |
324 | 62 if(strncasecmp(line, readhdrs[i].token, tokenlen) == 0) { |
120
154b486fefd5
When retrieving header info, make sure to get all headers since we might have
mmj
parents:
89
diff
changeset
|
63 readhdrs[i].valuecount++; |
11 | 64 valuelen = linelen - tokenlen + 1; |
120
154b486fefd5
When retrieving header info, make sure to get all headers since we might have
mmj
parents:
89
diff
changeset
|
65 readhdrs[i].values = |
245 | 66 (char **)myrealloc(readhdrs[i].values, |
120
154b486fefd5
When retrieving header info, make sure to get all headers since we might have
mmj
parents:
89
diff
changeset
|
67 readhdrs[i].valuecount * sizeof(char *)); |
154b486fefd5
When retrieving header info, make sure to get all headers since we might have
mmj
parents:
89
diff
changeset
|
68 readhdrs[i].values[readhdrs[i].valuecount - 1] = |
245 | 69 (char *)mymalloc(valuelen + 1); |
120
154b486fefd5
When retrieving header info, make sure to get all headers since we might have
mmj
parents:
89
diff
changeset
|
70 strncpy(readhdrs[i].values[readhdrs[i].valuecount - 1], |
285 | 71 line+tokenlen, valuelen); |
120
154b486fefd5
When retrieving header info, make sure to get all headers since we might have
mmj
parents:
89
diff
changeset
|
72 chomp(readhdrs[i].values[readhdrs[i].valuecount - 1]); |
11 | 73 } |
74 i++; | |
75 } | |
76 } | |
77 | |
748
dfc9ab125fd4
Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
Chris Webb
parents:
501
diff
changeset
|
78 int do_all_the_voodoo_here(int infd, int outfd, int hdrfd, int footfd, |
89 | 79 const char **delhdrs, struct mailhdr *readhdrs, |
254 | 80 struct strlist *allhdrs, const char *prefix) |
11 | 81 { |
265
f28ab75abba7
Quoted printable subjects are now matched against prefix as well
mmj
parents:
254
diff
changeset
|
82 char *hdrline, *subject, *unqp; |
298 | 83 int hdrsadded = 0; |
497 | 84 int subject_present = 0; |
11 | 85 |
254 | 86 allhdrs->count = 0; |
87 allhdrs->strs = NULL; | |
88 | |
161 | 89 while((hdrline = gethdrline(infd))) { |
11 | 90 /* Done with headers? Then add extra if wanted*/ |
307 | 91 if((strncasecmp(hdrline, "mime", 4) == 0) || |
298 | 92 ((strlen(hdrline) == 1) && (hdrline[0] == '\n'))){ |
497 | 93 |
94 /* add extra headers */ | |
298 | 95 if(!hdrsadded && hdrfd >= 0) { |
161 | 96 if(dumpfd2fd(hdrfd, outfd) < 0) { |
184 | 97 log_error(LOG_ARGS, "Could not " |
161 | 98 "add extra headers"); |
245 | 99 myfree(hdrline); |
161 | 100 return -1; |
298 | 101 } else |
102 hdrsadded = 1; | |
284 | 103 } |
104 | |
161 | 105 fsync(outfd); |
497 | 106 |
107 /* end of headers, write single LF */ | |
298 | 108 if(hdrline[0] == '\n') { |
497 | 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 | |
298 | 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 } | |
310 | 127 myfree(hdrline); |
298 | 128 break; |
129 } | |
11 | 130 } |
131 /* Do we want info from hdrs? Get it before it's gone */ | |
132 if(readhdrs) | |
133 getinfo(hdrline, readhdrs); | |
89 | 134 |
254 | 135 /* Snatch a copy of the header */ |
136 allhdrs->count++; | |
137 allhdrs->strs = myrealloc(allhdrs->strs, | |
325
1bbb75dfcea8
why, why, why? BECAUSE you have to learn to add parenthesises
mmj
parents:
324
diff
changeset
|
138 sizeof(char *) * (allhdrs->count + 1)); |
254 | 139 allhdrs->strs[allhdrs->count-1] = mystrdup(hdrline); |
140 allhdrs->strs[allhdrs->count] = NULL; /* XXX why, why, why? */ | |
141 | |
89 | 142 /* Add Subject: prefix if wanted */ |
143 if(prefix) { | |
352 | 144 if(strncasecmp(hdrline, "Subject:", 8) == 0) { |
499 | 145 subject_present = 1; |
352 | 146 unqp = cleanquotedp(hdrline + 8); |
147 if(strstr(hdrline + 8, prefix) == NULL && | |
265
f28ab75abba7
Quoted printable subjects are now matched against prefix as well
mmj
parents:
254
diff
changeset
|
148 strstr(unqp, prefix) == NULL) { |
352 | 149 subject = concatstr(3, |
89 | 150 "Subject: ", prefix, |
352 | 151 hdrline + 8); |
161 | 152 writen(outfd, subject, |
153 strlen(subject)); | |
245 | 154 myfree(subject); |
155 myfree(hdrline); | |
265
f28ab75abba7
Quoted printable subjects are now matched against prefix as well
mmj
parents:
254
diff
changeset
|
156 myfree(unqp); |
89 | 157 continue; |
158 } | |
265
f28ab75abba7
Quoted printable subjects are now matched against prefix as well
mmj
parents:
254
diff
changeset
|
159 myfree(unqp); |
89 | 160 } |
161 } | |
11 | 162 |
163 /* Should it be stripped? */ | |
89 | 164 if(delhdrs) { |
11 | 165 if(!findit(hdrline, delhdrs)) |
161 | 166 writen(outfd, hdrline, strlen(hdrline)); |
89 | 167 } else |
161 | 168 writen(outfd, hdrline, strlen(hdrline)); |
89 | 169 |
11 | 170 |
245 | 171 myfree(hdrline); |
11 | 172 } |
173 | |
174 /* Just print the rest of the mail */ | |
161 | 175 if(dumpfd2fd(infd, outfd) < 0) { |
176 log_error(LOG_ARGS, "Error when dumping rest of mail"); | |
177 return -1; | |
11 | 178 } |
179 | |
161 | 180 /* No more, lets add the footer if one */ |
184 | 181 if(footfd >= 0) |
161 | 182 if(dumpfd2fd(footfd, outfd) < 0) { |
183 log_error(LOG_ARGS, "Error when adding footer"); | |
184 return -1; | |
185 } | |
11 | 186 |
161 | 187 fsync(outfd); |
11 | 188 |
189 return 0; | |
190 } |