Пример #1
0
 public Chat()
 {
     ChatCommands.InitAll();
     Steam.AddHandler(this);
 }
Пример #2
0
        public static void HandleChatMessage(SteamFriends.ChatMsgCallback msg)
        {
            SteamID sender  = msg.ChatterID;
            SteamID room    = msg.ChatRoomID;
            String  message = msg.Message;

            Console.WriteLine(Steam.Friends.GetFriendPersonaName(sender) + ": " + message);

            if (Configurator.Config.EnableMySQL)
            {
                SQL.DB.QueryNoReturn(string.Format(@"INSERT INTO chat (uid, message) VALUES({0}, '{1}');", sender.ConvertToUInt64(), SQL.DB.EscapeString(message)));
            }

            recentRequests.RemoveAll(r => DateTime.Now.Subtract(r.Time).TotalSeconds > 2); //Remove recent requests if they were more than 2 seconds ago

            Request recent = recentRequests.FirstOrDefault(r => r.Sender == sender);

            if (recent != null) //This user has already sent a command in the last 2 seconds: Increase kick threshold!
            {
                recent.KickThreshold++;
                recent.Time = DateTime.Now;

                if (!Util.IsAdmin(recent.Sender) && recent.KickThreshold > 4) //When given enough warnings, and not an admin, KICK THE BASTARD
                {
                    Steam.Friends.KickChatMember(room, sender);
                    Send(room, string.Format("{0} has been kicked for spam.", Steam.Friends.GetFriendPersonaName(sender)));

                    recent.KickThreshold = 0; //Don't kick again!
                    return;
                }
            }
            else
            {
                recentRequests.Add(new Request {
                    Sender = sender, Time = DateTime.Now
                });
            }

            int sudocmd = message.StartsWith("sudo") ? 4 : (message.StartsWith("su ") ? 2 : 0);

            if (sudocmd != 0)
            {
                string[] args = message.Substring(sudocmd).Trim().Split(' ');
                if ((args.Length == 0))
                {
                    Send(room, "No command specified.");
                }
                else
                {
                    ChatCommands.HandleChatCommand(room, sender, args);
                }

                return; // We don't want it parsing URLs and what not.
            }

            Thread T = new Thread(() =>
            {
                try {
                    Regex r = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*");

                    Match m = r.Match(message);
                    if (m.Success)
                    {
                        if (m.Value.EndsWith(".mp3"))
                        {
                            /*
                             * Uri uri = new Uri(m.Value);
                             * using (var client = new System.Net.Sockets.TcpClient(uri.Host, uri.Port))
                             * {
                             *  using (System.Net.Sockets.NetworkStream n = client.GetStream())
                             *  {
                             *      uri.PathAndQuery
                             *  }
                             * }
                             *
                             * Send(room, "MP3 File: We'll finish this later.");
                             */
                        }
                        else if (m.Groups["Domain"].Value.EndsWith("youtube.com"))
                        {
                            Regex getvRegex = new Regex(@"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)");
                            Match YTMatch   = getvRegex.Match(m.Value);

                            if (YTMatch.Success)
                            {
                                string vid = YTMatch.Groups[1].Value;
                                string api = "http://www.youtube.com/get_video_info?video_id=" + vid;

                                WebRequest request       = WebRequest.Create(api);
                                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                                Stream dataStream         = response.GetResponseStream();
                                StreamReader reader       = new StreamReader(dataStream);
                                string responseFromServer = reader.ReadToEnd();

                                string name    = "Unknown";
                                string creator = "Probably a copyrighted video.";

                                foreach (var item in responseFromServer.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries))
                                {
                                    var tokens = item.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                                    if (tokens.Length < 2)
                                    {
                                        continue;
                                    }
                                    var paramName  = tokens[0];
                                    var paramValue = tokens[1];

                                    if (paramName == "title")
                                    {
                                        name = paramValue;
                                    }
                                    if (paramName == "author")
                                    {
                                        creator = paramValue;
                                    }
                                }

                                name    = Uri.UnescapeDataString(name.Replace('+', ' '));
                                creator = Uri.UnescapeDataString(creator.Replace('+', ' '));

                                Send(room, "YouTube: " + name + " - " + creator);
                            }
                        }
                        else if (m.Groups["Domain"].Value == "open.spotify.com")
                        {
                            string api               = "http://ws.spotify.com/lookup/1/.json?uri=spotify:track:" + m.Value.Substring(30);
                            WebRequest request       = WebRequest.Create(api);
                            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                            Stream dataStream         = response.GetResponseStream();
                            StreamReader reader       = new StreamReader(dataStream);
                            string responseFromServer = reader.ReadToEnd();

                            JObject token = JObject.Parse(responseFromServer);
                            string name   = token.SelectToken("track").SelectToken("name").ToObject <string>();
                            string artist = token.SelectToken("track").SelectToken("artists").First.SelectToken("name").ToObject <string>();

                            Send(room, "Spotify: '" + name + "' by " + artist);

                            reader.Close();
                            dataStream.Close();
                            response.Close();
                        }
                    }
                }
                catch (Exception ex) {
                    Console.WriteLine("Something went wrong with the URL parsing!");
                    Console.WriteLine(ex.Message);
                }
            });

            T.Start();

            if (message == "make me a sandwich")
            {
                Send(room, "What? Make it yourself.");
            }
        }