From 5d1953a807333c6e013d6be818f2e7c7133201df Mon Sep 17 00:00:00 2001 From: mathieui Date: Tue, 24 May 2011 20:33:37 +0200 Subject: fixes #2187 (import/export) --- src/roster.py | 12 ++++++++++++ src/tabs.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/roster.py b/src/roster.py index 79de1868..aed5f5a0 100644 --- a/src/roster.py +++ b/src/roster.py @@ -22,6 +22,7 @@ import logging log = logging.getLogger(__name__) from config import config +from os import path as p from contact import Contact, Resource from sleekxmpp.xmlstream.stanzabase import JID @@ -33,6 +34,17 @@ class Roster(object): self._contacts = {} # key = bare jid; value = Contact() self._roster_groups = [] + def export(self, path): + if p.isfile(path): + return + try: + f = open(path, 'w+') + f.writelines([i + "\n" for i in self._contacts]) + f.close() + return True + except IOError: + return + def add_contact(self, contact, jid): """ Add a contact to the contact list diff --git a/src/tabs.py b/src/tabs.py index 17b15cf3..0bcc95e9 100644 --- a/src/tabs.py +++ b/src/tabs.py @@ -50,6 +50,7 @@ from config import config from roster import RosterGroup, roster from contact import Contact, Resource from user import User +from os import getenv, path from logger import logger from datetime import datetime, timedelta @@ -1155,6 +1156,8 @@ class RosterInfoTab(Tab): self.commands['accept'] = (self.command_accept, _("Usage: /accept [jid]\nAccept: Use this command to authorize the provided JID (or the selected contact in your roster), to see your presence, and to ask to subscribe to it (mutual presence subscription)."), self.completion_deny) self.commands['add'] = (self.command_add, _("Usage: /add \Add: Use this command to add the specified JID to your roster. The reverse authorization will automatically be accepted if the remote JID accepts your subscription, leading to a mutual presence subscription."), None) self.commands['remove'] = (self.command_remove, _("Usage: /remove [jid]\Remove: Use this command to remove the specified JID from your roster. This wil unsubscribe you from its presence, cancel its subscription to yours, and remove the item from your roster"), self.completion_remove) + self.commands['export'] = (self.command_export, _("Usage: /export [/path/to/file]\nExport: Use this command to export your contacts into /path/to/file if specified, or $HOME/poezio_contacts if not."), None) + self.commands['import'] = (self.command_import, _("Usage: /import [/path/to/file]\nImport: Use this command to import your contacts from /path/to/file if specified, or $HOME/poezio_contacts if not."), None) self.resize() def resize(self): @@ -1222,6 +1225,56 @@ class RosterInfoTab(Tab): self.core.xmpp.sendPresence(pto=jid, ptype='unsubscribed') self.core.xmpp.del_roster_item(jid=jid) + def command_import(self, arg): + """ + Import the contacts + """ + args = common.shell_split(arg) + if len(args): + if args[0].startswith('/'): + filepath = args[0] + else: + filepath = path.join(getenv('HOME'), args[0]) + else: + filepath = path.join(getenv('HOME'), 'poezio_contacts') + if not path.isfile(filepath): + self.core.information('The file %s does not exist' % filepath, 'Error') + return + try: + handle = open(filepath, 'r') + lines = handle.readlines() + handle.close() + except IOError: + self.core.information('Could not open %s' % filepath, 'Error') + return + for jid in lines: + self.command_add(jid.lstrip('\n')) + self.core.information('Contacts imported from %s' % filepath, 'Info') + + + def command_export(self, arg): + """ + Export the contacts + """ + args = common.shell_split(arg) + if len(args): + if args[0].startswith('/'): + filepath = args[0] + else: + filepath = path.join(getenv('HOME'), args[0]) + else: + filepath = path.join(getenv('HOME'), 'poezio_contacts') + if path.isfile(filepath): + self.core.information('The file already exists', 'Error') + return + elif not path.isdir(path.dirname(filepath)): + self.core.information('Parent directory not found', 'Error') + return + if roster.export(filepath): + self.core.information('Contacts exported to %s' % filepath, 'Info') + else: + self.core.information('Failed to export contacts to %s' % filepath, 'Info') + def completion_remove(self, the_input): """ From with any JID presence in the roster -- cgit v1.2.3