Rearranged whitespace (and comments) in all the code!
[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 DFOLDSW  2
19         { "draftfolder +folder", 0 },
20 #define DMSGSW  3
21         { "draftmessage msg", 0 },
22 #define NDFLDSW  4
23         { "nodraftfolder", 0 },
24 #define EDITRSW  5
25         { "editor editor", 0 },
26 #define NEDITSW  6
27         { "noedit", 0 },
28 #define FORMSW  7
29         { "form formfile", 0 },
30 #define INPLSW  8
31         { "inplace", 0 },
32 #define NINPLSW  9
33         { "noinplace", 0 },
34 #define WHATSW  10
35         { "whatnowproc program", 0 },
36 #define NWHATSW  11
37         { "nowhatnowproc", 0 },
38 #define VERSIONSW  12
39         { "version", 0 },
40 #define HELPSW  13
41         { "help", 0 },
42 #define FILESW  14
43         { "file file", -4 },  /* interface from msh */
44         { NULL, 0 }
45 };
46
47 static struct swit aqrnl[] = {
48 #define NOSW  0
49         { "quit", 0 },
50 #define YESW  1
51         { "replace", 0 },
52 #define LISTDSW  2
53         { "list", 0 },
54 #define REFILSW  3
55         { "refile +folder", 0 },
56 #define NEWSW  4
57         { "new", 0 },
58         { NULL, 0 }
59 };
60
61
62 static struct swit aqrl[] = {
63         { "quit", 0 },
64         { "replace", 0 },
65         { "list", 0 },
66         { "refile +folder", 0 },
67         { NULL, 0 }
68 };
69
70
71 int
72 main (int argc, char **argv)
73 {
74         int anot = 0, inplace = 1, nedit = 0;
75         int nwhat = 0, i, in, isdf = 0, out;
76         char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
77         char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
78         char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
79         char **argp, **arguments;
80         struct msgs *mp = NULL;
81         struct stat st;
82
83 #ifdef LOCALE
84         setlocale(LC_ALL, "");
85 #endif
86         invo_name = r1bindex (argv[0], '/');
87
88         /* read user profile/context */
89         context_read();
90
91         arguments = getarguments (invo_name, argc, argv, 1);
92         argp = arguments;
93
94         while ((cp = *argp++)) {
95                 if (*cp == '-') {
96                         switch (smatch (++cp, switches)) {
97                                 case AMBIGSW:
98                                         ambigsw (cp, switches);
99                                         done (1);
100                                 case UNKWNSW:
101                                         adios (NULL, "-%s unknown", cp);
102
103                                 case HELPSW:
104                                         snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
105                                                 invo_name);
106                                         print_help (buf, switches, 1);
107                                         done (1);
108                                 case VERSIONSW:
109                                         print_version(invo_name);
110                                         done (1);
111
112                                 case ANNOSW:
113                                         anot++;
114                                         continue;
115                                 case NANNOSW:
116                                         anot = 0;
117                                         continue;
118
119                                 case EDITRSW:
120                                         if (!(ed = *argp++) || *ed == '-')
121                                                 adios (NULL, "missing argument to %s", argp[-2]);
122                                         nedit = 0;
123                                         continue;
124                                 case NEDITSW:
125                                         nedit++;
126                                         continue;
127
128                                 case WHATSW:
129                                         if (!(whatnowproc = *argp++) || *whatnowproc == '-')
130                                                 adios (NULL, "missing argument to %s", argp[-2]);
131                                         nwhat = 0;
132                                         continue;
133                                 case NWHATSW:
134                                         nwhat++;
135                                         continue;
136
137                                 case FILESW:
138                                         if (file)
139                                                 adios (NULL, "only one file at a time!");
140                                         if (!(cp = *argp++) || *cp == '-')
141                                                 adios (NULL, "missing argument to %s", argp[-2]);
142                                         file = path (cp, TFILE);
143                                         continue;
144                                 case FORMSW:
145                                         if (!(form = *argp++) || *form == '-')
146                                                 adios (NULL, "missing argument to %s", argp[-2]);
147                                         continue;
148
149                                 case INPLSW:
150                                         inplace++;
151                                         continue;
152                                 case NINPLSW:
153                                         inplace = 0;
154                                         continue;
155
156                                 case DFOLDSW:
157                                         if (dfolder)
158                                                 adios (NULL, "only one draft folder at a time!");
159                                         if (!(cp = *argp++) || *cp == '-')
160                                                 adios (NULL, "missing argument to %s", argp[-2]);
161                                         dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
162                                                         *cp != '@' ? TFOLDER : TSUBCWF);
163                                         continue;
164                                 case DMSGSW:
165                                         if (dmsg)
166                                                 adios (NULL, "only one draft message at a time!");
167                                         if (!(dmsg = *argp++) || *dmsg == '-')
168                                                 adios (NULL, "missing argument to %s", argp[-2]);
169                                         continue;
170                                 case NDFLDSW:
171                                         dfolder = NULL;
172                                         isdf = NOTOK;
173                                         continue;
174                         }
175                 }
176                 if (*cp == '+' || *cp == '@') {
177                         if (folder)
178                                 adios (NULL, "only one folder at a time!");
179                         else
180                                 folder = pluspath (cp);
181                 } else {
182                         if (msg)
183                                 adios (NULL, "only one message at a time!");
184                         else
185                                 msg = cp;
186                 }
187         }
188
189         cwd = getcpy (pwd ());
190
191         if (!context_find ("path"))
192                 free (path ("./", TFOLDER));
193         if (file && (msg || folder))
194                 adios (NULL, "can't mix files and folders/msgs");
195
196         in = open_form(&form, distcomps);
197
198 try_it_again:
199         strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
200
201         /* Check if draft already exists */
202         if (stat (drft, &st) != NOTOK) {
203                 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
204                 for (i = LISTDSW; i != YESW;) {
205                         if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
206                                 done (1);
207                         switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
208                                 case NOSW:
209                                         done (0);
210                                 case NEWSW:
211                                         dmsg = NULL;
212                                         goto try_it_again;
213                                 case YESW:
214                                         break;
215                                 case LISTDSW:
216                                         showfile (++argp, drft);
217                                         break;
218                                 case REFILSW:
219                                         if (refile (++argp, drft) == 0)
220                                                 i = YESW;
221                                         break;
222                                 default:
223                                         advise (NULL, "say what?");
224                                         break;
225                         }
226                 }
227         }
228         if ((out = creat (drft, m_gmprot ())) == NOTOK)
229                 adios (drft, "unable to create");
230
231         cpydata (in, out, form, drft);
232         close (in);
233         close (out);
234
235         if (file) {
236                 /*
237                  * Dist a file
238                  */
239                 anot = 0;  /* don't want to annotate a file */
240         } else {
241                 /*
242                  * Dist a message
243                  */
244                 if (!msg)
245                         msg = "cur";
246                 if (!folder)
247                         folder = getfolder (1);
248                 maildir = m_maildir (folder);
249
250                 if (chdir (maildir) == NOTOK)
251                         adios (maildir, "unable to change directory to");
252
253                 /* read folder and create message structure */
254                 if (!(mp = folder_read (folder)))
255                         adios (NULL, "unable to read folder %s", folder);
256
257                 /* check for empty folder */
258                 if (mp->nummsg == 0)
259                         adios (NULL, "no messages in %s", folder);
260
261                 /* parse the message range/sequence/name and set SELECTED */
262                 if (!m_convert (mp, msg))
263                         done (1);
264                 seq_setprev (mp);  /* set the previous-sequence */
265
266                 if (mp->numsel > 1)
267                         adios (NULL, "only one message at a time!");
268         }
269
270         msgnam = file ? file : getcpy (m_name (mp->lowsel));
271         if ((in = open (msgnam, O_RDONLY)) == NOTOK)
272                 adios (msgnam, "unable to open message");
273
274         if (!file) {
275                 context_replace (pfolder, folder);  /* update current folder */
276                 seq_setcur (mp, mp->lowsel);  /* update current message */
277                 seq_save (mp);  /* synchronize sequences  */
278                 context_save ();  /* save the context file  */
279         }
280
281         if (nwhat)
282                 done (0);
283         what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
284                 anot ? "Resent" : NULL, inplace, cwd);
285         done (1);
286         return 1;
287 }