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(); }
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); }
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; } }
/////////////////////////////////////////////////////////////////////////////// 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++); }
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); } }
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(); }
/// <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; }
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; } }
public void SignalMessageReceived(XmppMessage msg) { AppendMessage(msg, ToName); this.FlashWindow(5); lbBar.Text = string.Empty; }
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); }
private void OnMultiUserChatMessageReceived(XmppMessage message) { createChatRoomEvent.Set(); seekEnterChatRoomEvent.Set(); }
private void OnErrorMessageReceived(XmppMessage message) { messageReceivedSubject.OnNext(message); }