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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
# -*- coding: utf-8 -*-
#
## plugins/plugin_installer/plugin_installer.py
##
## Copyright (C) 2010-2012 Denis Fomin <fominde AT gmail.com>
## Copyright (C) 2011-2012 Yann Leboulanger <asterix AT lagaule.org>
## Copyright (C) 2017 Philipp Hörist <philipp AT hoerist.com>
##
## This file is part of Gajim.
##
## Gajim 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; version 3 only.
##
## Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
##
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from gi.repository import Pango
from gi.repository import GLib
import io
import threading
import configparser
import os
import ssl
import logging
import posixpath
import urllib.error
from zipfile import ZipFile
from distutils.version import LooseVersion as V
from urllib.request import urlopen
from common import gajim
from plugins import GajimPlugin
from htmltextview import HtmlTextView
from dialogs import WarningDialog, HigDialog, YesNoDialog
from plugins.gui import GajimPluginConfigDialog
from enum import IntEnum
from gtkgui_helpers import get_action
log = logging.getLogger('gajim.plugin_system.plugin_installer')
PLUGINS_URL = 'https://ftp.gajim.org/plugins_1/'
MANIFEST_URL = 'https://ftp.gajim.org/plugins_1/manifests.zip'
MANIFEST_IMAGE_URL = 'https://ftp.gajim.org/plugins_1/manifests_images.zip'
MANDATORY_FIELDS = ['name', 'version', 'description', 'authors', 'homepage']
FALLBACK_ICON = Gtk.IconTheme.get_default().load_icon(
'preferences-system', Gtk.IconSize.MENU, 0)
class Column(IntEnum):
PIXBUF = 0
DIR = 1
NAME = 2
LOCAL_VERSION = 3
VERSION = 4
UPGRADE = 5
DESCRIPTION = 6
AUTHORS = 7
HOMEPAGE = 8
def get_local_version(plugin_name):
for plugin in gajim.plugin_manager.plugins:
if plugin.name == plugin_name:
return plugin.version
class PluginInstaller(GajimPlugin):
def init(self):
self.description = _('Install and Upgrade Plugins')
self.config_dialog = PluginInstallerPluginConfigDialog(self)
self.config_default_values = {'check_update': (True, '')}
self.gui_extension_points = {'plugin_window': (self.on_activate, None)}
self.window = None
self.progressbar = None
self.available_plugins_model = None
self.timeout_id = 0
self.connected_ids = {}
def activate(self):
if self.config['check_update']:
self.timeout_id = GLib.timeout_add_seconds(30, self.check_update)
if 'plugins' in gajim.interface.instances:
self.on_activate(gajim.interface.instances['plugins'])
def warn_update(self, plugins):
def open_update(dummy):
get_action('plugins').activate()
page = self.notebook.page_num(self.paned)
self.notebook.set_current_page(page)
if plugins:
plugins_str = '\n' + '\n'.join(plugins)
YesNoDialog(
_('Plugins updates'),
_('Some updates are available for your installer plugins. '
'Do you want to update those plugins:\n%s')
% plugins_str, on_response_yes=open_update)
else:
log.info('No updates found')
if hasattr(self, 'thread'):
del self.thread
def check_update(self):
if hasattr(self, 'thread'):
return
log.info('Checking for Updates...')
self.start_download(check_update=True)
self.timeout_id = 0
def deactivate(self):
if hasattr(self, 'available_page'):
self.notebook.remove_page(self.notebook.page_num(self.paned))
self.notebook.set_current_page(0)
for id_, widget in list(self.connected_ids.items()):
widget.disconnect(id_)
del self.available_page
if hasattr(self, 'thread'):
del self.thread
if self.timeout_id > 0:
GLib.source_remove(self.timeout_id)
self.timeout_id = 0
def on_activate(self, plugin_win):
if hasattr(self, 'available_page'):
# 'Available' tab exists
return
if hasattr(self, 'thread'):
del self.thread
self.installed_plugins_model = plugin_win.installed_plugins_model
self.notebook = plugin_win.plugins_notebook
id_ = self.notebook.connect('switch-page', self.on_notebook_switch_page)
self.connected_ids[id_] = self.notebook
self.window = plugin_win.window
id_ = self.window.connect('destroy', self.on_win_destroy)
self.connected_ids[id_] = self.window
self.Gtk_BUILDER_FILE_PATH = self.local_file_path('config_dialog.ui')
self.xml = Gtk.Builder()
self.xml.set_translation_domain('gajim_plugins')
self.xml.add_objects_from_file(self.Gtk_BUILDER_FILE_PATH,
['refresh', 'paned', 'plugin_store'])
widgets_to_extract = (
'name_label', 'available_treeview', 'progressbar', 'paned',
'install_button', 'authors_label', 'homepage_linkbutton',
'version_label', 'scrolled_description_window')
for widget_name in widgets_to_extract:
setattr(self, widget_name, self.xml.get_object(widget_name))
# Make Link in LinkButton not centered
style_provider = Gtk.CssProvider()
css = '.link { padding-left: 0px; padding-right: 0px; }'
style_provider.load_from_data(css.encode())
context = self.homepage_linkbutton.get_style_context()
context.add_provider(style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_USER)
self.available_page = self.notebook.append_page(
self.paned, Gtk.Label.new(_('Available')))
self.available_plugins_model = self.xml.get_object('plugin_store')
self.available_plugins_model.set_sort_column_id(
2, Gtk.SortType.ASCENDING)
self.description_textview = HtmlTextView()
self.description_textview.set_wrap_mode(Gtk.WrapMode.WORD)
self.scrolled_description_window.add(self.description_textview)
self.xml.connect_signals(self)
self.window.show_all()
def on_win_destroy(self, widget):
if hasattr(self, 'thread'):
del self.thread
if hasattr(self, 'available_page'):
del self.available_page
def available_plugins_toggled_cb(self, cell, path):
is_active = self.available_plugins_model[path][Column.UPGRADE]
self.available_plugins_model[path][Column.UPGRADE] = not is_active
dir_list = []
for i in range(len(self.available_plugins_model)):
if self.available_plugins_model[i][Column.UPGRADE]:
dir_list.append(self.available_plugins_model[i][Column.DIR])
self.install_button.set_property('sensitive', bool(dir_list))
def on_notebook_switch_page(self, widget, page, page_num):
tab_label_text = self.notebook.get_tab_label_text(page)
if tab_label_text != (_('Available')):
return
if not hasattr(self, 'thread'):
self.available_plugins_model.clear()
self.start_download(upgrading=True)
def on_install_upgrade_clicked(self, widget):
self.install_button.set_property('sensitive', False)
dir_list = []
for i in range(len(self.available_plugins_model)):
if self.available_plugins_model[i][Column.UPGRADE]:
dir_list.append(self.available_plugins_model[i][Column.DIR])
self.start_download(remote_dirs=dir_list)
def on_error(self, reason):
if reason == 'CERTIFICATE_VERIFY_FAILED':
YesNoDialog(
_('Security error during download'),
_('A security error occurred when '
'downloading. The certificate of the '
'plugin archive could not be verified. '
'this might be a security attack. '
'\n\nYou can continue at your risk. '
'Do you want to do so? '
'(not recommended)'
),
on_response_yes=lambda dlg:
self.start_download(secure=False, upgrading=True))
else:
if self.available_plugins_model:
for i in range(len(self.available_plugins_model)):
self.available_plugins_model[i][Column.UPGRADE] = False
self.progressbar.hide()
text = GLib.markup_escape_text(reason)
WarningDialog(_('Error in download'),
_('An error occurred when downloading\n\n'
'<tt>[%s]</tt>' % (str(text))), self.window)
def start_download(self, secure=True, remote_dirs=False,
upgrading=False, check_update=False):
log.info('Start Download...')
log.debug(
'secure: %s, remote_dirs: %s, upgrading: %s, check_update: %s',
secure, remote_dirs, upgrading, check_update)
self.thread = DownloadAsync(
self, secure=secure, remote_dirs=remote_dirs,
upgrading=upgrading, check_update=check_update)
self.thread.start()
def on_plugin_downloaded(self, plugin_dirs):
for _dir in plugin_dirs:
is_active = False
plugins = None
plugin_dir = os.path.join(gajim.PLUGINS_DIRS[1], _dir)
plugin = gajim.plugin_manager.get_plugin_by_path(plugin_dir)
if plugin:
if plugin.active:
is_active = True
log.info('Deactivate Plugin: %s', plugin)
gajim.plugin_manager.deactivate_plugin(plugin)
gajim.plugin_manager.plugins.remove(plugin)
model = self.installed_plugins_model
for row in range(len(model)):
if plugin == model[row][0]:
model.remove(model.get_iter((row, 0)))
break
log.info('Load Plugin from: %s', plugin_dir)
plugins = gajim.plugin_manager.scan_dir_for_plugins(
plugin_dir, package=True)
if not plugins:
log.warning('Loading Plugin failed')
continue
gajim.plugin_manager.add_plugin(plugins[0])
plugin = gajim.plugin_manager.plugins[-1]
log.info('Loading successful')
for row in range(len(self.available_plugins_model)):
model_row = self.available_plugins_model[row]
if plugin.name == model_row[Column.NAME]:
model_row[Column.LOCAL_VERSION] = plugin.version
model_row[Column.UPGRADE] = False
if is_active:
log.info('Activate Plugin: %s', plugin)
gajim.plugin_manager.activate_plugin(plugin)
# get plugin icon
icon_file = os.path.join(plugin.__path__, os.path.split(
plugin.__path__)[1]) + '.png'
icon = FALLBACK_ICON
if os.path.isfile(icon_file):
icon = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_file, 16, 16)
row = [plugin, plugin.name, is_active, plugin.activatable, icon]
self.installed_plugins_model.append(row)
dialog = HigDialog(
self.window, Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
'', _('All selected plugins downloaded'))
dialog.set_modal(False)
dialog.popup()
def available_plugins_treeview_selection_changed(self, treeview_selection):
model, iter = treeview_selection.get_selected()
self.description_textview.get_buffer().set_text('')
self.name_label.set_text(model.get_value(iter, Column.NAME))
self.version_label.set_text(model.get_value(iter, Column.VERSION))
self.authors_label.set_text(model.get_value(iter, Column.AUTHORS))
self.homepage_linkbutton.set_uri(
model.get_value(iter, Column.HOMEPAGE))
self.homepage_linkbutton.set_label(
model.get_value(iter, Column.HOMEPAGE))
link_label = self.homepage_linkbutton.get_children()[0]
link_label.set_ellipsize(Pango.EllipsizeMode.END)
desc = _(model.get_value(iter, Column.DESCRIPTION))
if not desc.startswith('<body '):
desc = ('<body xmlns=\'http://www.w3.org/1999/xhtml\'>'
'%s</body>') % desc
desc = desc.replace('\n', '<br/>')
self.description_textview.display_html(
desc, self.description_textview, None)
def select_root_iter(self):
selection = self.available_treeview.get_selection()
if selection.count_selected_rows() == 0:
root_iter = self.available_plugins_model.get_iter_first()
path = self.available_plugins_model.get_path(root_iter)
selection.select_iter(root_iter)
self.name_label.show()
self.homepage_linkbutton.show()
self.available_treeview.scroll_to_cell(path)
class DownloadAsync(threading.Thread):
def __init__(self, plugin, secure, remote_dirs, upgrading, check_update):
threading.Thread.__init__(self)
self.plugin = plugin
self.window = plugin.window
self.progressbar = plugin.progressbar
self.model = plugin.available_plugins_model
self.remote_dirs = remote_dirs
self.upgrading = upgrading
self.secure = secure
self.check_update = check_update
self.pulse = None
def model_append(self, row):
row_data = [
row['icon'], row['remote_dir'], row['name'], row['local_version'],
row['version'], row['upgrade'], row['description'], row['authors'],
row['homepage']
]
self.model.append(row_data)
return False
def progressbar_pulse(self):
self.progressbar.pulse()
return True
def run(self):
try:
if self.check_update:
self.run_check_update()
else:
GLib.idle_add(self.progressbar.show)
self.pulse = GLib.timeout_add(150, self.progressbar_pulse)
self.run_download_plugin_list()
except urllib.error.URLError as exc:
if isinstance(exc.reason, ssl.SSLError):
ssl_reason = exc.reason.reason
if ssl_reason == 'CERTIFICATE_VERIFY_FAILED':
log.exception('Certificate verify failed')
GLib.idle_add(self.plugin.on_error, ssl_reason)
except Exception as exc:
GLib.idle_add(self.plugin.on_error, str(exc))
log.exception('Error fetching plugin list')
finally:
if self.pulse:
GLib.source_remove(self.pulse)
GLib.idle_add(self.progressbar.hide)
self.pulse = None
def parse_manifest(self, buf):
'''
given the buffer of the zipfile, returns the list of plugin manifests
'''
zip_file = ZipFile(buf)
manifest_list = zip_file.namelist()
plugins = []
for filename in manifest_list:
# Parse manifest
if not filename.endswith('manifest.ini'):
continue
config = configparser.ConfigParser()
conf_file = zip_file.open(filename)
config.read_file(io.TextIOWrapper(conf_file, encoding='utf-8'))
conf_file.close()
if not config.has_section('info'):
log.warning('Plugin is missing INFO section in manifest.ini. '
'Plugin not loaded.')
continue
opts = config.options('info')
if not set(MANDATORY_FIELDS).issubset(opts):
log.warning(
'%s is missing mandatory fields %s. '
'Plugin not loaded.',
filename,
set(MANDATORY_FIELDS).difference(opts))
continue
# Add icon and remote dir
icon = None
remote_dir = filename.split('/')[0]
png_filename = '{0}/{0}.png'.format(remote_dir)
icon = FALLBACK_ICON
if png_filename in manifest_list:
data = zip_file.open(png_filename).read()
pix = GdkPixbuf.PixbufLoader()
pix.set_size(16, 16)
pix.write(data)
pix.close()
icon = pix.get_pixbuf()
# transform to dictonary
config_dict = {}
for key, value in config.items('info'):
config_dict[key] = value
config_dict['icon'] = icon
config_dict['remote_dir'] = remote_dir
config_dict['upgrade'] = False
plugins.append(config_dict)
return plugins
def download_url(self, url):
log.info('Fetching %s', url)
ssl_args = {}
if self.secure:
ssl_args['context'] = ssl.create_default_context(
cafile=self.plugin.local_file_path('DST_Root_CA_X3.pem'))
else:
ssl_args['context'] = ssl.create_default_context()
ssl_args['context'].check_hostname = False
ssl_args['context'].verify_mode = ssl.CERT_NONE
for flag in ('OP_NO_SSLv2', 'OP_NO_SSLv3',
'OP_NO_TLSv1', 'OP_NO_TLSv1_1',
'OP_NO_COMPRESSION',
):
log.debug('SSL Options: +%s' % flag)
ssl_args['context'].options |= getattr(ssl, flag)
request = urlopen(url, **ssl_args)
return io.BytesIO(request.read())
def run_check_update(self):
to_update = []
zipbuf = self.download_url(MANIFEST_URL)
plugin_list = self.parse_manifest(zipbuf)
for plugin in plugin_list:
local_version = get_local_version(plugin['name'])
if local_version:
gajim_v = V(gajim.config.get('version'))
min_v = plugin.get('min_gajim_version', None)
min_v = V(min_v) if min_v else gajim_v
max_v = plugin.get('max_gajim_version', None)
max_v = V(max_v) if max_v else gajim_v
if (V(plugin['version']) > V(local_version)) and \
(gajim_v >= min_v) and (gajim_v <= max_v):
to_update.append(plugin['name'])
GLib.idle_add(self.plugin.warn_update, to_update)
def run_download_plugin_list(self):
if not self.remote_dirs:
log.info('Downloading Pluginlist...')
zipbuf = self.download_url(MANIFEST_IMAGE_URL)
plugin_list = self.parse_manifest(zipbuf)
for plugin in plugin_list:
plugin['local_version'] = get_local_version(plugin['name'])
if self.upgrading and plugin['local_version']:
if V(plugin['version']) > V(plugin['local_version']):
plugin['upgrade'] = True
GLib.idle_add(
self.plugin.install_button.set_property,
'sensitive', True)
GLib.idle_add(self.model_append, plugin)
GLib.idle_add(self.plugin.select_root_iter)
else:
self.download_plugin()
def download_plugin(self):
for remote_dir in self.remote_dirs:
filename = remote_dir + '.zip'
log.info('Download: %s', filename)
base_dir, user_dir = gajim.PLUGINS_DIRS
if not os.path.isdir(user_dir):
os.mkdir(user_dir)
local_dir = os.path.join(user_dir, remote_dir)
if not os.path.isdir(local_dir):
os.mkdir(local_dir)
local_dir = os.path.split(user_dir)[0]
# downloading zip file
try:
plugin = posixpath.join(PLUGINS_URL, filename)
buf = self.download_url(plugin)
except:
log.exception("Error downloading plugin %s" % filename)
continue
with ZipFile(buf) as zip_file:
zip_file.extractall(os.path.join(local_dir, 'plugins'))
GLib.idle_add(self.plugin.on_plugin_downloaded, self.remote_dirs)
class PluginInstallerPluginConfigDialog(GajimPluginConfigDialog):
def init(self):
glade_file_path = self.plugin.local_file_path('config_dialog.ui')
self.xml = Gtk.Builder()
self.xml.set_translation_domain('gajim_plugins')
self.xml.add_objects_from_file(glade_file_path, ['config_grid'])
grid = self.xml.get_object('config_grid')
self.get_child().pack_start(grid, True, True, 0)
self.xml.connect_signals(self)
def on_run(self):
self.xml.get_object('check_update').set_active(
self.plugin.config['check_update'])
def on_check_update_toggled(self, widget):
self.plugin.config['check_update'] = widget.get_active()
|