1) Adding initial version of luci-pbx-voicemail package.
[project/luci.git] / applications / luci-pbx-voicemail / luasrc / model / cbi / pbx-voicemail.lua
1 --[[
2     Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
3
4     This file is part of luci-pbx-voicemail.
5
6     luci-pbx-voicemail is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     luci-pbx-voicemail is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with luci-pbx-voicemail.  If not, see <http://www.gnu.org/licenses/>.
18 ]]--
19
20 if     nixio.fs.access("/etc/init.d/asterisk")   then
21    server = "asterisk"
22 elseif nixio.fs.access("/etc/init.d/freeswitch") then
23    server = "freeswitch"
24 else
25    server = ""
26 end
27
28 modulename = "pbx-voicemail"
29 vmlogfile  = "/tmp/last_sent_voicemail.log"
30
31 m = Map (modulename, translate("Voicemail Setup"),
32          translate("Here you can configure a global voicemail for this PBX. Since this system is \
33          intended to run on embedded systems like routers, there is no local storage of voicemail - \
34          it must be sent out by email. Therefore you need to configure an outgoing mail (SMTP) server \
35          (for example the SMTP server your ISP provides, or GMail), and provide a list of addresses the \
36          voicemail will be sent to."))
37
38 -- Recreate the config, and restart services after changes are commited to the configuration.
39 function m.on_after_commit(self)
40         luci.sys.call("/etc/init.d/pbx-" .. server .. " restart 1\>/dev/null 2\>/dev/null")
41         luci.sys.call("/etc/init.d/"     .. server .. " restart 1\>/dev/null 2\>/dev/null")
42 end
43
44
45 ----------------------------------------------------------------------------------------------------
46 s = m:section(NamedSection, "global_voicemail", "voicemail", translate("Global Voicemail Setup"),
47               translate("When you enable voicemail, you will have the opportunity to specify \
48               email addresses which receive the message. You must also set up an SMTP server below."))
49 s.anonymous = true
50
51 enable = s:option(ListValue, "enabled", translate("Enabled"))
52 enable:value("yes", translate("Yes"))
53 enable:value("no",  translate("No"))
54 enable.default = "no"
55
56 timeout = s:option(Value, "global_timeout",
57                    translate("Timeout before sending callers to voicemail"))
58 timeout:depends("enabled", "yes")
59 timeout.default = 30
60
61 emails = s:option(DynamicList, "global_email_addresses",
62                   translate("Email addresses to forward to"))
63 emails:depends("enabled", "yes")
64
65 savepath = s:option(Value, "global_save_path", translate("Directory to save voicemail into"),
66                     translate("You can also retain copies of voicemail messages on the device running \
67                               your PBX. The path specified here will be created if it doesn't exist. \
68                               Beware of limited space on embedded devices like routers, and enable this \
69                               option only if you know what you are doing."))
70 savepath.optional = true
71
72 if     nixio.fs.access("/etc/pbx-voicemail/greeting.WAV")   then
73    m1 = s:option(DummyValue, "_m1")
74    m1:depends("enabled", "yes")
75    m1.default = "NOTE: Found a voicemail greeting. To check or change your voicemail greeting, dial *789 \
76                  and the system will play your current message. You have 5 seconds to hangup, otherwise a \
77                  new recording will begin and your old message will be overwritten. Hang up or press # to \
78                  stop recording. When you press #, the system will play back the new recording."
79 else
80    m1 = s:option(DummyValue, "_m1")
81    m1:depends("enabled", "yes")
82    m1.default = "WARNING: Could not find voicemail greeting. Callers will hear only a beep before \
83                  recording starts. To record a greeting, dial *789 and record a greeting after the beep. \
84                  Hang up or press # to stop recording. When # is pressed the system will play back the \
85                  recording."
86 end
87
88
89 ----------------------------------------------------------------------------------------------------
90 s = m:section(NamedSection, "voicemail_smtp", "voicemail", translate("Outgoing mail (SMTP) Server"),
91               translate("In order for this PBX to send emails containing voicemail recordings, you need to \
92               set up an SMTP server here. Your ISP usually provides an SMTP server for that purpose. \
93               You can also set up a GMail, Yahoo, or other 3rd party SMTP server."))
94 s.anonymous = true
95
96 serv = s:option(Value, "smtp_server", translate("SMTP server hostname or IP"))
97 serv.datatype = "host"
98
99 port = s:option(Value, "smtp_port", translate("SMTP port number"))
100 port.datatype = "port"
101 port.default = "25"
102
103 tls = s:option(ListValue, "smtp_tls", translate("Use TLS (secure connection)"))
104 tls:value("on",  translate("Yes"))
105 tls:value("off", translate("No"))
106 tls.default = "on"
107
108 auth = s:option(ListValue, "smtp_auth", translate("SMTP server authentication"))
109 auth:value("on",  translate("Yes"))
110 auth:value("off", translate("No"))
111 auth.default = "on"
112
113 user = s:option(Value, "smtp_user", translate("SMTP user name"))
114 user:depends("smtp_auth", "on")
115
116 pwd = s:option(Value, "smtp_password", translate("SMTP Password"),
117                translate("Your real SMTP password is not shown for your protection. It will be changed \
118                          only when you change the value in this box."))
119 pwd.password = true
120 pwd:depends("smtp_auth", "on")
121                             
122 -- We skip reading off the saved value and return nothing.
123 function pwd.cfgvalue(self, section)
124    return "Password Not Displayed"
125 end
126    
127 -- We check the entered value against the saved one, and only write if the entered value is
128 -- something other than the empty string, and it differes from the saved value.
129 function pwd.write(self, section, value)
130    local orig_pwd = m:get(section, self.option)
131    if value == "Password Not Displayed" then value = "" end
132    if value and #value > 0 and orig_pwd ~= value then
133       Value.write(self, section, value)
134    end
135 end
136
137 ----------------------------------------------------------------------------------------------------
138 s = m:section(NamedSection, "voicemail_log", "voicemail",
139               translate("Last Sent Voicemail Log"))
140 s.anonymous = true
141
142 s:option (DummyValue, "vmlog")
143
144 sts = s:option(DummyValue, "_sts") 
145 sts.template = "cbi/tvalue"
146 sts.rows = 5
147
148 function sts.cfgvalue(self, section)
149    log = nixio.fs.readfile(vmlogfile)
150    if log == nil or log == "" then
151       log = "No errors or messages reported."
152    end
153    return log
154 end
155
156 return m