2011-03-06 06:58:58 -05:00
|
|
|
-- Miscellaneous auxiliary functions.
|
|
|
|
|
|
|
|
function form_date(days)
|
|
|
|
_check_required(days, 'number')
|
2012-02-22 12:57:54 -05:00
|
|
|
return os.date('%d-%b-%Y', os.time() - days * 60 * 60 * 24)
|
2011-03-06 06:58:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function get_password(prompt)
|
|
|
|
_check_optional(prompt, 'string')
|
|
|
|
|
2012-02-11 16:55:03 -05:00
|
|
|
if prompt ~= nil then
|
2011-03-06 06:58:58 -05:00
|
|
|
io.write(prompt)
|
|
|
|
else
|
|
|
|
io.write('Enter password: ')
|
|
|
|
end
|
|
|
|
ifsys.noecho()
|
|
|
|
local p = io.read()
|
|
|
|
ifsys.echo()
|
|
|
|
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function pipe_to(command, data)
|
|
|
|
_check_required(command, 'string')
|
|
|
|
_check_required(data, 'string')
|
|
|
|
|
2012-02-22 12:57:54 -05:00
|
|
|
f = ifsys.popen(command, 'w')
|
2011-03-06 06:58:58 -05:00
|
|
|
ifsys.write(f, data)
|
|
|
|
|
|
|
|
return ifsys.pclose(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
function pipe_from(command)
|
|
|
|
_check_required(command, 'string')
|
|
|
|
|
2012-02-22 12:57:54 -05:00
|
|
|
f = ifsys.popen(command, 'r')
|
2011-03-06 06:58:58 -05:00
|
|
|
local string = ''
|
2012-02-11 16:55:03 -05:00
|
|
|
while true do
|
2011-03-06 06:58:58 -05:00
|
|
|
s = ifsys.read(f)
|
2012-02-11 16:55:03 -05:00
|
|
|
if s ~= nil then
|
2011-03-06 06:58:58 -05:00
|
|
|
string = string .. s
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return ifsys.pclose(f), string
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-07-31 11:59:09 -04:00
|
|
|
function become_daemon(interval, commands, nochdir, noclose)
|
2011-03-06 06:58:58 -05:00
|
|
|
_check_required(interval, 'number')
|
|
|
|
_check_required(commands, 'function')
|
2011-07-31 11:59:09 -04:00
|
|
|
_check_optional(nochdir, 'boolean')
|
|
|
|
_check_optional(noclose, 'boolean')
|
2011-03-06 06:58:58 -05:00
|
|
|
|
2012-02-14 16:13:15 -05:00
|
|
|
if nochdir == nil then nochdir = false end
|
|
|
|
if noclose == nil then noclose = false end
|
2011-07-31 11:59:09 -04:00
|
|
|
ifsys.daemon(nochdir, noclose)
|
Ignore errors and retry later when in daemon mode
A persistent failure that could lead to a session not being able to be
recovered, or a failure during the login process (which is not covered
by the recovery function), would normally cause the process to
terminate. This is expected when the configuration is run once, but not
desirable when running in daemon mode.
And thus just after the process has become a daemon, a session that
could not be restored, either because the connection to the server could
not be established or the login and its related negotiations failed, is
now disabled temporarily, and a recoqnection is attempted at the
beginning of the next daemon loop iteration.
There is one exception to that, and that is when there is an
authentication error, for example when a wrong username or password has
been supplied, and in this case the process terminates and a relevant
error is printed.
2012-02-29 14:24:32 -05:00
|
|
|
_daemon = true
|
2011-03-06 06:58:58 -05:00
|
|
|
repeat
|
Ignore errors and retry later when in daemon mode
A persistent failure that could lead to a session not being able to be
recovered, or a failure during the login process (which is not covered
by the recovery function), would normally cause the process to
terminate. This is expected when the configuration is run once, but not
desirable when running in daemon mode.
And thus just after the process has become a daemon, a session that
could not be restored, either because the connection to the server could
not be established or the login and its related negotiations failed, is
now disabled temporarily, and a recoqnection is attempted at the
beginning of the next daemon loop iteration.
There is one exception to that, and that is when there is an
authentication error, for example when a wrong username or password has
been supplied, and in this case the process terminates and a relevant
error is printed.
2012-02-29 14:24:32 -05:00
|
|
|
for _, account in pairs(_imap) do
|
|
|
|
if not account._account.session then
|
|
|
|
account:_login_user(account)
|
|
|
|
end
|
|
|
|
end
|
2012-02-21 11:49:38 -05:00
|
|
|
commands()
|
Ignore errors and retry later when in daemon mode
A persistent failure that could lead to a session not being able to be
recovered, or a failure during the login process (which is not covered
by the recovery function), would normally cause the process to
terminate. This is expected when the configuration is run once, but not
desirable when running in daemon mode.
And thus just after the process has become a daemon, a session that
could not be restored, either because the connection to the server could
not be established or the login and its related negotiations failed, is
now disabled temporarily, and a recoqnection is attempted at the
beginning of the next daemon loop iteration.
There is one exception to that, and that is when there is an
authentication error, for example when a wrong username or password has
been supplied, and in this case the process terminates and a relevant
error is printed.
2012-02-29 14:24:32 -05:00
|
|
|
collectgarbage()
|
2012-02-11 16:55:03 -05:00
|
|
|
until ifsys.sleep(interval) ~= 0
|
2011-03-06 06:58:58 -05:00
|
|
|
end
|
2012-02-21 14:13:55 -05:00
|
|
|
|
|
|
|
sleep = ifsys.sleep
|