From fcdfb62d0f9ca9773da57e20286b8623c2c866f5 Mon Sep 17 00:00:00 2001 From: mms Date: Mon, 25 Nov 2024 00:11:05 +0100 Subject: feat: mailig list rule --- bin/chotto | 4 ++++ filters/mailing_lists_filter.rb | 21 ++++++++++++++++++++ lib/chotto.rb | 11 ++++++----- spec/filters/mailing_list_filter_spec.rb | 34 ++++++++++++++++++++++++++++++++ todo.txt | 2 +- 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 filters/mailing_lists_filter.rb create mode 100644 spec/filters/mailing_list_filter_spec.rb diff --git a/bin/chotto b/bin/chotto index 1998af5..0d6116f 100755 --- a/bin/chotto +++ b/bin/chotto @@ -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/" 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 ')) + .to eq('list-header.nisto.com') + } + + it { + expect(subject.list_id_from_header('')) + .to eq('commonspace-users.list-id.within.com') + } + + it { + expect(subject + .list_id_from_header("\"Lena's Personal Joke List\" +")) + .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('')) + .to eq('da39efc25c530ad145d41b86f7420c3b.052000.localhost') + } +end diff --git a/todo.txt b/todo.txt index 1d3fdfe..6abd1c1 100644 --- a/todo.txt +++ b/todo.txt @@ -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 -- cgit v1.2.3