Newer
Older
##
## 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, Gdk, GLib, GObject, GdkPixbuf, Gio
import hashlib
import binascii
from urllib.parse import urlparse
from io import BytesIO
import shutil
from gajim.common import configpaths
from gajim import dialogs
from gajim.plugins import GajimPlugin
from gajim.plugins.helpers import log_calls
from gajim.plugins.gui import GajimPluginConfigDialog
from gajim.conversation_textview import TextViewImage
from .http_functions import get_http_head, get_http_file
from .config_dialog import UrlImagePreviewConfigDialog
log = logging.getLogger('gajim.plugin_system.preview')
try:
from PIL import Image
except:
log.debug('Pillow not available')
try:
if os.name == 'nt':
from cryptography.hazmat.backends.openssl import backend
else:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers import algorithms
from cryptography.hazmat.primitives.ciphers.modes import GCM
decryption_available = True
except Exception as e:
DEP_MSG = 'For preview of encrypted images, ' \
'please install python-cryptography!'
log.debug('Cryptography Import Error: ' + str(e))
log.info('Decryption/Encryption disabled due to errors')
decryption_available = False
ACCEPTED_MIME_TYPES = ('image/png', 'image/jpeg', 'image/gif', 'image/raw',
'image/svg+xml', 'image/x-ms-bmp')
class UrlImagePreviewPlugin(GajimPlugin):
@log_calls('UrlImagePreviewPlugin')
def init(self):
if not decryption_available:
self.available_text = DEP_MSG
self.config_dialog = partial(UrlImagePreviewConfigDialog, self)
'chat_control_base': (self.connect_with_chat_control,
self.disconnect_from_chat_control),
'history_window':
(self.connect_with_history, self.disconnect_from_history),
'print_real_text': (self.print_real_text, None), }
'PREVIEW_SIZE': (150, 'Preview size(10-512)'),
'MAX_FILE_SIZE': (524288, 'Max file size for image preview'),
'LEFTCLICK_ACTION': ('open_menuitem', 'Open'),
'ANONYMOUS_MUC': False,}
self.history_window_control = None
@log_calls('UrlImagePreviewPlugin')
def connect_with_chat_control(self, chat_control):
account = chat_control.contact.account.name
jid = chat_control.contact.jid
if account not in self.controls:
self.controls[account] = {}
self.controls[account][jid] = Base(self, chat_control.conv_textview,
chat_control.parent_win.window)
@log_calls('UrlImagePreviewPlugin')
def disconnect_from_chat_control(self, chat_control):
account = chat_control.contact.account.name
jid = chat_control.contact.jid
self.controls[account][jid].deinit_handlers()
@log_calls('UrlImagePreviewPlugin')
def connect_with_history(self, history_window):
if self.history_window_control:
log.error("connect_with_history: deinit handlers")
self.history_window_control.deinit_handlers()
log.error("connect_with_history: create base")
self.history_window_control = Base(
self, history_window.history_textview, history_window)
@log_calls('UrlImagePreviewPlugin')
def disconnect_from_history(self, history_window):
if self.history_window_control:
self.history_window_control.deinit_handlers()
self.history_window_control = None
def print_real_text(self, tv, real_text, text_tags, graphics,
iter_, additional_data):
if tv.used_in_history_window and self.history_window_control:
self.history_window_control.print_real_text(
real_text, text_tags, graphics, iter_, additional_data)
account = tv.account
for jid in self.controls[account]:
if self.controls[account][jid].textview != tv:
self.controls[account][jid].print_real_text(
real_text, text_tags, graphics, iter_, additional_data)
def __init__(self, plugin, textview, parent_win=None):
self.parent_win = parent_win
self.textview = textview
self.directory = os.path.join(configpaths.gajimpaths['MY_DATA'],
'downloads')
self.thumbpath = os.path.join(configpaths.gajimpaths['MY_CACHE'],
'downloads.thumb')
try:
self._create_path(self.directory)
self._create_path(self.thumbpath)
except Exception as e:
log.error("Error creating download and/or thumbnail folder!")
raise
def deinit_handlers(self):
# remove all register handlers on wigets, created by self.xml
# to prevent circular references among objects
for i in list(self.handlers.keys()):
if self.handlers[i].handler_is_connected(i):
self.handlers[i].disconnect(i)
del self.handlers[i]
def print_real_text(self, real_text, text_tags, graphics, iter_,
additional_data):
urlparts = urlparse(real_text)
if (urlparts.scheme not in ["https", "aesgcm"] or
not urlparts.netloc):
log.info("Not accepting URL scheme '%s' for image preview: %s" %
(str(urlparts.scheme), real_text))
try:
oob_url = additional_data["gajim"]["oob_url"]
except (KeyError, AttributeError):
oob_url = None
# allow aesgcm uris without oob marker (aesgcm uris are always
# httpupload filetransfers)
if (urlparts.scheme != "aesgcm" and real_text != oob_url):
log.info("Not accepting URL for image preview "
"(wrong or no oob data): %s", real_text)
log.debug("additional_data: %s", additional_data)
return
# Don't print the URL in the message window (in the calling function)
self.textview.plugin_modified = True
if not iter_:
iter_ = buffer_.get_end_iter()
# Show URL, until image is loaded (if ever)
ttt = buffer_.get_tag_table()
repl_start = buffer_.create_mark(None, iter_, True)
buffer_.insert_with_tags(iter_, real_text,
*[(ttt.lookup(t) if isinstance(t, str) else t) for t in ["url"]])
repl_end = buffer_.create_mark(None, iter_, True)
filename = os.path.basename(urlparts.path)
ext = os.path.splitext(filename)[1]
name = os.path.splitext(filename)[0]
namehash = hashlib.sha1(real_text.encode('utf-8')).hexdigest()
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
newfilename = name + '_' + namehash + ext
thumbfilename = name + '_' + namehash + '_thumb_' \
+ str(self.plugin.config['PREVIEW_SIZE']) + ext
filepath = os.path.join(self.directory, newfilename)
thumbpath = os.path.join(self.thumbpath, thumbfilename)
filepaths = [filepath, thumbpath]
key = ''
iv = ''
encrypted = False
if urlparts.fragment:
fragment = binascii.unhexlify(urlparts.fragment)
key = fragment[16:]
iv = fragment[:16]
if len(key) == 32 and len(iv) == 16:
encrypted = True
if not encrypted:
key = fragment[12:]
iv = fragment[:12]
if len(key) == 32 and len(iv) == 12:
encrypted = True
# file exists but thumbnail got deleted
if os.path.exists(filepath) and not os.path.exists(thumbpath):
with open(filepath, 'rb') as f:
mem = f.read()
f.closed
app.thread_interface(
self._save_thumbnail, [thumbpath, (mem, '')],
self._update_img, [real_text, repl_start,
repl_end, filepath, encrypted])
# display thumbnail if already downloadeded
# (but only if file also exists)
elif os.path.exists(filepath) and os.path.exists(thumbpath):
app.thread_interface(
self._load_thumbnail, [thumbpath],
self._update_img, [real_text, repl_start,
repl_end, filepath, encrypted])
# or download file, calculate thumbnail and finally display it
else:
if encrypted and not decryption_available:
log.debug('Please install Crytography to decrypt pictures')
else:
# First get the http head request
# which does not fetch data, just headers
# then check the mime type and filesize
if urlparts.scheme == 'aesgcm':
real_text = 'https://' + real_text[9:]
get_http_head, [self.textview.account, real_text],
self._check_mime_size, [real_text, repl_start, repl_end,
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
filepaths, key, iv, encrypted])
def _save_thumbnail(self, thumbpath, tuple_arg):
mem, alt = tuple_arg
size = self.plugin.config['PREVIEW_SIZE']
use_Gtk = False
output = None
try:
output = BytesIO()
im = Image.open(BytesIO(mem))
im.thumbnail((size, size), Image.ANTIALIAS)
im.save(output, "jpeg", quality=100, optimize=True)
mem = output.getvalue()
output.close()
except Exception as e:
if output:
output.close()
log.info("Failed to load image using pillow, "
"falling back to gdk pixbuf.")
log.debug(e)
use_Gtk = True
if use_Gtk:
log.info("Pillow not available or file corrupt, "
"trying to load using gdk pixbuf.")
loader.write(mem)
loader.close()
pixbuf = loader.get_pixbuf()
pixbuf, w, h = self._get_pixbuf_of_size(pixbuf, size)
ok, mem = pixbuf.save_to_bufferv("jpeg", ["quality"], ["100"])
except Exception as e:
log.info("Failed to load image using gdk pixbuf, "
"ignoring image.")
log.debug(e)
return ('', '')
try:
self._create_path(os.path.dirname(thumbpath))
self._write_file(thumbpath, mem)
except Exception as e:
dialogs.ErrorDialog(
_('Could not save file'),
_('Exception raised while saving thumbnail '
'for image file (see error log for more '
'information)'),
transient_for=self.parent_win)
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
log.error(str(e))
return (mem, alt)
def _load_thumbnail(self, thumbpath):
with open(thumbpath, 'rb') as f:
mem = f.read()
f.closed
return (mem, '')
def _write_file(self, path, data):
log.info("Writing '%s' of size %d..." % (path, len(data)))
try:
with open(path, "wb") as output_file:
output_file.write(data)
output_file.closed
except Exception as e:
log.error("Failed to write file '%s'!" % path)
raise
def _update_img(self, tuple_arg, url, repl_start, repl_end,
filepath, encrypted):
mem, alt = tuple_arg
if mem:
try:
urlparts = urlparse(url)
filename = os.path.basename(urlparts.path)
eb = Gtk.EventBox()
eb.connect('button-press-event', self.on_button_press_event,
filepath, filename, url, encrypted)
eb.connect('enter-notify-event', self.on_enter_event)
eb.connect('leave-notify-event', self.on_leave_event)
# this is threadsafe
# (Gtk textview is NOT threadsafe by itself!!)
def add_to_textview():
try: # textview closed in the meantime etc.
at_end = self.textview.at_the_end()
buffer_ = repl_start.get_buffer()
iter_ = buffer_.get_iter_at_mark(repl_start)
#buffer_.insert(iter_, "\n")
anchor = buffer_.create_child_anchor(iter_)
# Use url as tooltip for image
img = TextViewImage(anchor, url)
loader = GdkPixbuf.PixbufLoader()
loader.write(mem)
loader.close()
pixbuf = loader.get_pixbuf()
img.set_from_pixbuf(pixbuf)
eb.add(img)
eb.show_all()
self.textview.tv.add_child_at_anchor(eb, anchor)
buffer_.delete(iter_,
buffer_.get_iter_at_mark(repl_end))
if at_end:
GObject.idle_add(self.textview.scroll_to_end_iter)
except Exception as ex:
log.warn("Exception while loading %s: %s" % (str(url), str(ex)))
return False
# add to mainloop --> make call threadsafe
GObject.idle_add(add_to_textview)
# URL is already displayed
log.error('Could not display image for URL: %s'
% url)
raise
else:
# If image could not be downloaded, URL is already displayed
log.error('Could not download image for URL: %s -- %s'
% (url, alt))
def _check_mime_size(self, tuple_arg,
url, repl_start, repl_end, filepaths,
key, iv, encrypted):
file_mime, file_size = tuple_arg
# Check if mime type is acceptable
if file_mime == '' and file_size == 0:
log.info("Failed to load HEAD Request for URL: '%s'"
"(see debug log for more info)" % url)
# URL is already displayed
return
if file_mime.lower() not in ACCEPTED_MIME_TYPES:
log.info("Not accepted mime type '%s' for URL: '%s'"
% (file_mime.lower(), url))
# URL is already displayed
return
# Check if file size is acceptable
max_size = int(self.plugin.config['MAX_FILE_SIZE'])
if file_size > max_size or file_size == 0:
log.info("File size (%s) too big or unknown (zero) for URL: '%s'"
% (str(file_size), url))
# URL is already displayed
return
attributes = {'src': url,
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
'filepaths': filepaths,
'key': key,
'iv': iv}
app.thread_interface(
self._download_image, [self.textview.account,
attributes, encrypted],
self._update_img, [url, repl_start, repl_end,
filepaths[0], encrypted])
def _download_image(self, account, attributes, encrypted):
filepath = attributes['filepaths'][0]
thumbpath = attributes['filepaths'][1]
key = attributes['key']
iv = attributes['iv']
mem, alt = get_http_file(account, attributes)
# Decrypt file if necessary
if encrypted:
mem = self._aes_decrypt_fast(key, iv, mem)
try:
# Write file to harddisk
self._write_file(filepath, mem)
except Exception as e:
dialogs.ErrorDialog(
_('Could not save file'),
_('Exception raised while saving image file'
' (see error log for more information)'),
transient_for=self.parent_win)
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
log.error(str(e))
# Create thumbnail, write it to harddisk and return it
return self._save_thumbnail(thumbpath, (mem, alt))
def _create_path(self, folder):
if os.path.exists(folder):
return
log.debug("creating folder '%s'" % folder)
os.mkdir(folder, 0o700)
def _aes_decrypt_fast(self, key, iv, payload):
# Use AES128 GCM with the given key and iv to decrypt the payload.
if os.name == 'nt':
be = backend
else:
be = default_backend()
data = payload[:-16]
tag = payload[-16:]
decryptor = Cipher(
algorithms.AES(key),
GCM(iv, tag=tag),
backend=be).decryptor()
return decryptor.update(data) + decryptor.finalize()
def _get_pixbuf_of_size(self, pixbuf, size):
# Creates a pixbuf that fits in the specified square of sizexsize
# while preserving the aspect ratio
# Returns tuple: (scaled_pixbuf, actual_width, actual_height)
image_width = pixbuf.get_width()
image_height = pixbuf.get_height()
if image_width > image_height:
if image_width > size:
image_height = int(size / float(image_width) * image_height)
image_width = int(size)
else:
if image_height > size:
image_width = int(size / float(image_height) * image_width)
image_height = int(size)
crop_pixbuf = pixbuf.scale_simple(image_width, image_height,
return (crop_pixbuf, image_width, image_height)
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
def make_rightclick_menu(self, event, data):
xml = Gtk.Builder()
xml.set_translation_domain('gajim_plugins')
xml.add_from_file(self.plugin.local_file_path('context_menu.ui'))
menu = xml.get_object('context_menu')
open_menuitem = xml.get_object('open_menuitem')
save_as_menuitem = xml.get_object('save_as_menuitem')
copy_link_location_menuitem = \
xml.get_object('copy_link_location_menuitem')
open_link_in_browser_menuitem = \
xml.get_object('open_link_in_browser_menuitem')
open_file_in_browser_menuitem = \
xml.get_object('open_file_in_browser_menuitem')
extras_separator = \
xml.get_object('extras_separator')
if data["encrypted"]:
open_link_in_browser_menuitem.hide()
if app.config.get('autodetect_browser_mailer') \
or app.config.get('custombrowser') == '':
extras_separator.hide()
open_file_in_browser_menuitem.hide()
id_ = open_menuitem.connect(
'activate', self.on_open_menuitem_activate, data)
self.handlers[id_] = open_menuitem
id_ = save_as_menuitem.connect(
'activate', self.on_save_as_menuitem_activate, data)
self.handlers[id_] = save_as_menuitem
id_ = copy_link_location_menuitem.connect(
'activate', self.on_copy_link_location_menuitem_activate, data)
self.handlers[id_] = copy_link_location_menuitem
id_ = open_link_in_browser_menuitem.connect(
'activate', self.on_open_link_in_browser_menuitem_activate, data)
self.handlers[id_] = open_link_in_browser_menuitem
id_ = open_file_in_browser_menuitem.connect(
'activate', self.on_open_file_in_browser_menuitem_activate, data)
self.handlers[id_] = open_file_in_browser_menuitem
return menu
def on_open_menuitem_activate(self, menu, data):
filepath = data["filepath"]
helpers.launch_file_manager(filepath)
def on_save_as_menuitem_activate(self, menu, data):
filepath = data["filepath"]
original_filename = data["original_filename"]
def on_continue(response, target_path):
if response < 0:
return
shutil.copy(filepath, target_path)
dialog.destroy()
def on_ok(widget):
target_path = dialog.get_filename()
if os.path.exists(target_path):
# check if we have write permissions
if not os.access(target_path, os.W_OK):
file_name = os.path.basename(target_path)
dialogs.ErrorDialog(
_('Cannot overwrite existing file "%s"') % file_name,
_('A file with this name already exists and you do '
'not have permission to overwrite it.'))
return
dialog2 = dialogs.FTOverwriteConfirmationDialog(
_('This file already exists'),
_('What do you want to do?'),
propose_resume=False,
on_response=(on_continue, target_path),
transient_for=dialog)
dialog2.set_destroy_with_parent(True)
else:
dirname = os.path.dirname(target_path)
if not os.access(dirname, os.W_OK):
dialogs.ErrorDialog(
_('Directory "%s" is not writable') % dirname,
_('You do not have permission to '
'create files in this directory.'))
return
on_continue(0, target_path)
def on_cancel(widget):
dialog.destroy()
dialog = dialogs.FileChooserDialog(
title_text=_('Save Image as...'),
action=Gtk.FileChooserAction.SAVE,
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.OK),
default_response=Gtk.ResponseType.OK,
current_folder=app.config.get('last_save_dir'),
on_response_ok=on_ok,
on_response_cancel=on_cancel)
dialog.set_current_name(original_filename)
dialog.connect('delete-event', lambda widget, event:
on_cancel(widget))
def on_copy_link_location_menuitem_activate(self, menu, data):
url = data["url"]
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(url, -1)
clipboard.store()
def on_open_link_in_browser_menuitem_activate(self, menu, data):
url = data["url"]
if data["encrypted"]:
dialogs.ErrorDialog(
_('Encrypted file'),
_('You cannot open encrypted files in your '
'browser directly. Try "Open Downloaded File '
'in Browser" instead.'),
transient_for=self.parent_win)
else:
helpers.launch_browser_mailer('url', url)
def on_open_file_in_browser_menuitem_activate(self, menu, data):
if os.name == "nt":
filepath = "file://" + os.path.abspath(data["filepath"])
else:
filepath = "file://" + data["filepath"]
if app.config.get('autodetect_browser_mailer') \
or app.config.get('custombrowser') == '':
dialogs.ErrorDialog(
_('Cannot open downloaded file in browser'),
_('You have to set a custom browser executable '
'in your gajim settings for this to work.'),
transient_for=self.parent_win)
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
return
command = app.config.get('custombrowser')
command = helpers.build_command(command, filepath)
try:
helpers.exec_command(command)
except Exception:
pass
# Change mouse pointer to HAND2 when
# mouse enter the eventbox with the image
def on_enter_event(self, eb, event):
self.textview.tv.get_window(
Gtk.TextWindowType.TEXT).set_cursor(Gdk.Cursor(Gdk.CursorType.HAND2))
# Change mouse pointer to default when mouse leaves the eventbox
def on_leave_event(self, eb, event):
self.textview.tv.get_window(
Gtk.TextWindowType.TEXT).set_cursor(Gdk.Cursor(Gdk.CursorType.XTERM))
def on_button_press_event(self, eb, event, filepath,
original_filename, url, encrypted):
data = {"filepath": filepath,
"original_filename": original_filename,
"url": url,
"encrypted": encrypted}
# left click
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1:
method = getattr(self, "on_"
+ self.plugin.config['LEFTCLICK_ACTION']
+ "_activate")
method(event, data)
# right klick
elif event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
menu = self.make_rightclick_menu(event, data)
#menu.attach_to_widget(self.tv, None)
#menu.popup(None, None, None, event.button, event.time)
menu.popup_at_pointer(event)
def disconnect_from_chat_control(self):
pass