Advertisement
Guest User

Evgeniy Arbatov

a guest
May 31st, 2010
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import socket, ssl
  4. import base64
  5. import sys, getopt
  6. import re
  7.  
  8. ##############################################################
  9. #
  10. # Author: Evgeniy Arbatov
  11. # Date: 31 May 2010
  12. #
  13. # Example usage
  14. # ./sieveManage.py -t "" -u username -a username -p passowrd \
  15. #  -f fileWithSieveScript -s host:port -v
  16. #
  17. # Tested with: Cyrus IMAP 2.3.14
  18. #
  19. ##############################################################
  20.  
  21. username = authname = scriptPath = password = ''
  22. verbose = enableSSL = False
  23. server, port = '127.0.0.1:2000'.split(':')
  24.  
  25. def usage():
  26.     """Print the udage string for the program """
  27.     print 'sieveshell.py [-t] [-u] [-a] [-p] [-s] [-f] [-v]'
  28.     sys.exit(0)
  29.  
  30. # What to look for in case of an error
  31. errorPattern = re.compile("NO")
  32.  
  33. # Read command-line arguments and parse the vars
  34. try:
  35.     optlist, args = getopt.getopt(sys.argv[1:], ':t:u:a:p:s:f:v')
  36.     print optlist
  37. except getopt.GetoptError:
  38.      usage()
  39.  
  40. for opt in optlist:
  41.     if (opt[0] == "-u"): username = opt[1]
  42.     if (opt[0] == "-a"): authname = opt[1]
  43.     if (opt[0] == "-v"): verbose = True
  44.     if (opt[0] == "-p"): password = opt[1]
  45.     if (opt[0] == "-t"): enableSSL = True
  46.     if (opt[0] == "-f"): scriptPath = opt[1]
  47.     if (opt[0] == "-s"): server, port = opt[1].split(':')
  48.  
  49.  
  50. # Encode username and password in Base64
  51. user = base64.b64encode(username+'\0'+authname+'\0'+password)
  52. if (verbose): print "Base64 login credentials: ", user
  53.  
  54. # Read the entire file with Sieve script here
  55. file = open(scriptPath, 'r')
  56. scriptFile = file.read()
  57. if (verbose):
  58.     print "Sieve script to upload: \n", scriptFile
  59.     print "Begin connecting to the server now\n"
  60.  
  61. # Try connecting to the Sieve server
  62. try:
  63.     conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  64.     conn.connect((server, int(port)))
  65. except socket.error, msg:
  66.     conn = None; print server, port, msg; sys.exit(0)
  67.    
  68.  
  69. # Get the inital server greeting
  70. if (verbose): print "Initial server greeting:"
  71. print conn.recv(1024),
  72.  
  73. # NOTE: for communicating with Cyrus IMAP timsieved, need to use \x0d\x0a and not \n
  74.  
  75. if (enableSSL):
  76.     # If SSL is enabled proceed here
  77.     conn.send('STARTTLS\n'); print conn.recv(1024),
  78.     sslConn = ssl.wrap_socket(conn)
  79.     if (verbose): print "=>TLS from now on"
  80.  
  81.     # Use sieve protcol from here
  82.     sslConn.write('AUTHENTICATE "PLAIN" {' + str(len(user)) + "+}" + '\x0d\x0a'+ user + '\x0d\x0a')
  83.     sslConn.write(user+'\x0d\x0a'); re = sslConn.read()
  84.     if (verbose): print "Sieve AUTH response: ", re,
  85.     if (errorPattern.match(re)): sys.exit(0)
  86.    
  87.     # Upload and activate the script
  88.     sslConn.write('PUTSCRIPT "sieve" {' + str(len(scriptFile))+ '+}\x0d\x0a')
  89.     sslConn.write(scriptFile + '\x0d\x0a'); re = sslConn.read()
  90.     if (verbose): print "Uploading a script: ", re,
  91.     if (errorPattern.match(re)): sys.exit(0)
  92.  
  93.     # Set the uploaded script active
  94.     sslConn.write('SETACTIVE "sieve"\x0d\x0a'); re = sslConn.read()
  95.     if (verbose): print "Setting the script active: ", re,
  96.     if (errorPattern.match(re)): sys.exit(0)
  97.  
  98.     # End the connection
  99.     sslConn.write('LOGOUT\x0d\x0a')
  100.  
  101. else:
  102.     # Proceed here with plaintext Sieve here
  103.     conn.send('AUTHENTICATE "PLAIN" {' + str(len(user)) + "+}" + '\x0d\x0a'+ user + '\x0d\x0a'); re = conn.recv(1024)
  104.     if (verbose): print "Sieve AUTH response: ", re,
  105.     if (errorPattern.match(re)): sys.exit(0)
  106.  
  107.     conn.send('PUTSCRIPT "sieve" {' + str(len(scriptFile))+ '+}\x0d\x0a')
  108.     conn.send(scriptFile + '\x0d\x0a'); re = conn.recv(1024)
  109.     if (verbose): print "Uploading a script: ", re,
  110.     if (errorPattern.match(re)): sys.exit(0)   
  111.  
  112.     conn.send('SETACTIVE "sieve"\x0d\x0a'); re = conn.recv(1024)
  113.     if (verbose): print "Setting the script active: ", re,
  114.     if (errorPattern.match(re)): sys.exit(0)   
  115.  
  116.     conn.send('LOGOUT\x0d\x0a')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement