Removed TLS support. We don't need it anymore as we stopped talking SMTP
[mmh] / uip / whom.c
1
2 /*
3  * whom.c -- report to whom a message would be sent
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12 #include <signal.h>
13
14 static struct swit switches[] = {
15 #define ALIASW              0
16     { "alias aliasfile", 0 },
17 #define CHKSW               1
18     { "check", 0 },
19 #define NOCHKSW             2
20     { "nocheck", 0 },
21 #define DRAFTSW             3
22     { "draft", 0 },
23 #define DFOLDSW             4
24     { "draftfolder +folder", 6 },
25 #define DMSGSW              5
26     { "draftmessage msg", 6 },
27 #define NDFLDSW             6
28     { "nodraftfolder", 0 },
29 #define VERSIONSW           7
30     { "version", 0 },
31 #define HELPSW              8
32     { "help", 0 },
33 #define CLIESW              9
34     { "client host", -6 },
35 #define SERVSW             10
36     { "server host", -6 },
37 #define SNOOPSW            11
38     { "snoop", -5 },
39 #define PORTSW             15
40     { "port server port name/number", 4 },
41     { NULL, 0 }
42 };
43
44
45 int
46 main (int argc, char **argv)
47 {
48     pid_t child_id;
49     int i, status, isdf = 0;
50     int distsw = 0, vecp = 0;
51     char *cp, *dfolder = NULL, *dmsg = NULL;
52     char *msg = NULL, **ap, **argp, backup[BUFSIZ];
53     char buf[BUFSIZ], **arguments, *vec[MAXARGS];
54
55 #ifdef LOCALE
56     setlocale(LC_ALL, "");
57 #endif
58     invo_name = r1bindex (argv[0], '/');
59
60     /* read user profile/context */
61     context_read();
62
63     arguments = getarguments (invo_name, argc, argv, 1);
64     argp = arguments;
65
66     vec[vecp++] = invo_name;
67     vec[vecp++] = "-whom";
68     vec[vecp++] = "-library";
69     vec[vecp++] = getcpy (m_maildir (""));
70
71     while ((cp = *argp++)) {
72         if (*cp == '-') {
73             switch (smatch (++cp, switches)) {
74                 case AMBIGSW: 
75                     ambigsw (cp, switches);
76                     done (1);
77                 case UNKWNSW: 
78                     adios (NULL, "-%s unknown", cp);
79
80                 case HELPSW: 
81                     snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
82                     print_help (buf, switches, 1);
83                     done (1);
84                 case VERSIONSW:
85                     print_version(invo_name);
86                     done (1);
87
88                 case CHKSW: 
89                 case NOCHKSW: 
90                 case SNOOPSW:
91                     vec[vecp++] = --cp;
92                     continue;
93
94                 case DRAFTSW:
95                     msg = draft;
96                     continue;
97
98                 case DFOLDSW: 
99                     if (dfolder)
100                         adios (NULL, "only one draft folder at a time!");
101                     if (!(cp = *argp++) || *cp == '-')
102                         adios (NULL, "missing argument to %s", argp[-2]);
103                     dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
104                             *cp != '@' ? TFOLDER : TSUBCWF);
105                     continue;
106                 case DMSGSW: 
107                     if (dmsg)
108                         adios (NULL, "only one draft message at a time!");
109                     if (!(dmsg = *argp++) || *dmsg == '-')
110                         adios (NULL, "missing argument to %s", argp[-2]);
111                     continue;
112                 case NDFLDSW: 
113                     dfolder = NULL;
114                     isdf = NOTOK;
115                     continue;
116
117                 case ALIASW: 
118                 case CLIESW: 
119                 case SERVSW: 
120                 case PORTSW:
121                     vec[vecp++] = --cp;
122                     if (!(cp = *argp++) || *cp == '-')
123                         adios (NULL, "missing argument to %s", argp[-2]);
124                     vec[vecp++] = cp;
125                     continue;
126             }
127         }
128         if (msg)
129             adios (NULL, "only one draft at a time!");
130         else
131             vec[vecp++] = msg = cp;
132     }
133
134     /* allow Aliasfile: profile entry */
135     if ((cp = context_find ("Aliasfile"))) {
136         char *dp = NULL;
137
138         for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
139             vec[vecp++] = "-alias";
140             vec[vecp++] = *ap;
141         }
142     }
143
144     if (msg == NULL) {
145 #ifdef  WHATNOW
146         if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
147 #endif  /* WHATNOW */
148             cp  = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
149         msg = vec[vecp++] = cp;
150     }
151     if ((cp = getenv ("mhdist"))
152             && *cp
153             && (distsw = atoi (cp))
154             && (cp = getenv ("mhaltmsg"))
155             && *cp) {
156         if (distout (msg, cp, backup) == NOTOK)
157             done (1);
158         vec[vecp++] = "-dist";
159         distsw++;
160     }
161     vec[vecp] = NULL;
162
163     closefds (3);
164
165     if (distsw) {
166         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
167             sleep (5);
168     }
169
170     switch (distsw ? child_id : OK) {
171         case NOTOK:
172             advise (NULL, "unable to fork, so checking directly...");
173         case OK:
174             execvp (postproc, vec);
175             fprintf (stderr, "unable to exec ");
176             perror (postproc);
177             _exit (-1);
178
179         default:
180             SIGNAL (SIGHUP, SIG_IGN);
181             SIGNAL (SIGINT, SIG_IGN);
182             SIGNAL (SIGQUIT, SIG_IGN);
183             SIGNAL (SIGTERM, SIG_IGN);
184
185             status = pidwait(child_id, OK);
186
187             unlink (msg);
188             if (rename (backup, msg) == NOTOK)
189                 adios (msg, "unable to rename %s to", backup);
190             done (status);
191     }
192
193     return 0;  /* dead code to satisfy the compiler */
194 }