From 7d0d96f94053472202fb2ef8f3f915f5ad3a2286 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 20 Jan 2013 23:59:28 -0800 Subject: Add Google Settings plugin. --- sleekxmpp/plugins/google_settings/__init__.py | 15 ++++ sleekxmpp/plugins/google_settings/settings.py | 65 +++++++++++++++ sleekxmpp/plugins/google_settings/stanza.py | 110 ++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 sleekxmpp/plugins/google_settings/__init__.py create mode 100644 sleekxmpp/plugins/google_settings/settings.py create mode 100644 sleekxmpp/plugins/google_settings/stanza.py (limited to 'sleekxmpp/plugins/google_settings') diff --git a/sleekxmpp/plugins/google_settings/__init__.py b/sleekxmpp/plugins/google_settings/__init__.py new file mode 100644 index 00000000..ef4b2342 --- /dev/null +++ b/sleekxmpp/plugins/google_settings/__init__.py @@ -0,0 +1,15 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.plugins.base import register_plugin + +from sleekxmpp.plugins.google_settings import stanza +from sleekxmpp.plugins.google_settings.settings import GoogleSettings + + +register_plugin(GoogleSettings) diff --git a/sleekxmpp/plugins/google_settings/settings.py b/sleekxmpp/plugins/google_settings/settings.py new file mode 100644 index 00000000..470660e3 --- /dev/null +++ b/sleekxmpp/plugins/google_settings/settings.py @@ -0,0 +1,65 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +import logging + +from sleekxmpp.stanza import Iq +from sleekxmpp.xmlstream.handler import Callback +from sleekxmpp.xmlstream.matcher import MatchXPath +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.plugins import BasePlugin +from sleekxmpp.plugins.google_settings import stanza + + +log = logging.getLogger(__name__) + + +class GoogleSettings(BasePlugin): + + """ + Google: Gmail Notifications + + Also see . + """ + + name = 'google_settings' + description = 'Google: User Settings' + dependencies = set() + stanza = stanza + + def plugin_init(self): + register_stanza_plugin(Iq, stanza.UserSettings) + + self.xmpp.register_handler( + Callback('Google Settings', + MatchXPath('{%s}iq/%s' % ( + self.xmpp.default_ns, + stanza.UserSettings.tag_name())), + self._handle_settings_change)) + + def plugin_end(self): + self.xmpp.remove_handler('Google Settings') + + def get(self, block=True, timeout=None, callback=None): + iq = self.xmpp.Iq() + iq['type'] = 'get' + iq.enable('google_settings') + return iq.send(block=block, timeout=timeout, callback=callback) + + def update(settings, block=True, timeout=None, callback=None): + iq = self.xmpp.Iq() + iq['type'] = 'set' + iq.enable('google_settings') + + for setting, value in settings.items(): + iq['google_settings'][setting] = value + + return iq.send(block=block, timeout=timeout, callback=callback) + + def _handle_settings_change(self, iq): + self.xmpp.event('google_settings_change', iq) diff --git a/sleekxmpp/plugins/google_settings/stanza.py b/sleekxmpp/plugins/google_settings/stanza.py new file mode 100644 index 00000000..cc887880 --- /dev/null +++ b/sleekxmpp/plugins/google_settings/stanza.py @@ -0,0 +1,110 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.xmlstream import ElementBase, register_stanza_plugin + + +class UserSettings(ElementBase): + name = 'usersetting' + namespace = 'google:setting' + plugin_attrib = 'google_settings' + interfaces = set(['auto_accept_suggestions', + 'mail_notifications', + 'archiving_enabled', + 'gmail', + 'email_verified', + 'domain_privacy_notice', + 'display_name']) + + def _get_setting(self, setting): + xml = self.xml.find('{%s}%s' % (self.namespace, setting)) + if xml is not None: + return xml.attrib.get('value', '') == 'true' + return False + + def _set_setting(self, setting, value): + self._del_setting(setting) + if value in (True, False): + xml = ET.Element('{%s}%s' % (self.namespace, setting)) + xml.attrib['value'] = 'true' if value else 'false' + self.xml.append(xml) + + def _del_setting(self, setting): + xml = self.xml.find('{%s}%s' % (self.namespace, setting)) + if xml is not None: + self.xml.remove(xml) + + def get_display_name(self): + xml = self.xml.find('{%s}%s' % (self.namespace, 'displayname')) + if xml is not None: + return xml.attrib.get('value', '') + return '' + + def set_display_name(self, value): + self._del_setting(setting) + if value: + xml = ET.Element('{%s}%s' % (self.namespace, 'displayname')) + xml.attrib['value'] = value + self.xml.append(xml) + + def del_display_name(self): + self._del_setting('displayname') + + def get_auto_accept_suggestions(self): + return self._get_setting('autoacceptsuggestions') + + def get_mail_notifications(self): + return self._get_setting('mailnotifications') + + def get_archiving_enabled(self): + return self._get_setting('archivingenabled') + + def get_gmail(self): + return self._get_setting('gmail') + + def get_email_verified(self): + return self._get_setting('emailverified') + + def get_domain_privacy_notice(self): + return self._get_setting('domainprivacynotice') + + def set_auto_accept_suggestions(self, value): + self._set_setting('autoacceptsuggestions', value) + + def set_mail_notifications(self, value): + self._set_setting('mailnotifications', value) + + def set_archiving_enabled(self, value): + self._set_setting('archivingenabled', value) + + def set_gmail(self, value): + self._set_setting('gmail', value) + + def set_email_verified(self, value): + self._set_setting('emailverified', value) + + def set_domain_privacy_notice(self, value): + self._set_setting('domainprivacynotice', value) + + def del_auto_accept_suggestions(self): + self._del_setting('autoacceptsuggestions') + + def del_mail_notifications(self): + self._del_setting('mailnotifications') + + def del_archiving_enabled(self): + self._del_setting('archivingenabled') + + def del_gmail(self): + self._del_setting('gmail') + + def del_email_verified(self): + self._del_setting('emailverified') + + def del_domain_privacy_notice(self): + self._del_setting('domainprivacynotice') -- cgit v1.2.3 From 903e641457f30670af36a13b51ef0ab3e611bead Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 21 Jan 2013 01:38:42 -0800 Subject: Fix issues in Google settings plugin. --- sleekxmpp/plugins/google_settings/settings.py | 13 ++++++++----- sleekxmpp/plugins/google_settings/stanza.py | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'sleekxmpp/plugins/google_settings') diff --git a/sleekxmpp/plugins/google_settings/settings.py b/sleekxmpp/plugins/google_settings/settings.py index 470660e3..6bd209c7 100644 --- a/sleekxmpp/plugins/google_settings/settings.py +++ b/sleekxmpp/plugins/google_settings/settings.py @@ -10,7 +10,7 @@ import logging from sleekxmpp.stanza import Iq from sleekxmpp.xmlstream.handler import Callback -from sleekxmpp.xmlstream.matcher import MatchXPath +from sleekxmpp.xmlstream.matcher import StanzaPath from sleekxmpp.xmlstream import register_stanza_plugin from sleekxmpp.plugins import BasePlugin from sleekxmpp.plugins.google_settings import stanza @@ -37,9 +37,7 @@ class GoogleSettings(BasePlugin): self.xmpp.register_handler( Callback('Google Settings', - MatchXPath('{%s}iq/%s' % ( - self.xmpp.default_ns, - stanza.UserSettings.tag_name())), + StanzaPath('iq@type=set/google_settings'), self._handle_settings_change)) def plugin_end(self): @@ -51,7 +49,7 @@ class GoogleSettings(BasePlugin): iq.enable('google_settings') return iq.send(block=block, timeout=timeout, callback=callback) - def update(settings, block=True, timeout=None, callback=None): + def update(self, settings, block=True, timeout=None, callback=None): iq = self.xmpp.Iq() iq['type'] = 'set' iq.enable('google_settings') @@ -62,4 +60,9 @@ class GoogleSettings(BasePlugin): return iq.send(block=block, timeout=timeout, callback=callback) def _handle_settings_change(self, iq): + reply = self.xmpp.Iq() + reply['type'] = 'result' + reply['id'] = iq['id'] + reply['to'] = iq['from'] + reply.send() self.xmpp.event('google_settings_change', iq) diff --git a/sleekxmpp/plugins/google_settings/stanza.py b/sleekxmpp/plugins/google_settings/stanza.py index cc887880..d8161770 100644 --- a/sleekxmpp/plugins/google_settings/stanza.py +++ b/sleekxmpp/plugins/google_settings/stanza.py @@ -6,7 +6,7 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.xmlstream import ElementBase, register_stanza_plugin +from sleekxmpp.xmlstream import ET, ElementBase, register_stanza_plugin class UserSettings(ElementBase): -- cgit v1.2.3