Annotations will always be done inplace from now on.
[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
49 #ifdef LOCALE
50         setlocale(LC_ALL, "");
51 #endif
52         invo_name = mhbasename(argv[0]);
53
54         /* read user profile/context */
55         context_read();
56
57         arguments = getarguments(invo_name, argc, argv, 1);
58         argp = arguments;
59
60         while ((cp = *argp++)) {
61                 if (*cp == '-') {
62                         switch (smatch(++cp, switches)) {
63                         case AMBIGSW:
64                                 ambigsw(cp, switches);
65                                 done(1);
66                         case UNKWNSW:
67                                 adios(NULL, "-%s unknown", cp);
68
69                         case HELPSW:
70                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
71                                 print_help(buf, switches, 1);
72                                 done(1);
73                         case VERSIONSW:
74                                 print_version(invo_name);
75                                 done(1);
76
77                         case ANNOSW:
78                                 anot++;
79                                 continue;
80                         case NANNOSW:
81                                 anot = 0;
82                                 continue;
83
84                         case EDITRSW:
85                                 if (!(ed = *argp++) || *ed == '-')
86                                         adios(NULL, "missing argument to %s",
87                                                         argp[-2]);
88                                 nedit = 0;
89                                 continue;
90                         case NEDITSW:
91                                 nedit++;
92                                 continue;
93
94                         case WHATSW:
95                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
96                                         adios(NULL, "missing argument to %s",
97                                                         argp[-2]);
98                                 nwhat = 0;
99                                 continue;
100                         case NWHATSW:
101                                 nwhat++;
102                                 continue;
103
104                         case FILESW:
105                                 if (file)
106                                         adios(NULL, "only one file at a time!");
107                                 if (!(cp = *argp++) || *cp == '-')
108                                         adios(NULL, "missing argument to %s",
109                                                         argp[-2]);
110                                 file = getcpy(expanddir(cp));
111                                 continue;
112                         case FORMSW:
113                                 if (!(form = *argp++) || *form == '-')
114                                         adios(NULL, "missing argument to %s",
115                                                         argp[-2]);
116                                 continue;
117                         }
118                 }
119                 if (*cp == '+' || *cp == '@') {
120                         if (folder)
121                                 adios(NULL, "only one folder at a time!");
122                         else
123                                 folder = getcpy(expandfol(cp));
124                 } else {
125                         if (msg)
126                                 adios(NULL, "only one message at a time!");
127                         else
128                                 msg = cp;
129                 }
130         }
131
132         cwd = getcpy(pwd());
133
134         if (file && (msg || folder))
135                 adios(NULL, "can't mix files and folders/msgs");
136
137         in = open_form(&form, distcomps);
138
139         strncpy(drft, m_draft(seq_beyond), sizeof(drft));
140
141         if ((out = creat(drft, m_gmprot())) == NOTOK)
142                 adios(drft, "unable to create");
143
144         cpydata(in, out, form, drft);
145         close(in);
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 }