forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremailer.c
214 lines (176 loc) · 4.18 KB
/
remailer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @file
* Mixmaster Remailer
*
* @authors
* Copyright (C) 2022 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page mixmaster_remailer Mixmaster Remailer
*
* Mixmaster Remailer
*/
#include "config.h"
#include <stddef.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "remailer.h"
/**
* remailer_free - Free a Remailer
* @param ptr Remailer to free
*/
void remailer_free(struct Remailer **ptr)
{
if (!ptr || !*ptr)
return;
struct Remailer *r = *ptr;
FREE(&r->shortname);
FREE(&r->addr);
FREE(&r->ver);
FREE(ptr);
}
/**
* remailer_new - Create a new Remailer
* @retval ptr Newly allocated Remailer
*/
struct Remailer *remailer_new(void)
{
return mutt_mem_calloc(1, sizeof(struct Remailer));
}
/**
* mix_get_caps - Get Mixmaster Capabilities
* @param capstr Capability string to parse
* @retval num Capabilities, see #MixCapFlags
*/
static MixCapFlags mix_get_caps(const char *capstr)
{
MixCapFlags caps = MIX_CAP_NO_FLAGS;
while (*capstr)
{
switch (*capstr)
{
case 'C':
caps |= MIX_CAP_COMPRESS;
break;
case 'M':
caps |= MIX_CAP_MIDDLEMAN;
break;
case 'N':
{
switch (*++capstr)
{
case 'm':
caps |= MIX_CAP_NEWSMAIL;
break;
case 'p':
caps |= MIX_CAP_NEWSPOST;
break;
}
}
}
if (*capstr)
capstr++;
}
return caps;
}
/**
* remailer_get_hosts - Parse the type2.list as given by mixmaster -T
* @retval obj Array of Remailer Hosts
*/
struct RemailerArray remailer_get_hosts(void)
{
struct RemailerArray ra = ARRAY_HEAD_INITIALIZER;
FILE *fp = NULL;
char line[8192] = { 0 };
char *t = NULL;
struct Remailer *p = NULL;
const char *const c_mixmaster = cs_subset_string(NeoMutt->sub, "mixmaster");
if (!c_mixmaster)
return ra;
int fd_null = open("/dev/null", O_RDWR);
if (fd_null == -1)
return ra;
struct Buffer *cmd = mutt_buffer_pool_get();
mutt_buffer_printf(cmd, "%s -T", c_mixmaster);
pid_t mm_pid = filter_create_fd(mutt_buffer_string(cmd), NULL, &fp, NULL,
fd_null, -1, fd_null);
window_invalidate_all();
if (mm_pid == -1)
{
mutt_buffer_pool_release(&cmd);
close(fd_null);
return ra;
}
mutt_buffer_pool_release(&cmd);
/* first, generate the "random" remailer */
p = remailer_new();
p->shortname = mutt_str_dup(_("<random>"));
p->num = 0;
ARRAY_ADD(&ra, p);
while (fgets(line, sizeof(line), fp))
{
p = remailer_new();
t = strtok(line, " \t\n");
if (!t)
goto problem;
p->shortname = mutt_str_dup(t);
t = strtok(NULL, " \t\n");
if (!t)
goto problem;
p->addr = mutt_str_dup(t);
t = strtok(NULL, " \t\n");
if (!t)
goto problem;
t = strtok(NULL, " \t\n");
if (!t)
goto problem;
p->ver = mutt_str_dup(t);
t = strtok(NULL, " \t\n");
if (!t)
goto problem;
p->caps = mix_get_caps(t);
p->num = ARRAY_SIZE(&ra);
ARRAY_ADD(&ra, p);
continue;
problem:
remailer_free(&p);
}
filter_wait(mm_pid);
close(fd_null);
return ra;
}
/**
* remailer_clear_hosts - Clear a Remailer List
* @param ra Array of Remailer hosts to clear
*
* @note The empty array is not freed
*/
void remailer_clear_hosts(struct RemailerArray *ra)
{
struct Remailer **r = NULL;
ARRAY_FOREACH(r, ra)
{
remailer_free(r);
}
ARRAY_FREE(ra);
}