Gmail.py 4.0 KB

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