Changeset 7889

Show
Ignore:
Timestamp:
11/07/11 03:02:33 (20 months ago)
Author:
iordan
Message:

1) Added handling for no provider accounts configured (or enabled for incoming/outgoing calls), and no
users configured (or enabled for outgoing calls).
2) Rewrote code to be more efficient by gathering all information about providers and users in one pass
at the start, and then using dictionaries with the data stored in them to build the sections.
3) Added translate() statements for default messages displayed (like "Dials all numbers").
4) Rewrote default messages for the "Incoming Calls" and "Providers Used for Outgoing Calls" sections
to be more descriptive.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/applications/luci-pbx/luasrc/model/cbi/pbx-calls.lua

    r7885 r7889  
    2626end 
    2727 
    28 modulename       = "pbx-calls" 
    29 voipmodulename   = "pbx-voip" 
    30 googlemodulename = "pbx-google" 
    31 usersmodulename  = "pbx-users" 
    32 allvalidaccounts = {} 
     28modulename        = "pbx-calls" 
     29voipmodulename    = "pbx-voip" 
     30googlemodulename  = "pbx-google" 
     31usersmodulename   = "pbx-users" 
     32allvalidaccounts  = {} 
    3333nallvalidaccounts = 0 
    34 validoutaccounts = {} 
     34validoutaccounts  = {} 
    3535nvalidoutaccounts = 0 
    36 validinaccounts  = {} 
     36validinaccounts   = {} 
    3737nvalidinaccounts  = 0 
    38 allvalidusers    = {} 
     38allvalidusers     = {} 
    3939nallvalidusers    = 0 
     40validoutusers     = {} 
     41nvalidoutusers    = 0 
    4042 
    4143 
     
    102104              end) 
    103105 
    104 ---------------------------------------------------------------------------------------------------- 
    105 -- If there are no accounts configured, or no accountsenabled for outgoing calls, display a warning. 
     106-- Add Local User accounts to all valid users, and users allowed to make outgoing calls. 
     107m.uci:foreach(usersmodulename, "local_user", 
     108              function(s1) 
     109                 -- Add user to list of all valid users. 
     110                 if s1.defaultuser ~= nil then 
     111                    allvalidusers[s1.defaultuser] = true 
     112                    nallvalidusers = nallvalidusers + 1 
     113                     
     114                    if s1.can_call == "yes" then 
     115                       validoutusers[s1.defaultuser] = true 
     116                       nvalidoutusers = nvalidoutusers + 1 
     117                    end 
     118                 end 
     119              end) 
     120 
     121 
     122---------------------------------------------------------------------------------------------------- 
     123-- If there are no accounts configured, or no accounts enabled for outgoing calls, display a warning. 
    106124-- Otherwise, display the usual help text within the section. 
    107125if     nallvalidaccounts == 0 then 
    108    text = "NOTE: There are no Google or SIP provider accounts configured." 
     126   text = translate("NOTE: There are no Google or SIP provider accounts configured.") 
    109127elseif nvalidoutaccounts == 0 then 
    110    text = "NOTE: There are no Google or SIP provider accounts enabled for outgoing calls." 
     128   text = translate("NOTE: There are no Google or SIP provider accounts enabled for outgoing calls.") 
    111129else 
    112    text = "If you have more than one account which can make outgoing calls, you \ 
     130   text = translate("If you have more than one account which can make outgoing calls, you \ 
    113131   should enter a list of phone numbers and prefixes in the following fields for each \ 
    114132   provider listed. Invalid prefixes are removed silently, and only 0-9, X, Z, N, #, *, \ 
     
    122140   possible (i.e. 1NXXNXXXXXX is better than 1). Please note all international dial codes \ 
    123141   are discarded (e.g. 00, 011, 010, 0011). Entries can be made in a space-separated \ 
    124    list, and/or one per line by hitting enter after every one." 
    125 end 
    126  
    127 s = m:section(NamedSection, "outgoing_calls", "call_routing", translate("Outgoing Calls"), 
    128               translate(text)) 
    129 s.anonymous = true 
    130  
    131 m.uci:foreach(googlemodulename, "gtalk_jabber",  
    132               function(s1) 
    133                  -- If this provider is valid AND is enabled for outgoing calls, add it to the section. 
    134                  if s1.username ~= nil and s1.name ~= nil and 
    135                     s1.make_outgoing_calls == "yes" then 
    136                     patt = s:option(DynamicList, s1.name, s1.username) 
    137                      
    138                     -- If the saved field is empty, we return a string 
    139                     -- telling the user that this account would dial any exten. 
    140                     function patt.cfgvalue(self, section) 
    141                        value = self.map:get(section, self.option) 
    142                         
    143                        if value == nil then 
    144                           return {"Dials any number"} 
    145                        else 
    146                           return value 
    147                        end 
    148                     end 
    149                      
    150                     -- Write only valid extensions into the config file. 
    151                     function patt.write(self, section, value) 
    152                        newvalue = {} 
    153                        nindex = 1 
    154                        for index, field in ipairs(value) do 
    155                           val = luci.util.trim(value[index]) 
    156                           if is_valid_extension(val) == true then 
    157                              newvalue[nindex] = val 
    158                              nindex = nindex + 1 
    159                           end 
    160                        end 
    161                        DynamicList.write(self, section, newvalue) 
    162                     end 
    163                  end 
    164               end) 
    165  
    166 m.uci:foreach(voipmodulename, "voip_provider",  
    167               function(s1) 
    168  
    169                  -- If this provider is valid AND is enabled for outgoing calls, add it to the section. 
    170                  if s1.defaultuser ~= nil and s1.host ~= nil and  
    171                     s1.name ~= nil        and s1.make_outgoing_calls == "yes" then 
    172                     patt = s:option(DynamicList, s1.name, s1.defaultuser .. "@" .. s1.host) 
    173                      
    174                     -- If the saved field is empty, we return a string 
    175                     -- telling the user that this account would dial any exten. 
    176                     function patt.cfgvalue(self, section) 
    177                        value = self.map:get(section, self.option) 
    178  
    179                        if value == nil then 
    180                           return {"Dials any number"} 
    181                        else 
    182                           return value 
    183                        end 
    184                     end 
    185  
    186                     -- Write only valid extensions into the config file. 
    187                     function patt.write(self, section, value) 
    188                        newvalue = {} 
    189                        nindex = 1 
    190                        for index, field in ipairs(value) do 
    191                           val = luci.util.trim(value[index]) 
    192                           if is_valid_extension(val) == true then 
    193                              newvalue[nindex] = val 
    194                              nindex = nindex + 1 
    195                           end 
    196                        end 
    197                        DynamicList.write(self, section, newvalue) 
    198                     end 
    199                  end 
    200               end) 
    201  
    202 ---------------------------------------------------------------------------------------------------- 
    203 s = m:section(NamedSection, "incoming_calls", "call_routing", translate("Incoming Calls"), 
    204         translate("For each provider that receives calls, here you can restrict which users to ring \ 
     142   list, and/or one per line by hitting enter after every one.") 
     143end 
     144 
     145 
     146s = m:section(NamedSection, "outgoing_calls", "call_routing", translate("Outgoing Calls"), text) 
     147s.anonymous = true 
     148 
     149for k,v in pairs(validoutaccounts) do 
     150   patterns = s:option(DynamicList, k, v) 
     151    
     152   -- If the saved field is empty, we return a string 
     153   -- telling the user that this account would dial any exten. 
     154   function patterns.cfgvalue(self, section) 
     155      value = self.map:get(section, self.option) 
     156       
     157      if value == nil then 
     158         return {translate("Dials any number")} 
     159      else 
     160         return value 
     161      end 
     162   end 
     163    
     164   -- Write only valid extensions into the config file. 
     165   function patterns.write(self, section, value) 
     166      newvalue = {} 
     167      nindex = 1 
     168      for index, field in ipairs(value) do 
     169         val = luci.util.trim(value[index]) 
     170         if is_valid_extension(val) == true then 
     171            newvalue[nindex] = val 
     172            nindex = nindex + 1 
     173         end 
     174      end 
     175      DynamicList.write(self, section, newvalue) 
     176   end 
     177end 
     178 
     179---------------------------------------------------------------------------------------------------- 
     180-- If there are no accounts configured, or no accounts enabled for incoming calls, display a warning. 
     181-- Otherwise, display the usual help text within the section. 
     182if     nallvalidaccounts == 0 then 
     183   text = translate("NOTE: There are no Google or SIP provider accounts configured.") 
     184elseif nvalidinaccounts == 0 then 
     185   text = translate("NOTE: There are no Google or SIP provider accounts enabled for incoming calls.") 
     186else 
     187   text = translate("For each provider that receives calls, here you can restrict which users to ring \ 
    205188                on incoming calls. If the list is empty, the system will indicate that all users \ 
    206189                which are enabled for incoming calls will ring. Invalid usernames will be rejected \ 
    207190                silently. Also, entering a username here overrides the user's setting to not receive \ 
    208                 incoming calls, so this way, you can make users ring only for select providers. \ 
    209                 Entries can be made in a space-separated list, and/or one per \ 
    210                 line by hitting enter after every one.")) 
    211 s.anonymous = true 
    212  
    213 m.uci:foreach(googlemodulename, "gtalk_jabber",  
    214               function(s1) 
    215                  if s1.username ~= nil and s1.register == "yes" then 
    216                     field_name=string.gsub(s1.username, "%W", "_") 
    217                     gtalkaccts = s:option(DynamicList, field_name, s1.username) 
    218                      
    219                     -- If the saved field is empty, we return a string 
    220                     -- telling the user that this account would dial any exten. 
    221                     function gtalkaccts.cfgvalue(self, section) 
    222                        value = self.map:get(section, self.option) 
    223                         
    224                        if value == nil then 
    225                           return {"Rings all users"} 
    226                        else 
    227                           return value 
    228                        end 
    229                     end 
    230  
    231                     -- Write only valid user names. 
    232                     function gtalkaccts.write(self, section, value) 
    233                        newvalue = {} 
    234                        nindex = 1 
    235                        for index, field in ipairs(value) do 
    236                           trimuser = luci.util.trim(value[index]) 
    237                           if allvalidusers[trimuser] == true then 
    238                              newvalue[nindex] = trimuser 
    239                              nindex = nindex + 1 
    240                           end 
    241                        end 
    242                        DynamicList.write(self, section, newvalue) 
    243                     end 
    244                  end 
    245               end) 
    246  
    247  
    248 m.uci:foreach(voipmodulename, "voip_provider",  
    249               function(s1) 
    250                  if s1.defaultuser ~= nil and s1.host ~= nil and s1.register == "yes" then 
    251                     field_name=string.gsub(s1.defaultuser .. "_" .. s1.host, "%W", "_") 
    252                     voipaccts = s:option(DynamicList, field_name, s1.defaultuser .. "@" .. s1.host) 
    253                      
    254                     -- If the saved field is empty, we return a string 
    255                     -- telling the user that this account would dial any exten. 
    256                     function voipaccts.cfgvalue(self, section) 
    257                        value = self.map:get(section, self.option) 
    258                         
    259                        if value == nil then 
    260                           return {"Rings all users"} 
    261                        else 
    262                           return value 
    263                        end 
    264                     end 
    265  
    266                     -- Write only valid user names. 
    267                     function voipaccts.write(self, section, value) 
    268                        newvalue = {} 
    269                        nindex = 1 
    270                        for index, field in ipairs(value) do 
    271                           trimuser = luci.util.trim(value[index]) 
    272                           if allvalidusers[trimuser] == true then 
    273                              newvalue[nindex] = trimuser 
    274                              nindex = nindex + 1 
    275                           end 
    276                        end 
    277                        DynamicList.write(self, section, newvalue) 
    278                     end 
    279                  end 
    280               end) 
    281  
    282  
    283 ---------------------------------------------------------------------------------------------------- 
     191                incoming calls. This way, you can make certain users ring only for specific providers. \ 
     192                Entries can be made in a space-separated list, and/or one per line by hitting enter after \ 
     193                every one.") 
     194end 
     195 
     196 
     197s = m:section(NamedSection, "incoming_calls", "call_routing", translate("Incoming Calls"), text) 
     198s.anonymous = true 
     199 
     200for k,v in pairs(validinaccounts) do 
     201   users = s:option(DynamicList, k, v) 
     202    
     203   -- If the saved field is empty, we return a string 
     204   -- telling the user that this account would dial any exten. 
     205   function users.cfgvalue(self, section) 
     206      value = self.map:get(section, self.option) 
     207       
     208      if value == nil then 
     209         return {translate("Rings all users enabled for incoming calls")} 
     210      else 
     211         return value 
     212      end 
     213   end 
     214    
     215   -- Write only valid user names. 
     216   function users.write(self, section, value) 
     217      newvalue = {} 
     218      nindex = 1 
     219      for index, field in ipairs(value) do 
     220         trimuser = luci.util.trim(value[index]) 
     221         if allvalidusers[trimuser] == true then 
     222            newvalue[nindex] = trimuser 
     223            nindex = nindex + 1 
     224         end 
     225      end 
     226      DynamicList.write(self, section, newvalue) 
     227   end 
     228end 
     229 
     230 
     231---------------------------------------------------------------------------------------------------- 
     232-- If there are no user accounts configured, no user accounts enabled for outgoing calls, 
     233-- display a warning. Otherwise, display the usual help text within the section. 
     234if     nallvalidusers == 0 then 
     235   text = translate("NOTE: There are no local user accounts configured.") 
     236elseif nvalidoutusers == 0 then 
     237   text = translate("NOTE: There are no local user accounts enabled for outgoing calls.") 
     238else 
     239   text = translate("If you would like, you could restrict which providers users are allowed to use for \ 
     240        outgoing calls. By default all users can use all providers. To show up in the list below the user \ 
     241        should be allowed to make outgoing calls in the \"User Accounts\" page. Enter VoIP providers in the \ 
     242        format username@some.host.name, as listed in \"Outgoing Calls\" above. It's easiest to copy and \ 
     243        paste the providers from above. Invalid entries, including providers not enabled for outgoing \ 
     244        calls, will be rejected silently. Entries can be made in a space-separated list, and/or one per \ 
     245        line by hitting enter after every one.") 
     246end 
     247 
     248 
    284249s = m:section(NamedSection, "providers_user_can_use", "call_routing", 
    285      translate("Providers Used for Outgoing Calls"), 
    286      translate("If you would like, you could restrict which providers users are allowed to use for outgoing \ 
    287         calls. By default all users can use all providers. To show up in the list below the user should \ 
    288         be allowed to make outgoing calls in the \"User Accounts\" page. Enter VoIP providers in the format \ 
    289         username@some.host.name, as listed in \"Outgoing Calls\" above. It's easiest to copy and paste \ 
    290         the providers from above. Invalid entries will be rejected silently. Entries can be made in a \ 
    291         space-separated list, and/or one per line by hitting enter after every one.")) 
    292 s.anonymous = true 
    293  
    294 m.uci:foreach(usersmodulename, "local_user", 
    295               function(s1) 
    296                  -- Add user to list of all valid users. 
    297                  if s1.defaultuser ~= nil then allvalidusers[s1.defaultuser] = true end 
    298                   
    299                  if s1.defaultuser ~= nil and s1.can_call == "yes" then 
    300                     providers = s:option(DynamicList, s1.defaultuser, s1.defaultuser) 
    301                      
    302                     -- If the saved field is empty, we return a string 
    303                     -- telling the user that this account would dial any exten. 
    304                     function providers.cfgvalue(self, section) 
    305                        value = self.map:get(section, self.option) 
    306  
    307                        if value == nil then 
    308                           return {"Uses all provider accounts"} 
    309                        else 
    310                           newvalue = {} 
    311                           -- Convert internal names to user@host values. 
    312                           for i,v in ipairs(value) do 
    313                              newvalue[i] = validoutaccounts[v] 
    314                           end 
    315                           return newvalue 
    316                        end 
    317                     end 
    318                      
    319                     -- Cook the new values prior to entering them into the config file. 
    320                     -- Also, enter them only if they are valid. 
    321                     function providers.write(self, section, value) 
    322                        cookedvalue = {} 
    323                        cindex = 1 
    324                        for index, field in ipairs(value) do 
    325                           cooked = string.gsub(luci.util.trim(value[index]), "%W", "_") 
    326                           if validoutaccounts[cooked] ~= nil then 
    327                              cookedvalue[cindex] = cooked 
    328                              cindex = cindex + 1 
    329                           end 
    330                        end 
    331                        DynamicList.write(self, section, cookedvalue) 
    332                     end 
    333                  end 
    334               end) 
     250     translate("Providers Used for Outgoing Calls"), text) 
     251s.anonymous = true 
     252 
     253for k,v in pairs(validoutusers) do 
     254   providers = s:option(DynamicList, k, k) 
     255 
     256   -- If the saved field is empty, we return a string 
     257   -- telling the user that this account would dial any exten. 
     258   function providers.cfgvalue(self, section) 
     259      value = self.map:get(section, self.option) 
     260       
     261      if value == nil then 
     262         return {translate("Uses all providers enabled for outgoing calls")} 
     263      else 
     264         newvalue = {} 
     265         -- Convert internal names to user@host values. 
     266         for i,v in ipairs(value) do 
     267            newvalue[i] = validoutaccounts[v] 
     268         end 
     269         return newvalue 
     270      end 
     271   end 
     272    
     273   -- Cook the new values prior to entering them into the config file. 
     274   -- Also, enter them only if they are valid. 
     275   function providers.write(self, section, value) 
     276      cookedvalue = {} 
     277      cindex = 1 
     278      for index, field in ipairs(value) do 
     279         cooked = string.gsub(luci.util.trim(value[index]), "%W", "_") 
     280         if validoutaccounts[cooked] ~= nil then 
     281            cookedvalue[cindex] = cooked 
     282            cindex = cindex + 1 
     283         end 
     284      end 
     285      DynamicList.write(self, section, cookedvalue) 
     286   end 
     287end 
    335288 
    336289---------------------------------------------------------------------------------------------------- 
     
    351304user = s:option(Value, "defaultuser",  translate("User Name"), 
    352305         translate("The number(s) specified above will be able to dial out with this user's providers. \ 
    353                    Invalid usernames are dropped silently, please verify that the entry was accepted.")) 
     306                   Invalid usernames, including users not enabled for outgoing calls, are dropped silently. \ 
     307                   Please verify that the entry was accepted.")) 
    354308function user.write(self, section, value) 
    355309   trimuser = luci.util.trim(value)