def check(self, accounts): with self._mailcheck_lock: print 'Checking %s email account(s) at: %s' % (len(accounts), time.asctime()) self._pid.kill() # kill all zombies if not is_online(): print 'Error: No internet connection' return self._mail_list = self._mailsyncer.sync(accounts) unseen_mails = [] new_mails = [] script_data = "" script_data_mailcount = 0 for mail in self._mail_list: if self._reminder.contains(mail.id): # mail was fetched before if self._reminder.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) script_data += ' "<%s> %s"' % (mail.sender, mail.subject) script_data_mailcount += 1 script_data = str(script_data_mailcount) + script_data if len(self._mail_list) == 0: # no mails (e.g. email client has been launched) -> close notifications for n in self._notifications.itervalues(): n.close() self._notifications = {} elif len(new_mails) > 0: if self._cfg.get('general', 'notification_mode') == '1': self._notify_summary(unseen_mails) else: self._notify_single(new_mails) # play sound if it is enabled in mailnags settings and # gnome-shell notifications aren't disabled if (self._cfg.get('general', 'playsound') == '1') and \ (self._gsettings.get_int('saved-session-presence') != 2): gstplay(get_data_file(self._cfg.get('general', 'soundfile'))) self._reminder.save(self._mail_list) self._run_user_scripts("on_mail_check", script_data) # process user scripts sys.stdout.flush() # write stdout to log file self._firstcheck = False return
def wait_for_inet_connection(): if not is_online(): logging.info('Waiting for internet connection...') while not is_online(): time.sleep(5)
def main(): global mainloop, mailchecker, idlers set_procname("mailnag") GObject.threads_init() signal.signal(signal.SIGTERM, sig_handler) try: write_pid() # write Mailnag's process id to file cfg = read_config() if (cfg == None): print 'Error: Cannot find configuration file. Please run mailnag_config first.' exit(1) if not is_online(): print 'Waiting for internet connection...' while not is_online(): time.sleep(5) accounts = AccountList() accounts.load_from_cfg(cfg, enabled_only = True) mailchecker = MailChecker(cfg) # immediate check, check *all* accounts mailchecker.check(accounts) idle_accounts = filter(lambda acc: acc.imap and acc.idle, accounts) non_idle_accounts = filter(lambda acc: (not acc.imap) or (acc.imap and not acc.idle), accounts) # start polling thread for POP3 accounts and # IMAP accounts without idle support if len(non_idle_accounts) > 0: def poll_func(): try: mailchecker.check(non_idle_accounts) except: traceback.print_exc() return True check_interval = int(cfg.get('general', 'check_interval')) GObject.timeout_add_seconds(60 * check_interval, poll_func) # start idler threads for IMAP accounts with idle support if len(idle_accounts) > 0: def sync_func(account): try: mailchecker.check([account]) except: traceback.print_exc() idlers = Idlers(idle_accounts, sync_func) idlers.run() mainloop = GObject.MainLoop() mainloop.run() except KeyboardInterrupt: pass # ctrl+c pressed finally: cleanup()
def wait_for_inet_connection(): if not is_online(): print 'Waiting for internet connection...' while not is_online(): time.sleep(5)