Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
Avatar
rfog #1
Member since Aug 2012 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: Is it possible to filter by chatstate?
Is there a way to filter a Message reception when it contains a valid Chatstate member?

I mean create a filter to receive only chatstate changes and launch an event as we can filter by Jid instead of subscribe to OnMessage event and manually separate state stanzas from other chat stuff.
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
There is not such a build in function in MatriX for WP7. There is a powerful XPath filter in the full version of Matrix, but WP7 supports no XPath. But you can write your own filter classes similar to MessageFilter, IqFilter and PresenceFilter.

Here is a small example how the MessageFilter works. Based on this example it should be easy to write your own filter classes.

  1. using System;
  2. using System.Collections.Generic;
  3. using Matrix.Xmpp.Client;
  4.  
  5. namespace Matrix
  6. {
  7.     class FilterData
  8.     {    
  9.         internal EventHandler<MessageEventArgs> MessageCallback;
  10.         internal object State;
  11.         internal IComparer<Jid> JidComparer;
  12.     }
  13.  
  14.     public class MyFilter
  15.     {
  16.         private XmppClient XmppClient;
  17.         private Dictionary<Jid, FilterData> JidDictFilter = new Dictionary<Jid, FilterData>();
  18.  
  19.         private object LockObjDictionaries = new object();
  20.  
  21.         public MessageFilter(XmppClient con)
  22.         {
  23.             XmppClient = con;
  24.             XmppClient.OnMessage += XmppConOnMessage;
  25.         }
  26.  
  27.         void XmppConOnMessage(object sender, MessageEventArgs e)
  28.         {
  29.             if (e.Message == null)
  30.                 return;
  31.  
  32.             lock (LockObjDictionaries)
  33.             {
  34.                 var values = new FilterData[JidDictFilter.Count];
  35.                 var keys = new Jid[JidDictFilter.Count];
  36.                
  37.                 JidDictFilter.Values.CopyTo(values, 0);
  38.                 JidDictFilter.Keys.CopyTo(keys, 0);
  39.                
  40.                 for (int i = 0; i < values.Length; i++)
  41.                 {
  42.                     var fd = values[i];
  43.                     if (fd.JidComparer.Compare(keys[i], e.Message.From) == 0)
  44.                     {
  45.                         // Execute the callback
  46.                         e.State = fd.State;
  47.                         fd.MessageCallback(this, new MessageEventArgs{Message = e.Message, State = fd.State });
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.  
  53.         public bool Add(Jid jid, IComparer<Jid> comparer, EventHandler<MessageEventArgs> eh)
  54.         {
  55.             return Add(jid, comparer, eh, null);
  56.         }
  57.  
  58.         public bool Add(Jid jid, IComparer<Jid> comparer, EventHandler<MessageEventArgs> eh, object state)
  59.         {
  60.             lock (LockObjDictionaries)
  61.             {
  62.                 if (JidDictFilter.ContainsKey(jid))
  63.                     return true;
  64.  
  65.                 var fd = new FilterData { MessageCallback = eh, JidComparer = comparer, State = state };
  66.                 JidDictFilter.Add(jid, fd);
  67.                 return true;
  68.             }
  69.         }
  70.  
  71.         public void Remove(Jid jid)
  72.         {
  73.             lock (LockObjDictionaries)
  74.             {
  75.                 if (JidDictFilter.ContainsKey(jid))
  76.                     JidDictFilter.Remove(jid);
  77.             }
  78.         }
  79.  
  80.         public void Clear()
  81.         {        
  82.             lock (LockObjDictionaries)
  83.             {
  84.                 DictFilter.Clear();
  85.                 JidDictFilter.Clear();
  86.             }
  87.         }
  88.     }
  89. }

Alex
Avatar
rfog #3
Member since Aug 2012 · 6 posts
Group memberships: Members
Show profile · Link to this post
Thank you for your answer! :-)
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters: