// ---------------- Functions ---------------- /// <summary> /// <see cref="IIrcHandler.HandleEvent(HandlerArgs)"/> /// </summary> public void HandleEvent(HandlerArgs args) { if (chaskisRegex.IsMatch(args.Line) == false) { return; } ChaskisEvent e = ChaskisEvent.FromXml(args.Line); string targetPlugin = e.DestinationPlugin; // We'll handle the event if it is targeted specifically to this plugin, OR it is a broadcast event. bool sendEvent = (this.creatorPlugin == targetPlugin) || (string.IsNullOrEmpty(targetPlugin)); if (e.SourceType == ChaskisEventSource.CORE) { sendEvent = true; } else if (this.expectedPlugin != null) { sendEvent &= (e.SourcePlugin == this.expectedPlugin); } // BCAST Events MUST be subscribed to a specific source plugin. // Otherwise, what happens if this happens (remember, an empty string for dest_plugin is a bcast): // <chaskis_event source_type="PLUGIN" source_plugin="plugin1" dest_plugin=""> // <chaskis_event source_type="PLUGIN" source_plugin="plugin2" dest_plugin=""> // Both events will trigger even though they came from two different plugins... // That is probably not good. // For BCAST events, the handler should subscribe to a specific source plugin. // This way, plugin1 will trigger one handler, while plugin 2 will // trigger a different handler. // // The exception to this rule is events from the CORE, which target ALL plugins. else if (string.IsNullOrEmpty(targetPlugin)) { sendEvent = false; } if (sendEvent) { ChaskisEventHandlerLineActionArgs eventArgs = new ChaskisEventHandlerLineActionArgs( e.SourcePlugin, e.Args, e.PassThroughArgs, args.IrcWriter ); this.lineAction(eventArgs); } }
private void AddCoreEvent(string eventStr) { ChaskisEvent e = new ChaskisEvent( ChaskisEventSource.CORE, ChaskisEventProtocol.IRC.ToString(), string.Empty, // For BCAST new Dictionary <string, string>() { ["event_id"] = eventStr, ["server"] = this.Config.Server, ["nick"] = this.Config.Nick }, null ); this.SendChaskisEvent(e); }
// ---------------- Constructor ---------------- internal static ChaskisEvent FromXml(string xmlString) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlString); ChaskisEvent e = new ChaskisEvent(); XmlNode rootNode = doc.DocumentElement; if (rootNode.Name != XmlElementName) { throw new XmlException( "Root XML node should be named \"" + XmlElementName + "\". Got: " + rootNode.Name ); } // Get Attributes foreach (XmlAttribute attr in rootNode.Attributes) { switch (attr.Name) { case "source_type": ChaskisEventSource sourceType; if (Enum.TryParse(attr.Value, out sourceType)) { e.SourceType = sourceType; } break; case "source_plugin": e.SourcePlugin = attr.Value; break; case "dest_plugin": e.DestinationPlugin = attr.Value; break; } } foreach (XmlNode node in rootNode.ChildNodes) { switch (node.Name) { case "args": foreach (XmlNode argNode in node.ChildNodes) { e.Args[argNode.Name] = argNode.InnerText; } break; case "passthrough_args": foreach (XmlNode argNode in node.ChildNodes) { e.PassThroughArgs[argNode.Name] = argNode.InnerText; } break; } } return(e); }
/// <summary> /// Adds the given Chaskis to the event queue. /// </summary> public void SendChaskisEvent(ChaskisEvent e) { string s = e.ToString(); this.OnReadLine(s); }