mh_init.c
22.1 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2009,
2010 Free Software Foundation, Inc.
GNU Mailutils 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 3, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
/* Initialize MH applications. */
#include <mh.h>
#include <mailutils/url.h>
#include <mailutils/tls.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
char mh_list_format[] =
"%4(msg)"
"%<(cur)+%| %>"
"%<{replied}-%?{encrypted}E%| %>"
"%02(mon{date})/%02(mday{date})"
"%<{date} %|*%>"
"%<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>"
"%<(zero)%17(decode(friendly{from}))%>"
" %(decode{subject})%<{body}<<%{body}>>%>";
/* Make sure stdin is open. If not, connect it to /dev/null. */
static void
mh_ensure_stdin ()
{
int fd = open ("/dev/null", O_RDONLY);
if (fd != 0)
close (fd);
}
void
mh_init ()
{
mh_ensure_stdin ();
/* Register all mailbox and mailer formats */
mu_register_all_formats ();
#ifdef WITH_TLS
mu_init_tls_libs ();
#endif
/* Read user's profile */
mh_read_profile ();
}
void
mh_init2 ()
{
mh_current_folder ();
}
int
mh_read_formfile (char *name, char **pformat)
{
FILE *fp;
struct stat st;
char *ptr;
size_t off = 0;
char *format_str;
char *file_name;
int rc;
rc = mh_find_file (name, &file_name);
if (rc)
{
mu_error (_("cannot access format file %s: %s"), name, strerror (rc));
return -1;
}
if (stat (file_name, &st))
{
mu_error (_("cannot stat format file %s: %s"), file_name,
strerror (errno));
free (file_name);
return -1;
}
fp = fopen (file_name, "r");
if (!fp)
{
mu_error (_("cannot open format file %s: %s"), file_name,
strerror (errno));
free (file_name);
return -1;
}
free (file_name);
format_str = xmalloc (st.st_size+1);
while ((ptr = fgets (format_str + off, st.st_size - off + 1, fp)) != NULL)
{
int len = strlen (ptr);
if (len == 0)
break;
if (*ptr == '%' && ptr[1] == ';')
continue;
if (len > 0 && ptr[len-1] == '\n')
{
if (ptr[len-2] == '\\')
{
len -= 2;
ptr[len] = 0;
}
}
off += len;
}
if (off > 0 && format_str[off-1] == '\n')
off--;
format_str[off] = 0;
fclose (fp);
*pformat = format_str;
return 0;
}
void
mh_err_memory (int fatal)
{
mu_error (_("not enough memory"));
if (fatal)
abort ();
}
static char *my_name;
static char *my_email;
void
mh_get_my_name (char *name)
{
if (!name)
{
struct passwd *pw = getpwuid (getuid ());
if (!pw)
{
mu_error (_("cannot determine my username"));
return;
}
name = pw->pw_name;
}
my_name = strdup (name);
my_email = mu_get_user_email (name);
}
int
emailcmp (char *pattern, char *name)
{
char *p;
p = strchr (pattern, '@');
if (p)
for (p++; *p; p++)
*p = mu_toupper (*p);
return fnmatch (pattern, name, 0);
}
int
mh_is_my_name (const char *name)
{
char *pname, *p;
int rc = 0;
pname = strdup (name);
p = strchr (pname, '@');
if (p)
for (p++; *p; p++)
*p = mu_toupper (*p);
if (!my_email)
mh_get_my_name (NULL);
if (emailcmp (my_email, pname) == 0)
rc = 1;
else
{
const char *nlist = mh_global_profile_get ("Alternate-Mailboxes", NULL);
if (nlist)
{
const char *end, *p;
char *pat;
int len;
for (p = nlist; rc == 0 && *p; p = end)
{
while (*p && mu_isspace (*p))
p++;
end = strchr (p, ',');
if (end)
{
len = end - p;
end++;
}
else
{
len = strlen (p);
end = p + len;
}
while (len > 0 && mu_isspace (p[len-1]))
len--;
pat = xmalloc (len + 1);
memcpy (pat, p, len);
pat[len] = 0;
rc = emailcmp (pat, pname) == 0;
free (pat);
}
}
}
free (pname);
return rc;
}
char *
mh_my_email ()
{
if (!my_email)
mh_get_my_name (NULL);
return my_email;
}
static int
make_dir_hier (const char *p, mode_t perm)
{
int rc = 0;
char *dir = xstrdup (p);
char *q = dir;
while (!rc && (q = strchr (q + 1, '/')))
{
*q = 0;
if (access (dir, X_OK))
{
if (errno != ENOENT)
{
mu_error (_("cannot create directory %s: error accessing name component %s: %s"),
p, dir, strerror (errno));
rc = 1;
}
else if ((rc = mkdir (dir, perm)))
mu_error (_("cannot create directory %s: error creating name component %s: %s"),
p, dir, mu_strerror (rc));
}
*q = '/';
}
free (dir);
return rc;
}
int
mh_makedir (const char *p)
{
int rc;
mode_t save_umask;
mode_t perm = 0711;
const char *pb = mh_global_profile_get ("Folder-Protect", NULL);
if (pb)
perm = strtoul (pb, NULL, 8);
save_umask = umask (0);
if ((rc = make_dir_hier (p, perm)) == 0)
{
rc = mkdir (p, perm);
if (rc)
mu_error (_("cannot create directory %s: %s"),
p, strerror (errno));
}
umask (save_umask);
return rc;
}
int
mh_check_folder (const char *pathname, int confirm)
{
const char *p;
struct stat st;
if ((p = strchr (pathname, ':')) != NULL)
p++;
else
p = pathname;
if (stat (p, &st))
{
if (errno == ENOENT)
{
/* TRANSLATORS: This is a question and will be followed
by question mark on output. */
if (!confirm || mh_getyn (_("Create folder \"%s\""), p))
return mh_makedir (p);
else
return 1;
}
else
{
mu_diag_funcall (MU_DIAG_ERROR, "stat", p, errno);
return 1;
}
}
return 0;
}
int
mh_interactive_mode_p ()
{
static int interactive = -1;
if (interactive < 0)
interactive = isatty (fileno (stdin)) ? 1 : 0;
return interactive;
}
int
mh_vgetyn (const char *fmt, va_list ap)
{
char repl[64];
while (1)
{
char *p;
int len, rc;
vfprintf (stdout, fmt, ap);
fprintf (stdout, "? ");
p = fgets (repl, sizeof repl, stdin);
if (!p)
return 0;
len = strlen (p);
if (len > 0 && p[len-1] == '\n')
p[len--] = 0;
rc = mu_true_answer_p (p);
if (rc >= 0)
return rc;
/* TRANSLATORS: See msgids "nN" and "yY". */
fprintf (stdout, _("Please answer yes or no: "));
}
return 0; /* to pacify gcc */
}
int
mh_getyn (const char *fmt, ...)
{
va_list ap;
int rc;
if (!mh_interactive_mode_p ())
return 1;
va_start (ap, fmt);
rc = mh_vgetyn (fmt, ap);
va_end (ap);
return rc;
}
int
mh_getyn_interactive (const char *fmt, ...)
{
va_list ap;
int rc;
va_start (ap, fmt);
rc = mh_vgetyn (fmt, ap);
va_end (ap);
return rc;
}
FILE *
mh_audit_open (char *name, mu_mailbox_t mbox)
{
FILE *fp;
char date[64];
time_t t;
struct tm *tm;
mu_url_t url;
char *namep;
namep = mu_tilde_expansion (name, "/", NULL);
if (strchr (namep, '/') == NULL)
{
char *p = mh_safe_make_file_name (mu_folder_directory (), namep);
free (namep);
namep = p;
}
fp = fopen (namep, "a");
if (!fp)
{
mu_error (_("cannot open audit file %s: %s"), namep, strerror (errno));
free (namep);
return NULL;
}
free (namep);
time (&t);
tm = localtime (&t);
mu_strftime (date, sizeof date, "%a, %d %b %Y %H:%M:%S %Z", tm);
mu_mailbox_get_url (mbox, &url);
fprintf (fp, "<<%s>> %s %s\n",
mu_program_name,
date,
mu_url_to_string (url));
return fp;
}
void
mh_audit_close (FILE *fp)
{
fclose (fp);
}
int
mh_message_number (mu_message_t msg, size_t *pnum)
{
return mu_message_get_uid (msg, pnum);
}
mu_mailbox_t
mh_open_folder (const char *folder, int flags)
{
mu_mailbox_t mbox = NULL;
char *name;
name = mh_expand_name (NULL, folder, 1);
if ((flags & MU_STREAM_CREAT) && mh_check_folder (name, 1))
exit (0);
if (mu_mailbox_create_default (&mbox, name))
{
mu_error (_("cannot create mailbox %s: %s"),
name, strerror (errno));
exit (1);
}
if (mu_mailbox_open (mbox, flags))
{
mu_error (_("cannot open mailbox %s: %s"), name, strerror (errno));
exit (1);
}
free (name);
return mbox;
}
char *
mh_get_dir ()
{
const char *mhdir = mh_global_profile_get ("Path", "Mail");
char *mhcopy;
if (mhdir[0] != '/')
{
char *p = mu_get_homedir ();
mhcopy = mh_safe_make_file_name (p, mhdir);
free (p);
}
else
mhcopy = strdup (mhdir);
if (!mhcopy)
{
mu_error (_("not enough memory"));
abort ();
}
return mhcopy;
}
char *
mh_expand_name (const char *base, const char *name, int is_folder)
{
char *p = NULL;
char *namep = NULL;
namep = mu_tilde_expansion (name, "/", NULL);
if (namep[0] == '+')
memmove (namep, namep + 1, strlen (namep)); /* copy null byte as well */
else if (strncmp (namep, "../", 3) == 0 || strncmp (namep, "./", 2) == 0)
{
char *cwd = mu_getcwd ();
char *tmp = mh_safe_make_file_name (cwd, namep);
free (cwd);
free (namep);
namep = tmp;
}
if (is_folder)
{
if (memcmp (namep, "mh:/", 4) == 0)
return namep;
else if (namep[0] == '/')
mu_asprintf (&p, "mh:%s", namep);
else
mu_asprintf (&p, "mh:%s/%s", base ? base : mu_folder_directory (),
namep);
}
else if (namep[0] != '/')
mu_asprintf (&p, "%s/%s", base ? base : mu_folder_directory (), namep);
else
return namep;
free (namep);
return p;
}
int
mh_find_file (const char *name, char **resolved_name)
{
char *s;
int rc;
if (name[0] == '/' ||
(name[0] == '.' && name[1] == '/') ||
(name[0] == '.' && name[1] == '.' && name[2] == '/'))
{
*resolved_name = xstrdup (name);
if (access (name, R_OK) == 0)
return 0;
return errno;
}
if (name[0] == '~')
{
s = mu_tilde_expansion (name, "/", NULL);
*resolved_name = s;
if (access (s, R_OK) == 0)
return 0;
return errno;
}
s = mh_expand_name (NULL, name, 0);
if (access (s, R_OK) == 0)
{
*resolved_name = s;
return 0;
}
if (errno != ENOENT)
mu_diag_output (MU_DIAG_WARNING,
_("cannot access %s: %s"), s, mu_strerror (errno));
free (s);
s = mh_expand_name (mh_global_profile_get ("mhetcdir", MHLIBDIR), name, 0);
if (access (s, R_OK) == 0)
{
*resolved_name = s;
return 0;
}
if (errno != ENOENT)
mu_diag_output (MU_DIAG_WARNING,
_("cannot access %s: %s"), s, mu_strerror (errno));
free (s);
*resolved_name = xstrdup (name);
if (access (name, R_OK) == 0)
return 0;
rc = errno;
if (rc != ENOENT)
mu_diag_output (MU_DIAG_WARNING,
_("cannot access %s: %s"), s, mu_strerror (rc));
return rc;
}
int
mh_iterate (mu_mailbox_t mbox, mh_msgset_t *msgset,
mh_iterator_fp itr, void *data)
{
int rc;
size_t i;
for (i = 0; i < msgset->count; i++)
{
mu_message_t msg;
size_t num;
num = msgset->list[i];
if ((rc = mu_mailbox_get_message (mbox, num, &msg)) != 0)
{
mu_error (_("cannot get message %lu: %s"),
(unsigned long) num, mu_strerror (rc));
return 1;
}
itr (mbox, msg, num, data);
}
return 0;
}
int
mh_spawnp (const char *prog, const char *file)
{
struct mu_wordsplit ws;
size_t i;
int rc, status;
char **xargv;
ws.ws_comment = "#";
if (mu_wordsplit (prog, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT))
{
mu_error (_("cannot split line `%s': %s"), prog,
mu_wordsplit_strerror (&ws));
return 1;
}
xargv = calloc (ws.ws_wordc + 2, sizeof (*xargv));
if (!xargv)
{
mh_err_memory (0);
mu_wordsplit_free (&ws);
return 1;
}
for (i = 0; i < ws.ws_wordc; i++)
xargv[i] = ws.ws_wordv[i];
xargv[i++] = (char*) file;
xargv[i++] = NULL;
rc = mu_spawnvp (xargv[0], xargv, &status);
free (xargv);
mu_wordsplit_free (&ws);
return rc;
}
/* Copy data from FROM to TO, creating the latter if necessary.
FIXME: How about formats?
*/
int
mh_file_copy (const char *from, const char *to)
{
mu_stream_t in, out, flt;
int rc;
if ((rc = mu_file_stream_create (&in, from, MU_STREAM_READ)))
{
mu_error (_("cannot open input file `%s': %s"),
from, mu_strerror (rc));
return 1;
}
if ((rc = mu_file_stream_create (&out, to, MU_STREAM_RDWR|MU_STREAM_CREAT)))
{
mu_error (_("cannot open output file `%s': %s"),
to, mu_strerror (rc));
mu_stream_destroy (&in);
return 1;
}
rc = mu_filter_create (&flt, in, "INLINE-COMMENT", MU_FILTER_DECODE,
MU_STREAM_READ);
mu_stream_unref (in);
if (rc)
{
mu_error (_("cannot open filter stream: %s"), mu_strerror (rc));
mu_stream_destroy (&out);
return 1;
}
rc = mu_stream_copy (out, flt, 0, NULL);
mu_stream_destroy (&flt);
mu_stream_destroy (&out);
if (rc)
mu_error (_("error copying file `%s' to `%s': %s"),
from, to, mu_strerror (rc));
return rc;
}
static mu_message_t
_file_to_message (const char *file_name)
{
struct stat st;
int rc;
mu_stream_t instream;
if (stat (file_name, &st) < 0)
{
mu_diag_funcall (MU_DIAG_ERROR, "stat", file_name, errno);
return NULL;
}
if ((rc = mu_file_stream_create (&instream, file_name, MU_STREAM_READ)))
{
mu_error (_("cannot create input stream (file %s): %s"),
file_name, mu_strerror (rc));
return NULL;
}
return mh_stream_to_message (instream);
}
mu_message_t
mh_file_to_message (const char *folder, const char *file_name)
{
mu_message_t msg;
char *tmp_name = NULL;
if (folder)
{
tmp_name = mh_expand_name (folder, file_name, 0);
msg = _file_to_message (tmp_name);
free (tmp_name);
}
else
msg = _file_to_message (file_name);
return msg;
}
void
mh_install_help (char *mhdir)
{
static char *text = N_(
"Prior to using MH, it is necessary to have a file in your login\n"
"directory (%s) named .mh_profile which contains information\n"
"to direct certain MH operations. The only item which is required\n"
"is the path to use for all MH folder operations. The suggested MH\n"
"path for you is %s...\n");
printf (_(text), mu_get_homedir (), mhdir);
}
void
mh_real_install (char *name, int automode)
{
char *home = mu_get_homedir ();
char *mhdir;
char *ctx;
mu_stream_t in;
int rc;
FILE *fp;
rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
if (rc)
{
mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
exit (1);
}
mhdir = mh_safe_make_file_name (home, "Mail");
if (!automode)
{
/* TRANSLATORS: This is a question and will be followed
by question mark on output. */
if (mh_getyn_interactive (_("Do you need help")))
mh_install_help (mhdir);
/* TRANSLATORS: This is a question and will be followed
by question mark on output. */
if (!mh_getyn_interactive (_("Do you want the standard MH path \"%s\""), mhdir))
{
int local;
char *p, *buf = NULL;
size_t size = 0;
/* TRANSLATORS: This is a question and will be followed
by question mark on output. */
local = mh_getyn_interactive (_("Do you want a path below your login directory"));
if (local)
printf (_("What is the path? "));
else
printf (_("What is the full path? "));
if (mu_stream_getline (in, &buf, &size, NULL))
exit (1);
p = mu_str_stripws (buf);
if (p > buf)
memmove (buf, p, strlen (p) + 1);
free (mhdir);
if (local)
{
mhdir = mh_safe_make_file_name (home, p);
free (p);
}
else
mhdir = p;
}
}
if (mh_check_folder (mhdir, !automode))
exit (1);
fp = fopen (name, "w");
if (!fp)
{
mu_error (_("cannot open file %s: %s"), name, mu_strerror (errno));
exit (1);
}
fprintf (fp, "Path: %s\n", mhdir);
fclose (fp);
ctx = mh_safe_make_file_name (mhdir, MH_CONTEXT_FILE);
fp = fopen (ctx, "w");
if (fp)
{
fprintf (fp, "Current-Folder: inbox\n");
fclose (fp);
}
free (ctx);
ctx = mh_safe_make_file_name (mhdir, "inbox");
if (mh_check_folder (ctx, !automode))
exit (1);
free (ctx);
free (mhdir);
}
void
mh_install (char *name, int automode)
{
struct stat st;
if (stat(name, &st))
{
if (errno == ENOENT)
{
if (automode)
printf(_("I'm going to create the standard MH path for you.\n"));
mh_real_install (name, automode);
}
else
{
mu_diag_funcall (MU_DIAG_ERROR, "stat", name, errno);
exit (1);
}
}
else if ((st.st_mode & S_IFREG) || (st.st_mode & S_IFLNK))
{
mu_error(_("You already have an MH profile, use an editor to modify it"));
exit (1);
}
else
{
mu_error (_("You already have file %s which is not a regular file or a symbolic link."), name);
mu_error (_("Please remove it and try again"));
exit (1);
}
}
void
mh_annotate (mu_message_t msg, char *field, char *text, int date)
{
mu_header_t hdr;
mu_attribute_t attr;
if (mu_message_get_header (msg, &hdr))
return;
if (date)
{
time_t t;
struct tm *tm;
char datebuf[80];
t = time (NULL);
tm = localtime (&t);
mu_strftime (datebuf, sizeof datebuf, "%a, %d %b %Y %H:%M:%S %Z", tm);
mu_header_set_value (hdr, field, datebuf, 0);
}
if (text)
mu_header_set_value (hdr, field, text, 0);
mu_message_get_attribute (msg, &attr);
mu_attribute_set_modified (attr);
}
char *
mh_draft_name ()
{
return mh_expand_name (mh_global_profile_get ("Draft-Folder",
mu_folder_directory ()),
"draft", 0);
}
char *
mh_create_message_id (int subpart)
{
char *p;
mu_rfc2822_msg_id (subpart, &p);
return p;
}
void
mh_set_reply_regex (const char *str)
{
char *err;
int rc = mu_unre_set_regex (str, 0, &err);
if (rc)
mu_error ("reply_regex: %s%s%s", mu_strerror (rc),
err ? ": " : "",
err ? err : "");
}
const char *
mh_charset (const char *dfl)
{
const char *charset = mh_global_profile_get ("Charset", dfl);
if (!charset)
return NULL;
if (mu_c_strcasecmp (charset, "auto") == 0)
{
/* Try to deduce the charset from LC_ALL variable */
char *lc_all = getenv ("LC_ALL");
if (lc_all)
{
char *sp;
char *lang;
char *terr;
char *tmp = strdup (lc_all);
lang = strtok_r (tmp, "_", &sp);
terr = strtok_r (NULL, ".", &sp);
charset = strtok_r (NULL, "@", &sp);
if (!charset)
charset = mu_charset_lookup (lang, terr);
free (tmp);
}
}
return charset;
}
int
mh_decode_2047 (char *text, char **decoded_text)
{
const char *charset = mh_charset (NULL);
if (!charset)
return 1;
return mu_rfc2047_decode (charset, text, decoded_text);
}
void
mh_quote (const char *in, char **out)
{
size_t len = strlen (in);
if (len && in[0] == '"' && in[len - 1] == '"')
{
const char *p;
char *q;
for (p = in + 1; p < in + len - 1; p++)
if (*p == '\\' || *p == '"')
len++;
*out = xmalloc (len + 1);
q = *out;
p = in;
*q++ = *p++;
while (p[1])
{
if (*p == '\\' || *p == '"')
*q++ = '\\';
*q++ = *p++;
}
*q++ = *p++;
*q = 0;
}
else
*out = xstrdup (in);
}
void
mh_expand_aliases (mu_message_t msg,
mu_address_t *addr_to,
mu_address_t *addr_cc,
mu_address_t *addr_bcc)
{
mu_header_t hdr;
size_t i, num;
const char *buf;
mu_message_get_header (msg, &hdr);
mu_header_get_field_count (hdr, &num);
for (i = 1; i <= num; i++)
{
if (mu_header_sget_field_name (hdr, i, &buf) == 0)
{
if (mu_c_strcasecmp (buf, MU_HEADER_TO) == 0
|| mu_c_strcasecmp (buf, MU_HEADER_CC) == 0
|| mu_c_strcasecmp (buf, MU_HEADER_BCC) == 0)
{
char *value;
mu_address_t addr = NULL;
int incl;
mu_header_aget_field_value_unfold (hdr, i, &value);
mh_alias_expand (value, &addr, &incl);
free (value);
if (mu_c_strcasecmp (buf, MU_HEADER_TO) == 0)
mu_address_union (addr_to, addr);
else if (mu_c_strcasecmp (buf, MU_HEADER_CC) == 0)
mu_address_union (addr_cc ? addr_cc : addr_to, addr);
else if (mu_c_strcasecmp (buf, MU_HEADER_BCC) == 0)
mu_address_union (addr_bcc ? addr_bcc : addr_to, addr);
}
}
}
}
int
mh_draft_message (const char *name, const char *msgspec, char **pname)
{
mu_url_t url;
size_t uid;
int rc;
mu_mailbox_t mbox;
const char *path;
mbox = mh_open_folder (name, MU_STREAM_RDWR);
if (!mbox)
return 1;
mu_mailbox_get_url (mbox, &url);
if (strcmp (msgspec, "new") == 0)
{
mu_property_t prop;
rc = mu_mailbox_uidnext (mbox, &uid);
if (rc)
{
mu_error (_("cannot obtain sequence number for the new message: %s"),
mu_strerror (rc));
exit (1);
}
mu_mailbox_get_property (mbox, &prop);
mu_property_set_value (prop, "cur", mu_umaxtostr (0, uid), 1);
}
else
{
char *argv[2];
mh_msgset_t msgset;
argv[0] = (char*) msgspec;
argv[1] = NULL;
mh_msgset_parse (mbox, &msgset, 1, argv, "cur");
if (msgset.count > 1)
mu_error (_("only one message at a time!"));
else
{
mh_msgset_uids (mbox, &msgset);
uid = msgset.list[0];
}
mh_msgset_free (&msgset);
}
mu_url_sget_path (url, &path);
rc = mu_asprintf (pname, "%s/%lu", path, (unsigned long) uid);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_asprintf", NULL, rc);
exit (1);
}
mu_mailbox_close (mbox);
mu_mailbox_destroy (&mbox);
return rc;
}
char *
mh_safe_make_file_name (const char *dir, const char *file)
{
char *name = mu_make_file_name (dir, file);
if (!name)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
abort ();
}
return name;
}