summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Sluift/client.cpp')
-rw-r--r--Sluift/client.cpp110
1 files changed, 35 insertions, 75 deletions
diff --git a/Sluift/client.cpp b/Sluift/client.cpp
index db259cd..06df6a4 100644
--- a/Sluift/client.cpp
+++ b/Sluift/client.cpp
@@ -12,33 +12,40 @@
#include <Sluift/SluiftClient.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Elements/SoftwareVersion.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Elements/RawXMLPayload.h>
#include <Swiften/Elements/RosterItemPayload.h>
#include <Swiften/Elements/RosterPayload.h>
#include <Swiften/Elements/DiscoInfo.h>
+#include <Swiften/Elements/MAMQuery.h>
#include <Swiften/Disco/ClientDiscoManager.h>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Presence/PresenceSender.h>
#include <Swiften/Roster/XMPPRoster.h>
#include <Swiften/Roster/SetRosterRequest.h>
#include <Swiften/Presence/SubscriptionManager.h>
#include <Swiften/Roster/XMPPRosterItem.h>
+#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h>
#include <Sluift/Lua/FunctionRegistration.h>
#include <Swiften/Base/foreach.h>
+#include <Swiften/Base/IDGenerator.h>
#include <Sluift/Lua/Check.h>
#include <Sluift/Lua/Value.h>
#include <Sluift/Lua/Exception.h>
#include <Sluift/Lua/LuaUtils.h>
#include <Sluift/globals.h>
+#include <Sluift/ElementConvertors/StanzaConvertor.h>
+#include <Sluift/ElementConvertors/IQConvertor.h>
+#include <Sluift/ElementConvertors/PresenceConvertor.h>
+#include <Sluift/ElementConvertors/MessageConvertor.h>
using namespace Swift;
namespace lambda = boost::lambda;
static inline SluiftClient* getClient(lua_State* L) {
return *Lua::checkUserData<SluiftClient>(L, 1);
}
static inline int getGlobalTimeout(lua_State* L) {
@@ -230,33 +237,19 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
if (boost::optional<std::string> value = Lua::getStringField(L, index, "to")) {
to = *value;
}
if (boost::optional<std::string> value = Lua::getStringField(L, index, "body")) {
body = value;
}
if (boost::optional<std::string> value = Lua::getStringField(L, index, "type")) {
- if (*value == "normal") {
- type = Message::Normal;
- }
- else if (*value == "chat") {
- type = Message::Chat;
- }
- else if (*value == "error") {
- type = Message::Error;
- }
- else if (*value == "groupchat") {
- type = Message::Groupchat;
- }
- else if (*value == "headline") {
- type = Message::Headline;
- }
+ type = MessageConvertor::convertMessageTypeFromString(*value);
}
if (boost::optional<std::string> value = Lua::getStringField(L, index, "subject")) {
subject = value;
}
payloads = getPayloadsFromTable(L, index);
}
@@ -306,42 +299,19 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
presence->setTo(*value);
}
if (boost::optional<std::string> value = Lua::getStringField(L, index, "status")) {
presence->setStatus(*value);
}
if (boost::optional<int> value = Lua::getIntField(L, index, "priority")) {
presence->setPriority(*value);
}
if (boost::optional<std::string> value = Lua::getStringField(L, index, "type")) {
- if (*value == "available") {
- presence->setType(Presence::Available);
- }
- else if (*value == "error") {
- presence->setType(Presence::Error);
- }
- else if (*value == "probe") {
- presence->setType(Presence::Probe);
- }
- else if (*value == "subscribe") {
- presence->setType(Presence::Subscribe);
- }
- else if (*value == "subscribed") {
- presence->setType(Presence::Subscribed);
- }
- else if (*value == "unavailable") {
- presence->setType(Presence::Unavailable);
- }
- else if (*value == "unsubscribe") {
- presence->setType(Presence::Unsubscribe);
- }
- else if (*value == "unsubscribed") {
- presence->setType(Presence::Unsubscribed);
- }
+ presence->setType(PresenceConvertor::convertPresenceTypeFromString(*value));
}
std::vector< boost::shared_ptr<Payload> > payloads = getPayloadsFromTable(L, index);
presence->addPayloads(payloads.begin(), payloads.end());
}
getClient(L)->getClient()->getPresenceSender()->sendPresence(presence);
lua_pushvalue(L, 1);
return 0;
}
@@ -382,27 +352,19 @@ SLUIFT_LUA_FUNCTION(Client, query_pubsub) {
}
int timeout = getGlobalTimeout(L);
if (boost::optional<int> timeoutInt = Lua::getIntField(L, 2, "timeout")) {
timeout = *timeoutInt;
}
IQ::Type type;
if (boost::optional<std::string> queryType = Lua::getStringField(L, 2, "type")) {
- if (*queryType == "get") {
- type = IQ::Get;
- }
- else if (*queryType == "set") {
- type = IQ::Set;
- }
- else {
- throw Lua::Exception("Illegal query type: '" + *queryType + "'");
- }
+ type = IQConvertor::convertIQTypeFromString(*queryType);
}
else {
throw Lua::Exception("Missing query type");
}
lua_getfield(L, 2, "query");
if (!lua_istable(L, -1)) {
throw Lua::Exception("Missing/incorrect query");
}
@@ -484,70 +446,68 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
}
lua_getfield(L, 2, "allow_plain_without_tls");
if (!lua_isnil(L, -1)) {
client->getOptions().allowPLAINWithoutTLS = lua_toboolean(L, -1);
}
lua_pushvalue(L, 1);
return 0;
}
-static std::string convertPresenceTypeToString(Presence::Type type) {
- std::string result;
-
- switch (type) {
- case Presence::Available: result = "available"; break;
- case Presence::Error: result = "error"; break;
- case Presence::Probe: result = "probe"; break;
- case Presence::Subscribe: result = "subscribe"; break;
- case Presence::Subscribed: result = "subscribed"; break;
- case Presence::Unavailable: result = "unavailable"; break;
- case Presence::Unsubscribe: result = "unsubscribe"; break;
- case Presence::Unsubscribed: result = "unsubscribed"; break;
- }
+SLUIFT_LUA_FUNCTION_WITH_HELP(
+ Client, send_mam_query,
- return result;
-}
+ "Builds and sends a MAM query.\n",
-static std::string convertMessageTypeToString(Message::Type type) {
- std::string result;
+ "self\n"
+ "mam_query parameters for the query\n"
+ "jid optional jid to set in the 'to' field of the IQ stanza",
- switch (type) {
- case Message::Normal: result = "normal"; break;
- case Message::Chat: result = "chat"; break;
- case Message::Error: result = "error"; break;
- case Message::Groupchat: result = "groupchat"; break;
- case Message::Headline: result = "headline"; break;
+ "See help('MAMQuery') for details."
+) {
+ if (!lua_istable(L, 2)) {
+ throw Lua::Exception("Missing MAMQuery");
+ }
+ if (boost::shared_ptr<MAMQuery> mamQuery = boost::dynamic_pointer_cast<MAMQuery>(Sluift::globals.elementConvertor.convertFromLuaUntyped(L, 2, "mam_query"))) {
+ IQRouter *router = getClient(L)->getClient()->getIQRouter();
+ JID jid;
+ lua_getfield(L, 2, "jid");
+ if (!lua_isnil(L, -1)) {
+ jid = JID(lua_tostring(L, -1));
+ }
+ router->sendIQ(IQ::createRequest(IQ::Get, jid, IDGenerator().generateID(), mamQuery));
}
-
- return result;
+ else {
+ throw Lua::Exception("Illegal MAMQuery");
+ }
+ return 0;
}
static void pushEvent(lua_State* L, const SluiftClient::Event& event) {
switch (event.type) {
case SluiftClient::Event::MessageType: {
Message::ref message = boost::dynamic_pointer_cast<Message>(event.stanza);
Lua::Table result = boost::assign::map_list_of
("type", boost::make_shared<Lua::Value>(std::string("message")))
("from", boost::make_shared<Lua::Value>(message->getFrom().toString()))
("body", boost::make_shared<Lua::Value>(message->getBody()))
- ("message_type", boost::make_shared<Lua::Value>(convertMessageTypeToString(message->getType())));
+ ("message_type", boost::make_shared<Lua::Value>(MessageConvertor::convertMessageTypeToString(message->getType())));
Lua::pushValue(L, result);
addPayloadsToTable(L, message->getPayloads());
Lua::registerTableToString(L, -1);
break;
}
case SluiftClient::Event::PresenceType: {
Presence::ref presence = boost::dynamic_pointer_cast<Presence>(event.stanza);
Lua::Table result = boost::assign::map_list_of
("type", boost::make_shared<Lua::Value>(std::string("presence")))
("from", boost::make_shared<Lua::Value>(presence->getFrom().toString()))
("status", boost::make_shared<Lua::Value>(presence->getStatus()))
- ("presence_type", boost::make_shared<Lua::Value>(convertPresenceTypeToString(presence->getType())));
+ ("presence_type", boost::make_shared<Lua::Value>(PresenceConvertor::convertPresenceTypeToString(presence->getType())));
Lua::pushValue(L, result);
addPayloadsToTable(L, presence->getPayloads());
Lua::registerTableToString(L, -1);
break;
}
case SluiftClient::Event::PubSubEventType: {
Sluift::globals.elementConvertor.convertToLua(L, event.pubsubEvent);
lua_pushstring(L, "pubsub");
lua_setfield(L, -2, "type");