public void SendChatMessage(TextMessage txtmsg)
        {
            txtmsg.Sent = true;
            ChatMessage msg = new ChatMessage(null);

            msg.From = txtmsg.From;
            msg.To   = txtmsg.To;
            msg.Type = "chat";
            msg.Body = txtmsg.Message;
            //suresh added for message receipt
            msg.InnerXML = "<request xmlns='urn:xmpp:receipts'/>";

            if (txtmsg.Thread == null || txtmsg.Thread.Length < 1)
            {
                //txtmsg = ExtractThread(txtmsg);
                ExtractThread(txtmsg);
            }

            msg.Thread = txtmsg.Thread;

            //msg.InnerXML = string.Format(@"<body>{0}</body>", txtmsg.Message);

            /// Find the roster guy for this message and add it to their conversation
            ///
            RosterItem item = XMPPClient.FindRosterItem(txtmsg.To);

            if (item != null)
            {
                item.AddSendTextMessage(txtmsg);
                //commented due to newconversation for conatct or delay msg send for not in the our roster item
                // Notify XMPPClient that a new conversation item has been added
                // XMPPClient.FireNewConversationItem(item, false, txtmsg,msg.ID);
            }
            XMPPClient.FireNewConversationItem(item, false, txtmsg, msg.ID);
            XMPPClient.SendXMPP(msg);
        }
Example #2
0
        public override bool NewIQ(IQ iq)
        {
            if (!(iq is RosterIQ))
                return false;

            RosterIQ rostiq = iq as RosterIQ;

            if (iq.ID == RosterIQ.ID)
            {
                //<iq type="result" id="aab8a" to="[email protected]/hypnotoad">
                   //<query xmlns="jabber:iq:roster">
                       ///<item jid="*****@*****.**" name="BrianBonnett" subscription="both">
                       ///    <group>Friends</group>
                       ///</item>
                   ///</query>
                ///</iq>
                ///
                

                this.Success = false;
                if (iq.Type == IQType.result.ToString())
                {
                    if ( (rostiq.Query != null) && (rostiq.Query.RosterItems != null) )
                    {
                        foreach (rosteritem item in rostiq.Query.RosterItems)
                        {
                            RosterItem ros = new RosterItem(XMPPClient, item.JID);

                            if (item.Ask == "subscribe")
                            {
                                /// Need to do subscribe to this user's presence some how
                                /// 
                            }

                            /// See if we already have this roster item
                            /// 
                            RosterItem existingitem = XMPPClient.FindRosterItem(ros.JID);
                            if (existingitem != null)
                            {
                                existingitem.Name = (item.Name != null) ? item.Name : ros.JID.BareJID;
                                existingitem.Subscription = (item.Subscription != null) ? item.Subscription : "";
                                existingitem.Node = item;
                                existingitem.Groups.Clear();
                                /// Get the group for this item
                                if (item.Groups != null)
                                {
                                    foreach (string strGroup in item.Groups)
                                    {
                                        ros.Group = strGroup;
                                        ros.Groups.Add(strGroup);
                                    }
                                }
                            }
                            else
                            {
                                ros.Name = (item.Name != null) ? item.Name : ros.JID.BareJID;
                                ros.Subscription = (item.Subscription != null) ? item.Subscription : "";
                                ros.Node = item;
                                XMPPClient.RosterItems.Add(ros);
                                /// Get the group for this item
                                if (item.Groups != null)
                                {
                                    foreach (string strGroup in item.Groups)
                                    {
                                        ros.Group = strGroup;
                                        if (ros.Groups.Contains(strGroup) == false)
                                            ros.Groups.Add(strGroup);
                                    }
                                }
                            }
                        }

                    }

                    this.Success = true;
                    XMPPClient.FireGotRoster();
                }

                this.IsCompleted = false;
                
                return true;
            }
            else if (iq.Type == "set")
            {
                //<iq type="set" id="640-356" to="[email protected]/phone"><query xmlns="jabber:iq:roster"><item jid="*****@*****.**" ask="subscribe" subscription="from"/></query></iq>

                if ( (rostiq.Query != null) && (rostiq.Query.RosterItems != null) )
                {
                    foreach (rosteritem item in rostiq.Query.RosterItems)
                    {
                        RosterItem ros = new RosterItem(XMPPClient, item.JID)
                        {
                            XMPPClient = XMPPClient,
                            Name = item.Name,
                            Subscription = item.Subscription,
                            Node = item,
                        };

                        if (XMPPClient.FindRosterItem(ros.JID) == null)
                        {

                            XMPPClient.RosterItems.Add(ros);

                            if (item.Groups != null)
                            {
                                foreach (string strGroup in item.Groups)
                                {
                                    ros.Group = strGroup;
                                    if (ros.Groups.Contains(strGroup) == false)
                                        ros.Groups.Add(strGroup);
                                }
                            }

                            XMPPClient.AsyncFireListChanged();
                        }

                        if (item.Subscription == subscription.from.ToString())  /// should only have a from subscription if we've added the roster item
                        {
                            //if (XMPPClient.AutoAcceptPresenceSubscribe
                            /// subscribe to presence of this one
                            XMPPClient.PresenceLogic.SubscribeToPresence(ros.JID.BareJID);
                        }

                    }
 
                    iq.Type = IQType.result.ToString();
                    iq.To = iq.From;
                    iq.From = XMPPClient.JID;
                    iq.InnerXML = null;
                    XMPPClient.SendXMPP(iq);

                    this.Success = true;
                    return true;
                }

            }


            return false;
        }
        public override bool NewMessage(Message iq)
        {
            /// See if this is a standard text message
            ///
            if (iq is ChatMessage)
            {
                ChatMessage chatmsg = iq as ChatMessage;
                RosterItem  item    = XMPPClient.FindRosterItem(chatmsg.From);
                if (item != null)
                {
                    if (chatmsg.Body != null)
                    {
                        TextMessage txtmsg = new TextMessage();
                        txtmsg.From     = chatmsg.From;
                        txtmsg.To       = chatmsg.To;
                        txtmsg.Received = DateTime.Now;
                        if (chatmsg.Delivered.HasValue == true)
                        {
                            txtmsg.Received = chatmsg.Delivered.Value; /// May have been a server stored message
                        }
                        txtmsg.Message = chatmsg.Body;
                        txtmsg         = ExtractThread(txtmsg, true);
                        txtmsg.Sent    = false;
                        item.AddRecvTextMessage(txtmsg);
                        item.HasNewMessages = true;

                        // Notify XMPPClient that a new conversation item has been added
                        XMPPClient.FireNewConversationItem(item, true, txtmsg, iq.ID);
                    }
                    if (chatmsg.ConversationState != ConversationState.none)// A conversation message
                    {
                        item.Conversation.ConversationState = chatmsg.ConversationState;
                        XMPPClient.FireNewConversationState(item, item.Conversation.ConversationState);
                    }
                    else
                    {
                        XDocument xmldocument = XDocument.Parse(iq.InnerXML.ToString());
                        if (xmldocument.Root.LastAttribute != null)
                        {
                            string id = xmldocument.Root.LastAttribute.Value.ToString();
                            XMPPClient.FireDelegateReceiptHandler(chatmsg.From, id);
                        }
                    }
                }
                if (item == null)
                {
                    XDocument xmldocument = XDocument.Parse(iq.InnerXML.ToString());
                    if (xmldocument.Root.LastAttribute != null)
                    {
                        string id = xmldocument.Root.LastAttribute.Value.ToString();
                        XMPPClient.FireDelegateReceiptHandler(chatmsg.From, id);
                    }
                }

                return(true);
            }
            //suresh handle group chat
            else if (iq.Type == "groupchat")
            {
                XDocument xmldoc = XDocument.Parse(iq.InnerXML.ToString());
                string    body   = xmldoc.Descendants("body").Single().Value.ToString();
                XMPPClient.FireNewGroupConversation(iq, body);
            }

            else if (iq.Type == "normal")
            {
                XDocument xmldocument = XDocument.Parse(iq.MessageXML.ToString());
                XMPPClient.FireNewGroupConversation(iq, xmldocument.ToString());
            }
            //suresh handle receipt
            else if (iq.Type == "" || iq.Type == null)
            {
                XDocument xmldocument    = XDocument.Parse(iq.InnerXML.ToString());
                string    id             = xmldocument.Root.LastAttribute.Value.ToString();
                JID       messageFromJID = iq.From;

                XMPPClient.FireDelegateReceiptHandler(messageFromJID, id);
            }
            return(false);
        }
Example #4
0
        /// <presence id="AWAQt-31" from="[email protected]/calculon" to="[email protected]/phone"><status>Away</status><priority>0</priority><show>away</show></presence>

        public override bool NewPresence(PresenceMessage pres)
        {
            if ((pres.Type == "") || (pres.Type == "unavailable"))
            {
                /// Means avialbe
                ///
                RosterItem item = XMPPClient.FindRosterItem(pres.From);
                if (item != null)
                {
                    item.SetPresence(pres);
                }
            }

            else if (pres.Type == "subscribe")
            {
                Answer answer = XMPPClient.ShouldSubscribeUser(pres);

                if (answer == Answer.Yes)
                {
                    AcceptUserPresence(pres, "", "");
                }
                else if (answer == Answer.No) /// reject if the user has responded
                {
                    DeclineUserPresence(pres);
                }
            }
            else if (pres.Type == "subscribed")
            {
            }
            else
            {
                RosterItem item = XMPPClient.FindRosterItem(pres.From);
                if (item != null)
                {
                    item.SetPresence(pres);
                    //item.Presence = pres.PresenceStatus;

                    /// Commented out because no one seems to support this method
                    //if (pres.AvatarHash != null)
                    //{
                    //    if (pres.AvatarHash.Hash != null)
                    //    {
                    //        /// May have a new avatar, check against our current file system
                    //        ///
                    //        if (XMPPClient.AvatarStorage.AvatarExist(pres.AvatarHash.Hash) == false)
                    //           DownloadAvatarJabberIQMethod(pres.From);
                    //    }
                    //}

                    if (pres.VCardUpdate != null)
                    {
                        if (pres.VCardUpdate.PhotoHash != null)
                        {
                            //byte [] bURL = Convert.FromBase64String(pres.VCardUpdate.PhotoHash);
                            //string strURL = System.Text.UTF8Encoding.UTF8.GetString(bURL, 0, bURL.Length);
                            //item.AvatarImagePath = strURL;



                            /// XEP-153 - vcard-based avatars... sha1 hash of the current avatar
                            if ((XMPPClient.AvatarStorage.AvatarExist(pres.VCardUpdate.PhotoHash) == false) && (XMPPClient.AutomaticallyDownloadAvatars == true))
                            {
                                RequestVCARD(pres.From);
                            }
                            else
                            {
                                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ThreadedUpdateAvatarImageHash),
                                                                              new RosterItemImageHash()
                                {
                                    RosterItem = item, ImageHash = pres.VCardUpdate.PhotoHash
                                });
                                //item.AvatarImagePath = pres.VCardUpdate.PhotoHash;
                            }


                            //RequestVCARD(pres.From);


                            /// or XEP-0008 IQ based avatars
                            ///


                            /// or XEP- jaber fldlkjsl
                            //string strRelativeImage = string.Format("Avatars/{0}", pres.VCardUpdate.PhotoHash);
                            ///// See if this file exists, if it doesn't, download the file
                            /////
                            //if (System.IO.File.Exists(strRelativeImage) == true)
                            //    item.AvatarImagePath = strRelativeImage;
                            //else
                            //    //XMPPClient.PersonalEventingLogic.DownloadDataNode(jidfrom, "urn:xmpp:avatar:data", strItem);
                            //    XMPPClient.DownloadAvatar(pres.From, pres.VCardUpdate.PhotoHash);
                        }
                    }


                    System.Diagnostics.Debug.WriteLine(item.ToString());
                    XMPPClient.FireListChanged(1);
                }
            }

            return(true);
        }
        public override bool NewMessage(Message iq)
        {
            /// Look for pubsub events
            ///
            if (iq is PubSubEventMessage)
            {
                PubSubEventMessage psem = iq as PubSubEventMessage;
                if (psem.Event != null)
                {
                    if ((psem.Event.Items != null) && (psem.Event.Items.Items != null) && (psem.Event.Items.Items.Length > 0))
                    {
                        PubSubItem psitem = psem.Event.Items.Items[0];
                        XElement   elem   = psitem.InnerItemXML as XElement;

                        if (psem.Event.Items.Node == "http://jabber.org/protocol/tune")
                        {
                            TuneItem item = psitem.GetObjectFromXML <TuneItem>();
                            if (item != null)
                            {
                                /// find the roster item, set the tune item
                                RosterItem rosteritem = XMPPClient.FindRosterItem(iq.From);
                                if (rosteritem != null)
                                {
                                    rosteritem.Tune = item;
                                }
                            }
                        }
                        else if (psem.Event.Items.Node == "http://jabber.org/protocol/geoloc")
                        {
                            geoloc item = psitem.GetObjectFromXML <geoloc>();
                            if (item != null)
                            {
                                /// find the roster item, set the tune item
                                RosterItem rosteritem = XMPPClient.FindRosterItem(iq.From);
                                if (rosteritem != null)
                                {
                                    rosteritem.GeoLoc = item;
                                }
                            }
                        }
                        else if (psem.Event.Items.Node == "http://jabber.org/protocol/mood")
                        {
                        }
                        else if (psem.Event.Items.Node == "urn:xmpp:avatar:metadata") /// item avatar metadata
                        {
                            /// We have update avatar info for this chap, we should then proceed to get the avatar data
                            ///
                            foreach (PubSubItem objItem in psem.Event.Items.Items)
                            {
                                avatarmetadata meta = psitem.GetObjectFromXML <avatarmetadata>();
                                if (meta != null)
                                {
                                    /// Request this node ? maybe we get it automatically?
                                    ///
                                }
                                /// Not sure why they would have more than 1 avatar item, so we'll ignore fom now
                                ///
                                break;
                            }
                        }
                        else if (psem.Event.Items.Node == "urn:xmpp:avatar:data") /// item avatar
                        {
                            /// We have update avatar info for this chap, we should then proceed to get the avatar data
                            ///
                            /// Works, but let's comment out for now to focus on more supported avatar methods
                            //foreach (PubSubItem objItem in psem.Items)
                            //{
                            //    avatardata data = Utility.ParseObjectFromXMLString(objItem.InnerItemXML, typeof(avatardata)) as avatardata;
                            //    if (data != null)
                            //    {
                            //        string strHash = XMPPClient.AvatarStorage.WriteAvatar(data.ImageData);
                            //        RosterItem item = XMPPClient.FindRosterItem(psem.From);
                            //        if (item != null)
                            //        {
                            //            item.AvatarImagePath = strHash;
                            //        }
                            //    }
                            //    /// Not sure why they would have more than 1 avatar item, so we'll ignore fom now
                            //    ///
                            //    break;
                            //}
                        }
                    }
                }
            }

            return(base.NewMessage(iq));
        }