Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.71 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import math
  5. import urllib
  6. import urllib2
  7. import base64
  8. import collections
  9. import optparse
  10. import getpass
  11. import ssl
  12. import time
  13.  
  14. def remove_messages(server,username,password,msgs,cuser):
  15.         print "Running API calls: \n"
  16.         for host, ids in msgs.iteritems():
  17.             for i in range(0, int(math.ceil(len(ids)/1000.0))):
  18.                 print "api_delete_queued_message_outgoing&host=%s&message_id=%s&method=remove" % (host, ",".join(ids[i*1000:(i+1)*1000]))
  19.                 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])))
  20.                 base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
  21.                 request.add_header("Authorization", "Basic %s" % base64string)
  22.                 result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read()
  23.                 print result
  24.  
  25. def main():
  26.    
  27.     parser = optparse.OptionParser()
  28.     parser.add_option('-f','--from',dest='sender',help='find and remove messages from the specified sender')
  29.     parser.add_option('-t','--to',dest='recipient',help='find and remove messages from the specified recipient')
  30.     parser.add_option('-s','--server',dest='server',help='API server hostname')
  31.     parser.add_option('-u','--username',dest='username',help='API username')
  32.     parser.add_option('-a','--alias',dest='alias',help='Additional username identifier in case multiple people use the same API username')
  33.     parser.add_option('-S','--subject',dest='subject',help='Subject of the message')
  34.     parser.add_option('-i','--identity',dest='identity',help='Submission identity')
  35.     parser.add_option('-o','--outgoingdomain',dest='domain',help='Outgoing domain. Only works for the subject/identity search')
  36.     parser.add_option('-d','--days',dest='days',help='Number of days back to look through the queue. Only works for the subject/identity search')
  37.     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')
  38.     (options, args) = parser.parse_args()
  39.    
  40.     if options.server is None or options.username is None:
  41.         print "\033[91m\nPlease specify the API server hostname and your API username!\n\033[0m"
  42.         parser.print_help()
  43.         exit(-1)
  44.  
  45.     if options.sender is None and options.recipient is None and options.subject is None and options.identity is None:
  46.         print "\033[91m\nPlease specify a sender,recipient,subject or identity!\n\033[0m"
  47.         parser.print_help()
  48.         exit(-1)
  49.    
  50.     if (options.subject or options.identity) and options.domain is None:
  51.         print "\033[91m\nPlease specify the outgoing domain name!\n\033[0m"
  52.         parser.print_help()
  53.         exit(-1)
  54.    
  55.     if options.alias is not None:
  56.         cuser = options.alias
  57.     elif options.username != "internal" and options.alias is None:
  58.         cuser= options.username
  59.     elif options.alias is None and options.username == "internal":
  60.         print "\033[91m\nPlease specify alias username!\n\033[0m"
  61.         parser.print_help()
  62.         exit(-1)
  63.    
  64.     msgs = collections.defaultdict(list)
  65.     sender = options.sender
  66.     recipient = options.recipient
  67.     server = options.server
  68.     username = options.username
  69.     if options.password:
  70.         password = options.password
  71.     else:
  72.         password = getpass.getpass('password: ')
  73.     todate=int(time.time())
  74.     if options.days is None:
  75.         days=1
  76.     else:
  77.         days = int(options.days)
  78.     fromdate=todate - days*86400
  79.    
  80.     if options.subject:
  81.         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"\
  82.          % (server,options.domain,fromdate,todate,urllib.quote(options.subject),"message_id%2Chost",cuser))
  83.          
  84.         base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
  85.         request.add_header("Authorization", "Basic %s" % base64string)
  86.         result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read().split("\n")
  87.         for row in result:
  88.             item = row.split(",")
  89.             if len(item) > 1:
  90.                 host, id = item[0], item[1]
  91.                 msgs[host].append(id.rstrip("\r"))
  92.         remove_messages(server,username,password,msgs)
  93.    
  94.     if options.identity:
  95.         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"\
  96.          % (server,options.domain,fromdate,todate,urllib.quote(options.identity),"message_id%2Chost",cuser))
  97.          
  98.         base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
  99.         request.add_header("Authorization", "Basic %s" % base64string)
  100.         result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read().split("\n")
  101.         for row in result:
  102.             item = row.split(",")
  103.             if len(item) > 1:
  104.                 host, id = item[0], item[1]
  105.                 msgs[host].append(id.rstrip("\r"))
  106.         remove_messages(server,username,password,msgs)
  107.  
  108.     if not options.subject and not options.identity:
  109.         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))
  110.         base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
  111.         request.add_header("Authorization", "Basic %s" % base64string)
  112.         result = urllib2.urlopen(request,context=ssl._create_unverified_context()).read().split("\n")
  113.         for row in result:
  114.             item = row.split(",")
  115.             if len(item) > 3:
  116.               if options.sender is not None and options.recipient is not None:
  117.                 if sender in item[5] and recipient in item[6]:
  118.                     host, id = item[1], item[2]
  119.                     msgs[host].append(id)
  120.               elif options.sender is not None:
  121.                 if sender in item[5]:
  122.                     host, id = item[1], item[2]
  123.                     msgs[host].append(id)
  124.               elif options.recipient is not None:
  125.                 if recipient in item[6]:
  126.                     host, id=item[1], item[2]
  127.                     msgs[host].append(id)
  128.         remove_messages(server,username,password,msgs,cuser)
  129.  
  130.  
  131. if __name__ == "__main__":
  132.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement