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