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