summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2011-02-27 22:45:32 (GMT)
committerKevin Smith <git@kismith.co.uk>2011-03-06 20:25:01 (GMT)
commit27d21b371f24272466a2d6a5bf2e2b717ee2d9fc (patch)
tree5f53281711d4f467933e4b3315241e4eee58a64c /Swiftob/MUCs.cpp
parentd9c9df3b4ae5432552417fc4db74d62ab34f066d (diff)
downloadswift-27d21b371f24272466a2d6a5bf2e2b717ee2d9fc.zip
swift-27d21b371f24272466a2d6a5bf2e2b717ee2d9fc.tar.bz2
A start on Swiftob, a Swiften-based chatbot.
Diffstat (limited to 'Swiftob/MUCs.cpp')
-rw-r--r--Swiftob/MUCs.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/Swiftob/MUCs.cpp b/Swiftob/MUCs.cpp
new file mode 100644
index 0000000..55bf313
--- /dev/null
+++ b/Swiftob/MUCs.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2011 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swiftob/MUCs.h"
+
+#include <Swiften/Client/Client.h>
+#include <Swiften/MUC/MUC.h>
+#include <Swiften/MUC/MUCManager.h>
+#include <Swiften/Base/String.h>
+
+#include "Swiftob/Storage.h"
+
+#define MUC_LIST_SETTING "muc_list"
+
+typedef std::pair<JID, MUC::ref> JIDMUCPair;
+
+MUCs::MUCs(Client* client, Storage* storage) : defaultNick_("Kanchil+") {
+ client_ = client;
+ storage_ = storage;
+ client_->onConnected.connect(boost::bind(&MUCs::handleConnected, this));
+}
+
+void MUCs::handleConnected() {
+ foreach (std::string room, String::split(storage_->getSetting(MUC_LIST_SETTING), ' ')) {
+ join(JID(room), boost::bind(&MUCs::handleInitialJoinSuccess, this), boost::bind(&MUCs::handleInitialJoinFailure, this, _1));
+ }
+}
+
+void MUCs::handleInitialJoinSuccess() {
+
+}
+
+void MUCs::handleInitialJoinFailure(const std::string&) {
+
+}
+
+void MUCs::join(const JID& room, boost::signal<void (const std::string&)>::slot_type successCallback, boost::function<void(const std::string& /*reason*/)> failureCallback) {
+ if (contains(room)) {
+ failureCallback("Already in room");
+ }
+ mucs_[room] = client_->getMUCManager()->createMUC(room);
+ mucs_[room]->onJoinComplete.connect(successCallback);
+ mucs_[room]->onJoinFailed.connect(boost::bind(&MUCs::handleJoinFailed, this, room, _1, failureCallback));
+ mucs_[room]->joinWithContextSince(defaultNick_, boost::posix_time::microsec_clock::universal_time());
+ save();
+}
+
+void MUCs::part(const JID& room) {
+ if (!contains(room)) {
+ return;
+ }
+ mucs_[room]->part();
+}
+
+bool MUCs::contains(const JID& room) {
+ return mucs_.find(room) != mucs_.end();
+}
+
+void MUCs::handleJoinFailed(const JID& muc, ErrorPayload::ref error, boost::function<void(const std::string& /*reason*/)> failureCallback) {
+ std::string errorMessage("Couldn't join room");
+ if (error) {
+ switch (error->getCondition()) {
+ case ErrorPayload::Conflict:
+ // rejoinNick = nick_ + "_";
+ errorMessage += ": Nickname in use";
+ // errorMessage = str(format("Unable to enter this room as %1%, retrying as %2%") % nick_ % rejoinNick);
+ break;
+ case ErrorPayload::JIDMalformed:
+ errorMessage += ": ";
+ errorMessage += "No nickname specified";
+ break;
+ case ErrorPayload::NotAuthorized:
+ errorMessage += ": ";
+ errorMessage += "A password needed";
+ break;
+ case ErrorPayload::RegistrationRequired:
+ errorMessage += ": ";
+ errorMessage += "Only members may enter";
+ break;
+ case ErrorPayload::Forbidden:
+ errorMessage += ": ";
+ errorMessage += "You are banned from the room";
+ break;
+ case ErrorPayload::ServiceUnavailable:
+ errorMessage += ": ";
+ errorMessage += "The room is full";
+ break;
+ case ErrorPayload::ItemNotFound:
+ errorMessage += ": ";
+ errorMessage += "The room does not exist";
+ break;
+ default:
+ break;
+ }
+ if (!error->getText().empty()) {
+ errorMessage += " - " + error->getText();
+ }
+ }
+ mucs_.erase(muc);
+ failureCallback(errorMessage);
+}
+
+void MUCs::save() {
+ std::string concat;
+ foreach (JIDMUCPair pair, mucs_) {
+ concat += pair.first.toString() + " ";
+ }
+ storage_->saveSetting(MUC_LIST_SETTING, concat);
+}
+
+MUC::ref MUCs::getMUC(const JID& room) {
+ return (mucs_.find(room) != mucs_.end()) ? mucs_[room] : MUC::ref();
+}