forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin_chain.c
442 lines (380 loc) · 9.69 KB
/
win_chain.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* @file
* Mixmaster Chain Window
*
* @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_win_chain Mixmaster Chain Window
*
* Display a menu of Remailer Hosts for the user to select.
*
* ## Windows
*
* | Name | Type | See Also |
* | :--------------------- | :-------- | :-------------- |
* | Mixmaster Chain Window | WT_CUSTOM | win_chain_new() |
*
* **Parent**
* - @ref mixmaster_dlg_mixmaster
*
* **Children**
* - None
*
* ## Data
* - #ChainData
*
* The Chain Window stores its data (#ChainData) in MuttWindow::wdata.
*/
#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "mutt/lib.h"
#include "gui/lib.h"
#include "win_chain.h"
#include "color/lib.h"
#include "chain_data.h"
#include "remailer.h"
/**
* cbar_update - Update the Chain bar (status bar)
* @param cd Chain data
*/
static void cbar_update(struct ChainData *cd)
{
char buf[1024] = { 0 };
snprintf(buf, sizeof(buf), "-- Remailer chain [Length: %d]", cd->chain_len);
sbar_set_title(cd->win_cbar, buf);
}
/**
* chain_add - Add a host to the chain
* @param cd Chain data
* @param s Hostname
* @param ra Remailer List
* @retval 0 Success
* @retval -1 Error
*/
static int chain_add(struct ChainData *cd, const char *s, struct RemailerArray *ra)
{
if (cd->chain_len >= MAX_MIXES)
return -1;
if (mutt_str_equal(s, "0") || mutt_istr_equal(s, "<random>"))
{
cd->chain[cd->chain_len++] = 0;
return 0;
}
struct Remailer **r = NULL;
ARRAY_FOREACH(r, ra)
{
if (mutt_istr_equal(s, (*r)->shortname))
{
cd->chain[cd->chain_len++] = ARRAY_FOREACH_IDX;
return 0;
}
}
/* replace unknown remailers by <random> */
cd->chain[cd->chain_len++] = 0;
return 0;
}
/**
* win_chain_recalc - Recalculate the Chain list - Implements MuttWindow::recalc() - @ingroup window_recalc
*/
static int win_chain_recalc(struct MuttWindow *win)
{
const int wrap_indent = 2;
if (!win || !win->wdata)
return 0;
struct ChainData *cd = win->wdata;
cbar_update(cd);
win->actions |= WA_REPAINT;
if (cd->chain_len == 0)
return 0;
short col = 0, row = 0;
for (int i = 0; i < cd->chain_len; i++)
{
short old_col = col;
int index = cd->chain[i];
struct Remailer **rp = ARRAY_GET(cd->ra, index);
col += strlen((*rp)->shortname) + 2;
if (col >= win->state.cols)
{
old_col = wrap_indent;
col = wrap_indent;
row++;
}
cd->coords[i].col = old_col;
cd->coords[i].row = row;
}
return 0;
}
/**
* win_chain_repaint - Repaint the Chain list - Implements MuttWindow::repaint() - @ingroup window_repaint
*/
static int win_chain_repaint(struct MuttWindow *win)
{
for (int i = 0; i < win->state.rows; i++)
{
mutt_window_move(win, 0, i);
mutt_window_clrtoeol(win);
}
struct ChainData *cd = win->wdata;
if (cd->chain_len == 0)
return 0;
for (int i = 0; i < cd->chain_len; i++)
{
if (cd->coords[i].row < win->state.rows)
{
if (i == cd->sel)
mutt_curses_set_color_by_id(MT_COLOR_INDICATOR);
else
mutt_curses_set_color_by_id(MT_COLOR_NORMAL);
int index = cd->chain[i];
struct Remailer **rp = ARRAY_GET(cd->ra, index);
mutt_window_mvaddstr(win, cd->coords[i].col, cd->coords[i].row, (*rp)->shortname);
mutt_curses_set_color_by_id(MT_COLOR_NORMAL);
if ((i + 1) < cd->chain_len)
mutt_window_addstr(win, ", ");
}
}
return 0;
}
/**
* win_chain_new - Create a new Chain list Window
* @param win_cbar Chain bar to keep updated (status bar)
* @retval ptr New Chain list Window
*/
struct MuttWindow *win_chain_new(struct MuttWindow *win_cbar)
{
struct MuttWindow *win = mutt_window_new(WT_CUSTOM, MUTT_WIN_ORIENT_VERTICAL, MUTT_WIN_SIZE_FIXED,
MUTT_WIN_SIZE_UNLIMITED, 4);
struct ChainData *cd = chain_data_new();
cd->win_cbar = win_cbar;
win->recalc = win_chain_recalc;
win->repaint = win_chain_repaint;
win->wdata = cd;
win->wdata_free = chain_data_free;
win->actions |= WA_RECALC;
return win;
}
/**
* win_chain_init - Initialise the Chain list Window
* @param win Chain list Window
* @param chain Chain data
* @param ra Array of all Remailer hosts
*/
void win_chain_init(struct MuttWindow *win, struct ListHead *chain, struct RemailerArray *ra)
{
if (!win || !win->wdata)
return;
struct ChainData *cd = win->wdata;
cd->ra = ra;
cd->sel = 0;
struct ListNode *p = NULL;
STAILQ_FOREACH(p, chain, entries)
{
chain_add(cd, p->data, cd->ra);
}
if (cd->chain_len > 1)
cd->sel = cd->chain_len - 1;
win->actions |= WA_RECALC;
}
/**
* win_chain_extract - Extract the Chain list data
* @param win Chain list Window
* @param chain Chain data
* @retval num Number of entries in the Chain
* @retval -1 Error
*/
int win_chain_extract(struct MuttWindow *win, struct ListHead *chain)
{
if (!win || !win->wdata)
return -1;
struct ChainData *cd = win->wdata;
if (cd->chain_len)
{
char *t = NULL;
for (int i = 0; i < cd->chain_len; i++)
{
const int j = cd->chain[i];
if (j != 0)
{
struct Remailer **rp = ARRAY_GET(cd->ra, j);
t = (*rp)->shortname;
}
else
{
t = "*";
}
mutt_list_insert_tail(chain, mutt_str_dup(t));
}
}
return cd->chain_len;
}
/**
* win_chain_get_length - Get the number of Remailers in the Chain
* @param win Chain list Window
* @retval num Number of entries in the Chain
*/
int win_chain_get_length(struct MuttWindow *win)
{
if (!win || !win->wdata)
return 0;
struct ChainData *cd = win->wdata;
return cd->chain_len;
}
/**
* win_chain_next - Select the next entry in the Chain list
* @param win Chain list Window
* @retval true Selection changed
*/
bool win_chain_next(struct MuttWindow *win)
{
if (!win || !win->wdata)
return false;
struct ChainData *cd = win->wdata;
if (cd->chain_len && (cd->sel < (cd->chain_len - 1)))
{
cd->sel++;
}
else
{
mutt_error(_("You already have the last chain element selected"));
return false;
}
win->actions |= WA_REPAINT;
return true;
}
/**
* win_chain_prev - Select the previous entry in the Chain list
* @param win Chain list Window
* @retval true Selection changed
*/
bool win_chain_prev(struct MuttWindow *win)
{
if (!win || !win->wdata)
return false;
struct ChainData *cd = win->wdata;
if (cd->sel)
{
cd->sel--;
}
else
{
mutt_error(_("You already have the first chain element selected"));
return false;
}
win->actions |= WA_REPAINT;
return true;
}
/**
* win_chain_append - Add an item to the Chain, after the current item
* @param win Chain list Window
* @param r Selected Remailer host
* @retval true Item added to Chain
*/
bool win_chain_append(struct MuttWindow *win, struct Remailer *r)
{
if (!win || !win->wdata || !r)
return false;
struct ChainData *cd = win->wdata;
if ((cd->chain_len < MAX_MIXES) && (cd->sel < cd->chain_len))
cd->sel++;
return win_chain_insert(win, r);
}
/**
* win_chain_insert - Add an item to the Chain, before the current item
* @param win Chain list Window
* @param r Selected Remailer host
* @retval true Item added to Chain
*/
bool win_chain_insert(struct MuttWindow *win, struct Remailer *r)
{
if (!win || !win->wdata || !r)
return false;
struct ChainData *cd = win->wdata;
if (cd->chain_len < MAX_MIXES)
{
cd->chain_len++;
for (int i = cd->chain_len - 1; i > cd->sel; i--)
cd->chain[i] = cd->chain[i - 1];
cd->chain[cd->sel] = r->num;
}
else
{
/* L10N The '%d' here hard-coded to 19 */
mutt_error(_("Mixmaster chains are limited to %d elements"), MAX_MIXES);
return false;
}
win->actions |= WA_RECALC;
return true;
}
/**
* win_chain_delete - Delete the current item from the Chain
* @param win Chain list Window
* @retval true Item deleted
*/
bool win_chain_delete(struct MuttWindow *win)
{
if (!win || !win->wdata)
return false;
struct ChainData *cd = win->wdata;
if (cd->chain_len)
{
cd->chain_len--;
for (int i = cd->sel; i < cd->chain_len; i++)
cd->chain[i] = cd->chain[i + 1];
if ((cd->sel == cd->chain_len) && cd->sel)
cd->sel--;
}
else
{
mutt_error(_("The remailer chain is already empty"));
return false;
}
win->actions |= WA_RECALC;
return true;
}
/**
* win_chain_validate - Validate the Chain
* @param win Chain list Window
* @retval true Chain is valid
*/
bool win_chain_validate(struct MuttWindow *win)
{
if (!win || !win->wdata)
return false;
struct ChainData *cd = win->wdata;
if (cd->chain_len == 0)
return false;
if (cd->chain_len != 0)
{
int last_index = cd->chain[cd->chain_len - 1];
if (last_index != 0)
{
struct Remailer **rp = ARRAY_GET(cd->ra, last_index);
if ((*rp)->caps & MIX_CAP_MIDDLEMAN)
{
mutt_error(_("Error: %s can't be used as the final remailer of a chain"),
(*rp)->shortname);
return false;
}
}
}
return true;
}