Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import socket, ssl
- import base64
- import sys, getopt
- import re
- ##############################################################
- #
- # Author: Evgeniy Arbatov
- # Date: 31 May 2010
- #
- # Example usage
- # ./sieveManage.py -t "" -u username -a username -p passowrd \
- # -f fileWithSieveScript -s host:port -v
- #
- # Tested with: Cyrus IMAP 2.3.14
- #
- ##############################################################
- username = authname = scriptPath = password = ''
- verbose = enableSSL = False
- server, port = '127.0.0.1:2000'.split(':')
- def usage():
- """Print the udage string for the program """
- print 'sieveshell.py [-t] [-u] [-a] [-p] [-s] [-f] [-v]'
- sys.exit(0)
- # What to look for in case of an error
- errorPattern = re.compile("NO")
- # Read command-line arguments and parse the vars
- try:
- optlist, args = getopt.getopt(sys.argv[1:], ':t:u:a:p:s:f:v')
- print optlist
- except getopt.GetoptError:
- usage()
- for opt in optlist:
- if (opt[0] == "-u"): username = opt[1]
- if (opt[0] == "-a"): authname = opt[1]
- if (opt[0] == "-v"): verbose = True
- if (opt[0] == "-p"): password = opt[1]
- if (opt[0] == "-t"): enableSSL = True
- if (opt[0] == "-f"): scriptPath = opt[1]
- if (opt[0] == "-s"): server, port = opt[1].split(':')
- # Encode username and password in Base64
- user = base64.b64encode(username+'\0'+authname+'\0'+password)
- if (verbose): print "Base64 login credentials: ", user
- # Read the entire file with Sieve script here
- file = open(scriptPath, 'r')
- scriptFile = file.read()
- if (verbose):
- print "Sieve script to upload: \n", scriptFile
- print "Begin connecting to the server now\n"
- # Try connecting to the Sieve server
- try:
- conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- conn.connect((server, int(port)))
- except socket.error, msg:
- conn = None; print server, port, msg; sys.exit(0)
- # Get the inital server greeting
- if (verbose): print "Initial server greeting:"
- print conn.recv(1024),
- # NOTE: for communicating with Cyrus IMAP timsieved, need to use \x0d\x0a and not \n
- if (enableSSL):
- # If SSL is enabled proceed here
- conn.send('STARTTLS\n'); print conn.recv(1024),
- sslConn = ssl.wrap_socket(conn)
- if (verbose): print "=>TLS from now on"
- # Use sieve protcol from here
- sslConn.write('AUTHENTICATE "PLAIN" {' + str(len(user)) + "+}" + '\x0d\x0a'+ user + '\x0d\x0a')
- sslConn.write(user+'\x0d\x0a'); re = sslConn.read()
- if (verbose): print "Sieve AUTH response: ", re,
- if (errorPattern.match(re)): sys.exit(0)
- # Upload and activate the script
- sslConn.write('PUTSCRIPT "sieve" {' + str(len(scriptFile))+ '+}\x0d\x0a')
- sslConn.write(scriptFile + '\x0d\x0a'); re = sslConn.read()
- if (verbose): print "Uploading a script: ", re,
- if (errorPattern.match(re)): sys.exit(0)
- # Set the uploaded script active
- sslConn.write('SETACTIVE "sieve"\x0d\x0a'); re = sslConn.read()
- if (verbose): print "Setting the script active: ", re,
- if (errorPattern.match(re)): sys.exit(0)
- # End the connection
- sslConn.write('LOGOUT\x0d\x0a')
- else:
- # Proceed here with plaintext Sieve here
- conn.send('AUTHENTICATE "PLAIN" {' + str(len(user)) + "+}" + '\x0d\x0a'+ user + '\x0d\x0a'); re = conn.recv(1024)
- if (verbose): print "Sieve AUTH response: ", re,
- if (errorPattern.match(re)): sys.exit(0)
- conn.send('PUTSCRIPT "sieve" {' + str(len(scriptFile))+ '+}\x0d\x0a')
- conn.send(scriptFile + '\x0d\x0a'); re = conn.recv(1024)
- if (verbose): print "Uploading a script: ", re,
- if (errorPattern.match(re)): sys.exit(0)
- conn.send('SETACTIVE "sieve"\x0d\x0a'); re = conn.recv(1024)
- if (verbose): print "Setting the script active: ", re,
- if (errorPattern.match(re)): sys.exit(0)
- conn.send('LOGOUT\x0d\x0a')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement