// Check against white/black lists or return true if neither are enabled public bool MeetsWhiteBlackList(TwitchStuff StreamInfo) { bool meetswhitelist = false; if (this.UseWhiteList) { foreach (string s in this.WhiteList) { if (StreamInfo.game.Contains(s)) meetswhitelist = true; } } if (this.UseBlackList) { // default to true if it's only the blacklist if (!this.UseWhiteList) meetswhitelist = true; foreach (string s in this.BlackList) { if (StreamInfo.game.Contains(s)) meetswhitelist = false; if (StreamInfo.streamname.Contains(s)) meetswhitelist = false; } } if (!this.UseBlackList && !this.UseWhiteList) { meetswhitelist = true; } return meetswhitelist; }
public static List<TwitchStuff> ConvertDataTable(DataTable dTable) { List<TwitchStuff> ReturnValue = new List<TwitchStuff>(); foreach(DataRow row in dTable.Rows) { TwitchStuff newTwitchStuff = new TwitchStuff(); newTwitchStuff.game = row["StreamGame"].ToString(); if (Convert.ToInt32(row["StreamOnline"]) == 1) newTwitchStuff.streamerlive = "true"; else newTwitchStuff.streamerlive = "false"; newTwitchStuff.streamid = Convert.ToInt32(row["StreamID"]); newTwitchStuff.streamerviewcount = row["StreamViewerCount"].ToString(); newTwitchStuff.streamername = row["StreamerName"].ToString(); newTwitchStuff.streamname = row["StreamTitle"].ToString(); newTwitchStuff.ChannelName = row["ChannelName"].ToString(); newTwitchStuff.ChannelID = Convert.ToInt32(row["ChannelID"]); ReturnValue.Add(newTwitchStuff); } return ReturnValue; }
public bool AddUser(string username, Channels ChannelWatch, IIrcMessageTarget target, IIrcMessageSource source, IrcClient ircConnection) { ModifyingConfig = true; bool returnvalue = false; IrcChannel findChannel = null; foreach (IrcChannel ircChannel in ircConnection.Channels) { if(ircChannel.Name == target.Name) { findChannel = ircChannel; } } if (Utilities.CheckOp(source.Name, findChannel)) { try { Channels Watch = new Channels(); foreach (Channels c in TwitchChannels) { if (findChannel.Name == c.ChannelName) { Watch = c; }//if (findChannel.Name == c.ChannelName) }//foreach (Channels c in TwitchChannels) // config writing XElement newStreamer = new XElement("streamer"); newStreamer.SetAttributeValue("value", username); XElement xElem = ConfigDocument.Descendants("servers").FirstOrDefault().Descendants("server").FirstOrDefault().Elements("channel").First(x => x.Attribute("id").Value == Watch.ChannelName); xElem.Descendants("streamers").FirstOrDefault().Add(newStreamer); TwitchStuff twitchInfo = new TwitchStuff(); twitchInfo.UpdateInfo(username,this); Watch.Streamers.Add(username); Watch.StreamInfo.Add(twitchInfo); returnvalue = true; ConfigDocument.Save(FileName); }//try catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; returnvalue = false; } } ModifyingConfig = false; return returnvalue; }
public bool SetNotice(string username, Channels ChannelWatch, IIrcMessageTarget target, IIrcMessageSource source, IrcClient ircConnection, bool SetNoticeValue) { ModifyingConfig = true; bool returnvalue = false; IrcChannel findChannel = null; foreach (IrcChannel ircChannel in ircConnection.Channels) { if (ircChannel.Name == target.Name) { findChannel = ircChannel; } } if (ChannelWatch.Streamers.Contains(username)) { try { TwitchStuff twitchInfo = new TwitchStuff(); foreach (TwitchStuff huntInfo in ChannelWatch.StreamInfo) { if (huntInfo.streamername == username) { twitchInfo = huntInfo; }//if (huntInfo.streamername == username) }//foreach (TwitchStuff huntInfo in Watch.StreamInfo) XElement chanNode = ConfigDocument.Descendants("servers").FirstOrDefault().Descendants("server").FirstOrDefault().Elements("channel").First(x => x.Attribute("id").Value == ChannelWatch.ChannelName); XElement streamercheck = chanNode.Element("streamers").Descendants("streamer").First(x => x.Attribute("value").Value == username); if (streamercheck != null) { if (SetNoticeValue) { try { streamercheck.Attribute("setnotice").SetValue("true"); } catch { streamercheck.Add(new XAttribute("setnotice", "true")); } twitchInfo.setnotice = true; } else { streamercheck.Attribute("setnotice").SetValue("false"); twitchInfo.setnotice = false; } } returnvalue = true; ConfigDocument.Save(FileName); }//try catch (Exception ex) { returnvalue = false; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; }//catch } else { ircConnection.LocalUser.SendNotice(source.Name, "You are not on the streamer list."); } ModifyingConfig = false; return returnvalue; }
public bool RemoveUser(string username, Channels ChannelWatch, IIrcMessageTarget target, IIrcMessageSource source, IrcClient ircConnection) { ModifyingConfig = true; bool returnvalue = false; IrcChannel findChannel = null; foreach (IrcChannel ircChannel in ircConnection.Channels) { if (ircChannel.Name == target.Name) { findChannel = ircChannel; } } if (Utilities.CheckOp(source.Name, findChannel)) { try { Channels Watch = new Channels(); foreach (Channels c in TwitchChannels) { if (findChannel.Name == c.ChannelName) { Watch = c; }//if (findChannel.Name == c.ChannelName) }//foreach (Channels c in TwitchChannels) Watch.Streamers.Remove(username); TwitchStuff twitchInfo = new TwitchStuff(); foreach (TwitchStuff huntInfo in Watch.StreamInfo) { if (huntInfo.streamername == username) { twitchInfo = huntInfo; }//if (huntInfo.streamername == username) }//foreach (TwitchStuff huntInfo in Watch.StreamInfo) Watch.StreamInfo.Remove(twitchInfo); // config writing if(AllStreamers.Keys.Contains(username)) { AllStreamers.Remove(username); } XElement chanNode = ConfigDocument.Descendants("servers").FirstOrDefault().Descendants("server").FirstOrDefault().Elements("channel").First(x => x.Attribute("id").Value == Watch.ChannelName); XElement streamercheck = chanNode.Element("streamers").Descendants("streamer").First(x => x.Attribute("value").Value == username); if (streamercheck != null) streamercheck.Remove(); returnvalue = true; ConfigDocument.Save(FileName); }//try catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; returnvalue = false; }//catch } ModifyingConfig = false; return returnvalue; }
// this is a hot mess, trying to improve it public void ParseConfig() { ConfigDocument = XDocument.Load(FileName); XElement serversNode = ConfigDocument.Root; foreach (XElement serverNode in serversNode.Descendants("server")) { ServerNodes.Add(serverNode); try { string updateToDB = serverNode.Attribute("updatedb").Value; if (updateToDB == "true") { this.ConnectionString = serverNode.Attribute("ConnString").Value; this.UseDB = true; } } catch (Exception ex) { } XElement userinfo = serverNode.Descendants("userinfo").FirstOrDefault(); BotUsername = userinfo.Descendants("nick").FirstOrDefault().Value; BotRealname = userinfo.Descendants("name").FirstOrDefault().Value; try { OwnerIdentity = userinfo.Descendants("ownerident").FirstOrDefault().Value; } catch { OwnerIdentity = ""; } try { HasOwner = Convert.ToBoolean(userinfo.Descendants("hasowner").FirstOrDefault().Value); } catch { HasOwner = false; } try { UsePassword = Convert.ToBoolean(userinfo.Descendants("needsserverpassword").FirstOrDefault().Value.ToString()); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; UsePassword = false; }//catch (Exception ex) BotInfo.NickName = BotUsername; BotInfo.UserName = BotUsername; BotInfo.RealName = BotRealname; //BotInfo.Password = userinfo.Descendants("userpassword").FirstOrDefault().Value; BotInfo.Password = ServerNodes.Descendants("serverpassword").FirstOrDefault().Value; ServerName = serverNode.Attribute("id").Value; Console.WriteLine("Server pointed to: " + ServerName); List<TwitchStuff> StreamersList = new List<TwitchStuff>(); TwitchStuff StreamInfo = new TwitchStuff(); LiveMessage = serversNode.Elements("liveannouncement").FirstOrDefault().Attribute("value").Value.ToString(); string valuecheck = serversNode.Elements("liveannouncement").FirstOrDefault().Value; XElement serversubnode = serversNode.Elements("streamannouncement").FirstOrDefault(); BaseMessageStartStreaming = serversubnode.Attribute("value").Value.ToString(); ChangeMessage = serversNode.Elements("titlechangeannouncement").FirstOrDefault().Attribute("value").Value.ToString(); List<XElement> channelNodeElements = new List<XElement>(); foreach (XElement channelNode in serverNode.Descendants("channel")) { List<XElement> StreamerNodes = new List<XElement>(); channelNodeElements.Add(channelNode); #region TODO look into better error handling for this shit lol -- Pull channel config values valuecheck = ""; Channels channelMonitor = new Channels(); try { valuecheck = channelNode.Descendants("liveannouncement").FirstOrDefault().Attribute("value").Value.ToString(); }//try catch { valuecheck = ""; }//catch if (valuecheck != "") // custom announcements inside the channel, let's use them instead. { channelMonitor.LiveMessage = valuecheck; }//if (valuecheck != "") valuecheck = ""; try { valuecheck = channelNode.Attribute("useinfo").Value.ToString(); } catch { valuecheck = ""; } if (valuecheck != "") { channelMonitor.InfoCommands = Convert.ToBoolean(valuecheck); } try { valuecheck = channelNode.Attribute("").Value.ToString(); } catch { valuecheck = ""; } if (valuecheck != "") { channelMonitor.Mystery = Convert.ToBoolean(valuecheck); } try { valuecheck = channelNode.Descendants("streamannouncement").FirstOrDefault().Attribute("value").Value.ToString(); }//try catch { valuecheck = ""; }//catch if (valuecheck != "") // custom announcements inside the channel, let's use them instead. { channelMonitor.AnnounceMessage = valuecheck; }//if (valuecheck != "") valuecheck = ""; try { valuecheck = channelNode.Descendants("titlechangeannouncement").FirstOrDefault().Attribute("value").Value.ToString(); } catch { valuecheck = ""; } if (valuecheck != "") // custom announcements inside the channel, let's use them instead. { channelMonitor.ChangedMessage = valuecheck; }//if (valuecheck != "") channelMonitor.ChannelName = channelNode.Attribute("id").Value; try { valuecheck = channelNode.Attribute("usewhitelist").Value.ToString(); channelMonitor.UseWhiteList = Convert.ToBoolean(channelNode.Attribute("usewhitelist").Value.ToString()); }//try catch { channelMonitor.UseWhiteList = false; }//catch try { channelMonitor.UseBlackList = Convert.ToBoolean(channelNode.Attribute("useblacklist").Value.ToString()); }//try catch { channelMonitor.UseBlackList = false; }//catch try { channelMonitor.ChannelPassword = channelNode.Attribute("password").Value.ToString(); }//try catch { channelMonitor.ChannelPassword = ""; }//catch #endregion channelMonitor.Streamers = new List<string>(); Console.WriteLine("Adding Channel " + channelMonitor.ChannelName + " to the monitor list"); List<string> Streamers = new List<string>(); TwitchAPIInterface checkTwitch = new TwitchAPIInterface(); StreamersList = new List<TwitchStuff>(); XElement whiteListInfo = channelNode.Elements("whitelist").FirstOrDefault(); if (whiteListInfo != null) { foreach (XElement whitelistitem in whiteListInfo.Elements("game").DefaultIfEmpty()) { string gamename = whitelistitem.Attribute("name").Value.ToString(); channelMonitor.WhiteList.Add(gamename); }//foreach (XElement whitelistitem in whiteListInfo.Elements("game").DefaultIfEmpty()) }//if (whiteListInfo != null) XElement blackListInfo = channelNode.Elements("blacklist").FirstOrDefault(); if (blackListInfo != null) { foreach (XElement BlackListitem in blackListInfo.Elements("game").DefaultIfEmpty()) { string gamename = BlackListitem.Attribute("name").Value.ToString(); channelMonitor.BlackList.Add(gamename); }//foreach (XElement BlackListitem in blackListInfo.Elements("game").DefaultIfEmpty()) }//if (blackListInfo != null) foreach (XElement streamers in channelNode.Elements("streamers")) { StreamersNodes.Add(channelMonitor.ChannelName, streamers); } foreach (XElement streamer in channelNode.Elements("streamers").Elements("streamer").ToList()) { StreamerNodes.Add(streamer); try { string twitchid = streamer.Attribute("value").Value.ToString(); Streamers.Add(twitchid); bool setnoticevalue = false; try { setnoticevalue = Convert.ToBoolean(streamer.Attribute("setnotice").Value.FirstOrDefault()); } catch { setnoticevalue = false; } StreamInfo = new TwitchStuff(); StreamInfo.setnotice = setnoticevalue; if (StreamInfo.UpdateInfo(twitchid,this) != false) { if (StreamInfo.streamerlive == "true") { } } else { StreamInfo = new TwitchStuff(twitchid); Console.WriteLine("Adding offline stream info for: " + StreamInfo.streamername); }//else channelMonitor.StreamInfo.Add(StreamInfo); channelMonitor.Streamers.Add(StreamInfo.streamername); if(!AllStreamers.Keys.Contains(StreamInfo.streamername)) AllStreamers.Add(StreamInfo.streamername, StreamInfo); }//try catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; continue; }//catch (Exception ex) }//foreach (XElement streamer in channelNode.Elements("streamers").Elements("streamer").ToList()) channelMonitor.LastLiveAnnouncement = DateTime.Now.AddMinutes(-3); TwitchChannels.Add(channelMonitor); List<string> raceList = new List<string>(); foreach (XElement race in channelNode.Elements("races").Elements("race").ToList()) { channelMonitor.ChannelRaces.Add(race.Attribute("name").Value.ToString()); Console.WriteLine("Adding watch for race: " + race.Attribute("name").Value.ToString() + " for channel " + channelMonitor.ChannelName); }//foreach (XElement race in channelNode.Elements("races").Elements("race").ToList()) }//foreach (XElement channelNode in serverNode.Descendants("channel")) } }
public void SweepChannels() { while (config.ModifyingConfig) { } // wait out config modifications SweepingChannels = true; if (LastFullUpdate < DateTime.Now) { LastFullUpdate = DateTime.Now.AddMinutes(2); foreach (Channels channel in config.TwitchChannels) { Dictionary<string, List<TwitchStuff>> updateDB = new Dictionary<string, List<TwitchStuff>>(); List<TwitchStuff> dbList = new List<TwitchStuff>(); foreach (TwitchStuff streamInfo in channel.StreamInfo) { TwitchStuff oldInfo = new TwitchStuff(); oldInfo.streamerlive = streamInfo.streamerlive; oldInfo.streamername = streamInfo.streamername; oldInfo.streamname = streamInfo.streamname; oldInfo.game = streamInfo.game; try { string wasLive = streamInfo.streamerlive; // get the info for the 'stream' field string addToList = ""; if(streamInfo.UpdateInfo(streamInfo.streamername,config)) { if (oldInfo.streamerlive == "false" && streamInfo.streamerlive == "true") { if (channel.LiveMessage != "") { addToList = channel.LiveMessage.Trim(); }//if (channel.LiveMessage != "") else { addToList = config.LiveMessage.Trim(); }//else addToList = Utilities.TemplateString(addToList, streamInfo.streamername, streamInfo.game, streamInfo.streamerviewcount, streamInfo.streamname, config.ServerName.ToLower().Contains("twitch.tv")); bool meetswhitelist = channel.MeetsWhiteBlackList(streamInfo); if (streamInfo.lastannounce.AddMinutes(30) <= DateTime.Now && meetswhitelist && streamInfo.LastOffLine.AddMinutes(30) <= DateTime.Now) { if (meetswhitelist && !streamInfo.setnotice) ircConnection.LocalUser.SendMessage(channel.ChannelName, addToList); streamInfo.lastannounce = DateTime.Now; }//if (streamInfo.lastannounce.AddMinutes(30) <= DateTime.Now && meetswhitelist) } else if (streamInfo.streamerlive == "true" && oldInfo.streamname != streamInfo.streamname && oldInfo.game != streamInfo.game) { bool changesmeetwhitelist = channel.MeetsWhiteBlackList(streamInfo); if (changesmeetwhitelist) { if (channel.ChangedMessage != "") { addToList = channel.ChangedMessage; }//if (channel.ChangedMessage != "") else { addToList = config.ChangeMessage.Trim(); }//else addToList = Utilities.TemplateString(addToList, streamInfo.streamername, streamInfo.game, streamInfo.streamerviewcount, streamInfo.streamname, config.ServerName.ToLower().Contains("twitch.tv")); if (streamInfo.lastchangeannounce.AddMinutes(15) < DateTime.Now && !streamInfo.setnotice) { ircConnection.LocalUser.SendMessage(channel.ChannelName, addToList); streamInfo.lastchangeannounce = DateTime.Now; } }//if (changesmeetwhitelist) streamInfo.lastannounce = DateTime.Now; } if (streamInfo.streamerlive == "false" && oldInfo.streamerlive == "true") { streamInfo.LastOffLine = DateTime.Now; } dbList.Add(streamInfo); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; } } // Update DB updateDB.Add(channel.ChannelName, dbList); if (config.UseDB) { DataAccess daMumbo = new DataAccess(); daMumbo.UpdateStreamInfo(updateDB, config.ConnectionString); } } } SweepingChannels = false; }