From: Xah Lee
Subject: Re: Help needed to simplify code for customisation
Date: 
Message-ID: <46c6811e-771a-4162-a0c7-967b788793e1@v35g2000pro.googlegroups.com>
On Mar 9, 7:14 pm, Richard Riley <··········@gmail.com> wrote:
> Could someone please recommend the best way to remove the 3 similar lines
> doing string-match on the "account" assign and iterate a variable list to
> which I can "add-to-list" in other .el libraries for example?
>
> ,----
> |  (if (message-mail-p)
> |       (save-excursion
> |       (let* ((from
> |               (save-restriction
> |                 (message-narrow-to-headers)
> |                 (message-fetch-field "from")))
> |              (account
> |               (cond
> |                ((string-match ".*root.*" from)"richardriley")
> |                ((string-match ".*richardriley.*" from)"richardriley")
> |                ((string-match ".*rileyrgdev.*" from)"rileyrgdev")
> |                ))
> |              )
> |         (setq message-sendmail-extra-arguments (list "-a" account))
> |         )))
> |   )
> `----

perhaps something like the following. The code is tested.

(defun canonicalString (from pairs)
  "Returns the canonical string of FROM, determined by the pairs in
PAIRS.

The PAIRS should be a nested vector of the form:
“[[\"a\" \"α\"] [\"b\" \"β\"] [\"γ\" \"g\"] ...]”
where the first element is a regex string to be matched with FROM.
If match, then the second element is returned.

If no match is found, nil is returned.

Example:
 (canonicalString \"b\" [[\"a\" \"α\"] [\"b\" \"β\"] [\"γ\" \"g\"]])
returns \"β\".
"
  (let (totalItems matchFound i result)
    (setq totalItems (length pairs))
    (setq foundMatch nil)
    (setq i 0)
    (while (and (not matchFound)
                (not (= i totalItems)))
      (if (string-match (elt (elt pairs i) 0) from)
          (progn
            (setq result (elt (elt pairs i) 1))
            (setq matchFound t)))
      (setq i (1+ i)))
    result))

; testing
 (canonicalString "b" [["a" "α"] ["b" "β"] ["γ" "g"]])

  Xah
∑ http://xahlee.org/

☄

From: TomSW
Subject: Re: Help needed to simplify code for customisation
Date: 
Message-ID: <b5ecffc4-3770-4c6e-8f1c-38042d65d09e@a39g2000yqc.googlegroups.com>
> On Mar 9, 7:14 pm, Richard Riley <··········@gmail.com> wrote:

> > Could someone please recommend the best way to remove the 3 similar lines
> > doing string-match on the "account" assign and iterate a variable list to
> > which I can "add-to-list" in other .el libraries for example?

(require 'cl)

(defvar my-accounts-alist
  '(("richardriley"  "root" "richardriley")
    ("rileyrgdev"    "rileyrgdev"))
  "Associate email accounts with sender addresses: an alist each item
of
which is a list whose first member is the account name and any
following
members are regular expressions to match against a sender address.")

(defun my-get-account (from)
  (car (find-if (lambda (regexps)
                  (some (lambda (regexp)
                          (string-match regexp from))
                        regexps))
                from my-accounts-alist
                :key 'cdr)))

...
 (if (message-mail-p)
     (save-excursion
       (let* ((account
               (save-restriction
                 (message-narrow-to-headers)
                 (my-get-account (message-fetch-field "from"))))
              ;; use let to set the value of message-sendmail-extra-
arguments
              ;; dynamically
              ;; - this assumes that whatever uses the arguments will
be called
              ;; inside the let. Otherwise use setq.
              (message-sendmail-extra-arguments
               (append (when account (list "-a" account))
                       message-sendmail-extra-arguments)))
         ;; do message stuff
         )))
...

You can then push extra associations to the front of my-accounts-
alist, or even add regular expressions to a particular account, as
long as you know it's already in the list:

(push '("richardriley" "anotherrileyaddr")
      my-accounts-alist)

(push "foo"
      (cdr  (assoc "richardriley" my-accounts-alist)))

Note that the first match wins, so the sorting of the alist may become
significant if your regular expressions are too greedy.

regards,
Tom SW
From: TomSW
Subject: Re: Help needed to simplify code for customisation
Date: 
Message-ID: <0a66c2d9-1293-4d3b-b026-10dea2172a58@t7g2000yqa.googlegroups.com>
On Mar 10, 11:02 am, TomSW <·············@gmail.com> wrote Emacs-
related stuff

... and didn't mean to post it to comp.lang.lisp too - sorry.
From: William James
Subject: Re: Help needed to simplify code for customisation
Date: 
Message-ID: <gp5jn001kva@enews2.newsguy.com>
TomSW wrote:

> (require 'cl)
> 
> (defvar my-accounts-alist
>   '(("richardriley"  "root" "richardriley")
>     ("rileyrgdev"    "rileyrgdev"))
>   "Associate email accounts with sender addresses: an alist each item
> of
> which is a list whose first member is the account name and any
> following
> members are regular expressions to match against a sender address.")
> 
> (defun my-get-account (from)
>   (car (find-if (lambda (regexps)
>                   (some (lambda (regexp)
>                           (string-match regexp from))
>                         regexps))
>                 from my-accounts-alist
>                 :key 'cdr)))

Clojure:

(def my-accounts-map
  { "richardriley"  [#"root" #"richardriley"],
    "rileyrgdev"    [#"rileyrgdev"] } )

(defn my-get-account [from]
  (ffirst
    (filter
      (fn [[acct re-list]] (some #(re-find % from) re-list))
      my-accounts-map)))
From: Marco Antoniotti
Subject: Re: Help needed to simplify code for customisation
Date: 
Message-ID: <d7760ed0-8cb5-42f1-963d-afe0dbeee3f0@h5g2000yqh.googlegroups.com>
On Mar 10, 12:42 pm, "William James" <·········@yahoo.com> wrote:
> TomSW wrote:
> > (require 'cl)
>
> > (defvar my-accounts-alist
> >   '(("richardriley"  "root" "richardriley")
> >     ("rileyrgdev"    "rileyrgdev"))
> >   "Associate email accounts with sender addresses: an alist each item
> > of
> > which is a list whose first member is the account name and any
> > following
> > members are regular expressions to match against a sender address.")
>
> > (defun my-get-account (from)
> >   (car (find-if (lambda (regexps)
> >                   (some (lambda (regexp)
> >                           (string-match regexp from))
> >                         regexps))
> >                 from my-accounts-alist
> >                 :key 'cdr)))
>
> Clojure:
>
> (def my-accounts-map
>   { "richardriley"  [#"root" #"richardriley"],
>     "rileyrgdev"    [#"rileyrgdev"] } )
>
> (defn my-get-account [from]
>   (ffirst
>     (filter
>       (fn [[acct re-list]] (some #(re-find % from) re-list))
>       my-accounts-map)))

Hey.  That is not the homework you are supposed to hand in.

Cheers
--
Marco
From: Kenneth Tilton
Subject: Re: Help needed to simplify code for customisation
Date: 
Message-ID: <49b75c2b$0$22514$607ed4bc@cv.net>
Marco Antoniotti wrote:
> On Mar 10, 12:42 pm, "William James" <·········@yahoo.com> wrote:
>> TomSW wrote:
>>> (require 'cl)
>>> (defvar my-accounts-alist
>>>   '(("richardriley"  "root" "richardriley")
>>>     ("rileyrgdev"    "rileyrgdev"))
>>>   "Associate email accounts with sender addresses: an alist each item
>>> of
>>> which is a list whose first member is the account name and any
>>> following
>>> members are regular expressions to match against a sender address.")
>>> (defun my-get-account (from)
>>>   (car (find-if (lambda (regexps)
>>>                   (some (lambda (regexp)
>>>                           (string-match regexp from))
>>>                         regexps))
>>>                 from my-accounts-alist
>>>                 :key 'cdr)))
>> Clojure:
>>
>> (def my-accounts-map
>>   { "richardriley"  [#"root" #"richardriley"],
>>     "rileyrgdev"    [#"rileyrgdev"] } )
>>
>> (defn my-get-account [from]
>>   (ffirst
>>     (filter
>>       (fn [[acct re-list]] (some #(re-find % from) re-list))
>>       my-accounts-map)))
> 
> Hey.  That is not the homework you are supposed to hand in.


<sigh> What are we going to do with our boy Willy? He never does his 
Ruby assignmens any more and just plays with a Lisp all day. I say no 
television until we seem some work.

kt
From: Richard Riley
Subject: Re: Help needed to simplify code for customisation
Date: 
Message-ID: <gp5m7f$an5$1@rileyrgdev.motzarella.org>
Many thanks for all the replies. Andy Stewart emailed me what I consider
the easiest (for me!) to customise, follow and understand:

,----
| (defvar msmtp-name-list '("root" "richardriley" "rileyrgdev" "riley" "sham"))
| 
| (defun msmtp-change-smtp ()
|   
|  (setq sendmail-program "/usr/bin/msmtp")
|  (setq smtpmail-starttls-credentials '(("smtp.gmail.com" 587 nil nil)))
|  (setq smtpmail-smtp-server "smtp.gmail.com")
|  (setq message-sendmail-envelope-from 'header)
|  
|  (if (message-mail-p)
|       (save-excursion
| 	(let* ((from
| 		(save-restriction
| 		  (message-narrow-to-headers)
| 		  (message-fetch-field "from")))
| 	       (account (catch 'match
| 			  (dolist (element msmtp-name-list)
| 			    (when (string-match (format ".*%s.*" element) from)
| 			      (throw 'match element))))))
| 	  (setq message-sendmail-extra-arguments (list "-a" account))
| 	  ))))
| 
| (add-hook 'message-send-hook 'msmtp-change-smtp)
`----

And adding a new match element and associated posting style for another
msmtp account in a, possibly, readonly, include is as simple as (using
"name" as match):

,----
| (add-to-list 'msmtp-name-list "name" t)
`----

along with the appropriate posting style:

,----
| (add-to-list `gnus-posting-styles `(
| 	 ,(rx(or "groupname"))
| 	 (name "name")
| 	 (from "name <name-email>")
| 	 (face nil)
| 	 (x-face-file nil)
| 	 (eval(setq gnushush-user-agent-header (quote real)))
| 	 (eval (setq pgg-gpg-user-id nil))
| 	 (signature-file nil)) t )
`----


Thanks again to all.

r.


-- 
 important and urgent problems of the technology of today are no longer the satisfactions of the primary needs or of archetypal wishes, but the reparation of the evils and damages by the technology of yesterday.  ~Dennis Gabor, Innovations:  Scientific, Technological and Social, 1970