New DataFormWidget: Add Captcha support
This commit is contained in:
parent
4be4998704
commit
7f1dfe5a8c
|
@ -57,6 +57,7 @@ config = c_config.Config()
|
|||
version = gajim.__version__
|
||||
connections = {} # type: Dict[str, ConnectionT]
|
||||
avatar_cache = {} # type: Dict[str, Dict[str, Any]]
|
||||
bob_cache = {} # type: Dict[str, bytes]
|
||||
ipython_window = None
|
||||
app = None # Gtk.Application
|
||||
|
||||
|
@ -668,3 +669,14 @@ def get_win_debug_mode() -> bool:
|
|||
debug_folder = Path(configpaths.get('DEBUG'))
|
||||
debug_enabled = debug_folder / 'debug-enabled'
|
||||
return debug_enabled.exists()
|
||||
|
||||
def get_stored_bob_data(algo_hash: str) -> Optional[bytes]:
|
||||
try:
|
||||
return bob_cache[algo_hash]
|
||||
except KeyError:
|
||||
filepath = Path(configpaths.get('BOB')) / algo_hash
|
||||
if filepath.exists():
|
||||
with open(str(filepath), 'r+b') as file:
|
||||
data = file.read()
|
||||
return data
|
||||
return None
|
||||
|
|
|
@ -229,6 +229,7 @@ class ConfigPaths:
|
|||
# Cache paths
|
||||
('CACHE_DB', 'cache.db', PathLocation.CACHE, PathType.FILE),
|
||||
('AVATAR', 'avatars', PathLocation.CACHE, PathType.FOLDER),
|
||||
('BOB', 'bob', PathLocation.CACHE, PathType.FOLDER),
|
||||
|
||||
# Config paths
|
||||
('MY_THEME', 'theme', PathLocation.CONFIG, PathType.FOLDER),
|
||||
|
|
|
@ -15,9 +15,15 @@
|
|||
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import hashlib
|
||||
from base64 import b64decode
|
||||
from pathlib import Path
|
||||
|
||||
import nbxmpp
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common import configpaths
|
||||
|
||||
log = logging.getLogger('gajim.c.m.bob')
|
||||
|
||||
|
||||
|
@ -91,5 +97,78 @@ class BitsOfBinary:
|
|||
iq, self._on_bob_received, {'cid': cid})
|
||||
|
||||
|
||||
def parse_bob_data(stanza):
|
||||
data_node = stanza.getTag('data', namespace=nbxmpp.NS_BOB)
|
||||
if data_node is None:
|
||||
return
|
||||
|
||||
cid = data_node.getAttr('cid')
|
||||
type_ = data_node.getAttr('type')
|
||||
max_age = data_node.getAttr('max-age')
|
||||
if max_age is not None:
|
||||
try:
|
||||
max_age = int(max_age)
|
||||
except Exception:
|
||||
log.exception(stanza)
|
||||
return
|
||||
|
||||
if cid is None or type_ is None:
|
||||
log.warning('Invalid data node (no cid or type attr): %s', stanza)
|
||||
return
|
||||
|
||||
try:
|
||||
algo_hash = cid.split('@')[0]
|
||||
algo, hash_ = algo_hash.split('+')
|
||||
except Exception:
|
||||
log.exception('Invalid cid: %s', stanza)
|
||||
return
|
||||
|
||||
bob_data = data_node.getData()
|
||||
if not bob_data:
|
||||
log.warning('No data found: %s', stanza)
|
||||
return
|
||||
|
||||
filepath = Path(configpaths.get('BOB')) / algo_hash
|
||||
if algo_hash in app.bob_cache or filepath.exists():
|
||||
log.info('BoB data already cached')
|
||||
return
|
||||
|
||||
try:
|
||||
bob_data = b64decode(bob_data)
|
||||
except Exception:
|
||||
log.warning('Unable to decode data')
|
||||
log.exception(stanza)
|
||||
return
|
||||
|
||||
if len(bob_data) > 10000:
|
||||
log.warning('%s: data > 10000 bytes', stanza.getFrom())
|
||||
return
|
||||
|
||||
try:
|
||||
sha = hashlib.new(algo)
|
||||
except ValueError as error:
|
||||
log.warning(stanza)
|
||||
log.warning(error)
|
||||
return
|
||||
|
||||
sha.update(bob_data)
|
||||
if sha.hexdigest() != hash_:
|
||||
log.warning('Invalid hash: %s', stanza)
|
||||
return
|
||||
|
||||
if max_age == 0:
|
||||
app.bob_cache[algo_hash] = bob_data
|
||||
else:
|
||||
try:
|
||||
with open(str(filepath), 'w+b') as file:
|
||||
file.write(bob_data)
|
||||
except Exception:
|
||||
log.warning('Unable to save data')
|
||||
log.exception(stanza)
|
||||
return
|
||||
log.info('BoB data stored: %s', algo_hash)
|
||||
return filepath
|
||||
|
||||
|
||||
def get_instance(*args, **kwargs):
|
||||
return BitsOfBinary(*args, **kwargs), 'BitsOfBinary'
|
||||
|
|
|
@ -26,6 +26,7 @@ from gajim.common.nec import NetworkIncomingEvent, NetworkEvent
|
|||
from gajim.common.modules.security_labels import parse_securitylabel
|
||||
from gajim.common.modules.user_nickname import parse_nickname
|
||||
from gajim.common.modules.carbons import parse_carbon
|
||||
from gajim.common.modules.bits_of_binary import parse_bob_data
|
||||
from gajim.common.modules.misc import parse_delay
|
||||
from gajim.common.modules.misc import parse_eme
|
||||
from gajim.common.modules.misc import parse_correction
|
||||
|
@ -228,6 +229,8 @@ class Message:
|
|||
timestamp = time.time()
|
||||
delayed = False
|
||||
|
||||
parse_bob_data(event.stanza)
|
||||
|
||||
event_attr = {
|
||||
'popup': False,
|
||||
'msg_log_id': None,
|
||||
|
|
|
@ -20,6 +20,7 @@ import weakref
|
|||
import nbxmpp
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common.modules.bits_of_binary import parse_bob_data
|
||||
|
||||
log = logging.getLogger('gajim.c.m.register')
|
||||
|
||||
|
@ -125,6 +126,7 @@ class Register:
|
|||
error_cb()(error)
|
||||
else:
|
||||
log.info('Register form received')
|
||||
parse_bob_data(stanza.getQuery())
|
||||
form = stanza.getQuery().getTag('x', namespace=nbxmpp.NS_DATA)
|
||||
is_form = form is not None
|
||||
if not is_form:
|
||||
|
|
|
@ -250,3 +250,6 @@ button.flat.link { padding: 0; border: 0; }
|
|||
.data-form-widget grid { margin: 18px; }
|
||||
.data-form-widget treeview { padding: 5px; }
|
||||
.data-form-widget scrolledwindow { border: 1px solid; border-color:@unfocused_borders; }
|
||||
|
||||
/*Image Preview*/
|
||||
.preview-image { box-shadow: 0px 0px 3px 0px alpha(@theme_text_color, 0.2); }
|
||||
|
|
|
@ -17,6 +17,9 @@ from gi.repository import GLib
|
|||
from gi.repository import GObject
|
||||
from gi.repository import Pango
|
||||
|
||||
from gajim.gtkgui_helpers import scale_pixbuf_from_data
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common.i18n import _
|
||||
|
||||
|
||||
|
@ -82,21 +85,21 @@ class FormGrid(Gtk.Grid):
|
|||
|
||||
if form_node.title is not None:
|
||||
self.title = form_node.title
|
||||
self.add_row(Title(form_node.title))
|
||||
self._add_row(Title(form_node.title))
|
||||
if form_node.instructions is not None:
|
||||
self.instructions = form_node.instructions
|
||||
self.add_row(Instructions(form_node.instructions))
|
||||
self._add_row(Instructions(form_node.instructions))
|
||||
|
||||
self.analyse_fields(form_node, options)
|
||||
self.parse_form(form_node, options)
|
||||
self._analyse_fields(form_node, options)
|
||||
self._parse_form(form_node, options)
|
||||
|
||||
def add_row(self, field):
|
||||
def _add_row(self, field):
|
||||
field.add(self, self.row_count)
|
||||
self.row_count += 1
|
||||
self.rows.append(field)
|
||||
|
||||
@staticmethod
|
||||
def analyse_fields(form_node, options):
|
||||
def _analyse_fields(form_node, options):
|
||||
if 'right_align' in options:
|
||||
# Dont overwrite option
|
||||
return
|
||||
|
@ -113,13 +116,33 @@ class FormGrid(Gtk.Grid):
|
|||
|
||||
options['right_align'] = max(label_lengths) < 30
|
||||
|
||||
def parse_form(self, form_node, options):
|
||||
def _parse_form(self, form_node, options):
|
||||
for field in form_node.iter_fields():
|
||||
if field.type_ == 'hidden':
|
||||
continue
|
||||
|
||||
if field.media:
|
||||
if not self._add_media_field(field, options):
|
||||
# We dont understand this media element, ignore it
|
||||
continue
|
||||
|
||||
widget = self._fields[field.type_]
|
||||
self.add_row(widget(field, self, options))
|
||||
self._add_row(widget(field, self, options))
|
||||
|
||||
def _add_media_field(self, field, options):
|
||||
if not field.type_ in ('text-single', 'text-private', 'text-multi'):
|
||||
return False
|
||||
|
||||
for uri in field.media.uris:
|
||||
if not uri.type_.startswith('image/'):
|
||||
continue
|
||||
|
||||
if not uri.uri_data.startswith('cid'):
|
||||
continue
|
||||
|
||||
self._add_row(ImageMediaField(uri, self, options))
|
||||
return True
|
||||
return False
|
||||
|
||||
def validate(self, is_valid):
|
||||
value = self._data_form.is_valid() if is_valid else False
|
||||
|
@ -460,3 +483,23 @@ class TextMultiField(Field):
|
|||
def _changed(self, widget):
|
||||
self._field.value = widget.get_text(*widget.get_bounds(), False)
|
||||
self._validate()
|
||||
|
||||
|
||||
class ImageMediaField():
|
||||
def __init__(self, uri, form_grid, options):
|
||||
self._uri = uri
|
||||
self._form_grid = form_grid
|
||||
|
||||
filename = uri.uri_data.split(':')[1].split('@')[0]
|
||||
data = app.bob_cache.get(filename)
|
||||
if data is None:
|
||||
self._image = Gtk.Image()
|
||||
return
|
||||
|
||||
pixbuf = scale_pixbuf_from_data(data, 170)
|
||||
self._image = Gtk.Image.new_from_pixbuf(pixbuf)
|
||||
self._image.set_halign(Gtk.Align.CENTER)
|
||||
self._image.get_style_context().add_class('preview-image')
|
||||
|
||||
def add(self, form_grid, row_number):
|
||||
form_grid.attach(self._image, 1, row_number, 1, 1)
|
||||
|
|
|
@ -1,13 +1,45 @@
|
|||
from base64 import b64decode
|
||||
|
||||
from gi.repository import Gtk
|
||||
import nbxmpp
|
||||
|
||||
from gajim.gtk.dataform import DataFormWidget
|
||||
from gajim.common.modules.dataforms import extend_form
|
||||
from gajim.common.const import CSSPriority
|
||||
from gajim.common import app
|
||||
|
||||
from test.gtk import util
|
||||
util.load_style('gajim.css', CSSPriority.APPLICATION)
|
||||
|
||||
image = '''iVBORw0KGgoAAAANSUhEUgAAAIwAAAA8CAAAAACRYQ2XAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElNRQfiCwQXMiypK
|
||||
zsIAAAM4ElEQVRo3u1ZeVxUR7b+moZmE1AWFVDQNoIiEBFFxUHEXaJGQX0TnWhi0DhjYlQwbviEbOKOE9eIOjFGR8e44C4qcYkIssmiIMiO7Es3vXffe94ftxsaaUh0Zt689/ul/ulb59a5/VXVWb5TxSP832lG+B3M/wMwxr9plF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Y3y9MfwfzO5hO2
|
||||
v8AIg7mWYx8/rwAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTgtMTEtMDRUMjM6NTA6NDQrMDE6MDBAxMf7AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE4LTExLTA0VDIzOjUwOjQ0KzAxOjAwMZl/RwAAAABJRU5ErkJggolQTkcNChoKAAAADUlIRFIAAAAoAAAAPAEAAAAAP
|
||||
MLFTQAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAJiS0dEAAHdihOkAAAAB3RJTUUH4gsEFzIsqSs7CAAAABBJREFUGNNj+A8CDKMkjUkAKsYq5D2hXoMAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTgtM
|
||||
TEtMDRUMjM6NTA6NDQrMDE6MDBAxMf7AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE4LTExLTA0VDIzOjUwOjQ0KzAxOjAwMZl/RwAAAABJRU5ErkJggg=='''
|
||||
|
||||
app.bob_cache['sha1+8f35fef110ffc5df08d579a50083ff9308fb6242'] = b64decode(image)
|
||||
|
||||
FORM = '''
|
||||
<x xmlns='jabber:x:data' type='form'>
|
||||
<title>Bot Configuration</title>
|
||||
|
@ -66,9 +98,16 @@ FORM = '''
|
|||
<desc>Tell all your friends about your new bot!</desc>
|
||||
<required/>
|
||||
</field>
|
||||
<field var='ocr' type='text-single' label='Fill in what you see'>
|
||||
<media xmlns='urn:xmpp:media-element'>
|
||||
<uri type='image/png'>cid:sha1+8f35fef110ffc5df08d579a50083ff9308fb6242@bob.xmpp.org</uri>
|
||||
</media>
|
||||
<required/>
|
||||
</field>
|
||||
</x>
|
||||
'''
|
||||
|
||||
|
||||
class DataFormWindow(Gtk.Window):
|
||||
def __init__(self):
|
||||
Gtk.Window.__init__(self, title="Data Form Test")
|
||||
|
|
Loading…
Reference in New Issue