diff options
author | SignpostMarv | 2012-10-23 15:42:16 +0100 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2012-10-26 02:08:00 +0100 |
commit | e977761071a2d614a9a621437fbf86479b414759 (patch) | |
tree | 3d9e6a65c9462ca8f6439184887a632dc041eb27 /OpenSim/Region/CoreModules/Scripting/WorldComm | |
parent | Formatting and casing correction in WorldCommModule, trailing new line in OSS... (diff) | |
download | opensim-SC-e977761071a2d614a9a621437fbf86479b414759.zip opensim-SC-e977761071a2d614a9a621437fbf86479b414759.tar.gz opensim-SC-e977761071a2d614a9a621437fbf86479b414759.tar.bz2 opensim-SC-e977761071a2d614a9a621437fbf86479b414759.tar.xz |
adding ability for listeners to be filtered by regular expressions and a general-purpose function to see if a given string matches a given regex
Diffstat (limited to 'OpenSim/Region/CoreModules/Scripting/WorldComm')
-rw-r--r-- | OpenSim/Region/CoreModules/Scripting/WorldComm/WorldCommModule.cs | 129 |
1 files changed, 110 insertions, 19 deletions
diff --git a/OpenSim/Region/CoreModules/Scripting/WorldComm/WorldCommModule.cs b/OpenSim/Region/CoreModules/Scripting/WorldComm/WorldCommModule.cs index c68ed6b..cf0eb2a 100644 --- a/OpenSim/Region/CoreModules/Scripting/WorldComm/WorldCommModule.cs +++ b/OpenSim/Region/CoreModules/Scripting/WorldComm/WorldCommModule.cs | |||
@@ -28,6 +28,7 @@ | |||
28 | using System; | 28 | using System; |
29 | using System.Collections; | 29 | using System.Collections; |
30 | using System.Collections.Generic; | 30 | using System.Collections.Generic; |
31 | using System.Text.RegularExpressions; | ||
31 | using Nini.Config; | 32 | using Nini.Config; |
32 | using OpenMetaverse; | 33 | using OpenMetaverse; |
33 | using OpenSim.Framework; | 34 | using OpenSim.Framework; |
@@ -170,12 +171,42 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
170 | /// <param name="hostID">UUID of the SceneObjectPart</param> | 171 | /// <param name="hostID">UUID of the SceneObjectPart</param> |
171 | /// <param name="channel">channel to listen on</param> | 172 | /// <param name="channel">channel to listen on</param> |
172 | /// <param name="name">name to filter on</param> | 173 | /// <param name="name">name to filter on</param> |
173 | /// <param name="id">key to filter on (user given, could be totally faked)</param> | 174 | /// <param name="id"> |
175 | /// key to filter on (user given, could be totally faked) | ||
176 | /// </param> | ||
174 | /// <param name="msg">msg to filter on</param> | 177 | /// <param name="msg">msg to filter on</param> |
175 | /// <returns>number of the scripts handle</returns> | 178 | /// <returns>number of the scripts handle</returns> |
176 | public int Listen(uint localID, UUID itemID, UUID hostID, int channel, string name, UUID id, string msg) | 179 | public int Listen(uint localID, UUID itemID, UUID hostID, int channel, |
180 | string name, UUID id, string msg) | ||
177 | { | 181 | { |
178 | return m_listenerManager.AddListener(localID, itemID, hostID, channel, name, id, msg); | 182 | return m_listenerManager.AddListener(localID, itemID, hostID, |
183 | channel, name, id, msg); | ||
184 | } | ||
185 | |||
186 | /// <summary> | ||
187 | /// Create a listen event callback with the specified filters. | ||
188 | /// The parameters localID,itemID are needed to uniquely identify | ||
189 | /// the script during 'peek' time. Parameter hostID is needed to | ||
190 | /// determine the position of the script. | ||
191 | /// </summary> | ||
192 | /// <param name="localID">localID of the script engine</param> | ||
193 | /// <param name="itemID">UUID of the script engine</param> | ||
194 | /// <param name="hostID">UUID of the SceneObjectPart</param> | ||
195 | /// <param name="channel">channel to listen on</param> | ||
196 | /// <param name="name">name to filter on</param> | ||
197 | /// <param name="id"> | ||
198 | /// key to filter on (user given, could be totally faked) | ||
199 | /// </param> | ||
200 | /// <param name="msg">msg to filter on</param> | ||
201 | /// <param name="regexBitfield"> | ||
202 | /// Bitfield indicating which strings should be processed as regex. | ||
203 | /// </param> | ||
204 | /// <returns>number of the scripts handle</returns> | ||
205 | public int Listen(uint localID, UUID itemID, UUID hostID, int channel, | ||
206 | string name, UUID id, string msg, int regexBitfield) | ||
207 | { | ||
208 | return m_listenerManager.AddListener(localID, itemID, hostID, | ||
209 | channel, name, id, msg, regexBitfield); | ||
179 | } | 210 | } |
180 | 211 | ||
181 | /// <summary> | 212 | /// <summary> |
@@ -465,10 +496,20 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
465 | m_curlisteners = 0; | 496 | m_curlisteners = 0; |
466 | } | 497 | } |
467 | 498 | ||
468 | public int AddListener(uint localID, UUID itemID, UUID hostID, int channel, string name, UUID id, string msg) | 499 | public int AddListener(uint localID, UUID itemID, UUID hostID, |
500 | int channel, string name, UUID id, string msg) | ||
501 | { | ||
502 | return AddListener(localID, itemID, hostID, channel, name, id, | ||
503 | msg, 0); | ||
504 | } | ||
505 | |||
506 | public int AddListener(uint localID, UUID itemID, UUID hostID, | ||
507 | int channel, string name, UUID id, string msg, | ||
508 | int regexBitfield) | ||
469 | { | 509 | { |
470 | // do we already have a match on this particular filter event? | 510 | // do we already have a match on this particular filter event? |
471 | List<ListenerInfo> coll = GetListeners(itemID, channel, name, id, msg); | 511 | List<ListenerInfo> coll = GetListeners(itemID, channel, name, id, |
512 | msg); | ||
472 | 513 | ||
473 | if (coll.Count > 0) | 514 | if (coll.Count > 0) |
474 | { | 515 | { |
@@ -485,7 +526,9 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
485 | 526 | ||
486 | if (newHandle > 0) | 527 | if (newHandle > 0) |
487 | { | 528 | { |
488 | ListenerInfo li = new ListenerInfo(newHandle, localID, itemID, hostID, channel, name, id, msg); | 529 | ListenerInfo li = new ListenerInfo(newHandle, localID, |
530 | itemID, hostID, channel, name, id, msg, | ||
531 | regexBitfield); | ||
489 | 532 | ||
490 | List<ListenerInfo> listeners; | 533 | List<ListenerInfo> listeners; |
491 | if (!m_listeners.TryGetValue(channel,out listeners)) | 534 | if (!m_listeners.TryGetValue(channel,out listeners)) |
@@ -626,6 +669,22 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
626 | return -1; | 669 | return -1; |
627 | } | 670 | } |
628 | 671 | ||
672 | /// These are duplicated from ScriptBaseClass | ||
673 | /// http://opensimulator.org/mantis/view.php?id=6106#c21945 | ||
674 | #region Constants for the bitfield parameter of osListenRegex | ||
675 | |||
676 | /// <summary> | ||
677 | /// process name parameter as regex | ||
678 | /// </summary> | ||
679 | public const int OS_LISTEN_REGEX_NAME = 0x1; | ||
680 | |||
681 | /// <summary> | ||
682 | /// process message parameter as regex | ||
683 | /// </summary> | ||
684 | public const int OS_LISTEN_REGEX_MESSAGE = 0x2; | ||
685 | |||
686 | #endregion | ||
687 | |||
629 | // Theres probably a more clever and efficient way to | 688 | // Theres probably a more clever and efficient way to |
630 | // do this, maybe with regex. | 689 | // do this, maybe with regex. |
631 | // PM2008: Ha, one could even be smart and define a specialized Enumerator. | 690 | // PM2008: Ha, one could even be smart and define a specialized Enumerator. |
@@ -651,7 +710,10 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
651 | { | 710 | { |
652 | continue; | 711 | continue; |
653 | } | 712 | } |
654 | if (li.GetName().Length > 0 && !li.GetName().Equals(name)) | 713 | if (li.GetName().Length > 0 && ( |
714 | ((li.GetRegexBitfield() & OS_LISTEN_REGEX_NAME) != OS_LISTEN_REGEX_NAME && !li.GetName().Equals(name)) || | ||
715 | ((li.GetRegexBitfield() & OS_LISTEN_REGEX_NAME) == OS_LISTEN_REGEX_NAME && !Regex.IsMatch(name, li.GetName())) | ||
716 | )) | ||
655 | { | 717 | { |
656 | continue; | 718 | continue; |
657 | } | 719 | } |
@@ -659,7 +721,10 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
659 | { | 721 | { |
660 | continue; | 722 | continue; |
661 | } | 723 | } |
662 | if (li.GetMessage().Length > 0 && !li.GetMessage().Equals(msg)) | 724 | if (li.GetMessage().Length > 0 && ( |
725 | ((li.GetRegexBitfield() & OS_LISTEN_REGEX_MESSAGE) != OS_LISTEN_REGEX_MESSAGE && !li.GetMessage().Equals(msg)) || | ||
726 | ((li.GetRegexBitfield() & OS_LISTEN_REGEX_MESSAGE) == OS_LISTEN_REGEX_MESSAGE && !Regex.IsMatch(msg, li.GetMessage())) | ||
727 | )) | ||
663 | { | 728 | { |
664 | continue; | 729 | continue; |
665 | } | 730 | } |
@@ -692,10 +757,13 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
692 | { | 757 | { |
693 | int idx = 0; | 758 | int idx = 0; |
694 | Object[] item = new Object[6]; | 759 | Object[] item = new Object[6]; |
760 | int dataItemLength = 6; | ||
695 | 761 | ||
696 | while (idx < data.Length) | 762 | while (idx < data.Length) |
697 | { | 763 | { |
698 | Array.Copy(data, idx, item, 0, 6); | 764 | dataItemLength = (idx + 7 == data.Length || (idx + 7 < data.Length && data[idx + 7] is bool)) ? 7 : 6; |
765 | item = new Object[dataItemLength]; | ||
766 | Array.Copy(data, idx, item, 0, dataItemLength); | ||
699 | 767 | ||
700 | ListenerInfo info = | 768 | ListenerInfo info = |
701 | ListenerInfo.FromData(localID, itemID, hostID, item); | 769 | ListenerInfo.FromData(localID, itemID, hostID, item); |
@@ -707,7 +775,7 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
707 | m_listeners[(int)item[2]].Add(info); | 775 | m_listeners[(int)item[2]].Add(info); |
708 | } | 776 | } |
709 | 777 | ||
710 | idx+=6; | 778 | idx+=dataItemLength; |
711 | } | 779 | } |
712 | } | 780 | } |
713 | } | 781 | } |
@@ -723,19 +791,33 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
723 | private UUID m_id; // ID to filter messages from | 791 | private UUID m_id; // ID to filter messages from |
724 | private string m_name; // Object name to filter messages from | 792 | private string m_name; // Object name to filter messages from |
725 | private string m_message; // The message | 793 | private string m_message; // The message |
794 | private int m_regexBitfield; // The regex bitfield | ||
726 | 795 | ||
727 | public ListenerInfo(int handle, uint localID, UUID ItemID, UUID hostID, int channel, string name, UUID id, string message) | 796 | public ListenerInfo(int handle, uint localID, UUID ItemID, UUID hostID, int channel, string name, UUID id, string message) |
728 | { | 797 | { |
729 | Initialise(handle, localID, ItemID, hostID, channel, name, id, message); | 798 | Initialise(handle, localID, ItemID, hostID, channel, name, id, |
799 | message, 0); | ||
800 | } | ||
801 | |||
802 | public ListenerInfo(int handle, uint localID, UUID ItemID, | ||
803 | UUID hostID, int channel, string name, UUID id, | ||
804 | string message, int regexBitfield) | ||
805 | { | ||
806 | Initialise(handle, localID, ItemID, hostID, channel, name, id, | ||
807 | message, regexBitfield); | ||
730 | } | 808 | } |
731 | 809 | ||
732 | public ListenerInfo(ListenerInfo li, string name, UUID id, string message) | 810 | public ListenerInfo(ListenerInfo li, string name, UUID id, string message) |
733 | { | 811 | { |
734 | Initialise(li.m_handle, li.m_localID, li.m_itemID, li.m_hostID, li.m_channel, name, id, message); | 812 | Initialise(li.m_handle, li.m_localID, li.m_itemID, li.m_hostID, li.m_channel, name, id, message, 0); |
813 | } | ||
814 | |||
815 | public ListenerInfo(ListenerInfo li, string name, UUID id, string message, int regexBitfield) | ||
816 | { | ||
817 | Initialise(li.m_handle, li.m_localID, li.m_itemID, li.m_hostID, li.m_channel, name, id, message, regexBitfield); | ||
735 | } | 818 | } |
736 | 819 | ||
737 | private void Initialise(int handle, uint localID, UUID ItemID, UUID hostID, int channel, string name, | 820 | private void Initialise(int handle, uint localID, UUID ItemID, UUID hostID, int channel, string name, UUID id, string message, int regexBitfield) |
738 | UUID id, string message) | ||
739 | { | 821 | { |
740 | m_active = true; | 822 | m_active = true; |
741 | m_handle = handle; | 823 | m_handle = handle; |
@@ -746,11 +828,12 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
746 | m_name = name; | 828 | m_name = name; |
747 | m_id = id; | 829 | m_id = id; |
748 | m_message = message; | 830 | m_message = message; |
831 | m_regexBitfield = regexBitfield; | ||
749 | } | 832 | } |
750 | 833 | ||
751 | public Object[] GetSerializationData() | 834 | public Object[] GetSerializationData() |
752 | { | 835 | { |
753 | Object[] data = new Object[6]; | 836 | Object[] data = new Object[7]; |
754 | 837 | ||
755 | data[0] = m_active; | 838 | data[0] = m_active; |
756 | data[1] = m_handle; | 839 | data[1] = m_handle; |
@@ -758,16 +841,19 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
758 | data[3] = m_name; | 841 | data[3] = m_name; |
759 | data[4] = m_id; | 842 | data[4] = m_id; |
760 | data[5] = m_message; | 843 | data[5] = m_message; |
844 | data[6] = m_regexBitfield; | ||
761 | 845 | ||
762 | return data; | 846 | return data; |
763 | } | 847 | } |
764 | 848 | ||
765 | public static ListenerInfo FromData(uint localID, UUID ItemID, UUID hostID, Object[] data) | 849 | public static ListenerInfo FromData(uint localID, UUID ItemID, UUID hostID, Object[] data) |
766 | { | 850 | { |
767 | ListenerInfo linfo = new ListenerInfo((int)data[1], localID, | 851 | ListenerInfo linfo = new ListenerInfo((int)data[1], localID, ItemID, hostID, (int)data[2], (string)data[3], (UUID)data[4], (string)data[5]); |
768 | ItemID, hostID, (int)data[2], (string)data[3], | 852 | linfo.m_active = (bool)data[0]; |
769 | (UUID)data[4], (string)data[5]); | 853 | if (data.Length >= 7) |
770 | linfo.m_active=(bool)data[0]; | 854 | { |
855 | linfo.m_regexBitfield = (int)data[6]; | ||
856 | } | ||
771 | 857 | ||
772 | return linfo; | 858 | return linfo; |
773 | } | 859 | } |
@@ -826,5 +912,10 @@ namespace OpenSim.Region.CoreModules.Scripting.WorldComm | |||
826 | { | 912 | { |
827 | return m_id; | 913 | return m_id; |
828 | } | 914 | } |
915 | |||
916 | public int GetRegexBitfield() | ||
917 | { | ||
918 | return m_regexBitfield; | ||
919 | } | ||
829 | } | 920 | } |
830 | } | 921 | } |