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

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

from __main__ import *
from __main__ import _
import forms


@utils.threaded
def initializeUser(user, cl, iq, kwargs):
	result = iq.buildReply("result")
	connect = False
	resource = iq.getFrom().getResource()
	source = user.source
	try:
		connect = user.connect(**kwargs)
	except (api.TokenError, api.AuthError) as e:
		result = utils.buildIQError(iq, xmpp.ERR_NOT_AUTHORIZED, _(str(e) + " Try logging in by token."))
	else:
		if connect:
			user.initialize(resource=resource, first=True)
			executeHandlers("evt08", (source,))
		else:
			logger.error("user connection failed (jid: %s)" % source)
			result = utils.buildIQError(iq, xmpp.ERR_BAD_REQUEST, _("Incorrect password or access token!"))
	sender(cl, result)


@utils.safe
def register_handler(cl, iq):
	source = iq.getFrom().getStripped()
	destination = iq.getTo().getStripped()
	result = iq.buildReply("result")
	if USER_LIMIT:
		count = calcStats()[0]
		if count >= USER_LIMIT and source not in Users:
			sender(cl, utils.buildIQError(iq, xmpp.ERR_NOT_ALLOWED, _("The gateway admins limited registrations, sorry.")))
			raise xmpp.NodeProcessed()

	if not ALLOW_REGISTRATION and source not in Users:
		sender(cl, utils.buildIQError(iq, xmpp.ERR_NOT_ALLOWED, _("The gateway admins limited registrations, sorry.")))
		raise xmpp.NodeProcessed()

	if destination == TransportID and iq.getQueryChildren():
		phone, password, use_password, token, result = None, None, None, None, None
		query = iq.getTag("query")
		data = query.getTag("x", namespace=xmpp.NS_DATA)
		if data:
			form = xmpp.DataForm(node=data).asDict()
			phone = str(form.get("phone", "")).lstrip("+")
			password = str(form.get("password", ""))
			use_password = utils.normalizeValue(form.get("use_password", ""))  # In case here comes some unknown crap

			if not password:
				result = utils.buildIQError(iq, xmpp.ERR_BAD_REQUEST, _("The token/password field can't be empty!"))
			else:
				if use_password:
					logger.debug("user want to use a password (jid: %s)" % source)
					if not phone or phone == "+":
						result = utils.buildIQError(iq, xmpp.ERR_BAD_REQUEST, _("Phone is incorrect."))
				else:
					logger.debug("user won't use a password (jid: %s)" % source)
					token = password
					password = None
					# If not using a password, then we need to check if there a link or token. It's possible that user's wrong and that's a password.
					match = api.token_exp.search(token)
					if match:
						token = match.group(0)
					elif phone:
						password = token
					else:
						result = utils.buildIQError(iq, xmpp.ERR_NOT_AUTHORIZED, _("Fill the fields!"))

				# If phone or password (token)
				if token or (phone and password):
					user = User(source)
					initializeUser(user, cl, iq, {"username": phone, "password": password, "token": token})
					result = None

		elif query.getTag("remove"):
			logger.debug("user %s wants to remove me..." % source)
			if source in Users:
				user = Users[source]
				result = iq.buildReply("result")
				result.setPayload([], add=False)
				executeHandlers("evt09", (source,))

			elif findUserInDB(source):
				removeUser(source, True, False)
				sendPresence(TransportID, destination, "unsubscribe")
				executeHandlers("evt09", (source,))
	if result:
		sender(cl, result)


def sendRegisterForm(cl, iq):
	logger.debug("Sending registration form to user (jid: %s)", iq.getFrom().getStripped())
	form = utils.buildDataForm(fields=forms.Forms.getComlicatedForm(), data=[_("Fill the fields below")])
	result = iq.buildReply("result")
	result.setQueryPayload([form])
	sender(cl, result)


MOD_TYPE = "iq"
MOD_FEATURES = [xmpp.NS_DATA, xmpp.NS_REGISTER]
MOD_HANDLERS = ((register_handler, "set", xmpp.NS_REGISTER, False), (sendRegisterForm, "get", xmpp.NS_REGISTER, False))