Example #1
0
        protected void JsonChatControler(string message, string sender_name, UUID sender_uuid, bool avatar, bool group, UUID group_uuid, bool localchat, bool fromme)
        {
            if ((group == true) || (localchat == true) || (fromme == true))
            {
                return;
            }
            try
            {
                bool frommaster = true;
                if (sender_uuid != master_uuid)
                {
                    frommaster = false;
                }
                if (frommaster == true)
                {
                    if (message == "fakerestart")
                    {
                        AlertMessageEventArgs Alertargs = new AlertMessageEventArgs("this is a fake restart message", new Random().Next(99999).ToString(), new OpenMetaverse.StructuredData.OSDMap());
                        AlertEvent(this, Alertargs);
                        return;
                    }
                }
                JsonApiDefine APIE = JsonConvert.DeserializeObject <JsonApiDefine>(message);

                CoreCommandLib(sender_uuid, frommaster, APIE.cmd, APIE.args, APIE.signing, APIE.reply);
            }
            catch (Exception e)
            {
                LogFormater.Crit("[CoreCommandLib] exploded: " + e.Message + "");
            }
        }
Example #2
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)
                 {
                     string name = D.CommandName;
                     if (subtype_map.ContainsKey(name) == false)
                     {
                         string worknamespace = com.Namespace;
                         worknamespace = worknamespace.Replace("BetterSecondBot.Commands.", "");
                         worknamespace = worknamespace.Replace("BetterSecondBot.RLV.", "");
                         worknamespace = worknamespace.Replace("BetterSecondbot.HttpServer.View.", "");
                         worknamespace = worknamespace.Replace("BetterSecondbot.HttpServer.view.", "");
                         worknamespace = worknamespace.Replace("BetterSecondbot.HttpServer.Control.", "");
                         worknamespace = worknamespace.Replace("BetterSecondbot.HttpServer.control.", "");
                         worknamespace = worknamespace.Replace("CMD_", "");
                         subtype_map.Add(name, D.GetType());
                         subtype_workspace_map.Add(name, worknamespace);
                     }
                     else
                     {
                         LogFormater.Crit("[CMD] command: " + D.CommandName + " already defined!");
                     }
                 }
             }
         }
     }
 }
Example #3
0
 protected void SetPropTypeAndValue(JsonConfig reply, PropertyInfo prop, string arg, string arg_type, string arg_value_default)
 {
     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 if (arg_type == "Float")
             {
                 if (float.TryParse(arg_value_default, out float result) == true)
                 {
                     prop.SetValue(reply, result, null);
                 }
             }
             else
             {
                 LogFormater.Debug("unsupported arg_type: " + arg_type + " for " + arg + "");
             }
         }
         else
         {
             LogFormater.Warn("Unable to write to " + arg + "");
         }
     }
     else
     {
         LogFormater.Crit("unknown prop " + arg + "");
     }
 }
Example #4
0
        protected void SmartCommandReply(bool run_status, string target, string output, string command)
        {
            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)
                {
                    mode = "None";
                }
            }
            if (mode == "CHAT")
            {
                if (target_channel >= 0)
                {
                    Client.Self.Chat(output, target_channel, ChatType.Normal);
                }
                else
                {
                    run_status = false;
                    LogFormater.Crit("[SmartReply] output Channel must be zero or higher");
                }
            }
            else if (mode == "IM")
            {
                Client.Self.InstantMessage(target_avatar, output);
            }
            else if (mode == "HTTP")
            {
                Dictionary <string, string> values = new Dictionary <string, string>
                {
                    { "reply", output },
                    { "command", command },
                };

                var content = new FormUrlEncodedContent(values);
                try
                {
                    HTTPclient.PostAsync(target, content);
                }
                catch (Exception e)
                {
                    LogFormater.Crit("[SmartReply] HTTP failed: " + e.Message + "");
                }
            }
        }
Example #5
0
        public object SetParcelFlag([FormField] string escapedflagdata, string token)
        {
            KeyValuePair <bool, string> tests = SetupCurrentParcel(token, "parcel", "SetParcelFlag");

            if (tests.Key == false)
            {
                return(Failure(tests.Value, "SetParcelFlag", new [] { escapedflagdata }));
            }
            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>();

            string[] args = escapedflagdata.Split(":::");
            foreach (string a in args)
            {
                string[] parts = a.Split('=', StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 2)
                {
                    LogFormater.Warn("Flag: " + a + " missing \"=\"");
                    continue;
                }
                if (flags.ContainsKey(parts[0]) == false)
                {
                    LogFormater.Warn("Flag: " + parts[0] + " is unknown");
                    continue;
                }
                if (acceptablewords.Contains(parts[1]) == false)
                {
                    LogFormater.Crit("Unable to set flag " + parts[0] + " to : " + parts[1] + "");
                }
                setflags.Add(parts[0], Convert.ToBoolean(parts[1]));
            }
            if (setflags.Count == 0)
            {
                return(Failure("No accepted flags", "SetParcelFlag", new [] { escapedflagdata }));
            }
            if (parcel_static.has_parcel_perm(targetparcel, bot) == false)
            {
                return(Failure("Incorrect perms to control parcel", "SetParcelFlag", new [] { escapedflagdata }));
            }
            foreach (KeyValuePair <string, bool> cfg in setflags)
            {
                if (flags.ContainsKey(cfg.Key) == true)
                {
                    parcel_static.ParcelSetFlag(flags[cfg.Key], targetparcel, cfg.Value);
                }
            }
            targetparcel.Update(bot.GetClient.Network.CurrentSim, false);
            return(BasicReply("Applying perms", "SetParcelFlag", new [] { escapedflagdata }));
        }
Example #6
0
        protected void SendHTTPData(object data)
        {
            Dictionary <string, string> values = new Dictionary <string, string>
            {
                { "reply", data.ToString() },
            };
            var content = new FormUrlEncodedContent(values);

            try
            {
                HTTPclient.PostAsync(controler.getBot().getMyConfig.Setting_Tracker, content);
            }
            catch (Exception e)
            {
                LogFormater.Crit("[Tracker] HTTP failed: " + e.Message + "");
            }
        }
Example #7
0
        protected IEnumerable <Type> GetAPICommandClasses()
        {
            if (API_type != null)
            {
                Assembly[] asses = AppDomain.CurrentDomain.GetAssemblies();

                List <Assembly> GoodLooking = new List <Assembly>();
                foreach (Assembly Ass in asses)
                {
                    if (Ass.FullName.StartsWith("BetterSecondbot") == true)
                    {
                        GoodLooking.Add(Ass);
                    }
                    else if (Ass.FullName.StartsWith("Core") == true)
                    {
                        GoodLooking.Add(Ass);
                    }
                    else if (Ass.FullName.StartsWith("Shared") == true)
                    {
                        GoodLooking.Add(Ass);
                    }
                }

                List <Type> reply = new List <Type>();
                foreach (Assembly Ass in GoodLooking)
                {
                    foreach (Type ClassType in Ass.GetTypes())
                    {
                        if (ClassType.IsSubclassOf(API_type) == true)
                        {
                            reply.Add(ClassType);
                        }
                    }
                }
                return(reply);
            }
            else
            {
                LogFormater.Crit(this.GetType() + " Attempted to call GetSubTypes without setting API_TYPE first!");
            }
            return(null);
        }
Example #8
0
        protected async Task <Task> TriggerRelay(string sourcetype, string name, string message, string filtervalue, string discordServerid, string discordChannelid)
        {
            List <relay_config> Dataset = new List <relay_config>();

            if (sourcetype == "localchat")
            {
                Dataset = LocalchatRelay;
            }
            else if (sourcetype == "groupchat")
            {
                Dataset = GroupChatRelay;
            }
            else if (sourcetype == "avatarim")
            {
                Dataset = AvatarIMRelay;
            }
            else if (sourcetype == "objectim")
            {
                Dataset = ObjectIMRelay;
            }
            else if (sourcetype == "discord")
            {
                Dataset = DiscordRelay;
            }
            if (message.Contains("[relay]") == true)
            {
                return(Task.CompletedTask);
            }

            string message_no_name  = "[relay]" + message;
            string message_no_addon = message;

            message = "[relay] " + sourcetype + " # " + name + ": " + message;


            foreach (relay_config cfg in Dataset)
            {
                string sendmessage = message;
                if (cfg.encode_as_json == true)
                {
                    relay_packet packet = new relay_packet();
                    packet.source_message = message_no_name;
                    packet.source_user    = name;
                    if (sourcetype == "discord")
                    {
                        packet.discord_channelid = discordChannelid;
                    }
                    sendmessage = JsonConvert.SerializeObject(packet);
                }

                if (((cfg.sourcevalue == "all") && (sourcetype != "discordchat")) || (cfg.sourcevalue == filtervalue))
                {
                    if (cfg.targetname == "discordTTS")
                    {
                        string[] cfga = cfg.targetvalue.Split("@");
                        if (cfga.Length == 2)
                        {
                            if ((cfga[0] + "@" + cfga[1]) != filtervalue)
                            {
                                await controler.getBot().SendMessageToDiscord(cfga[0], cfga[1], message_no_addon, true);
                            }
                        }
                    }
                    else if (cfg.targetname == "discord")
                    {
                        string[] cfga = cfg.targetvalue.Split("@");
                        if (cfga.Length == 2)
                        {
                            if ((cfga[0] + "@" + cfga[1]) != filtervalue)
                            {
                                await controler.getBot().SendMessageToDiscord(cfga[0], cfga[1], message_no_addon, false);
                            }
                        }
                    }
                    else if (cfg.targetname == "http")
                    {
                        try
                        {
                            string[] bits = cfg.targetvalue.Split("@|@");
                            if (bits.Length == 2)
                            {
                                var client  = new RestClient(bits[0]);
                                var request = new RestRequest(bits[1], Method.POST);
                                request.AddParameter("reply", sendmessage);
                                request.AddHeader("content-type", "application/x-www-form-urlencoded");
                                _ = client.ExecutePostAsync(request).ConfigureAwait(false);
                            }
                            else
                            {
                            }
                        }
                        catch (Exception e)
                        {
                            LogFormater.Crit("[BetterRelay] HTTP failed: " + e.Message + "");
                        }
                    }
                    else if (cfg.targetname == "localchat")
                    {
                        int chan = 0;
                        int.TryParse(cfg.targetvalue, out chan);
                        controler.getBot().GetClient.Self.Chat(sendmessage, chan, ChatType.Normal);
                    }
                    else if (cfg.targetname == "avatarchat")
                    {
                        if (cfg.targetvalue != filtervalue)
                        {
                            if (UUID.TryParse(cfg.targetvalue, out UUID target) == true)
                            {
                                controler.getBot().SendIM(target, sendmessage);
                            }
                        }
                    }
                    else if (cfg.targetname == "groupchat")
                    {
                        if (cfg.targetvalue != filtervalue)
                        {
                            controler.getBot().CallAPI("Groupchat", new[] { cfg.targetvalue, sendmessage });
                        }
                    }
                }
            }
            return(Task.CompletedTask);
        }
Example #9
0
 public virtual void Crit(string message)
 {
     Log2File(LogFormater.Crit(message, false), ConsoleLogLogLevel.Crit);
 }
Example #10
0
        public CliHardware(string[] args)
        {
            SimpleIO   io         = new SimpleIO();
            JsonConfig Config     = new JsonConfig();
            string     loadFolder = "debug";
            string     json_file  = "bot.json";

#if DEBUG
            LogFormater.Debug("!! RUNNING IN DEBUG !!");
#else
            LogFormater.Status("Hardware config");
            if (args.Length == 1)
            {
                loadFolder = args[0];
            }
            else
            {
                loadFolder = "default";
                io.ChangeRoot("default");
                LogFormater.Warn("Using: using default folder");
            }
#endif
            io.ChangeRoot(loadFolder);
            if (SimpleIO.DirExists("wiki") == false)
            {
                LogFormater.Info("Basic Wiki [Creating]");
                new DebugModeCreateWiki(AssemblyInfo.GetGitHash(), io);
                LogFormater.Info("Basic Wiki [Ready]");
                io = new SimpleIO();
            }
            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)
                        {
                            LogFormater.Warn("Unable to read config file\n moving config to " + json_file + ".old and creating a empty config\nError was: " + e.Message + "");
                            io.MarkOld(json_file);
                            Config = new JsonConfig();
                            io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                        }
                    }
                    else
                    {
                        LogFormater.Warn("Json config is empty creating an empty one for you");
                        io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                    }
                }
                else
                {
                    LogFormater.Warn("Json config does not Exist creating it for you");
                    io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                }
            }
            else
            {
                LogFormater.Crit("you must select a .json file for config! example \"BetterSecondBot.exe mybot\" will use the mybot.json file!");
            }
            if (ok_to_try_start == true)
            {
                Config = MakeJsonConfig.Http_config_check(Config);
                new Discord_super(Config, false, loadFolder);
            }
        }