Skip to content

Instantly share code, notes, and snippets.

@aeris
Last active Nov 4, 2017
Embed
What would you like to do?
rspamc spam learn with gpgit encrypted email
#!/usr/bin/env ruby
# License AGPLv3 (https://www.gnu.org/licenses/agpl-3.0.txt)
IMAPS_SERVER = 'xxx'
IMAPS_USER = 'xxx'
RSPAMC_HOST = 'xxx'
AKONADI_ACCOUNT = 'xxx'
KWALLET_APP = 'gpgit-rspamc-learn'
require 'awesome_print'
require 'net/imap'
require 'openssl'
require 'dbus'
require 'mail'
require 'mail/gpg'
require 'tempfile'
require 'open3'
bus = DBus.session_bus
akonadi = bus.service 'org.freedesktop.Akonadi'
akonadi = akonadi.object '/ResourceManager'
akonadi = akonadi['org.freedesktop.Akonadi.ResourceManager']
resources = akonadi.resourceInstances.first.select { |r| r =~ /_imap_/ or r =~ /_pop3_/ }
resource = resources.find do |resource|
resource = bus.service "org.freedesktop.Akonadi.Agent.#{resource}"
resource = resource.object '/'
resource = resource['org.freedesktop.Akonadi.Resource']
resource.name.first == AKONADI_ACCOUNT
end
kwallet = bus.service 'org.kde.kwalletd5'
kwallet = kwallet.object '/modules/kwalletd5'
kwallet = kwallet['org.kde.KWallet']
wallet = kwallet.localWallet.first
handle = kwallet.open(wallet, 0, KWALLET_APP).first
begin
password = kwallet.readPassword(handle, 'imap', "#{resource}rc", KWALLET_APP).first
ensure
kwallet.disconnectApplication wallet, KWALLET_APP
end
Mail.defaults do
retriever_method :imap, {
address: IMAPS_SERVER,
port: 'imaps',
user_name: IMAPS_USER,
password: password,
enable_ssl: true
}
end
mails = Mail.all mailbox: 'Spam', keys: %w[NOT HEADER X-Spam Yes]
mails.each do |mail|
puts "from: #{mail.from}, subject: #{mail.subject}"
mail = mail.decrypt if mail.encrypted?
o, _ = Open3.capture2e 'ssh', RSPAMC_HOST, 'rspamc', 'learn_spam', stdin_data: mail
puts o
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment