-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.c
executable file
·2213 lines (2058 loc) · 65.8 KB
/
editor.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* evergreen -- online only terminal mail user agent
*
* Copyright (C) 2024, Naveen Albert
*
* Naveen Albert <[email protected]>
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Message editor
*
* \author Naveen Albert <[email protected]>
*/
#include "evergreen.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <form.h>
/* Initially, a double line break was used to indicate
* a hard line break, but it's usually better to try
* to heuristically determine this. Define this
* to restore the old behavior.
*
* Pros of defining HARD_LINE_BREAK_REQUIRES_DOUBLE:
* - Definitive algorithm for parsing line breaks, as opposed to heuristics
*
* Cons:
* - It looks weird, not intuitive for users at all
* - Hard line breaks need to be inserted into messages we reply to. There is currently incomplete logic for handling --- Forwarded Message --- headers, etc.
*/
/* #define HARD_LINE_BREAK_REQUIRES_DOUBLE */
/* ncurses internally for REQ_END_LINE uses After_End_Of_Data to search for the last non blank character on a line,
* so no, there is nothing internal we can use to get at that, apart from computing it. */
static inline const char *get_current_text(FORM *form)
{
const char *s;
FIELD *field = current_field(form);
form_driver(form, REQ_VALIDATION); /* flush buffer */
s = field_buffer(field, 0) + (field->dcols * form->currow) + form->curcol;
return s;
}
static inline int nonspace_after_in_line(FORM *form)
{
int curcol = form->curcol;
const char *c = get_current_text(form);
while (*c) {
if (*c != ' ') {
return 1;
}
c++;
if (++curcol == COLS) {
break;
}
}
return 0;
}
static inline int nonspace_after_in_field(FORM *form)
{
const char *c = get_current_text(form);
while (*c) {
if (*c != ' ') {
return 1;
}
c++;
}
return 0;
}
#ifndef HARD_LINE_BREAK_REQUIRES_DOUBLE
static inline int next_word_count(const char *c)
{
int quoted = 0;
int length = 0;
if (*c == '>') {
quoted = 1;
}
while (*c) {
if (length >= COLS) {
return length; /* ??? */
}
if (*c == ' ') {
if (!quoted) {
return length;
}
/* Tolerate just one space in this case: >> Something... */
quoted = 0;
} else {
length++;
}
c++;
}
return length;
}
#endif
#define END_OF_LINE() (!nonspace_after_in_line(form))
#define END_OF_FIELD() (!nonspace_after_in_field(form))
#define FORMAT_FLOWED_WRAP_COL 72
static size_t get_field_length(FIELD *field, int cols, int trailing_crlf)
{
int output_col = 0;
int col = 0;
int row = 0;
size_t len = 0;
int blank_lines = 0;
int consec_spaces = 0;
int in_quotes = 0, quote_depth = 0;
int last_flowed = 0;
const char *sp_start = NULL;
const char *src = field_buffer(field, 0);
assert(cols > 0);
/* Compute the non-trivial field length.
* ncurses will allocate entire lines at a time, that are by default filled with spaces (see field_buffer(3))
* and these don't count.
*
* The algorithm here is based on copy_trimmed_str, just to calculate length instead of copy the characters. */
/* Skip leading whitespace */
while (*src == ' ') {
src++;
}
while (*src) {
if (col == 0) {
if (consec_spaces > cols) {
blank_lines++;
consec_spaces = 0;
sp_start = NULL;
}
#ifndef HARD_LINE_BREAK_REQUIRES_DOUBLE
if (consec_spaces > 0 && next_word_count(src) < consec_spaces - 7) {
blank_lines++;
consec_spaces = 0;
sp_start = NULL;
}
#endif
in_quotes = *src == '>';
quote_depth = in_quotes ? 1 : 0;
if (last_flowed) {
len++;
}
last_flowed = 0;
#if 0
} else if (output_col == 1) {
/* As below, assume that we'll have to space stuff, since we don't know what was last */
len++;
output_col++;
#endif
} else if (output_col >= FORMAT_FLOWED_WRAP_COL) {
len += 2; /* Just assume that we'd wrap. This is the only calculation that's not precise, since we don't keep track of dst when calculating length. */
output_col = 0;
len += quote_depth + 1; /* Assume we need to add quotes */
last_flowed = 1;
}
if (*src == ' ') {
if (!consec_spaces++) {
sp_start = src;
}
} else {
if (in_quotes) {
if (*src == '>') {
quote_depth++;
} else {
in_quotes = 0;
}
}
if (!blank_lines && !col && row > 0) {
if (*src == '>') {
len += 2;
output_col = 0;
consec_spaces = 0; /* Let the normal logic handle this case */
} else {
len++;
output_col++;
}
}
len += blank_lines * 2;
if (blank_lines) {
output_col = 0;
blank_lines = 0;
}
if (consec_spaces) {
int diff = src - sp_start;
if (col > 0) {
len += diff + 1;
output_col += diff + 1;
} else {
len++;
output_col++;
}
} else {
len++;
output_col++;
}
consec_spaces = 0;
}
src++;
if (++col == cols) {
col = 0;
row++;
}
}
if (trailing_crlf) {
len += 2; /* Add trailing CR LF to ensure message is well terminated */
}
return len;
}
static const char *field_names[] = {
"From",
"To",
"Cc",
"Bcc",
"Reply-To",
"Subject", /* This should be last, since the rest are all address related */
};
#define NUM_FIELDS 7 /* Number of headers above, plus 1 for message body itself */
#define FIELD_DESC_WIDTH 9 /* Max length of any of the above, plus 1 */
static inline __attribute__((always_inline)) int num_quotes(const char *s)
{
int n = 0;
while (*s == '>') {
n++;
s++;
}
return n;
}
static void copy_trimmed_str(char *restrict dst, const char *restrict src, size_t len, int cols, int trailing_crlf, size_t *restrict leftover)
{
int output_col = 0;
int col = 0;
int row = 0;
int blank_lines = 0;
int consec_spaces = 0;
int in_quotes = 0, quote_depth = 0;
const char *sp_start = NULL;
int last_flowed = 0;
assert(cols > 0);
/* This is a bit of an eyesore, but the reason we need to keep track of so much state is
* to keep the overall function linear time with respect to message size. */
/* Skip leading whitespace */
while (*src == ' ') {
src++;
}
while (*src) {
if (col == 0) {
if (consec_spaces > cols) {
client_debug(9, "Detected a blank line");
blank_lines++;
consec_spaces = 0;
sp_start = NULL;
}
#ifndef HARD_LINE_BREAK_REQUIRES_DOUBLE
if (consec_spaces > 0 && next_word_count(src) < consec_spaces - 7) { /* Leave some wiggle room */
/* We try to determine if this is a real line break
* or a "soft" line break due to wrapping.
* This won't be perfect, but if the previous line
* ended significantly before the end of the line,
* and the next word isn't that long,
* then that suggests a hard line break is appropriate. */
client_debug(8, "Looks like a hard line break on line %d, based on context", row);
blank_lines++;
consec_spaces = 0;
sp_start = NULL;
}
#endif
in_quotes = *src == '>';
quote_depth = 0;
if (last_flowed) {
/* If we wrap text shorter due to a resize to smaller col dimensions,
* then we'll want to do that at the end of the actual line as well. */
client_debug(7, "Last line was soft wrapped, adding a soft wrap here");
*dst++ = ' ';
len--;
}
last_flowed = 0;
#if 0
/* Don't do this, since the quotes at beginnings of lines are for quoting messages,
* not quote literals in a reply. If that's what someone wants, the user will need
* to manually escape... */
} else if (output_col == 1) {
/* Space-stuff lines which start with a space, "From ", or ">". */
if (dst[-1] == ' ' || dst[-1] == '>' || (dst[-1] == 'F' && !strncmp(src, "rom ", 4))) {
client_debug(9, "Space stuffing line");
*dst = dst[-1];
dst[-1] = ' ';
len++;
output_col++;
}
#endif
} else if (output_col >= FORMAT_FLOWED_WRAP_COL) {
/* Find the most recent occurence of a space that we've already copied */
const char *sp = memrchr(dst - FORMAT_FLOWED_WRAP_COL, ' ', FORMAT_FLOWED_WRAP_COL);
if (sp) { /* Most lines do not contain words that are ~70+ characters long */
char *mvdst, *mvsrc;
char mvsrcc;
/* Say that the dest buffer looks like this:
*
* 0 1 2 3 4 5 6 7 8 9
* a b c d e f g j _ <- current value of dst.
*
* memrchr(dst - 1) will start at 8 and look backwards for a space, which it finds at 3.
* Thus, dst - 1 - sp = 9 - 1 - 3 = 5, which is how many characters exist in the buffer after the space.
*
* We insert a newline at 4, and shift everything else forward two characters:
* memmove(dst - diff + 1, dst - diff, diff)
*
* If we have to insert quotes, we also need to leave room for # of quotes + another space.
*/
int diff = dst - 1 - sp;
/* If we need to insert quotes, we need to leave room for those + 1 space */
mvsrc = dst - diff;
mvsrcc = *mvsrc;
mvdst = dst - diff + 2;
if (quote_depth) {
client_debug(9, "Adjusted mvdst from %p to %p", mvdst, mvdst + (quote_depth + 1));
mvdst += (quote_depth + 1);
}
client_debug(9, "Need to shift(%d) '%.*s'", diff, diff, mvsrc);
memmove(mvdst, mvsrc, diff);
dst[-diff] = '\r'; /* Insert soft line wrap at the start of that word, which'll now get wrapped to the next line. */
dst[-diff + 1] = '\n';
dst += 2;
len -= 2;
client_debug(5, "Detected long line on row %d at %d cols, wrapped at %d cols (starting at %d: '%c')",
row, output_col, FORMAT_FLOWED_WRAP_COL - diff, mvsrcc, isprint(mvsrcc) ? mvsrcc : ' ');
output_col = 0;
/* When wrapping, maintain the same quote depth */
if (quote_depth) {
int j;
/* Back up to the beginning of the line */
dst -= diff;
len += diff;
client_debug(5, "Prefixing with %d quotes and a space", quote_depth);
for (j = 0; j < quote_depth; j++) {
*dst++ = '>';
len--;
}
*dst++ = ' ';
len--;
if (unlikely(dst > mvdst)) {
client_debug(1, "ERROR: Overran start of shifted data by %ld bytes! %p > %p", dst - mvdst, dst, mvdst);
assert(dst == mvdst); /* Crash */
}
dst += diff; /* Skip data we copied */
len -= diff;
}
assert(dst == mvdst + diff); /* Should be positioned after the data that we moved */
last_flowed = 1;
} else {
client_debug(5, "Encountered super long word at row %d, can't wrap", row);
}
}
if (*src == ' ') {
if (!consec_spaces++) {
sp_start = src;
}
} else {
if (in_quotes) {
if (*src == '>') {
quote_depth++;
} else {
in_quotes = 0;
}
}
if (!blank_lines && !col && row > 0) {
if (*src == '>') {
/* Quote at column 0, so actual newline */
*dst++ = '\r';
*dst++ = '\n';
output_col = 0;
len -= 2;
consec_spaces = 0; /* Let the normal logic handle this case */
} else {
/* So that if text wraps to the next line, we insert a space as appropriate */
if (*(dst - 1) == ' ') {
/* XXX Should prevent this from happening, but don't add two spaces if we detect it */
client_debug(9, "Already added space at end of line for row %d col %d, not adding another one before '%c'", row, col, *src);
} else {
client_debug(9, "Implicit line wrap on row %d col %d, adding space before '%c'", row, col, *src);
*dst++ = ' ';
output_col++;
len--;
}
}
}
while (blank_lines > 0) {
/* \n doesn't appear in the buffer, we have to determine where line breaks are by counting columns */
/* Process empty lines in body */
*dst++ = '\r';
*dst++ = '\n';
output_col = 0;
len -= 2;
blank_lines--;
}
if (consec_spaces) {
if (col > 0) {
/* Spaces in the middle of the line, print em out, they count */
assert(sp_start != NULL);
/* Yeah, we could use memcpy here, but this isn't a particularly common case either: */
while (sp_start <= src) { /* up to and including the first char after the spaces */
*dst++ = *sp_start++;
len--;
output_col++;
}
} else {
*dst++ = *src;
len--;
output_col++;
}
} else {
/* Normal character */
*dst++ = *src;
len--;
output_col++;
}
consec_spaces = 0;
}
src++;
if (++col == cols) {
col = 0;
row++;
}
}
if (trailing_crlf) {
*dst++ = '\r';
*dst++ = '\n';
len -= 2;
}
/* Was originally len == 1, but due to the conservative format=flowed line wrapping calculation, we might have some excess room left over */
assert(len >= 1);
if (len > 1) {
client_debug(7, "Finished copying field with %lu bytes left over", len - 1);
}
*dst = '\0';
len--;
*leftover = len;
}
static void dump_body(const char *s, size_t len)
{
client_debug(1, "Body(%lu):", len);
while (*s) {
const char *eol = strstr(s, "\r\n");
if (eol) {
int diff = (int) (eol - s);
client_debug(10, "'%.*s'", diff, s);
s += diff + 2;
} else {
client_debug(10, "%s", s);
return;
}
}
}
/*! \brief Convert form fields into strings */
static int unformat_fields(FIELD *fields[NUM_FIELDS + 1], struct message_constructor *msgc)
{
int i;
char *s[NUM_FIELDS];
size_t lengths[NUM_FIELDS];
for (i = 0; i < NUM_FIELDS; i++) {
lengths[i] = get_field_length(fields[i], fields[i]->dcols, i == NUM_FIELDS - 1);
if (lengths[i]) {
size_t leftover;
s[i] = malloc(lengths[i] + 1);
if (!s[i]) {
/* Clean up unassigned results */
for (i = 0; i < NUM_FIELDS; i++) {
free_if(s[i]);
}
return -1;
}
/* Build trimmed string, which is also format=flowed wrapped at 72 columns. */
client_debug(6, "Parsing message from %d-col field", fields[i]->dcols);
copy_trimmed_str(s[i], field_buffer(fields[i], 0), lengths[i] + 1, fields[i]->dcols, i == NUM_FIELDS - 1, &leftover);
if (i == NUM_FIELDS - 1) {
/* Since get_field_length can over estimate for the body, subtract how much was actually left at the end
* to get the correct length. */
msgc->bodylen = lengths[i] - leftover;
}
} else {
s[i] = NULL;
}
}
#ifdef DEBUG_MODE
/* Dump post-processed form fields */
for (i = 0; i < NUM_FIELDS - 1; i++) {
if (s[i]) {
client_debug(1, "%s(%lu): '%s'", field_names[i], lengths[i], s[i]);
}
}
/* This could be huge: */
dump_body(s[NUM_FIELDS - 1], msgc->bodylen);
#endif
/* Process message */
msgc->from = s[0];
msgc->to = s[1];
msgc->cc = s[2];
msgc->bcc = s[3];
msgc->replyto = s[4];
msgc->subject = s[5];
msgc->body = s[6];
return 0;
}
/*! \brief Create a RFC822 message from the form fields */
static char *create_message(FIELD *fields[NUM_FIELDS + 1], struct message_constructor *msgc, int addbcc, size_t *restrict msglen)
{
char *msg;
char *bcc;
if (unformat_fields(fields, msgc)) {
return NULL;
}
bcc = msgc->bcc;
if (!addbcc) {
/* hide Bcc from RFC822 message creation... */
msgc->bcc = NULL;
}
msg = create_email_message(msgc, msglen);
msgc->bcc = bcc; /* ... and restore it here, for the envelope */
return msg;
}
static void cleanup_message_constructor(struct message_constructor *msgc)
{
free_if(msgc->from);
free_if(msgc->to);
free_if(msgc->cc);
free_if(msgc->bcc);
free_if(msgc->replyto);
free_if(msgc->subject);
free_if(msgc->body);
free_if(msgc->references);
}
static int reformat_msg(FIELD *fields[NUM_FIELDS + 1], struct message_constructor *msgc)
{
memset(msgc, 0, sizeof(struct message_constructor));
if (unformat_fields(fields, msgc)) {
cleanup_message_constructor(msgc);
return -1;
}
return 0;
}
/*! \brief Create and send a message */
static int send_message(struct client *client, FIELD *fields[NUM_FIELDS + 1], struct message_constructor *msgc, struct message_data *mdata)
{
size_t msglen;
int res = -1;
char *msg;
memset(msgc, 0, sizeof(struct message_constructor));
if (mdata) {
msgc->inreplyto = mdata->messageid; /* This is a direct reply to this message */
msgc->references = mdata->references;
}
msg = create_message(fields, msgc, 0, &msglen); /* Don't add Bcc to the RFC822 message, only to the envelope */
msgc->inreplyto = NULL; /* We didn't allocate these, don't free them */
msgc->references = NULL;
if (!msg) {
cleanup_message_constructor(msgc);
return 1;
}
/* Sending can take a moment, update the status bar now */
client_set_status_nout(client, "Sending...");
doupdate();
if (0) {
/*! \todo Future support for BURL IMAP */
} else {
/* The traditional way of sending a message via SMTP
* and then saving a copy of it to the Sent folder
* does involve a classic race condition, whereby if
* the message is sent but the program crashes prior
* to a copy being saved to "Sent", the message is lost.
* Server-side saving or BURL IMAP avoid this. */
res = smtp_send(client, msgc, msg, msglen);
if (!res && client->config->imap_append) {
/* Save sent copy */
res = client_idle_stop(client);
if (!res) {
res = client_append(client, SENT_MAILBOX(client), IMAP_MESSAGE_FLAG_SEEN, msg, msglen);
if (!res && client->sent_mbox) {
increment_stats_by_size(client->sent_mbox, msglen, 0);
increment_stats_by_size(&client->mailboxes[0], msglen, 0); /* Aggregate stats */
}
} else {
res = 2;
}
}
}
free(msg);
return res ? 1 : 0; /* Don't abort program if SMTP transaction failed, just display error and continue */
}
/*! \brief Create a message and save to Drafts */
static int save_message(struct client *client, FIELD *fields[NUM_FIELDS + 1], struct message_constructor *msgc, struct message_data *mdata)
{
size_t msglen;
int res = -1;
char *msg;
memset(msgc, 0, sizeof(struct message_constructor));
if (mdata) {
msgc->inreplyto = mdata->messageid; /* This is a direct reply to this message */
msgc->references = mdata->references;
}
msg = create_message(fields, msgc, 1, &msglen); /* Do add Bcc to RFC822 message, for Drafts */
msgc->inreplyto = NULL; /* We didn't allocate these, don't free them */
msgc->references = NULL;
if (!msg) {
cleanup_message_constructor(msgc);
return 1;
}
/* Saving can take a moment, update the status bar now */
client_set_status_nout(client, "Saving...");
doupdate();
/* Upload to Drafts folder */
if (client_idle_stop(client)) {
return -1;
}
res = client_append(client, DRAFTS_MAILBOX(client), IMAP_MESSAGE_FLAG_DRAFT, msg, msglen); /* Do not mark \Seen, but mark as \Draft */
if (!res && client->draft_mbox) {
increment_stats_by_size(client->draft_mbox, msglen, 1);
increment_stats_by_size(&client->mailboxes[0], msglen, 1); /* Aggregate stats */
}
free(msg);
return res ? 1 : 0;
}
static int addr_needs_quotes(const char *s)
{
/* RFC 5322 3.2.4 */
while (*s) {
if (!isalnum(*s)) {
switch (*s) {
case '!':
case '#':
case '$':
case '%':
case '*':
case '+':
case '-':
case '/':
case '=':
case '?':
case '^':
case '_':
case '`':
case '{':
case '|':
case '}':
case '~':
break;
default:
return 1;
}
}
s++;
}
return 0;
}
static int compute_sender_identity(struct client *client, char *buf, size_t len)
{
if (!strlen_zero(client->config->fromaddr)) {
if (!strlen_zero(client->config->fromname)) {
if (addr_needs_quotes(client->config->fromname)) {
snprintf(buf, len, "\"%s\" <%s>", client->config->fromname, client->config->fromaddr);
} else {
snprintf(buf, len, "%s <%s>", client->config->fromname, client->config->fromaddr);
}
} else {
snprintf(buf, len, "%s", client->config->fromaddr);
}
return 0;
} else if (!strlen_zero(client->config->imap_username)) {
if (strchr(client->config->imap_username, '@')) {
snprintf(buf, len, "<%s>", client->config->imap_username);
} else {
snprintf(buf, len, "<%s@%s>", client->config->imap_username, client->config->imap_hostname);
}
return 0;
} else {
return -1;
}
}
static void populate_fields(FIELD *fields[NUM_FIELDS + 1], struct message_constructor *msgcin)
{
#define PREPOPULATE(index, field) \
if (msgcin->field) { \
client_debug(9, "Prepopulating %s with %lu bytes", #field, index == NUM_FIELDS - 1 ? msgcin->bodylen : strlen(msgcin->field)); \
set_field_buffer(fields[index], 0, msgcin->field); \
}
PREPOPULATE(0, from);
PREPOPULATE(1, to);
PREPOPULATE(2, cc);
PREPOPULATE(3, bcc);
PREPOPULATE(4, replyto);
PREPOPULATE(5, subject);
/* Not body here */
#undef PREPOPULATE
}
static inline __attribute__((always_inline)) void form_driver_str(FORM *form, const char *s)
{
while (*s) {
form_driver(form, *s);
s++;
}
}
static inline __attribute__((always_inline)) int line_len(const char *s)
{
int n = 0;
while (*s) {
if (*s == '\r') {
return n;
} else if (*s == '\n') {
client_debug(4, "Bare LF encountered?");
return n;
}
s++;
n++;
}
return n;
}
static void format_and_quote_body(FORM *form, const char *body, int addquotes)
{
int inputcol = 0;
int outputcol = 0;
int in_quotes = 0, quote_depth = 0;
int row = 0;
const char *s = body;
client_debug(4, "Formatting body for %d col display (add quotes: %d)", COLS, addquotes);
/* We can't print newlines into the form field,
* we have to format it exactly as lines of width COLS.
*
* Additionally, quote each line with >
*
* This would be trivial, were it not for the fact that we want
* the quoted text to fill the entire terminal. */
#define VERIFY_INTEGRITY() \
if (unlikely(form->curcol != outputcol)) { \
client_debug(7, "row %d: curcol is %d but outputcol is %d???", row, form->curcol, outputcol); \
assert(form->curcol == outputcol); /* Crash */ \
}
#define FORM_PUTC(c) \
form_driver(form, c); \
outputcol++; \
if (unlikely(form->curcol != outputcol)) { \
client_debug(7, "row %d: curcol is %d but outputcol is %d? (after inserting %d/%c)??", row, form->curcol, outputcol, c, c); \
assert(form->curcol == outputcol); /* Crash */ \
}
VERIFY_INTEGRITY();
while (*s) {
VERIFY_INTEGRITY();
#ifdef HARDCORE_DEBUG
/* Dump it, dump it all... */
client_debug(10, "row %d, col %d (outputcol %d, quote depth %d) - %d %c", row, inputcol, outputcol, quote_depth, *s, *s);
#endif
if (!strncmp(s, "\r\n", 2)) {
if (outputcol > 0 && s[-1] == ' ' && *(s + 2) && next_n_quotes(s + 2, quote_depth)) {
/* We just printed the format=flowed space at the end of the input line,
* but that conflicts with this path:
* if (addquotes && inputcol == 0 && quote_depth == 0) {
*/
form_driver(form, REQ_DEL_PREV);
outputcol--;
client_debug(9, "format=flowed line break on row %d, col %d", row, outputcol);
/* This is a format=flowed soft line wrap, don't actually wrap */
s += STRLEN("\r\n"); /* Skip CR LF */
s += quote_depth; /* Skip quotes */
inputcol = quote_depth;
if (*s == ' ') { /* XXX Looking back at this, I don't think there would be a space here, but doesn't hurt anything either */
/* Skip space, since we already printed the format=flowed space at end of previous line */
inputcol++;
s++;
client_debug(9, "Skipped format=flowed space, next character is '%c'", *s);
} else {
client_debug(9, "Didn't skip format=flowed space, next character is '%c'", *s);
}
/* Resume */
} else {
if (outputcol > 0 && s[-1] == ' ' && *(s + 2)) {
client_debug(9, "Hard line break on row %d, col %d (would've soft wrapped but quote depth changed)", row, outputcol);
} else {
client_debug(10, "Hard line break on row %d, col %d", row, outputcol);
}
inputcol = 0;
s += STRLEN("\r\n");
form_driver(form, REQ_NEW_LINE);
assert(form->curcol == 0);
outputcol = 0;
if (addquotes) {
FORM_PUTC('>');
}
}
row++;
} else {
if (outputcol == 0) {
if (addquotes) {
client_debug(9, "Quoting beginning of line");
FORM_PUTC('>');
}
} else if (outputcol + 1 >= COLS) {
int j;
const char *last_sp;
/* Need a line wrap, at a word boundary */
client_debug(9, "Wrapping long line at %d cols", outputcol);
last_sp = memrchr(s - outputcol, ' ', outputcol);
if (last_sp) {
int diff = s - 1 - last_sp;
if (diff < outputcol - 2) {
client_debug(9, "Backing up %d characters", diff);
for (j = 0; j < diff; j++) {
form_driver(form, REQ_DEL_PREV);
}
/* Back up and break the line there instead */
s -= diff;
outputcol -= diff;
inputcol -= diff;
} else {
client_debug(1, "Can't wrap this line: %d/%d", diff, outputcol);
}
}
outputcol = 0;
form_driver(form, REQ_NEW_LINE);
VERIFY_INTEGRITY();
if (addquotes) {
FORM_PUTC('>');
}
for (j = 0; j < quote_depth; j++) {
FORM_PUTC('>');
}
if (quote_depth || *s != ' ') { /* Condition added since down below we also add a space, but only if quote_depth is 0 */
/* Ensure there's a space after the quotes before continuing */
FORM_PUTC(' ');
}
if (!strncmp(s, "\r\n", 2)) {
client_debug(9, "Was at CR LF, skipping");
s += 2;
inputcol += 2; /* Needed to avoid adding extraneous "space between quotes and quoted text" */
if (!*s) {
break;
}
}
}
VERIFY_INTEGRITY();
if (inputcol == 0) {
if (*s == '>') {
in_quotes = 1;
quote_depth = 1;
} else {
in_quotes = 0;
quote_depth = 0;
client_debug(10, "Quote depth of row %d is %d", row, quote_depth);
}
} else if (in_quotes) {
if (*s == '>') {
quote_depth++;
} else {
int linelen = line_len(s);
in_quotes = 0;
client_debug(10, "Quote depth of row %d is %d (%d chars left on line)", row, quote_depth, linelen);
/* It doesn't start with space, add one for readability */
if (*s != ' ') {
FORM_PUTC(' ');
}
}
}
if (addquotes && inputcol == 0 && quote_depth == 0) {
/* If this line was previously unquoted, also add a space prior to the quoted text */
client_debug(9, "Adding space between quotes (depth 1) and quoted text");
FORM_PUTC(' ');
}
if (*s == '\t') {
int t, sp_req = 8 - outputcol % 8;
/* Convert tabs to spaces... the editor doesn't support tabs anyways */
for (t = 0; t < sp_req; t++) {
FORM_PUTC(' ');
}
s++;
inputcol++;
} else if (*s > 127) {
switch (*s) {
case 194:
if (*(s + 1) == 160) {
/* NO BREAK SPACE. Thunderbird-based stuff likes to use this for consecutive spaces, tabs, etc. */
FORM_PUTC(' ');
s += 2;
inputcol += 2;
break;
}
/* Fall through */
default:
client_debug(6, "Dunno how to handle UTF-8 character %d %d", *s, *(s + 1));
s++;
inputcol++;
}
} else if (!isprint(*s)) {
client_debug(6, "Skipping non-printable character %d", *s);
s++;
inputcol++;
} else {
FORM_PUTC(*s);
s++;
inputcol++;
}
}
}
#undef FORM_PUTC
#undef VERIFY_INTEGRITY
/* Start on blank line at end */
form_driver(form, REQ_NEW_LINE);
form_driver(form, REQ_NEW_LINE);
}
static char *addrstr(char *s)
{
while (*s) {
if (*s == ',' || *s == ';') {
return s;
}
s++;
}
return NULL;
}
/*! \brief Check whether an address is one of our identities */
static int one_of_our_idents(struct client *client, char *s)
{
char findstr[256];
const char *domain;
char *space;
int res = 0;
static int wildcard_domains = -1;
space = strchr(s, ' '); /* Since we didn't rtrim */
if (space) {
*space = '\0';
}
/* The trivial one */