Пример #1
0
def _initialize_mailpile_for_testing(workdir, test_data):
    config = mailpile.app.ConfigManager(workdir=workdir,
                                        rules=mailpile.defaults.CONFIG_RULES)
    config.sys.http_port = random.randint(33500, 34000)
    session = mailpile.ui.Session(config)
    session.config.load(session)
    session.main = True
    ui = session.ui = SilentInteraction(config)

    mp = mailpile.Mailpile(session=session)
    session.config.plugins.load('demos')
    mp.set('prefs.index_encrypted=true')

    # Add some mail, scan it.
    # Create local mailboxes
    session.config.open_local_mailbox(session)
    for t in TAGS:
        AddTag(session, arg=[t]).run(save=False)
        session.config.get_tag(t).update(TAGS[t])
    Filter(session,
           arg=['new', '@incoming', '+Inbox', '+New',
                'Incoming mail filter']).run(save=False)

    mp.add(test_data)
    mp.rescan()

    return mp, session, config, ui
Пример #2
0
def _initialize_mailpile_for_testing(workdir, test_data):
    config = mailpile.config.manager.ConfigManager(
        workdir=workdir, rules=mailpile.config.defaults.CONFIG_RULES)
    session = mailpile.ui.Session(config)
    session.config.load(session)
    session.main = True
    ui = session.ui = SilentInteraction(config)

    mailpile.util.TESTING = True
    config.sys.http_port = random.randint(33500, 34000)
    #   config.sys.debug = 'log'

    mp = mailpile.Mailpile(session=session)
    session.config.plugins.load('demos')
    mp.set('prefs.index_encrypted=true')

    # Add some mail, scan it.
    # Create local mailboxes
    session.config.open_local_mailbox(session)
    for t in TAGS:
        AddTag(session, arg=[t]).run(save=False)
        session.config.get_tag(t).update(TAGS[t])

    mp.add(test_data)
    mp.rescan('mailboxes')
    mp.profiles_add(MY_FROM, '=', MY_NAME)

    return mp, session, config, ui
Пример #3
0
 def _prepare_new_vcard(self, vcard):
     session, handle = self.session, vcard.nickname
     return (AddTag(session, arg=[handle]).run() and Filter(
         session,
         arg=['add',
              'group:%s' % handle,
              '+%s' % handle, vcard.fn]).run())
Пример #4
0
    def _create_tag(self,
                    tag_name_or_id,
                    use_existing=True,
                    unique=False,
                    parent=None):  # -> tag ID
        if tag_name_or_id in self.session.config.tags:
            # Short circuit if this is a tag ID for an existing tag
            return tag_name_or_id

        tags = self.session.config.get_tags(tag_name_or_id)
        if tags and unique:
            raise ValueError('Tag name is not unique!')
        elif len(tags) == 1 and use_existing:
            tag_id = tags[0]._key
        elif len(tags) > 1:
            raise ValueError('Tag name matches multiple tags!')
        else:
            from mailpile.plugins.tags import AddTag
            AddTag(self.session, arg=[tag_name_or_id]).run(save=False)
            tags = self.session.config.get_tags(tag_name_or_id)
            if tags:
                if parent:
                    tags[0].parent = parent
                tag_id = tags[0]._key
            else:
                raise ValueError('Failed to create tag?')
        return tag_id
Пример #5
0
    def command(self):
        session = self.session

        # Create local mailboxes
        session.config.open_local_mailbox(session)

        # Create standard tags and filters
        created = []
        for t in ('New', 'Inbox', 'Spam', 'Drafts', 'Blank', 'Sent', 'Trash'):
            if not session.config.get_tag_id(t):
                AddTag(session, arg=[t]).run()
                created.append(t)
        session.config.get_tag('New').update({
            'type': 'unread',
            'label': False,
            'display': 'invisible'
        })
        session.config.get_tag('Blank').update({
            'type': 'drafts',
            'flag_editable': True,
            'display': 'invisible'
        })
        session.config.get_tag('Drafts').update({
            'type': 'drafts',
            'flag_editable': True,
            'display': 'priority',
            'display_order': 1,
        })
        session.config.get_tag('Inbox').update({
            'display': 'priority',
            'display_order': 2,
        })
        session.config.get_tag('Sent').update({
            'display': 'priority',
            'display_order': 3,
        })
        session.config.get_tag('Spam').update({
            'type': 'spam',
            'flag_hides': True,
            'display': 'priority',
            'display_order': 4,
        })
        session.config.get_tag('Trash').update({
            'type': 'trash',
            'flag_hides': True,
            'display': 'priority',
            'display_order': 5,
        })
        if 'New' in created:
            Filter(session, arg=['new', '+Inbox', '+New',
                                 'New Mail filter']).run()
            Filter(session, arg=['read', '-New', 'Read Mail filter']).run()

        for old in ('invisible_tags', 'writable_tags'):
            if old in session.config.sys:
                del session.config.sys[old]

        session.config.save()
        return True
Пример #6
0
def _initialize_mailpile_for_testing(MP, test_data):
    # Add some mail, scan it.
    MP._config.sys.http_port = random.randint(40000, 45000)
    session = MP._session
    # Create local mailboxes
    session.config.open_local_mailbox(session)
    for t in TAGS:
        AddTag(MP._session, arg=[t]).run(save=False)
        session.config.get_tag(t).update(TAGS[t])
    Filter(session, arg=['new', '@incoming', '+Inbox', '+New', 'Incoming mail filter']).run(save=False)
    #    MP.setup()
    MP.add(test_data)
    MP.rescan()
Пример #7
0
 def _create_tag(self, tag_name_or_id, unique=False):  # -> tag ID
     tags = self.session.config.get_tags(tag_name_or_id)
     if tags and unique:
         raise ValueError('Tag name is not unique!')
     elif len(tags) == 1:
         tag_id = tags[0]._key
     elif len(tags) > 1:
         raise ValueError('Tag name matches multiple tags!')
     else:
         from mailpile.plugins.tags import AddTag
         AddTag(self.session, arg=[tag_name_or_id]).run(save=False)
         tags = self.session.config.get_tags(tag_name_or_id)
         tag_id = tags[0]._key
     return tag_id
Пример #8
0
    def _create_tag(self,
                    tag_name_or_id,
                    use_existing=True,
                    unique=False,
                    label=True,
                    icon=None,
                    parent=None):  # -> tag ID
        if tag_name_or_id in self.session.config.tags:
            # Short circuit if this is a tag ID for an existing tag
            return tag_name_or_id
        else:
            tag_name = tag_name_or_id

        tags = self.session.config.get_tags(tag_name)
        if tags and unique:
            raise ValueError('Tag name is not unique!')
        elif len(tags) == 1 and use_existing:
            tag_id = tags[0]._key
        elif len(tags) > 1:
            raise ValueError('Tag name matches multiple tags!')
        else:
            from mailpile.plugins.tags import AddTag, Slugify
            bogus_name = 'New-Tag-%s' % len(str(self.session.config))
            AddTag(self.session, arg=[bogus_name]).run(save=False)
            tags = self.session.config.get_tags(bogus_name)
            if tags:
                if self.my_config.name:
                    tags[0].slug = Slugify(
                        '/'.join([self.my_config.name, tag_name]),
                        self.session.config.tags)
                else:
                    tags[0].slug = Slugify(tag_name, self.session.config.tags)
                tags[0].name = tag_name
                tags[0].label = label
                if icon:
                    tags[0].icon = icon
                if parent:
                    tags[0].parent = parent
                tag_id = tags[0]._key
            else:
                raise ValueError('Failed to create tag?')
        return tag_id
Пример #9
0
    def command(self):
        session = self.session

        if session.config.sys.lockdown:
            return self._error(_('In lockdown, doing nothing.'))

        # Perform any required migrations
        Migrate(session).run(before_setup=True, after_setup=False)

        # Create local mailboxes
        session.config.open_local_mailbox(session)

        # Create standard tags and filters
        created = []
        for t in self.TAGS:
            if not session.config.get_tag_id(t):
                AddTag(session, arg=[t]).run(save=False)
                created.append(t)
            session.config.get_tag(t).update(self.TAGS[t])
        for stype, statuses in (('sig', SignatureInfo.STATUSES),
                                ('enc', EncryptionInfo.STATUSES)):
            for status in statuses:
                tagname = 'mp_%s-%s' % (stype, status)
                if not session.config.get_tag_id(tagname):
                    AddTag(session, arg=[tagname]).run(save=False)
                    created.append(tagname)
                session.config.get_tag(tagname).update({
                    'type': 'attribute',
                    'display': 'invisible',
                    'label': False,
                })

        if 'New' in created:
            session.ui.notify(_('Created default tags'))

        # Import all the basic plugins
        for plugin in PLUGINS:
            if plugin not in session.config.sys.plugins:
                session.config.sys.plugins.append(plugin)
        try:
            # If spambayes is not installed, this will fail
            import mailpile.plugins.autotag_sb
            if 'autotag_sb' not in session.config.sys.plugins:
                session.config.sys.plugins.append('autotag_sb')
                session.ui.notify(_('Enabling spambayes autotagger'))
        except ImportError:
            session.ui.warning(
                _('Please install spambayes '
                  'for super awesome spam filtering'))
        session.config.save()
        session.config.load(session)

        vcard_importers = session.config.prefs.vcard.importers
        if not vcard_importers.gravatar:
            vcard_importers.gravatar.append({'active': True})
            session.ui.notify(_('Enabling gravatar image importer'))

        gpg_home = os.path.expanduser('~/.gnupg')
        if os.path.exists(gpg_home) and not vcard_importers.gpg:
            vcard_importers.gpg.append({'active': True, 'gpg_home': gpg_home})
            session.ui.notify(_('Importing contacts from GPG keyring'))

        if ('autotag_sb' in session.config.sys.plugins
                and len(session.config.prefs.autotag) == 0):
            session.config.prefs.autotag.append({
                'match_tag': 'spam',
                'unsure_tag': 'maybespam',
                'tagger': 'spambayes',
                'trainer': 'spambayes'
            })
            session.config.prefs.autotag[0].exclude_tags[0] = 'ham'

        # Assumption: If you already have secret keys, you want to
        #             use the associated addresses for your e-mail.
        #             If you don't already have secret keys, you should have
        #             one made for you, if GnuPG is available.
        #             If GnuPG is not available, you should be warned.
        gnupg = GnuPG()
        accepted_keys = []
        if gnupg.is_available():
            keys = gnupg.list_secret_keys()
            for key, details in keys.iteritems():
                # Ignore revoked/expired keys.
                if ("revocation-date" in details and details["revocation-date"]
                        <= date.today().strftime("%Y-%m-%d")):
                    continue

                accepted_keys.append(key)
                for uid in details["uids"]:
                    if "email" not in uid or uid["email"] == "":
                        continue

                    if uid["email"] in [
                            x["email"] for x in session.config.profiles
                    ]:
                        # Don't set up the same e-mail address twice.
                        continue

                    # FIXME: Add route discovery mechanism.
                    profile = {
                        "email": uid["email"],
                        "name": uid["name"],
                    }
                    session.config.profiles.append(profile)
                if (not session.config.prefs.gpg_recipient
                        and details["capabilities_map"][0]["encrypt"]):
                    session.config.prefs.gpg_recipient = key
                    session.ui.notify(_('Encrypting config to %s') % key)
                if session.config.prefs.crypto_policy == 'none':
                    session.config.prefs.crypto_policy = 'openpgp-sign'

            if len(accepted_keys) == 0:
                # FIXME: Start background process generating a key once a user
                #        has supplied a name and e-mail address.
                pass

        else:
            session.ui.warning(_('Oh no, PGP/GPG support is unavailable!'))

        if (session.config.prefs.gpg_recipient
                and not (self._idx() and self._idx().INDEX)
                and not session.config.prefs.obfuscate_index):
            randcrap = sha512b64(
                open('/dev/urandom').read(1024),
                session.config.prefs.gpg_recipient, '%s' % time.time())
            session.config.prefs.obfuscate_index = randcrap
            session.config.prefs.index_encrypted = True
            session.ui.notify(
                _('Obfuscating search index and enabling '
                  'indexing of encrypted e-mail. '))

        # Perform any required migrations
        Migrate(session).run(before_setup=False, after_setup=True)

        session.config.save()
        return self._success(_('Performed initial Mailpile setup'))
Пример #10
0
    def basic_app_config(self, session,
                         save_and_update_workers=True,
                         want_daemons=True):
        # Create local mailboxes
        session.config.open_local_mailbox(session)

        # Create standard tags and filters
        created = []
        for t in self.TAGS:
            if not session.config.get_tag_id(t):
                AddTag(session, arg=[t]).run(save=False)
                created.append(t)
            session.config.get_tag(t).update(self.TAGS[t])
        for stype, statuses in (('sig', SignatureInfo.STATUSES),
                                ('enc', EncryptionInfo.STATUSES)):
            for status in statuses:
                tagname = 'mp_%s-%s' % (stype, status)
                if not session.config.get_tag_id(tagname):
                    AddTag(session, arg=[tagname]).run(save=False)
                    created.append(tagname)
                session.config.get_tag(tagname).update({
                    'type': 'attribute',
                    'display': 'invisible',
                    'label': False,
                })

        if 'New' in created:
            session.ui.notify(_('Created default tags'))

        # Import all the basic plugins
        reload_config = False
        for plugin in PLUGINS:
            if plugin not in session.config.sys.plugins:
                session.config.sys.plugins.append(plugin)
                reload_config = True
        for plugin in session.config.plugins.WANTED:
            if plugin in session.config.plugins.available():
                session.config.sys.plugins.append(plugin)
        if reload_config:
            with session.config._lock:
                session.config.save()
                session.config.load(session)

        try:
            # If spambayes is not installed, this will fail
            import mailpile.plugins.autotag_sb
            if 'autotag_sb' not in session.config.sys.plugins:
                session.config.sys.plugins.append('autotag_sb')
                session.ui.notify(_('Enabling spambayes autotagger'))
        except ImportError:
            session.ui.warning(_('Please install spambayes '
                                 'for super awesome spam filtering'))

        vcard_importers = session.config.prefs.vcard.importers
        if not vcard_importers.gravatar:
            vcard_importers.gravatar.append({'active': True})
            session.ui.notify(_('Enabling gravatar image importer'))

        gpg_home = os.path.expanduser('~/.gnupg')
        if os.path.exists(gpg_home) and not vcard_importers.gpg:
            vcard_importers.gpg.append({'active': True,
                                        'gpg_home': gpg_home})
            session.ui.notify(_('Importing contacts from GPG keyring'))

        if ('autotag_sb' in session.config.sys.plugins and
                len(session.config.prefs.autotag) == 0):
            session.config.prefs.autotag.append({
                'match_tag': 'spam',
                'unsure_tag': 'maybespam',
                'tagger': 'spambayes',
                'trainer': 'spambayes'
            })
            session.config.prefs.autotag[0].exclude_tags[0] = 'ham'

        if save_and_update_workers:
            session.config.save()
            session.config.prepare_workers(session, daemons=want_daemons)
Пример #11
0
    def command(self):
        session = self.session

        # Create local mailboxes
        session.config.open_local_mailbox(session)

        # Create standard tags and filters
        created = []
        for t in ('New', 'Inbox', 'Spam', 'Drafts', 'Blank', 'Sent', 'Trash'):
            if not session.config.get_tag_id(t):
                AddTag(session, arg=[t]).run()
                created.append(t)
        session.config.get_tag('New').update({
            'type': 'unread',
            'label': False,
            'display': 'invisible'
        })
        session.config.get_tag('Blank').update({
            'type': 'drafts',
            'flag_editable': True,
            'display': 'invisible'
        })
        session.config.get_tag('Drafts').update({
            'type': 'drafts',
            'flag_editable': True,
            'display': 'priority',
            'display_order': 1,
        })
        session.config.get_tag('Inbox').update({
            'display': 'priority',
            'display_order': 2,
        })
        session.config.get_tag('Sent').update({
            'display': 'priority',
            'display_order': 3,
        })
        session.config.get_tag('Spam').update({
            'type': 'spam',
            'flag_hides': True,
            'display': 'priority',
            'display_order': 4,
        })
        session.config.get_tag('Trash').update({
            'type': 'trash',
            'flag_hides': True,
            'display': 'priority',
            'display_order': 5,
        })
        if 'New' in created:
            Filter(session,
                   arg=['new', '+Inbox', '+New', 'New Mail filter']).run()
            Filter(session,
                   arg=['read', '-New', 'Read Mail filter']).run()

        for old in ('invisible_tags', 'writable_tags'):
            if old in  session.config.sys:
                del session.config.sys[old]

        vcard_importers = session.config.prefs.vcard.importers

        if not vcard_importers.gravatar:
            vcard_importers.gravatar.append({'active': True})

        gpg_home = os.path.expanduser('~/.gnupg')
        if os.path.exists(gpg_home) and not vcard_importers.gpg:
            vcard_importers.gpg.append({'gpg_home': gpg_home})

        session.config.save()
        return True
Пример #12
0
    def basic_app_config(self, session,
                         save_and_update_workers=True,
                         want_daemons=True):
        # Create local mailboxes
        session.config.open_local_mailbox(session)

        # Create standard tags and filters
        created = []
        for t, tag_settings in self.TAGS.iteritems():
            tag_settings = copy.copy(tag_settings)

            tid = session.config.get_tag_id(t.replace(' ', '-'))
            if not tid:
                AddTag(session, arg=[t]).run(save=False)
                tid = session.config.get_tag_id(t)
                created.append(t)
            if not tid:
                session.ui.notify(_('Failed to create tag: %s') % t)
                continue

            tag_info = session.config.tags[tid]

            # Delete any old filters...
            old_fids = [f for f, v in session.config.filters.iteritems()
                        if v.primary_tag == tid]
            if old_fids:
                session.config.filter_delete(*old_fids)

            # Create new ones?
            tag_filters = tag_settings.get('_filters', [])
            for search in tag_filters:
                session.config.filters.append({
                    'type': 'system',
                    'terms': search,
                    'tags': '+%s' % tid,
                    'primary_tag': tid,
                    'comment': t
                })
            if tag_filters:
                del tag_settings['_filters']
            for k in ('magic_terms', 'search_terms', 'search_order'):
                if k in tag_info:
                    del tag_info[k]
            tag_info.update(tag_settings)

        for stype, statuses in (('sig', SignatureInfo.STATUSES),
                                ('enc', EncryptionInfo.STATUSES)):
            for status in statuses:
                tagname = 'mp_%s-%s' % (stype, status)
                if not session.config.get_tag_id(tagname):
                    AddTag(session, arg=[tagname]).run(save=False)
                    created.append(tagname)
                session.config.get_tag(tagname).update({
                    'type': 'attribute',
                    'flag_msg_only': True,
                    'display': 'invisible',
                    'label': False,
                })

        if 'New' in created:
            session.ui.notify(_('Created default tags'))

        # Import all the basic plugins
        reload_config = False
        for plugin in PLUGINS:
            if plugin not in session.config.sys.plugins:
                session.config.sys.plugins.append(plugin)
                reload_config = True
        for plugin in session.config.plugins.WANTED:
            if plugin in session.config.plugins.available():
                session.config.sys.plugins.append(plugin)
        if reload_config:
            with session.config._lock:
                session.config.save()
                session.config.load(session)

        try:
            # If spambayes is not installed, this will fail
            import mailpile.plugins.autotag_sb
            if 'autotag_sb' not in session.config.sys.plugins:
                session.config.sys.plugins.append('autotag_sb')
                session.ui.notify(_('Enabling SpamBayes autotagger'))
        except ImportError:
            session.ui.warning(_('Please install SpamBayes '
                                 'for super awesome spam filtering'))

        vcard_importers = session.config.prefs.vcard.importers
        if not vcard_importers.gravatar:
            vcard_importers.gravatar.append({'active': True})
            session.ui.notify(_('Enabling Gravatar image importer'))
        if not vcard_importers.libravatar:
            vcard_importers.libravatar.append({'active': True})
            session.ui.notify(_('Enabling Libravatar image importer'))

        gpg_home = os.path.expanduser('~/.gnupg')
        if os.path.exists(gpg_home) and not vcard_importers.gpg:
            vcard_importers.gpg.append({'active': True,
                                        'gpg_home': gpg_home})
            session.ui.notify(_('Importing contacts from GPG keyring'))

        if ('autotag_sb' in session.config.sys.plugins and
                len(session.config.prefs.autotag) == 0):
            session.config.prefs.autotag.append({
                'match_tag': 'spam',
                'unsure_tag': 'maybespam',
                'tagger': 'spambayes',
                'trainer': 'spambayes'
            })
            session.config.prefs.autotag[0].exclude_tags[0] = 'ham'

        # Mark config as up-to-date
        session.config.version = APPVER

        if save_and_update_workers:
            session.config.save()
            session.config.prepare_workers(session, daemons=want_daemons)

        # Enable Tor in the background, if we have it...
        session.config.slow_worker.add_unique_task(
            session, 'tor-autoconfig', lambda: SetupTor.autoconfig(session))