Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
except self.readonly as e:
# pass self.readonly to our callers
raise
except self.abort as e:
# self.abort is raised when we are supposed to retry
errstr = "Server '%s' closed connection, error on SELECT '%s'. Ser"\
"ver said: %s" % (self.host, mailbox, e.args[0])
severity = OfflineImapError.ERROR.FOLDER_RETRY
six.reraise(OfflineImapError,
OfflineImapError(errstr, severity),
exc_info()[2])
if result[0] != 'OK':
#in case of error, bail out with OfflineImapError
errstr = "Error SELECTing mailbox '%s', server reply:\n%s" %\
(mailbox, result)
severity = OfflineImapError.ERROR.FOLDER
raise OfflineImapError(errstr, severity)
return result
# positive UID, so simply return here.
if uid < 0:
return uid
# If msg uid already exists, just modify the flags.
if uid in self.r2l:
self.savemessageflags(uid, flags)
return uid
newluid = self._mb.savemessage(-1, content, flags, rtime)
if newluid < 1:
raise OfflineImapError("server of repository '%s' did not return "
"a valid UID (got '%s') for UID '%s' from '%s'"% (
self._mb.getname(), newluid, uid, self.getname()
),
OfflineImapError.ERROR.MESSAGE
)
with self.maplock:
self.diskl2r[newluid] = uid
self.diskr2l[uid] = newluid
self.l2r[newluid] = uid
self.r2l[uid] = newluid
self._savemaps()
return uid
continue
tried_to_authn = True
self.ui.debug('imap', u'Attempting '
'%s authentication'% m)
try:
if func(imapobj):
return
except (imapobj.error, OfflineImapError) as e:
self.ui.warn('%s authentication failed: %s'% (m, e))
exc_stack.append((m, e))
if len(exc_stack):
msg = "\n\t".join([": ".join((x[0], str(x[1]))) for x in exc_stack])
raise OfflineImapError("All authentication types "
"failed:\n\t%s"% msg, OfflineImapError.ERROR.REPO)
if not tried_to_authn:
methods = ", ".join([x[5:] for x in
[x for x in imapobj.capabilities if x[0:5] == "AUTH="]])
raise OfflineImapError(u"Repository %s: no supported "
"authentication mechanisms found; configured %s, "
"server advertises %s"% (self.repos,
", ".join(self.authmechs), methods),
OfflineImapError.ERROR.REPO)
:returns: hostname as string or throws Exception"""
if self._host: # Use cached value if possible.
return self._host
# 1) Check for remotehosteval setting.
if self.config.has_option(self.getsection(), 'remotehosteval'):
host = self.getconf('remotehosteval')
try:
host = self.localeval.eval(host)
except Exception as e:
six.reraise(OfflineImapError,
OfflineImapError(
"remotehosteval option for repository "
"'%s' failed:\n%s"% (self, e),
OfflineImapError.ERROR.REPO),
exc_info()[2])
if host:
self._host = host
return self._host
# 2) Check for plain remotehost setting.
host = self.getconf('remotehost', None)
if host != None:
self._host = host
return self._host
# No success.
raise OfflineImapError("No remote host for repository "
"'%s' specified."% self, OfflineImapError.ERROR.REPO)
if e.severity >= OfflineImapError.ERROR.CRITICAL:
raise
return
# Loop account sync if needed (bail out after 3 failures).
looping = 3
while looping:
self.ui.acct(self)
try:
self.__lock()
self.__sync()
except (KeyboardInterrupt, SystemExit):
raise
except OfflineImapError as e:
# Stop looping and bubble up Exception if needed.
if e.severity >= OfflineImapError.ERROR.REPO:
if looping:
looping -= 1
if e.severity >= OfflineImapError.ERROR.CRITICAL:
raise
self.ui.error(e, exc_info()[2])
except Exception as e:
self.ui.error(e, exc_info()[2], msg=
"While attempting to sync account '%s'"% self)
else:
# After success sync, reset the looping counter to 3.
if self.refreshperiod:
looping = 3
finally:
self.ui.acctdone(self)
self._unlock()
if looping and self._sleeper() >= 2:
if self.__getselectedfolder() == mailbox and \
self.is_readonly == readonly and \
not force:
# No change; return.
return
try:
result = super(UsefulIMAPMixIn, self).select(mailbox, readonly)
except self.readonly as e:
# pass self.readonly to our callers
raise
except self.abort as e:
# self.abort is raised when we are supposed to retry
errstr = "Server '%s' closed connection, error on SELECT '%s'. Ser"\
"ver said: %s" % (self.host, mailbox, e.args[0])
severity = OfflineImapError.ERROR.FOLDER_RETRY
six.reraise(OfflineImapError,
OfflineImapError(errstr, severity),
exc_info()[2])
if result[0] != 'OK':
#in case of error, bail out with OfflineImapError
errstr = "Error SELECTing mailbox '%s', server reply:\n%s" %\
(mailbox, result)
severity = OfflineImapError.ERROR.FOLDER
raise OfflineImapError(errstr, severity)
return result
if infomatch:
filename = filename[:-len(infomatch.group())] #strip off
infostr = '%s2,%s'% (self.infosep, ''.join(sorted(flags)))
filename += infostr
newfilename = os.path.join(dir_prefix, filename)
if (newfilename != oldfilename):
try:
os.rename(os.path.join(self.getfullname(), oldfilename),
os.path.join(self.getfullname(), newfilename))
except OSError as e:
six.reraise(OfflineImapError,
OfflineImapError(
"Can't rename file '%s' to '%s': %s"%
(oldfilename, newfilename, e[1]),
OfflineImapError.ERROR.FOLDER),
exc_info()[2])
self.messagelist[uid]['flags'] = flags
self.messagelist[uid]['filename'] = newfilename
the UIDs of the messages in localfolder might not be in the same
order as those of corresponding messages in remotefolder, so if L in
local corresponds to R in remote, the ranges [L, ...] and [R, ...]
might not correspond. But, if we're cloning a folder into a new one,
[min_uid, ...] does correspond to [1, ...].
This is just for IMAP-IMAP. For Maildir-IMAP, use maxage instead."""
new.cachemessagelist()
min_uid = partial.retrieve_min_uid()
if min_uid == None: # min_uid file didn't exist
if len(new.getmessageuidlist()) > 0:
raise OfflineImapError("To use startdate on Repository %s, "
"Repository %s must be empty"%
(partial.repository.name, new.repository.name),
OfflineImapError.ERROR.MESSAGE)
else:
partial.cachemessagelist(min_date=date)
# messagelist.keys() instead of getuidmessagelist() because in
# the UID mapped case we want the actual local UIDs, not their
# remote counterparts.
positive_uids = [uid for uid in list(partial.messagelist.keys()) if uid > 0]
if len(positive_uids) > 0:
min_uid = min(positive_uids)
else:
min_uid = 1
partial.save_min_uid(min_uid)
else:
partial.cachemessagelist(min_uid=min_uid)
def open(self, host=None, port=None):
if not self.ca_certs and not self._fingerprint:
raise OfflineImapError("No CA certificates "
"and no server fingerprints configured. "
"You must configure at least something, otherwise "
"having SSL helps nothing.", OfflineImapError.ERROR.REPO)
super(WrappedIMAP4_SSL, self).open(host, port)
if self._fingerprint:
server_cert = self.sock.getpeercert(True)
hashes = sha512, sha384, sha256, sha224, sha1
server_fingerprints = [hash(server_cert).hexdigest() for hash in hashes]
# compare fingerprints
matches = [(server_fingerprint in self._fingerprint) for server_fingerprint in server_fingerprints]
if not any(matches):
raise OfflineImapError("Server SSL fingerprint(s) '%s' "
"for hostname '%s' "
"does not match configured fingerprint(s) %s. "
"Please verify and set 'cert_fingerprint' accordingly "
"if not set yet."%
(zip([hash.__name__ for hash in hashes], server_fingerprints), host, self._fingerprint),
OfflineImapError.ERROR.REPO)
imapobj.identifier)
self.parent.releaseconnection(imapobj, True)
else:
self.parent.releaseconnection(imapobj)
while not self.stop_sig.isSet():
self.needsync = False
success = False # Successfully selected FOLDER?
while not success:
imapobj = self.parent.acquireconnection()
try:
imapobj.select(self.folder)
except OfflineImapError as e:
if e.severity == OfflineImapError.ERROR.FOLDER_RETRY:
# Connection closed, release connection and retry.
self.ui.error(e, exc_info()[2])
self.parent.releaseconnection(imapobj, True)
elif e.severity == OfflineImapError.ERROR.FOLDER:
# Just continue the process on such error for now.
self.ui.error(e, exc_info()[2])
else:
# Stops future attempts to sync this account.
raise
else:
success = True
if "IDLE" in imapobj.capabilities:
imapobj.idle(callback=callback)
else:
self.ui.warn("IMAP IDLE not supported on server '%s'."
"Sleep until next refresh cycle."% imapobj.identifier)