public IRCBot(string ServerAddress, IrcUser USer) : base(ServerAddress, USer) { Settings = JsonConvert.DeserializeObject<BotSettings>(File.ReadAllText("BotSettings.txt")); if (Settings.Logging) LogWriter = new StreamWriter("IRCBotLog.txt"); this.ConnectionComplete += IRCBot_ConnectionComplete; this.RawMessageRecieved += bot_RawMessage; RegisterCommand("function", FunctionMod); RegisterCommand("f", FunctionMod); RegisterCommand("ping", Ping); RegisterCommand("class", ModifyClass); RegisterCommand("param", ParamMod); RegisterCommand("diffdb", DiffDB); RegisterCommand("dumpwiki", DumpWiki); RegisterCommand("writepage", ForceWriteWikiPage); RegisterCommand("writeallpages", UpdateAllWikiPages); RegisterCommand("updatestatus", WikiStatus); RegisterCommand("user", UserMod); RegisterCommand("reload", Reload); RegisterCommand("addcaptcha", AddCaptcha); RegisterCommand("insult", InsultBot); RegisterCommand("help", Help); RegisterCommand("?", Help); }
private Action<IrcUser> WhoIsCallback; // TODO: Make this better #endregion Fields #region Constructors public IrcBot(string ServerAddress, IrcUser user, string ControlCharacter, Encoding encoding) { this.ServerAddress = ServerAddress; this.ControlCharacter = ControlCharacter; this.RegisteredCommands = new Dictionary<string, CommandHandler>(); this.Encoding = encoding; this.User = user; this.Channels = new List<IrcChannel>(); this.ChanServ = new ChanServ(this); this.LastRawMessageSent = DateTime.MinValue; }
public IrcBot(string ServerAddress, IrcUser user, string ControlCharacter, Encoding encoding) { this.ServerAddress = ServerAddress; this.ControlCharacter = ControlCharacter; this.RegisteredCommands = new Dictionary <string, CommandHandler>(); this.Encoding = encoding; this.User = user; this.Channels = new List <IrcChannel>(); this.ChanServ = new ChanServ(this); this.LastRawMessageSent = DateTime.MinValue; }
public IrcUserMessage(IrcMessage Message) { this.Target = Message.Payload.Remove(Message.Payload.IndexOf(' ')); this.Message = Message.Payload.Substring(Message.Payload.IndexOf(':') + 1); string[] parts = Message.Prefix.Split('!', '@'); this.Source = new IrcUser(parts[0], parts[1]); this.Source.Hostname = parts[2]; if (this.Target.StartsWith("#")) ChannelMessage = true; else this.Target = this.Source.Nick; }
public PS2StatBot(string ServerAddress, IrcUser User) : base(ServerAddress, User) { LoadBot(); StartTimers(); InitUserDB(); #if OLD RegisterCommand("openrev", OpenRevisionOLD); RegisterCommand("lookup", LookupFileOld); RegisterCommand("rawadd", CalculateRevisionOLD); RegisterCommand("closerev", CloseRevisionOLD); RegisterCommand("report_chng", DumpChangesInRevOLD); RegisterCommand("find", FindFileOLD); RegisterCommand("changebranch", SwitchBranchOLD); #else RegisterCommand("openrev", OpenRevision); RegisterCommand("lookup", LookupFile); RegisterCommand("rawadd", RawAdd); RegisterCommand("closerev", CloseRevision); RegisterCommand("report_chng", ReportChangesInRev); RegisterCommand("find", Find); RegisterCommand("changebranch", SwitchBranch); #endif // .ping RegisterCommand("ping", command => "get off my dick"); RegisterCommand("lastquery", commad => lastquery); RegisterCommand("stats", QueryStats); //RegisterCommand("mark", MarkFile); RegisterCommand("help", command => "https://github.com/RoyAwesome/ps2ls/wiki/lebot"); RegisterCommand("ps2ls", command => "https://github.com/RoyAwesome/ps2ls"); RegisterCommand("deleterevision", DeleteRevision); RegisterCommand("cleanorphanedfiles", CleanOrphandFiles); RegisterCommand("lastupdate", LastUpdate); RegisterCommand("register", RegisterUser); RegisterCommand("sendmessage", SendMessage); RegisterCommand("togglesysmessages", ToggleSysMessageRecieve); RegisterCommand("trackmanifest", TrackManifestFile); RegisterCommand("tracklist", AllTrackedFiles); RegisterCommand("diffmanifests", DiffManifests); RegisterCommand("downloadstatus", DownloadStatus); RegisterCommand("forcedownload", ForceDownload); RegisterCommand("forceanalyze", AnalyzeChanges); RegisterCommand("dumprevisions", DumpRevs); }
public IrcCommand(IrcUser Source, string Message, string Prefix) { this.Source = Source; this.Prefix = Prefix; if (IndexOfWhitespace(Message) == -1) this.Message = ""; else this.Message = Message.Substring(IndexOfWhitespace(Message) + 1).Trim(); string[] parts = SplitOnWhitespace(Message); this.Command = parts[0].Substring(1); if (parts.Length > 1) this.Parameters = parts.Skip(1).ToArray(); else this.Parameters = new string[0]; }
public IrcCommand(IrcUser Source, string Message, string Prefix) { this.Source = Source; this.Prefix = Prefix; if (IndexOfWhitespace(Message) == -1) { this.Message = ""; } else { this.Message = Message.Substring(IndexOfWhitespace(Message) + 1).Trim(); } string[] parts = SplitOnWhitespace(Message); this.Command = parts[0].Substring(1); if (parts.Length > 1) { this.Parameters = parts.Skip(1).ToArray(); } else { this.Parameters = new string[0]; } }
public UserJoinEventArgs(IrcUser User, IrcChannel Channel) { this.User = User; this.Channel = Channel; }
public IrcBot(string ServerAddress, IrcUser User) : this(ServerAddress, User, ".", Encoding.UTF8) { }
public UserPartEventArgs(IrcUser User, IrcChannel Channel, string Reason) { this.User = User; this.Channel = Channel; this.Reason = Reason; }
private void HandleMessage(IrcMessage message) { // scope in switch...case is stupid in C# string raw, channelName, reason; IrcChannel channel; switch (message.Command) { case "001": if (ConnectionComplete != null) { ConnectionComplete(this, null); } break; case "PRIVMSG": IrcCommand command = null; IrcUserMessage userMessage = new IrcUserMessage(message); if (UserMessageRecieved != null) { UserMessageRecieved(this, new UserMessageEventArgs(userMessage)); } string messagePrepend = "", destination = userMessage.Target; string sourceNick = userMessage.Source.Nick; if (userMessage.Message.Contains("@")) { sourceNick = userMessage.Message.Substring(userMessage.Message.IndexOf("@") + 1).Trim(); userMessage.Message = userMessage.Message.Remove(userMessage.Message.IndexOf("@")); } if (message.Payload.StartsWith("#")) // channel message { messagePrepend = sourceNick + ": \u000f"; } if (userMessage.Message.StartsWith(ControlCharacter)) { command = new IrcCommand(userMessage.Source, userMessage.Message.Trim(), messagePrepend); command.Destination = destination; if (RegisteredCommands.ContainsKey(command.Command.ToLower())) { Thread tempThread = new Thread(() => { try { if (PreCommand != null) { var eventArgs = new CommandEventArgs(command); PreCommand(this, eventArgs); command = eventArgs.Command; } string response = RegisteredCommands[command.Command.ToLower()](command); if (response != null) { SendMessage(destination, messagePrepend + response); } if (PostCommand != null) { PostCommand(this, new CommandEventArgs(command)); } } catch (Exception e) { SendMessage(destination, messagePrepend + "Error: " + e.ToString().Haste()); } }); tempThread.Start(); } } break; case "PING": SendRawMessage("PONG " + message.Payload); ServerPingAddress = message.Payload; LastPingRecieved = DateTime.Now; break; case "PONG": LastPongRecieved = DateTime.Now; break; case "332": // Channel topic raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 1); channelName = raw.Remove(raw.IndexOf(' ')); raw = raw.Substring(raw.IndexOf(' ') + 1).Substring(1); channel = GetChannel(channelName); channel.Topic = raw; break; case "353": raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 3); channelName = raw.Remove(raw.IndexOf(' ')); raw = raw.Substring(raw.IndexOf(':') + 1); string[] names = raw.Split(' '); channel = GetChannel(channelName); foreach (var user in names) { if (user.StartsWith("+")) { channel.Users.Add(user.Substring(1)); } else if (user.StartsWith("@")) { channel.Users.Add(user.Substring(1)); } else { channel.Users.Add(user); } } break; case "366": // End of names raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 1); channelName = raw.Remove(raw.IndexOf(' ')); if (ChannelListRecieved != null) { ChannelListRecieved(this, new ChannelListEventArgs(GetChannel(channelName))); } break; case "MODE": if (message.Payload.StartsWith("#")) { ChannelModeChangeEventArgs eventArgs = new ChannelModeChangeEventArgs(); eventArgs.Source = new IrcUser(message.Prefix); eventArgs.Channel = GetChannel(message.Payload.Remove(message.Payload.IndexOf(' '))); eventArgs.ModeChange = message.Payload.Substring(message.Payload.IndexOf(' ') + 1); if (ChannelModeChanged != null) { ChannelModeChanged(this, eventArgs); } } break; case "NOTICE": if (NoticeRecieved != null) { NoticeRecieved(this, new NoticeEventArgs(new IrcNotice(message))); } break; case "PART": // #IAmABotAMA :\"hello world\" channelName = message.Parameters[0]; channel = GetChannel(channelName); reason = message.Payload.Substring(message.Payload.IndexOf(':') + 1); if (new IrcUser(message.Prefix).Nick != User.Nick) { channel.Users.Remove(new IrcUser(message.Prefix).Nick); if (UserPartedChannel != null) { UserPartedChannel(this, new UserPartEventArgs(new IrcUser(message.Prefix), channel, reason)); } } break; case "JOIN": channelName = message.Parameters[0]; channel = GetChannel(channelName); channel.Users.Add(new IrcUser(message.Prefix).Nick); if (UserJoinedChannel != null) { UserJoinedChannel(this, new UserJoinEventArgs(new IrcUser(message.Prefix), channel)); } break; case "QUIT": reason = message.Payload.Substring(message.Payload.IndexOf(':') + 1); if (new IrcUser(message.Prefix).Nick != User.Nick) { foreach (var c in Channels) { if (c.Users.Contains(new IrcUser(message.Prefix).Nick)) { c.Users.Remove(new IrcUser(message.Prefix).Nick); } } if (UserQuit != null) { UserQuit(this, new UserQuitEventArgs(new IrcUser(message.Prefix), reason)); } } else { if (ServerQuit != null) { ServerQuit(this, null); } // Reconnect if (IrcConnection.Connected) { IrcConnection.Disconnect(false); } IrcConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ReadBuffer = new byte[1024]; ReadBufferIndex = 0; IrcConnection.BeginConnect(ServerHostname, ServerPort, ConnectComplete, null); } break; case "311": // WHOIS response string[] whois = message.Payload.Split(' '); var result = new IrcUser(whois[1], whois[2].Substring(1)); result.Hostname = whois[3]; if (WhoIsCallback != null) { WhoIsCallback(result); WhoIsCallback = null; } break; case "TOPIC": if (TopicCallback != null) { TopicCallback(message.Payload.Substring(message.Payload.IndexOf(":") + 1)); TopicCallback = null; } break; case "NICK": if (NickChanged != null) { NickChanged(this, new NickChangedEventArgs(new IrcUser(message.Prefix), message.Payload)); } break; } }
void JeBotMain_UserQuit(object sender, UserQuitEventArgs e) { // Send users a notice when they change hosts if (e.Reason == "Changing host") ChangingHostNotify = e.User; }
public NickChangedEventArgs(IrcUser user, string newNick) { User = user; NewNick = newNick; }
void JeBotMain_NoticeRecieved(object sender, NoticeEventArgs e) { if (!(e.Notice.Source.Contains("@") && e.Notice.Source.Contains("!"))) // verify that this came from a user return; var source = new IrcUser(e.Notice.Source); if (source.Nick == "NickServ" && e.Notice.Message.Contains("ACC")) // Do user identification { var parts = e.Notice.Message.Split(' '); lock (IdentificationQueueLock) { CommandPendingID callback = null; if (IdentificationQueue.Count != 0) callback = IdentificationQueue.Dequeue(); if (parts[2] == "3") // verified { VerifiedUsers.Add(parts[0]); if (callback != null) callback.Callback(callback.Command); if (NotifyVerification.Contains(parts[0])) { this.SendNotice(parts[0], "You have been successfully identified."); NotifyVerification.Remove(parts[0]); } } } } }
public JeBotMain(string ServerAddress, IrcUser User) : base(ServerAddress, User) { // Event handlers this.ChannelListRecieved += new EventHandler<ChannelListEventArgs>(JeBotMain_ChannelListRecieved); this.UserJoinedChannel += new EventHandler<UserJoinEventArgs>(JeBotMain_UserJoinedChannel); this.UserPartedChannel += new EventHandler<UserPartEventArgs>(JeBotMain_UserPartedChannel); this.UserQuit += new EventHandler<UserQuitEventArgs>(JeBotMain_UserQuit); this.UserMessageRecieved += new EventHandler<UserMessageEventArgs>(JeBotMain_UserMessageRecieved); this.NoticeRecieved += new EventHandler<NoticeEventArgs>(JeBotMain_NoticeRecieved); VerifiedUsers = new List<string>(); NotifyVerification = new List<string>(); Managers = new List<string>(); IdentificationQueue = new Queue<CommandPendingID>(); ManagerCommands = new Dictionary<string, CommandHandler>(); // Userspace commands RegisterCommand("down", DoDown); RegisterCommand("docs", (command) => "https://github.com/irc-bot-framework/JeBot/blob/master/README.md"); RegisterCommand("documentation", (command) => "https://github.com/irc-bot-framework/JeBot/blob/master/README.md"); // alias RegisterCommand("help", (command) => "https://github.com/irc-bot-framework/JeBot/blob/master/README.md"); // alias RegisterCommand("man", (command) => "https://github.com/irc-bot-framework/JeBot/blob/master/README.md"); // alias RegisterCommand("hug", DoHug); RegisterCommand("identify", DoIdentify); RegisterCommand("karma", DoKarma); RegisterCommand("lwjgl", (command) => "Can you use git? Use this script: https://gist.github.com/2086385 No? Read this: http://www.minecraftwiki.net/wiki/Tutorials/Update_LWJGL"); RegisterCommand("mwiki", DoMwiki); RegisterCommand("owner", (command) => "I was created and am maintained by SirCmpwn ([email protected]). Please send him an email if you find a bug or want a new feature."); RegisterCommand("ping", DoPing); RegisterCommand("readers", DoReaders); RegisterCommand("search", DoSearch); RegisterCommand("lucky", DoSearch); // alias RegisterCommand("servers", DoServers); RegisterCommand("source", (command) => "https://github.com/irc-bot-framework/JeBot"); RegisterCommand("status", DoStatus); RegisterCommand("title", DoTitle); RegisterCommand("twitter", DoTwitter); RegisterCommand("tw", DoTwitter); // alias RegisterCommand("weather", (command) => FetchWeather(command.Message) ?? "Unable to fetch weather."); RegisterCommand("whoami", (command) => command.Source.Nick); RegisterCommand("wikipedia", DoWikipedia); RegisterCommand("wiki", DoWikipedia); // alias RegisterCommand("youtube", DoYoutube); RegisterCommand("yt", DoYoutube); // alias RegisterCommand("xkcd", DoXkcd); // Manager commands RegisterManagerCommand("ban", DoBan); RegisterManagerCommand("deop", DoDeop); RegisterManagerCommand("devoice", DoDevoice); RegisterManagerCommand("echo", DoEcho); RegisterManagerCommand("invite", DoInvite); RegisterManagerCommand("join", DoJoin); RegisterManagerCommand("kick", DoKick); RegisterManagerCommand("op", DoOp); RegisterManagerCommand("topic", DoTopic); }
public UserQuitEventArgs(IrcUser User, string Reason) { this.User = User; this.Reason = Reason; }
private void HandleMessage(IrcMessage message) { // scope in switch...case is stupid in C# string raw, channelName, reason; IrcChannel channel; switch (message.Command) { case "001": if (ConnectionComplete != null) ConnectionComplete(this, null); break; case "PRIVMSG": IrcCommand command = null; IrcUserMessage userMessage = new IrcUserMessage(message); if (UserMessageRecieved != null) UserMessageRecieved(this, new UserMessageEventArgs(userMessage)); string messagePrepend = "", destination = userMessage.Target; string sourceNick = userMessage.Source.Nick; if (userMessage.Message.Contains("@")) { sourceNick = userMessage.Message.Substring(userMessage.Message.IndexOf("@") + 1).Trim(); userMessage.Message = userMessage.Message.Remove(userMessage.Message.IndexOf("@")); } if (message.Payload.StartsWith("#")) // channel message messagePrepend = sourceNick + ": \u000f"; if (userMessage.Message.StartsWith(ControlCharacter)) { command = new IrcCommand(userMessage.Source, userMessage.Message.Trim(), messagePrepend); command.Destination = destination; if (RegisteredCommands.ContainsKey(command.Command.ToLower())) { Thread tempThread = new Thread(() => { try { if (PreCommand != null) { var eventArgs = new CommandEventArgs(command); PreCommand(this, eventArgs); command = eventArgs.Command; } string response = RegisteredCommands[command.Command.ToLower()](command); if (response != null) SendMessage(destination, messagePrepend + response); if (PostCommand != null) PostCommand(this, new CommandEventArgs(command)); } catch (Exception e) { SendMessage(destination, messagePrepend + "Error: " + e.ToString().Haste()); } }); tempThread.Start(); } } break; case "PING": SendRawMessage("PONG " + message.Payload); ServerPingAddress = message.Payload; LastPingRecieved = DateTime.Now; break; case "PONG": LastPongRecieved = DateTime.Now; break; case "332": // Channel topic raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 1); channelName = raw.Remove(raw.IndexOf(' ') ); raw = raw.Substring(raw.IndexOf(' ') + 1).Substring(1); channel = GetChannel(channelName); channel.Topic = raw; break; case "353": raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 3); channelName = raw.Remove(raw.IndexOf(' ')); raw = raw.Substring(raw.IndexOf(':') + 1); string[] names = raw.Split(' '); channel = GetChannel(channelName); foreach (var user in names) { if (user.StartsWith("+")) channel.Users.Add(user.Substring(1)); else if (user.StartsWith("@")) channel.Users.Add(user.Substring(1)); else channel.Users.Add(user); } break; case "366": // End of names raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 1); channelName = raw.Remove(raw.IndexOf(' ')); if (ChannelListRecieved != null) ChannelListRecieved(this, new ChannelListEventArgs(GetChannel(channelName))); break; case "MODE": if (message.Payload.StartsWith("#")) { ChannelModeChangeEventArgs eventArgs = new ChannelModeChangeEventArgs(); eventArgs.Source = new IrcUser(message.Prefix); eventArgs.Channel = GetChannel(message.Payload.Remove(message.Payload.IndexOf(' '))); eventArgs.ModeChange = message.Payload.Substring(message.Payload.IndexOf(' ') + 1); if (ChannelModeChanged != null) ChannelModeChanged(this, eventArgs); } break; case "NOTICE": if (NoticeRecieved != null) NoticeRecieved(this, new NoticeEventArgs(new IrcNotice(message))); break; case "PART": // #IAmABotAMA :\"hello world\" channelName = message.Parameters[0]; channel = GetChannel(channelName); reason = message.Payload.Substring(message.Payload.IndexOf(':') + 1); if (new IrcUser(message.Prefix).Nick != User.Nick) { channel.Users.Remove(new IrcUser(message.Prefix).Nick); if (UserPartedChannel != null) UserPartedChannel(this, new UserPartEventArgs(new IrcUser(message.Prefix), channel, reason)); } break; case "JOIN": channelName = message.Parameters[0]; channel = GetChannel(channelName); channel.Users.Add(new IrcUser(message.Prefix).Nick); if (UserJoinedChannel != null) UserJoinedChannel(this, new UserJoinEventArgs(new IrcUser(message.Prefix), channel)); break; case "QUIT": reason = message.Payload.Substring(message.Payload.IndexOf(':') + 1); if (new IrcUser(message.Prefix).Nick != User.Nick) { foreach (var c in Channels) { if (c.Users.Contains(new IrcUser(message.Prefix).Nick)) c.Users.Remove(new IrcUser(message.Prefix).Nick); } if (UserQuit != null) UserQuit(this, new UserQuitEventArgs(new IrcUser(message.Prefix), reason)); } else { if (ServerQuit != null) ServerQuit(this, null); // Reconnect if (IrcConnection.Connected) IrcConnection.Disconnect(false); IrcConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ReadBuffer = new byte[1024]; ReadBufferIndex = 0; IrcConnection.BeginConnect(ServerHostname, ServerPort, ConnectComplete, null); } break; case "311": // WHOIS response string[] whois = message.Payload.Split(' '); var result = new IrcUser(whois[1], whois[2].Substring(1)); result.Hostname = whois[3]; if (WhoIsCallback != null) { WhoIsCallback(result); WhoIsCallback = null; } break; case "TOPIC": if (TopicCallback != null) { TopicCallback(message.Payload.Substring(message.Payload.IndexOf(":") + 1)); TopicCallback = null; } break; case "NICK": if (NickChanged != null) NickChanged(this, new NickChangedEventArgs(new IrcUser(message.Prefix), message.Payload)); break; } }
static void Main(string[] args) { LogWriter = new StreamWriter("log.txt", true); Settings = XDocument.Load("config.xml"); string address = Settings.Root.Element("server").Attribute("address").Value; IrcUser user = new IrcUser( Settings.Root.Element("server").Element("user").Element("nick").Value, Settings.Root.Element("server").Element("user").Element("user").Value, Settings.Root.Element("server").Element("user").Element("password").Value); JeBot = new JeBotMain(address, user); foreach (var manager in Settings.Root.Element("server").Element("managers").Elements("manager")) JeBot.Managers.Add(manager.Attribute("name").Value); JeBot.ConnectionComplete += new EventHandler(jebot_ConnectionComplete); JeBot.RawMessageRecieved += new EventHandler<RawMessageEventArgs>(JeBot_RawMessageRecieved); JeBot.RawMessageSent += new EventHandler<RawMessageEventArgs>(JeBot_RawMessageRecieved); JeBot.Run(); while (true) Thread.Sleep(1); }
void JeBotMain_UserJoinedChannel(object sender, UserJoinEventArgs e) { if (ChangingHostNotify != null) { // Notify the user if they need to reconfigure their client for changing hosts if (e.User.Nick == ChangingHostNotify.Nick) { this.SendNotice(e.User.Nick, "Please reconfigure your client to use your NickServ password as your server password to avoid spamming when you join. See http://freenode.net/faq.shtml#nocloakonjoin"); ChangingHostNotify = null; } } // Identify this user if they are included in the manager list if (Managers.Contains(e.User.Nick)) this.SendMessage("NickServ", "ACC " + e.User.Nick); }
public FrankerZBot(string ServerAddress, IrcUser User) : base(ServerAddress, User) { RegisterCommand("ping", (command) => "pong"); }
public BotController(IrcUser user, string address, string channel) { this.user = user; this.address = address; this.channel = channel; }