/// <summary> /// Initializes a new instance of the <see cref="CommandPacket"/> class. /// </summary> /// <param name="trigger">The bot trigger.</param> /// <param name="botUserName">UserName for the bot.</param> /// <param name="packet">The packet.</param> public CommandPacket(string trigger, string botUserName, ServerPacket packet) : base(packet) { Guard.Against(string.IsNullOrEmpty(trigger), "Trigger cannot be empty or null."); Guard.Against(string.IsNullOrEmpty(botUserName), "Bot user name cannot be empty or null."); string command = string.Empty; string message = base.Message; // this is set by the base class // if we have an empty message, bail if (string.IsNullOrEmpty(message)) return; // check if this is a trig check which is a special command bool isTrigCheck = Regex.IsMatch(message.Trim(), botUserName + @":\s+" + TrigCheckCommand, RegexOptions.IgnoreCase); if (isTrigCheck) { command = "trigcheck"; message = ""; } else { try { // get the command and message var result = ParseCommandMessage(trigger, message); command = result.Item1; message = result.Item2; } catch { } } // set values Command = command; Message = message; }
/// <summary> /// Invokes a command. /// </summary> /// <param name="command">The command.</param> private void InvokeCommand(string command) { var botSettings = Module.UIContext.BotSettings; // get the settings var botTrigger = botSettings.Trigger; var username = botSettings.UserName; var owner = botSettings.Owner; // create the command packet as if it from from the server string data = string.Format("recv chat:{0}\n\nmsg main\nfrom={1}\n\n{2}", chatroom.Name, owner, command); ServerPacket serverPacket = new ServerPacket(data); EventPacket eventPacket = new EventPacket(serverPacket); CommandPacket commandPacket = new CommandPacket(botTrigger, username, serverPacket); // create a plugin context PluginContext context = Module.UIContext.PluginContextFactory.Create(eventPacket, null); // send the command! Oberon.Core.Plugins.System.ValidateAndInvokeCommand(commandPacket, context); }
/// <summary> /// Logs in to the dAmn server. /// </summary> /// <param name="username">Username to use to log in.</param> /// <param name="authToken">AuthToken to use to log in.</param> /// <returns>True if login was successful. Otherwise fals.</returns> protected bool Login(string username) { Send(BuildPacket("login", username, "pk=" + this.authToken)); System.Threading.Thread.Sleep(300); string loginResponse = Read(); LoginPacket = new ServerPacket(loginResponse); if (loginResponse.ToLower().Contains("e=ok")) return true; else return false; }
/// <summary> /// Recods the specified packet in the chatroom log. /// </summary> /// <param name="packet"></param> public void Log(ServerPacket packet) { if (!Enabled) return; if (!IsCurrentDateValid()) Reset(); // get the packet details var subPacket = packet.GetSubPacket(); string username = string.Empty; string reason = "reason unknown"; if (subPacket != null) { if (subPacket.args.ContainsKey("from")) username = subPacket.args["from"]; if (subPacket.args.ContainsKey("r")) reason = subPacket.args["r"]; } // message to be written string message = null; switch (packet.PacketType) { case PacketType.Disconnect: message = "Experienced an unexpected disconnect!"; break; case PacketType.Chat: message = string.Format("<{0}> {1}", username, subPacket.body); break; case PacketType.Action: message = string.Format("{0} {1} {2}", "", username, subPacket.body); break; case PacketType.Part: message = string.Format("** Bot has left the chatroom {0}. {1}", this.ChatRoomName, reason); break; case PacketType.MemberJoin: message = username + " joined."; break; case PacketType.MemberPart: message = string.Format("** {0} has left. [{1}]", username, reason); break; case PacketType.Title: message = "Room topic is: " + packet.body; break; case PacketType.PrivChange: message = string.Format("** {0} has been made a member of {1} by {2} *", username, subPacket.args["pc"], subPacket.args["by"]); break; case PacketType.AdminCreate: message = string.Format("Privclass '{0}' was created by {1}.", subPacket.args["name"], subPacket.args["by"]); break; case PacketType.AdminUpdate: message = string.Format("** privilege class {0} has been updated by {1} with {2}", subPacket.args["name"], subPacket.args["by"], subPacket.args["privs"]); break; case PacketType.AdminRename: message = string.Format("The privclass '{0}' was renamed to '{1}' by {2}.", subPacket.args["prev"], subPacket.args["name"], subPacket.args["by"]); break; case PacketType.AdminMove: message = string.Format("Users moved from privclass '{1}' to '{2}' by {3}.", subPacket.args["prev"], subPacket.args["name"], subPacket.args["by"]); break; case PacketType.AdminRemove: message = string.Format("The privclass '{0}' was removed by {1}.", subPacket.args["name"], subPacket.args["by"]); break; case PacketType.AdminError: message = string.Format("Admin error. The command '{0}' returned: {1}", subPacket.body, subPacket.args["e"]); break; case PacketType.Kicked: message = string.Format("Bot was kicked from {0}. {1}", this.ChatRoomName, reason); break; } // write it out if (!string.IsNullOrEmpty(message)) { this.Writer.WriteLine(string.Format("[{0:M/dd/yyyy H:mm:ss}] {1}", DateTime.Now, message)); this.Writer.Flush(); } // trigger the event if (OnPacketReceived != null) OnPacketReceived(this, new ServerPacketEventArgs(packet)); }
public ServerPacketEventArgs(ServerPacket packet) { this.Packet = packet; }
/// <summary> /// Parses packet header to determine what the type is. /// </summary> /// <returns>Packet type.</returns> private PacketType GetPacketType() { // get packet header string[] tokens = this.raw.Split(new string[] { "\n\n" }, StringSplitOptions.None); string header = tokens[0]; // get each line string[] lines = header.Split('\n'); string pktId = lines[0]; string[] tmp = pktId.Split(new char[] { ':' }, 2); if (tmp[0].ToLower() == "recv chat") { Packet subPacket = new ServerPacket(this.body); if (subPacket.cmd == "msg") { return PacketType.Chat; } else if (subPacket.cmd == "action") { return PacketType.Action; } else if (subPacket.cmd == "join") { return PacketType.MemberJoin; } else if (subPacket.cmd == "part") { return PacketType.MemberPart; } else if (subPacket.cmd == "privchg") { return PacketType.PrivChange; } else if (subPacket.cmd == "kicked") { return PacketType.MemberKick; } else if (subPacket.cmd == "login") { return Net.PacketType.Login; } else if (subPacket.cmd == "admin") { if (subPacket.param == "create") return PacketType.AdminCreate; else if (subPacket.param == "update") return PacketType.AdminUpdate; else if (subPacket.param == "rename") return PacketType.AdminRename; else if (subPacket.param == "move") return PacketType.AdminMove; else if (subPacket.param == "remove") return PacketType.AdminRemove; else if (subPacket.param == "show") return PacketType.AdminShow; else if (subPacket.param == "privclass") return PacketType.AdminError; else return PacketType.Unknown; } else { return PacketType.Unknown; } } else if (tmp[0].ToLower() == "recv pchat") { return PacketType.Chat; } else if (tmp[0].ToLower() == "disconnect") { return PacketType.Disconnect; } else if (tmp[0].ToLower() == "shutdown") { return PacketType.Shutdown; // special bot packet type } else if (tmp[0].ToLower() == "join chat") { return PacketType.Join; } else if (tmp[0].ToLower() == "part chat") { return PacketType.Part; } else if (tmp[0].ToLower() == "kicked chat") { return PacketType.Kicked; } else if (tmp[0].ToLower() == "kicked pchat") { return PacketType.Kicked; } else if (tmp[0].ToLower() == "send chat") { return PacketType.ErrorSend; } else if (tmp[0].ToLower() == "send pchat") { return PacketType.ErrorSend; } else if (tmp[0].ToLower() == "kick chat") { return PacketType.ErrorKick; } else if (tmp[0].ToLower() == "kick pchat") { return PacketType.ErrorKick; } else if (tmp[0].ToLower() == "get pchat") { return PacketType.ErrorGet; } else if (tmp[0].ToLower() == "set pchat") { return PacketType.ErrorSet; } else if (tmp[0].ToLower() == "kill login") { return PacketType.ErrorKill; } else if (tmp[0].ToLower() == "property login") { return PacketType.Whois; } else if (tmp[0] == "property chat") { tmp = lines[1].Split(new char[] { '=' }, 2); if (tmp[0] == "p" & tmp[1] == "topic") { return PacketType.Topic; } else if (tmp[0] == "p" & tmp[1] == "title") { return PacketType.Title; } else if (tmp[0] == "p" & tmp[1] == "privclasses") { return PacketType.PrivClasses; } else if (tmp[0] == "p" & tmp[1] == "members") { return PacketType.MemberList; } else { return PacketType.Unknown; } } else if (tmp[0] == "property pchat") { tmp = lines[1].Split(new char[] { '=' }, 2); if (tmp[0] == "p" & tmp[1] == "members") { return PacketType.MemberList; } else if (tmp[0] == "p" & tmp[1] == "title") { return PacketType.Title; } else if (tmp[0] == "p" & tmp[1] == "topic") { return PacketType.Topic; } else if (tmp[0] == "p" & tmp[1] == "privclasses") { return PacketType.PrivClasses; } else { return PacketType.Unknown; } } else if (tmp[0].StartsWith("login ")) { return PacketType.Login; } else if (pktId == "ping") { return PacketType.Ping; } return PacketType.Unknown; }
/// <summary> /// Parses the specified data. /// </summary> /// <param name="data">The data.</param> /// <param name="parseExceptions">Exceptions that occurred during parsing.</param> /// <returns><see cref="ServerPacket"/>.</returns> public static IEnumerable<ServerPacket> Parse(string data, List<Exception> parseExceptions) { foreach (var rawPacket in data.Split('\0').Where(s => s != "\0")) { if (string.IsNullOrWhiteSpace(rawPacket)) continue; ServerPacket packet = null; try { packet = new ServerPacket(rawPacket); } catch (Exception ex) { parseExceptions.Add(ex); // malformed packet! discard it continue; } yield return packet; } }
/// <summary> /// Processes the packet. /// </summary> /// <param name="packet">The packet.</param> public void ProcessPacket(ServerPacket packet) { // get the event handlers for this packet type var handlerBag = this.pluginHandler.GetEventHandlers(packet.PacketType); // see if we have a system handler to execute - if so, run it first foreach (var systemHandlerData in handlerBag.SystemHandlers) { ExecuteEventHandler(systemHandlerData, packet); } // execute everything else in parallel var enabledHandlers = handlerBag.Handlers.Where(h => h.Target.Enabled); Parallel.ForEach(enabledHandlers, (handlerData) => { ExecuteEventHandler(handlerData, packet); }); }
/// <summary> /// Executes the event handler. /// </summary> /// <param name="handler">The handler.</param> /// <param name="packet">The packet.</param> public void ExecuteEventHandler(EventData eventData, ServerPacket packet) { string ns = null; if (!string.IsNullOrEmpty(packet.param)) ns = MessageHelper.DeformatChat(packet.param); var eventPacket = new EventPacket(packet); var context = GenerateContext(eventPacket, eventData.Target); var handler = eventData.EventHandler; try { handler(eventPacket, context); } catch (Exception ex) { Logger.Error(ex.Message, ex); console.WriteLine("Error occured. See bot log for details.", Style.Error); } }
/// <summary> /// Initializes a new instance of the <see cref="Listener"/> class. /// </summary> /// <param name="pluginHandler">The dAmn client.</param> public Listener(Client client, IDispatcher dispatcher) { this.client = client; this.dispatcher = dispatcher; this.shutdownPacket = new ServerPacket("shutdown\ne=bot shutdown"); }