Пример #1
0
 public Sc2Chat(uint lastMsgId)
 {
     LastMessageId = lastMsgId;
     settingsWC    = new CookieAwareWebClient();
     loginWC       = new CookieAwareWebClient();
     chatWC        = new CookieAwareWebClient();
     loginWC.Headers["User-Agent"] = userAgent;
     loginWC.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8";
     chat           = new ChatMessages();
     chatDownloader = new Timer(new TimerCallback(chatDownloader_Tick), null, Timeout.Infinite, Timeout.Infinite);
     GameList       = new List <KeyValuePair <string, string> >();
 }
Пример #2
0
        public bool DownloadChat(bool reload)
        {
            lock ( chatLock )
            {
                chatWC.Cookies = loginWC.Cookies;

                if (reload)
                {
                    _lastStatus   = null;
                    chat.messages = null;
                }

                var url = String.Format(messagesUrl, ChannelId, TimeUtils.UnixTimestamp());
                System.IO.Stream stream = chatWC.downloadURL(url);

                _lastStatus = chatWC.LastWebError;
                if (_lastStatus == "ProtocolError")
                {
                    //Chat json isn't available at the moment
                    return(false);
                }

                if (stream == null)
                {
                    Debug.Print(String.Format("Sc2tv: stream is null"));
                    return(false);
                }

                var newchat = ParseJson <ChatMessages> .ReadObject(stream);

                if (newchat == null)
                {
                    return(false);
                }
                else if (newchat.messages.Count <= 0)
                {
                    return(false);
                }


                if (chat.messages == null)
                {
                    chat.messages = new List <ChatMessage>();
                    chat.messages = newchat.messages.Where(msg => msg.id < LastMessageId).ToList();
                }

                // Find new messages
                var newmessages = newchat.messages.Except(
                    chat.messages, new LambdaComparer <ChatMessage>((x, y) => x.id == y.id));

                chat = newchat;

                if (newmessages.Count() > 0)
                {
                    LastMessageId = newchat.MaxID();
                }

                // Put "to" nickname into separate property
                foreach (var m in newmessages)
                {
                    var re            = @"\[b\](.*)?\[/b\],";
                    var matchesToUser = Regex.Matches(m.message, re, RegexOptions.IgnoreCase | RegexOptions.Multiline);

                    if (matchesToUser.Count > 0)
                    {
                        if (matchesToUser[0].Groups.Count > 0)
                        {
                            m.to      = matchesToUser[0].Groups[1].Value;
                            m.message = Regex.Replace(m.message, re, "", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        }
                    }
                    OnMessageReceived(new Sc2MessageEvent(m));
                }
                return(true);
            }
        }