Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mod_xhtml.py « modules - github.com/mrDoctorWho/vk4xmpp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d6c9e07c8172e96e445188e1f651b4ddbdb9198 (plain)
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
# coding: utf-8
# This file is a part of VK4XMPP transport
# © simpleApps, 2013 — 2015.

from __main__ import *
from __main__ import _
import random
import urllib


def sendPhoto(user, data, type, address, mType):
	mask = user.vk.permissions
	if mType == "chat_id":
		address = address.split("@")[0].split("#")[1]
		send = False
	else:
		destination = address
		address = vk2xmpp(address)
		send = True

	if address == TransportID:
		answer = _("Are you kidding me?")
	elif mask:
		if mask & 4 == 4:  # we have enough access?
			ext = type.split("/")[1]
			name = "vk4xmpp_%s.%s" % (random.randint(1000, 9000), ext)
			server = str(user.vk.method("photos.getMessagesUploadServer")["upload_url"])
			response = json.loads(user.vk.engine.post(
					server,
					user.vk.engine.multipart("photo", str(name), str(type), data),
					urlencode=False)[0])

			id = user.vk.method("photos.saveMessagesPhoto", response)
			if id:
				photo = "photo%(owner_id)d_%(id)s" % id[0]
				user.vk.sendMessage("", address, mType, {"attachment": photo})
				logger.debug("sendPhoto: image was successfully sent by user %s" % user.source)
				answer = _("Your image was successfully sent.")
			else:
				answer = _("Sorry but we have failed to send this image."
					" Seems you haven't enough permissions. Your token should be updated, register again.")
		else:
			answer = _("Sorry but we have failed to send this image."
					" Seems you haven't enough permissions. Your token should be updated, register again.")
	else:
		answer = _("Something went wrong. We are so sorry.")
	if send:
		sendMessage(user.source, destination, answer, timestamp=1)


def parseXHTML(user, html, source, destination, mType="user_id"):
	body = html.getTag("body")
	if body:
		# TODO: Maybe would be better if we use regular expressions?
		src = body.getTagAttr("img", "src")
		raw_data = src.split("data:")[1]
		mime_type = raw_data.split(";")[0]
		data = raw_data.split("base64,")[1]
		if data:
			try:
				data = urllib.unquote(data).decode("base64")
			except Exception:
				logger.error("xhmtlParse: fetched wrong xhtml image (jid: %s)" % source)
				return False
			utils.runThread(sendPhoto, (user, data, mime_type, destination, mType))
	return True


MOD_TYPE = ""
MOD_HANDLERS = ()
MOD_FEATURES = []