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