diff --git a/guile/examples/mu-contacts-find b/guile/examples/mu-contacts-find new file mode 100755 index 00000000..42c0f286 --- /dev/null +++ b/guile/examples/mu-contacts-find @@ -0,0 +1,65 @@ +#!/bin/sh +exec guile -e main -s $0 $@ +!# + +;; +;; Copyright (C) 2011 Dirk-Jan C. Binnema +;; +;; This program is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by the +;; Free Software Foundation; either version 3, or (at your option) any +;; later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; + +;; You should have received a copy of the GNU General Public License +;; along with this program; if not, write to the Free Software Foundation, +;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +(use-modules (ice-9 getopt-long)) +(use-modules (mu) (mu contacts)) + +(define (sort-by-freq) #f) +(define (sort-by-newness) #f) + + +(define (main args) + (let* ((optionspec '( (muhome (value #t)) + (sort-by (value #t)) + (revert (value #f)) + (limit (value #t)) + (help (single-char #\h) (value #f)))) + (options (getopt-long args optionspec)) + (msg (string-append + "usage: mu-contacts-export [--help] [--muhome=] " + "--format= " + "--sort-by= [--revert] [--limit=]\n")) + (help (option-ref options 'help #f)) + (sort-by (or (option-ref options 'sort-by #f) "freq")) + (revert (option-ref options 'revert #f)) + (format (or (option-ref options 'format #f) "plain")) + (limit (option-ref options 'limit #f))) + (if help + (begin + (display msg) + (exit 0)) + (begin + (if (option-ref options 'muhome #f) + (mu:init (option-ref options 'muhome)) + (mu:init)) + (let* ((sort-func + (cond + ((string= sort-by "freq") 'sort-by-freq) + ((string= sort-by "newness") 'sort-by-newness) + (else (begin (display msg) (exit 1))))) + (contacts (mu:contacts:list))) + (display "%S" contacts)))))) + +;; Local Variables: +;; mode: scheme +;; End: diff --git a/guile/mu/Makefile.am b/guile/mu/Makefile.am index 42c3b0e0..6f04e1b4 100644 --- a/guile/mu/Makefile.am +++ b/guile/mu/Makefile.am @@ -22,7 +22,8 @@ module_DATA= \ msg.scm \ log.scm \ store.scm \ - stats.scm + stats.scm \ + contacts.scm EXTRA_DIST= \ README diff --git a/guile/mu/contacts.scm b/guile/mu/contacts.scm new file mode 100644 index 00000000..8eea59a3 --- /dev/null +++ b/guile/mu/contacts.scm @@ -0,0 +1,113 @@ +;; +;; Copyright (C) 2011 Dirk-Jan C. Binnema +;; +;; This program is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by the +;; Free Software Foundation; either version 3, or (at your option) any +;; later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; + +;; You should have received a copy of the GNU General Public License +;; along with this program; if not, write to the Free Software Foundation, +;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +;; some guile/scheme functions to get various statistics of my mu +;; message store. + +(use-modules (ice-9 optargs) (ice-9 popen)) + +(define-module (mu contacts) + :use-module (mu log) + :use-module (mu store) + :use-module (mu msg) + :use-module (ice-9 format) + :use-module (srfi srfi-1) + :export + (mu:contacts:list + mu:contacts:export)) + +(define (mu:contacts:hash) + "Create a hash of all the contacts (name . email) in the store. Each entry looks like + email-address => #( )." + (let ((contacts-hash (make-hash-table 2048))) ;; the contacts hash + (mu:store:for-each + (lambda (msg) + (for-each + (lambda (contact) + (let* ((tstamp (mu:msg:date msg)) + ;; the contact we just found + (name (car contact)) + (email (cdr contact)) + ;; the contact found in the hash + (entry (hash-ref contacts-hash email)) + (hash-name (and entry (vector-ref entry 0))) + (hash-freq (and entry (vector-ref entry 1))) + (hash-tstamp (and entry (vector-ref entry 2))) + ;; we don't use last-seen yet + (last-seen (if (and hash-tstamp (> hash-tstamp tstamp)) + hash-tstamp + tstamp))) + (if (not entry) + (hash-set! contacts-hash email (vector name 1 tstamp)) + ;; we replace the name field if either: + ;; 1) the timestamp is newer and the name is non-empty, or + ;; 2) the current name is empty + (if (and (> tstamp hash-tstamp) name (> (string-length name) 0)) + (hash-set! contacts-hash email (vector name (1+ hash-freq) tstamp)) + ;; otherwise, only update the freq, and possibly the last-seen + (hash-set! contacts-hash email + (vector hash-name (1+ hash-freq) hash-tstamp)))))) + (append (mu:msg:to msg) (mu:msg:from msg) (mu:msg:cc msg) (mu:msg:bcc msg)))) + "") + contacts-hash)) + +(define* (mu:contacts:list #:optional (sortfunc #f)) + "Get an unsorted list of contacts (each of which is a contact-vector +#( ). If SORTFUNC is provided, sort the +list using SORT-FUNC. SORT-FUNC takes as arguments two contact-vectors +and returns #t if the first one is smaller than the second one." + (let* ((lst (hash-map->list + (lambda (email vec) + (vector email + (vector-ref vec 0) + (vector-ref vec 1) + (vector-ref vec 2))) + (mu:contacts:hash))) + (lst (if (not sortfunc) + lst + (sort lst sortfunc)))) + lst)) + +(define (mu:contacts:convert contact format) + "Convert a contact vector CONTACT into FORMAT, where format is a +symbol, either 'org-contact, 'mutt-alias, 'bbdb, 'wl, or 'plain." + (let* ( (email (vector-ref contact 0)) + (name (or (vector-ref contact 1) email)) + (freq (vector-ref contact 2)) + (tstamp (vector-ref contact 3)) + (nick (email))) ;; FIXME + (case + ('mutt-alias + (format #f "alias ~a ~a <~a>\n" nick name email)) + ('org-contact + (format #f "* ~a\n:PROPERTIES:\n:EMAIL:~a\n:NICK:~a\n:END:\n\n" + name nick email)) + ('wl ;; wanderlust + (format #f "~a \"~a\" \"~a\"\n" email nick name)) + ('plain + (format #f "~a <~a>\n" name email)) + (else (error "unsupported format ~s" format))))) + +(define* (mu:contacts:export format #:optional (sortfunc #f) (maxnum #f)) + "Write contacts to standard output, optionally sorted with SORTFUNC and optionally only the first MAXNUM entries." + (let* ((clist (mu:contacts:list sortfunc)) + (clist (if maxnum (take clist maxnum) clist))) + (for-each + (lambda (contact) + (mu:contacts:convert contact format)) + clist)))