//清空Dialplan設定檔,重建預設的設定(但各個dialplan需要自己加) public static void ResetDialplan() { string path = Path.Combine(new string[] { ConfigPath, "dialplan" }); string file = Path.Combine(new string[] { path, DialplanFile }); Utils.DeleteAll(path); File.Delete(file); Dictionary <string, string> attr_map = new Dictionary <string, string>(); XmlDocument doc = new XmlDocument(); XmlNode parent = doc.CreateNode(XmlNodeType.Element, "include", null); XmlNode child = null; doc.AppendChild(parent); attr_map.Clear(); attr_map["name"] = Dialplan_Ctx; child = XMLUtils.CreateElementNode(doc, "context", attr_map); parent.AppendChild(child); //切換到 <context> parent = child; attr_map.Clear(); attr_map["name"] = "unloop"; child = XMLUtils.CreateElementNode(doc, "extension", attr_map); parent.AppendChild(child); //切換到<extension> parent = child; attr_map.Clear(); attr_map["field"] = "${unroll_loops}"; attr_map["expression"] = "^true$"; child = XMLUtils.CreateElementNode(doc, "condition", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["field"] = "${sip_looped_call}"; attr_map["expression"] = "^true$"; child = XMLUtils.CreateElementNode(doc, "condition", attr_map); parent.AppendChild(child); //切換到<condition> parent = child; attr_map.Clear(); attr_map["application"] = "deflect"; attr_map["data"] = "${destination_number}"; child = XMLUtils.CreateElementNode(doc, "action", attr_map); parent.AppendChild(child); //切換回 <context> parent = parent.ParentNode.ParentNode; attr_map.Clear(); attr_map["cmd"] = "include"; attr_map["data"] = string.Format("{0}/*.xml", Dialplan_Ctx); child = XMLUtils.CreateElementNode(doc, "X-PRE-PROCESS", attr_map); parent.AppendChild(child); XMLUtils.SaveXML(doc, file); Directory.CreateDirectory(path + "\\" + Dialplan_Ctx); }
//增加一個SIP User (注意User ID不能重複) public static void AddSipUser(string id, string pwd) { string file = Path.Combine(new string[] { ConfigPath, "directory", UserFolder, $"{id}.xml" }); if (File.Exists(file) == true) { File.Delete(file); } Dictionary <string, string> attr_map = new Dictionary <string, string>(); XmlDocument doc = new XmlDocument(); XmlNode child = null; XmlNode parent = doc.CreateNode(XmlNodeType.Element, "include", null); doc.AppendChild(parent); attr_map["id"] = id; child = XMLUtils.CreateElementNode(doc, "user", attr_map); parent.AppendChild(child); //切換到<user> parent = child; child = doc.CreateNode(XmlNodeType.Element, "params", null); parent.AppendChild(child); //切換到<params> parent = child; attr_map.Clear(); attr_map["name"] = "password"; attr_map["value"] = pwd; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "vm-password"; attr_map["value"] = pwd; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); //回到<user> parent = parent.ParentNode; child = doc.CreateNode(XmlNodeType.Element, "variables", null); parent.AppendChild(child); //切換到 <variables> parent = child; attr_map.Clear(); attr_map["name"] = "toll_allow"; attr_map["value"] = "sip-call"; child = XMLUtils.CreateElementNode(doc, "variable", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "user_context"; attr_map["value"] = Dialplan_Ctx; child = XMLUtils.CreateElementNode(doc, "variable", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "effective_caller_id_name"; attr_map["value"] = string.Format("Extension {0}", id); child = XMLUtils.CreateElementNode(doc, "variable", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "effective_caller_id_number"; attr_map["value"] = id; child = XMLUtils.CreateElementNode(doc, "variable", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "callgroup"; attr_map["value"] = CallGroup; child = XMLUtils.CreateElementNode(doc, "variable", attr_map); parent.AppendChild(child); XMLUtils.SaveXML(doc, file); }
//清空Sip User設定 public static void ResetSipUser(bool clear_all = true) { string path = Path.Combine(new string[] { ConfigPath, "directory" }); string file = Path.Combine(new string[] { path, UserCfgFile }); if (clear_all) { Utils.DeleteAll(path); } else { File.Delete(file); } //以下建立基礎profile of sip user....(指定要include哪個目錄的account) Dictionary <string, string> attr_map = new Dictionary <string, string>(); XmlDocument doc = new XmlDocument(); XmlNode parent = doc.CreateNode(XmlNodeType.Element, "include", null); doc.AppendChild(parent); attr_map.Clear(); attr_map["name"] = "$${domain}"; XmlNode child = XMLUtils.CreateElementNode(doc, "domain", attr_map); parent.AppendChild(child); //現在parent變成 <domain> parent = child; child = doc.CreateNode(XmlNodeType.Element, "params", null); parent.AppendChild(child); //現在parent變成 <params> parent = child; attr_map.Clear(); attr_map["name"] = "dial-string"; attr_map["value"] = "{^^:sip_invite_domain=${dialed_domain}:presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(*/${dialed_user}@${dialed_domain})},${verto_contact(${dialed_user}@${dialed_domain})}"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "jsonrpc-allowed-methods"; attr_map["value"] = "verto"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); //回上一層 <domain> parent = parent.ParentNode; child = doc.CreateNode(XmlNodeType.Element, "variables", null); parent.AppendChild(child); //現在parent變成 <variables> parent = child; attr_map.Clear(); attr_map["name"] = "record_stereo"; attr_map["value"] = "false"; child = XMLUtils.CreateElementNode(doc, "variable", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "transfer_fallback_extension"; attr_map["value"] = "operator"; child = XMLUtils.CreateElementNode(doc, "variable", attr_map); parent.AppendChild(child); //回上一層 <domain> parent = parent.ParentNode; child = doc.CreateNode(XmlNodeType.Element, "groups", null); parent.AppendChild(child); //現在parent變成 <groups> parent = child; attr_map["name"] = "sip"; child = XMLUtils.CreateElementNode(doc, "group", attr_map); parent.AppendChild(child); //現在parent變成 <group> parent = child; child = doc.CreateNode(XmlNodeType.Element, "users", null); parent.AppendChild(child); //現在parent變成 <users> parent = child; attr_map["cmd"] = "include"; attr_map["data"] = string.Format("{0}/*.xml", UserFolder); child = XMLUtils.CreateElementNode(doc, "X-PRE-PROCESS", attr_map); parent.AppendChild(child); //收工 XMLUtils.SaveXML(doc, file); Directory.CreateDirectory(path + "\\" + UserFolder); }
//注意:regex字串最外層一定要有個() 把整個ID包起來 // 不然會撥不通...因為裡面dest_id=$1 會抓不到 public static void SetConferenceDialplan(string regex) { string path = Path.Combine(new string[] { ConfigPath, "dialplan", Dialplan_Ctx }); string file = Path.Combine(new string[] { path, $"{ Dialplan_Conf }.xml" }); if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } Log.Debug($"SetConferenceDialplan = {regex}"); //先處理regex: //1.頭尾要加上^跟$ //2.要加大括號,不然設定裡那個 ${dest_id} 變數會拉不到資料 //範例: "conf-\d{4}" => 處理後變成 "^(conf-\d{4})$" if (regex[0] == '^') { regex = regex.Substring(1); } if (regex[regex.Length - 1] == '$') { regex = regex.Substring(0, regex.Length - 2); } regex = string.Format("^({0})$", regex); if (File.Exists(file)) { File.Delete(file); } Dictionary <string, string> attr_map = new Dictionary <string, string>(); XmlDocument doc = new XmlDocument(); XmlNode child = null; XmlNode parent = doc.CreateNode(XmlNodeType.Element, "include", null); doc.AppendChild(parent); attr_map["name"] = Dialplan_SIP; child = XMLUtils.CreateElementNode(doc, "extension", attr_map); parent.AppendChild(child); //切換到 <extension> parent = child; attr_map.Clear(); attr_map["field"] = "destination_number"; attr_map["expression"] = regex; child = XMLUtils.CreateElementNode(doc, "condition", attr_map); parent.AppendChild(child); //切換到 <condition> parent = child; attr_map.Clear(); attr_map["application"] = "set"; attr_map["data"] = "dest_id=$1"; child = XMLUtils.CreateElementNode(doc, "action", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["application"] = "answer"; child = XMLUtils.CreateElementNode(doc, "action", attr_map); parent.AppendChild(child); //<action application="conference" data="${dest_id}@sip-conf"/> attr_map.Clear(); attr_map["application"] = "conference"; attr_map["data"] = "${dest_id}@" + Conference_Ctx; child = XMLUtils.CreateElementNode(doc, "action", attr_map); parent.AppendChild(child); XMLUtils.SaveXML(doc, file); }
//清掉指定的Conference設定,重新填回預設值 public static void ResetConferenceConfig() { string file = Path.Combine(new string[] { ConfigPath, "autoload_configs", ConferenceFile }); if (System.IO.File.Exists(file) == false) { throw new FileNotFoundException($"config file not exist! => {file}"); } XmlDocument doc = XMLUtils.LoadXML(file); string xpath = string.Format("/configuration/profiles/profile[@name='{0}']", Conference_Ctx); XmlNode node = doc.SelectSingleNode(xpath); XmlNode parent = null; XmlNode child = null; Dictionary <string, string> attr_map = new Dictionary <string, string>(); if (node != null) { parent = node.ParentNode; parent.RemoveChild(node); } attr_map["name"] = Conference_Ctx; child = XMLUtils.CreateElementNode(doc, "profile", attr_map); parent.AppendChild(child); //切換到 <profile> parent = child; attr_map.Clear(); attr_map["name"] = "domain"; attr_map["value"] = "$${domain}"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "rate"; attr_map["value"] = "8000"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "interval"; attr_map["value"] = "20"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "energy-level"; attr_map["value"] = "100"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "caller-id-name"; attr_map["value"] = "$${outbound_caller_name}"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "caller-id-number"; attr_map["value"] = "$${outbound_caller_id}"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map.Clear(); attr_map["name"] = "comfort-noise"; attr_map["value"] = "true"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); attr_map["name"] = "auto-gain-level"; attr_map["value"] = "true"; child = XMLUtils.CreateElementNode(doc, "param", attr_map); parent.AppendChild(child); XMLUtils.SaveXML(doc, file); }