Something like "cfind" for individual messages?

14 views
Skip to first unread message

Hippo Man

unread,
Nov 27, 2016, 6:23:39 PM11/27/16
to mu-discuss
I'd like to extract all of the email addresses from a given set of messages.

The "cfind" manages all the addresses for every email within the entire mu instance, but I want to simply get a list of addresses for any one single email or selected group thereof.

I know I can do this via regex matches within emacs, but before I go down that road, I'm wondering if something might already have been written to extract email addresses from individual messages.

Thanks in advance.
.



Hippo Man

unread,
Nov 29, 2016, 1:09:14 PM11/29/16
to mu-discuss
I managed to cobble something together which gives me what I want.

I wrote a python script which takes a list file names on the command llne. It creates a temporary directory with a "cur" subdirectory, and it copies each of the those files into that "cur" subdirectory. It then it runs "mu index" and "mu cfind" with "--muhome" set to that temp directory. This produeces a suitable cfind-based list of addresses for that group of message files.

I invoke this script from emacs using the values of the :path attribute of the message or messages from which I want the addresses, and I capture its output in another emacs buffer.

It's a hack job, but it works.

Here's the python script, in case anyone is interested ...

#!/usr/bin/python

import re
import os
import sys
import csv
import shutil
import getopt
import StringIO
import subprocess

from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS

from email.Parser import Parser as EmailParser

tmpdir  
= '/opt/tmp/md{:05}'.format(os.getpid())
curdir  
= os.path.join(tmpdir, 'cur')
mu      
= '/usr/local/bin/mu'
cformat
= 'csv'
index  
= [ mu, 'index', '--nocolor', '--muhome={}'.format(tmpdir), '--log-stderr', '--maildir={}'.format(tmpdir) ]
cfind  
= [ mu, 'cfind', '--nocolor', '--muhome={}'.format(tmpdir), '--log-stderr', '--format={}'.format(cformat) ]

def main():

   
if len(sys.argv) < 2:
        usage
()
     
    rc
= 0
   
   
try:
        os
.makedirs(curdir)
        copied
= False
       
for fname in sys.argv[1:]:
           
try:
                shutil
.copy2(fname, curdir)
                copied
= True
           
except Exception as e:
               
print('!!! unable to copy: {}'.format(fname))
                rc
= 1
               
continue
           
       
if not copied:
           
return rc

        cmd
= index
        p
= subprocess.Popen(cmd,
                             stdin
=subprocess.PIPE,
                             stdout
=subprocess.PIPE,
                             stderr
=subprocess.PIPE)
       
out, err = p.communicate('')
        rc
= p.returncode
       
if rc != 0:
           
raise Exception('rc={}'.format(rc))

        cmd
= cfind
        p
= subprocess.Popen(cmd,
                             stdin
=subprocess.PIPE,
                             stdout
=subprocess.PIPE,
                             stderr
=subprocess.PIPE)
       
out, err = p.communicate('')
        rc
= p.returncode
       
if rc != 0:
           
raise Exception('rc={}'.format(rc))

        rows
= csv.reader(StringIO.StringIO(out))
        addrs
= {}
       
for row in rows:
           
if row[0]:
                row
[0] = r'\"{}\" '.format(row[0])
            addrs
[row[1]] = '{}<{}>'.format(*row)
       
print("'(")
       
for item in addrs.items():
           
print('  ("{}" . "{}")'.format(*item))
       
print(')')

   
except Exception as e:
       
print('!!! failed: {}: {}'.format(e, cmd[0]))
        rc
= 1
   
finally:
       
try:
            shutil
.rmtree(tmpdir)
       
except Exception as e:
           
print('{}'.format(e))

   
return rc

def usage():
   
print('usage: {} file ...'.format(os.path.basename(sys.argv[0])))
   sys
.exit(1)
   
if __name__ == '__main__':
   sys
.exit(main())



Hippo Man

unread,
Nov 29, 2016, 1:41:02 PM11/29/16
to mu-discuss
You can remove the Image, PIL, and email imports in that python program. I started the program by hacking another program which needed those packages, and I forgot to remove them from this current program.

Reply all
Reply to author
Forward
0 new messages