diff options
author | mms <git@sapka.me> | 2024-11-27 20:50:16 +0100 |
---|---|---|
committer | mms <git@sapka.me> | 2024-11-27 20:50:16 +0100 |
commit | 1354fcfd222a922762999cd6a97dfe5845d4f835 (patch) | |
tree | 44522c5d6eddcd58e98f8507c274a8c1ca351112 | |
parent | 4c911a441e9533b5bb323b26be748abfd4c5d316 (diff) |
-rw-r--r-- | .rubocop_todo.yml | 24 | ||||
-rw-r--r-- | .ruby-version | 1 | ||||
-rw-r--r-- | Rakefile | 8 | ||||
-rw-r--r-- | chotto-0.1.0.gem | bin | 0 -> 4608 bytes | |||
-rw-r--r-- | chotto.gemspec | 17 | ||||
-rw-r--r-- | filters/mailing_lists_filter.rb | 5 | ||||
-rw-r--r-- | filters/spam_filter.rb | 23 | ||||
-rw-r--r-- | lib/chotto.rb | 5 | ||||
-rw-r--r-- | lib/chotto/helpers.rb | 2 | ||||
-rw-r--r-- | lib/chotto/message.rb | 11 | ||||
-rw-r--r-- | spec/chotto_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/helpers_spec.rb | 8 | ||||
-rw-r--r-- | spec/lib/message_spec.rb | 3 | ||||
-rw-r--r-- | todo.txt | 2 |
14 files changed, 93 insertions, 22 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fbaf0c0..e65fe0c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,18 +1,29 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-11-14 21:18:09 UTC using RuboCop version 1.68.0. +# on 2024-11-25 21:56:14 UTC using RuboCop version 1.68.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 7 +# Offense count: 1 +# Configuration parameters: AllowComments, AllowNil. +Lint/SuppressedException: + Exclude: + - 'filters/spam_filter.rb' + +# Offense count: 6 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: - 'spec/**/*' - 'test/**/*' - - 'lib/**/*' + - 'lib/chotto.rb' + - 'lib/chotto/database.rb' + - 'lib/chotto/helpers.rb' + - 'lib/chotto/message.rb' + - 'lib/chotto/message_thread.rb' + - 'lib/chotto/messages.rb' # Offense count: 2 # Configuration parameters: AllowedVariables. @@ -24,3 +35,10 @@ Style/GlobalVars: Style/MissingRespondToMissing: Exclude: - 'lib/chotto/message.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 129 diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..bec3a35 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +system diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..14d4b82 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require "rake" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +desc "Run tests" +task default: :spec + diff --git a/chotto-0.1.0.gem b/chotto-0.1.0.gem Binary files differnew file mode 100644 index 0000000..b60919d --- /dev/null +++ b/chotto-0.1.0.gem diff --git a/chotto.gemspec b/chotto.gemspec new file mode 100644 index 0000000..10fbc9f --- /dev/null +++ b/chotto.gemspec @@ -0,0 +1,17 @@ +Gem::Specification.new do |s| + s.name = 'chotto' + s.version = '0.1.0' + s.licenses = ['BSD-3-Clause'] + s.summary = "chotto-0.0.1" + s.description = "Initial tagging script for Notmuch" + s.authors = ["mms"] + s.email = 'chotto@sapka.me' + s.files = `git ls-files -- lib/*`.split("\n") + s.homepage = 'https://crys.site/projects/chotto' + s.executables << "chotto" + s.metadata = { "source_code_uri" => "https://cgit.crys.site/chotto" } + + s.add_development_dependency "rspec", ">= 2" +end + + diff --git a/filters/mailing_lists_filter.rb b/filters/mailing_lists_filter.rb index feaee0e..ce11018 100644 --- a/filters/mailing_lists_filter.rb +++ b/filters/mailing_lists_filter.rb @@ -6,7 +6,7 @@ module Chotto module MailingListsFilter def self.list_id_from_header(value) id = value.match(/<(.*)>/) - if id + if id id[1]&.gsub(' ', '') else value @@ -18,12 +18,11 @@ end MAILING_LIST_FILTER = lambda do Chotto.rule_set 'mailing_lists' do messages.filter('NOT tag:lists').each do |msg| - unless msg.list_id.empty? + next if msg.list_id.empty? list_id = Chotto::MailingListsFilter.list_id_from_header(msg.list_id) msg.tags = ['unread', 'inbox', 'lists', "lists/#{list_id}"] msg.save! - end end end end diff --git a/filters/spam_filter.rb b/filters/spam_filter.rb new file mode 100644 index 0000000..ef3ba9e --- /dev/null +++ b/filters/spam_filter.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +SPAM_THRESHOLD = 5.0 + +SPAM_FILTER = lambda do + Chotto.rule_set 'spam_filter' do + messages.filter('NOT tag:spam').each do |msg| + if msg.x_spam_score.to_f > SPAM_THRESHOLD + tags = [ + 'spam', + "spam/#{msg.x_spam_score}" + ] + + msg.tags = tags + msg.save! + + end + + # TODO: understand why sometimes Notmuch raises MemoryErrpr + rescue Notmuch::MemoryError + end + end +end diff --git a/lib/chotto.rb b/lib/chotto.rb index 434002c..f6b9cac 100644 --- a/lib/chotto.rb +++ b/lib/chotto.rb @@ -3,16 +3,17 @@ require 'notmuch' require 'pry' +require_relative '../filters/mailing_lists_filter' +require_relative '../filters/spam_filter' require_relative 'chotto/config' require_relative 'chotto/database' require_relative 'chotto/helpers' require_relative 'chotto/message' +require_relative 'chotto/message_thread' require_relative 'chotto/messages' 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 diff --git a/lib/chotto/helpers.rb b/lib/chotto/helpers.rb index 02ee81e..598b207 100644 --- a/lib/chotto/helpers.rb +++ b/lib/chotto/helpers.rb @@ -6,7 +6,7 @@ module Chotto method_name .to_s .split('_') - .map(&:capitalize) + .map(&:downcase) .join('-') end end diff --git a/lib/chotto/message.rb b/lib/chotto/message.rb index cc3a134..82d77a4 100644 --- a/lib/chotto/message.rb +++ b/lib/chotto/message.rb @@ -3,18 +3,25 @@ module Chotto class Message attr_reader :message, :messages, :db - attr_accessor :tags + + # attr_accessor :tags def initialize(msg:, db:) @message = msg @db = db - @tags = @message.tags + # @tags = MessageTags.new(message: message) end def method_missing(method_name, *_args) handle_get_header(Chotto::Helpers.header_name_from_dsl(method_name)) end + def tags + @tags ||= message.tags + end + + attr_writer :tags + def save! message.remove_all_tags tags.each do |tag| diff --git a/spec/chotto_spec.rb b/spec/chotto_spec.rb index 7593a48..019c0e4 100644 --- a/spec/chotto_spec.rb +++ b/spec/chotto_spec.rb @@ -35,8 +35,7 @@ describe Chotto do end it 'sends the rules to Notmuch - scenario 1' do - expect(message_double).to receive(:tags) - expect(message_double).to receive(:header).with('Subject').and_return('Number Six') + expect(message_double).to receive(:header).with('subject').and_return('Number Six') expect(message_double).to receive(:remove_all_tags) expect(message_double).to receive(:add_tag).with('todo') expect(db_instance_double).to receive(:close) @@ -45,8 +44,7 @@ describe Chotto do end it 'sends the rules to Notmuch - scenario 2' do - expect(message_double).to receive(:tags) - expect(message_double).to receive(:header).with('Subject').and_return('Boomer') + expect(message_double).to receive(:header).with('subject').and_return('Boomer') expect(message_double).to receive(:remove_all_tags) expect(message_double).to receive(:add_tag).with('spam') expect(db_instance_double).to receive(:close) diff --git a/spec/lib/helpers_spec.rb b/spec/lib/helpers_spec.rb index 5b841e4..c2f21f3 100644 --- a/spec/lib/helpers_spec.rb +++ b/spec/lib/helpers_spec.rb @@ -6,10 +6,10 @@ require_relative '../../lib/chotto/helpers' RSpec.describe Chotto::Helpers do let(:subject) { Chotto::Helpers } describe '#header_name_from_dsl' do - it { expect(subject.header_name_from_dsl('word')).to eq('Word') } - it { expect(subject.header_name_from_dsl('wOrd')).to eq('Word') } - it { expect(subject.header_name_from_dsl('Word')).to eq('Word') } + it { expect(subject.header_name_from_dsl('word')).to eq('word') } + it { expect(subject.header_name_from_dsl('wOrd')).to eq('word') } + it { expect(subject.header_name_from_dsl('Word')).to eq('word') } - it { expect(subject.header_name_from_dsl('many_words')).to eq('Many-Words') } + it { expect(subject.header_name_from_dsl('many_words')).to eq('many-words') } end end diff --git a/spec/lib/message_spec.rb b/spec/lib/message_spec.rb index 8994216..375b611 100644 --- a/spec/lib/message_spec.rb +++ b/spec/lib/message_spec.rb @@ -12,7 +12,7 @@ RSpec.describe Chotto::Message do let(:subject) { Chotto::Message.new(msg: msg, db: db_double) } it 'reads headers' do - expect(msg).to receive(:header).with('A-Header') + expect(msg).to receive(:header).with('a-header') subject.a_header end @@ -53,7 +53,6 @@ RSpec.describe Chotto::Message do it 'fetches messages for thread' do expect(msg).to receive(:thread_id).and_return(thread_id) expect(db_double).to receive(:search_messages).with("thread:#{thread_id}").and_return([msg_in_thread]) - expect(msg_in_thread).to receive(:tags) expect(tester).to receive(:test).with(an_instance_of(Chotto::Message)) @@ -3,6 +3,6 @@ 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 [X] add default filter: mailing lists -[ ] add default filter: spam +[X] add default filter: spam [x] add "--new" to bin script. Adds default "tag:new" filter [ ] add gemspec |