soc.2009.transport: 51e1e912: Transport settings Adhoc command

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Sun Jun 14 05:25:28 EDT 2009


-----------------------------------------------------------------
Revision: 51e1e912f22b657146a10d05c7ef6473ac6f72cf
Ancestor: a0846900f3a20afc48915df8974aa58bcb7117fa
Author: hanzz at soc.pidgin.im
Date: 2009-06-14T08:59:35
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/51e1e912f22b657146a10d05c7ef6473ac6f72cf

Added files:
        adhocsettings.cpp adhocsettings.h
Modified files:
        CMakeLists.txt adhochandler.cpp adhochandler.h
        adhocrepeater.cpp localization.h sql.cpp sql.h user.cpp
        user.h

ChangeLog: 

Transport settings Adhoc command

-------------- next part --------------
============================================================
--- adhocsettings.cpp	1995f5a816b804443e7c2da53c91a55851e0ef10
+++ adhocsettings.cpp	1995f5a816b804443e7c2da53c91a55851e0ef10
@@ -0,0 +1,127 @@
+/**
+ * XMPP - libpurple transport
+ *
+ * Copyright (C) 2009, Jan Kaluza <hanzz at soc.pidgin.im>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ */
+ 
+#include "adhocsettings.h"
+#include "gloox/stanza.h"
+#include "log.h"
+
+ 
+AdhocSettings::AdhocSettings(GlooxMessageHandler *m, User *user, const std::string &from, const std::string &id) {
+	main = m;
+	m_user = user;
+	std::string setting;
+	
+	IQ _response(IQ::Result, from, id);
+	Tag *response = _response.tag();
+	response->addAttribute("from",main->jid());
+
+	Tag *c = new Tag("command");
+	c->addAttribute("xmlns","http://jabber.org/protocol/commands");
+	c->addAttribute("sessionid",main->j->getID());
+	c->addAttribute("node","transport_settings");
+	c->addAttribute("status","executing");
+
+	Tag *actions = new Tag("actions");
+	actions->addAttribute("execute","complete");
+	actions->addChild(new Tag("complete"));
+	c->addChild(actions);
+
+	Tag *xdata = new Tag("x");
+	xdata->addAttribute("xmlns","jabber:x:data");
+	xdata->addAttribute("type","form");
+	xdata->addChild(new Tag("title","Transport settings"));
+	xdata->addChild(new Tag("instructions","Change your transport settings here."));
+
+	Tag *field = new Tag("field");
+	field->addAttribute("type","boolean");
+	field->addAttribute("label","Enable transport");
+	field->addAttribute("var","enable_transport");
+	setting = m_user->getSetting("enable_transport");
+	if (setting.empty() || setting == "0")
+		field->addChild(new Tag("value","0"));
+	else
+		field->addChild(new Tag("value","1"));
+	xdata->addChild(field);
+
+	c->addChild(xdata);
+	response->addChild(c);
+	main->j->send(response);
+
+}
+
+AdhocSettings::~AdhocSettings() {}
+
+bool AdhocSettings::handleIq(const IQ &stanza) {
+	Tag *tag = stanza.tag()->findChild( "command" );
+	if (tag->hasAttribute("action","cancel")){
+		IQ _response(IQ::Result, stanza.from().full(), stanza.id());
+		_response.setFrom(main->jid());
+		Tag *response = _response.tag();
+
+		Tag *c = new Tag("command");
+		c->addAttribute("xmlns","http://jabber.org/protocol/commands");
+		c->addAttribute("sessionid",tag->findAttribute("sessionid"));
+		c->addAttribute("node","configuration");
+		c->addAttribute("status","canceled");
+		main->j->send(response);
+
+// 		g_timeout_add(0,&removeHandler,this);
+
+		return true;
+	}
+	
+	Tag *x = tag->findChildWithAttrib("xmlns","jabber:x:data");
+	if (x) {
+		std::string result("");
+		for(std::list<Tag*>::const_iterator it = x->children().begin(); it != x->children().end(); ++it){
+			std::string key = (*it)->findAttribute("var");
+			if (key.empty()) continue;
+			
+			std::string savedValue = m_user->getSetting(key.c_str());
+			if (savedValue.empty()) continue;
+			
+			Tag *v =(*it)->findChild("value");
+			if (!v) continue;
+			
+			std::string value = v->cdata();
+			if (savedValue == value) continue;
+			
+			m_user->updateSetting(key, value);
+		}
+
+		IQ _s(IQ::Result, stanza.from().full(), stanza.id());
+		_s.setFrom(main->jid());
+		Tag *s = _s.tag();
+
+		Tag *c = new Tag("command");
+		c->addAttribute("xmlns","http://jabber.org/protocol/commands");
+		c->addAttribute("sessionid",tag->findAttribute("sessionid"));
+		c->addAttribute("node","configuration");
+		c->addAttribute("status","completed");
+		main->j->send(s);
+		
+// 		g_timeout_add(0,&removeRepeater,this);
+		
+	}
+
+
+	return true;
+}
+
============================================================
--- adhocsettings.h	f50f758f65f66b644f845eefa307e060a8a407bb
+++ adhocsettings.h	f50f758f65f66b644f845eefa307e060a8a407bb
@@ -0,0 +1,47 @@
+/**
+ * XMPP - libpurple transport
+ *
+ * Copyright (C) 2009, Jan Kaluza <hanzz at soc.pidgin.im>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ */
+ 
+#ifndef _HI_ADHOC_SETTINGS_H
+#define _HI_ADHOC_SETTINGS_H
+
+#include <string>
+#include "account.h"
+#include "user.h"
+#include "glib.h"
+#include "request.h"
+#include "adhoccommandhandler.h"
+
+class GlooxMessageHandler;
+class User;
+
+class AdhocSettings : public AdhocCommandHandler
+{
+	public:
+		AdhocSettings(GlooxMessageHandler *m, User *user, const std::string &from, const std::string &id);
+		~AdhocSettings();
+		bool handleIq(const IQ &iq);
+	
+	private:
+		GlooxMessageHandler *main;
+		User *m_user;
+};
+
+#endif
+ 
\ No newline at end of file
============================================================
--- CMakeLists.txt	cd1f3c9ca955c8a0d398a4ff6cd713be1ec7f55c
+++ CMakeLists.txt	c2244a1c346875818ea8a2baafa183292d1e5313
@@ -90,6 +90,7 @@
 	adhocrepeater.cpp
 	localization.cpp
 	muchandler.cpp
+	adhocsettings.cpp
 	protocols/icq.cpp
 	protocols/facebook.cpp
 	protocols/gg.cpp
@@ -122,6 +123,7 @@
 	localization.h
 	muchandler.h
 	adhoccommandhandler.h
+	adhocsettings.h
 	protocols/abstractprotocol.h
 	protocols/icq.h
 	protocols/facebook.h
============================================================
--- adhochandler.cpp	28e12c5ee42e406d43df01bf7248c33d43dcff02
+++ adhochandler.cpp	4587e178fbdef0f00291853b8a9522a0d3287bbe
@@ -18,13 +18,19 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
  
- #include "adhochandler.h"
- #include "usermanager.h"
- #include "log.h"
- #include "adhocrepeater.h"
- #include "gloox/disconodehandler.h"
- #include "gloox/adhoc.h"
+#include "adhochandler.h"
+#include "usermanager.h"
+#include "log.h"
+#include "adhocrepeater.h"
+#include "adhocsettings.h"
+#include "gloox/disconodehandler.h"
+#include "gloox/adhoc.h"
  
+static AdhocCommandHandler * createSettingsHandler(GlooxMessageHandler *m, User *user, const std::string &from, const std::string &id) {
+	AdhocCommandHandler *handler = new AdhocSettings(m, user, from, id);
+	return handler;
+}
+ 
 GlooxAdhocHandler::GlooxAdhocHandler(GlooxMessageHandler *m) {
 	main = m;
 	main->j->registerIqHandler( this, ExtAdhocCommand );
@@ -32,7 +38,9 @@ GlooxAdhocHandler::GlooxAdhocHandler(Glo
 	main->j->disco()->addFeature( XMLNS_ADHOC_COMMANDS );
 	main->j->disco()->registerNodeHandler( this, XMLNS_ADHOC_COMMANDS );
 	main->j->disco()->registerNodeHandler( this, std::string() );
-
+	
+	m_handlers["transport_settings"].name = "Transport settings";
+	m_handlers["transport_settings"].createHandler = createSettingsHandler;
 }
 
 GlooxAdhocHandler::~GlooxAdhocHandler() { }
@@ -55,6 +63,9 @@ Disco::ItemList GlooxAdhocHandler::handl
 		if (to == main->jid()) {
 			User *user = main->userManager()->getUserByJID(from);
 			if (user) {
+				for(std::map<std::string, adhocCommand>::iterator u = m_handlers.begin(); u != m_handlers.end() ; u++) {
+					lst.push_back( new Disco::Item( main->jid(),(*u).first, (std::string) tr(user->getLang(),(*u).second.name) ) );
+				}
 				if (user->isConnected() && purple_account_get_connection(user->account())) {
 					PurpleConnection *gc = purple_account_get_connection(user->account());
 					PurplePlugin *plugin = gc && PURPLE_CONNECTION_IS_CONNECTED(gc) ? gc->prpl : NULL;
@@ -119,11 +130,21 @@ bool GlooxAdhocHandler::handleIq( const 
 
 	User *user = main->userManager()->getUserByJID(from);
 	if (user) {
-		if (user->isConnected() && purple_account_get_connection(user->account())) {
-			if (hasSession(stanza.from().full())) {
-				return m_sessions[stanza.from().full()]->handleIq(stanza);
+		if (hasSession(stanza.from().full())) {
+			if( m_sessions[stanza.from().full()]->handleIq(stanza) ) {
+				std::string jid = stanza.from().full();
+				delete m_sessions[jid];
+				unregisterSession(jid);
 			}
-			else if (to == main->jid()) {
+			return true;
+		}
+		else if (m_handlers.find(node) != m_handlers.end()) {
+			AdhocCommandHandler *handler = m_handlers[node].createHandler(main, user, stanza.from().full(), stanza.id());
+			registerSession(stanza.from().full(), handler);
+			return true;
+		}
+		if (user->isConnected() && purple_account_get_connection(user->account())) {
+			if (to == main->jid()) {
 				PurpleConnection *gc = purple_account_get_connection(user->account());
 				PurplePlugin *plugin = gc && PURPLE_CONNECTION_IS_CONNECTED(gc) ? gc->prpl : NULL;
 				if (plugin && PURPLE_PLUGIN_HAS_ACTIONS(plugin)) {
============================================================
--- adhochandler.h	64fb66fbf5b8272e79b374240550475f76ee4158
+++ adhochandler.h	0949f16231e286dc210d600ade72a82478a1bf8e
@@ -33,8 +33,7 @@ struct adhocCommand {
 
 struct adhocCommand {
 	std::string name;
-	std::string node;
-	AdhocCommandHandler (*createHandler)(GlooxMessageHandler *m, User *user);
+	AdhocCommandHandler * (*createHandler)(GlooxMessageHandler *m, User *user, const std::string &from, const std::string &id);
 };
 
 class GlooxAdhocHandler : public DiscoNodeHandler, public DiscoHandler, public IqHandler
@@ -57,6 +56,7 @@ class GlooxAdhocHandler : public DiscoNo
 	private:
 		GlooxMessageHandler *main;
 		std::map<std::string, AdhocCommandHandler *> m_sessions;
+		std::map<std::string, adhocCommand> m_handlers;	// m_handlers[node] = handler
 
 };
 
============================================================
--- adhocrepeater.cpp	e6b8a4ec9bf6bb92e424fa5b9ef5e131020fa0bb
+++ adhocrepeater.cpp	2d2e1a37ac9e0f387ef7c751c2aea2581bc8699c
@@ -160,7 +160,7 @@ bool AdhocRepeater::handleIq(const IQ &s
 
 		g_timeout_add(0,&removeRepeater,this);
 
-		return true;
+		return false;
 	}
 	
 	Tag *x = tag->findChildWithAttrib("xmlns","jabber:x:data");
@@ -203,6 +203,6 @@ bool AdhocRepeater::handleIq(const IQ &s
 	}
 
 
-	return true;
+	return false;
 }
 
============================================================
--- localization.h	094f6506cc163319c482410e1874c87c80156435
+++ localization.h	d8f6ea769348d6015fbfe129cd35196e166e1377
@@ -32,6 +32,7 @@ class Localization {
 		
 		bool loadLocale(const std::string &lang);
 		const char * translate(const char *lang, const char *key);
+		const char * translate(const char *lang, std::string key) { return translate(lang, key.c_str()); }
 		
 	private:
 		GHashTable *m_locales;		// xml_lang, hash table with localizations
============================================================
--- sql.cpp	d9016ca070e9c5be2d6121d84caca97c2b0dc001
+++ sql.cpp	d617e860739f28d13f23a3e0a47af3863493cac6
@@ -330,13 +330,13 @@ void SQLClass::getRandomStatus(std::stri
 
 // settings
 
-void SQLClass::addSetting(const std::string &jid, const std::string &key, const std::string &value, int type) {
+void SQLClass::addSetting(const std::string &jid, const std::string &key, const std::string &value, SettingType type) {
 	mysqlpp::Query query = sql->query();
 	query << "INSERT INTO "<< p->configuration().sqlPrefix <<"settings " << "(jid, var, type, value) VALUES (\"" << jid << "\",\"" << key << "\", \"" << type << "\", \"" << value << "\")";
 	query.execute();
 }
 
-void SQLClass::updateSetting(const std::string &jid, const std::string &key, const std::string &value, int type) {
+void SQLClass::updateSetting(const std::string &jid, const std::string &key, const std::string &value) {
 	mysqlpp::Query query = sql->query();
 	query << "UPDATE "<< p->configuration().sqlPrefix <<"settings SET value=\"" << value <<"\" WHERE jid=\"" << jid << "\" AND var=\"" << key << "\";";
 	query.execute();
============================================================
--- sql.h	626d6f80eb5d33f41d6236e13baf260965a9e0d4
+++ sql.h	7cdc7138b7e6fd5ad70f6f2ddd026e6d4dff16b0
@@ -28,6 +28,9 @@ using namespace gloox;
 #include "main.h"
 using namespace gloox;
 
+typedef enum { 	SETTING_BOOLEAN = 2,
+				} SettingType;
+
 struct UserRow {
 	long id;
 	std::string jid;
@@ -69,8 +72,8 @@ public:
     void getRandomStatus(std::string & status);
 	
 	// settings
-	void addSetting(const std::string &jid, const std::string &key, const std::string &value, int type);
-	void updateSetting(const std::string &jid, const std::string &key, const std::string &value, int type);
+	void addSetting(const std::string &jid, const std::string &key, const std::string &value, SettingType type);
+	void updateSetting(const std::string &jid, const std::string &key, const std::string &value);
 	void getSetting(const std::string &jid, const std::string &key);
 	GHashTable * getSettings(const std::string &jid);
 	
============================================================
--- user.cpp	f591f4c77514348e85bc6ac1f904444517048f04
+++ user.cpp	ccf973635cf814780dd166295e89a459934295d7
@@ -45,6 +45,7 @@ User::User(GlooxMessageHandler *parent, 
 	m_connected = false;
 	m_roster = p->sql()->getRosterByJid(m_jid);
 	m_vip = p->sql()->isVIP(m_jid);
+	m_settings = p->sql()->getSettings(m_jid);
 	m_syncTimer = 0;
 	m_readyForConnect = false;
 	m_rosterXCalled = false;
@@ -55,6 +56,15 @@ User::User(GlooxMessageHandler *parent, 
 	m_lang = NULL;
 	this->features = 6; // TODO: I can't be hardcoded
 	m_mucs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+	std::string setting;
+	
+	// check default settings
+	setting = getSetting("enable_transport");
+	if (setting.empty()) {
+		p->sql()->addSetting(m_jid, "enable_transport", "1", SETTING_BOOLEAN);
+		g_hash_table_replace(m_settings, g_strdup("enable_transport"), g_strdup("1"));
+	}
+	
 }
 
 bool User::syncCallback() {
@@ -572,6 +582,18 @@ void User::purpleChatRemoveUsers(PurpleC
 	}
 }
 
+std::string User::getSetting(const char *key) {
+	char *value = (char *) g_hash_table_lookup(m_settings, key);
+	if (!value)
+		return std::string("");
+	return std::string(value);
+}
+
+void User::updateSetting(const std::string &key, const std::string &value) {
+	p->sql()->updateSetting(m_jid, key, value);
+	g_hash_table_replace(m_settings, g_strdup(key.c_str()), g_strdup(value.c_str()));
+}
+
 /*
  * Called when legacy network user stops typing.
  */
============================================================
--- user.h	df704a2f6dafc1de2fabac290322b1303ddab517
+++ user.h	ce64b18c320edb82385a258add824caf2e45597c
@@ -109,6 +109,10 @@ class User {
 		// Connected
 		bool isConnected() { return m_connected; }
 
+		// Settings
+		std::string getSetting(const char *key);
+		void updateSetting(const std::string &key, const std::string &value);
+
 		// Entity Capabilities
 		std::string capsVersion() { return m_capsVersion; }
 		void setCapsVersion(const std::string &capsVersion) { m_capsVersion = capsVersion; }
@@ -163,6 +167,7 @@ class User {
 		std::map<std::string,authRequest> m_authRequests;	// list of authorization requests (holds callbacks and user data)
 		std::map<std::string,PurpleConversation *> m_conversations; // list of opened conversations
 		std::map<std::string,PurpleBuddy *> m_subscribeCache;	// cache for contacts for roster X
+		GHashTable *m_settings;		// user settings
 };
 
 #endif


More information about the Commits mailing list