From f249147024f07960e2482f5a4611f26d2c0184e5 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Thu, 14 Jan 2016 10:48:17 +0100 Subject: [PATCH] smarter reading contacts from archived messages --- sup/hooks/extra-contact-addresses.rb | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 sup/hooks/extra-contact-addresses.rb diff --git a/sup/hooks/extra-contact-addresses.rb b/sup/hooks/extra-contact-addresses.rb new file mode 100755 index 0000000..00efe50 --- /dev/null +++ b/sup/hooks/extra-contact-addresses.rb @@ -0,0 +1,46 @@ +#!/usr/bin/env ruby + +$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) + +require 'xapian' +require 'trollop' +require 'set' + +unless defined? BASE_DIR + BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup") +end + +xapian = Xapian::Database.new File.join(BASE_DIR, 'xapian') +version = xapian.get_metadata 'rescue-version' +version = '0' if version.empty? + +contacts = Hash.new { |key| Set.new } + +case version +when '0' + xapian.postlist('Kmail').each do |x| + entry = Marshal.load(xapian.document(x.docid).data) + ([entry[:from]] + entry[:to] + entry[:cc] + entry[:bcc]).compact.each do |email, name| + next if email.nil? + name = name.strip.chomp("'").chomp('"').reverse + .chomp("'").chomp('"').reverse unless name.nil? + email.downcase! + if name.nil? or email.include? name + contacts[email] = Set.new unless contacts.has_key? email + else + contacts[email] << name + end + end + end +else + abort "this sup-dump version doesn't understand your index" +end + +contacts.map do |email, names| + if names.empty? + email + else + names.map { |name| "#{name} <#{email}>" } + end +end.flatten +