public static WhoInfo GetUser(Irc server, string nick) { bool gotWho = false; WhoInfo wi = null; server.Writer.SendMessage(RFC1459.IrcCommands.Who(nick, false)); while (!gotWho) { RFC1459.ReplyCode code; string message, prefix, command; string[] parameters; message = server.Reader.ReadLine( ); RFC1459.IrcCommands.ParseReply(message, out prefix, out command, out parameters); if (Enum.TryParse <RFC1459.ReplyCode> (command, out code)) { switch (code) { case RFC1459.ReplyCode.RPL_WHOREPLY: wi = WhoInfo.Parse(new IrcMessage(prefix, command, parameters)); break; case RFC1459.ReplyCode.RPL_ENDOFWHO: gotWho = true; break; } } } return(wi); }
public static WhoInfo Parse(IrcMessage msg) { WhoInfo wi = new WhoInfo( ); // Prefix CMD Parameters // 0 1 2 3 4 5 6 7 8 // 0 1 2 3 4 5 6 7 8 9 10 // :Lucifer.GeekShed.net 352 Blizzardo1 #Blizzeta ~DrAlanD *.dhcp.nwtn.ct.charter.com Komma.GeekShed.net user99672 Hr& :0 realname string prefix = msg.Prefix; string command = msg.Command; string[] parameters = msg.Parameters; wi.channel = parameters[1]; wi.ident = parameters[2]; wi.host = parameters[3]; wi.server = parameters[4]; wi.nick = parameters[5]; string umode = parameters[6]; string[] realdata = parameters[7].Split(' '); wi.hopcount = int.Parse(realdata[0]); wi.realname = string.Join(" ", realdata, 1, realdata.Length - 1); for (int i = 0; i < umode.Length; i++) { switch (umode[i]) { case 'H': wi.isAway = false; break; case 'G': wi.isAway = true; break; case '~': wi.isOwner = true; break; case '&': wi.isSop = true; break; case '@': wi.isOp = true; break; case '%': wi.isHop = true; break; case '+': wi.isVoice = true; break; case 'r': wi.isRegistered = true; break; case '*': wi.isIrcOp = true; break; } } return(wi); }
public static WhoInfo Parse( IrcMessage msg ) { WhoInfo wi = new WhoInfo ( ); // Prefix CMD Parameters // 0 1 2 3 4 5 6 7 8 // 0 1 2 3 4 5 6 7 8 9 10 // :Lucifer.GeekShed.net 352 Blizzardo1 #Blizzeta ~DrAlanD *.dhcp.nwtn.ct.charter.com Komma.GeekShed.net user99672 Hr& :0 realname string prefix = msg.Prefix; string command = msg.Command; string[] parameters = msg.Parameters; wi.channel = parameters[ 1 ]; wi.ident = parameters[ 2 ]; wi.host = parameters[ 3 ]; wi.server = parameters[ 4 ]; wi.nick = parameters[ 5 ]; string umode = parameters[ 6 ]; string[] realdata = parameters[ 7 ].Split ( ' ' ); wi.hopcount = int.Parse ( realdata[ 0 ] ); wi.realname = string.Join ( " ", realdata, 1, realdata.Length - 1 ); for ( int i = 0; i < umode.Length; i++ ) switch ( umode[ i ] ) { case 'H': wi.isAway = false; break; case 'G': wi.isAway = true; break; case '~': wi.isOwner = true; break; case '&': wi.isSop = true; break; case '@': wi.isOp = true; break; case '%': wi.isHop = true; break; case '+': wi.isVoice = true; break; case 'r': wi.isRegistered = true; break; case '*': wi.isIrcOp = true; break; } return wi; }
public static void IncludeBuiltInCommands( ) { // act AddCommand("act", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { string msg = string.Join(" ", o as string[], 2, o.Length - 2); i.SendAction(c, msg); i.Format("{0} sent {1} this action: {2}", ConsoleColor.DarkMagenta, n, c, msg); return(0); })); // ajoin AddCommand("ajoin", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { int len = o.Length; bool alert = false; string cmd = string.Empty; string Formatted = string.Empty; if (len > 0) { cmd = o[0] as string; } else { i.SendMessage(n, "What do you want me to do again with Ajoin?"); alert = true; } if (o.Length > 1) { Formatted = string.Format("AJOIN {0} {1}", cmd, c); } else { Formatted = string.Format("AJOIN {0}", cmd); } if (cmd == "add") { if (len > 1) { i.SendMessage(n, string.Format("Added {0} to Ajoin", c)); } else { i.SendMessage(n, "Nothing to add!"); alert = true; } } else if (cmd == "addall") { i.SendMessage(n, "Adding all open channels to Ajoin"); } else if (cmd == "del") { if (len > 1) { i.SendMessage(n, string.Format("Deleting {0} from Ajoin")); } else { i.SendMessage(n, "Noting to delete!"); alert = true; } } else if (cmd == "list") { i.SendMessage(n, "Listing all available channels"); } else if (cmd == "clear") { i.SendMessage(n, "Cleared Channels"); } if (!alert) { i.SendMessage("NickServ", Formatted); } return(0); })); // away AddCommand("away", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { string amsg = string.Join(" ", o as string[]); i.Raw(RFC1459.IrcCommands.Away(amsg)); return(0); })); //ban AddCommand("ban", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { if (!ignoreBan) { i.Raw(RFC1459.IrcCommands.Ban(c, o[0] as string)); commands["kick"].Invoke(i, c, n, o); } else { i.Format("Ignoring ban called by {0}", ConsoleColor.Red, n); } return(0); })); // check [permission] AddCommand("check", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( ); subcmd.Add("permission", new Func <int> (() => { // TODO: Add permissions check return(0); })); subcmd.Add("time", new Func <int> (() => { // TODO: Add configuration for callee DateTime dt = DateTime.Now; i.Format("Current Time: {0:dddd MMMM dd, yyyy} at {0:HH:mm:ss}", ConsoleColor.Yellow, dt); i.GetChannel(c).SendMessage(string.Format("{0} {1:dddd MMMM dd, yyyy} at {1:HH:mm:ss}", CheckS(i.Owner), dt)); return(0); })); subcmd.Add("uptime", new Func <int> (() => { i.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); })); return(0); })); // clear AddCommand("clear", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { Console.Clear( ); return(0); })); // createdb AddCommand("createdb", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( ); subcmd.Add("activate", new Func <int> (() => { string key = o[1] as string; string[] decryptedKey; if (ProductKey.ActivateKey(key, out decryptedKey)) { if (!File.Exists(userdb)) { XmlDocument xDoc = new XmlDocument( ); XmlNode xDeclaration = xDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes"); XmlNode xRoot = xDoc.CreateElement("Users"); xDoc.AppendChild(xDeclaration); xDoc.AppendChild(xRoot); xDoc.Save(userdb); i.GetChannel(c).SendMessage(string.Format("A new Database has been created and activated, thanks to {0} in {1} on {2: dddd MMMM dd, yyyy} at {2:hh:mm:ss tt}!", decryptedKey[0], decryptedKey[1], DateTime.FromBinary(long.Parse(decryptedKey[2])))); } else { i.GetChannel(c).SendMessage("A database already exists and has been authenticated by me. :( If my Userbase is corrupt or you would like to clear it, please do +createdb override"); } } return(0); })); // 0 1 // createdb override key subcmd.Add("override", new Func <int> (() => { int key = (new Random( )).Next(10000, int.MaxValue); if (o.Length < 2) { overridekey = key; i.GetChannel(c).SendMessage(string.Format("Please tell {0} to come override.", i.Owner)); i.SendMessage(i.Owner, string.Format("Override Activation Key is {0}. This is here to prevent misuse of the createdb command. Please ignore this if your Database is not corrupted or you don't want to start over...", overridekey)); } else { if (int.Parse(o[1] as string) == overridekey) { File.Delete(userdb); i.SendMessage(i.Owner, "Database has been destroyed."); } else { i.SendMessage(i.Owner, string.Format("{0} has attempted to override the database! Their code was {1}, real code is {2}", n, o[1] as string, overridekey)); } } return(0); })); if (o.Length > 0) { try { subcmd[o[0] as string].Invoke( ); } catch (Exception) { i.GetChannel(c).SendMessage("Invalid command in creatdb"); } } else { if (!File.Exists(userdb)) { string Code = ProductKey.GenerateProductKey(n, c).ProductID; i.Format("Access Code is {0}", ConsoleColor.DarkGreen, Code); System.IO.File.WriteAllText(Irc.StartupPath + "\\code.esd", Code.ToString( )); i.GetChannel(c).SendMessage(string.Format("Access Code sent to {0}", i.Owner)); } else { i.GetChannel(c).SendMessage("User Database already exists!!!"); } } return(0); })); // get AddCommand("get", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { return(0); })); // set AddCommand("set", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( ); subcmd.Add("kick", new Func <int> (() => { string val = (o[1] as string).ToLower( ); if (val == "on" || val == "true") { ignoreKick = false; } else if (val == "off" || val == "false") { ignoreKick = true; } return(0); })); subcmd.Add("say", new Func <int> (() => { string val = (o[1] as string).ToLower( ); if (val == "on" || val == "true") { ignoreSay = false; } else if (val == "off" || val == "false") { ignoreSay = true; } return(0); })); subcmd.Add("ban", new Func <int> (() => { string val = (o[1] as string).ToLower( ); if (val == "on" || val == "true") { ignoreBan = false; } else if (val == "off" || val == "false") { ignoreBan = true; } return(0); })); return(0); })); // help AddCommand("help", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { // +help [-mpuv] [command] [subcommand] /* * Options: * -m | --more : More Command * -p | --permissions : Permissions Command * -u | --usage : Usage Command * -v | --version : Version Command */ return(0); })); // join AddCommand("join", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { 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)); } return(-1); })); // kick AddCommand("kick", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { string[] ss = o as string[]; if (!ignoreKick) { i.Raw(RFC1459.IrcCommands.Kick(c, o[0] as string, string.Join(" ", ss, 1, ss.Length - 1))); } return(0); })); // list AddCommand("list", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { Channel[] chans = i.Channels.ToArray( ); i.SendMessage(n, "I am in these channels:"); foreach (Channel chan in chans) { i.SendMessage(n, string.Format(chan.Name)); } return(0); })); // me AddCommand("me", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { Dictionary <string, Func <int> > subcmd = new Dictionary <string, Func <int> > ( ); XmlDocument xDoc = new XmlDocument( ); subcmd.Add("add", new Func <int> (() => { foreach (User u in ReadUsers( )) { if (u.name == n) { i.GetChannel(c).SendMessage(string.Format("The Account for {0} already exists!", n)); return(1); } } xDoc.Load(userdb); XmlNode xRoot = xDoc.SelectSingleNode("/Users"); XmlNode xUser = xDoc.CreateElement("User"); XmlAttribute aUser = xDoc.CreateAttribute("name"); XmlAttribute aHost = xDoc.CreateAttribute("host"); XmlAttribute aPermission = xDoc.CreateAttribute("permission"); XmlAttribute aBan = xDoc.CreateAttribute("banned"); i.Raw(RFC1459.IrcCommands.Who(n, false)); WhoInfo wi = WhoInfo.GetUser(i, o[0] as string); aUser.Value = wi.Nick; aHost.Value = wi.Host; aPermission.Value = Enum.GetName(typeof(Scripts.Permissions), Scripts.Permissions.User); aBan.Value = false.ToString( ); xUser.Attributes.Append(aUser); xUser.Attributes.Append(aHost); xUser.Attributes.Append(aPermission); xUser.Attributes.Append(aBan); xRoot.AppendChild(xUser); xDoc.AppendChild(xRoot); xDoc.Save(userdb); i.GetChannel(c).SendMessage(string.Format("Welcome {0}!", n)); return(0); })); subcmd.Add("del", new Func <int> (() => { xDoc.Load(userdb); XmlNodeList list = xDoc.SelectNodes("/Users/User"); foreach (XmlNode no in list) { string name = no.Attributes["name"].Value; if (name == n) { i.Format("Selected {0}!", ConsoleColor.DarkGreen, name); xDoc.SelectSingleNode("/Users").RemoveChild(no); xDoc.Save(userdb); i.GetChannel(c).SendMessage(string.Format("Sorry to see you go {0}! :(", name)); break; } } return(0); })); try { if (System.IO.File.Exists(userdb)) { subcmd[o[0] as string].Invoke( ); } else { i.GetChannel(c).SendMessage("There is no Database. Use +createdb to send an Access code to Blizzardo1 for a new File."); } } catch (Exception ex) { i.GetChannel(c).SendMessage(ex.Message); Console.WriteLine(ex); } return(0); })); // mode // TODO: add mode for moderation // nick AddCommand("nick", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { i.Raw(RFC1459.IrcCommands.Nick(o[0] as string)); return(0); })); // part AddCommand("part", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { string[] format = o as string[]; string channel = format[0]; i.Part(channel, string.Join(" ", format, 1, format.Length - 1)); return(0); })); // quit AddCommand("quit", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { i.Raw(RFC1459.IrcCommands.Quit(string.Join(" ", o as string[]))); return(0); })); // raw AddCommand("raw", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { i.Raw(string.Join(" ", o)); return(0); })); // reboot AddCommand("reboot", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { System.Diagnostics.Process p = new System.Diagnostics.Process( ) { StartInfo = new System.Diagnostics.ProcessStartInfo( ) { FileName = System.Reflection.Assembly.GetExecutingAssembly( ).GetName( ).Name + ".exe" } }; i.Disconnect("Rebooting!"); p.Start( ); Environment.Exit(0); return(0); })); // say AddCommand("say", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { if (!ignoreSay) { i.GetChannel(c).SendMessage(string.Join(" ", o as string[])); } return(0); })); // version AddCommand("version", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { 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); })); // weather [No API] // whois AddCommand("whois", new Func <Irc, string, string, object[], int> ((i, c, n, o) => { i.Raw(RFC1459.IrcCommands.Whois(o[0] as string)); return(0); })); }