def command(self, save=True): session, config = self.session, self.session.config args = list(self.args) flags = [] while args and args[0] in ('add', 'set', 'new', 'read', 'notag'): flags.append(args.pop(0)) if args and args[0] and args[0][0] == '=': filter_id = args.pop(0)[1:] else: filter_id = None if args and args[0] and args[0][0] == '@': filter_type = args.pop(0)[1:] else: filter_type = FILTER_TYPES[0] auto_tag = False if 'read' in flags: terms = ['@read'] elif 'new' in flags: terms = ['*'] elif args[0] and args[0][0] not in ('-', '+'): terms = [] while args and args[0][0] not in ('-', '+'): terms.append(args.pop(0)) else: terms = session.searched auto_tag = True if not terms or (len(args) < 1): raise UsageError('Need flags and search terms or a hook') tags, tids = [], [] while args and args[0][0] in ('-', '+'): tag = args.pop(0) tags.append(tag) tids.append(tag[0] + config.get_tag_id(tag[1:])) if not args: args = ['Filter for %s' % ' '.join(tags)] if auto_tag and 'notag' not in flags: if not Tag(session, arg=tags + ['all']).run(save=False): raise UsageError() filter_dict = { 'comment': ' '.join(args), 'terms': ' '.join(terms), 'tags': ' '.join(tids), 'type': filter_type } if filter_id: config.filters[filter_id] = filter_dict else: config.filters.append(filter_dict) self.finish(save=save) return True
def command(self, save=True): session, config = self.session, self.session.config args = list(self.args) flags = [] while args and args[0] in ('add', 'set', 'new', 'read', 'notag'): flags.append(args.pop(0)) if args and args[0] and args[0][0] == '=': filter_id = args.pop(0)[1:] else: filter_id = None if args and args[0] and args[0][0] == '@': filter_type = args.pop(0)[1:] else: filter_type = FILTER_TYPES[0] auto_tag = False if 'read' in flags: terms = ['@read'] elif 'new' in flags: terms = ['*'] elif args[0] and args[0][0] not in ('-', '+'): terms = [] while args and args[0][0] not in ('-', '+'): terms.append(args.pop(0)) else: terms = session.searched auto_tag = True if not terms or (len(args) < 1): raise UsageError('Need flags and search terms or a hook') tags, tids = [], [] while args and args[0][0] in ('-', '+'): tag = args.pop(0) tags.append(tag) tids.append(tag[0] + config.get_tag_id(tag[1:])) if not args: args = ['Filter for %s' % ' '.join(tags)] if auto_tag and 'notag' not in flags: if not Tag(session, arg=tags + ['all']).run(save=False): raise UsageError() filter_dict = { 'comment': ' '.join(args), 'terms': ' '.join(terms), 'tags': ' '.join(tids), 'type': filter_type } if filter_id: config.filters[filter_id] = filter_dict else: config.filters.append(filter_dict) self.finish(save=save) return True
def command(self): args, session, config = self.args, self.session, self.session.config flags = [] while args and args[0] in ('add', 'set', 'new', 'read', 'notag'): flags.append(args.pop(0)) if args and args[0] and args[0][0] == '=': filter_id = args.pop(0)[1:] else: filter_id = config.nid('filter') auto_tag = False if 'read' in flags: terms = ['@read'] elif 'new' in flags: terms = ['*'] elif args[0] and args[0][0] not in ('-', '+'): terms = [] while args and args[0][0] not in ('-', '+'): terms.append(args.pop(0)) else: terms = session.searched auto_tag = True if not terms or (len(args) < 1): raise UsageError('Need flags and search terms or a hook') tags, tids = [], [] while args and args[0][0] in ('-', '+'): tag = args.pop(0) tags.append(tag) tids.append(tag[0]+config.get_tag_id(tag[1:])) if not args: args = ['Filter for %s' % ' '.join(tags)] if auto_tag and 'notag' not in flags: if not Tag(session, arg=tags + ['all']).run(save=False): raise UsageError() if (config.parse_set(session, ('filter:%s=%s' ) % (filter_id, ' '.join(args))) and config.parse_set(session, ('filter_tags:%s=%s' ) % (filter_id, ' '.join(tids))) and config.parse_set(session, ('filter_terms:%s=%s' ) % (filter_id, ' '.join(terms)))): def save_filter(): config.save() if config.index: config.index.save_changes() return True self._serialize('Save filter', save_filter) return True else: raise Exception('That failed, not sure why?!')
def command(self): config = self.session.config existing = [v.lower() for v in config.get('tag', {}).values()] creating = (self.args or []) + self.data.get('name', []) for tag in creating: if ' ' in tag: return self._error('Invalid tag: %s' % tag) if tag.lower() in existing: return self._error('Tag already exists: %s' % tag) result = [] for tag in sorted(creating): if config.parse_set(self.session, 'tag:%s=%s' % (config.nid('tag'), tag)): result.append({'name': tag, 'tid': config.get_tag_id(tag), 'new': 0}) if result: self._background('Save config', lambda: config.save()) return {'added': result}
def command(self): session, config = self.session, self.session.config existing = [v.lower() for v in config.get('tag', {}).values()] clean_session = mailpile.ui.Session(config) clean_session.ui = session.ui result = [] for tag in self.args: tag_id = config.get_tag_id(tag) if tag_id: # FIXME: Update filters too if (Search(clean_session, arg=['tag:%s' % tag]).run() and Tag(clean_session, arg=['-%s' % tag, 'all']).run() and config.parse_unset(session, 'tag:%s' % tag_id)): result.append({'name': tag, 'tid': tag_id}) else: raise Exception('That failed, not sure why?!') else: self._error('No such tag %s' % tag) if result: config.save() return {'removed': result}
def command(self, save=True): session, config = self.session, self.session.config args = list(self.args) flags = [] while args and args[0] in ('add', 'set', 'new', 'read', 'notag', 'maketag'): flags.append(args.pop(0)) if self._truthy('create-tag'): flags.append('maketag') if args and args[0][:1] == '=': filter_id = args.pop(0)[1:] else: filter_id = self.data.get('replace', [None])[0] or None if args and args[0][:1] == '@': filter_type = args.pop(0)[1:] else: filter_type = FILTER_TYPES[0] # Convert HTTP variable tag ops... for tag in self.data.get('add-tag', []): args.append('+%s' % tag) for tag in self.data.get('del-tag', []): args.append('-%s' % tag) if self._truthy('mark-read'): args.append('-new') if self._truthy('skip-inbox'): args.append('-inbox') if self._truthy('never-spam'): args.append('-spam') auto_tag = False if 'read' in flags: terms = ['@read'] elif 'new' in flags: terms = ['*'] elif self.data.get('terms', [''])[0]: terms = self.data['terms'][0].strip().split() auto_tag = True elif args and args[0][:1] not in ('-', '+'): terms = [] while args and args[0][0] not in ('-', '+'): terms.append(args.pop(0)) else: terms = session.searched auto_tag = True tag_ops = [] while args and args[0][0] in ('-', '+'): tag_ops.append(args.pop(0)) comment = self.data.get('comment', [None])[0] or ' '.join(args) if filter_id: primary_tag = config.filters[filter_id].primary_tag or None else: primary_tag = None if primary_tag is None and 'maketag' in flags: if not comment: raise UsageError(_('Need tag name')) result = AddTag(session, arg=[comment]).run(save=False).result primary_tag = result['added'][0]['tid'] if not terms or (len(tag_ops) < 1): raise UsageError(_('Need flags and search terms or a hook')) tags, tids = [], [] for tag in tag_ops: if tag[1:] == '!PRIMARY': tid = primary_tag tag = tag[0] + tid else: tid = config.get_tag_id(tag[1:]) if tid is not None: tags.append(tag) tids.append(tag[0] + tid) else: raise UsageError(_('No such tag: %s') % tag) if not args: args = ['Filter for %s' % ' '.join(tags)] if auto_tag and 'notag' not in flags: if not Tag(session, arg=tags + ['all']).run(save=False): raise UsageError() filter_dict = { 'primary_tag': primary_tag, 'comment': comment, 'terms': ' '.join(terms), 'tags': ' '.join(tids), 'type': filter_type } if filter_id: config.filters[filter_id] = filter_dict else: filter_id = config.filters.append(filter_dict) if 'maketag' in flags and primary_tag and primary_tag in config.tags: tag_icon = self.data.get('tag-icon', [None])[0] tag_color = self.data.get('tag-color', [None])[0] if tag_icon: config.tags[primary_tag].icon = tag_icon if tag_color: config.tags[primary_tag].label_color = tag_color config.tags[primary_tag].name = comment config.tags[primary_tag].slug = 'saved-search-%s' % filter_id self.finish(save=save) filter_dict['id'] = filter_id return self._success(_('Added new filter'), result=filter_dict)
def command(self, save=True): session, config = self.session, self.session.config args = list(self.args) flags = [] while args and args[0] in ('add', 'set', 'new', 'read', 'notag', 'maketag'): flags.append(args.pop(0)) if self._truthy('create-tag'): flags.append('maketag') if args and args[0][:1] == '=': filter_id = args.pop(0)[1:] else: filter_id = self.data.get('replace', [None])[0] or None if args and args[0][:1] == '@': filter_type = args.pop(0)[1:] else: filter_type = FILTER_TYPES[0] # Convert HTTP variable tag ops... for tag in self.data.get('add-tag', []): args.append('+%s' % tag) for tag in self.data.get('del-tag', []): args.append('-%s' % tag) if self._truthy('mark-read'): args.append('-new') if self._truthy('skip-inbox'): args.append('-inbox') if self._truthy('never-spam'): args.append('-spam') auto_tag = False if 'read' in flags: terms = ['@read'] elif 'new' in flags: terms = ['*'] elif self.data.get('terms', [''])[0]: terms = self.data['terms'][0].strip().split() auto_tag = True elif args and args[0][:1] not in ('-', '+'): terms = [] while args and args[0][0] not in ('-', '+'): terms.append(args.pop(0)) else: terms = session.searched auto_tag = True tag_ops = [] while args and args[0][0] in ('-', '+'): tag_ops.append(args.pop(0)) comment = self.data.get('comment', [None])[0] or ' '.join(args) if filter_id: primary_tag = config.filters[filter_id].primary_tag or None else: primary_tag = None if primary_tag is None and 'maketag' in flags: if not comment: raise UsageError(_('Need tag name')) result = AddTag(session, arg=[comment]).run(save=False).result primary_tag = result['added'][0]['tid'] if not terms or (len(tag_ops) < 1): raise UsageError(_('Need flags and search terms or a hook')) tags, tids = [], [] for tag in tag_ops: if tag[1:] == '!PRIMARY': tid = primary_tag tag = tag[0] + tid else: tid = config.get_tag_id(tag[1:]) if tid is not None: tags.append(tag) tids.append(tag[0] + tid) else: raise UsageError(_('No such tag: %s') % tag) if not args: args = ['Filter for %s' % ' '.join(tags)] if auto_tag and 'notag' not in flags: if not Tag(session, arg=tags + ['all']).run(save=False): raise UsageError() filter_dict = { 'primary_tag': primary_tag, 'comment': comment, 'terms': ' '.join(terms), 'tags': ' '.join(tids), 'type': filter_type } if filter_id: config.filters[filter_id] = filter_dict else: filter_id = config.filters.append(filter_dict) if 'maketag' in flags and primary_tag and primary_tag in config.tags: tag_icon = self.data.get('tag-icon', [None])[0] tag_color = self.data.get('tag-color', [None])[0] if tag_icon: config.tags[primary_tag].icon = tag_icon if tag_color: config.tags[primary_tag].label_color = tag_color config.tags[primary_tag].name = comment config.tags[primary_tag].slug = 'saved-search-%s' % filter_id self.finish(save=save) filter_dict['id'] = filter_id return self._success(_('Added new filter'), result=filter_dict)
def command(self, save=True): session, config = self.session, self.session.config args = list(self.args) flags = [] while args and args[0] in ("add", "set", "new", "read", "notag", "maketag"): flags.append(args.pop(0)) if self._truthy("create-tag"): flags.append("maketag") if args and args[0][:1] == "=": filter_id = args.pop(0)[1:] else: filter_id = self.data.get("replace", [None])[0] or None if args and args[0][:1] == "@": filter_type = args.pop(0)[1:] else: filter_type = FILTER_TYPES[0] # Convert HTTP variable tag ops... for tag in self.data.get("add-tag", []): args.append("+%s" % tag) for tag in self.data.get("del-tag", []): args.append("-%s" % tag) if self._truthy("mark-read"): args.append("-new") if self._truthy("skip-inbox"): args.append("-inbox") if self._truthy("never-spam"): args.append("-spam") auto_tag = False if "read" in flags: terms = ["@read"] elif "new" in flags: terms = ["*"] elif self.data.get("terms", [""])[0]: terms = self.data["terms"][0].strip().split() auto_tag = True elif args and args[0][:1] not in ("-", "+"): terms = [] while args and args[0][0] not in ("-", "+"): terms.append(args.pop(0)) else: terms = session.searched auto_tag = True tag_ops = [] while args and args[0][0] in ("-", "+"): tag_ops.append(args.pop(0)) comment = self.data.get("comment", [None])[0] or " ".join(args) if filter_id: primary_tag = config.filters[filter_id].primary_tag or None else: primary_tag = None if primary_tag is None and "maketag" in flags: if not comment: raise UsageError(_("Need tag name")) result = AddTag(session, arg=[comment]).run(save=False).result primary_tag = result["added"][0]["tid"] if not terms or (len(tag_ops) < 1): raise UsageError(_("Need flags and search terms or a hook")) tags, tids = [], [] for tag in tag_ops: if tag[1:] == "!PRIMARY": tid = primary_tag tag = tag[0] + tid else: tid = config.get_tag_id(tag[1:]) if tid is not None: tags.append(tag) tids.append(tag[0] + tid) else: raise UsageError(_("No such tag: %s") % tag) if not args: args = ["Filter for %s" % " ".join(tags)] if auto_tag and "notag" not in flags: if not Tag(session, arg=tags + ["all"]).run(save=False): raise UsageError() filter_dict = { "primary_tag": primary_tag, "comment": comment, "terms": " ".join(terms), "tags": " ".join(tids), "type": filter_type, } if filter_id: config.filters[filter_id] = filter_dict else: filter_id = config.filters.append(filter_dict) if "maketag" in flags and primary_tag and primary_tag in config.tags: tag_icon = self.data.get("tag-icon", [None])[0] tag_color = self.data.get("tag-color", [None])[0] if tag_icon: config.tags[primary_tag].icon = tag_icon if tag_color: config.tags[primary_tag].label_color = tag_color config.tags[primary_tag].name = comment config.tags[primary_tag].slug = "saved-search-%s" % filter_id self.finish(save=save) filter_dict["id"] = filter_id return self._success(_("Added new filter"), result=filter_dict)