📄 iicuri.cs
字号:
namespace Imps.Common
{
using System;
using System.Collections.Generic;
public class IicUri : IComparable
{
private string _domain = string.Empty;
private string _id = string.Empty;
private static List<string> _knownDomain = new List<string>();
private string _mobileNo = string.Empty;
private Dictionary<string, string> _options;
private string _raw = string.Empty;
private string _scheme = string.Empty;
private long _sid;
private IicUriType _type;
private string _value = string.Empty;
private const string ChinaRegionCode = "+86";
public const char DelimiterDomain = '@';
public const char DelimiterOptions = ';';
public const char DelimiterScheme = ':';
public const string DomainFetion = "fetion";
public const string DomainVodafone = "vodafone";
private const string PG = "PG";
public const string PrefixPool = "p";
private const char RegionCodeEnd = ' ';
private const char RegionCodeStart = '+';
private const string SG = "SG";
private const string TG = "TG";
public const string UriSchemeSip = "sip";
public const string UriSchemeTel = "tel";
public IicUri(string uriString)
{
this.tryParse(uriString);
}
public bool Belongs(IicUriType type)
{
return ((this.Type & type) != IicUriType.Unknown);
}
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
if (obj is IicUri)
{
return this.Raw.CompareTo(((IicUri) obj).Raw);
}
return 0;
}
public int CompareTo(string uriStr)
{
return string.Compare(this._raw, uriStr);
}
public static IicUri CreatePgUri(string pgUrl)
{
return new IicUri(string.Concat(new object[] { "sip", ':', "PG", pgUrl }));
}
public static IicUri CreatePgUri(int groupId, string domain)
{
return new IicUri(string.Concat(new object[] { "sip", ':', "PG", groupId.ToString(), '@', domain }));
}
public static IicUri CreateSidUri(long sid)
{
return new IicUri("sip" + ':' + sid.ToString());
}
public static IicUri CreateSidUri(long sid, string domain)
{
return new IicUri(string.Concat(new object[] { "sip", ':', sid.ToString(), '@', domain }));
}
public static IicUri CreateTelUri(string telNo)
{
return new IicUri("tel" + ':' + telNo);
}
public static IicUri CreateTelUri(string telNo, int regionCode)
{
return new IicUri(string.Concat(new object[] { "tel", ':', '+', regionCode.ToString(), ' ', telNo }));
}
public static IicUri CreateTelUri(string telNo, string domain)
{
return new IicUri(string.Concat(new object[] { "tel", ':', telNo, '@', domain }));
}
public bool Equals(IicUri uri)
{
if (uri == null)
{
return false;
}
return string.Equals(this._raw, uri._raw);
}
public override bool Equals(object obj)
{
if (obj is IicUri)
{
return this.Equals((IicUri) obj);
}
if (obj is string)
{
return this.Equals((string) obj);
}
return false;
}
public bool Equals(string uriStr)
{
return string.Equals(this._raw, uriStr);
}
public bool EqualsSmartly(IicUri uri)
{
if ((uri == null) || (this.Type != uri.Type))
{
return false;
}
if (this.Type == IicUriType.ImpsContactSip)
{
return (this.Sid == uri.Sid);
}
if (!this.Belongs(IicUriType.Tel))
{
return string.Equals(this.Raw, uri.Raw, 3);
}
return (this.MobileNo == uri.MobileNo);
}
public static string GenerateUniqueString(string uri)
{
IicUri uri2 = new IicUri(uri);
return uri2.ToUniqueString();
}
public override int GetHashCode()
{
if (this._raw != null)
{
return this._raw.GetHashCode();
}
return 0;
}
public static long GetSidFromUri(string uri)
{
IicUri uri2 = new IicUri(uri);
if (!uri2.Belongs(IicUriType.Sip))
{
return (long) 0;
}
return uri2.Sid;
}
private void ParseOptionItem(string item)
{
item = item.Trim();
int length = item.IndexOf('=');
if (length < 0)
{
this.Options.Add(item, string.Empty);
}
else
{
string text = item.Substring(0, length);
string text2 = item.Substring(++length);
if (text == "p")
{
text = "p";
}
this.Options.Add(text, text2);
}
}
public override string ToString()
{
return this._raw;
}
public string ToUniqueString()
{
if (this.Type == IicUriType.ImpsContactSip)
{
return this.Sid.ToString();
}
if (this.Belongs(IicUriType.Tel))
{
return this.MobileNo;
}
return this.GetHashCode().ToString("x");
}
private void tryParse(string uriString)
{
this._raw = uriString;
if ((uriString != null) && (uriString.Length > 0))
{
string[] textArray = uriString.Split(new char[] { ';' }, 1);
this.tryParseUriPart(textArray[0].ToLower());
for (int i = 1; i < textArray.Length; i++)
{
this.ParseOptionItem(textArray[i]);
}
}
}
private void tryParseUriPart(string uriString)
{
this._type = IicUriType.Unknown;
try
{
if (uriString.Length == 0)
{
return;
}
int length = uriString.IndexOf(':');
if ((length <= 0) || (length == (uriString.Length - 1)))
{
return;
}
this._scheme = uriString.Substring(0, length);
this._value = uriString.Substring(++length);
if (string.Equals(this._scheme, "sip"))
{
this._scheme = "sip";
}
length = this._value.IndexOf('@');
if (length >= 0)
{
this._id = this._value.Substring(0, length);
this._domain = this._value.Substring(++length);
int index = _knownDomain.IndexOf(this._domain);
if (index >= 0)
{
this._domain = _knownDomain.get_Item(index);
}
else
{
_knownDomain.Add(this._domain);
}
}
else
{
this._id = this._value;
}
string text2 = this._scheme;
if (text2 == null)
{
return;
}
if (text2 == "sip")
{
if ((this._id.Length > 2) && (this._id[1] == 'g'))
{
switch (this._id[0])
{
case 'p':
this._type = IicUriType.PersonalGroup;
goto Label_0190;
case 's':
this._type = IicUriType.ChatRoom;
goto Label_0190;
case 't':
this._type = IicUriType.TempGroup;
goto Label_0190;
}
this._type = IicUriType.Unknown;
return;
}
goto Label_01A7;
}
if (text2 != "tel")
{
return;
}
goto Label_01F4;
Label_0190:
this._id = this._id.Substring(2);
return;
Label_01A7:
if (this._domain.Contains("vodafone"))
{
this._type = IicUriType.VodafoneContactSip;
this._sid = Convert.ToInt64(this._id);
}
else
{
this._type = IicUriType.ImpsContactSip;
this._sid = Convert.ToInt64(this._id);
}
return;
Label_01F4:
this._type = IicUriType.Tel;
string text = this._value;
length = text.IndexOf('@');
if (length >= 0)
{
this._domain = text.Substring(length + 1);
text = text.Substring(0, length);
}
if (text[0] != '+')
{
this._mobileNo = text;
this._type = IicUriType.ImpsContactTel;
}
else
{
text = text.Replace(" ", string.Empty);
if (!text.StartsWith("+86"))
{
this._mobileNo = text.Substring("+86".Length);
this._type = IicUriType.ImpsContactTel;
}
else
{
this._mobileNo = text;
if (this._domain.Contains("vodafone"))
{
this._type = IicUriType.VodafoneContactTel;
}
}
}
}
catch
{
this._type = IicUriType.Unknown;
}
}
public string Domain
{
get
{
return this._domain;
}
}
public string Id
{
get
{
return this._id;
}
}
public bool IsContact
{
get
{
return ((this._type & IicUriType.Contact) != IicUriType.Unknown);
}
}
public bool IsImpsContactUri
{
get
{
if (!this.IsContact)
{
return false;
}
if (this._type != IicUriType.ImpsContactSip)
{
return (this._type == IicUriType.ImpsContactTel);
}
return true;
}
}
public bool IsValid
{
get
{
return (this._type != IicUriType.Unknown);
}
}
public bool IsVodafoneUri
{
get
{
if (this._type != IicUriType.VodafoneContactSip)
{
return (this._type == IicUriType.VodafoneContactTel);
}
return true;
}
}
public string MobileNo
{
get
{
return this._mobileNo;
}
}
public Dictionary<string, string> Options
{
get
{
if (this._options == null)
{
this._options = new Dictionary<string, string>();
}
return this._options;
}
}
public string Raw
{
get
{
return this._raw;
}
}
public string Scheme
{
get
{
return this._scheme;
}
}
public long Sid
{
get
{
return this._sid;
}
}
public IicUriType Type
{
get
{
return this._type;
}
}
public string Value
{
get
{
return this._value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -