예제 #1
0
 protected virtual void LoadCommandsList()
 {
     foreach (var com in GetAPICommandClasses())
     {
         if (com != null)
         {
             if (com.IsAbstract == false)
             {
                 API_interface D = (API_interface)Activator.CreateInstance(com);
                 if (D != null)
                 {
                     if (subtype_map.ContainsKey(D.CommandName) == false)
                     {
                         string worknamespace = com.Namespace;
                         worknamespace = worknamespace.Replace("BSB.Commands.", "");
                         worknamespace = worknamespace.Replace("BSB.RLV.", "");
                         worknamespace = worknamespace.Replace("BetterSecondBot.HttpServer.View.", "");
                         worknamespace = worknamespace.Replace("BetterSecondBot.HttpServer.Control.", "");
                         worknamespace = worknamespace.Replace("CMD_", "");
                         subtype_map.Add(D.CommandName, D.GetType());
                         subtype_workspace_map.Add(D.CommandName, worknamespace);
                     }
                     else
                     {
                         ConsoleLog.Crit("[CMD] command: " + D.CommandName + " already defined!");
                     }
                 }
             }
         }
     }
 }
예제 #2
0
        public bool SendNotecard(string name, string content, UUID sendToUUID)
        {
            bool returnstatus = true;

            name = name + " " + DateTime.Now;
            Client.Inventory.RequestCreateItem(
                Client.Inventory.FindFolderForType(AssetType.Notecard),
                name,
                name + " Created via SecondBot notecard API",
                AssetType.Notecard,
                UUID.Random(),
                InventoryType.Notecard,
                PermissionMask.All,
                (bool Success, InventoryItem item) =>
            {
                if (Success)
                {
                    AssetNotecard empty = new AssetNotecard {
                        BodyText = "\n"
                    };
                    empty.Encode();
                    Client.Inventory.RequestUploadNotecardAsset(empty.AssetData, item.UUID,
                                                                (bool emptySuccess, string emptyStatus, UUID emptyItemID, UUID emptyAssetID) =>
                    {
                        if (emptySuccess)
                        {
                            empty.BodyText = content;
                            empty.Encode();
                            Client.Inventory.RequestUploadNotecardAsset(empty.AssetData, emptyItemID,
                                                                        (bool finalSuccess, string finalStatus, UUID finalItemID, UUID finalID) =>
                            {
                                if (finalSuccess)
                                {
                                    Client.Inventory.GiveItem(finalItemID, name, AssetType.Notecard, sendToUUID, false);
                                }
                                else
                                {
                                    returnstatus = false;
                                    ConsoleLog.Warn("Unable to request notecard upload");
                                }
                            });
                        }
                        else
                        {
                            ConsoleLog.Crit("The f**k empty success notecard create");
                            returnstatus = false;
                        }
                    });
                }
                else
                {
                    ConsoleLog.Warn("Unable to find default notecards folder");
                    returnstatus = false;
                }
            }
                );
            return(returnstatus);
        }
예제 #3
0
        public override bool CallFunction(string[] args)
        {
            if (base.CallFunction(args) == true)
            {
                List <string> acceptablewords          = new List <string>();
                Dictionary <string, ParcelFlags> flags = parcel_static.get_flags_list();
                acceptablewords.AddRange(new[] { "True", "False" });

                Dictionary <string, bool> setflags = new Dictionary <string, bool>();
                foreach (string a in args)
                {
                    string[] parts = a.Split('=');
                    if (flags.ContainsKey(parts[0]) == true)
                    {
                        if (acceptablewords.Contains(parts[1]))
                        {
                            setflags.Add(parts[0], Convert.ToBoolean(parts[1]));
                        }
                        else
                        {
                            ConsoleLog.Crit("Unable to set flag " + parts[0] + " to : " + parts[1] + "");
                        }
                    }
                    else
                    {
                        ConsoleLog.Warn("Flag: " + parts[0] + " is unknown");
                    }
                }
                if (setflags.Count > 0)
                {
                    int    localid = bot.GetClient.Parcels.GetParcelLocalID(bot.GetClient.Network.CurrentSim, bot.GetClient.Self.SimPosition);
                    Parcel p       = bot.GetClient.Network.CurrentSim.Parcels[localid];
                    if (p.OwnerID != bot.GetClient.Self.AgentID)
                    {
                        if (p.IsGroupOwned)
                        {
                            bot.GetClient.Groups.ActivateGroup(p.GroupID);
                        }
                    }
                    foreach (KeyValuePair <string, bool> cfg in setflags)
                    {
                        if (flags.ContainsKey(cfg.Key) == true)
                        {
                            Dosetflag(flags[cfg.Key], p, cfg.Value);
                        }
                    }
                    p.Update(bot.GetClient.Network.CurrentSim, false);
                    return(true);
                }
                else
                {
                    return(Failed("No accepted flags"));
                }
            }
            return(false);
        }
예제 #4
0
        public bool SendScript(string name, string content, UUID sendToUUID)
        {
            bool returnstatus = true;

            Client.Inventory.RequestCreateItem(
                Client.Inventory.FindFolderForType(AssetType.LSLText),
                name,
                name,
                AssetType.LSLText,
                UUID.Random(),
                InventoryType.LSL,
                PermissionMask.All,
                (bool Success, InventoryItem item) =>
            {
                if (Success)
                {
                    AssetScriptText empty = new AssetScriptText {
                        Source = "\n"
                    };
                    empty.Encode();
                    Client.Inventory.RequestUpdateScriptAgentInventory(new byte[] { }, item.UUID, true,
                                                                       (bool uploadSuccess, string uploadStatus, bool compileSuccess, List <string> compileMessages, UUID itemID, UUID assetID) =>
                    {
                        if (uploadSuccess)
                        {
                            empty.AssetData = Encoding.ASCII.GetBytes(content);
                            Client.Inventory.RequestUpdateScriptAgentInventory(empty.AssetData, itemID, true,
                                                                               (bool finaluploadSuccess, string finaluploadStatus, bool finalcompileSuccess, List <string> finalcompileMessages, UUID finalitemID, UUID finalassetID) =>
                            {
                                if (finalcompileSuccess == false)
                                {
                                    ConsoleLog.Warn("Script failed to compile, sending anyway.");
                                }
                                Client.Inventory.GiveItem(finalitemID, name, AssetType.LSLText, sendToUUID, false);
                            });
                        }
                        else
                        {
                            ConsoleLog.Crit("The f**k empty success Script create");
                            returnstatus = false;
                        }
                    });
                }
                else
                {
                    ConsoleLog.Warn("Unable to find default Scripts folder");
                    returnstatus = false;
                }
            }
                );
            return(returnstatus);
        }
예제 #5
0
        protected void OpenHttpService()
        {
            post_controler = new HTTPCommandsInterface(Bot, this);
            Thread t = new Thread(delegate(object _)
            {
                bool ok = true;
                try
                {
                    listener = new HttpListener();
                    listener.Prefixes.Add(url);
                    listener.Start();
                }
                catch (Exception e)
                {
                    ok = false;
                    ConsoleLog.Crit("Unable to setup http service: " + e.Message + "");
                    ConsoleLog.Crit("if running on windows please check: https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/configuring-http-and-https?redirectedfrom=MSDN");
                }
                if (ok == true)
                {
                    try
                    {
                        ConsoleLog.Status("Listening for connections on " + url + "");
                        Task listenTask = HandleIncomingConnections();
                        listenTask.GetAwaiter().GetResult();
                    }
                    catch (Exception e)
                    {
                        ConsoleLog.Status("Http interface killed itself: " + e.ToString() + "");
                    }
                }
                listener.Close();
            })
            {
                Name         = "http thead",
                IsBackground = true
            };

            t.Start();
        }
예제 #6
0
        public override void Callback(string[] args, EventArgs e)
        {
            GroupMembersReplyEventArgs reply = (GroupMembersReplyEventArgs)e;

            if (UUID.TryParse(args[0], out UUID target_group) == true)
            {
                if (UUID.TryParse(args[1], out UUID target_avatar) == true)
                {
                    if (reply.GroupID == target_group)
                    {
                        if (reply.Members.ContainsKey(target_avatar) == false)
                        {
                            ForRealGroupInvite(args, target_group, target_avatar);
                        }
                    }
                    else
                    {
                        ConsoleLog.Crit("Callback sent for the wrong group as part of group invite!");
                    }
                }
            }
        }
예제 #7
0
 protected IEnumerable <Type> GetAPICommandClasses()
 {
     if (API_type != null)
     {
         return(AppDomain.CurrentDomain.GetAssemblies().Where(Ass => {
             bool reply = false;
             if (Ass.FullName.StartsWith("BSB") == true)
             {
                 reply = true;
             }
             else if (Ass.FullName.StartsWith("BetterSecondBot") == true)
             {
                 reply = true;
             }
             return reply;
         }).SelectMany(assembly => assembly.GetTypes()).Where(type => type.IsSubclassOf(API_type)));
     }
     else
     {
         ConsoleLog.Crit(this.GetType() + " Attempted to call GetSubTypes without setting API_TYPE first!");
     }
     return(null);
 }
예제 #8
0
        public bool SmartCommandReply(bool run_status, string target, string output, string command, Dictionary <string, string> collection)
        {
            string mode           = "CHAT";
            UUID   target_avatar  = UUID.Zero;
            int    target_channel = 0;

            if (target.StartsWith("http://"))
            {
                mode = "HTTP";
            }
            else if (UUID.TryParse(target, out target_avatar) == true)
            {
                mode = "IM";
            }
            else
            {
                if (int.TryParse(target, out target_channel) == false)
                {
                    run_status = false;
                }
            }

            if (collection != null)
            {
                var reply_object = new CoreCommand_json_reply_wit_collection();
                reply_object.status     = run_status;
                reply_object.command    = command;
                reply_object.output     = output;
                reply_object.collection = collection;
                output = JsonConvert.SerializeObject(reply_object);
            }
            else
            {
                var reply_object = new CoreCommand_json_reply();
                reply_object.status  = run_status;
                reply_object.command = command;
                reply_object.output  = output;
                output = JsonConvert.SerializeObject(reply_object);
            }

            if (mode == "CHAT")
            {
                if (target_channel >= 0)
                {
                    bot.GetClient.Self.Chat(output, target_channel, ChatType.Normal);
                }
                else
                {
                    run_status = false;
                    ConsoleLog.Crit("[SmartReply] output Channel must be zero or higher");
                }
            }
            else if (mode == "IM")
            {
                bot.GetClient.Self.InstantMessage(target_avatar, output);
            }
            else if (mode == "HTTP")
            {
                Dictionary <string, string> values = new Dictionary <string, string>
                {
                    { "reply", output },
                };

                var content = new FormUrlEncodedContent(values);
                try
                {
                    HTTPclient.PostAsync(target, content);
                }
                catch (Exception e)
                {
                    run_status = false;
                    ConsoleLog.Crit("[SmartReply] HTTP failed: " + e.Message + "");
                }
            }
            else
            {
                ConsoleLog.Crit("[SmartReply] Unsupported output target");
                return(false);
            }
            return(run_status);
        }
예제 #9
0
        public CliHardware(string[] args)
        {
            SimpleIO   io = new SimpleIO();
            JsonConfig Config;
            string     json_file = "";

#if DEBUG
            ConsoleLog.Status("Hardware/Debug version");
            json_file = "debug.json";
#else
            ConsoleLog.Status("Hardware/Live version");
            Parser.Default.ParseArguments <Options>(args).WithParsed <Options>(o =>
            {
                json_file = o.Json;
            });
#endif
            if (SimpleIO.dir_exists("wiki") == false)
            {
                ConsoleLog.Info("Basic Wiki [Creating]");
                new DebugModeCreateWiki(AssemblyInfo.GetGitHash(), io);
                ConsoleLog.Info("Basic Wiki [Ready]");
                io = new SimpleIO();
            }
            Config = MakeJsonConfig.GetDefault();
            if (helpers.notempty(json_file) == false)
            {
                json_file = "mybot.json";
                ConsoleLog.Warn("Using: mybot.json as the config");
            }
            bool ok_to_try_start = false;
            if (SimpleIO.FileType(json_file, "json") == true)
            {
                if (io.Exists(json_file))
                {
                    string json = io.ReadFile(json_file);
                    if (json.Length > 0)
                    {
                        try
                        {
                            Config          = JsonConvert.DeserializeObject <JsonConfig>(json);
                            ok_to_try_start = true;
                        }
                        catch (Exception e)
                        {
                            ConsoleLog.Warn("Unable to read config file\n moving config to " + json_file + ".old and creating a empty config\nError was: " + e.Message + "");
                            io.makeOld(json_file);
                            Config = new JsonConfig();
                            io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                        }
                    }
                    else
                    {
                        ConsoleLog.Warn("Json config is empty creating an empty one for you");
                        io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                    }
                }
                else
                {
                    ConsoleLog.Warn("Json config does not Exist creating it for you");
                    io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                }
            }
            else
            {
                ConsoleLog.Crit("you must select a .json file for config!");
            }
            if (ok_to_try_start == true)
            {
                Config = MakeJsonConfig.http_config_check(Config);
                if (Config.HttpAsCnC == true)
                {
                    new HttpCnC(Config);
                }
                else
                {
                    new CliExitOnLogout(Config);
                }
            }
        }
예제 #10
0
        public bool SmartCommandReply(string target, string output, string command, bool vaildate_only)
        {
            string mode           = "CHAT";
            UUID   target_avatar  = UUID.Zero;
            int    target_channel = 0;

            if (target.StartsWith("http://"))
            {
                mode = "HTTP";
            }
            else if (UUID.TryParse(target, out target_avatar) == true)
            {
                mode = "IM";
            }
            else
            {
                int.TryParse(target, out target_channel);
            }
            if (vaildate_only == false)
            {
                if (mode == "CHAT")
                {
                    bot.GetClient.Self.Chat(output, target_channel, ChatType.Normal);
                }
                else if (mode == "IM")
                {
                    bot.GetClient.Self.InstantMessage(target_avatar, output);
                }
                else if (mode == "HTTP")
                {
                    Dictionary <string, string> values = new Dictionary <string, string>
                    {
                        { "reply", output },
                        { "unixtime", helpers.UnixTimeNow().ToString() }
                    };

                    if (command != "")
                    {
                        values.Add("command", command);
                    }
                    var content = new FormUrlEncodedContent(values);
                    try
                    {
                        HTTPclient.PostAsync(target, content);
                    }
                    catch (Exception e)
                    {
                        ConsoleLog.Crit("[SmartReply] HTTP failed: " + e.Message + "");
                    }
                }
                else
                {
                    return(false);
                }
                return(true);
            }
            else
            {
                if ((mode == "CHAT") || (mode == "IM") || (mode == "HTTP"))
                {
                    return(true);
                }
                return(false);
            }
        }
예제 #11
0
        protected JsonConfig MakeDefault()
        {
            string[]   args  = this.GetCommandsList();
            JsonConfig reply = new JsonConfig();

            foreach (string arg in args)
            {
                string arg_type          = GetCommandArgTypes(arg).First();
                string arg_value_default = null;
                if (arg_type != "")
                {
                    string[] ArgHints = GetCommandArgHints(arg);
                    if (ArgHints.Length > 0)
                    {
                        arg_value_default = ArgHints[0];
                        arg_value_default = arg_value_default.Replace("[", "");
                        arg_value_default = arg_value_default.Replace("]", "");
                        arg_value_default = arg_value_default.Replace("\"", "");
                    }
                    else
                    {
                        ConsoleLog.Debug("[Notice] No args hint found for " + arg + "");
                    }
                }
                if (arg_value_default != null)
                {
                    Type         Dtype = reply.GetType();
                    PropertyInfo prop  = Dtype.GetProperty(arg, BindingFlags.Public | BindingFlags.Instance);
                    if (prop != null)
                    {
                        if (prop.CanWrite == true)
                        {
                            if (arg_type == "Number")
                            {
                                if (int.TryParse(arg_value_default, out int result) == true)
                                {
                                    prop.SetValue(reply, result, null);
                                }
                            }
                            else if (arg_type == "Text")
                            {
                                prop.SetValue(reply, arg_value_default, null);
                            }
                            else if (arg_type == "True|False")
                            {
                                if (bool.TryParse(arg_value_default, out bool result) == true)
                                {
                                    prop.SetValue(reply, result, null);
                                }
                            }
                            else if (arg_type == "Collection")
                            {
                                prop.SetValue(reply, arg_value_default.Split(','), null);
                            }
                            else if (arg_type == "BigNumber")
                            {
                                if (ulong.TryParse(arg_value_default, out ulong result) == true)
                                {
                                    prop.SetValue(reply, result, null);
                                }
                            }
                            else
                            {
                                ConsoleLog.Debug("unknown arg_type: " + arg_type + " for " + arg + "");
                            }
                        }
                        else
                        {
                            ConsoleLog.Warn("Unable to write to " + arg + "");
                        }
                    }
                    else
                    {
                        ConsoleLog.Crit("unknown prop " + arg + "");
                    }
                }
            }
            return(reply);
        }
예제 #12
0
        protected JsonConfig process_prop(JsonConfig reply, string arg, string arg_value_default)
        {
            string arg_type = GetCommandArgTypes(arg).First();

            if (arg_type != "")
            {
                if (arg_value_default != null)
                {
                    Type         Dtype = reply.GetType();
                    PropertyInfo prop  = Dtype.GetProperty(arg, BindingFlags.Public | BindingFlags.Instance);
                    if (prop != null)
                    {
                        if (prop.CanWrite == true)
                        {
                            if (arg_type == "Number")
                            {
                                if (int.TryParse(arg_value_default, out int result) == true)
                                {
                                    prop.SetValue(reply, result, null);
                                }
                            }
                            else if (arg_type == "Text")
                            {
                                prop.SetValue(reply, arg_value_default, null);
                            }
                            else if (arg_type == "True|False")
                            {
                                if (bool.TryParse(arg_value_default, out bool result) == true)
                                {
                                    prop.SetValue(reply, result, null);
                                }
                            }
                            else if (arg_type == "Collection")
                            {
                                prop.SetValue(reply, arg_value_default.Split(','), null);
                            }
                            else if (arg_type == "BigNumber")
                            {
                                if (ulong.TryParse(arg_value_default, out ulong result) == true)
                                {
                                    prop.SetValue(reply, result, null);
                                }
                            }
                            else
                            {
                                ConsoleLog.Debug("unsupported arg_type: " + arg_type + " for " + arg + "");
                            }
                        }
                        else
                        {
                            ConsoleLog.Warn("Unable to write to " + arg + "");
                        }
                    }
                    else
                    {
                        ConsoleLog.Crit("unknown prop " + arg + "");
                    }
                }
            }
            else
            {
                ConsoleLog.Debug("unknown arg_type for " + arg + "");
            }
            return(reply);
        }
예제 #13
0
        public override bool CallFunction(string[] args)
        {
            if (base.CallFunction(args) == true)
            {
                List <string> acceptablewords          = new List <string>();
                Dictionary <string, ParcelFlags> flags = parcel_static.get_flags_list();
                acceptablewords.AddRange(new[] { "True", "False" });

                Dictionary <string, bool> setflags = new Dictionary <string, bool>();
                foreach (string a in args)
                {
                    string[] parts = a.Split('=', StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 2)
                    {
                        if (flags.ContainsKey(parts[0]) == true)
                        {
                            if (acceptablewords.Contains(parts[1]))
                            {
                                setflags.Add(parts[0], Convert.ToBoolean(parts[1]));
                            }
                            else
                            {
                                ConsoleLog.Crit("Unable to set flag " + parts[0] + " to : " + parts[1] + "");
                            }
                        }
                        else
                        {
                            ConsoleLog.Warn("Flag: " + parts[0] + " is unknown");
                        }
                    }
                    else
                    {
                        ConsoleLog.Warn("Flag: " + a + " missing = True|False");
                    }
                }
                if (setflags.Count > 0)
                {
                    int localid = bot.GetClient.Parcels.GetParcelLocalID(bot.GetClient.Network.CurrentSim, bot.GetClient.Self.SimPosition);
                    if (bot.GetClient.Network.CurrentSim.Parcels.ContainsKey(localid) == true)
                    {
                        Parcel p = bot.GetClient.Network.CurrentSim.Parcels[localid];
                        if (parcel_static.has_parcel_perm(p, bot) == true)
                        {
                            foreach (KeyValuePair <string, bool> cfg in setflags)
                            {
                                if (flags.ContainsKey(cfg.Key) == true)
                                {
                                    parcel_static.ParcelSetFlag(flags[cfg.Key], p, cfg.Value);
                                }
                            }
                            p.Update(bot.GetClient.Network.CurrentSim, false);
                            return(true);
                        }
                        else
                        {
                            return(Failed("Incorrect perms to control parcel"));
                        }
                    }
                    else
                    {
                        return(Failed("Unable to find parcel in memory, please wait and try again"));
                    }
                }
                else
                {
                    return(Failed("No accepted flags"));
                }
            }
            return(false);
        }