File: userscriptplugin.py

package info (click to toggle)
mailnag 2.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 968 kB
  • sloc: python: 5,332; xml: 38; sh: 15; makefile: 11
file content (126 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download | duplicates (2)
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
# Copyright 2013 - 2020 Patrick Ulbrich <zulu99@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.
#

import gi
gi.require_version('Gtk', '3.0')

import os
from gi.repository import Gtk
from Mailnag.common.plugins import Plugin, HookTypes
from Mailnag.common.i18n import _
from Mailnag.common.subproc import start_subprocess

plugin_defaults = { 'script_file' : '' }


class UserscriptPlugin(Plugin):
	def __init__(self):
		self._mails_added_hook = None

	
	def enable(self):
		def mails_added_hook(new_mails, all_mails):
			self._run_userscript(new_mails)
		
		self._mails_added_hook = mails_added_hook
		
		controller = self.get_mailnag_controller()
		hooks = controller.get_hooks()
		
		hooks.register_hook_func(HookTypes.MAILS_ADDED, 
			self._mails_added_hook)
		
	
	def disable(self):
		controller = self.get_mailnag_controller()
		hooks = controller.get_hooks()
		
		if self._mails_added_hook != None:
			hooks.unregister_hook_func(HookTypes.MAILS_ADDED,
				self._mails_added_hook)
			self._mails_added_hook = None

	
	def get_manifest(self):
		return (_("User Script"),
				_("Runs an user defined script on mail arrival."),
				"2.0",
				"Patrick Ulbrich <zulu99@gmx.net>")


	def get_default_config(self):
		return plugin_defaults
	
	
	def has_config_ui(self):
		return True
	
	
	def get_config_ui(self):
		box = Gtk.Box()
		box.set_spacing(12)
		box.set_orientation(Gtk.Orientation.VERTICAL)
		#box.set_size_request(100, -1)
		
		markup_str = "<i>&lt;%s&gt; &lt;%s&gt; &lt;%s&gt;</i>" % (_('account'), _('sender'), _('subject'))
		desc =  _(	"The following script will be executed whenever new mails arrive.\n"
					"Mailnag passes the total count of new mails to this script,\n"
					"followed by %s sequences.") % markup_str
		
		label = Gtk.Label()
		label.set_line_wrap(True)
		label.set_markup(desc)
		#label.set_size_request(100, -1);
		box.pack_start(label, False, False, 0)
		
		filechooser = Gtk.FileChooserButton()
		box.pack_start(filechooser, False, False, 0)
		
		return box
	
	
	def load_ui_from_config(self, config_ui):
		config = self.get_config()
		script_file = config['script_file']
		if len(script_file) > 0:
			filechooser = config_ui.get_children()[1]
			filechooser.set_filename(script_file)
	
	
	def save_ui_to_config(self, config_ui):
		config = self.get_config()
		filechooser = config_ui.get_children()[1]
		script_file = filechooser.get_filename()
		if script_file == None: script_file = ''
		config['script_file'] = script_file
	
	
	def _run_userscript(self, new_mails):
		config = self.get_config()
		script_file = config['script_file'].strip()
		if (len(script_file) > 0) and os.path.exists(script_file):
			script_args = [ script_file, str(len(new_mails)) ]
			
			for m in new_mails:
				sender_name, sender_addr = m.sender
				if len(sender_addr) == 0: sender_addr = 'UNKNOWN_SENDER'
				
				script_args.append(m.account.name)
				script_args.append(sender_addr)
				script_args.append(m.subject)
			start_subprocess(script_args)