| 1 | require("luci.tools.webadmin") |
|---|
| 2 | |
|---|
| 3 | local util = require "luci.util" |
|---|
| 4 | local pdb = loadfile((os.getenv("LUCI_SYSROOT") or "") .. "/etc/3gsp.db.lua")() |
|---|
| 5 | local c3166 = loadfile((os.getenv("LUCI_SYSROOT") or "") .. "/etc/3166en.db.lua")() |
|---|
| 6 | local uci1 = luci.model.uci.cursor() |
|---|
| 7 | |
|---|
| 8 | -- Try to guess the user's country |
|---|
| 9 | if not uci1:get("umtsd", "umtsd", "_country") then |
|---|
| 10 | local aclang = luci.http.getenv("HTTP_ACCEPT_LANGUAGE") or "" |
|---|
| 11 | aclang = aclang:match("%s*(%w%w).*") or "" |
|---|
| 12 | aclang = pdb[aclang] and aclang or "es" |
|---|
| 13 | uci1:set("umtsd", "umtsd", "_country", aclang) |
|---|
| 14 | uci1:save("umtsd") |
|---|
| 15 | uci1:commit("umtsd") |
|---|
| 16 | end |
|---|
| 17 | |
|---|
| 18 | local uci = luci.model.uci.cursor_state() |
|---|
| 19 | local attached = uci:get("umtsd", "umtsd", "attached") |
|---|
| 20 | local apn = uci:get("umtsd", "umtsd", "apn") or "" |
|---|
| 21 | local pin = uci:get("umtsd", "umtsd", "pin") or "" |
|---|
| 22 | local needpin = uci:get("umtsd", "umtsd", "needpin") or "" |
|---|
| 23 | local wrongpin = uci:get("umtsd", "umtsd", "wrongpin") or "" |
|---|
| 24 | local pinpuk = uci:get("umtsd", "umtsd", "pinpuk") or "" |
|---|
| 25 | local authfail = uci:get("umtsd", "umtsd", "authfail") or "" |
|---|
| 26 | local online = uci:get("umtsd", "umtsdstate", "online") or "0" |
|---|
| 27 | local state = uci:get("umtsd", "umtsdstate", "state") or "0" |
|---|
| 28 | local wan = uci:get("network", "wan", "ipaddr") |
|---|
| 29 | |
|---|
| 30 | local pinchanged |
|---|
| 31 | local pukchanged |
|---|
| 32 | local authchanged |
|---|
| 33 | local n |
|---|
| 34 | local m = Map("umtsd", |
|---|
| 35 | translate("umts_title", "UMTS Settings")) |
|---|
| 36 | m.events = {"UMTS"} |
|---|
| 37 | function m.commit_handler(self, form) |
|---|
| 38 | local u = luci.model.uci.cursor() |
|---|
| 39 | if not(form) then |
|---|
| 40 | return |
|---|
| 41 | end |
|---|
| 42 | if pinchanged then |
|---|
| 43 | u:set("umtsd", "umtsd", "pinfail", "0") |
|---|
| 44 | u:set("umtsd", "umtsd", "wrongpin", "0") |
|---|
| 45 | u:set("umtsd", "umtsd", "needpin", "0") |
|---|
| 46 | local count = tonumber(u:get("umtsd", "umtsd", "failcount") or "0") |
|---|
| 47 | u:set("umtsd", "umtsd", "failcount", count + 1) |
|---|
| 48 | end |
|---|
| 49 | if pukchanged then |
|---|
| 50 | u:set("umtsd", "umtsd", "pinpuk", "0") |
|---|
| 51 | end |
|---|
| 52 | u:commit("umtsd") |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | s = m:section(NamedSection, "umtsd") |
|---|
| 56 | |
|---|
| 57 | if apn and #apn == 0 then |
|---|
| 58 | m.description = translate("umts_cred", "Please enter your UMTS/3G credentials") |
|---|
| 59 | local country = s:option(ListValue, "_country", translate("country")) |
|---|
| 60 | local provider = s:option(ListValue, "_provider", translate("umts_provider")) |
|---|
| 61 | country:value("_custom", translate("umts_custom")) |
|---|
| 62 | |
|---|
| 63 | local providers = {} |
|---|
| 64 | |
|---|
| 65 | for i, info in ipairs(c3166) do |
|---|
| 66 | local cc, cname, regdom = unpack(info) |
|---|
| 67 | cc = string.lower(cc) |
|---|
| 68 | |
|---|
| 69 | local plist = pdb[cc] |
|---|
| 70 | if plist then |
|---|
| 71 | country:value(cc, cname) |
|---|
| 72 | |
|---|
| 73 | for k, p in util.kspairs(plist) do |
|---|
| 74 | local pk = cc .. "_" .. k |
|---|
| 75 | provider:value(pk, p.name, {_country=cc}) |
|---|
| 76 | providers[pk] = p |
|---|
| 77 | end |
|---|
| 78 | end |
|---|
| 79 | end |
|---|
| 80 | |
|---|
| 81 | for k in pairs(pdb) do |
|---|
| 82 | provider:depends({_country=k}) |
|---|
| 83 | end |
|---|
| 84 | |
|---|
| 85 | local apn = s:option(Value, "apn", translate("umts_apn", "APN")) |
|---|
| 86 | apn:depends({_country="_custom"}) |
|---|
| 87 | |
|---|
| 88 | function apn.formvalue(self, section) |
|---|
| 89 | local uservalue = Value.formvalue(self, section) |
|---|
| 90 | |
|---|
| 91 | if not uservalue or #uservalue == 0 then |
|---|
| 92 | local p = providers[provider:formvalue(section)] |
|---|
| 93 | if p then |
|---|
| 94 | if p.cdma then |
|---|
| 95 | uservalue = "cdma" |
|---|
| 96 | else |
|---|
| 97 | uservalue = p and p.gsm and p.gsm[1] |
|---|
| 98 | end |
|---|
| 99 | end |
|---|
| 100 | end |
|---|
| 101 | |
|---|
| 102 | return uservalue or "" |
|---|
| 103 | end |
|---|
| 104 | |
|---|
| 105 | local pin = s:option(Value, "pin", translate("umts_pin", "PIN")) |
|---|
| 106 | pin.override_scheme = true |
|---|
| 107 | pin.rmempty = true |
|---|
| 108 | pin.password = true |
|---|
| 109 | |
|---|
| 110 | local user = s:option(Value, "user", translate("umts_user", "Username")) |
|---|
| 111 | user:depends({_country="_custom"}) |
|---|
| 112 | |
|---|
| 113 | function user.formvalue(self, section) |
|---|
| 114 | local uservalue = Value.formvalue(self, section) |
|---|
| 115 | |
|---|
| 116 | if not uservalue or #uservalue == 0 then |
|---|
| 117 | local p = providers[provider:formvalue(section)] |
|---|
| 118 | if p then |
|---|
| 119 | uservalue = p and p.username |
|---|
| 120 | end |
|---|
| 121 | end |
|---|
| 122 | |
|---|
| 123 | return uservalue or "" |
|---|
| 124 | end |
|---|
| 125 | |
|---|
| 126 | local pass = s:option(Value, "pass", translate("umts_pass", "Password")) |
|---|
| 127 | pass:depends({_country="_custom"}) |
|---|
| 128 | |
|---|
| 129 | function pass.formvalue(self, section) |
|---|
| 130 | local uservalue = Value.formvalue(self, section) |
|---|
| 131 | |
|---|
| 132 | if not uservalue or #uservalue == 0 then |
|---|
| 133 | local p = providers[provider:formvalue(section)] |
|---|
| 134 | if p then |
|---|
| 135 | uservalue = p and p.password |
|---|
| 136 | end |
|---|
| 137 | end |
|---|
| 138 | |
|---|
| 139 | return uservalue or "" |
|---|
| 140 | end |
|---|
| 141 | |
|---|
| 142 | local dns = s:option(Value, "dns", translate("wan_dns", "DNS")) |
|---|
| 143 | dns:depends({_country="_custom"}) |
|---|
| 144 | |
|---|
| 145 | function dns.formvalue(self, section) |
|---|
| 146 | local uservalue = Value.formvalue(self, section) |
|---|
| 147 | |
|---|
| 148 | if not uservalue or #uservalue == 0 then |
|---|
| 149 | local p = providers[provider:formvalue(section)] |
|---|
| 150 | if p then |
|---|
| 151 | uservalue = p and p.dns |
|---|
| 152 | end |
|---|
| 153 | end |
|---|
| 154 | |
|---|
| 155 | return uservalue or "" |
|---|
| 156 | end |
|---|
| 157 | n = Map("fon", translate("umts_adv_title", "Advanced Settings")) |
|---|
| 158 | s = n:section(NamedSection, "advanced") |
|---|
| 159 | |
|---|
| 160 | local override = s:option(ListValue, "umts_dns_override", translate("umts_dns_override", "Use default DNS")) |
|---|
| 161 | override.override_values = true |
|---|
| 162 | override:value("0", translate("disable", "disable")) |
|---|
| 163 | override:value("1", translate("enable", "enable")) |
|---|
| 164 | |
|---|
| 165 | local net = s:option(ListValue, "umts_net", translate("umts_net", "Network")) |
|---|
| 166 | net:value("0", translate("umts_gprs", "GPRS")) |
|---|
| 167 | net:value("1", translate("umts_umts", "UMTS")) |
|---|
| 168 | net:value("3", translate("umts_auto", "UMTS/GPRS")) |
|---|
| 169 | net.default = "3" |
|---|
| 170 | |
|---|
| 171 | local spot = s:option(ListValue, "umts", translate("umts_hotspot", "Activate Hotspot"), translate("umts_cost_warning", "This may produce extra cost")) |
|---|
| 172 | spot.override_values = true |
|---|
| 173 | spot.events = {"RestartChilli"} |
|---|
| 174 | spot:value("1", translate("disable", "disable")) |
|---|
| 175 | spot:value("0", translate("enable", "enable")) |
|---|
| 176 | |
|---|
| 177 | elseif needpin == "1" then |
|---|
| 178 | local pin = s:option(Value, "pin", translate("umts_pin", "PIN")) |
|---|
| 179 | m.description = translate("umts_simpin", "The SIM requires a PIN number").."<br>" |
|---|
| 180 | pin.override_scheme = true |
|---|
| 181 | pin.rmempty = true |
|---|
| 182 | pin.write = function(self, ...) |
|---|
| 183 | pinchanged = true |
|---|
| 184 | return Value.write(self, ...) |
|---|
| 185 | end |
|---|
| 186 | elseif wrongpin == "1" then |
|---|
| 187 | local failcount = tonumber(uci:get("umtsd", "umtsd", "failcount") or "0") |
|---|
| 188 | if failcount > 3 then |
|---|
| 189 | failcount = 3 |
|---|
| 190 | end |
|---|
| 191 | m.description = translate("umts_pinfail", "The PIN number was not accepted.").. |
|---|
| 192 | "<br><h3>".. |
|---|
| 193 | translate("umts_attempts", "Remaining attempts").. |
|---|
| 194 | " : :"..(3 - failcount).."</h3><br>" |
|---|
| 195 | local pin = s:option(Value, "pin", "PIN") |
|---|
| 196 | pin.override_scheme = true |
|---|
| 197 | pin.rmempty = true |
|---|
| 198 | pin.write = function(self, ...) |
|---|
| 199 | pinchanged = true |
|---|
| 200 | return Value.write(self, ...) |
|---|
| 201 | end |
|---|
| 202 | elseif authfail == "1" and tonumber(online) < 1 then |
|---|
| 203 | m.description = translate("umts_authfail", "The username/password you entered is not accepted by the cellular network.") |
|---|
| 204 | local apn = s:option(Value, "apn", translate("umts_apn", "APN")) |
|---|
| 205 | local user =s:option(Value, "user", translate("umts_user", "Username")) |
|---|
| 206 | local pass = s:option(Value, "pass", translate("umts_pass", "Password")) |
|---|
| 207 | pass.write = function(self, ...) |
|---|
| 208 | authchanged = true |
|---|
| 209 | return Value.write(self, ...) |
|---|
| 210 | end |
|---|
| 211 | user.write = function(self, ...) |
|---|
| 212 | authchanged = true |
|---|
| 213 | return Value.write(self, ...) |
|---|
| 214 | end |
|---|
| 215 | apn.write = function(self, ...) |
|---|
| 216 | authchanged = true |
|---|
| 217 | return Value.write(self, ...) |
|---|
| 218 | end |
|---|
| 219 | elseif pinpuk == "1" then |
|---|
| 220 | s:append(Template("fon_umtsd/pinpuk")) |
|---|
| 221 | local puk = s:option(Value, "puk", translate("umts_puk", "PUK")) |
|---|
| 222 | puk.write = function(self, ...) |
|---|
| 223 | pukchanged = true |
|---|
| 224 | return Value.write(self, ...) |
|---|
| 225 | end |
|---|
| 226 | local pin = s:option(Value, "pin", translate("umts_pin", "PIN")) |
|---|
| 227 | pin.override_scheme = true |
|---|
| 228 | pin.rmempty = true |
|---|
| 229 | |
|---|
| 230 | elseif attached and state == "ONLINE" and wan then |
|---|
| 231 | local x = Template("fon_umtsd/main") |
|---|
| 232 | m.description = translate("umts_status", "Status Information") |
|---|
| 233 | m.pageaction = false |
|---|
| 234 | x.model = uci:get("umtsd", "umtsdstate", "model") or "N/A" |
|---|
| 235 | x.vendor = uci:get("umtsd", "umtsdstate", "vendor") or "N/A" |
|---|
| 236 | x.provider = uci:get("umtsd", "umtsdstate", "provider") or "N/A" |
|---|
| 237 | x.signal = uci:get("umtsd", "umtsdstate", "signal") or "N/A" |
|---|
| 238 | x.vidpid = uci:get("umtsd", "umtsdstate", "vidpid") or "N/A" |
|---|
| 239 | x.driver = uci:get("umtsd", "umtsd", "device") |
|---|
| 240 | x.online = online |
|---|
| 241 | x.rx = uci:get("umtsd", "umtsdstate", "rx") or "N/A" |
|---|
| 242 | x.tx = uci:get("umtsd", "umtsdstate", "tx") or "N/A" |
|---|
| 243 | x.state = uci:get("umtsd", "umtsdstate", "state") or "N/A" |
|---|
| 244 | s:append(x) |
|---|
| 245 | s:append(Template("fon_umtsd/menu")) |
|---|
| 246 | elseif attached then |
|---|
| 247 | m.pageaction = false |
|---|
| 248 | s:append(Template("fon_umtsd/dialin")) |
|---|
| 249 | s:append(Template("fon_umtsd/menu")) |
|---|
| 250 | else |
|---|
| 251 | m.pageaction = false |
|---|
| 252 | s:append(Template("fon_umtsd/none")) |
|---|
| 253 | s:append(Template("fon_umtsd/menu")) |
|---|
| 254 | end |
|---|
| 255 | s:append(Template("fon/page_reload")) |
|---|
| 256 | if n then |
|---|
| 257 | return m, n |
|---|
| 258 | end |
|---|
| 259 | return m |
|---|