예제 #1
0
파일: IrcBot.cs 프로젝트: SplittyDev/DaBawd
 public IrcBot LoadScript(string path)
 {
     using (var file = File.OpenRead (path))
     using (var reader = new StreamReader (file)) {
         Console.WriteLine ("Loading script: {0}", file.Name);
         try {
             Console.WriteLine ("Creating lua engine");
             var engine = new IrcScriptEngine ();
             Console.WriteLine ("Loading script source");
             var name = Path.GetFileNameWithoutExtension (file.Name);
             engine.LoadSource (reader.ReadToEnd (), name);
             Console.WriteLine ("Registering script");
             ScriptEngines.Add (name, engine);
         } catch (Exception e) {
             Console.WriteLine ("Error: {0}", e.Message);
             Client.PRIVMSG (Client.Channels.First (), e.Message);
         }
     }
     return this;
 }
예제 #2
0
        public MainClass(IrcOptions options)
        {
            Options = options;
            Bot = new IrcBot ();
            API = new IrcApi ();
            ScriptActions = new Queue<ScriptAction> ();
            ScriptDir = "../../scripts";

            #region API
            API.Register ("test", (args => "test"));
            API.Register ("loadscript", (args => {
                ScriptActions.Enqueue (new ScriptAction ((string) args [0], ScriptActionEnum.Load));
                return null;
            }));
            API.Register ("loadscripts", (args => {
                ScriptActions.Enqueue (new ScriptAction (null, ScriptActionEnum.LoadAll));
                return null;
            }));
            API.Register ("reloadscript", (args => {
                ScriptActions.Enqueue (new ScriptAction ((string) args [0], ScriptActionEnum.Reload));
                return null;
            }));
            API.Register ("reloadscripts", (args => {
                ScriptActions.Enqueue (new ScriptAction (null, ScriptActionEnum.ReloadAll));
                return null;
            }));
            API.Register ("unloadscript", (args => {
                ScriptActions.Enqueue (new ScriptAction ((string) args [0], ScriptActionEnum.Unload));
                return null;
            }));
            API.Register ("sendmsg", (args => {
                Bot.Client.Message ((string) args [1], (string) args [0]);
                return null;
            }));
            API.Register ("listscripts", (args => {
                var files = Directory.GetFiles (ScriptDir, "*.lua", SearchOption.AllDirectories);
                return string.Join (" ", files.Select (f => Path.GetFileNameWithoutExtension (f)).ToArray ());
            }));
            API.Register ("callscript", (args => {
                var script = (string) args [0];
                var engine = Bot.ScriptEngines.FirstOrDefault (e => e.Key == script);
                return engine.Value != null
                    ? TryCall (engine.Value, Bot.Client.Channels.First (), (string)args [1], args.Skip (2).ToArray<object> ())
                    : null;
            }));
            API.Register ("eval", (args => {
                var statements = string.Join (" ", args);
                if (statements.Contains ("os.execute"))
                    return "(nil): Don't you dare to this to me";
                statements = string.Format ("function entry (api) return ({0}) end", statements);
                string result;
                using (var temp = new IrcScriptEngine ()) {
                    try {
                        var task = Task.Factory.StartNew<string> (() => {
                            temp.LoadSource (statements, "__temp.lua");
                            return temp.Call ("entry", API).ToString ();
                        });
                        task.Wait (3000);
                        result = task.IsCompleted ? task.Result : "(nil): Task didn't complete in time";
                    } catch (Exception e) {
                        result = string.Format ("(nil): {0}", e.Message);
                    }
                }
                result = result.Trim ('{', '}', ' ');
                return result == string.Empty ? "(nil)" : result;
            }));
            #endregion
        }
예제 #3
0
 object TryCall(IrcScriptEngine engine, string channel, string func, params object[] args)
 {
     object result = null;
     try {
         result = engine.Call (func, args);
     } catch (Exception e) {
         Log ("[{0}] {1}", e.GetType ().Name, e.Message);
         Bot.Client.PRIVMSG (channel, string.Format ("[{0}] {1}", e.GetType ().Name, e.Message));
     }
     return result;
 }