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