Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. /**
  2. * Copying and distribution of this file, with or without modification,
  3. * are permitted in any medium without royalty provided the copyright
  4. * notice and this notice are preserved. This file is offered as-is,
  5. * without any warranty.
  6. *
  7. * Script to take an Android ADB email dump and process emails to .eml RFC822
  8. * format files. Does not handle attachments/multipart at present.
  9. *
  10. * <em>Notes:</em>
  11. * Configured for a specific account and the Sent items mailbox
  12. * in the 1st query.
  13. *
  14. * @author Robin Bramley (c) 2014
  15. * @author Alan Poulain
  16. */
  17.  
  18.  
  19. import groovy.sql.Sql
  20.  
  21. import javax.mail.*
  22. import javax.mail.internet.*
  23. @Grapes([
  24. @Grab(group = 'org.xerial', module = 'sqlite-jdbc', version = '3.7.2'),
  25. @GrabConfig(systemClassLoader = true)
  26. ])
  27.  
  28. @Grapes(
  29. @Grab(group = 'com.icegreen', module = 'greenmail', version = '1.3')
  30. )
  31. // lazy way of pulling in JavaMail & Glasgow JAF (showing my age?)
  32.  
  33. import java.sql.*
  34.  
  35. // Configurable variables
  36. def mailFrom = 'foo@example.com'
  37. def accountKey = 1
  38. def mailboxKey = 5
  39.  
  40. // Utility to convert addresses into InternetAddress[]
  41. def convertAddresses = { inList ->
  42. def cleanListStr = ""
  43. def i = 0
  44. def dirtyList = inList.tokenize('\u0001') // split on SOH
  45.  
  46. dirtyList.each {
  47. if (it.contains("@")) {
  48. // Set list separator when required
  49. if (i > 0) {
  50. cleanListStr += ','
  51. }
  52.  
  53. // Clear any text, we'll just use the email address
  54. if (it.contains('\u0002')) { // STX
  55. def stxIndex = it.indexOf(2)
  56. it = it.substring(0, stxIndex)
  57. }
  58.  
  59. cleanListStr += it
  60. i++
  61. }
  62. }
  63.  
  64. return addresses = InternetAddress.parse(cleanListStr, false) // strict
  65. }
  66.  
  67. // Connection handles for finally block
  68. def sql, sqlBody
  69.  
  70. try {
  71. def props = new Properties()
  72.  
  73. // Get mail session
  74. def session = Session.getDefaultInstance(props)
  75.  
  76. // Get the DBs
  77. sql = Sql.newInstance('jdbc:sqlite:EmailProvider.db', 'org.sqlite.JDBC')
  78. sqlBody = Sql.newInstance('jdbc:sqlite:EmailProviderBody.db', 'org.sqlite.JDBC')
  79.  
  80. // Get the headers
  81. sql.eachRow("""
  82. SELECT _id as msgKey, timeStamp, subject, messageId, fromList, toList, ccList, bccList, replyToList
  83. FROM Message
  84. WHERE accountKey = ${accountKey} and mailboxKey = ${mailboxKey}
  85. ORDER BY timeStamp ASC
  86. """) { row ->
  87. try {
  88. def msgKey = row.msgKey
  89.  
  90. // Get the message body
  91. def body = sqlBody.firstRow("SELECT htmlContent, textContent, textReply FROM Body WHERE messageKey = ${msgKey}")
  92.  
  93. // Construct the email
  94. def msg = new MimeMessage(session)
  95. msg.setFrom(new InternetAddress(mailFrom))
  96. msg.setSubject(row.subject)
  97. msg.setSentDate(new Date(row.timeStamp))
  98.  
  99. if (row.toList) {
  100. msg.setRecipients(Message.RecipientType.TO, convertAddresses(row.toList))
  101. }
  102. if (row.ccList) {
  103. msg.setRecipients(Message.RecipientType.CC, convertAddresses(row.ccList))
  104. }
  105. if (row.bccList) {
  106. msg.setRecipients(Message.RecipientType.BCC, convertAddresses(row.bccList))
  107. }
  108.  
  109. // Set the HTML content if it exists
  110. if (body.htmlContent) {
  111. def contentType = 'text/html'
  112. msg.setContent(body.htmlContent, contentType)
  113. } else {
  114. msg.setText(body.textContent)
  115. }
  116.  
  117. // Write out the email
  118. new File("${row.timestamp}-${msgKey}.eml").withOutputStream { os ->
  119. msg.writeTo(os)
  120. }
  121. } catch (Exception e) {
  122. println e
  123. e.printStackTrace(System.out)
  124. if(row && row.msgKey) {
  125. println row.msgKey
  126. }
  127. }
  128. }
  129. } catch (Exception e) {
  130. println e
  131. e.printStackTrace(System.out)
  132. } finally {
  133. if (sql) {
  134. sql.close()
  135. }
  136. if (sqlBody) {
  137. sqlBody.close()
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement