Gmail.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """
  2. Gmail IMAP repository support
  3. Copyright (C) 2008-2016 Riccardo Murri <riccardo.murri@gmail.com> &
  4. contributors
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. """
  17. from offlineimap.repository.IMAP import IMAPRepository
  18. from offlineimap import folder, OfflineImapError
  19. class GmailRepository(IMAPRepository):
  20. """Gmail IMAP repository.
  21. This class just has default settings for GMail's IMAP service. So
  22. you can do 'type = Gmail' instead of 'type = IMAP' and skip
  23. specifying the hostname, port etc. See
  24. http://mail.google.com/support/bin/answer.py?answer=78799&topic=12814
  25. for the values we use."""
  26. def __init__(self, reposname, account):
  27. """Initialize a GmailRepository object."""
  28. IMAPRepository.__init__(self, reposname, account)
  29. def gethost(self):
  30. """Return the server name to connect to.
  31. We first check the usual IMAP settings, and then fall back to
  32. imap.gmail.com if nothing is specified."""
  33. try:
  34. return super().gethost()
  35. except OfflineImapError:
  36. # Nothing was configured, cache and return hardcoded
  37. # one. See the parent class (IMAPRepository) for how this
  38. # cache is used.
  39. self._host = "imap.gmail.com"
  40. return self._host
  41. def getoauth2_request_url(self):
  42. """Return the OAuth URL to request tokens from.
  43. We first check the usual OAuth settings, and then fall back to
  44. https://accounts.google.com/o/oauth2/token if nothing is
  45. specified."""
  46. url = super().getoauth2_request_url()
  47. if url is None:
  48. # Nothing was configured, use a hardcoded one.
  49. url = "https://accounts.google.com/o/oauth2/token"
  50. self.setoauth2_request_url(url)
  51. return self.oauth2_request_url
  52. def getport(self):
  53. """Return the port number to connect to.
  54. This Gmail implementation first checks for the usual IMAP settings
  55. and falls back to 993 if nothing is specified."""
  56. port = super().getport()
  57. if port is not None:
  58. return port
  59. return 993
  60. def getssl(self):
  61. ssl = self.getconfboolean('ssl', None)
  62. if ssl is None:
  63. # Nothing was configured, return our default setting for
  64. # GMail. Maybe this should look more similar to gethost &
  65. # we could just rely on the global "ssl = yes" default.
  66. return True
  67. return ssl
  68. def getpreauthtunnel(self):
  69. return None
  70. def getfolder(self, foldername, decode=True):
  71. return self.getfoldertype()(self.imapserver, foldername,
  72. self, decode)
  73. def getfoldertype(self):
  74. return folder.Gmail.GmailFolder
  75. def gettrashfolder(self):
  76. """
  77. Where deleted mail should be moved
  78. """
  79. return self.getconf('trashfolder', '[Gmail]/Trash')
  80. def getspamfolder(self):
  81. """
  82. Depending on the IMAP settings (Settings -> Forwarding and
  83. POP/IMAP -> IMAP Access -> "When I mark a message in IMAP as
  84. deleted") GMail might also deletes messages upon EXPUNGE in
  85. the Spam folder.
  86. """
  87. return self.getconf('spamfolder', '[Gmail]/Spam')