Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # Wikemail.py - import your emails into dokuwiki to create
  4. # a searchable, wiki-able archive of you mail - Duncan Gough 23/10/04
  5. # Ronald van Raaij 2019-03-20:
  6. # Updated libraries and code to python3
  7. # Added commandline arguments for setting username, password, servername, outputdirectory
  8. # Used BeatifulSoup to extract html from mail. Todo: add threading/waiting
  9. # dependencies:
  10. # pip3 install imap_tools
  11. # pip3 install python-dateutil
  12. # pip3 install beautifulsoup4
  13. # pip3 install lxml
  14. # for chown to work, the script needs to be run with sudo
  15.  
  16.  
  17. import sys, re, email, os, logging, getopt, mmap
  18. import email.utils
  19. from imap_tools import MailBox
  20. from dateutil import parser
  21. from bs4 import BeautifulSoup, Doctype
  22.  
  23. #from TaskThread import TaskThread
  24. #import threading
  25. def main(argv):
  26. user=''
  27. password=''
  28. server=''
  29. outputdir=''
  30. logFile = 'import_mail.log'
  31. loglevel = 2
  32. index_file='berichten.txt'
  33. filepermissions = 0o775
  34. owner_group = (1,4)
  35. logging.basicConfig( filename = logFile,filemode = 'w',level = logging.DEBUG,format = '%(asctime)s - %(levelname)s: %(message)s',\
  36. datefmt = '%m/%d/%Y %I:%M:%S %p' )
  37. logging.debug("Starting of the import")
  38.  
  39. try:
  40. opts, args = getopt.getopt(argv,"hu:p:s:o:l",["user=","pass=","server=","outputdir=",'loglevel='])
  41. except getopt.GetoptError:
  42. print( 'test.py -u <user> -p <password> -s <server> -o <outputdir>')
  43. sys.exit(2)
  44. for opt, arg in opts:
  45. if opt == '-h':
  46. print ('test.py -i <user> -p <password> -s <server> -o <outputdir>')
  47. sys.exit()
  48. elif opt in ("-u", "--user"):
  49. user = arg
  50. elif opt in ("-p", "--pass"):
  51. password = arg
  52. elif opt in ("-s", "--server"):
  53. server = arg
  54. elif opt in ("-o", "--outputdir"):
  55. outputdir = arg
  56. if loglevel==4:
  57. logging.debug('server name is "' + server)
  58. logging.debug('user name is "' + user)
  59. logging.debug('password is "' + password)
  60. logging.debug('Output dir is "' + outputdir)
  61. #assume wiki starts in ../docuwiki/data/pages to determine namespace start
  62. if outputdir[-1:]=='/':
  63. namespace=outputdir.rpartition('pages/')[2]
  64. else:
  65. outputdir=outputdir+"/"
  66. namespace=outputdir.rpartition('pages/')[2]
  67. namespace=namespace[:-1].lower()
  68. namespace=namespace.replace('/', ":")
  69. logging.debug('namespace='+ namespace)
  70. try:
  71. mailbox = MailBox(server, ssl=False)
  72. mailbox.login(user, password )
  73. logging.debug('Logged in')
  74. except Exception as e:
  75. logging.debug(repr(e))
  76. print('unable to log in or other imap access error: ' + (repr(e)))
  77. sys.exit(2)
  78. for msg in mailbox.fetch('(UNDELETED)'):
  79. logging.debug(msg.date)
  80. dateasdate=parser.parse(msg.date)
  81. wiki_date = dateasdate.strftime('%Y%m%d')
  82. wiki_date = str(wiki_date)
  83. wiki_from = msg.from_.replace( '@', '_at_' )
  84. wiki_to = ', '.join(msg.to)
  85.  
  86. wiki_filename = msg.uid + msg.subject.lower()
  87. wiki_filename = wiki_filename.replace( 're: ', '' )
  88. wiki_filename = wiki_filename.replace( 'fwd: ', '' )
  89. wiki_filename = wiki_filename.replace( ' ', '_' )
  90. wiki_filename = wiki_filename.replace( '/', '_' )
  91. wiki_filename = wiki_filename.replace( ',', '_' )
  92. soup=BeautifulSoup(msg.html, "lxml")
  93. for e in soup.contents:
  94. if isinstance(e, Doctype):
  95. e.extract()
  96. break
  97. email_body = str(soup)
  98. email_body = email_body.replace("<?xml version=?1.0? encoding=?UTF-8??>", "" )
  99. message = ''
  100. message += '===== ' + msg.subject.upper() + '=====\n\n'
  101. message += 'From: ' + msg.from_ + ')\n\n'
  102. message += 'To: ' + wiki_to + '\n\n'
  103. message += 'Date: ' + wiki_date + '\n\n'
  104. message += email_body + "\n\n"
  105. link_to_message='[['+ namespace + ':' + wiki_date.replace( ' ', '_' ) + ':' + wiki_filename + '|' + wiki_date + '-'+ msg.subject + ']]\n\n'
  106. outputdir_with_date=outputdir + wiki_date.replace( ' ', '_' )
  107. try:
  108. os.stat(outputdir_with_date )
  109. except:
  110. os.makedirs(outputdir_with_date )
  111.  
  112. f = open(outputdir_with_date + '/' + wiki_filename + '.txt', 'a' )
  113. f.write(message)
  114. f.close
  115. os.chmod(outputdir_with_date, filepermissions)
  116. os.chown(outputdir_with_date, owner_group[0], owner_group[1])
  117. # make a link in the main page/textfile
  118. date_heading='=== ' + wiki_date + ' ===' +'\n\n'
  119. with open(outputdir + index_file, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
  120. if s.find(date_heading.encode()) == -1:
  121. f = open(outputdir + index_file, 'a+' )
  122. f.write(date_heading )
  123. f = open(outputdir + index_file, 'a+' )
  124. f.write(link_to_message)
  125. f.close
  126.  
  127.  
  128.  
  129. mailbox.logout()
  130.  
  131. if __name__ == "__main__":
  132. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement