public Channel( Irc Parent, string Name, string Key )
 {
     this.parent = Parent;
     this.channel = Name;
     this.key = Key;
     this.modes = string.Empty;
     streamer = Streamer.GetProfile ( Name.Replace ( "#", "" ) );
     Irc.Format ( "Caught Streamer: {0}", ConsoleColor.Green, streamer.name );
 }
        public static void IncludeBuiltInCommands( )
        {
            #region check

            AddCommand("check", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );
                Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( );
                subcmd.Add("permission", () =>
                {
                    // TODO: Add permissions check
                    i.GetChannel(c).SendMessage(string.Format("{0}, Your Permissions are \"{1}\"", n, p));
                    return(0);
                });

                subcmd.Add("time", () =>
                {
                    // TODO: Add configuration for callee
                    DateTime dt = DateTime.Now;
                    Irc.Format("Current Time: {0:dddd MMMM dd, yyyy} at {0:HH:mm:ss}", ConsoleColor.Yellow, dt);
                    i.GetChannel(c).SendMessage(string.Format("{0} time is {1:dddd MMMM dd, yyyy} at {1:HH:mm:ss}", CheckS(i.Owner), dt));
                    return(0);
                });
                subcmd.Add("uptime", () =>
                {
                    Irc.Format("Uptime: {0}", ConsoleColor.DarkGreen);
                    i.GetChannel(c).SendMessage(string.Format("{0}, I've been up for {1} {2}, {3} {4}, {5} {6}, and {7} {8}"));
                    return(0);
                });

                try
                {
                    subcmd[o[0] as string].Invoke( );
                }
                catch (Exception ex)
                {
                    i.SendMessage(c, "Invalid command");
                }

                return(0);
            });

            #endregion check

            #region clear

            AddCommand("clear", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );
                if (p <= Permissions.Root)
                {
                    Console.Clear( );
                }
                else
                {
                    i.SendMessage(n, "Let me clean that nose of yours... in other words... \"MIND YOUR DAMN BUSINESS!\" o_o");
                }
                return(0);
            });

            #endregion clear

            #region get

            AddCommand("get", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );
                Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( );

                subcmd.Add("say", () =>
                {
                    i.SendMessage(n, ignoreSay ? "I'm ignoring say." : "I'm not ignoring say.");
                    return(0);
                });

                if (p <= Permissions.Operator)
                {
                    subcmd[o[0] as string].Invoke( );
                }
                else
                {
                    i.GetChannel(c).SendMessage(SpecialMessage(n));
                }
                return(0);
            });

            #endregion get

            #region set

            AddCommand("set", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );
                Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( );

                subcmd.Add("say", () =>
                {
                    string val = (o[1] as string).ToLower( );
                    if (val == "on" || val == "true")
                    {
                        ignoreSay = false;
                    }
                    else if (val == "off" || val == "false")
                    {
                        ignoreSay = true;
                    }
                    return(0);
                });

                return(0);
            });

            #endregion set

            #region help

            AddCommand("help", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );

                // + help [-mpuv] [command] [subcommand]

                /*
                 *  Options:
                 *      -m | --more         : More Command
                 *      -p | --permissions  : Permissions Command
                 *      -u | --usage        : Usage Command
                 *      -v | --version      : Version Command
                 */

                return(0);
            });

            #endregion help

            #region join

            AddCommand("join", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );
                if (p <= Permissions.Administrator)
                {
                    bool chanExists = i.Channels.Exists(new Predicate <Channel> ((ch) =>
                    {
                        return(ch.Name == o[0] as string);
                    }));

                    if (!chanExists)
                    {
                        if (o.Length > 1)
                        {
                            i.Join(o[0] as string, o[1] as string);
                            return(0);
                        }
                        else if (o.Length > 0)
                        {
                            i.Join(o[0] as string, string.Empty);
                            return(0);
                        }
                        else
                        {
                            i.SendMessage(c, "I can't join without a channel name");
                        }
                    }
                    else
                    {
                        i.SendMessage(c, string.Format("I'm already in {0}!", o[0] as string));
                    }
                }
                else
                {
                    i.SendMessage(n, SpecialMessage(n));
                }
                return(-1);
            });

            #endregion join

            #region list

            AddCommand("list", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );

                if (p <= Permissions.Administrator)
                {
                    Channel[] chans = i.Channels.ToArray( );
                    i.SendMessage(n, "I am in these channels:");
                    foreach (Channel channel in chans)
                    {
                        i.SendMessage(n, string.Format(channel.Name));
                    }
                }
                else
                {
                    i.GetChannel(c).SendMessage(SpecialMessage(n));
                }
                return(0);
            });

            #endregion list

            #region Topic

            AddCommand("topic", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );
                if (p <= Permissions.Operator)
                {
                    string msg = string.Join(" ", o as string[], 1, o.Length - 1);
                }

                return(0);
            });

            #endregion Topic

            #region Playlist

            AddCommand("playlist", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );
                if (p <= Permissions.Administrator)
                {
                    i.GetChannel(c).SendMessage(string.Format("{0} playlist: http://playlist.blizzeta.net", CheckS(i.Owner)));
                }
                return(0);
            });

            #endregion Playlist

            #region Game

            AddCommand("game", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );
                if (p <= Permissions.User)
                {
                }
                return(0);
            });

            #endregion Game

            #region account

            AddCommand("acc", (i, c, n, o) =>
            {
                Channel chan  = i.GetChannel(c);
                Permissions p = User.GetUser(chan.Streamer, n).GetPermission( );

                Streamer streamer = Streamer.GetProfile(c.Replace("#", ""));
                string userdb     = string.Format("{0}\\users.xml");
                XDocument xDoc    = XDocument.Load(userdb);
                i.SendNotice(n, string.Format("Please see {0} or a mod for assistance.", streamer.name));

                #region depcode WORK ON THIS

                Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( );

                subcmd.Add("streamer", () =>
                {
                    Dictionary <string, Func <int> > subsubcmd = new Dictionary <string, Func <int> > ( );

                    subsubcmd.Add("register", new Func <int> (() =>
                    {
                        string channel = c.Replace("#", "");
                        string botnick = Streamer.GetProfile(i.Nick).name;
                        if (p <= Permissions.Administrator && channel.Equals(botnick))
                        {
                            string nc = o[3] as string;
                            Streamer.CreateProfile(nc);
                            i.GetChannel(c).SendMessage(string.Format("Streamer \"{0}\" has registered!", nc));
                            i.Join(string.Format("#{0}", nc));
                        }
                        else
                        {
                            i.GetChannel(c).SendMessage(string.Format("{0}, I can only register streamers on my profile and with the approval of an Administrator", n));
                        }
                        return(0);
                    }));

                    subsubcmd[o[2] as string].Invoke( );

                    return(0);
                });

                subcmd.Add("register", () =>
                {
                    string uname = ((o.Length > 1 ? (string.IsNullOrEmpty(o[1] as string) ? n : o[1] as string) : n));
                    Irc.Format("<Accounts> Formatting and adding {0}", ConsoleColor.Green, uname);

                    User u = chan.Streamer.users.Single((U) =>
                    {
                        bool b = (U.name == uname);

                        if (b)
                        {
                            chan.SendMessage(string.Format("{0} is already registered! "));
                        }

                        return(b);
                    });

                    XAttribute xName = new XAttribute("name", o.Length > 1 ? o[1] as string : n);
                    XAttribute xPerm = new XAttribute("permission", (p <= Permissions.Root ?
                                                                     (o.Length > 2 ?
                                                                      Enum.GetName(typeof(Permissions), o[2] as string)
                            : Enum.GetName(typeof(Permissions), Permissions.User))
                        : Enum.GetName(typeof(Permissions), Permissions.User)));
                    XAttribute xBanned = new XAttribute("banned", false);

                    //XAttribute xToken = new XAttribute ( "token",
                    //    Token.Generate ( xName.Value, c, ( Permissions ) Enum.Parse ( typeof ( Permissions ), xPerm.Value ) ).TokenData );
                    XElement xUser = new XElement("User");
                    xUser.Add(xName, xPerm, xBanned);   //, xToken );

                    XElement xUsers = xDoc.Element("Users");
                    xUsers.Add(xUser);

                    xDoc.Save(userdb);
                    i.GetChannel(c).SendMessage(string.Format("Welcome {0}!", uname));

                    return(0);
                });

                subcmd.Add("ban", () =>
                {
                    if (p <= Permissions.Operator)
                    {
                        XElement xel = xDoc.Descendants("Users")
                                       .Descendants("User")
                                       .Where((x) => x.Attribute("name")
                                              .Value == o[1] as string).FirstOrDefault( );

                        xel.SetAttributeValue("banned", true);

                        xDoc.Save(userdb);
                    }
                    return(0);
                });

                subcmd.Add("unban", () =>
                {
                    if (p <= Permissions.Operator)
                    {
                        XElement xel = xDoc.Descendants("Users")
                                       .Descendants("User")
                                       .Where((x) => x.Attribute("name")
                                              .Value == o[1] as string).FirstOrDefault( );

                        xel.SetAttributeValue("banned", false);

                        xDoc.Save(userdb);
                    }
                    return(0);
                });

                // 0 1 2 promote user permission
                subcmd.Add("promote", () =>
                {
                    Dictionary <string, Func <int> > perms = new Dictionary <string, Func <int> > ( );
                    XElement xel = xDoc.Descendants("Users")
                                   .Descendants("User")
                                   .Where((x) => x.Attribute("name")
                                          .Value == o[1] as string).FirstOrDefault( );
                    perms.Add("administrator", new Func <int> (() =>
                    {
                        if (p == Permissions.Root)
                        {
                            xel.SetAttributeValue("permission", Permissions.Administrator);

                            xDoc.Save(userdb);
                        }
                        else
                        {
                            i.SendMessage(n, "I'm sorry, Only Root Users can set Admin on other users.");
                        }
                        return(0);
                    }));

                    perms.Add("operator", new Func <int> (() =>
                    {
                        if (p <= Permissions.Administrator)
                        {
                            xDoc.Descendants("Users")
                            .Descendants("User")
                            .Where((x) => x.Attribute("name")
                                   .Value == o[1] as string).FirstOrDefault( );

                            xel.SetAttributeValue("permission", Permissions.Operator);

                            xDoc.Save(userdb);
                        }
                        else
                        {
                            i.SendMessage(n, "I'm sorry, Only Administrative Users can set Operator on other users.");
                        }

                        return(0);
                    }));

                    try
                    {
                        if (p <= Permissions.Administrator)
                        {
                            perms[o[2] as string].Invoke( );
                        }
                    }
                    catch (Exception)
                    {
                        i.SendNotice(n, "+acc promote <user> <permission>");
                    }
                    return(0);
                });

                subcmd.Add("demote", () =>
                {
                    if (p <= Permissions.Administrator)
                    {
                        XElement xel = xDoc.Descendants("Users")
                                       .Descendants("User")
                                       .Where((x) => x.Attribute("name")
                                              .Value == o[1] as string).FirstOrDefault( );
                        Permissions perm = ( Permissions )Enum.Parse(typeof(Permissions), xel.Attribute("permission").Value);

                        // We check to see if an Administrator is trying to revoke Administrator or
                        // Root. If Administrator < Operator <-- TRUE If Administrator <
                        // Administrator <-- FALSE If Administrator < Root <-- FALSE If Root < Root
                        // <-- FALSE

                        if (xel.Attribute("name").Value == i.Owner)
                        {
                            i.SendNotice(n, string.Format("God-Mode: De-Ranking {0}", xel.Attribute("name").Value));
                            xel.SetAttributeValue("permission", Permissions.User);
                        }
                        else if (p < perm)
                        {
                            xel.SetAttributeValue("permission", Permissions.User);
                        }
                        else
                        {
                            i.SendNotice(n, "You cannot De-Rank either yourself, a higher user, or someone on the same rank as yourself.");
                        }

                        xDoc.Save(streamer.config);
                    }
                    return(0);
                });

                subcmd.Add("bye", () =>
                {
                    XElement xel = o.Length < 2 ? User.GetUserXmlProfile(n) : (p <= Scripts.Permissions.Administrator ? User.GetUserXmlProfile(o[1] as string) : User.GetUserXmlProfile(n, true, i));
                    string name  = xel.Attribute("name").Value;

                    Irc.Format("Selected {0}!", ConsoleColor.DarkGreen, name);

                    if (p <= Permissions.Administrator)
                    {
                        i.GetChannel(c).SendMessage(string.Format("Account {0} has been deregistered by an Administrator!", name));
                    }
                    else
                    {
                        i.GetChannel(c).SendMessage(string.Format("Sorry to see you go {0}! :(", name));
                    }

                    xel.Remove( );

                    xDoc.Save(userdb);

                    return(0);
                });

                try
                {
                    if (System.IO.File.Exists(userdb))
                    {
                        try
                        {
                            subcmd[o[0] as string].Invoke( );
                        }
                        catch (Exception ex)
                        {
                            i.SendMessage(c, "Invalid command");
                        }
                    }
                    else
                    {
                        i.GetChannel(c).SendMessage(string.Format("There is no Database. Use +createdb to send an Access code to {0} for a new File.", i.Owner));
                    }
                }
                catch (Exception ex)
                {
                    i.GetChannel(c).SendMessage(ex.Message);
                    Console.WriteLine(ex);
                }

                #endregion depcode WORK ON THIS

                return(0);
            });

            #endregion account

            #region part

            AddCommand("part", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );
                if (p <= Permissions.Administrator)
                {
                    string[] format = o as string[];
                    string channel  = format[0];
                    i.Part(channel, string.Join(" ", format, 1, format.Length - 1));
                }
                else
                {
                    i.GetChannel(c).SendMessage(SpecialMessage(n));
                }
                return(0);
            });

            #endregion part

            #region quit

            AddCommand("quit", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );
                if (p <= Permissions.Administrator)
                {
                    i.Disconnect(string.Join(" ", o as string[]));
                }
                else
                {
                    i.GetChannel(c).SendMessage(SpecialMessage(n));
                }
                return(0);
            });

            #endregion quit

            #region raw

            AddCommand("raw", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );
                if (p <= Permissions.Root)
                {
                    i.Raw(string.Join(" ", o));
                }
                else
                {
                    SendMessage(i, c, n, SpecialMessage(n));
                }
                return(0);
            });

            #endregion raw

            #region reboot

            AddCommand("reboot", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );

                if (p <= Permissions.Administrator)
                {
                    System.Diagnostics.Process pr = new System.Diagnostics.Process( )
                    {
                        StartInfo = new System.Diagnostics.ProcessStartInfo( )
                        {
                            FileName = System.Reflection.Assembly.GetExecutingAssembly( ).GetName( ).Name + ".exe"
                        }
                    };
                    i.Disconnect("Rebooting!");
                    pr.Start( );
                    Environment.Exit(0);
                }
                else
                {
                    i.SendMessage(n, "You may not reboot me. Only an Administrator or the Owner may do so.");
                }
                return(0);
            });

            #endregion reboot

            #region say

            AddCommand("say", (i, c, n, o) =>
            {
                Permissions p = User.GetUserProfile(n).GetPermission( );
                if (p <= Permissions.User)
                {
                    if (!ignoreSay)
                    {
                        SendMessage(i, c, n, string.Join(" ", o as string[]));
                    }
                }
                else
                {
                    SendMessage(i, c, n, SpecialMessage(n));
                }

                return(0);
            });

            #endregion say

            #region Uptime

            AddCommand("uptime", (i, c, n, o) =>
            {
                i.GetChannel(c).SendMessage(string.Format("I have been online for {0}", Global.FormatUptime( )));
                return(0);
            });

            #endregion Uptime

            #region version

            AddCommand("version", (i, c, n, o) =>
            {
                // No Permissions needed
                Dictionary <string, Func <int> > arguments = new Dictionary <string, Func <int> > ( );
                arguments.Add("core", new Func <int> (() =>
                {
                    i.GetChannel(c).SendMessage(string.Format("{0}; Core Version {1}", Global.Title, Global.Core));
                    return(0);
                }));

                arguments.Add("scripts", new Func <int> (() =>
                {
                    i.GetChannel(c).SendMessage(string.Format("{0}; Scripts Version {1}", Global.Title, Global.Scripts));
                    return(0);
                }));

                try
                {
                    arguments[o[0] as string].Invoke( );
                }
                catch
                {
                    i.GetChannel(c).SendMessage(string.Format("{0}; For more information, see \"core\" and \"scripts\"", Global.Title));
                }
                return(0);
            });

            #endregion version
        }
 public static User GetUser(Streamer streamer, string name)
 {
     Load(streamer.config);
     return(GetUserProfile(name));
 }
        public static Streamer GetProfile( string name )
        {
            Streamer str = new Streamer ( );
            XElement xel = str.GetXmlProfile ( name );

            return str.ToStreamer ( xel );
        }
 public static User GetUser( Streamer streamer, string name )
 {
     Load ( streamer.config );
     return GetUserProfile ( name );
 }
        private static void CreateDefaultStreamerFiles( Streamer streamer )
        {
            XDocument xConfig = new XDocument ( new XDeclaration ( "1.0", "utf-8", "yes" ) );

            XElement xRoot = new XElement ( "Config" );
            XElement xCommands = new XElement ( "Commands" );
            XElement xBans = new XElement ( "Bans" );

            XElement xFiles = new XElement ( "Files" );
            XElement xFile = new XElement ( "Config" );
            xFile.SetAttributeValue ( "path", string.Format ( "{0}\\users.xml", streamer.config ) );

            xFiles.Add ( xFile );
            xRoot.SetAttributeValue ( "owner", streamer.name );
            xRoot.Add ( xCommands, xBans, xFiles );
            xConfig.Add ( xRoot );
            xConfig.Save ( string.Format ( "{0}\\config.xml", streamer.config ) );
        }