예제 #1
0
 public void HandleIQStanza(XElement iqStanza)
 {
     if (iqStanza.Attribute("type").Value == "result" || iqStanza.Attribute("type").Value == "error") { //it's a response
         var responseId = iqStanza.Attribute("id").Value;
         if(!_iqRequests.ContainsKey(responseId)) {
             throw new InvalidOperationException("IQ response has no corresponding requests");
         }
         var iqRequest = _iqRequests[responseId];
         JId from = iqStanza.Attribute("from") != null ? JId.Parse(iqStanza.Attribute("from").Value) : null;
         JId to = iqStanza.Attribute("to") != null ? JId.Parse(iqStanza.Attribute("to").Value) : null;
         IQResponse iqResponse;
         if(iqStanza.Attribute("type").Value == "result") {
             var content = iqStanza.Elements().First().Elements().ToArray();
             iqResponse = new IQResponse(from, to, iqStanza.Descendants().First().Name.LocalName, content);
         }
         else {
             var error = Error.CreateFromXml(iqStanza.Element("error"));
             iqResponse = new IQResponse(from, to, error);
         }
         iqRequest.ResponseCallback(iqResponse);
     }
     else HandleIQStanzaPush(iqStanza);
 }
예제 #2
0
 private void OnRosterInitialResult(IQResponse response)
 {
     if(response.ResponseType == IQResponseType.Result) {
         foreach (var itemElement in response.Content) {
             var jid = JId.Parse(itemElement.Attribute("jid").Value);
             string name = itemElement.Attribute("name") != null ? itemElement.Attribute("name").Value : null;
             var subscriptionType = itemElement.Attribute("subscription") != null ? (RosterItemSubscriptionType)Enum.Parse(typeof(RosterItemSubscriptionType), itemElement.Attribute("subscription").Value, true) : RosterItemSubscriptionType.None;
             var rosterItem = new RosterItem(jid, name, subscriptionType);
             foreach (var groupElement in itemElement.Descendants(XName.Get("group", Namespace))) {
                 rosterItem.Groups.Add(groupElement.Value);
             }
             AddOrUpdate(rosterItem);
         }
         SendPresence(null, null);
     } //TODO: handle error
 }