aboutsummaryrefslogtreecommitdiff
path: root/lib/osugiru
diff options
context:
space:
mode:
Diffstat (limited to 'lib/osugiru')
-rw-r--r--lib/osugiru/config.rb7
-rw-r--r--lib/osugiru/database.rb19
-rw-r--r--lib/osugiru/helpers.rb13
-rw-r--r--lib/osugiru/message.rb23
-rw-r--r--lib/osugiru/messages.rb103
-rw-r--r--lib/osugiru/ruleset.rb11
6 files changed, 176 insertions, 0 deletions
diff --git a/lib/osugiru/config.rb b/lib/osugiru/config.rb
new file mode 100644
index 0000000..033d501
--- /dev/null
+++ b/lib/osugiru/config.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module Osugiru
+ class Config
+ attr_accessor :database_path
+ end
+end
diff --git a/lib/osugiru/database.rb b/lib/osugiru/database.rb
new file mode 100644
index 0000000..d014c94
--- /dev/null
+++ b/lib/osugiru/database.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+module Osugiru
+ class Database
+ attr_reader :db
+
+ def initialize(path:)
+ @db = ::Notmuch::Database.new(path)
+ end
+
+ def query(query)
+ db.query(query)
+ end
+
+ def search_messages(query)
+ query(query).search_messages
+ end
+ end
+end
diff --git a/lib/osugiru/helpers.rb b/lib/osugiru/helpers.rb
new file mode 100644
index 0000000..c493bbd
--- /dev/null
+++ b/lib/osugiru/helpers.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module Osugiru
+ module Helpers
+ def self.header_name_from_dsl(method_name)
+ method_name
+ .to_s
+ .split('_')
+ .map(&:capitalize)
+ .join('-')
+ end
+ end
+end
diff --git a/lib/osugiru/message.rb b/lib/osugiru/message.rb
new file mode 100644
index 0000000..3446f17
--- /dev/null
+++ b/lib/osugiru/message.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+module Osugiru
+ class Message
+ attr_reader :message
+
+ def initialize(msg:)
+ @message = msg
+ end
+
+ def method_missing(method_name, *_args)
+ handle_get(Osugiru::Helpers.header_name_from_dsl(method_name))
+ end
+
+ def handle_get(header_name)
+ message.header(header_name) if message.header(header_name)
+ end
+
+ def tags
+ @message.tags
+ end
+ end
+end
diff --git a/lib/osugiru/messages.rb b/lib/osugiru/messages.rb
new file mode 100644
index 0000000..4af19b6
--- /dev/null
+++ b/lib/osugiru/messages.rb
@@ -0,0 +1,103 @@
+# frozen_string_literal: true
+
+require 'pry'
+module Osugiru
+ class Messages
+ AND_CONJUCTION = :and
+ OR_CONJUCTION = :or
+ DEFAULT_CONJUCTION = AND_CONJUCTION
+
+ Token = Struct.new(:conjuction, :field, :value, :messages) do
+ def to_query
+ conjuction_to_use = messages.next_token_id > 1 ? conjuction : ''
+ return "#{conjuction_to_use} (#{value})" if field == :direct
+
+ "#{conjuction_to_use} (#{field}:#{value})"
+ end
+ end
+
+ TokenGroup = Struct.new(:conjuction, :tokens, :messages) do
+ def initialize(*)
+ super
+ end
+
+ def to_query
+ tokens.flat_map(&:to_query).join(' ').to_s
+ end
+
+ def push_token(field, value)
+ if value.is_a? String
+ tokens.push(Token.new(conjuction, field, value, messages))
+ elsif value.is_a? Array
+ group = TokenGroup.new(OR_CONJUCTION, [], messages)
+ value.each do |val|
+ group.push_token(field, val)
+ end
+ tokens.push group
+
+ end
+ end
+
+ def next_token_id
+ @token_count += 1
+ end
+ end
+
+ attr_accessor :query, :current_conjuction, :token_count
+ attr_reader :db
+
+ def initialize(db:)
+ @db = db
+ @query = []
+ @current_conjuction = AND_CONJUCTION
+ @token_count = 0
+ end
+
+ def or
+ @current_conjuction = OR_CONJUCTION
+ self
+ end
+
+ def and
+ @current_conjuction = AND_CONJUCTION
+ self
+ end
+
+ def filter(params)
+ case params
+ when String
+ add_filter_from_string(params)
+ when Hash
+ add_filter_from_hash(params)
+ end
+
+ self
+ end
+
+ def each
+ db.search_messages(query_string).each do |msg|
+ yield(Message.new(msg: msg))
+ end
+ end
+
+ def add_filter_from_string(string)
+ @query << Token.new(current_conjuction, :direct, string, self)
+ end
+
+ def add_filter_from_hash(hash)
+ group = TokenGroup.new(current_conjuction, [], self)
+ hash.each do |key, value|
+ group.push_token(key, value)
+ end
+ @query << group
+ end
+
+ def query_string
+ query.map(&:to_query).join(' ')
+ end
+
+ def next_token_id
+ @token_count += 1
+ end
+ end
+end
diff --git a/lib/osugiru/ruleset.rb b/lib/osugiru/ruleset.rb
new file mode 100644
index 0000000..a9bac16
--- /dev/null
+++ b/lib/osugiru/ruleset.rb
@@ -0,0 +1,11 @@
+module Osugiru
+ RuleSet = Struct.new(:name, :rule) do
+ def messages
+ Messages.new(db: Osugiru.db)
+ end
+
+ def run
+ instance_eval(&rule)
+ end
+ end
+end