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