コード例 #1
0
        protected void AppendMessage(bool incoming, Message msg)
        {
            string iconPath = null;
            string from = null;
            JID fromJid = null;

            if (msg.From == null) {
                from = m_Account.UserDisplayName;
                fromJid = m_Account.Jid;
            } else {
                if (this is MucHandler) {
                    // FIXME: Abstract this...
                    var participant = ((MucHandler)this).Room.Participants[msg.From];
                    if (participant != null) {
                        fromJid = (!String.IsNullOrEmpty(participant.RealJID)) ? participant.RealJID : participant.NickJID;
                        from = participant.Nick;
                    } else {
                        fromJid = msg.From;
                        from = msg.From.Resource;
                    }
                } else if (this is ChatHandler && ((ChatHandler)this).IsMucMessage) {
                    fromJid = msg.From;
                    from = msg.From.Resource;
                } else {
                    fromJid = msg.From;
                    from = m_Account.GetDisplayName(msg.From);
                }
            }

            foreach (XmlNode child in msg) {
                if (child.NamespaceURI == Namespace.ChatStates) {
                    TypingState state = TypingState.None;
                    if (child.LocalName == "active") {
                        state = TypingState.Active;
                    } else if (child.LocalName == "composing") {
                        state = TypingState.Composing;
                    } else if (child.LocalName == "paused") {
                        state = TypingState.Paused;
                    } else if (child.LocalName == "inactive") {
                        state = TypingState.Inactive;
                    } else if (child.LocalName == "gone") {
                        state = TypingState.Gone;
                    } else {
                        Console.WriteLine(String.Format("Unknown chatstate from {0}: {1}", from, child.LocalName));
                    }

                    var typingContent = new ChatContentTyping(m_Account, fromJid, from, null, state);
                    OnNewContent(typingContent);
                }
            }

            if (msg.Body != null || msg.Html != null) {
                string body = null;

                if (!String.IsNullOrEmpty(msg.Html)) {
                    body = HtmlSanitizer.Sanitize(msg.Html, true);
                } else {
                    body = Util.EscapeHtml(msg.Body);
                    body = Util.Linkify(body);
                    body = body.Replace("  ", "  ");
                    body = body.Replace("\t", "    ");
                    body = body.Replace("\r\n", "<br/>");
                    body = body.Replace("\n", "<br/>");
                }

                var guiService = ServiceManager.Get<GuiService>();
                foreach (var formatter in guiService.MessageDisplayFormatters) {
                    if (formatter.SupportsMessage(body, msg)) {
                        body = formatter.FormatMessage(body, msg);
                        if (formatter.StopAfter) {
                            break;
                        }
                    }
                }

                DateTime date = DateTime.Now;

                var nsmgr = new XmlNamespaceManager(msg.OwnerDocument.NameTable);
                nsmgr.AddNamespace("delay", "jabber:x:delay");
                var delay = (XmlElement)msg.SelectSingleNode("delay:x", nsmgr);
                if (delay != null) {
                    string stamp = delay.GetAttribute("stamp");
                    // CCYYMMDDThh:mm:ss
                    date = DateTime.ParseExact(stamp, @"yyyyMMdd\THH:mm:ss", null).ToLocalTime();
                }

                var content = new ChatContentMessage(m_Account, fromJid, from, msg.To, date);
                content.IsOutgoing = !incoming;
                content.MessageHtml = body;

                OnNewContent(content);
            }
        }
コード例 #2
0
        protected void AppendMessage(bool incoming, Message msg)
        {
            string iconPath = null;
            string from     = null;
            JID    fromJid  = null;

            if (msg.From == null)
            {
                from    = m_Account.UserDisplayName;
                fromJid = m_Account.Jid;
            }
            else
            {
                if (this is MucHandler)
                {
                    // FIXME: Abstract this...
                    var participant = ((MucHandler)this).Room.Participants[msg.From];
                    if (participant != null)
                    {
                        fromJid = (!String.IsNullOrEmpty(participant.RealJID)) ? participant.RealJID : participant.NickJID;
                        from    = participant.Nick;
                    }
                    else
                    {
                        fromJid = msg.From;
                        from    = msg.From.Resource;
                    }
                }
                else if (this is ChatHandler && ((ChatHandler)this).IsMucMessage)
                {
                    fromJid = msg.From;
                    from    = msg.From.Resource;
                }
                else
                {
                    fromJid = msg.From;
                    from    = m_Account.GetDisplayName(msg.From);
                }
            }

            foreach (XmlNode child in msg)
            {
                if (child.NamespaceURI == Namespace.ChatStates)
                {
                    TypingState state = TypingState.None;
                    if (child.LocalName == "active")
                    {
                        state = TypingState.Active;
                    }
                    else if (child.LocalName == "composing")
                    {
                        state = TypingState.Composing;
                    }
                    else if (child.LocalName == "paused")
                    {
                        state = TypingState.Paused;
                    }
                    else if (child.LocalName == "inactive")
                    {
                        state = TypingState.Inactive;
                    }
                    else if (child.LocalName == "gone")
                    {
                        state = TypingState.Gone;
                    }
                    else
                    {
                        Console.WriteLine(String.Format("Unknown chatstate from {0}: {1}", from, child.LocalName));
                    }

                    var typingContent = new ChatContentTyping(m_Account, fromJid, from, null, state);
                    OnNewContent(typingContent);
                }
            }

            if (msg.Body != null || msg.Html != null)
            {
                string body = null;

                if (!String.IsNullOrEmpty(msg.Html))
                {
                    body = HtmlSanitizer.Sanitize(msg.Html, true);
                }
                else
                {
                    body = Util.EscapeHtml(msg.Body);
                    body = Util.Linkify(body);
                    body = body.Replace("  ", " &nbsp;");
                    body = body.Replace("\t", " &nbsp;&nbsp;&nbsp;");
                    body = body.Replace("\r\n", "<br/>");
                    body = body.Replace("\n", "<br/>");
                }

                var guiService = ServiceManager.Get <GuiService>();
                foreach (var formatter in guiService.MessageDisplayFormatters)
                {
                    if (formatter.SupportsMessage(body, msg))
                    {
                        body = formatter.FormatMessage(body, msg);
                        if (formatter.StopAfter)
                        {
                            break;
                        }
                    }
                }

                DateTime date = DateTime.Now;

                var nsmgr = new XmlNamespaceManager(msg.OwnerDocument.NameTable);
                nsmgr.AddNamespace("delay", "jabber:x:delay");
                var delay = (XmlElement)msg.SelectSingleNode("delay:x", nsmgr);
                if (delay != null)
                {
                    string stamp = delay.GetAttribute("stamp");
                    // CCYYMMDDThh:mm:ss
                    date = DateTime.ParseExact(stamp, @"yyyyMMdd\THH:mm:ss", null).ToLocalTime();
                }

                var content = new ChatContentMessage(m_Account, fromJid, from, msg.To, date);
                content.IsOutgoing  = !incoming;
                content.MessageHtml = body;

                OnNewContent(content);
            }
        }