2008-01-03 04:56:55 +01:00
|
|
|
# Gmail IMAP repository support
|
|
|
|
# Copyright (C) 2008 Riccardo Murri <riccardo.murri@gmail.com>
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
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
|
|
|
|
|
|
|
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."""
|
2014-01-09 21:30:41 +01:00
|
|
|
|
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:
|
|
|
|
return super(GmailRepository, self).gethost()
|
|
|
|
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
|
|
|
|
|
|
|
url = super(GmailRepository, self).getoauth2_request_url()
|
|
|
|
if url is None:
|
|
|
|
# Nothing was configured, cache and return hardcoded one.
|
2016-11-17 11:45:24 +01:00
|
|
|
self.setoauth2_request_url("https://accounts.google.com/o/oauth2/token")
|
2015-10-12 07:51:05 +02:00
|
|
|
else:
|
2016-07-28 18:35:33 +02:00
|
|
|
self.setoauth2_request_url(url)
|
|
|
|
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."""
|
|
|
|
|
|
|
|
port = super(GmailRepository, self).getport()
|
|
|
|
|
|
|
|
if port is None:
|
|
|
|
return 993
|
|
|
|
else:
|
|
|
|
return port
|
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
|
|
|
|
else:
|
|
|
|
return ssl
|
2008-01-03 04:56:55 +01:00
|
|
|
|
|
|
|
def getpreauthtunnel(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getfolder(self, foldername):
|
|
|
|
return self.getfoldertype()(self.imapserver, foldername,
|
2011-09-19 14:22:03 +02:00
|
|
|
self)
|
2008-01-03 04:56:55 +01:00
|
|
|
|
|
|
|
def getfoldertype(self):
|
|
|
|
return folder.Gmail.GmailFolder
|
|
|
|
|
2008-08-13 01:19:38 +02:00
|
|
|
def gettrashfolder(self, foldername):
|
2016-07-28 18:35:33 +02:00
|
|
|
# Where deleted mail should be moved
|
|
|
|
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):
|
2016-11-17 11:45:24 +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.
|
|
|
|
return self.getconf('spamfolder', '[Gmail]/Spam')
|