more setupts in cfg.lua
[apps/madmutt.git] / lib-lua / madmutt.cpkg
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2007 Pierre Habouzit
18  */
19
20 #include <lib-lib/lib-lib.h>
21
22 #include <sys/utsname.h>
23 #include <sys/types.h>
24 #include <pwd.h>
25
26 #include <lib-lua/lib-lua.h>
27 #include <lib-sys/unix.h>
28
29 #include "../mutt.h"
30 @import "base.cpkg"
31
32 static char *madmutt_init_shell(void)
33 {
34     struct passwd *pw = getpwuid(getuid());
35     return m_strdup(pw ? pw->pw_shell : (getenv("SHELL") ?: "/bin/sh"));
36 }
37
38 static char *madmutt_init_username(void)
39 {
40     struct passwd *pw = getpwuid(getuid());
41     return m_strdup(pw ? pw->pw_name : (getenv("USER") ?: "john_doe"));
42 }
43
44 static char *madmutt_init_homedir(void)
45 {
46     struct passwd *pw = getpwuid(getuid());
47     return m_strdup(pw ? pw->pw_dir : (getenv("HOME") ?: "/"));
48 }
49
50 static char *madmutt_init_shorthost(void)
51 {
52     struct utsname utsname;
53     const char *p;
54
55     /* some systems report the FQDN instead of just the hostname */
56     uname(&utsname);
57     p = strchrnul(utsname.nodename, '.');
58     return p_dupstr(utsname.nodename, p - utsname.nodename);
59 }
60
61 static char *madmutt_init_os(void)
62 {
63     struct utsname un;
64     return m_strdup(uname(&un) < 0 ? "Unix" : un.sysname);
65 }
66
67 static char *madmutt_init_hostname(void)
68 {
69     char buffer[STRING];
70
71     if (getdnsdomainname(buffer, sizeof(buffer)) < 0)
72         return m_strdup("@");
73
74     if (*buffer != '@') {
75         int len   = m_strlen(buffer) + m_strlen(MCore.shorthost) + 2;
76         char *res = p_new(char, len);
77         snprintf(res, len, "%s.%s", NONULL(MCore.shorthost), buffer);
78         return res;
79     }
80
81     return m_strdup(NONULL(MCore.shorthost));
82 }
83
84 #if defined(HAVE_QDBM)
85 #  define HCACHE_BACKEND  "qdbm"
86 #elif defined(HAVE_GDBM)
87 #  define HCACHE_BACKEND  "gdbm"
88 #elif defined(HAVE_DB4)
89 #  define HCACHE_BACKEND  "db4"
90 #else
91 #  define HCACHE_BACKEND  NULL
92 #endif
93
94 @package MCore {
95     /*
96      ** .pp
97      ** \fIThis is a read-only system property and specifies madmutt's
98      ** version string.\fP
99      */
100     const string_t version    = VERSION;
101     /*
102      ** .pp
103      ** \fIThis is a read-only system property and specifies madmutt's
104      ** subversion revision string.\fP
105      */
106     const string_t sysconfdir = SYSCONFDIR;
107     /*
108      ** .pp
109      ** \fIThis is a read-only system property and specifies the
110      ** directory containing the madmutt binary.\fP
111      */
112     const string_t bindir     = BINDIR;
113     /*
114      ** .pp
115      ** \fIThis is a read-only system property and specifies the
116      ** directory containing the madmutt documentation.\fP
117      */
118     const string_t docdir     = PKGDOCDIR;
119     /*
120      ** .pp
121      ** \fIThis is a read-only system property and specifies the
122      ** header chaching's database backend.\fP
123      */
124     const string_t hcache_backend = HCACHE_BACKEND;
125
126     /*
127      ** .pp
128      ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
129      ** Madmutt.
130      */
131     path_t dotlock        = m_strdup(BINDIR "/mutt_dotlock");
132     /*
133      ** .pp
134      ** This variable specifies which editor is used by Madmutt.
135      ** It defaults to the value of the \fT$$$VISUAL\fP, or \fT$$$EDITOR\fP, environment
136      ** variable, or to the string "\fTvi\fP" if neither of those are set.
137      */
138     path_t editor         = m_strdup(getenv("VISUAL") ?: getenv("EDITOR") ?: "vi");
139     /*
140      ** .pp
141      ** Command to use when spawning a subshell.  By default, the user's login
142      ** shell from \fT/etc/passwd\fP is used.
143      */
144     path_t shell          = madmutt_init_shell();
145
146     /*
147      ** .pp
148      ** This specifies the operating system name for the \fTUser-Agent:\fP header field. If
149      ** this is \fIunset\fP, it will be set to the operating system name that \fTuname(2)\fP
150      ** returns. If \fTuname(2)\fP fails, ``UNIX'' will be used.
151      ** .pp
152      ** It may, for example, look as: ``\fTMadmutt 1.5.9i (Linux)\fP''.
153      */
154     string_t operating_system = madmutt_init_os();
155
156     path_t username       = madmutt_init_username();
157     path_t homedir        = madmutt_init_homedir();
158
159     /*
160      ** .pp
161      ** Specifies the hostname to use after the ``\fT@\fP'' in local e-mail
162      ** addresses and during generation of \fTMessage-ID:\fP headers.
163      ** .pp
164      ** Please be sure to really know what you are doing when changing this variable
165      ** to configure a custom domain part of Message-IDs.
166      */
167     string_t hostname     = madmutt_init_hostname();
168     string_t shorthost    = madmutt_init_shorthost();
169
170     /*
171      ** .pp
172      ** This variable allows you to specify where Madmutt will place its
173      ** temporary files needed for displaying and composing messages.  If
174      ** this variable is not set, the environment variable \fT$$$TMPDIR\fP is
175      ** used.  If \fT$$$TMPDIR\fP is not set then "\fT/tmp\fP" is used.
176      */
177     path_t tmpdir         = m_strdup(getenv("TMPDIR") ?: "/tmp");
178     /*
179      ** .pp
180      ** A regular expression used by Madmutt to parse the GECOS field of a password
181      ** entry when expanding the alias.  By default the regular expression is set
182      ** to ``\fT^[^,]*\fP'' which will return the string up to the first ``\fT,\fP'' encountered.
183      ** If the GECOS field contains a string like "lastname, firstname" then you
184      ** should do: \fTset gecos_mask=".*"\fP.
185      ** .pp
186      ** This can be useful if you see the following behavior: you address a e-mail
187      ** to user ID stevef whose full name is Steve Franklin.  If Madmutt expands
188      ** stevef to ``Franklin'' stevef@foo.bar then you should set the gecos_mask to
189      ** a regular expression that will match the whole name so Madmutt will expand
190      ** ``Franklin'' to ``Franklin, Steve''.
191      */
192     rx_t   gecos_mask     = luaM_rxnew("^[^,]*");
193
194     /*
195      ** .pp
196      ** This variable controls whether ``quit'' and ``exit'' actually quit
197      ** from Madmutt.  If it set to \fIyes\fP, they do quit, if it is set to \fIno\fP, they
198      ** have no effect, and if it is set to \fIask-yes\fP or \fIask-no\fP, you are
199      ** prompted for confirmation when you try to quit.
200      */
201     quadopt_t quit        = M_YES;
202     /*
203      ** .pp
204      ** When this variable is \fIset\fP, Madmutt will beep when an error occurs.
205      */
206     bool      beep        = 1;
207     /*
208      ** .pp
209      ** When this variable is \fIset\fP, Madmutt will beep whenever it prints a message
210      ** notifying you of new mail.  This is independent of the setting of the
211      ** ``$$beep'' variable.
212      */
213     bool      beep_new    = 0;
214
215     /*
216      ** .pp
217      ** When \fIset\fP, Madmutt will qualify all local addresses (ones without the
218      ** @host portion) with the value of ``$$hostname''.  If \fIunset\fP, no
219      ** addresses will be qualified.
220      */
221     bool      use_domain  = 1;
222
223     /*
224      ** .pp
225      ** \fIThis is a read-only system property and, at runtime,
226      ** specifies the current working directory of the madmutt
227      ** binary.\fP
228      */
229     const string_t pwd(void) {
230         char path[_POSIX_PATH_MAX];
231         getcwd(path, sizeof(path));
232         RETURN(path);
233     };
234
235     /*
236      ** .pp
237      ** \fIThis is a read-only system property and, at runtime,
238      ** specifies the full path or URI of the folder currently
239      ** open (if any).\fP
240      */
241     const string_t folder_path(void) {
242         RETURN(CurrentFolder);
243     };
244
245     /*
246      ** .pp
247      ** \fIThis is a read-only system property and, at runtime,
248      ** specifies the actual name of the folder as far as it could
249      ** be detected.\fP
250      ** .pp
251      ** For detection, $$$folder is first taken into account
252      ** and simply stripped to form the result when a match is found. For
253      ** example, with $$$folder being \fTimap://host\fP and the folder is
254      ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
255      ** \fTINBOX/foo\fP.)
256      ** .pp
257      ** Second, if the initial portion of a name is not $$$folder,
258      ** the result will be everything after the last ``/''.
259      ** .pp
260      ** Third and last, the result will be just the name if neither
261      ** $$$folder nor a ``/'' were found in the name.
262      */
263     const string_t folder_name(void) {
264         const char *p;
265
266         if (!m_strisempty(Maildir)
267         &&  m_strstart(CurrentFolder, Maildir, &p) && *p) {
268             while (*p == '/')
269                 p++;
270             RETURN(p);
271         } else {
272             p = strchr(CurrentFolder ?: "", '/');
273             RETURN(p ? p + 1 : (CurrentFolder ?: ""));
274         }
275     };
276 };
277
278 @package MTransport {
279     /*
280      ** .pp
281      ** Specifies the program and arguments used to deliver mail sent by Madmutt.
282      ** Madmutt expects that the specified program interprets additional
283      ** arguments as recipient addresses.
284      */
285     path_t   sendmail = m_strdup(SENDMAIL " -eom -oi");
286
287     /*
288      ** .pp
289      ** \fBNote:\fP you should not enable this unless you are using Sendmail
290      ** 8.8.x or greater or in connection with the SMTP support via libESMTP.
291      ** .pp
292      ** This variable sets the request for when notification is returned.  The
293      ** string consists of a comma separated list (no spaces!) of one or more
294      ** of the following: \fInever\fP, to never request notification,
295      ** \fIfailure\fP, to request notification on transmission failure,
296      ** \fIdelay\fP, to be notified of message delays, \fIsuccess\fP, to be
297      ** notified of successful transmission.
298      ** .pp
299      ** Example: \fTset dsn_notify="failure,delay"\fP
300      */
301     /* TODO: check it's NULL, hdrs or full */
302     string_t dsn_notify = NULL;
303
304     /*
305      ** .pp
306      ** \fBNote:\fP you should not enable this unless you are using Sendmail
307      ** 8.8.x or greater or in connection with the SMTP support via libESMTP.
308      ** .pp
309      ** This variable controls how much of your message is returned in DSN
310      ** messages.  It may be set to either \fIhdrs\fP to return just the
311      ** message header, or \fIfull\fP to return the full message.
312      ** .pp
313      ** Example: \fTset dsn_return=hdrs\fP
314      */
315     /* TODO: check it's never, delay, failure, success with ',' */
316     string_t dsn_return = NULL;
317
318     /*
319      ** .pp
320      ** Specifies the number of seconds to wait for the ``$$sendmail'' process
321      ** to finish before giving up and putting delivery in the background.
322      ** .pp
323      ** Madmutt interprets the value of this variable as follows:
324      ** .dl
325      ** .dt >0 .dd number of seconds to wait for sendmail to finish before continuing
326      ** .dt 0  .dd wait forever for sendmail to finish
327      ** .dt <0 .dd always put sendmail in the background without waiting
328      ** .de
329      ** .pp
330      ** Note that if you specify a value other than 0, the output of the child
331      ** process will be put in a temporary file.  If there is some error, you
332      ** will be informed as to where to find the output.
333      */
334     int sendmail_wait = 0;
335     /*
336      ** .pp
337      ** \fBWarning:\fP do not set this variable unless you are using a version
338      ** of sendmail which supports the \fT-B8BITMIME\fP flag (such as sendmail
339      ** 8.8.x) or in connection with the SMTP support via libESMTP.
340      ** Otherwise you may not be able to send mail.
341      ** .pp
342      ** When \fIset\fP, Madmutt will either invoke ``$$sendmail'' with the \fT-B8BITMIME\fP
343      ** flag when sending 8-bit messages to enable ESMTP negotiation or tell
344      ** libESMTP to do so.
345      */
346     bool use_8bitmime = 0;
347
348     /*
349      ** .pp
350      ** When \fIset\fP, Madmutt will use ``$$envelope_from_address'' as the
351      ** \fIenvelope\fP sender if that is set, otherwise it will attempt to
352      ** derive it from the "From:" header.
353      **
354      ** .pp
355      ** \fBNote:\fP This information is passed
356      ** to sendmail command using the "-f" command line switch and
357      ** passed to the SMTP server for libESMTP (if support is compiled in).
358      */
359     bool use_envelope_from = 0;
360
361     /*
362      ** .pp
363      ** Manually sets the \fIenvelope\fP sender for outgoing messages.
364      ** This value is ignored if ``$$use_envelope_from'' is unset.
365      */
366     address_t envelope_from_address = NULL;
367 };
368
369 /* vim:set ft=c: */