1-- Miscellaneous auxiliary functions.
2
3function form_date(days)
4    _check_required(days, 'number')
5    return os.date('%d-%b-%Y', os.time() - days * 60 * 60 * 24)
6end
7
8
9function get_password(prompt)
10    _check_optional(prompt, 'string')
11
12    if prompt ~= nil then
13        io.write(prompt)
14    else
15        io.write('Enter password: ')
16    end
17    io.flush()
18    ifsys.noecho()
19    local p = io.read()
20    ifsys.echo()
21
22    return p
23end
24
25
26function pipe_to(command, data)
27    _check_required(command, 'string')
28    _check_required(data, 'string')
29
30    f = ifsys.popen(command, 'w')
31    ifsys.write(f, data)
32
33    return ifsys.pclose(f)
34end
35
36function pipe_from(command)
37    _check_required(command, 'string')
38
39    f = ifsys.popen(command, 'r')
40    local string = ''
41    while true do
42        s = ifsys.read(f)
43        if s ~= nil then
44            string = string .. s
45        else
46            break
47        end
48    end
49
50    return ifsys.pclose(f), string
51end
52
53
54function become_daemon(interval, commands, nochdir, noclose)
55    _check_required(interval, 'number')
56    _check_required(commands, 'function')
57    _check_optional(nochdir, 'boolean')
58    _check_optional(noclose, 'boolean')
59
60    if nochdir == nil then nochdir = false end
61    if noclose == nil then noclose = false end
62    ifsys.daemon(nochdir, noclose)
63    _daemon = true
64    repeat
65        for _, account in pairs(_imap) do
66            if not account._account.session then
67                account:_login_user(account)
68            end
69        end
70        commands()
71        collectgarbage()
72    until ifsys.sleep(interval) ~= 0
73end
74
75sleep = ifsys.sleep
76