diff options
-rwxr-xr-x | bin/chotto | 4 | ||||
-rw-r--r-- | filters/mailing_lists_filter.rb | 21 | ||||
-rw-r--r-- | lib/chotto.rb | 11 | ||||
-rw-r--r-- | spec/filters/mailing_list_filter_spec.rb | 34 | ||||
-rw-r--r-- | todo.txt | 2 |
5 files changed, 66 insertions, 6 deletions
@@ -7,6 +7,10 @@ require_relative '../lib/chotto' CONFIG_FILEPATH = 'chotto/config.rb' XDG_HOME = ENV.fetch('XDG_CONFIG_HOME', "#{Dir.home}/.config") def start_chotto(options) + Chotto.configure do + config.only_new = options.fetch(:only_new, false) + end + config_file = File.read("#{XDG_HOME}/#{CONFIG_FILEPATH}") instance_eval(config_file) eval_rules(options) diff --git a/filters/mailing_lists_filter.rb b/filters/mailing_lists_filter.rb new file mode 100644 index 0000000..872a817 --- /dev/null +++ b/filters/mailing_lists_filter.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Chotto + # Adds "list" and "list/<list-id>" tags to messages from mailing list + # Baed on RFC 2919 + module MailingListsFilter + def self.list_id_from_header(value) + value.match(/<(.*)>/)[1].gsub(' ', '') + end + end +end + +MAILING_LIST_FILTER = lambda do + Chotto.rule_set 'mailing_lists' do + messages.filter('NOT tag:lists').each do |msg| + list_id = Chotto::MailingListsFilter.list_id_from_header(msg.list_id) + msg.tags = ['unread', 'inbox', 'lists', "lists/#{list_id}"] + msg.save! + end + end +end diff --git a/lib/chotto.rb b/lib/chotto.rb index 61f149b..434002c 100644 --- a/lib/chotto.rb +++ b/lib/chotto.rb @@ -12,6 +12,7 @@ require_relative 'chotto/ruleset' require_relative 'chotto/token' require_relative 'chotto/token_group' require_relative 'chotto/message_thread' +require_relative '../filters/mailing_lists_filter' module Chotto class << self @@ -43,14 +44,14 @@ module Chotto def close_db db.close end - end -end -def eval_rules(options = {}) - Chotto.configure do - config.only_new = options.fetch(:only_new, false) + def include_rule(rule) + rule.call + end end +end +def eval_rules(_options = {}) Chotto.rule_sets.each(&:run) Chotto.close_db end diff --git a/spec/filters/mailing_list_filter_spec.rb b/spec/filters/mailing_list_filter_spec.rb new file mode 100644 index 0000000..613a648 --- /dev/null +++ b/spec/filters/mailing_list_filter_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require_relative '../../filters/mailing_lists_filter' + +RSpec.describe Chotto::MailingListsFilter do + let(:subject) { Chotto::MailingListsFilter } + + it { + expect(subject.list_id_from_header('List Header Mailing List <list-header.nisto.com>')) + .to eq('list-header.nisto.com') + } + + it { + expect(subject.list_id_from_header('<commonspace-users.list-id.within.com>')) + .to eq('commonspace-users.list-id.within.com') + } + + it { + expect(subject + .list_id_from_header("\"Lena's Personal Joke List\" <lenas-jokes.da39efc25c530ad145d41b86f7420c3b.021999.localhost> +")) + .to eq('lenas-jokes.da39efc25c530ad145d41b86f7420c3b.021999.localhost') + } + + it { + expect(subject.list_id_from_header('"An internal CMU List" <0Jks9449.list-id.cmu.edu>')) + .to eq('0Jks9449.list-id.cmu.edu') + } + + it { + expect(subject.list_id_from_header('<da39efc25c530ad145d41b86f7420c3b.052000.localhost>')) + .to eq('da39efc25c530ad145d41b86f7420c3b.052000.localhost') + } +end @@ -2,7 +2,7 @@ Task list for 0.1.0 [x] allow for selecting order of messages in filter [x] add helper for searching other messages in the same thread as first -[ ] add default filter: mailing lists +[X] add default filter: mailing lists [ ] add default filter: spam [x] add "--new" to bin script. Adds default "tag:new" filter [ ] add gemspec |