9ff2ed97fe6aaa73d0a3fb992cc5b7cc6dcf2e41
[project/luci.git] / applications / luci-app-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 your ISP's, Google's, or Yahoo's SMTP server), and provide a list of \
36          addresses that receive recorded voicemail."))
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 that receive recorded voicemail. You must also set up an SMTP server below."))
49 s.anonymous = true
50
51 enable = s:option(ListValue, "enabled", translate("Enable Voicemail"))
52 enable:value("yes", translate("Yes"))
53 enable:value("no",  translate("No"))
54 enable.default = "no"
55
56 emails = s:option(DynamicList, "global_email_addresses",
57                   translate("Email Addresses that Receive Voicemail"))
58 emails:depends("enabled", "yes")
59
60 savepath = s:option(Value, "global_save_path", translate("Local Storage Directory"),
61                     translate("You can also retain copies of voicemail messages on the device running \
62                               your PBX. The path specified here will be created if it doesn't exist. \
63                               Beware of limited space on embedded devices like routers, and enable this \
64                               option only if you know what you are doing."))
65 savepath.optional = true
66
67 if     nixio.fs.access("/etc/pbx-voicemail/recordings/greeting.gsm")   then
68    m1 = s:option(DummyValue, "_m1")
69    m1:depends("enabled", "yes")
70    m1.default = "NOTE: Found a voicemail greeting. To check or change your voicemail greeting, dial *789 \
71                  and the system will play back your current greeting. After that, a long beep will sound and \
72                  you can press * in order to record a new message. Hang up to avoid recording a message. \
73                  If you press *, a second long beep will sound, and you can record a new greeting. \
74                  Hang up or press # to stop recording. When # is pressed the system will play back the \
75                  new greeting."
76 else
77    m1 = s:option(DummyValue, "_m1")
78    m1:depends("enabled", "yes")
79    m1.default = "WARNING: Could not find voicemail greeting. Callers will hear only a beep before \
80                  recording starts. To record a greeting, dial *789, and press * after the long beep. \
81                  If you press *, a second long beep will sound, and you can record a new greeting. \
82                  Hang up or press # to stop recording. When # is pressed the system will play back the \
83                  new greeting."
84 end
85
86
87 ----------------------------------------------------------------------------------------------------
88 s = m:section(NamedSection, "voicemail_smtp", "voicemail", translate("Outgoing mail (SMTP) Server"),
89               translate("In order for this PBX to send emails containing voicemail recordings, you need to \
90               set up an SMTP server here. Your ISP usually provides an SMTP server for that purpose. \
91               You can also set up a third party SMTP server such as the one provided by Google or Yahoo."))
92 s.anonymous = true
93
94 serv = s:option(Value, "smtp_server", translate("SMTP Server Hostname or IP Address"))
95 serv.datatype = "host"
96
97 port = s:option(Value, "smtp_port", translate("SMTP Port Number"))
98 port.datatype = "port"
99 port.default = "25"
100
101 tls = s:option(ListValue, "smtp_tls", translate("Secure Connection Using TLS"))
102 tls:value("on",  translate("Yes"))
103 tls:value("off", translate("No"))
104 tls.default = "on"
105
106 auth = s:option(ListValue, "smtp_auth", translate("SMTP Server Authentication"))
107 auth:value("on",  translate("Yes"))
108 auth:value("off", translate("No"))
109 auth.default = "off"
110
111 user = s:option(Value, "smtp_user", translate("SMTP User Name"))
112 user:depends("smtp_auth", "on")
113
114 pwd = s:option(Value, "smtp_password", translate("SMTP Password"),
115                translate("Your real SMTP password is not shown for your protection. It will be changed \
116                          only when you change the value in this box."))
117 pwd.password = true
118 pwd:depends("smtp_auth", "on")
119                             
120 -- We skip reading off the saved value and return nothing.
121 function pwd.cfgvalue(self, section)
122    return "Password Not Displayed"
123 end
124    
125 -- We check the entered value against the saved one, and only write if the entered value is
126 -- something other than the empty string, and it differes from the saved value.
127 function pwd.write(self, section, value)
128    local orig_pwd = m:get(section, self.option)
129    if value == "Password Not Displayed" then value = "" end
130    if value and #value > 0 and orig_pwd ~= value then
131       Value.write(self, section, value)
132    end
133 end
134
135 ----------------------------------------------------------------------------------------------------
136 s = m:section(NamedSection, "voicemail_log", "voicemail", translate("Last Sent Voicemail Log"))
137 s.anonymous = true
138
139 s:option (DummyValue, "vmlog")
140
141 sts = s:option(DummyValue, "_sts") 
142 sts.template = "cbi/tvalue"
143 sts.rows = 5
144
145 function sts.cfgvalue(self, section)
146    log = nixio.fs.readfile(vmlogfile)
147    if log == nil or log == "" then
148       log = "No errors or messages reported."
149    end
150    return log
151 end
152
153 return m