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

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

"""
Module purpose is to receive and handle presences
"""

from __main__ import *
from __main__ import _

USERS_ON_INIT = set([])


def initializeUser(source, resource, prs):
	"""
	Initializes user for the first time after they connected
	"""
	logger.debug("Got a presence. Searching jid in the database. (jid: %s)", source)
	user = User(source)
	try:
		user.connect()
	except RuntimeError:
		pass
	except Exception as e:
		if not isinstance(e, api.TokenError):
			report(crashLog("user.connect"))
		sendMessage(source, TransportID,
			_("Auth failed! If this error repeated, "
				"please register again."
				" This incident will be reported.\nCause: %s") % returnExc())
		logger.error("Failed to authenicate user! Error: %s (jid: %s)", e.message, source)
	finally:
		if source in USERS_ON_INIT:
			USERS_ON_INIT.remove(source)
	else:
		user.initialize(send=True, resource=resource)  # probably we need to know resource a bit earlier than this time
		utils.runThread(executeHandlers, ("prs01", (source, prs)))


def presence_handler(cl, prs):
	pType = prs.getType()
	jidFrom = prs.getFrom()
	source = jidFrom.getStripped()
	resource = jidFrom.getResource()
	destination = prs.getTo().getStripped()
	if source in Users:
		user = Users[source]
		if pType in ("available", "probe", None):
			if destination == TransportID:
				if resource not in user.resources and user not in USERS_ON_INIT:
					logger.debug("Received presence %s from user. Calling sendInitPresence() (jid: %s)" % (pType, source))
					user.resources.add(resource)
					utils.runThread(user.sendInitPresence)

		elif pType == "unavailable":
			if destination == TransportID and resource in user.resources:
				user.resources.remove(resource)
				if user.resources:
					user.sendOutPresence(jidFrom)
			if not user.resources:
				sendPresence(source, TransportID, "unavailable")
				if Transport.settings.send_unavailable:
					user.sendOutPresence(source)
				try:
					user.vk.disconnect()
					del Users[source]
				except (AttributeError, KeyError):
					pass

		elif pType == "error":
			if prs.getErrorCode() == "404":
				user.vk.disconnect()

		elif pType == "subscribe":
			sendPresence(source, destination, "subscribed")
			if user.friends:
				id = vk2xmpp(destination)
				if id in user.friends:
					if user.friends[id]["online"]:
						sendPresence(source, destination, hash=USER_CAPS_HASH)
			if destination == TransportID:
				sendPresence(source, destination, hash=TRANSPORT_CAPS_HASH)

		elif pType == "unsubscribe":
			if destination == TransportID:
				removeUser(user, True, False)
				executeHandlers("evt09", (source,))

	elif pType in ("available", None) and destination == TransportID:
		# It's possible to receive more than one presence from @gmail.com
		if source not in USERS_ON_INIT:
			utils.runThread(initializeUser, args=(source, resource, prs))
			USERS_ON_INIT.add(source)
	utils.runThread(executeHandlers, ("prs01", (source, prs)))


MOD_TYPE = "presence"
MOD_HANDLERS = ((presence_handler, "", "", False),)
MOD_FEATURES = []