コード例 #1
0
ファイル: ChatUserMessage.cs プロジェクト: zdchupka/HMCT
        private static ChatUserMessage Parse(ChatUser fromUser, string text, IList <ChatUser> allUsers, bool ignoreTagAndUsers)
        {
            List <string>   tags    = new List <string>();
            List <ChatUser> toUsers = new List <ChatUser>();

            if (!ignoreTagAndUsers)
            {
                // If the message is tagged, send to all users, don't even parse usernames
                if (TryGetTags(text, out tags, out text))
                {
                }
                else
                {
                    toUsers = GetWhisperUsers(text, allUsers, out text);
                }
            }

            ChatUserMessage message = null;

            if (MatchesCommand(text, Commands.IMAGEURL))
            {
                string imageUrl = RemoveCommand(text, Commands.IMAGEURL);
                message = new ImageUrlChatMessage {
                    ImageUrl = imageUrl
                };
            }
            else if (MatchesCommand(text, Commands.WEB))
            {
                string url = RemoveCommand(text, Commands.WEB);
                message = new WebUrlChatMessage {
                    Url = url
                };
            }
            else if (MatchesCommand(text, Commands.STATUS_UPDATE))
            {
                string status = RemoveCommand(text, Commands.STATUS_UPDATE);
                message = new UserStatusMessage {
                    Text = status
                };
            }
            else if (MatchesCommand(text, Commands.ASCII))
            {
                string asciiText = RemoveCommand(text, Commands.ASCII);
                message = new ASCIIChatMessage {
                    Text = asciiText
                };
            }
            else if (MatchesCommand(text, Commands.XAML))
            {
                string xaml = RemoveCommand(text, Commands.XAML);
                message = new XamlChatMessage {
                    Xaml = xaml
                };
            }
            else if (MatchesCommand(text, Commands.MEME))
            {
                string memeText = RemoveCommand(text, Commands.MEME);
                message = MemeChatMessage.FromString(memeText);
            }
            else if (MatchesCommand(text, Commands.POLL))
            {
                string pollText = RemoveCommand(text, Commands.POLL);
                message = PollMessage.FromString(pollText);
            }
            else if (MatchesCommand(text, Commands.LINK) || HyperlinkUtility.IsHyperlink(text))
            {
                string url = RemoveCommand(text, Commands.LINK);
                message = new HyperlinkChatMessage(url);
            }
            else if (MatchesCommand(text, Commands.VIDEOURL))
            {
                string url = RemoveCommand(text, Commands.VIDEOURL);

                if (url.Contains("https://"))
                {
                    url = url.Replace("https://", "http://");
                }
                message = new VideoUrlChatMessage {
                    VideoUrl = url
                };
            }
            else if (MatchesCommand(text, Commands.WHITEBOARD) || text == "/" + Commands.WHITEBOARD)
            {
                if (text == "/" + Commands.WHITEBOARD)
                {
                    text += " ";
                }
                string url = RemoveCommand(text, Commands.WHITEBOARD);
                message = new WhiteboardChatMessage {
                    ImageUrl = url
                };
            }
            else
            {
                message = new NormalChatUserMessage {
                    Text = text
                };
            }

            message.ToUsers  = toUsers;
            message.FromUser = fromUser;
            message.Tags     = tags;

            return(message);
        }
コード例 #2
0
        public static void DownloadMetadata(this HyperlinkChatMessage message)
        {
            WebResponse  response = null;
            StreamReader reader   = null;
            string       result   = string.Empty;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(message.Url);
                request.Method = "GET";
                response       = request.GetResponse();
                reader         = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result         = reader.ReadToEnd();
                bool hasMetaData = false;

                HtmlDocument doc = new HtmlDocument();

                doc.LoadHtml(result);

                var imageNode = doc.DocumentNode.SelectSingleNode("//meta[@property='og:image']");

                if (imageNode != null)
                {
                    message.ImageUrl = imageNode.Attributes["content"].Value;
                }

                var siteName = doc.DocumentNode.SelectSingleNode("//meta[@property='og:site_name']");
                if (siteName != null)
                {
                    message.SiteName = HttpUtility.HtmlDecode(siteName.Attributes["content"].Value.ToUpper());
                }


                var title = doc.DocumentNode.SelectSingleNode("//meta[@property='og:title']");
                if (title != null)
                {
                    message.Title = HttpUtility.HtmlDecode(title.Attributes["content"].Value);
                    hasMetaData   = true;
                }

                var description = doc.DocumentNode.SelectSingleNode("//meta[@property='og:description']");
                if (description != null)
                {
                    message.Description = HttpUtility.HtmlDecode(description.Attributes["content"].Value);
                }
                else
                {
                    message.Description = "(No description)";
                }
                message.HasMetaData = hasMetaData;
            }
            catch
            {
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }