int GroupchatTask::handleStanza(Stanza& stanza, StanzaHandlerResult& result) { // Buddy presence // <presence/> int ok = 1; int bUsed = 0; String sType = stanza.getAttribute("type").getValue(); if (stanza.getName() == "message" && sType == "groupchat") { JabberId from = stanza.getAttribute("from").getValue(); apLog_Verbose((LOG_CHANNEL, LOG_CONTEXT, "<message from=%s ...", _sz(from))); Room* pRoom = pClient_->findRoom(from.base()); if (pRoom) { ok = pRoom->receiveGroupchat(stanza); bUsed = 1; } } if (bUsed) { result.stanzaHandled(1); result.stanzaConsumed(1); } return ok; }
int VersionTask::sendResponse(Stanza& stanza) { // <iq type='result' id='jcl_9' to='[email protected]/exodus' from='[email protected]/Winjab'> // <query xmlns='jabber:iq:version'> // <name>Winjab</name> // <version>1.1.0.1</version> // <os>NT 5.0</os> // </query> // </iq> int ok = 1; String sId = stanza.getAttribute("id").getValue(); String sFrom = stanza.getAttribute("from").getValue(); if (sFrom.empty()) { ok = 0; apLog_Warning((LOG_CHANNEL, LOG_CONTEXT, "missing from")); } else { ResultStanza result(sId, sFrom); Apollo::XMLNode& AP_UNUSED_VARIABLE query = result.addQuery(XMPP_NS_VERSION); String sName = Apollo::getModuleConfig(MODULE_NAME, "ClientInfo/Name", "Apollo"); String sVersion = Apollo::getModuleConfig(MODULE_NAME, "ClientInfo/Version", "0.1"); String sOs = Apollo::getModuleConfig(MODULE_NAME, "ClientInfo/OS", "Unknown"); Apollo::XMLNode& name = result.addChildRef("name"); if (name) { name.setCData(sName); } Apollo::XMLNode& version = result.addChildRef("version"); if (version) { version.setCData(sVersion); } Apollo::XMLNode& os = result.addChildRef("os"); if (os) { os.setCData(sOs); } ok = pClient_->sendStanza(result); if (!ok) { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "pClient_->sendStanza() failed to=%s", _sz(sFrom))); } } return ok; }
/** * Создать заготовку-ответ на комманду */ AdHocCommand AdHocCommand::reply(Stanza stanza) { Stanza reply = new XmlTag("iq"); reply->setAttribute("from", stanza.to().full()); reply->setAttribute("to", stanza.from().full()); reply->setAttribute("id", stanza->getAttribute("id")); reply->setAttribute("type", "result"); Stanza command = reply["command"]; command->setDefaultNameSpaceAttribute("http://jabber.org/protocol/commands"); command->setAttribute("node", stanza["command"]->getAttribute("node")); command["x"]->setDefaultNameSpaceAttribute("jabber:x:data"); return reply; }
int LoginTask::handleStanza(Stanza& stanza, StanzaHandlerResult& result) { int ok = 1; switch(nPhase_) { case StreamOpen: // <stream:stream xmlns:stream='http://etherx.jabber.org/streams' id='43956666' xmlns='jabber:client' from='user.virtual-presence.org'> if (stanza.getName() == "stream:stream") { result.stanzaHandled(1); result.stanzaConsumed(1); sStreamId_ = stanza.getAttribute("id").getValue(); if (sStreamId_.empty()) { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "<stream:stream> No stream ID")); nPhase_ = LoginFailed; result.connectionFinished(1); result.taskFinished(1); pClient_->onProtocolStartFailed(); } else { sendAuthMechQuery(); nPhase_ = AuthMechanisms; pClient_->onProtocolStart(); } } break; case AuthMechanisms: if (stanza.getName() == "iq") { // <iq id='1' type='result'><query xmlns='jabber:iq:auth'><username>lluna_484de02a54c5</username><digest/><password/><resource/></query></iq> if (sId_ == stanza.getAttribute("id").getValue()) { result.stanzaHandled(1); result.stanzaConsumed(1); String sType = stanza.getAttribute("type").getValue(); if (sType == "error") { String sError; int nError = 0; if (stanza.getError(nError, sError)) { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "jabber:iq:auth error result: %d %s", nError, _sz(sError))); } else { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "jabber:iq:auth unknown error result")); } nPhase_ = LoginFailed; result.connectionFinished(1); pClient_->onProtocolLoginFailed(); } else { Apollo::XMLNode& query = stanza.getChildRef("query"); Apollo::XMLNode& digest = query.getChildRef("digest"); Apollo::XMLNode& password = query.getChildRef("password"); AP_UNUSED_VARIABLE Apollo::XMLNode& resource = query.getChildRef("resource"); if (digest) { sendDigestAuth(); nPhase_ = DigestAuth; } else if (password) { sendPasswordAuth(); nPhase_ = PasswordAuth; } else { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "jabber:iq:auth result: neither digest nor password auth available")); nPhase_ = LoginFailed; result.connectionFinished(1); pClient_->onProtocolLoginFailed(); } } } } break; case DigestAuth: if (stanza.getName() == "iq") { // <iq id='2' type='result'/> if (sId_ == stanza.getAttribute("id").getValue()) { result.stanzaHandled(1); result.stanzaConsumed(1); result.taskFinished(1); String sType = stanza.getAttribute("type").getValue(); if (sType == "error") { String sError; int nError = 0; if (stanza.getError(nError, sError)) { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "DigestAuth error result: %d %s", nError, _sz(sError))); } else { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "DigestAuth unknown error result")); } nPhase_ = LoginFailed; result.connectionFinished(1); pClient_->onProtocolLoginFailed(); } else { apLog_Info((LOG_CHANNEL, LOG_CONTEXT, "Logged in")); nPhase_ = LoggedIn; pClient_->onProtocolLogin(); } } } break; case PasswordAuth: if (stanza.getName() == "iq") { // <iq id='2' type='result'/> if (sId_ == stanza.getAttribute("id").getValue()) { result.stanzaHandled(1); result.stanzaConsumed(1); result.taskFinished(1); String sType = stanza.getAttribute("type").getValue(); if (sType == "error") { String sError; int nError = 0; if (stanza.getError(nError, sError)) { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "PasswordAuth error result: %d %s", nError, _sz(sError))); } else { apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "PasswordAuth unknown error result")); } nPhase_ = LoginFailed; result.connectionFinished(1); pClient_->onProtocolLoginFailed(); } else { apLog_Info((LOG_CHANNEL, LOG_CONTEXT, "Logged in")); nPhase_ = LoggedIn; pClient_->onProtocolLogin(); } } } break; case LoginFailed: apLog_Warning((LOG_CHANNEL, LOG_CONTEXT, "Ignoring stanza after LoginFailed")); result.connectionFinished(1); break; default: apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "Unknown phase")); result.connectionFinished(1); } return ok; }
int PresenceTask::handleStanza(Stanza& stanza, StanzaHandlerResult& result) { // Buddy presence // <presence/> int ok = 1; int bUsed = 0; if (stanza.getName() == "presence") { JabberId from = stanza.getAttribute("from").getValue(); apLog_Verbose((LOG_CHANNEL, LOG_CONTEXT, "<presence from=%s ...", _sz(from))); Room* pRoom = pClient_->findRoom(from.base()); Buddy* pBuddy = pClient_->findBuddy(from.base()); String sType = stanza.getAttribute("type").getValue(); if (sType.empty()) { sType = XMPP_PRESENCE_AVAILABLE; } if (0) { } else if (sType == XMPP_PRESENCE_AVAILABLE) { if (0) { } else if (pRoom != 0){ ok = pRoom->presenceAvailable(stanza); bUsed = 1; } else if (pBuddy != 0) { ok = pBuddy->presenceAvailable(stanza); bUsed = 1; } else if (from.base() == pClient_->getJabberId()) { ok = pClient_->selfPresenceAvailable(stanza); bUsed = 1; } } else if (sType == XMPP_PRESENCE_UNAVAILABLE) { if (0) { } else if (pRoom != 0){ ok = pRoom->presenceUnavailable(stanza); bUsed = 1; } else if (pBuddy != 0) { ok = pBuddy->presenceUnavailable(stanza); bUsed = 1; } else if (from.base() == pClient_->getJabberId()) { ok = pClient_->selfPresenceUnavailable(stanza); bUsed = 1; } } else if (sType == XMPP_PRESENCE_ERROR) { if (0) { } else if (pRoom != 0){ ok = pRoom->presenceError(stanza); bUsed = 1; } else if (pBuddy != 0) { ok = pBuddy->presenceError(stanza); bUsed = 1; } else if (from.base() == pClient_->getJabberId()) { ok = pClient_->selfPresenceError(stanza); bUsed = 1; } } else { // not handled } } if (bUsed) { result.stanzaHandled(1); result.stanzaConsumed(1); } return ok; }