PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-core/SIPSorcery.AppServer.DialPlan/DialPlanFacades/DialPlanSettings.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 187 lines | 110 code | 20 blank | 57 comment | 14 complexity | 300d70df9555f54917025e684158d38f MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // ============================================================================
  2. // FileName: DialPlanSettings.cs
  3. //
  4. // Description:
  5. // This class represents settings that are passed to a dial plan that is intended to operate
  6. // with the dial plan wizard Ruby script. The settings are configured through a custom designed
  7. // user interface.
  8. //
  9. // Author(s):
  10. // Aaron Clauson
  11. //
  12. // History:
  13. // 08 Feb 2011 Aaron Clauson Created.
  14. //
  15. // License:
  16. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  17. //
  18. // Copyright (c) 2011 Aaron Clauson (aaron@sipsorcery.com), SIP Sorcery Pty Ltd
  19. // All rights reserved.
  20. //
  21. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  22. // the following conditions are met:
  23. //
  24. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  25. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  26. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of SIP Sorcery Ltd.
  27. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  28. // prior written permission.
  29. //
  30. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  31. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  32. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  33. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  34. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. // POSSIBILITY OF SUCH DAMAGE.
  37. // ============================================================================
  38. using System;
  39. using System.Collections.Generic;
  40. using System.Linq;
  41. using System.Text;
  42. using System.Text.RegularExpressions;
  43. using SIPSorcery.SIP.App;
  44. using SIPSorcery.SIP.App.Entities;
  45. using SIPSorcery.Sys;
  46. using log4net;
  47. namespace SIPSorcery.AppServer.DialPlan
  48. {
  49. public class DialPlanSettings
  50. {
  51. private static ILog logger = AppState.logger;
  52. public List<SIPDialplanLookup> Lookups;
  53. public List<SIPDialplanRoute> Routes;
  54. public Dictionary<string, SIPDialplanProvider> Providers;
  55. public SIPDialplanOption Options;
  56. public DialPlanSettings(List<SIPDialplanLookup> lookups,
  57. List<SIPDialplanRoute> routes,
  58. Dictionary<string, SIPDialplanProvider> providers,
  59. SIPDialplanOption options)
  60. {
  61. Lookups = lookups;
  62. Routes = routes;
  63. Providers = providers;
  64. Options = options;
  65. }
  66. public Dictionary<string, string> GetSpeedDials()
  67. {
  68. return GetLookups(SIPDialPlanLookupTypes.SpeedDial);
  69. }
  70. public Dictionary<string, string> GetCNAMs()
  71. {
  72. return GetLookups(SIPDialPlanLookupTypes.CNAM);
  73. }
  74. public Dictionary<string, string> GetENUMs()
  75. {
  76. return GetLookups(SIPDialPlanLookupTypes.ENUM);
  77. }
  78. private Dictionary<string, string> GetLookups(SIPDialPlanLookupTypes lookupType)
  79. {
  80. try
  81. {
  82. if (Lookups != null)
  83. {
  84. var lookups = (from lookup in Lookups
  85. where lookup.lookuptype == (int)lookupType
  86. select lookup).ToList();
  87. if (lookups != null && lookups.Count > 0)
  88. {
  89. Dictionary<string, string> lookupsTable = new Dictionary<string, string>();
  90. foreach (SIPDialplanLookup lookup in lookups)
  91. {
  92. lookupsTable.Add(lookup.lookupkey, lookup.lookupvalue);
  93. }
  94. return lookupsTable;
  95. }
  96. }
  97. return null;
  98. }
  99. catch (Exception excp)
  100. {
  101. logger.Error("Exception GetLookups. " + excp.Message);
  102. return null;
  103. }
  104. }
  105. /// <summary>
  106. /// If set retrieves the user's UTC time zone offset for use in the dialplan.
  107. /// </summary>
  108. /// <returns>An integer that represent the offset from UTC for the user's timezone. If no timezone
  109. /// is set 0 is returned meaning any time calculations will assume UTC.</returns>
  110. public int GetTimezoneOffset()
  111. {
  112. if (Options != null && !Options.timezone.IsNullOrBlank())
  113. {
  114. foreach (TimeZoneInfo timezone in TimeZoneInfo.GetSystemTimeZones())
  115. {
  116. if (timezone.DisplayName == Options.timezone)
  117. {
  118. return (int)timezone.GetUtcOffset(DateTimeOffset.UtcNow).TotalMinutes;
  119. }
  120. }
  121. }
  122. return 0;
  123. }
  124. /// <summary>
  125. /// Gets a list of any excluded prefixes the user has specified in their dialplan options. The
  126. /// excluded prefix strings will be stored in the database as a CRLF separated list of strings.
  127. /// Spaces are used in excluded prefixes so they should not be trimmed.
  128. /// </summary>
  129. /// <returns>If available a list of ENUM servers or null if none have been specified.</returns>
  130. public List<string> GetExcludedPrefixes()
  131. {
  132. if (!Options.excludedprefixes.IsNullOrBlank())
  133. {
  134. string[] excludedPrefixes = Regex.Split(Options.excludedprefixes, @"\r\n", RegexOptions.Multiline);
  135. return excludedPrefixes.Where(x => !x.Trim().IsNullOrBlank()).ToList();
  136. }
  137. return null;
  138. }
  139. /// <summary>
  140. /// Gets a list of any ENUM servers the user has specified in their dialplan options. The ENUM
  141. /// servers will be stored in the database as a CRLF separated list of strings.
  142. /// </summary>
  143. /// <returns>If available a list of ENUM servers or null if none have been specified.</returns>
  144. public List<string> GetENUMServers()
  145. {
  146. if (!Options.enumservers.IsNullOrBlank())
  147. {
  148. string[] enumServers = Regex.Split(Options.enumservers, @"\r\n", RegexOptions.Multiline);
  149. return enumServers.Where(x => !x.Trim().IsNullOrBlank()).Select(x => x.Trim()).ToList();
  150. }
  151. return null;
  152. }
  153. /// <summary>
  154. /// Gets a list of allowed country codes that the user has specified in their dialplan options.
  155. /// </summary>
  156. /// <returns>A list of strings that represent country codes for white listed call destinations.</returns>
  157. public List<string> GetAllowedCountries()
  158. {
  159. if (!Options.allowedcountrycodes.IsNullOrBlank())
  160. {
  161. string[] allowedCodes = Regex.Split(Options.allowedcountrycodes, @"\s+", RegexOptions.Multiline);
  162. return allowedCodes.Where(x => !x.Trim().IsNullOrBlank()).Select(x => x.Trim()).ToList();
  163. }
  164. return null;
  165. }
  166. }
  167. }