// ---------------- Constructor ---------------- public HttpResponseHandler(IIrcWriter writer) { this.isConnected = false; this.isConnectedLock = new object(); this.writer = writer; }
/// <summary> /// Handles the response when the user wants to calculate something. /// </summary> /// <param name="writer"></param> /// <param name="response"></param> private static void MathHandler(IIrcWriter writer, IrcResponse response) { Match match = Regex.Match(response.Message, handlerRegex); string expression = match.Groups["expression"].Value; if (match.Success) { try { string answer = MathBotCalculator.Calculate(expression); writer.SendMessageToUser( "'" + expression + "' calculates to '" + answer + "'", response.Channel ); } catch (Exception) { writer.SendMessageToUser( "'" + expression + "' is not something I can calculate :(", response.Channel ); } } else { writer.SendMessageToUser( "'" + expression + "' is not something I can calculate :(", response.Channel ); } }
// -------- Functions -------- /// <summary> /// Handles the event and sends the responses to the channel if desired. /// </summary> /// <param name="line">The RAW line from IRC to check.</param> /// <param name="ircConfig">The irc config to use when parsing this line.</param> /// <param name="ircWriter">The way to write to the irc channel.</param> public void HandleEvent(string line, IIrcConfig ircConfig, IIrcWriter ircWriter) { ArgumentChecker.StringIsNotNullOrEmpty(line, nameof(line)); ArgumentChecker.IsNotNull(ircConfig, nameof(ircConfig)); ArgumentChecker.IsNotNull(ircWriter, nameof(ircWriter)); Match match = pattern.Match(line); if (match.Success) { string remoteUser = match.Groups["nick"].Value; // Don't fire if we were the ones to trigger the event. if (remoteUser.ToUpper() == ircConfig.Nick.ToUpper()) { return; } IrcResponse response = new IrcResponse( remoteUser, match.Groups["channel"].Value, string.Empty ); this.PartAction(ircWriter, response); } }
// ---- Handlers ---- /// <summary> /// Handles the help command. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private static void HandleHelpCommand(IIrcWriter writer, MessageHandlerArgs response) { writer.SendMessage( "Valid commands: XXXXX (US Zip Code), help, about, sourcecode. Each command has a " + cooldown + " second cooldown.", response.Channel ); }
/// <summary> /// Handles the time command. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private static void HandleTimeCmd(IIrcWriter writer, IrcResponse response) { DateTime time = DateTime.UtcNow; writer.SendCommandToChannel( "My time is " + time.ToString("yyyy-MM-dd hh:mm:ss") + " UTC." ); }
// ---- Handlers ---- /// <summary> /// Ran when someone joins the channel. /// </summary> /// <param name="writer">The means to write to an IRC channel.</param> /// <param name="response">The command from the server.</param> private static void JoinMessage(IIrcWriter writer, IrcResponse response) { writer.SendMessageToUser( "Greetings " + response.RemoteUser + ", welcome to " + response.Channel + "!", response.RemoteUser ); writer.SendCommandToChannel(response.RemoteUser + " has joined " + response.Channel); }
// ---------------- Constructor ---------------- public JoinHandlerArgs(IIrcWriter writer, string user, string channel, Regex regex, Match match) { this.Writer = writer; this.User = user; this.Channel = channel; this.Regex = regex; this.Match = match; }
/// <summary> /// Handles a message from the channel. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private void HandleMessage(IIrcWriter writer, IrcResponse response) { if (CheckForCaps(response.Message)) { string msgToSend = SelectMessage().Replace("{%user%}", response.RemoteUser); writer.SendCommandToChannel(msgToSend); } }
/// <summary> /// Ran when someone parts the channel. /// </summary> /// <param name="writer">The means to write to an IRC channel.</param> /// <param name="response">The command from the server.</param> private static void PartMessage(IIrcWriter writer, IrcResponse response) { writer.SendMessageToUser( "Thanks for visiting " + response.Channel + "! Please come back soon!", response.RemoteUser ); writer.SendCommandToChannel( response.RemoteUser + " has left " + response.Channel ); }
// ---------------- Constructor ---------------- public CtcpPingHandlerArgs( IIrcWriter writer, string user, string channel, string message, Regex regex, Match match ) : base(writer, user, channel, message, regex, match) { }
/// <summary> /// Handles the weather command by doing a GET request to NOAA's API /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private async void HandleWeatherCommand(IIrcWriter writer, IrcResponse response) { Match match = Regex.Match(response.Message, weatherCommand); if (match.Success) { string zip = match.Groups["zipCode"].Value; string strToWrite = await this.reporter.QueryWeather(zip); writer.SendCommandToChannel(strToWrite); } }
/// <summary> /// Handles the query command. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private async void HandleQueryCommand(IIrcWriter writer, IrcResponse response) { Match match = Regex.Match(response.Message, this.config.QueryCommand); if (match.Success) { string userName = match.Groups["name"].Value; int karma = await this.dataBase.QueryKarma(userName); writer.SendMessageToUser(userName + " has " + karma + " karma.", response.Channel); } }
// ---------------- Constructor ---------------- public ChaskisEventHandlerLineActionArgs( string pluginName, IDictionary <string, string> eventArgs, IDictionary <string, string> passThroughArgs, IIrcWriter ircWriter ) { this.PluginName = pluginName; this.EventArgs = eventArgs; this.PassThroughArgs = passThroughArgs; this.IrcWriter = ircWriter; }
/// <summary> /// Handles the decrease command. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private async void HandleDecreaseCommand(IIrcWriter writer, IrcResponse response) { Match match = Regex.Match(response.Message, this.config.DecreaseCommandRegex); if (match.Success) { string userName = match.Groups["name"].Value; int karma = await this.dataBase.DecreaseKarma(userName); writer.SendMessageToUser(userName + " has had their karma decreased to " + karma, response.Channel); } }
// -------- Functions -------- /// <summary> /// Handles the event and the pong back to the server. /// </summary> /// <param name="line">The RAW line from IRC to check.</param> /// <param name="ircConfig">The irc config to use when parsing this line.</param> /// <param name="ircWriter">The way to write to the irc channel.</param> public void HandleEvent(string line, IIrcConfig ircConfig, IIrcWriter ircWriter) { ArgumentChecker.StringIsNotNullOrEmpty(line, nameof(line)); ArgumentChecker.IsNotNull(ircConfig, nameof(ircConfig)); ArgumentChecker.IsNotNull(ircWriter, nameof(ircWriter)); Match match = pattern.Match(line); if (match.Success) { ircWriter.SendPong(match.Groups["response"].Value); } }
// -------- Functions -------- /// <summary> /// Handles the event. /// </summary> /// <param name="line">The RAW line from IRC to check.</param> /// <param name="ircConfig">The irc config to use when parsing this line.</param> /// <param name="ircWriter">The way to write to the irc channel.</param> public void HandleEvent(string line, IIrcConfig ircConfig, IIrcWriter ircWriter) { ArgumentChecker.StringIsNotNullOrEmpty(line, nameof(line)); ArgumentChecker.IsNotNull(ircConfig, nameof(ircConfig)); ArgumentChecker.IsNotNull(ircWriter, nameof(ircWriter)); IrcResponse response = new IrcResponse( string.Empty, string.Empty, line ); this.AllAction(ircWriter, response); }
/// <summary> /// Handles the end-of-names response from the server. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private void HandleEndOfNamesResponse(IIrcWriter writer, IrcResponse response) { Tuple <string, string> userList = this.userList.CheckAndHandleEndMessage(response.Message); if (userList != null) { if (this.isQueried.ContainsKey(userList.Item1) && this.isQueried[userList.Item1]) { writer.SendMessageToUser( string.Format("Users in {0}: {1}", userList.Item1, userList.Item2), userList.Item1 ); } } }
// ---- Handlers ---- /// <summary> /// Handles the up time command. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private static void HandleUpTimeCmd(IIrcWriter writer, IrcResponse response) { TimeSpan span = DateTime.UtcNow - startTime; string str = string.Format( "I have been running for {0} Day(s), {1} Hour(s), {2} Minute(s), and {3} Second(s).", span.Days, span.Hours, span.Minutes, span.Seconds ); writer.SendCommandToChannel( str ); }
// ---------------- Constructor ---------------- public PartHandlerArgs( IIrcWriter writer, string user, string channel, string reason, Regex regex, Match match ) { this.Writer = writer; this.User = user; this.Channel = channel; this.Reason = reason; this.Regex = regex; this.Match = match; }
/// <summary> /// Handles a message from the channel. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private async void HandleMessage(IIrcWriter writer, IrcResponse response) { string url; if (UrlReader.TryParseUrl(response.Message, out url)) { StringBuilder builder = new StringBuilder(); builder.AppendFormat("@{0}: Here's a description of that URL: ", response.RemoteUser); builder.Append(await this.urlReader.GetDescription(url)); writer.SendMessageToUser( builder.ToString(), response.Channel ); } }
// ---------------- Constructor ---------------- protected BasePrivateMessageHandlerArgs( IIrcWriter writer, string user, string channel, string message, Regex regex, Match match ) { this.Writer = writer; this.User = user; this.Channel = channel; this.Message = message; this.Regex = regex; this.Match = match; }
// ---------------- Constructor ---------------- public KickHandlerArgs( IIrcWriter writer, string userWhoSentKick, string channel, string userWhoWasKicked, string reason, Regex regex, Match match ) { this.Writer = writer; this.UserWhoSentKick = userWhoSentKick; this.Channel = channel; this.UserWhoWasKicked = userWhoWasKicked; this.Reason = reason; this.Regex = regex; this.Match = match; }
/// <summary> /// Handles the cowsay command. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private void HandleCowsayCommand(IIrcWriter writer, IrcResponse response) { try { Match cowMatch = Regex.Match(response.Message, this.cowsayRegex); if (cowMatch.Success) { string cowFile = this.cowSayConfig.CowFileInfoList.CommandList[cowMatch.Groups["command"].Value]; if (cowFile == "DEFAULT") { cowFile = null; } string cowSayedMessage; int exitCode = LaunchCowsay(cowMatch.Groups["msg"].Value, out cowSayedMessage, cowFile); if ((string.IsNullOrEmpty(cowSayedMessage) == false) && (exitCode == 0)) { writer.SendCommandToChannel(cowSayedMessage); } else if (exitCode != 0) { Console.Error.WriteLine("CowSayBot: Exit code not 0. Got: " + exitCode); } else if (string.IsNullOrEmpty(cowSayedMessage)) { Console.Error.WriteLine("CowSayBot: Nothing returned from cowsay process."); } } else { Console.Error.WriteLine("CowSayBot: Saw unknown line:" + response.Message); } } catch (Exception e) { Console.Error.WriteLine("*********************"); Console.Error.WriteLine("CowSayBot: Caught Exception:"); Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); Console.Error.WriteLine("**********************"); } }
public static ConnectedEventArgs FromXml(string xmlString, IIrcWriter writer) { XElement root = XElement.Parse(xmlString); string server = null; ChaskisEventProtocol?protocol = null; if (XmlElementName.EqualsIgnoreCase(root.Name.LocalName) == false) { throw new ValidationException( $"Invalid XML root name: {XmlElementName} for {nameof( ConnectedEventArgs )}" ); } foreach (XElement child in root.Elements()) { if ("server".EqualsIgnoreCase(child.Name.LocalName)) { server = child.Value; } else if ("protocol".EqualsIgnoreCase(child.Name.LocalName)) { if (Enum.TryParse <ChaskisEventProtocol>(child.Value, out ChaskisEventProtocol foundProtocol)) { protocol = foundProtocol; } } } if ((server != null) && (protocol != null)) { return(new ConnectedEventArgs(server, protocol.Value, writer)); } else { throw new ValidationException( $"Could not find all required properties when creating {nameof( ConnectedEventArgs )}" ); } }
/// <summary> /// Handles the source command, which returns the source code of the plugin. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private void HandleSourceCommand(IIrcWriter writer, IrcResponse response) { Match match = Regex.Match(response.Message, this.sourceCommand, RegexOptions.IgnoreCase); if (match.Success) { string pluginName = match.Groups["pluginName"].Value.ToLower(); if (this.plugins.ContainsKey(pluginName)) { string msg = "Source of the plugin " + pluginName + ": " + this.plugins[pluginName].SourceCodeLocation; writer.SendMessageToUser(msg, response.Channel); } else if (pluginName == "chaskis") { string msg = "My source code is located: https://github.com/xforever1313/Chaskis"; writer.SendMessageToUser(msg, response.Channel); } else { writer.SendMessageToUser(pluginName + " is not a plugin I have loaded...", response.Channel); } } }
// ---------------- Constructor ---------------- public AllHandlerArgs(IIrcWriter writer, string line) { this.Writer = writer; this.Line = line; }
// -------- Default Handlers -------- /// <summary> /// Handles the "list plugin" command. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private void HandlePluginListCommand(IIrcWriter writer, IrcResponse response) { writer.SendMessageToUser(this.pluginListResponse, response.Channel); }
// -------- Test Helpers -------- /// <summary> /// The function that is called /// </summary> /// <param name="writer">The writer that can be written to.</param> /// <param name="response">The response from the server.</param> private static void PartFunction(IIrcWriter writer, IrcResponse response) { Assert.AreSame(ircConnection, writer); responseReceived = response; }
/// <summary> /// Handles the names response from the server. /// Adds the names to the list. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private void HandleNamesResponse(IIrcWriter writer, IrcResponse response) { this.userList.ParseNameResponse(response.Message); }
/// <summary> /// Handles the get users command, which queries the server for the user list. /// </summary> /// <param name="writer">The IRC Writer to write to.</param> /// <param name="response">The response from the channel.</param> private void HandleGetUsersCommand(IIrcWriter writer, IrcResponse response) { writer.SendRawCmd("NAMES " + this.ircConfig.Channel); this.isQueried[response.Channel] = true; }