+--- LuCI Asterisk - Voicemailbox
+-- @type module
+voicemail = luci.util.class()
+
+--- Parse a voicemail section
+-- @param zone Table containing the mailbox info
+-- @return Table with parsed information
+function voicemail.parse(z)
+ if z.number and #z.number > 0 then
+ local v = {
+ id = '%s@%s' %{ z.number, z.context or 'default' },
+ number = z.number,
+ context = z.context or 'default',
+ name = z.name or z['.name'] or 'OpenWrt',
+ zone = z.zone or 'homeloc',
+ password = z.password or '0000',
+ email = z.email or '',
+ page = z.page or '',
+ dialplans = { }
+ }
+
+ uci:foreach("asterisk", "dialplanvoice",
+ function(s)
+ if s.dialplan and #s.dialplan > 0 and
+ s.voicebox == v.number
+ then
+ v.dialplans[#v.dialplans+1] = s.dialplan
+ end
+ end)
+
+ return v
+ end
+end
+
+--- Get a list of known voicemail boxes
+-- @return Associative table of boxes and table of box numbers
+function voicemail.boxes()
+ local vboxes = { }
+ local vnames = { }
+ uci:foreach("asterisk", "voicemail",
+ function(z)
+ local v = voicemail.parse(z)
+ if v then
+ local n = '%s@%s' %{ v.number, v.context }
+ vboxes[n] = v
+ vnames[#vnames+1] = n
+ end
+ end)
+ return vboxes, vnames
+end
+
+--- Get a specific voicemailbox
+-- @param number Number of the voicemailbox
+-- @return Table containing mailbox information
+function voicemail.box(n)
+ local box
+ n = n:gsub("@.+$","")
+ uci:foreach("asterisk", "voicemail",
+ function(z)
+ if z.number == tostring(n) then
+ box = voicemail.parse(z)
+ end
+ end)
+ return box
+end
+
+--- Find all voicemailboxes within the given dialplan
+-- @param plan Dialplan name or table
+-- @return Associative table containing extensions mapped to mailbox info
+function voicemail.in_dialplan(p)
+ local plan = type(p) == "string" and p or p.name
+ local boxes = { }
+ uci:foreach("asterisk", "dialplanvoice",
+ function(s)
+ if s.extension and #s.extension > 0 and s.dialplan == plan then
+ local box = voicemail.box(s.voicebox)
+ if box then
+ boxes[s.extension] = box
+ end
+ end
+ end)
+ return boxes
+end
+
+--- Remove voicemailbox and associated extensions from config
+-- @param box Voicemailbox number or table
+-- @return Boolean indicating success
+function voicemail.remove(v)
+ local box = type(v) == "string" and v or v.number
+ local ok1 = uci:delete_all("asterisk", "voicemail", {number=box})
+ local ok2 = uci:delete_all("asterisk", "dialplanvoice", {voicebox=box})
+ return ( ok1 or ok2 ) and true or false
+end
+
+