phone_sip.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local ast = require("luci.asterisk")
  4. local function find_outgoing_contexts(uci)
  5. local c = { }
  6. local h = { }
  7. uci:foreach("asterisk", "dialplan",
  8. function(s)
  9. if not h[s['.name']] then
  10. c[#c+1] = { s['.name'], "Dialplan: %s" % s['.name'] }
  11. h[s['.name']] = true
  12. end
  13. end)
  14. return c
  15. end
  16. local function find_incoming_contexts(uci)
  17. local c = { }
  18. local h = { }
  19. uci:foreach("asterisk", "sip",
  20. function(s)
  21. if s.context and not h[s.context] and
  22. uci:get_bool("asterisk", s['.name'], "provider")
  23. then
  24. c[#c+1] = { s.context, "Incoming: %s" % s['.name'] or s.context }
  25. h[s.context] = true
  26. end
  27. end)
  28. return c
  29. end
  30. --
  31. -- SIP phone info
  32. --
  33. if arg[2] == "info" then
  34. form = SimpleForm("asterisk", "SIP Phone Information")
  35. form.reset = false
  36. form.submit = "Back to overview"
  37. local info, keys = ast.sip.peer(arg[1])
  38. local data = { }
  39. for _, key in ipairs(keys) do
  40. data[#data+1] = {
  41. key = key,
  42. val = type(info[key]) == "boolean"
  43. and ( info[key] and "yes" or "no" )
  44. or ( info[key] == nil or #info[key] == 0 )
  45. and "(none)"
  46. or tostring(info[key])
  47. }
  48. end
  49. itbl = form:section(Table, data, "SIP Phone %q" % arg[1])
  50. itbl:option(DummyValue, "key", "Key")
  51. itbl:option(DummyValue, "val", "Value")
  52. function itbl.parse(...)
  53. luci.http.redirect(
  54. luci.dispatcher.build_url("admin", "asterisk", "phones")
  55. )
  56. end
  57. return form
  58. --
  59. -- SIP phone configuration
  60. --
  61. elseif arg[1] then
  62. cbimap = Map("asterisk", "Edit SIP Client")
  63. peer = cbimap:section(NamedSection, arg[1])
  64. peer.hidden = {
  65. type = "friend",
  66. qualify = "yes",
  67. host = "dynamic",
  68. nat = "no",
  69. canreinvite = "no"
  70. }
  71. back = peer:option(DummyValue, "_overview", "Back to phone overview")
  72. back.value = ""
  73. back.titleref = luci.dispatcher.build_url("admin", "asterisk", "phones")
  74. active = peer:option(Flag, "disable", "Account enabled")
  75. active.enabled = "yes"
  76. active.disabled = "no"
  77. function active.cfgvalue(...)
  78. return AbstractValue.cfgvalue(...) or "yes"
  79. end
  80. exten = peer:option(Value, "extension", "Extension Number")
  81. cbimap.uci:foreach("asterisk", "dialplanexten",
  82. function(s)
  83. exten:value(
  84. s.extension,
  85. "%s (via %s/%s)" %{ s.extension, s.type:upper(), s.target }
  86. )
  87. end)
  88. display = peer:option(Value, "callerid", "Display Name")
  89. username = peer:option(Value, "username", "Authorization ID")
  90. password = peer:option(Value, "secret", "Authorization Password")
  91. password.password = true
  92. regtimeout = peer:option(Value, "registertimeout", "Registration Time Value")
  93. function regtimeout.cfgvalue(...)
  94. return AbstractValue.cfgvalue(...) or "60"
  95. end
  96. sipport = peer:option(Value, "port", "SIP Port")
  97. function sipport.cfgvalue(...)
  98. return AbstractValue.cfgvalue(...) or "5060"
  99. end
  100. linekey = peer:option(ListValue, "_linekey", "Linekey Mode (broken)")
  101. linekey:value("", "Off")
  102. linekey:value("trunk", "Trunk Appearance")
  103. linekey:value("call", "Call Appearance")
  104. dialplan = peer:option(ListValue, "context", "Assign Dialplan")
  105. dialplan.titleref = luci.dispatcher.build_url("admin", "asterisk", "dialplans")
  106. for _, v in ipairs(find_outgoing_contexts(cbimap.uci)) do
  107. dialplan:value(unpack(v))
  108. end
  109. incoming = peer:option(StaticList, "incoming", "Receive incoming calls from")
  110. for _, v in ipairs(find_incoming_contexts(cbimap.uci)) do
  111. incoming:value(unpack(v))
  112. end
  113. --function incoming.cfgvalue(...)
  114. --error(table.concat(MultiValue.cfgvalue(...),"."))
  115. --end
  116. return cbimap
  117. end