private IQ ProcessBind(XmppStream stream, IQ iq, XmppHandlerContext context) { if (iq.Type != IqType.set) { return(XmppStanzaError.ToBadRequest(iq)); } var answer = new IQ(IqType.result); answer.Id = iq.Id; var bind = (Bind)iq.Bind; var resource = !string.IsNullOrEmpty(bind.Resource) ? bind.Resource : stream.User; if (bind.TagName.Equals("bind", StringComparison.OrdinalIgnoreCase)) { var jid = new Jid(stream.User, stream.Domain, resource); var session = context.SessionManager.GetSession(jid); if (session != null) { if (session.Stream.Id != stream.Id) { context.Sender.SendToAndClose(session.Stream, XmppStreamError.Conflict); answer.AddTag("newTalkWindow", "true"); } else { return(XmppStanzaError.ToConflict(iq)); } } stream.BindResource(resource); context.SessionManager.AddSession(new XmppSession(jid, stream)); answer.Bind = new Bind(jid); } else if (bind.TagName.Equals("unbind", StringComparison.OrdinalIgnoreCase)) { if (!stream.Resources.Contains(resource)) { return(XmppStanzaError.ToNotFound(iq)); } context.SessionManager.CloseSession(iq.From); stream.UnbindResource(resource); if (stream.Resources.Count == 0) { context.Sender.CloseStream(stream); } } else { return(XmppStanzaError.ToBadRequest(iq)); } if (stream.MultipleResources) { answer.To = iq.From; } return(answer); }
private Element ValidateClientStanza(Stanza stanza, XmppStream stream) { if (!stream.Authenticated) { if (!(stanza is AuthIq) && (stanza is IQ && !(((IQ)stanza).Query is Register))) { return(XmppStanzaError.ToNotAuthorized(stanza)); } } //remove empty jids if (stanza.HasFrom && string.IsNullOrEmpty(stanza.From.ToString())) { stanza.From = null; } if (stanza.HasTo && string.IsNullOrEmpty(stanza.To.ToString())) { stanza.To = null; } //prep strings stanza.From = NodePrep(stanza.From); stanza.To = NodePrep(stanza.To); if (!ValidateJid(stanza.From) || !ValidateJid(stanza.To)) { return(XmppStanzaError.ToBadRequest(stanza)); } if (stanza.HasFrom) { if (!stream.JidBinded(stanza.From)) { // return null if we have from in bind iq (for qutIM 0.3 client) if (!(stanza is IQ) || ((IQ)stanza).Bind == null || ((IQ)stanza).Bind.Resource != stanza.From.Resource) { return(XmppStreamError.InvalidFrom); } } } else { if (stream.MultipleResources) { return(XmppStanzaError.ToConflict(stanza)); } stanza.From = new Jid(string.Format("{0}@{1}/{2}", stream.User, stream.Domain, 0 < stream.Resources.Count ? stream.Resources[0] : null)); } if (stanza is Message) { var message = (Message)stanza; if (message.Type == MessageType.chat && message.To == null) { return(XmppStanzaError.ToRecipientUnavailable(stanza)); } } return(null); }
public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context) { if (!iq.HasTo) { iq.To = iq.From; } if (iq.Type == IqType.get) { return(GetVCard(stream, iq, context)); } else if (iq.Type == IqType.set) { return(SetVCard(stream, iq, context)); } else { return(XmppStanzaError.ToBadRequest(iq)); } }
public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context) { if (iq.To != null && iq.From != iq.To) { return(XmppStanzaError.ToForbidden(iq)); } if (iq.Type == IqType.get) { return(GetPrivate(stream, iq, context)); } else if (iq.Type == IqType.set) { return(SetPrivate(stream, iq, context)); } else { return(XmppStanzaError.ToBadRequest(iq)); } }
private IQ GetPrivateLog(XmppStream stream, IQ iq, XmppHandlerContext context) { if (!iq.HasTo) { return(XmppStanzaError.ToBadRequest(iq)); } var privateLog = (PrivateLog)iq.Query; privateLog.RemoveAllChildNodes(); var logging = archiveStore.GetMessageLogging(iq.From, iq.To); privateLog.AddChild(new PrivateLogItem() { Jid = iq.To, Log = logging }); iq.SwitchDirection(); iq.Type = IqType.result; return(iq); }
public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context) { //Admins iq //New member MucRoomMember member = Room.GetRealMember(iq.From); if (member != null) { if (iq.Query != null) { if (iq.Query is Admin && (member.Affiliation == Affiliation.admin || member.Affiliation == Affiliation.owner)) { Room.AdminCommand(iq, member); } else if (iq.Query is Owner && (member.Affiliation == Affiliation.owner)) { Room.OwnerCommand(iq, member); } else { XmppStanzaError.ToForbidden(iq); } } else { XmppStanzaError.ToBadRequest(iq); } } else { XmppStanzaError.ToForbidden(iq); } if (!iq.Switched) { iq.SwitchDirection(); } iq.From = Room.Jid; return(iq); }
public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context) { var answer = new IQ(IqType.result) { Id = iq.Id, To = iq.From, From = iq.To, }; //iq sended to server if (iq.Type == IqType.get && (!iq.HasTo || iq.To.IsServer || iq.To == iq.From)) { if (iq.Query is Version) { answer.Query = new Version() { Name = "TeamLab Jabber Server", Os = System.Environment.OSVersion.ToString(), Ver = "1.0", }; return(answer); } else if (iq.Query is Ping) { return(answer); } return(XmppStanzaError.ToServiceUnavailable(iq)); } if (iq.Type == IqType.get && iq.HasTo) { //resend iq var sessionTo = context.SessionManager.GetSession(iq.To); var sessionFrom = context.SessionManager.GetSession(iq.From); if (sessionTo != null && sessionFrom != null) { if (string.IsNullOrEmpty(iq.Id)) { iq.Id = System.Guid.NewGuid().ToString("N"); } IdleWatcher.StartWatch( iq.Id + iq.From, System.TimeSpan.FromSeconds(3), (s, e) => { context.Sender.SendTo(sessionFrom, XmppStanzaError.ToServiceUnavailable(iq)); }); context.Sender.SendTo(sessionTo, iq); } else { return(XmppStanzaError.ToRecipientUnavailable(iq)); } } if (iq.Type == IqType.error || iq.Type == IqType.result) { if (!iq.HasTo) { return(XmppStanzaError.ToBadRequest(iq)); } IdleWatcher.StopWatch(iq.Id + iq.To); var session = context.SessionManager.GetSession(iq.To); if (session != null) { context.Sender.SendTo(session, iq); } } return(null); }
public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context) { //Admins iq //New member MucRoomMember member = Room.GetRealMember(iq.From); if (member != null) { if (iq.Query != null) { if (iq.Query is Admin && (member.Affiliation == Affiliation.admin || member.Affiliation == Affiliation.owner)) { Room.AdminCommand(iq, member); } else if (iq.Query is Owner && (member.Affiliation == Affiliation.owner)) { Room.OwnerCommand(iq, member); } else if (iq.Query is Core.protocol.x.tm.history.History && iq.Type == IqType.get) { Jid jid = iq.To; var mucStore = new DbMucStore(); var properties = new Dictionary <string, string>(1) { { "connectionStringName", "core" } }; mucStore.Configure(properties); var history = (Core.protocol.x.tm.history.History)iq.Query; foreach (var msg in mucStore.GetMucMessages(jid, history.Count, history.StartIndex)) { if (msg == null) { continue; } history.AddChild(HistoryItem.FromMessage(msg)); } iq.Type = IqType.result; iq.SwitchDirection(); return(iq); } else { XmppStanzaError.ToForbidden(iq); } } else { XmppStanzaError.ToBadRequest(iq); } } else { XmppStanzaError.ToForbidden(iq); } if (!iq.Switched) { iq.SwitchDirection(); } iq.From = Room.Jid; return(iq); }