21c67028f30a7303fa35232ef08d3fc80075c837
[mmh] / uip / dist.c
1 /*
2 ** dist.c -- re-distribute a message
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <locale.h>
14
15 static struct swit switches[] = {
16 #define ANNOSW  0
17         { "annotate", 0 },
18 #define NANNOSW  1
19         { "noannotate", 2 },
20 #define EDITRSW  2
21         { "editor editor", 0 },
22 #define FORMSW  3
23         { "form formfile", 0 },
24 #define WHATSW  4
25         { "whatnowproc program", 0 },
26 #define VERSIONSW  5
27         { "Version", 0 },
28 #define HELPSW  6
29         { "help", 0 },
30         { NULL, 0 }
31 };
32
33
34 int
35 main(int argc, char **argv)
36 {
37         int anot = 0;
38         int in, out;
39         char *cp, *cwd, *maildir, *msgnam;
40         char *ed = NULL, *folder = NULL;
41         char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
42         char **argp, **arguments;
43         struct msgs *mp = NULL;
44         char *fmtstr;
45
46         setlocale(LC_ALL, "");
47         invo_name = mhbasename(argv[0]);
48
49         /* read user profile/context */
50         context_read();
51
52         arguments = getarguments(invo_name, argc, argv, 1);
53         argp = arguments;
54
55         while ((cp = *argp++)) {
56                 if (*cp == '-') {
57                         switch (smatch(++cp, switches)) {
58                         case AMBIGSW:
59                                 ambigsw(cp, switches);
60                                 /* sysexits.h EX_USAGE*/
61                                 exit(1);
62                         case UNKWNSW:
63                                 adios(NULL, "-%s unknown", cp);
64
65                         case HELPSW:
66                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
67                                 print_help(buf, switches, 1);
68                                 exit(0);
69                         case VERSIONSW:
70                                 print_version(invo_name);
71                                 exit(0);
72
73                         case ANNOSW:
74                                 anot++;
75                                 continue;
76                         case NANNOSW:
77                                 anot = 0;
78                                 continue;
79
80                         case EDITRSW:
81                                 if (!(ed = *argp++) || *ed == '-')
82                                         adios(NULL, "missing argument to %s",
83                                                         argp[-2]);
84                                 continue;
85
86                         case WHATSW:
87                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
88                                         adios(NULL, "missing argument to %s",
89                                                         argp[-2]);
90                                 continue;
91
92                         case FORMSW:
93                                 if (!(form = *argp++) || *form == '-')
94                                         adios(NULL, "missing argument to %s",
95                                                         argp[-2]);
96                                 continue;
97                         }
98                 }
99                 if (*cp == '+' || *cp == '@') {
100                         if (folder)
101                                 adios(NULL, "only one folder at a time!");
102                         else
103                                 folder = getcpy(expandfol(cp));
104                 } else {
105                         if (msg)
106                                 adios(NULL, "only one message at a time!");
107                         else
108                                 msg = cp;
109                 }
110         }
111
112         cwd = getcpy(pwd());
113
114         strncpy(drft, m_draft(seq_beyond), sizeof(drft));
115         if ((out = creat(drft, m_gmprot())) == NOTOK)
116                 adios(drft, "unable to create");
117
118         fmtstr = new_fs(form, distcomps);
119         if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
120                 adios(drft, "error writing");
121         }
122         close(out);
123
124         if (!msg)
125                 msg = seq_cur;
126         if (!folder)
127                 folder = getcurfol();
128         maildir = toabsdir(folder);
129
130         if (chdir(maildir) == NOTOK)
131                 adios(maildir, "unable to change directory to");
132
133         if (!(mp = folder_read(folder)))
134                 adios(NULL, "unable to read folder %s", folder);
135
136         /* check for empty folder */
137         if (mp->nummsg == 0)
138                 adios(NULL, "no messages in %s", folder);
139
140         /* parse the message range/sequence/name and set SELECTED */
141         if (!m_convert(mp, msg))
142                 /* sysexits.h EX_USAGE*/
143                 exit(1);
144         seq_setprev(mp);
145
146         if (mp->numsel > 1)
147                 adios(NULL, "only one message at a time!");
148
149         msgnam = getcpy(m_name(mp->lowsel));
150         if ((in = open(msgnam, O_RDONLY)) == NOTOK)
151                 adios(msgnam, "unable to open message");
152
153         context_replace(curfolder, folder);
154         seq_setcur(mp, mp->lowsel);
155         seq_save(mp);  /* synchronize sequences  */
156         context_save();
157
158         what_now(ed, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL, cwd);
159         /*sysexits.h EX_SOFTWARE*/
160         return 1;
161 }