Maildir.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #
  2. # Copyright (C) 2023, Jaidyn Levesque <jadedctrl@posteo.at>
  3. #
  4. # This program is free software: you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License as
  6. # published by the Free Software Foundation, either version 3 of
  7. # the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. #
  17. import os
  18. import re
  19. from sys import argv
  20. import urllib
  21. import email.parser
  22. from dateutil import parser
  23. import mimetypes
  24. from gi.repository import Caja, GObject, Gtk, GdkPixbuf
  25. class Maildir(GObject.GObject,
  26. Caja.ColumnProvider,
  27. Caja.InfoProvider):
  28. # Pass our columns to Caja
  29. def get_columns(self):
  30. return (
  31. Caja.Column(
  32. name="Maildir::subject_column",
  33. attribute="subject",
  34. label="Subject",
  35. description=""
  36. ),
  37. Caja.Column(
  38. name="Maildir::sent_column",
  39. attribute="sent",
  40. label="Sent",
  41. description=""
  42. ),
  43. Caja.Column(
  44. name="Maildir::from_column",
  45. attribute="from",
  46. label="From",
  47. description=""
  48. ),
  49. Caja.Column(
  50. name="Maildir::from_addr_column",
  51. attribute="from_addr",
  52. label="From (Addr)",
  53. description=""
  54. ),
  55. Caja.Column(
  56. name="Maildir::from_name_column",
  57. attribute="from_name",
  58. label="From (Name)",
  59. description=""
  60. ),
  61. Caja.Column(
  62. name="Maildir::to_column",
  63. attribute="to",
  64. label="To",
  65. description=""
  66. ),
  67. Caja.Column(
  68. name="Maildir::to_addr_column",
  69. attribute="to_addr",
  70. label="To (Addr)",
  71. description=""
  72. ),
  73. Caja.Column(
  74. name="Maildir::to_name_column",
  75. attribute="to_name",
  76. label="To (Name)",
  77. description=""
  78. )
  79. )
  80. # Implants e-mail-related attributes into a file's columns
  81. def update_file_info(self, file):
  82. if file.get_uri_scheme() == 'file' and not file.is_mime_type("message/rfc822"):
  83. return
  84. filepath = file.get_location().get_path()
  85. message = email.message_from_file(open(filepath))
  86. sender = message.get("From")
  87. to = message.get("To")
  88. subject = message.get("Subject")
  89. if to:
  90. file.add_string_attribute('to', to)
  91. file.add_string_attribute('to_addr', self.from_header_addr(to))
  92. file.add_string_attribute('to_name', self.from_header_name(to))
  93. if sender:
  94. file.add_string_attribute('from', sender)
  95. file.add_string_attribute('from_addr', self.from_header_addr(sender))
  96. file.add_string_attribute('from_name', self.from_header_name(sender))
  97. if subject:
  98. file.add_string_attribute('subject', message.get("Subject"))
  99. try:
  100. date = parser.parse(message.get("Date"))
  101. if date:
  102. file.add_string_attribute('sent', date.strftime("%Y-%m-%d %H:%M:%S %z"))
  103. except:
  104. print("The date couldn't be parsed — that's alright, we didn't need it anyway.")
  105. # Helper function, for parsing e-mail addresses of to/from headers
  106. def from_header_addr(self, str):
  107. if re.search(r'<', str):
  108. return re.sub('[<|>]', '',
  109. re.search(r'<.*@.*>', str).group(0))
  110. return str
  111. # Helper function, for parsing names of to/from headers
  112. def from_header_name(self, str):
  113. if re.search(r'<', str):
  114. return re.sub('<.*', '', str)
  115. return ''