/usr/share/pyshared/Mailnag/daemon/mailchecker.py is in mailnag 0.5.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# mailchecker.py
#
# Copyright 2011, 2012 Patrick Ulbrich <zulu99@gmx.net>
# Copyright 2011 Ralf Hersel <ralf.hersel@gmx.net>
#
# 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 Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
from gi.repository import Notify, Gio
import threading
import sys
import subprocess
import os
import time
from common.utils import get_data_file, gstplay, is_online, get_default_mail_reader
from common.i18n import _
from daemon.reminder import Reminder
from daemon.mailsyncer import MailSyncer
from daemon.pid import Pid
class MailChecker:
def __init__(self, cfg):
self.MAIL_LIST_LIMIT = 10 # prevent flooding of the messaging tray
self._firstcheck = True; # first check after startup
self._mailcheck_lock = threading.Lock()
self._mail_list = []
self._mailsyncer = MailSyncer(cfg)
self._reminder = Reminder()
self._pid = Pid()
self._cfg = cfg
self._gsettings = Gio.Settings.new('org.gnome.shell')
# dict that tracks all notifications that need to be closed
self._notifications = {}
self._reminder.load()
# initialize Notification
Notify.init("Mailnag")
def check(self, accounts):
with self._mailcheck_lock:
print 'Checking %s email account(s) at: %s' % (len(accounts), time.asctime())
# kill all zombies
self._pid.kill()
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)
# process user scripts
self._run_user_scripts("on_mail_check", script_data)
# write stdout to log file
sys.stdout.flush()
self._firstcheck = False
return
def dispose(self):
for n in self._notifications.itervalues():
n.close()
def _notify_summary(self, unseen_mails):
summary = ""
body = ""
if len(self._notifications) == 0:
self._notifications['0'] = self._get_notification(" ", None, None) # empty string will emit a gtk warning
ubound = len(unseen_mails) if len(unseen_mails) <= self.MAIL_LIST_LIMIT else self.MAIL_LIST_LIMIT
for i in range(ubound):
body += unseen_mails[i].sender + ":\n<i>" + unseen_mails[i].subject + "</i>\n\n"
if len(unseen_mails) > self.MAIL_LIST_LIMIT:
body += "<i>" + _("(and {0} more)").format(str(len(unseen_mails) - self.MAIL_LIST_LIMIT)) + "</i>"
if len(unseen_mails) > 1: # multiple new emails
summary = _("You have {0} new mails.").format(str(len(unseen_mails)))
else:
summary = _("You have a new mail.")
self._notifications['0'].update(summary, body, "mail-unread")
self._notifications['0'].show()
def _notify_single(self, new_mails):
for mail in new_mails:
n = self._get_notification(mail.sender, mail.subject, "mail-unread")
notification_id = str(id(n))
n.add_action("mark-as-read", _("Mark as read"), self._notification_action_handler, (mail, notification_id))
n.show()
self._notifications[notification_id] = n
def _get_notification(self, summary, body, icon):
n = Notify.Notification.new(summary, body, icon)
n.set_category("email")
n.add_action("default", "default", self._notification_action_handler, None)
return n
def _notification_action_handler(self, n, action, user_data):
with self._mailcheck_lock:
if action == "default":
mailclient = get_default_mail_reader()
if mailclient != None:
self._pid.append(subprocess.Popen(mailclient))
# clicking the notification bubble has closed all notifications
# so clear the reference array as well.
self._notifications = {}
elif action == "mark-as-read":
self._reminder.set_to_seen(user_data[0].id)
self._reminder.save(self._mail_list)
# clicking the action has closed the notification
# so remove its reference
del self._notifications[user_data[1]]
def _run_user_scripts(self, event, data):
if event == "on_mail_check":
if self._cfg.get('script', 'script0_enabled') == '1':
script_file = self._cfg.get('script', 'script0_file')
if script_file != '' and os.path.exists(script_file):
self._pid.append(subprocess.Popen("%s %s" % (script_file, data), shell = True))
else:
print 'Warning: cannot execute script:', script_file
if (data != '0') and (self._cfg.get('script', 'script1_enabled') == '1'):
script_file = self._cfg.get('script', 'script1_file')
if script_file != '' and os.path.exists(script_file):
self._pid.append(subprocess.Popen("%s %s" % (script_file, data), shell = True))
else:
print 'Warning: cannot execute script:', script_file
|