5c088a83fc4fa32955e75b70e045ecd9263e3500
[mmh] / uip / comp.c
1 /*
2 ** comp.c -- compose 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 EDITRSW  0
15         { "editor editor", 0 },
16 #define FORMSW  1
17         { "form formfile", 0 },
18 #define USESW  2
19         { "use", 0 },
20 #define NUSESW  3
21         { "nouse", 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 use = NOUSE;
36         int in, out;
37         char *cp, *cwd, *maildir;
38         char *ed = NULL, *form = NULL;
39         char *folder = NULL, *msg = NULL, buf[BUFSIZ];
40         char drft[BUFSIZ], **argp, **arguments;
41         struct msgs *mp = NULL;
42         char *fmtstr;
43
44
45 #ifdef LOCALE
46         setlocale(LC_ALL, "");
47 #endif
48         invo_name = mhbasename(argv[0]);
49
50         /* read user profile/context */
51         context_read();
52
53         arguments = getarguments(invo_name, argc, argv, 1);
54         argp = arguments;
55
56         while ((cp = *argp++)) {
57                 if (*cp == '-') {
58                         switch (smatch(++cp, switches)) {
59                         case AMBIGSW:
60                                 ambigsw(cp, switches);
61                                 done(1);
62                         case UNKWNSW:
63                                 adios(NULL, "-%s unknown", cp);
64
65                         case HELPSW:
66                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
67                                 print_help(buf, switches, 1);
68                                 done(1);
69                         case VERSIONSW:
70                                 print_version(invo_name);
71                                 done(1);
72
73                         case EDITRSW:
74                                 if (!(ed = *argp++) || *ed == '-')
75                                         adios(NULL, "missing argument to %s", argp[-2]);
76                                 continue;
77
78                         case WHATSW:
79                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
80                                         adios(NULL, "missing argument to %s", argp[-2]);
81                                 continue;
82
83                         case FORMSW:
84                                 if (!(form = *argp++) || *form == '-')
85                                         adios(NULL, "missing argument to %s", argp[-2]);
86                                 continue;
87
88                         case USESW:
89                                 use++;
90                                 continue;
91                         case NUSESW:
92                                 use = NOUSE;
93                                 continue;
94                         }
95                 }
96                 if (*cp == '+' || *cp == '@') {
97                         if (folder)
98                                 adios(NULL, "only one folder at a time!");
99                         else
100                                 folder = getcpy(expandfol(cp));
101                 } else {
102                         if (msg)
103                                 adios(NULL, "only one message at a time!");
104                         else
105                                 msg = cp;
106                 }
107         }
108
109         cwd = getcpy(pwd());
110
111         if (form && (folder || msg))
112                 adios(NULL, "can't mix forms and folders/msgs");
113
114         if (use && folder)
115                 adios(NULL, "can't mix -use and +folder");
116
117         if (use) {
118                 /* Don't copy; the draft shall get removed in the end. */
119                 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
120
121         } else if (folder || msg) {
122                 /* Take a message as the "form" for the new message. */
123                 if (!msg)
124                         msg = seq_cur;
125                 if (!folder)
126                         folder = getcurfol();
127                 maildir = toabsdir(folder);
128                 if (chdir(maildir) == NOTOK)
129                         adios(maildir, "unable to change directory to");
130                 /* read folder and create message structure */
131                 if (!(mp = folder_read(folder)))
132                         adios(NULL, "unable to read folder %s", folder);
133                 /* check for empty folder */
134                 if (mp->nummsg == 0)
135                         adios(NULL, "no messages in %s", folder);
136                 /* parse the message range/sequence/name and set SELECTED */
137                 if (!m_convert(mp, msg))
138                         done(1);
139                 seq_setprev(mp);  /* set the previous-sequence */
140                 if (mp->numsel > 1)
141                         adios(NULL, "only one message at a time!");
142                 if ((in = open(form = getcpy(m_name(mp->lowsel)),
143                                 O_RDONLY)) == NOTOK)
144                         adios(form, "unable to open message");
145
146                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
147                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
148                         adios(drft, "unable to create");
149                 }
150                 cpydata(in, out, form, drft);
151                 close(in);
152                 close(out);
153
154         } else {
155                 fmtstr = new_fs(form, components);
156                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
157                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
158                         adios(drft, "unable to create");
159                 }
160                 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
161                         adios(drft, "error writing");
162                 }
163                 close(out);
164         }
165
166         context_save();
167         what_now(ed, use, drft, NULL, 0, NULLMP, NULL, cwd);
168         done(1);
169         return 1;
170 }