示例#1
0
        private void AppendMessage(XmppMessage msg, string displayName)
        {
            int index = displayName.IndexOf('@');

            if (index > 0)
            {
                displayName = displayName.Remove(index);
            }

            IsEmptyChat = false;
            Paragraph para = chatContent.Blocks.LastBlock as Paragraph;

            if (para == null || para.Tag as string != msg.From.Bare)
            {
                para         = new Paragraph();
                para.Padding = new Thickness(1);
                para.Margin  = new Thickness(1, 1, 1, 3);
                para.Tag     = msg.From.Bare;
                para.Inlines.Add(new Run("[" + DateTime.Now.ToShortTimeString() + "] <"));
                para.Inlines.Add(new Bold(new Run(displayName)));
                para.Inlines.Add(new Run(">"));

                chatContent.Blocks.Add(para);
            }

            para.Inlines.Add(new Run(Environment.NewLine + msg.Body));
            lstMessages.ScrollToEnd();
        }
示例#2
0
 void client_Message(object sender, XmppMessageEventArgs e)
 {
     Dispatcher.BeginInvoke(new Action(() => {
         XmppMessage msg = e.Message;
         Chat chat       = SingletonChat(msg.From, msg.Thread);
         if (chat != null)
         {
             chat.WindowState = (chat.IsEmptyChat && !chat.IsVisible ? WindowState.Minimized : WindowState.Normal);
             chat.Show();
             chat.SignalMessageReceived(msg);
         }
     }), DispatcherPriority.Normal);
 }
示例#3
0
        private void OnActivityMessage(XmppMessage message)
        {
            switch (message.Type)
            {
            case MessageType.Normal:
                activities.Add(new XmppMessageEvent(message));
                break;

            case MessageType.Headline:
                activities.Add(new XmppMessageEvent(message));
                break;
            }
        }
示例#4
0
        ///////////////////////////////////////////////////////////////////////////////
        public int SendMessage(XmppMessage msg)
        {
            if (!string.IsNullOrEmpty(msg.Subject))
            {
                msg.Subject = "<subject>" + msg.Subject.HtmlEncode() + "</subject>";
            }
            //if (!string.IsNullOrEmpty(msg.Thread))
            //msg.Thread = "<thread>" + msg.Thread + "</thread>";
            msg.Thread = "";
            if (!string.IsNullOrEmpty(msg.Type))
            {
                msg.Type = " type=\"" + msg.Type + "\"";
            }

            string buf = string.Format("<message to=\"{0}\"{1} id=\"{2}\" from=\"{3}\">{4}<body>{5}</body>{6}<active xmlns=\"http://jabber.org/protocol/chatstates\"/></message>",
                                       msg.To.Bare, msg.Type, iqCount, Jid.Value, msg.Thread, msg.Body.HtmlEncode(), msg.Subject);

            sendStream(buf);
            return(iqCount++);
        }
示例#5
0
        private void OnChatMessageReceived(XmppMessage message)
        {
            XmppChat chat = null;

            if (String.IsNullOrEmpty(message.Body) &&
                !chats.ContainsKey(message.From.BareIdentifier))
            {
            }
            else
            {
                if (!chats.ContainsKey(message.From.BareIdentifier))
                {
                    chat = CreateChat(message.From);
                }
                else
                {
                    chat = chats[message.From.BareIdentifier];
                }

                messageReceivedSubject.OnNext(message);
            }
        }
示例#6
0
        private void SendMessage()
        {
            string message = txtMessage.Text;

            if (string.IsNullOrWhiteSpace(message))
            {
                //@@@
                return;
            }

            XmppMessage msg = new XmppMessage();

            msg.To     = new XmppJid(riTo.Jid.Bare);
            msg.From   = new XmppJid(client.UserName);
            msg.Body   = message;
            msg.Thread = Thread;
            msg.Type   = "chat";           //@@@ enum

            client.SendMessage(msg);
            AppendMessage(msg, FromName);
            lbBar.Text      = string.Empty;
            txtMessage.Text = string.Empty;
            txtMessage.Focus();
        }
示例#7
0
 /// <summary>
 ///   Initializes a new instance of the <see cref = "">XmppMessageEvent</see> class.
 /// </summary>
 /// <param name = "message">The message information</param>
 public XmppMessageEvent(XmppMessage message)
 {
     this.message = message;
 }
示例#8
0
        private void OnMessage(XmppClient Client, XmppMessage Message)
        {
            if (Message.MessageType != MessageType.Chat)
            {
                return;
            }

            string s = Message.Body.Trim();

            if (string.IsNullOrEmpty(s))
            {
                return;
            }

            DateTime             Now      = DateTime.Now;
            DateTime             Timeout  = Now.AddMinutes(-15);
            LinkedList <Session> ToRemove = null;
            Session Session;
            string  s2;
            int     i;

            lock (this.sessionByJid)
            {
                foreach (KeyValuePair <DateTime, Session> P in this.sessionByLastAccess)
                {
                    if (P.Key <= Timeout)
                    {
                        if (ToRemove == null)
                        {
                            ToRemove = new LinkedList <Session> ();
                        }

                        ToRemove.AddLast(P.Value);
                    }
                    else
                    {
                        break;
                    }
                }

                if (ToRemove != null)
                {
                    foreach (Session S in ToRemove)
                    {
                        this.sessionByJid.Remove(S.From);
                        this.sessionByLastAccess.Remove(S.LastAccess);
                    }
                }

                if (this.sessionByJid.TryGetValue(Message.From, out Session))
                {
                    this.sessionByLastAccess.Remove(Session.LastAccess);
                }
                else
                {
                    Session      = new Session();
                    Session.From = Message.From;
                    this.sessionByJid [Session.From] = Session;
                }

                while (this.sessionByLastAccess.ContainsKey(Now))
                {
                    Now = Now.AddTicks(gen.Next(1, 10));
                }

                Session.LastAccess             = Now;
                this.sessionByLastAccess [Now] = Session;
            }

            switch (s)
            {
            case "#":
                this.SendMenu(Client, Message.From, true, string.Empty, Session);
                break;

            case "##":
                this.SendMenu(Client, Message.From, false, string.Empty, Session);
                break;

            case "?":
                if (this.provisioning == null)
                {
                    this.SendErrorMessage(Message.From, "No provisioning server has been found, so readout through the chat interface is not allowed.", Session);
                }
                else if (this.sensor == null)
                {
                    this.SendErrorMessage(Message.From, "No sensor interface provided.", Session);
                }
                else
                {
                    ReadoutRequest Request = new ReadoutRequest(ReadoutType.MomentaryValues, DateTime.MinValue, DateTime.MaxValue);
                    this.provisioning.CanRead(Request, Message.From, this.CanReadResponse, new object[] { Message.From, Session });
                }
                break;

            case "??":
                if (this.provisioning == null)
                {
                    this.SendErrorMessage(Message.From, "No provisioning server has been found, so readout through the chat interface is not allowed.", Session);
                }
                else if (this.sensor == null)
                {
                    this.SendErrorMessage(Message.From, "No sensor interface provided.", Session);
                }
                else
                {
                    ReadoutRequest Request = new ReadoutRequest(ReadoutType.All, DateTime.MinValue, DateTime.MaxValue);
                    this.provisioning.CanRead(Request, Message.From, this.CanReadResponse, new object[] { Message.From, Session });
                }
                break;

            case "html+":
                Session.Html = true;
                Client.SendMessage(Message.From, "HTML mode turned on.", MessageType.Chat);
                break;

            case "html-":
                Session.Html = false;
                Client.SendMessage(Message.From, "HTML mode turned off.", MessageType.Chat);
                break;

            case "=?":
            case "!?":
                if (this.provisioning == null)
                {
                    this.SendErrorMessage(Message.From, "No provisioning server has been found, so control  through the chat interface is not allowed.", Session);
                }
                else if (this.control == null)
                {
                    this.SendErrorMessage(Message.From, "No control interface provided.", Session);
                }
                else
                {
                    this.provisioning.CanControl(Message.From, this.CanControlListParametersResponse, new object[] {
                        Message.From,
                        Session
                    }, null, null, null, this.control.Parameters, null);
                }
                break;

            default:
                if (s.EndsWith("??"))
                {
                    if (this.provisioning == null)
                    {
                        this.SendErrorMessage(Message.From, "No provisioning server has been found, so readout through the chat interface is not allowed.", Session);
                    }
                    else if (this.sensor == null)
                    {
                        this.SendErrorMessage(Message.From, "No sensor interface provided.", Session);
                    }
                    else
                    {
                        ReadoutRequest Request = new ReadoutRequest(ReadoutType.All, DateTime.MinValue, DateTime.MaxValue, null, new string[] { s.Substring(0, s.Length - 2) });
                        this.provisioning.CanRead(Request, Message.From, this.CanReadResponse, new object[] { Message.From, Session });
                    }
                }
                else if (s.EndsWith("?"))
                {
                    if (this.provisioning == null)
                    {
                        this.SendErrorMessage(Message.From, "No provisioning server has been found, so readout through the chat interface is not allowed.", Session);
                    }
                    else if (this.sensor == null)
                    {
                        this.SendErrorMessage(Message.From, "No sensor interface provided.", Session);
                    }
                    else
                    {
                        ReadoutRequest Request = new ReadoutRequest(ReadoutType.MomentaryValues, DateTime.MinValue, DateTime.MaxValue, null, new string[] { s.Substring(0, s.Length - 1) });
                        this.provisioning.CanRead(Request, Message.From, this.CanReadResponse, new object[] { Message.From, Session });
                    }
                }
                else if ((i = s.IndexOfAny(controlDelimiters)) > 0)
                {
                    IControlParameter Parameter;

                    if (this.provisioning == null)
                    {
                        this.SendErrorMessage(Message.From, "No provisioning server has been found, so control operations through the chat interface is not allowed.", Session);
                    }
                    else if (this.control == null)
                    {
                        this.SendErrorMessage(Message.From, "No control interface provided.", Session);
                    }
                    else if ((Parameter = this.control [s2 = s.Substring(0, i).Trim()]) == null)
                    {
                        this.SendErrorMessage(Message.From, "No control parameter named '" + s2 + "' found.", Session);
                    }
                    else
                    {
                        this.provisioning.CanControl(Message.From, this.CanControlParameterResponse, new object[] {
                            Message.From,
                            Session,
                            Parameter,
                            s2,
                            s.Substring(i + 1).Trim()
                        }, null, null, null, new string[] { s2 }, null);
                    }
                }
                else
                {
                    this.SendMenu(Client, Message.From, true, "Hello. Following is a list of commands you can use when chatting with me.\r\n\r\n", Session);
                }
                break;
            }
        }
示例#9
0
 public void SignalMessageReceived(XmppMessage msg)
 {
     AppendMessage(msg, ToName);
     this.FlashWindow(5);
     lbBar.Text = string.Empty;
 }
示例#10
0
        private void doParseStream(string stream)
        {
            XmppIQ xStream;

            try {
                xStream = XmppIQ.Parse(stream);
            }       catch (Exception) {
                //MessageBox.Show("Invalid stream :(\n\n" + stream);
                return;
            }

            switch (xStream.Name)
            {
                #region PRESENCE
            case "presence": {
                XmppPresence presence = new XmppPresence();
                string       type     = xStream.GetAttribute("type");
                presence.From = new XmppJid(xStream.GetAttribute("from"));
                presence.To   = new XmppJid(xStream.GetAttribute("to"));

                // # type == ""
                if (string.IsNullOrEmpty(type))
                {
                    XmppIQ xStatus = xStream.FindDescendant("status");
                    if (xStatus != null)
                    {
                        presence.MessageStatus = xStatus.Text;
                    }

                    XmppIQ xShow = xStream.FindDescendant("show");
                    presence.Status = xShow != null?XmppIq.StrToPresence(xShow.Text) : XmppPresenceStatus.Online;

                    XmppIQ xCard = xStream.FindDescendant("x", "vcard-temp:x:update");
                    if (xCard != null)
                    {
                        XmppIQ xPhoto = xCard.FindDescendant("photo", "vcard-temp:x:update");
                        if (xPhoto != null)
                        {
                            presence.PhotoHash = xPhoto.Text;
                        }
                    }

                    if (Presence != null)
                    {
                        Presence(this, new XmppPresenceEventArgs(presence));
                    }
                }
                // # type == "unavailable"
                else if (type == "unavailable")
                {
                    presence.Status = XmppPresenceStatus.Unavailable;
                    if (Presence != null)
                    {
                        Presence(this, new XmppPresenceEventArgs(presence));
                    }
                }
                // # type == "probe"	// probe request from server
                else if (type == "probe")
                {
                    SetPresence(statePresence, string.Empty, null);
                }
                // # type == "subscribe"	// new subscription request
                else if (type == "subscribe")
                {
                    if (SubscribeRequest != null)
                    {
                        SubscribeRequest(this, new XmppSubscribeRequestEventArgs(presence.From));
                    }
                }
                // # type == "error"	// presence stanza error
                else if (type == "error")
                {
                    // @@@
                }
            }
            break;

                #endregion
                #region MESSAGE
            case "message": {
                XmppMessage message = new XmppMessage();
                message.Type = xStream.GetAttribute("type");
                message.From = new XmppJid(xStream.GetAttribute("from"));
                message.To   = new XmppJid(xStream.GetAttribute("to"));
                Int32.TryParse(xStream.GetAttribute("id"), out message.ID);

                if (message.Type != "error")
                {
                    // # user composing new message
                    if (xStream.FindDescendant("composing", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.NowComposing));
                        }
                    }
                    // # user stop composing
                    else if (xStream.FindDescendant("paused", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.StopComposing));
                        }
                    }
                    // # user is inactive
                    else if (xStream.FindDescendant("inactive", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.Inactive));
                        }
                    }
                    // # user has left conversation
                    else if (xStream.FindDescendant("gone", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.Gone));
                        }
                    }
                    // # user is active ;)
                    else if (xStream.FindDescendant("active", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.Active));
                        }
                    }

                    // # check for new message
                    XmppIQ xBody = xStream.FindDescendant("body");
                    if (xBody != null)
                    {
                        message.Body = xBody.Text;

                        XmppIQ xThread = xStream.FindDescendant("thread");
                        if (xThread != null)
                        {
                            message.Thread = xThread.Text;
                        }

                        XmppIQ xSubject = xStream.FindDescendant("subject");
                        if (xSubject != null)
                        {
                            message.Subject = xSubject.Text;
                        }

                        if (Message != null)
                        {
                            Message(this, new XmppMessageEventArgs(message));
                        }
                    }
                }
            }
            break;

                #endregion
                #region IQ
            case "iq": {
                XmppIQ xQuery = null;
                string type   = xStream.GetAttribute("type");

                // Roster
                xQuery = xStream.FindDescendant("query", "jabber:iq:roster");
                if (xQuery != null)
                {
                    List <XmppRosterItem> roster = new List <XmppRosterItem>();
                    foreach (XmppIQ xItem in xQuery.Children())
                    {
                        if (xItem.Name != "item")
                        {
                            continue;
                        }

                        string jid   = xItem.GetAttribute("jid");
                        string sub   = xItem.GetAttribute("subscription");
                        string ask   = xItem.GetAttribute("ask");
                        string name  = xItem.GetAttribute("name");
                        string group = xItem.GetAttribute("group");
                        roster.Add(new XmppRosterItem(jid, sub, name, group, ask));
                    }

                    if (Roster != null)
                    {
                        Roster(this, new XmppRosterEventArgs(roster, XmppIq.StrToType(type)));
                    }
                }

                // Disco
                xQuery = xStream.FindDescendant("query", "http://jabber.org/protocol/disco#info");
                if (xQuery != null)
                {
                    if (type == "get")
                    {
                        XmppJid from = new XmppJid(xStream.GetAttribute("from"));
                        string  id   = xStream.GetAttribute("id");
                        sendStream(new XmppIq().ToStream(Jid, from, XmppIqType.Result, id, Disco.ToStreamResult()));                                // @@@@@ ottimizza SendIq(...)
                        //SendIq(from, XmppIqType.Result, Disco.ToStreamResult());// @@@@@ ottimizza SendIq(...)
                    }
                }

                // vCard
                xQuery = xStream.FindDescendant("vCard", "vcard-temp");
                if (xQuery != null)
                {
                    if (type == "result")
                    {
                        XmppJid   from  = new XmppJid(xStream.GetAttribute("from"));
                        XmppVCard vCard = new XmppVCard(from, xQuery);
                        if (VCard != null)
                        {
                            VCard(this, new XmppVCartEventArgs(vCard));
                        }
                    }
                }

                // Ping
                xQuery = xStream.FindDescendant("ping", "urn:xmpp:ping");
                if (xQuery != null)
                {
                    if (type == "get")
                    {
                        XmppJid from = new XmppJid(xStream.GetAttribute("from"));
                        string  id   = xStream.GetAttribute("id");
                        sendStream(new XmppIq().ToStream(Jid, from, XmppIqType.Result, id, string.Empty));                                // @@@@@ ottimizza SendIq(...)
                    }
                }
            }

            break;
                #endregion
            }

            Signal(xStream);
        }
示例#11
0
 private void OnMultiUserChatMessageReceived(XmppMessage message)
 {
     createChatRoomEvent.Set();
     seekEnterChatRoomEvent.Set();
 }
示例#12
0
 /// <summary>
 ///   Initializes a new instance of the <see cref = "">XmppMessageEvent</see> class.
 /// </summary>
 /// <param name = "message">The message information</param>
 public XmppMessageEvent(XmppMessage message)
 {
     this.message = message;
 }
示例#13
0
 private void OnErrorMessageReceived(XmppMessage message)
 {
     messageReceivedSubject.OnNext(message);
 }
示例#14
0
        private void OnChatMessageReceived(XmppMessage message)
        {
            XmppChat chat = null;

            if (String.IsNullOrEmpty(message.Body) &&
                !chats.ContainsKey(message.From.BareIdentifier))
            {
            }
            else
            {
                if (!chats.ContainsKey(message.From.BareIdentifier))
                {
                    chat = CreateChat(message.From);
                }
                else
                {
                    chat = chats[message.From.BareIdentifier];
                }

                messageReceivedSubject.OnNext(message);
            }
        }
示例#15
0
 private void OnErrorMessageReceived(XmppMessage message)
 {
     messageReceivedSubject.OnNext(message);
 }
示例#16
0
 private void OnMultiUserChatMessageReceived(XmppMessage message)
 {
     createChatRoomEvent.Set();
     seekEnterChatRoomEvent.Set();
 }