private void OnBotCommandMessage(InvokeCommandMessage message) { string commandName = message.Command; if (!scripts.ContainsKey(commandName)) { var matches = scripts.Keys.Where(x => x.StartsWith(commandName)).ToList(); if (matches.Any()) { commandName = matches.First(); } else { log.Debug("Command not found"); return; } } if (!string.IsNullOrWhiteSpace(commandName) && scripts.ContainsKey(commandName)) { string args = ""; if (message.IrcEventArgs.Data.MessageArray.Length > 1) args = string.Join(" ", message.IrcEventArgs.Data.MessageArray.Skip(1)); args = args.Replace("\'", ""); args = args.Replace(">", ""); args = args.Replace("<", ""); args = args.Replace("|", ""); try { var proc = new System.Diagnostics.Process(); proc.EnableRaisingEvents = false; proc.StartInfo.FileName = @"python"; proc.StartInfo.Arguments = scripts[commandName] + " \"" + args + "\""; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory(); proc.StartInfo.UseShellExecute = false; proc.StartInfo.StandardOutputEncoding = Encoding.UTF8; proc.Start(); string data = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); message.IrcEventArgs.Data.Irc.SendMessage(Meebey.SmartIrc4net.SendType.Message, message.IrcEventArgs.Data.Channel, data); } catch (Exception ex) { log.Debug(ex); } } }
private void OnBotCommandMessage(InvokeCommandMessage message) { if (message.Command == "reload") { LoadScripts("Scripts"); return; } try { var fn = clojure.lang.RT.var("bot.plugin", message.Command); if (!fn.isBound) return; object result = null; if (message.IrcEventArgs.Data.MessageArray.Length > 1) { result = fn.invoke(string.Join(" ", message.IrcEventArgs.Data.MessageArray.Skip(1))); } else { result = fn.invoke(); } var lines = result as IEnumerable<object>; if (lines != null) { foreach (var line in lines) { hub.Publish<IrcSendMessage>(new IrcSendMessage(this, Meebey.SmartIrc4net.SendType.Message, message.IrcEventArgs.Data.Irc.Address, message.IrcEventArgs.Data.Channel, line.ToString())); } } else { hub.Publish<IrcSendMessage>(new IrcSendMessage(this, Meebey.SmartIrc4net.SendType.Message, message.IrcEventArgs.Data.Irc.Address, message.IrcEventArgs.Data.Channel, result.ToString())); } } catch (Exception e) { log.Error(e); } }
private void OnBotCommandMessage(InvokeCommandMessage message) { string commandName = message.Command.Substring(1); try { string args = ""; if (message.IrcEventArgs.Data.MessageArray.Length > 1) args = string.Join(" ", message.IrcEventArgs.Data.MessageArray.Skip(1)); PyString param = new PyString(args); IntPtr ptr = PythonEngine.AcquireLock(); module.SetAttr(new PyString("sys.argv"), new PyString("param")); PyObject result = module.InvokeMethod(commandName, param); PythonEngine.ReleaseLock(ptr); hub.Publish<IrcSendMessage>(new IrcSendMessage(this, Meebey.SmartIrc4net.SendType.Message, message.IrcEventArgs.Data.Irc.Address, message.IrcEventArgs.Data.Channel, result.ToString())); } catch (Exception ex) { log.Debug("Could not invoke Python function", ex); } }
private void InvokeCommand(string commandName, InvokeCommandMessage message) { ICommand command = commands[commandName]; if (command.Async) { var worker = new Execute(command.Execute); var callback = new AsyncCallback(CommandCompletedCallback); AsyncOperation async = AsyncOperationManager.CreateOperation(message); log.DebugFormat("Starting asynchronous exeucution of \"{0}\"", message.Command); worker.BeginInvoke(message.IrcEventArgs, callback, async); } else { IEnumerable<string> response = commands[commandName].Execute(message.IrcEventArgs); log.DebugFormat("Synchronous execution of \"{0}\" complete. Sending response...", message.Command); if (response != null && response.Any()) SendIrcResponse(message, response); } }
private void SendIrcResponse(InvokeCommandMessage message, IEnumerable<string> response) { if (message.IrcEventArgs.Data.Type == ReceiveType.ChannelMessage) SendIrcResponse(SendType.Message, message.IrcEventArgs.Data.Irc.Address, message.IrcEventArgs.Data.Channel, response); else if (message.IrcEventArgs.Data.Type == ReceiveType.QueryNotice) SendIrcResponse(SendType.Notice, message.IrcEventArgs.Data.Irc.Address, message.IrcEventArgs.Data.Nick, response); }
private void OnBotCommandMessage(InvokeCommandMessage message) { if (message.Command == "reload") { log.Debug("Reloading commands..."); LoadCommands(); return; } string commandName = message.Command; if (!commands.ContainsKey(commandName)) { var matches = commands.Keys.Where(x => x.StartsWith(commandName)).ToList(); if (matches.Count() == 1) { commandName = matches.First(); } else if (matches.Count() > 1) { string response = "Did you mean " + matches.Aggregate( (sentence, x) => (x == matches.Last() ? sentence + " or " + x : sentence + ", " + x) ); SendIrcResponse(message, new [] {response}); } } if (!string.IsNullOrWhiteSpace(commandName) && commands.ContainsKey(commandName)) { InvokeCommand(commandName, message); } }