コード例 #1
0
ファイル: mailnagdaemon.py プロジェクト: wodole/mailnag
    def _start(self):
        try:
            # Call Accounts-Loaded plugin hooks
            for f in self._hookreg.get_hook_funcs(HookTypes.ACCOUNTS_LOADED):
                try_call(lambda: f(self._accounts))

            if not self._wait_for_inet_connection():
                return

            # Immediate check, check *all* accounts
            try:
                self._mailchecker.check(self._accounts)
            except:
                logging.exception('Caught an exception.')

            idle_accounts = filter(lambda acc: acc.imap and acc.idle,
                                   self._accounts)
            non_idle_accounts = filter(
                lambda acc: (not acc.imap) or (acc.imap and not acc.idle),
                self._accounts)

            # start polling thread for POP3 accounts and
            # IMAP accounts without idle support
            if len(non_idle_accounts) > 0:
                poll_interval = int(self._cfg.get('core', 'poll_interval'))

                def poll_func():
                    try:
                        while True:
                            self._poll_thread_stop.wait(timeout=60.0 *
                                                        poll_interval)
                            if self._poll_thread_stop.is_set():
                                break

                            self._mailchecker.check(non_idle_accounts)
                    except:
                        logging.exception('Caught an exception.')

                self._poll_thread = threading.Thread(target=poll_func)
                self._poll_thread.start()

            # start idler threads for IMAP accounts with idle support
            if len(idle_accounts) > 0:

                def sync_func(account):
                    try:
                        self._mailchecker.check([account])
                    except:
                        logging.exception('Caught an exception.')

                idle_timeout = int(self._cfg.get('core', 'imap_idle_timeout'))
                self._idlrunner = IdlerRunner(idle_accounts, sync_func,
                                              idle_timeout)
                self._idlrunner.start()
        except Exception as ex:
            logging.exception('Caught an exception.')
            if self._fatal_error_handler != None:
                self._fatal_error_handler(ex)
コード例 #2
0
	def _start(self):
		try:
			# Call Accounts-Loaded plugin hooks
			for f in self._hookreg.get_hook_funcs(HookTypes.ACCOUNTS_LOADED):
				try_call( lambda: f(self._accounts) )
			
			if not self._wait_for_inet_connection():
				return
			
			# Immediate check, check *all* accounts
			try:
				self._mailchecker.check(self._accounts)
			except:
				logging.exception('Caught an exception.')

			idle_accounts = filter(lambda acc: acc.imap and acc.idle, self._accounts)
			non_idle_accounts = filter(lambda acc: (not acc.imap) or 
				(acc.imap and not acc.idle), self._accounts)

			# start polling thread for POP3 accounts and
			# IMAP accounts without idle support
			if len(non_idle_accounts) > 0:
				poll_interval = int(self._cfg.get('core', 'poll_interval'))

				def poll_func():
					try:
						while True:
							self._poll_thread_stop.wait(timeout = 60.0 * poll_interval)
							if self._poll_thread_stop.is_set():
								break

							self._mailchecker.check(non_idle_accounts)
					except:
						logging.exception('Caught an exception.')

				self._poll_thread = threading.Thread(target = poll_func)
				self._poll_thread.start()

			# start idler threads for IMAP accounts with idle support
			if len(idle_accounts) > 0:
				def sync_func(account):
					try:
						self._mailchecker.check([account])
					except:
						logging.exception('Caught an exception.')


				idle_timeout = int(self._cfg.get('core', 'imap_idle_timeout'))				
				self._idlrunner = IdlerRunner(idle_accounts, sync_func, idle_timeout)
				self._idlrunner.start()
		except Exception as ex:
			logging.exception('Caught an exception.')
			if self._fatal_error_handler != None:
				self._fatal_error_handler(ex)
コード例 #3
0
ファイル: mailchecker.py プロジェクト: andia89/mailnag
    def check(self, accounts):
        # make sure multiple threads (idler and polling thread)
        # don't check for mails simultaneously.
        with self._mailcheck_lock:
            logging.info('Checking %s email account(s).' % len(accounts))

            for f in self._hookreg.get_hook_funcs(HookTypes.MAIL_CHECK):
                try_call(f)

            if self._conntest.is_offline():
                logging.warning('No internet connection.')
                return

            all_mails = self._mailsyncer.sync(accounts)
            unseen_mails = []
            new_mails = []

            for mail in all_mails:
                if self._memorizer.contains(
                        mail.id):  # mail was fetched before
                    if self._memorizer.is_unseen(
                            mail.id):  # mail was not marked as seen
                        unseen_mails.append(mail)
                        if self._firstcheck:
                            new_mails.append(mail)

                else:  # mail is fetched the first time
                    unseen_mails.append(mail)
                    new_mails.append(mail)

            self._memorizer.sync(all_mails)
            self._memorizer.save()
            self._firstcheck = False

            # apply filter plugin hooks
            filtered_unseen_mails = unseen_mails
            for f in self._hookreg.get_hook_funcs(HookTypes.FILTER_MAILS):
                filtered_unseen_mails = try_call(
                    lambda: f(filtered_unseen_mails), filtered_unseen_mails)

            filtered_new_mails = [
                m for m in new_mails if m in filtered_unseen_mails
            ]

            if len(filtered_new_mails) > 0:
                self._dbus_service.signal_mails_added(filtered_new_mails,
                                                      filtered_unseen_mails)

                for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_ADDED):
                    try_call(
                        lambda: f(filtered_new_mails, filtered_unseen_mails))
            elif len(filtered_unseen_mails) != self._count_on_last_check:
                self._dbus_service.signal_mails_removed(filtered_unseen_mails)

                for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_REMOVED):
                    try_call(lambda: f(filtered_unseen_mails))

            self._count_on_last_check = len(filtered_unseen_mails)

        return
コード例 #4
0
ファイル: mailchecker.py プロジェクト: trinajstica/mailnag
	def check(self, accounts):
		# make sure multiple threads (idler and polling thread) 
		# don't check for mails simultaneously.
		with self._mailcheck_lock:
			logging.info('Checking %s email account(s).' % len(accounts))
			
			for f in self._hookreg.get_hook_funcs(HookTypes.MAIL_CHECK):
				try_call( f )
				
			if self._conntest.is_offline():
				logging.warning('No internet connection.')
				return
			
			all_mails = self._mailsyncer.sync(accounts)
			unseen_mails = []
			new_mails = []
			
			for mail in all_mails:
				if self._memorizer.contains(mail.id): # mail was fetched before
					if self._memorizer.is_unseen(mail.id): # mail was not marked as seen
						unseen_mails.append(mail)
						if self._firstcheck:
							new_mails.append(mail)
				
				else: # mail is fetched the first time
					unseen_mails.append(mail)
					new_mails.append(mail)
			
			self._memorizer.sync(all_mails)
			self._memorizer.save()
			self._firstcheck = False
			
			# apply filter plugin hooks
			filtered_unseen_mails = unseen_mails
			for f in self._hookreg.get_hook_funcs(HookTypes.FILTER_MAILS):
				filtered_unseen_mails = try_call( lambda: f(filtered_unseen_mails), filtered_unseen_mails )
			
			filtered_new_mails = [m for m in new_mails if m in filtered_unseen_mails]
			
			if len(filtered_new_mails) > 0:
				for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_ADDED):
					try_call( lambda: f(filtered_new_mails, filtered_unseen_mails) )
			elif (not self._zero_mails_on_last_check) and (len(filtered_unseen_mails) == 0):
				# TODO : signal MailsRemoved if not all mails have been removed
				# (i.e. if mailcount has been decreased)
				for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_REMOVED):
					try_call( lambda: f(filtered_unseen_mails) )
			
			self._zero_mails_on_last_check = (filtered_unseen_mails == 0)
		
		return
コード例 #5
0
ファイル: mailchecker.py プロジェクト: vantu5z/mailnag
	def check(self, accounts):
		# make sure multiple threads (idler and polling thread) 
		# don't check for mails simultaneously.
		with self._mailcheck_lock:
			logging.info('Checking %s email account(s).' % len(accounts))
			
			for f in self._hookreg.get_hook_funcs(HookTypes.MAIL_CHECK):
				try_call( f )
				
			if self._conntest.is_offline():
				logging.warning('No internet connection.')
				return
			
			all_mails = self._mailsyncer.sync(accounts)
			unseen_mails = []
			new_mails = []
			seen_mails_by_account = {}
			
			for mail in all_mails:
				if self._memorizer.contains(mail.id): # mail was fetched before
					if self._memorizer.is_unseen(mail.id): # mail was not marked as seen
						unseen_mails.append(mail)
						if self._firstcheck:
							new_mails.append(mail)
					else:
						# if the mail account supports tagging mails as seen (e.g. IMAP), 
						# mark the mail as seen on the server as well.
						if mail.account.supports_mark_as_seen():
							if not mail.account in seen_mails_by_account:
								seen_mails_by_account[mail.account] = []
							seen_mails_by_account[mail.account].append(mail)
				else: # mail is fetched the first time
					unseen_mails.append(mail)
					new_mails.append(mail)
			
			# Flag mails to seen on server
			for acc, mails in seen_mails_by_account.items():
				try:
					acc.mark_as_seen(mails)
				except:
					logging.warning("Failed to set mails to seen on server (account: '%s').", acc.name)
			
			self._memorizer.sync(all_mails)
			self._memorizer.save()
			self._firstcheck = False
			
			# apply filter plugin hooks
			filtered_unseen_mails = unseen_mails
			for f in self._hookreg.get_hook_funcs(HookTypes.FILTER_MAILS):
				filtered_unseen_mails = try_call( lambda: f(filtered_unseen_mails), filtered_unseen_mails )
			
			filtered_new_mails = [m for m in new_mails if m in filtered_unseen_mails]
			
			if len(filtered_new_mails) > 0:
				self._dbus_service.signal_mails_added(filtered_new_mails, filtered_unseen_mails)
				
				for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_ADDED):
					try_call( lambda: f(filtered_new_mails, filtered_unseen_mails) )
			elif len(filtered_unseen_mails) != self._count_on_last_check:
				self._dbus_service.signal_mails_removed(filtered_unseen_mails)
				
				for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_REMOVED):
					try_call( lambda: f(filtered_unseen_mails) )
			
			self._count_on_last_check = len(filtered_unseen_mails)
		
		return