private static BMessageNode _parseRecursive(ref StringReader reader, BMessageNode root, int lineNum, bool ignoreAttr = false) { string line = reader.ReadLine().Trim(); while (true) { if (line.StartsWith("BEGIN:")) { switch (line) { case "BEGIN:MSG": case "BEGIN:VCARD": root.ChildrenNode.Add(line.Substring(6), _parseRecursive(ref reader, new BMessageNode(line.Substring(6)), lineNum, true)); break; default: root.ChildrenNode.Add(line.Substring(6), _parseRecursive(ref reader, new BMessageNode(line.Substring(6)), lineNum)); break; } } else if (line.StartsWith("END:")) { if (line.Substring(4) != root.NodeName) { throw new BMessageException(lineNum, "Enclosing node name does not equal to opening node name."); } return(root); } else { if (line.Contains(":") && !ignoreAttr) { string[] kv = line.Split(':'); root.Attributes[kv[0]] = kv[1]; } else { root.Value += line; root.Value += Environment.NewLine; } } line = reader.ReadLine().Trim(); lineNum++; if (line == null) { throw new BMessageException(lineNum, "Reach end of file."); } } }
public static BMessageNode Parse(string bMessageString) { StringReader reader = new StringReader(bMessageString); string line = reader.ReadLine().Trim(); if (!line.StartsWith("BEGIN:")) { throw new BMessageException(1, "bMessage string should starts with BEGIN"); } BMessageNode root = new BMessageNode(line.Substring(6)); return(_parseRecursive(ref reader, root, 1)); }
public async Task <BMessage> GetMessage(string messageHandle) { ObexPacket packet = new ObexPacket( Opcode.GetAlter, new AsciiStringValueHeader(HeaderId.Type, "x-bt/message"), new UnicodeStringValueHeader(HeaderId.Name, messageHandle), new AppParamHeader( new AppParameter(AppParamTagId.Attachment, MasConstants.ATTACHMENT_ON), new AppParameter(AppParamTagId.Charset, MasConstants.CHARSET_UTF8) ) ); Console.WriteLine("Sending GetMessage request "); ObexPacket resp = await RunObexRequest(packet); // "EndOfBody" has been copied to "Body" by ObexClient string bMsgStr = ((BodyHeader)resp.Headers[HeaderId.Body]).Value !; BMessage bMsg; try { BMessageNode bMsgNode = BMessageNode.Parse(bMsgStr); bMsg = new BMessage( status: bMsgNode.Attributes["STATUS"] == "UNREAD" ? MessageStatus.UNREAD : MessageStatus.READ, type: bMsgNode.Attributes["TYPE"], folder: bMsgNode.Attributes["FOLDER"], charset: bMsgNode.ChildrenNode["BENV"].ChildrenNode["BBODY"].Attributes["CHARSET"], length: int.Parse(bMsgNode.ChildrenNode["BENV"].ChildrenNode["BBODY"].Attributes["LENGTH"]), body: bMsgNode.ChildrenNode["BENV"].ChildrenNode["BBODY"].ChildrenNode["MSG"].Value !, sender: MixERP.Net.VCards.Deserializer.GetVCard(bMsgNode.ChildrenNode["VCARD"].ToString()) ); } catch (BMessageException ex) { throw new ObexRequestException($"Failed to get message (handle: {messageHandle}) from MSE. The MSE send back a invalid response", ex); } return(bMsg); }