Beispiel #1
0
 def _do_handle_select(self, cmdname, tag, rest, uid):
     name = decode_imap_utf7(parse_response([rest])[0])
     self.current_mbox = None
     for mbox in self.imap.mailboxes:
         if name == mbox.name:
             self.current_mbox = mbox
             self.send_untagged_response(str(mbox.get_message_count()) + ' EXISTS')
             self.send_untagged_response(str(mbox.get_recent_count()) + ' RECENT')
             self.send_untagged_response('FLAGS (%s)' % ' '.join(mbox.flags))
             self.send_positive_response(None, '[UIDVALIDITY %d]' % mbox.get_uid_validity())
             self.send_positive_response(tag, '%s worked' % cmdname)
             break
     else:
         self.send_negative_response(tag, 'no such mailbox %r' % name)
Beispiel #2
0
  def _reqList(self, conn, *args, **kwargs):
    self.account.reportStatus(brat.EVERYTHING, brat.GOOD)
    acct_id = self.account.details.get('id','')
    caps = conn.capabilities()
    if 'XLIST' in caps:
      result = conn.xlist_folders('', '*')
      kind = self.account.details.get('kind','')
      if kind is '':
        logger.warning("set kind=gmail for account %s in your .raindrop for correct settings",
                        acct_id)
    else:
      logger.warning("This IMAP server doesn't support XLIST, so performance may suffer")
      result = conn.list_folders('', '*')
    # quickly scan through the folders list building the ones we will
    # process and the order.
    logger.info("examining folders")
    folders_use = []
    # First pass - filter folders we don't care about.
    if 'exclude_folders' in self.account.details:
      to_exclude = set(o.lower() for o in re.split(", *", self.account.details['exclude_folders']))
    else:
      to_exclude = set()
    for flags, delim, name in result:
      name = decode_imap_utf7(name)
      ok = True
      for flag in (r'\Noselect', r'\AllMail', r'\Trash', r'\Spam'):
        if flag in flags:
          logger.debug("'%s' has flag %r - skipping", name, flag)
          ok = False
          break
      if ok and self.options.folders and \
         name.lower() not in [o.lower() for o in self.options.folders]:
        logger.debug('skipping folder %r - not in specified folder list', name)
        ok = False
      if ok and 'exclude_folders' in self.account.details and \
         name.lower() in to_exclude:
        logger.debug('skipping folder %r - in exclude list', name)
        ok = False
      if ok:
        folders_use.append((flags, delim, name ))

    # Second pass - prioritize the folders into the order we want to
    # process them - 'special' ones first in a special order, then remaining
    # top-level folders the order they appear, then sub-folders in the order
    # they appear...
    todo_special_folders = []
    todo_top = []
    todo_sub = []

    if 'XLIST' in caps:
      for flags, delim, name in folders_use:
        folder_info = (delim, name)
        # see if this is a special folder 
        for flag in flags:
          if flag == r'\Inbox':
            # Don't use the localized inbox name when talking to the server.
            # Gmail doesn't like this, for example.
            todo_special_folders.insert(0, (delim, "INBOX"))
            break
          elif flag in (r'\Sent', r'\Drafts'):
            todo_special_folders.append(folder_info)
            break
        else:
          # for loop wasn't broken - not a special folder
          if delim in name:
            todo_sub.append(folder_info)
          else:
            todo_top.append(folder_info)
    else:
      # older imap server - just try and find the inbox.
      for flags, delim, name in folders_use:
        folder_info = (delim, name)
        if delim in name:
          todo_sub.append(folder_info)
        elif name.lower()=='inbox':
          todo_top.insert(0, folder_info)
        else:
          todo_top.append(folder_info)
    
    todo = todo_special_folders + todo_top + todo_sub
    try:
      self._updateFolders(conn, todo)
    except:
      log_exception("Failed to update folders for account %r", acct_id)
    # and tell the query queue everything is done.
    self.query_queue.put(None)