📄 useraccounts.cs
字号:
namespace Imps.Client.Core
{
using Imps.Client;
using Imps.Client.Utils;
using Imps.Client.Utils.Cryptography;
using Imps.Common;
using Imps.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
public static class UserAccounts
{
private static XmlNode _acccountsNode;
private static AccountData _lastestAccount;
private static bool _modified;
private static IPersistentManager _persistentMgr;
public const string AN_AutoLogin = "auto-login";
public const string AN_AutoStartup = "auto-start";
public const string AN_EncryptPassword = "EncryptPassword";
public const string AN_Id = "id";
public const string AN_MobileNo = "mobile-no";
public const string AN_Password = "password";
public const string AN_Presence = "presence";
public const string AN_Sid = "sid";
public const string NN_UserConfig = "ImpsConfiguration";
public const string NN_UserConfig_Accounts = "Accounts";
public const string NN_UserConfig_Accounts_Item = "Account";
public const string NN_UserConfig_LatestAccount = "LatestAccount";
public const string NN_UserConfig_Mappings = "Mappings";
public const string NN_UserConfig_Mappings_Item = "Mapping";
public const string NN_UserConfig_Startup = "Startup";
public static bool AddUpdateAccountElement(AccountData ad)
{
XmlElement node = FindAccountElement(ad.Id);
if (node == null)
{
node = XmlHelper.AppendChildElement(XmlNodeAccounts, "Account");
XmlHelper.SetNodeAttribute(node, "id", ad.Id);
}
string attrVal = ad.Password ?? string.Empty;
if (!FuncLimitedSetting.SSIEncryptLoginLimited)
{
if (!string.IsNullOrEmpty(attrVal))
{
XmlHelper.SetNodeAttribute(node, "EncryptPassword", attrVal);
node.RemoveAttribute("password");
}
else
{
node.RemoveAttribute("EncryptPassword");
}
}
else if (!string.IsNullOrEmpty(attrVal))
{
XmlHelper.SetNodeAttribute(node, "password", EncryptPassword(attrVal));
}
else
{
node.RemoveAttribute("password");
}
if (ad.AutoLogin)
{
XmlHelper.SetNodeAttribute(node, "auto-login", "1");
}
else
{
node.RemoveAttribute("auto-login");
}
if (ad.PresenceValue != 400)
{
XmlHelper.SetNodeAttributeInt32(node, "presence", ad.PresenceValue);
}
else
{
node.RemoveAttribute("presence");
}
_modified = true;
return true;
}
public static bool AddUpdateAccountMappingElement(string mobileNo, long sid)
{
string attrVal = sid.ToString();
XmlElement node = FindMappingElement(mobileNo);
if (node == null)
{
node = XmlHelper.AppendChildElement(XmlNodeMappings, "Mapping");
}
string text2 = XmlHelper.ReadXmlAttributeString(node, "mobile-no");
string text3 = XmlHelper.ReadXmlAttributeString(node, "sid");
if ((mobileNo == text2) && (attrVal == text3))
{
return false;
}
XmlHelper.SetNodeAttribute(node, "mobile-no", mobileNo);
XmlHelper.SetNodeAttribute(node, "sid", attrVal);
_modified = true;
return true;
}
private static string DecryptPassword(string text)
{
try
{
return SecurityHelper.DecryptPassword(text);
}
catch
{
return string.Empty;
}
}
private static string EncryptPassword(string plain)
{
return SecurityHelper.EncryptPassword(plain);
}
public static string[] EnumAccounts()
{
XmlNodeList list = XmlNodeAccounts.SelectNodes("Account");
List<string> list2 = new List<string>(list.Count);
for (int i = 0; i < list.Count; i++)
{
long nbr;
string s = XmlHelper.ReadXmlAttributeString(list[i], "id");
if (ImpsHelper.TryParseSidOrCmccMobileNo(s, out nbr))
{
list2.Add(s);
}
}
return list2.ToArray();
}
public static bool EraseAccountElement(string id)
{
XmlElement oldChild = FindAccountElement(id);
if (oldChild == null)
{
return false;
}
XmlNodeAccounts.RemoveChild(oldChild);
XmlNode xmlNodeLatest = XmlNodeLatest;
if (xmlNodeLatest != null)
{
string text = XmlHelper.ReadXmlAttributeString(xmlNodeLatest, "id");
if (id == text)
{
XmlHelper.SetNodeAttribute(xmlNodeLatest, "id", string.Empty);
}
}
_modified = true;
return true;
}
public static bool EraseAccountPassword(string id)
{
XmlElement element = FindAccountElement(id);
if (element == null)
{
return false;
}
XmlAttribute node = element.Attributes["password"];
if (node == null)
{
return false;
}
if (node.Value.Length <= 0)
{
return false;
}
element.Attributes.Remove(node);
_modified = true;
return true;
}
public static void EraseLatestAccountPassword()
{
if (EraseAccountPassword(LatestId))
{
SaveToLocal();
}
}
public static bool EraseMappedSid(string mobileNo)
{
if (string.IsNullOrEmpty(mobileNo))
{
return false;
}
XmlElement element = FindMappingElement(mobileNo);
if (element == null)
{
return false;
}
XmlAttribute node = element.Attributes["sid"];
if (node == null)
{
return false;
}
element.Attributes.Remove(node);
return true;
}
private static XmlElement FindAccountElement(string id)
{
XmlNode xmlNodeAccounts = XmlNodeAccounts;
if (xmlNodeAccounts == null)
{
return null;
}
return (xmlNodeAccounts.SelectSingleNode(string.Format("{0}[@{1}='{2}']", "Account", "id", id)) as XmlElement);
}
private static XmlElement FindMappingElement(string mobileNo)
{
XmlNode xmlNodeMappings = XmlNodeMappings;
if (xmlNodeMappings == null)
{
return null;
}
return (xmlNodeMappings.SelectSingleNode(string.Format("{0}[@{1}='{2}']", "Mapping", "mobile-no", mobileNo)) as XmlElement);
}
public static AccountData GetAccount(string id)
{
AccountData data = null;
XmlNode node = FindAccountElement(id);
if (node != null)
{
data = new AccountData(id);
data.Password = XmlHelper.ReadXmlAttributeString(node, "EncryptPassword");
if (string.IsNullOrEmpty(data.Password))
{
string text = XmlHelper.ReadXmlAttributeString(node, "password");
if (!string.IsNullOrEmpty(text))
{
data.Password = User.DoHashPassword(Encoding.UTF8.GetBytes(DecryptPassword(text)));
}
}
data.AutoLogin = XmlHelper.ReadXmlAttributeBoolean(node, "auto-login", false);
data.PresenceValue = XmlHelper.ReadXmlAttributeInt32(node, "presence", 400);
}
return data;
}
public static long GetMappedSid(string mobileNo)
{
XmlNode node = FindMappingElement(mobileNo);
if (node == null)
{
return (long) 0;
}
try
{
return XmlHelper.ReadXmlAttributeInt64(node, "sid", (long) 0);
}
catch
{
}
}
private static void LoadAccountData(XmlNode node)
{
_acccountsNode = node;
}
public static void LoadFromLocal()
{
_persistentMgr.LoadAccountsData(new PersistentDelegate(UserAccounts.LoadAccountData));
}
public static void SaveToLocal()
{
if (_modified)
{
_persistentMgr.SaveLocalAccountsData(delegate (XmlNode section) {
section.InnerXml = (_acccountsNode == null) ? string.Empty : _acccountsNode.InnerXml;
});
_modified = false;
}
}
public static bool SetAccountAutoLogin(string id, bool autoLogin)
{
XmlElement node = FindAccountElement(id);
if (node == null)
{
return false;
}
XmlAttribute attribute = node.Attributes["auto-login"];
if (autoLogin)
{
XmlHelper.SetNodeAttribute(node, "auto-login", "1");
}
else
{
if (attribute == null)
{
return false;
}
node.Attributes.Remove(attribute);
}
_modified = true;
return true;
}
public static void SetPersistentManager(IPersistentManager mgr)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -