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