--- /usr/local/upfront/library/software/zope/products/ZMailIn/zopeMailIn.py	Mon Feb 11 18:56:25 2002
+++ zopeMailIn.py	Fri Jun  7 15:38:00 2002
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python2.1
 
 # This python module will send a mail message to a zope instance that has the relevent
 # ZMailIn client present.
@@ -12,6 +12,7 @@
 
 def GetLocationHTTP(host, emailaddress):
         # Attempt to get the location of the ZMailIn client for the given email address
+
         try:
                 returned = urllib.urlopen(host, urllib.urlencode({'email_address':emailaddress})).read()
         except IOError,e:
@@ -21,68 +22,101 @@
 
 def _make_url(host=zopeMailInConf.host, path=''):
         user_and_pass=''
-        
+
         if zopeMailInConf.username and zopeMailInConf.password:
                 user_and_pass="%s:%s@"%(zopeMailInConf.username,zopeMailInConf.password)
-                
+
         return host[:7] + user_and_pass + host[7:] + path
 
+
 if __name__ == '__main__':
         # This gets called by the MTA when a new message arrives.
         # The mail message file gets passed in on the stdin
-        
+
         f = sys.stdin   # First get a handle on the message file
         messageText = f.read()
-        
+
         url = ''
         if len(sys.argv)>1:
                 url = sys.argv[1]
-        
+
         if not url:
                 # get a url
                 mf = rfc822.Message(StringIO.StringIO(messageText))
-                messageTo = mf.get('envelope-to',mf.get('to',''))
-                if not messageTo:
-                        print "ZMailIn Error: No 'envelope-to' or 'to' header in message"
+                # rcc: this is not very resilient
+                # njj: it'll fail if either of these headers contain
+                # multiple addresses. Use mail objects' 'getaddr' method
+                # instead of list subscription.
+                # messageTo = mf.get('envelope-to',mf.get('to',''))
+                #
+                # Check for recipient first ('envelope-to', then 'to');
+                # then for cc ('cc', then 'bcc'). Then look for a
+                # ZMailIn Client: if not 'recipient_client', look for
+                # 'cc_client'.
+                #
+                name, recipient_client = mf.getaddr('envelope-to')
+                if not recipient_client:
+                    name, recipient_client = mf.getaddr('to')
+                name, cc_client = mf.getaddr('cc')
+                if not cc_client:
+                    name, cc_client = mf.getaddr('bcc')
+
+                if (not recipient_client) and (not cc_client):
+                        print "ZMailIn Error: No 'envelope-to', 'to', 'cc' or 'bcc' header in message"
                         sys.exit(1)
-                        
+
                 host = _make_url(path='getZMailInClientLocation')
 
-                # Try to get the exact email address
-                url = GetLocationHTTP(host, messageTo)
-                
-                if url[:4]=='FAIL':
+
+                for client_address in [recipient_client, cc_client]:
+                    if not client_address: continue
+
+                    # Try to get the exact email address
+                    url = GetLocationHTTP(host, client_address)
+
+                    if url[:4]=='FAIL':
                         print "ZMailIn Error:",url
-                        sys.exit(1)
-                
-                if url=='Not Found':
-                        # We didn't get the exact email address so try for a domain match only
-                        domain = messageTo[string.rfind(messageTo,'@'):]
+                        # sys.exit(1)
+                        url = None
+
+                    elif url=='Not Found':
+                        # We didn't get the exact email address so try for a
+                        # domain match only
+                        domain = client_address[
+                                    string.rfind(client_address,'@'):]
                         url = GetLocationHTTP(host, domain)
-                
-                if url=='Not Found' or '\n' in url:
-                        print "ZMailIn Error: ZMailIn Client does not exist"
-                        sys.exit(1)
-                        
-                url = _make_url(url)
+
+                        if url=='Not Found' or '\n' in url:
+                            print "ZMailIn Error: ZMailIn Client does not exist %s"%client_address
+                            # sys.exit(1)
+                            url = None
+
+                    if url: break # OK, we've found one, stop looking.
+
+                if url:
+                    url = _make_url(url)
+                    print "Found client at %s"%url
+                else:
+                    sys.exit(1)
+
 
         if url:
-                
+
                 url = url + '/postZMailMessage'
                 try:
                         result = urllib.urlopen(url, urllib.urlencode({'file':messageText})).read()
                 except (IOError,EOFError),e:
                         print "ZMailIn Error: Problem Connecting to server",e
                         sys.exit(73)
-                
+
                 # if the ZMailIn Client's method returned anything, then 'something bad' happened.
                 if result:
                         print result
                         sys.exit(1)
-                        
+
                 sys.exit(0)
 
         print "ZMailIn Error: No ZMailIn Client URL found or specified."
         sys.exit(1)
-        
-        
+
+
