2020-11-01 08:43:16 +01:00
|
|
|
"""
|
|
|
|
Gmail IMAP repository support
|
|
|
|
Copyright (C) 2008-2016 Riccardo Murri <riccardo.murri@gmail.com> &
|
|
|
|
contributors
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
"""
|
2008-01-03 04:56:55 +01:00
|
|
|
|
2011-03-03 11:05:15 +01:00
|
|
|
from offlineimap.repository.IMAP import IMAPRepository
|
2011-06-06 13:12:23 +02:00
|
|
|
from offlineimap import folder, OfflineImapError
|
2008-01-03 04:56:55 +01:00
|
|
|
|
2017-10-26 18:23:15 +02:00
|
|
|
|
2008-01-03 04:56:55 +01:00
|
|
|
class GmailRepository(IMAPRepository):
|
|
|
|
"""Gmail IMAP repository.
|
|
|
|
|
2016-11-17 11:45:24 +01:00
|
|
|
This class just has default settings for GMail's IMAP service. So
|
|
|
|
you can do 'type = Gmail' instead of 'type = IMAP' and skip
|
|
|
|
specifying the hostname, port etc. See
|
2011-06-06 13:12:23 +02:00
|
|
|
http://mail.google.com/support/bin/answer.py?answer=78799&topic=12814
|
2016-11-17 11:45:24 +01:00
|
|
|
for the values we use."""
|
2008-01-03 04:56:55 +01:00
|
|
|
def __init__(self, reposname, account):
|
|
|
|
"""Initialize a GmailRepository object."""
|
|
|
|
IMAPRepository.__init__(self, reposname, account)
|
|
|
|
|
|
|
|
def gethost(self):
|
2011-06-06 13:12:23 +02:00
|
|
|
"""Return the server name to connect to.
|
|
|
|
|
2016-11-17 11:45:24 +01:00
|
|
|
We first check the usual IMAP settings, and then fall back to
|
|
|
|
imap.gmail.com if nothing is specified."""
|
2011-06-06 13:12:23 +02:00
|
|
|
try:
|
2020-11-01 09:02:09 +01:00
|
|
|
return super().gethost()
|
2011-06-06 13:12:23 +02:00
|
|
|
except OfflineImapError:
|
2016-11-17 11:45:24 +01:00
|
|
|
# Nothing was configured, cache and return hardcoded
|
|
|
|
# one. See the parent class (IMAPRepository) for how this
|
|
|
|
# cache is used.
|
|
|
|
self._host = "imap.gmail.com"
|
2011-06-06 13:12:23 +02:00
|
|
|
return self._host
|
2008-01-03 04:56:55 +01:00
|
|
|
|
2014-01-09 21:30:41 +01:00
|
|
|
def getoauth2_request_url(self):
|
2016-11-17 11:45:24 +01:00
|
|
|
"""Return the OAuth URL to request tokens from.
|
2014-01-09 21:30:41 +01:00
|
|
|
|
2016-11-17 11:45:24 +01:00
|
|
|
We first check the usual OAuth settings, and then fall back to
|
|
|
|
https://accounts.google.com/o/oauth2/token if nothing is
|
|
|
|
specified."""
|
2015-10-12 07:37:39 +02:00
|
|
|
|
2020-11-01 09:02:09 +01:00
|
|
|
url = super().getoauth2_request_url()
|
2015-10-12 07:37:39 +02:00
|
|
|
if url is None:
|
2020-11-01 10:06:32 +01:00
|
|
|
# Nothing was configured, use a hardcoded one.
|
2020-11-01 10:04:18 +01:00
|
|
|
url = "https://accounts.google.com/o/oauth2/token"
|
2020-11-01 10:06:32 +01:00
|
|
|
|
|
|
|
self.setoauth2_request_url(url)
|
|
|
|
|
2016-07-28 18:35:33 +02:00
|
|
|
return self.oauth2_request_url
|
2014-01-09 21:30:41 +01:00
|
|
|
|
2008-01-03 04:56:55 +01:00
|
|
|
def getport(self):
|
2016-11-17 11:45:24 +01:00
|
|
|
"""Return the port number to connect to.
|
|
|
|
|
|
|
|
This Gmail implementation first checks for the usual IMAP settings
|
|
|
|
and falls back to 993 if nothing is specified."""
|
|
|
|
|
2020-11-01 09:02:09 +01:00
|
|
|
port = super().getport()
|
2016-11-17 11:45:24 +01:00
|
|
|
|
2020-11-01 08:53:08 +01:00
|
|
|
if port is not None:
|
|
|
|
return port
|
2020-11-01 08:51:03 +01:00
|
|
|
|
2020-11-01 08:53:08 +01:00
|
|
|
return 993
|
2008-01-03 04:56:55 +01:00
|
|
|
|
|
|
|
def getssl(self):
|
2016-11-17 11:45:24 +01:00
|
|
|
ssl = self.getconfboolean('ssl', None)
|
|
|
|
|
|
|
|
if ssl is None:
|
|
|
|
# Nothing was configured, return our default setting for
|
|
|
|
# GMail. Maybe this should look more similar to gethost &
|
|
|
|
# we could just rely on the global "ssl = yes" default.
|
|
|
|
return True
|
2020-11-01 08:54:16 +01:00
|
|
|
|
|
|
|
return ssl
|
2008-01-03 04:56:55 +01:00
|
|
|
|
|
|
|
def getpreauthtunnel(self):
|
|
|
|
return None
|
|
|
|
|
2017-10-26 18:23:15 +02:00
|
|
|
def getfolder(self, foldername, decode=True):
|
2008-01-03 04:56:55 +01:00
|
|
|
return self.getfoldertype()(self.imapserver, foldername,
|
2017-10-26 18:23:15 +02:00
|
|
|
self, decode)
|
2008-01-03 04:56:55 +01:00
|
|
|
|
|
|
|
def getfoldertype(self):
|
|
|
|
return folder.Gmail.GmailFolder
|
|
|
|
|
2020-08-30 14:17:10 +02:00
|
|
|
def gettrashfolder(self):
|
2020-11-01 08:57:43 +01:00
|
|
|
"""
|
|
|
|
Where deleted mail should be moved
|
|
|
|
"""
|
2016-07-28 18:35:33 +02:00
|
|
|
return self.getconf('trashfolder', '[Gmail]/Trash')
|
2011-08-16 10:55:13 +02:00
|
|
|
|
2008-08-13 01:19:38 +02:00
|
|
|
def getspamfolder(self):
|
2020-11-01 08:57:43 +01:00
|
|
|
"""
|
|
|
|
Depending on the IMAP settings (Settings -> Forwarding and
|
|
|
|
POP/IMAP -> IMAP Access -> "When I mark a message in IMAP as
|
|
|
|
deleted") GMail might also deletes messages upon EXPUNGE in
|
|
|
|
the Spam folder.
|
|
|
|
"""
|
2016-11-17 11:45:24 +01:00
|
|
|
return self.getconf('spamfolder', '[Gmail]/Spam')
|