/// <summary> /// Creates the session. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> public void CreateSession(User user, string channel) { if (!this.interactiveSessions.Contains(user.Nick)) { var ironRuby = CreateProcess(); this.AddUserToSession(user, ironRuby); ironRuby.Start(); this.ReadFromSession(ironRuby, user, channel); } }
/// <summary> /// Called when [user left]. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> public virtual void OnUserLeft(User user, string channel) { }
private void StopCommand(User user, string channel) { }
private void JoinCommand(User user, string channel) { }
/// <summary> /// Lists the admins. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void ListAdmins(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> arguments) { var admins = this.userService.ListAdmins().Select(a => string.Format("{0} : {1}", a.Nick, a.Email)); foreach (var admin in admins) { var response = new Response(admin, new[] { channel ?? user.Nick }, MessageFormat.Message, MessageType.Both); this.IrcClient.SendResponse(response); } }
/// <summary> /// Interactive ruby. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="args">The arguments.</param> private void InteractiveRuby(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> args) { message = message.Remove(0, "!irb".Length); if (args.ContainsKey("start")) { this.irbService.CreateSession(user, channel); } else if (args.ContainsKey("stop")) { this.irbService.RemoveSession(user); } else if (!string.IsNullOrEmpty(message) && this.interactiveSessions.Contains(user.Nick)) { this.irbService.WriteToSession(user, message); } }
/// <summary> /// Called when [public message]. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="message">The message.</param> public override void OnPublicMessage(User user, string channel, string message) { foreach (var module in this.instancedModules) { try { module.on_public_message(user, channel, message); } catch (Exception e) { Trace.TraceError(e.Message); } } }
/// <summary> /// Gets the weather. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void GetWeather(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> arguments) { var targets = new[] { channel ?? user.Nick }; IEnumerable<IResponse> weatherResponses = this.weatherProvider.GetWeather(targets, messageFormat, messageType, message, arguments); foreach (var weatherResponse in weatherResponses) { this.SendResponse(weatherResponse); } }
/// <summary> /// Reads from session. /// </summary> /// <param name="irb">The irb.</param> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> private void ReadFromSession(Process irb, User user, string channel) { // Read all standard output Task.Factory.StartNew(() => { while (!irb.StandardError.EndOfStream) { string line = irb.StandardError.ReadLine(); var response = new Response(line, new[] { channel ?? user.Nick }, MessageFormat.Message, MessageType.Both); this.ircClient.SendResponse(response); } }); // Read all error output Task.Factory.StartNew(() => { while (!irb.StandardOutput.EndOfStream) { string line = irb.StandardOutput.ReadLine(); var response = new Response(line, new[] { channel ?? user.Nick }, MessageFormat.Message, MessageType.Both); this.ircClient.SendResponse(response); } }); }
/// <summary> /// Adds the user to session. /// </summary> /// <param name="user">The user.</param> /// <param name="ironRuby">The iron ruby.</param> private void AddUserToSession(User user, Process ironRuby) { var item = new CacheItem(user.Nick, ironRuby); var policy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(5), RemovedCallback = this.KillOnRemoval }; this.interactiveSessions.Add(item, policy); }
/// <summary> /// Writes to session. /// </summary> /// <param name="user">The user.</param> /// <param name="message">The message.</param> public void WriteToSession(User user, string message) { var process = this.interactiveSessions[user.Nick] as Process; if (process != null) { process.StandardInput.WriteLine(message); } }
/// <summary> /// Sends the module commands. /// </summary> /// <param name="user">The user.</param> /// <param name="messageType">Type of the message.</param> /// <param name="module">The module.</param> /// <param name="targets">The targets.</param> private void SendModuleCommands(User user, MessageType messageType, IModule module, string[] targets) { var response = new Response("{0} has the following commands: ".FormatWith(module.Name), targets, MessageFormat.Notice, messageType); this.SendResponse(response); // For each possible command where the command is accessible foreach (var command in module.Commands.Where(c => c.LevelRequired <= user.AccessLevel)) { response = new Response("{0}: {1}".FormatWith(command.Trigger, command.Description), targets, MessageFormat.Notice, messageType); this.SendResponse(response); response.Message = string.Empty; foreach (var argument in command.KnownArguments) { response.Message += "{0}{1} {2}".FormatWith(command.ArgumentSplitter, argument.Key, argument.Value); } this.SendResponse(response); this.SendCommandUsageExamples(module, targets, messageType, MessageFormat.Notice); } }
/// <summary> /// Lists the modules. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void ListModules(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> arguments) { var targets = new[] { user.Nick }; if (arguments.None()) { this.SendCommandUsageExamples(this, targets, messageType, messageFormat); } foreach (var key in arguments.Keys) { AccessLevel level; if (Enum.TryParse(key, ignoreCase: true, result: out level)) { foreach (var module in this.IrcClient.Modules.Where(m => m.Commands.Any(c => c.LevelRequired <= level && c.LevelRequired <= user.AccessLevel))) { var moduleInfoResponse = new Response("{0}: {1}".FormatWith(module.Name, module.Description), targets, MessageFormat.Notice, messageType); this.SendResponse(moduleInfoResponse); } } } if (arguments.ContainsKey("list")) { #region Send all Module descriptions foreach (var module in this.IrcClient.Modules.Where(m => m.Commands.Any(c => c.LevelRequired <= user.AccessLevel))) { var moduleInfoResponse = new Response("{0}: {1}".FormatWith(module.Name, module.Description), targets, MessageFormat.Notice, messageType); this.SendResponse(moduleInfoResponse); } #endregion this.GetMoreInformation(messageType, targets); } if (arguments.ContainsKey("examples")) { // For each module where the module has a command that is accessible by this user foreach (var module in this.IrcClient.Modules.Where(m => m.Commands.Any(c => c.LevelRequired <= user.AccessLevel))) { #region Module Description var response = new Response("{0}: {1}".FormatWith(module.Name, module.Description), targets, MessageFormat.Notice, messageType); this.SendResponse(response); #endregion #region Usage Examples response.Message = "Usage Examples: "; this.SendResponse(response); this.SendCommandUsageExamples(module, targets, messageType, MessageFormat.Notice); #endregion } } }
/// <summary> /// Lists the commands of a given module. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void ListCommands(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> arguments) { var targets = new[] { user.Nick }; foreach (var key in arguments.Keys) { var module = this.IrcClient.Modules.FirstOrDefault(m => UserHasAccess(user, m) && m.Name.Equals(key, StringComparison.OrdinalIgnoreCase)); if (module != null) { this.SendModuleCommands(user, messageType, module, targets); } } }
/// <summary> /// Users the has access. /// </summary> /// <param name="user">The user.</param> /// <param name="module">The module.</param> /// <returns> /// True if the user has access to a command in the module, otherwise false. /// </returns> private static bool UserHasAccess(User user, IModule module) { return module.Commands.Any(c => c.LevelRequired <= user.AccessLevel); }
/// <summary> /// Called when [private message received]. /// </summary> /// <param name="user"> /// The user. /// </param> /// <param name="message"> /// The message. /// </param> private void OnPrivateMessageReceived(User user, string message) { foreach (var module in this.Modules) { module.OnPrivateMessage(user, message); } }
/// <summary> /// Called when [user left]. /// </summary> /// <param name="user"> /// The user. /// </param> /// <param name="channel"> /// The channel. /// </param> private void OnUserLeft(User user, string channel) { foreach (var module in this.Modules) { module.OnUserLeft(user, channel); } }
/// <summary> /// Removes the session. /// </summary> /// <param name="user">The user.</param> public void RemoveSession(User user) { if (this.interactiveSessions.Contains(user.Nick)) { this.interactiveSessions.Remove(user.Nick); } }
/// <summary> /// Called when [notice]. /// </summary> /// <param name="user">The user.</param> /// <param name="notice">The notice.</param> public override void OnNotice(User user, string notice) { foreach (var module in this.instancedModules) { try { module.on_notice(user, notice); } catch (Exception e) { Trace.TraceError(e.Message); } } }
/// <summary> /// Creates the urban message. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="task">The task.</param> private void SendUrbanResponse(User user, string channel, MessageType messageType, MessageFormat messageFormat, Task<IUrbanResponse> task) { if (task.Result.Results.Any()) { var targets = new[] { channel ?? user.Nick }; IResult result = task.Result.Results.First(); Response response = new Response(result.Definition, targets, messageFormat, messageType); this.SendResponse(response); } }
/// <summary> /// Called when [user left]. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> public override void OnUserLeft(User user, string channel) { foreach (var module in this.instancedModules) { try { module.on_user_left(user, channel); } catch (Exception e) { Trace.TraceError(e.Message); } } }
/// <summary> /// Urban Dictionary command. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void UrbanCommand(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> arguments) { message = message.TrimStart("!urban".ToCharArray()).Trim(); this.urbanService.GetResultsAsync(message).ContinueWith(task => this.SendUrbanResponse(user, channel, messageType, messageFormat, task)); }
/// <summary> /// Joins the channel. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void JoinChannel(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> arguments) { if (arguments.ContainsKey("channel")) { if (arguments.ContainsKey("key")) { this.IrcClient.Join(arguments["channel"], arguments["key"]); } else { this.IrcClient.Join(arguments["channel"]); } } }
/// <summary> /// Called when [roll]. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="type">The type.</param> /// <param name="format">The format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void OnRoll(User user, string channel, MessageType type, MessageFormat format, string message, Dictionary<string, string> arguments) { Match match = DiceRegex.Match(message); if (match.Success) { var group = match.Groups["times"]; if (group.Success) { int value = int.Parse(group.Value); var results = new List<int>(); value.Times(() => results.Add(this.random.Next(1, 6))); var response = new Response(string.Join(", ", results), new [] { channel ?? user.Nick }, MessageFormat.Message, MessageType.Both); response.MessageType = MessageType.Both; this.SendResponse(response); } else { var response = new Response(string.Join(", ", this.random.Next(1, 6)), new[] { channel ?? user.Nick }, MessageFormat.Message, MessageType.Both); response.MessageType = MessageType.Both; this.SendResponse(response); } } }
private void HitCommand(User user, string channel) { }
/// <summary> /// Called when [public message received]. /// </summary> /// <param name="user"> /// The user. /// </param> /// <param name="channel"> /// The channel. /// </param> /// <param name="message"> /// The message. /// </param> public void OnPublicMessageReceived(User user, string channel, string message) { foreach (var module in this.Modules) { module.OnPublicMessage(user, channel, message); } }
private void StartCommand(User user, string channel) { BlackjackService service = this.GetOrCreateBlackjackService(channel); }
/// <summary> /// Called when [notice received]. /// </summary> /// <param name="source"> /// The source. /// </param> /// <param name="notice"> /// The notice. /// </param> private void OnNoticeReceived(User source, string notice) { foreach (var module in this.Modules) { module.OnNotice(source, notice); } }
/// <summary> /// Shortens the URL. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="messageType">Type of the message.</param> /// <param name="messageFormat">The message format.</param> /// <param name="message">The message.</param> /// <param name="arguments">The arguments.</param> private void ShortenUrl(User user, string channel, MessageType messageType, MessageFormat messageFormat, string message, Dictionary<string, string> arguments) { foreach (IUrlShortenerProvider provider in this.providers) { if(arguments.ContainsKey(provider.Trigger)) { try { var url = UrlRegex.Match(message).Groups["url"].Value; string shortUrl; shortUrl = this.GetShortUrl(url, provider); var response = new Response(shortUrl, new[] { channel ?? user.Nick }, messageFormat, messageType); this.SendResponse(response); } catch (Exception e) { Trace.TraceError(e.Source); Trace.TraceError(e.Message); } } } }
/// <summary> /// Called when [user kicked]. /// </summary> /// <param name="user">The user.</param> /// <param name="channel">The channel.</param> /// <param name="message">The message.</param> public virtual void OnUserKicked(User user, string channel, string message) { }