diff --git a/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs index 46d497924e..9fb2b4b677 100644 --- a/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs +++ b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs @@ -93,21 +93,30 @@ namespace OpenSim.OfflineIM if (ret == null) return ims; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object resultobj)) return ims; - string result = ret["RESULT"].ToString(); - if (result == "NULL" || result.ToLower() == "false") + if(resultobj is string result) { - string reason = ret.ContainsKey("REASON") ? ret["REASON"].ToString() : "Unknown error"; - m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: GetMessages for {0} failed: {1}", principalID, reason); - return ims; + if (result == "NULL" || result.Equals("false", StringComparison.InvariantCultureIgnoreCase)) + { + if (ret.TryGetValue("REASON", out object rso)) + m_log.Debug($"[OfflineIM.V2.RemoteConnector]: GetMessages for {principalID} failed: {rso}"); + else + m_log.Debug($"[OfflineIM.V2.RemoteConnector]: GetMessages for {principalID} failed: Unknown error"); + return ims; + } } - - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + else if(resultobj is Dictionary resultdic) { - GridInstantMessage m = OfflineIMDataUtils.GridInstantMessage((Dictionary)v); - ims.Add(m); + foreach (object v in resultdic.Values) + { + if (v is Dictionary vdic) + { + GridInstantMessage m = OfflineIMDataUtils.GridInstantMessage(vdic); + ims.Add(m); + } + } } return ims; @@ -115,7 +124,6 @@ namespace OpenSim.OfflineIM public bool StoreMessage(GridInstantMessage im, out string reason) { - reason = string.Empty; Dictionary sendData = OfflineIMDataUtils.GridInstantMessage(im); Dictionary ret = MakeRequest("STORE", sendData); @@ -126,13 +134,20 @@ namespace OpenSim.OfflineIM return false; } - string result = ret["RESULT"].ToString(); - if (result == "NULL" || result.ToLower() == "false") + if(ret.TryGetValue("RESULT", out object o)) { - reason = ret.ContainsKey("REASON") ? ret["REASON"].ToString() : "Unknown error"; - return false; + string result = o.ToString(); + if (result == "NULL" || result.Equals("false", StringComparison.InvariantCultureIgnoreCase)) + { + if(ret.TryGetValue("REASON", out object ro)) + reason = ro.ToString(); + else + reason = "Unknown error"; + return false; + } } + reason = string.Empty; return true; } @@ -160,8 +175,7 @@ namespace OpenSim.OfflineIM ServerUtils.BuildQueryString(sendData), m_Auth); - Dictionary replyData = ServerUtils.ParseXmlResponse( - reply); + Dictionary replyData = ServerUtils.ParseXmlResponse(reply); return replyData; } diff --git a/OpenSim/Services/Interfaces/IOfflineIMService.cs b/OpenSim/Services/Interfaces/IOfflineIMService.cs index db501fd7f5..b7d7682916 100644 --- a/OpenSim/Services/Interfaces/IOfflineIMService.cs +++ b/OpenSim/Services/Interfaces/IOfflineIMService.cs @@ -50,49 +50,50 @@ namespace OpenSim.Services.Interfaces public static GridInstantMessage GridInstantMessage(Dictionary dict) { GridInstantMessage im = new GridInstantMessage(); + object otmp; - if (dict.ContainsKey("BinaryBucket") && dict["BinaryBucket"] != null) - im.binaryBucket = OpenMetaverse.Utils.HexStringToBytes(dict["BinaryBucket"].ToString(), true); + if (dict.TryGetValue("BinaryBucket", out otmp) && otmp is string bbs) + im.binaryBucket = OpenMetaverse.Utils.HexStringToBytes(bbs, true); - if (dict.ContainsKey("Dialog") && dict["Dialog"] != null) - im.dialog = byte.Parse(dict["Dialog"].ToString()); + if (dict.TryGetValue("Dialog", out otmp) && otmp is string ds) + im.dialog = byte.Parse(ds); - if (dict.ContainsKey("FromAgentID") && dict["FromAgentID"] != null) - im.fromAgentID = new Guid(dict["FromAgentID"].ToString()); + if (dict.TryGetValue("FromAgentID", out otmp) && otmp is string faid) + im.fromAgentID = new Guid(faid); - if (dict.ContainsKey("FromAgentName") && dict["FromAgentName"] != null) - im.fromAgentName = dict["FromAgentName"].ToString(); + if (dict.TryGetValue("FromAgentName", out otmp) && otmp is string fan) + im.fromAgentName = fan; else im.fromAgentName = string.Empty; - if (dict.ContainsKey("FromGroup") && dict["FromGroup"] != null) - im.fromGroup = bool.Parse(dict["FromGroup"].ToString()); + if (dict.TryGetValue("FromGroup", out otmp) && otmp is string fg) + im.fromGroup = bool.Parse(fg); - if (dict.ContainsKey("SessionID") && dict["SessionID"] != null) - im.imSessionID = new Guid(dict["SessionID"].ToString()); + if (dict.TryGetValue("SessionID", out otmp) && otmp is string sid) + im.imSessionID = new Guid(sid); - if (dict.ContainsKey("Message") && dict["Message"] != null) - im.message = dict["Message"].ToString(); + if (dict.TryGetValue("Message", out otmp) && otmp is string msg) + im.message = msg; else im.message = string.Empty; - if (dict.ContainsKey("Offline") && dict["Offline"] != null) - im.offline = byte.Parse(dict["Offline"].ToString()); + if (dict.TryGetValue("Offline", out otmp) && otmp is string off) + im.offline = byte.Parse(off); - if (dict.ContainsKey("EstateID") && dict["EstateID"] != null) - im.ParentEstateID = UInt32.Parse(dict["EstateID"].ToString()); + if (dict.TryGetValue("EstateID", out otmp) && otmp is string eid) + im.ParentEstateID = UInt32.Parse(eid); - if (dict.ContainsKey("Position") && dict["Position"] != null) - im.Position = Vector3.Parse(dict["Position"].ToString()); + if (dict.TryGetValue("Position", out otmp) && otmp is string vpos) + im.Position = Vector3.Parse(vpos); - if (dict.ContainsKey("RegionID") && dict["RegionID"] != null) - im.RegionID = new Guid(dict["RegionID"].ToString()); + if (dict.TryGetValue("RegionID", out otmp) && otmp is string rid) + im.RegionID = new Guid(rid); - if (dict.ContainsKey("Timestamp") && dict["Timestamp"] != null) - im.timestamp = UInt32.Parse(dict["Timestamp"].ToString()); + if (dict.TryGetValue("Timestamp", out otmp) && otmp is string ts) + im.timestamp = UInt32.Parse(ts); - if (dict.ContainsKey("ToAgentID") && dict["ToAgentID"] != null) - im.toAgentID = new Guid(dict["ToAgentID"].ToString()); + if (dict.TryGetValue("ToAgentID", out otmp) && otmp is string tid) + im.toAgentID = new Guid(tid); return im; }