| Current File : //proc/24011/root/proc/3/cwd/usr/local/scripts/queue_remove_emails.py |
#!/usr/bin/env python
import sys
import math
import urllib
import urllib2
import base64
import collections
import optparse
import getpass
import ssl
import time
def remove_messages(server,username,password,msgs,cuser):
print "Running API calls: \n"
for host, ids in msgs.iteritems():
for i in range(0, int(math.ceil(len(ids)/1000.0))):
print "api_delete_queued_message_outgoing&host=%s&message_id=%s&method=remove" % (host, ",".join(ids[i*1000:(i+1)*1000]))
request = urllib2.Request("https://%s/cgi-bin/api?call=api_delete_queued_message_outgoing&host=%s&client_username=%s&message_id=%s&method=remove" % (server,host,cuser, ",".join(ids[i*1000:(i+1)*1000])))
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read()
print result
def main():
parser = optparse.OptionParser()
parser.add_option('-f','--from',dest='sender',help='find and remove messages from the specified sender')
parser.add_option('-t','--to',dest='recipient',help='find and remove messages from the specified recipient')
parser.add_option('-s','--server',dest='server',help='API server hostname')
parser.add_option('-u','--username',dest='username',help='API username')
parser.add_option('-a','--alias',dest='alias',help='Additional username identifier in case multiple people use the same API username')
parser.add_option('-S','--subject',dest='subject',help='Subject of the message')
parser.add_option('-i','--identity',dest='identity',help='Submission identity')
parser.add_option('-o','--outgoingdomain',dest='domain',help='Outgoing domain. Only works for the subject/identity search')
parser.add_option('-d','--days',dest='days',help='Number of days back to look through the queue. Only works for the subject/identity search')
parser.add_option('-p','--password',dest='password',help='password for the API username. Note that if you pass it this way, it may be visible to other people and will remain in your bash history')
(options, args) = parser.parse_args()
if options.server is None or options.username is None:
print "\033[91m\nPlease specify the API server hostname and your API username!\n\033[0m"
parser.print_help()
exit(-1)
if options.sender is None and options.recipient is None and options.subject is None and options.identity is None:
print "\033[91m\nPlease specify a sender,recipient,subject or identity!\n\033[0m"
parser.print_help()
exit(-1)
if (options.subject or options.identity) and options.domain is None:
print "\033[91m\nPlease specify the outgoing domain name!\n\033[0m"
parser.print_help()
exit(-1)
if options.alias is not None:
cuser = options.alias
elif options.username != "internal" and options.alias is None:
cuser= options.username
elif options.alias is None and options.username == "internal":
print "\033[91m\nPlease specify alias username!\n\033[0m"
parser.print_help()
exit(-1)
msgs = collections.defaultdict(list)
sender = options.sender
recipient = options.recipient
server = options.server
username = options.username
if options.password:
password = options.password
else:
password = getpass.getpass('password: ')
todate=int(time.time())
if options.days is None:
days=1
else:
days = int(options.days)
fromdate=todate - days*86400
if options.subject:
request = urllib2.Request("https://%s/cgi-bin/api?call=api_find_outgoing_messages&domain=%s&from_date=%s&to_date=%s&predicate=and&partial=False&status=queued&subject=%s&columns=%s&client_username=%s"\
% (server,options.domain,fromdate,todate,urllib.quote(options.subject),"message_id%2Chost",cuser))
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read().split("\n")
for row in result:
item = row.split(",")
if len(item) > 1:
host, id = item[0], item[1]
msgs[host].append(id.rstrip("\r"))
remove_messages(server,username,password,msgs)
if options.identity:
request = urllib2.Request("https://%s/cgi-bin/api?call=api_find_outgoing_messages&domain=%s&from_date=%s&to_date=%s&predicate=and&partial=False&status=queued&identity=%s&columns=%s&client_username=%s"\
% (server,options.domain,fromdate,todate,urllib.quote(options.identity),"message_id%2Chost",cuser))
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read().split("\n")
for row in result:
item = row.split(",")
if len(item) > 1:
host, id = item[0], item[1]
msgs[host].append(id.rstrip("\r"))
remove_messages(server,username,password,msgs)
if not options.subject and not options.identity:
request = urllib2.Request("https://%s/cgi-bin/api?call=api_get_outgoing_delivery_queue&include_retry_time=False&sort_field=&api_language=en&client_username=%s" % (server,cuser))
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read().split("\n")
for row in result:
item = row.split(",")
if len(item) > 3:
if options.sender is not None and options.recipient is not None:
if sender in item[5] and recipient in item[6]:
host, id = item[1], item[2]
msgs[host].append(id)
elif options.sender is not None:
if sender in item[5]:
host, id = item[1], item[2]
msgs[host].append(id)
elif options.recipient is not None:
if recipient in item[6]:
host, id=item[1], item[2]
msgs[host].append(id)
remove_messages(server,username,password,msgs,cuser)
if __name__ == "__main__":
main()