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

code.py « get_iq « expansions - github.com/alkorgun/blacksmith-2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9fce7ce276d3321b5c170a6f34af5d5f1617fa7 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
# coding: utf-8

#  BlackSmith mark.2
exp_name = "get_iq" # /code.py v.x5
#  Id: 13~4b
#  Code © (2010-2011) by WitcherGeralt [alkorgun@gmail.com]

expansion_register(exp_name)

class expansion_temp(expansion):

	def __init__(self, name):
		expansion.__init__(self, name)

	def command_ping(self, ltype, source, instance, disp):
		if instance:
			source_ = instance
			if Chats.has_key(source[1]) and Chats[source[1]].isHere(instance):
				if Chats[source[1]].isHereTS(instance):
					conf_nick = (source[1], instance)
					instance, source_ = "%s/%s" % conf_nick, get_source(*conf_nick)
				else:
					Answer(self.AnsBase[5] % (instance), ltype, source, disp)
					raise iThr.ThrKill("exit")
		else:
			instance, source_ = source[0], get_source(source[1], source[2])
		iq = xmpp.Iq(to = instance, typ = Types[10])
		iq.addChild(Types[16], {}, [], xmpp.NS_PING)
		iq.setID("Bs-i%d" % Info["outiq"].plus())
		CallForResponse(disp, iq, self.answer_ping, {"ltype": ltype, "source": source, "instance": instance, "source_": source_, "start": time.time()})

	PingStat = {}

	def answer_ping(self, disp, stanza, ltype, source, instance, source_, start):
		if xmpp.isResultNode(stanza):
			answer = round(time.time() - start, 3)
			if source_:
				if not self.PingStat.has_key(source_):
					self.PingStat[source_] = []
				self.PingStat[source_].append(answer)
			Answer(self.AnsBase[0] % str(answer), ltype, source, disp)
		else:
			iq = xmpp.Iq(to = instance, typ = Types[10])
			iq.addChild(Types[18], {}, [], xmpp.NS_VERSION)
			iq.setID("Bs-i%d" % Info["outiq"].plus())
			CallForResponse(disp, iq, self.answer_ping_ver, {"ltype": ltype, "source": source, "instance": instance, "source_": source_, "start": time.time()})

	def answer_ping_ver(self, disp, stanza, ltype, source, instance, source_, start):
		if xmpp.isResultNode(stanza):
			answer = round(time.time() - start, 3)
			if source_:
				if not self.PingStat.has_key(source_):
					self.PingStat[source_] = []
				self.PingStat[source_].append(answer)
			Name = "[None]"
			for x in stanza.getQueryChildren():
				xname = x.getName()
				if xname == "name":
					Name = x.getData()
					break
			answer = self.AnsBase[1] % (Name, str(answer))
		else:
			answer = self.AnsBase[2]
		Answer(answer, ltype, source, disp)

	def command_ping_stat(self, ltype, source, source_, disp):
		if source_:
			if Chats.has_key(source[1]) and Chats[source[1]].isHere(source_):
				source_ = get_source(source[1], source_)
			else:
				source_ = source_.lower()
		else:
			source_ = get_source(source[1], source[2])
		if source_ and self.PingStat.has_key(source_):
			Number = float(sum(self.PingStat[source_]))
			len_ = len(self.PingStat[source_])
			max_ = max(self.PingStat[source_])
			min_ = min(self.PingStat[source_])
			if len_:
				answer = self.AnsBase[3] % (str(len_), str(min_), str(max_), str(round(Number / len_, 3)))
			else:
				answer = self.AnsBase[4]
		else:
			answer = self.AnsBase[4]
		Answer(answer, ltype, source, disp)

	def command_version(self, ltype, source, instance, disp):
		if Chats.has_key(source[1]):
			if instance:
				if Chats.has_key(source[1]) and Chats[source[1]].isHere(instance):
					if Chats[source[1]].isHereTS(instance):
						instance = "%s/%s" % (source[1], instance)
					else:
						Answer(self.AnsBase[5] % (instance), ltype, source, disp)
						raise iThr.ThrKill("exit")
			else:
				instance = source[0]
			iq = xmpp.Iq(to = instance, typ = Types[10])
			iq.addChild(Types[18], {}, [], xmpp.NS_VERSION)
			iq.setID("Bs-i%d" % Info["outiq"].plus())
			CallForResponse(disp, iq, self.answer_version, {"ltype": ltype, "source": source})
		else:
			Answer(AnsBase[0], ltype, source, disp)

	def answer_version(self, disp, stanza, ltype, source):
		if xmpp.isResultNode(stanza):
			Name, Ver, Os = "[None]", "[None]", "[None]"
			for x in stanza.getQueryChildren():
				xname = x.getName()
				if xname == "name":
					Name = x.getData()
				elif xname == "version":
					Ver = x.getData()
				elif xname == "os":
					Os = x.getData()
			answer = "\nName: %s\nVer.: %s\nOS: %s" % (Name, Ver, Os)
		else:
			answer = self.AnsBase[6]
		Answer(answer, ltype, source, disp)

	def command_uptime(self, ltype, source, server, disp):
		if not server:
			server = InstansesDesc[Gen_disp][0]
		iq = xmpp.Iq(to = server, typ = Types[10])
		iq.addChild(Types[18], {}, [], xmpp.NS_LAST)
		iq.setID("Bs-i%d" % Info["outiq"].plus())
		CallForResponse(disp, iq, self.answer_idle, {"ltype": ltype, "source": source, "instance": server, "typ": None})

	def command_idle(self, ltype, source, instance, disp):
		if instance:
			nick = instance
			if Chats.has_key(source[1]) and Chats[source[1]].isHere(instance):
				if Chats[source[1]].isHereTS(instance):
					instance = "%s/%s" % (source[1], instance)
				else:
					answer = self.AnsBase[5] % (instance)
			if not locals().has_key(Types[12]):
				iq = xmpp.Iq(to = instance, typ = Types[10])
				iq.addChild(Types[18], {}, [], xmpp.NS_LAST)
				iq.setID("Bs-i%d" % Info["outiq"].plus())
				CallForResponse(disp, iq, self.answer_idle, {"ltype": ltype, "source": source, "instance": nick, "typ": True})
		else:
			answer = AnsBase[1]
		if locals().has_key(Types[12]):
			Answer(answer, ltype, source, disp)

	def answer_idle(self, disp, stanza, ltype, source, instance, typ):
		if xmpp.isResultNode(stanza):
			seconds = stanza.getTagAttr(Types[18], "seconds")
			if seconds and seconds != "0" and isNumber(seconds):
				answer = (self.AnsBase[8] if typ else self.AnsBase[7]) % (instance, Time2Text(int(seconds)))
		if not locals().has_key(Types[12]):
			answer = self.AnsBase[6]
		Answer(answer, ltype, source, disp)

	affs = ("owner", "admin", "member", "outcast")

	def command_aflist(self, ltype, source, body, disp):

		def get_req(body):
			if DefLANG in ("RU", "UA"):
				affsRU = ["овнер", "админ", "мембер", "бан"]
				for name in affsRU:
					if body.count(name.decode("utf-8")):
						return self.affs[affsRU.index(name)]
			return (body if self.affs.count(body) else None)

		if Chats.has_key(source[1]):
			if body:
				ls = body.split()
				body = (ls.pop(0)).lower()
				if body in ("search", "искать".decode("utf-8")):
					if ls:
						data = (ls.pop(0)).lower()
						desc = {}
						for name in self.affs:
							iq = xmpp.Iq(to = source[1], typ = Types[10])
							query = xmpp.Node(Types[18])
							query.setNamespace(xmpp.NS_MUC_ADMIN)
							query.addChild("item", {aRoles[0]: name})
							iq.addChild(node = query)
							iq.setID("Bs-i%d" % Info["outiq"].plus())
							CallForResponse(disp, iq, self.answer_aflist_search, {"desc": desc, "name": name, "data": data})
						for x in xrange(60):
							time.sleep(0.2)
							if len(desc.keys()) == 4:
								break
						Number = itypes.Number()
						ls = []
						for name, matches in desc.iteritems():
							if matches:
								ls.append(name.capitalize() + "s:")
								for jid in matches:
									ls.append("%d) %s" % (Number.plus(), jid))
						if ls:
							Message(source[0], str.join(chr(10), ls), disp)
							if ltype == Types[1]:
								answer = AnsBase[11]
						else:
							answer = self.AnsBase[9]
					else:
						answer = AnsBase[2]
				else:
					body = get_req(body)
					if body:
						Numb = 0
						if ls:
							if isNumber(ls[0]):
								x = int(ls.pop(0))
								if x < 20:
									Numb = 20
								else:
									Numb = x
						iq = xmpp.Iq(to = source[1], typ = Types[10])
						query = xmpp.Node(Types[18])
						query.setNamespace(xmpp.NS_MUC_ADMIN)
						query.addChild("item", {aRoles[0]: body})
						iq.addChild(node = query)
						iq.setID("Bs-i%d" % Info["outiq"].plus())
						CallForResponse(disp, iq, self.answer_aflist, {"ltype": ltype, "source": source, "Numb": Numb})
					else:
						answer = AnsBase[2]
			else:
				answer = AnsBase[1]
		else:
			answer = AnsBase[0]
		if locals().has_key(Types[12]):
			Answer(answer, ltype, source, disp)

	def answer_aflist_search(self, disp, stanza, desc, name, data):
		if xmpp.isResultNode(stanza):
			count = []
			for node in stanza.getChildren():
				for node in node.getChildren():
					if node and node != "None":
						jid = node.getAttr("jid")
						if jid and jid.count(data):
							signature = node.getTagData("reason")
							if signature:
								jid = "%s (%s)" % (jid, signature)
							count.append(jid)
			desc[name] = count

	def answer_aflist(self, disp, stanza, ltype, source, Numb):
		if xmpp.isResultNode(stanza):
			ls, Number = [], itypes.Number()
			for node in stanza.getChildren():
				for node in node.getChildren():
					if node and node != "None":
						jid = node.getAttr("jid")
						if jid:
							if Numb and Numb <= Number._int():
								Number.plus()
							else:
								signature = node.getTagData("reason")
								if signature:
									jid = "%s (%s)" % (jid, signature)
								ls.append("%d) %s" % (Number.plus(), jid))
			if ls:
				if Numb and Numb < Number._int():
					ls.append("...\nTotal: %s items." % (Number._str()))
				Message(source[0], str.join(chr(10), ls), disp)
				if ltype == Types[1]:
					answer = AnsBase[11]
			else:
				answer = self.AnsBase[6]
		else:
			answer = self.AnsBase[6]
		if locals().has_key(Types[12]):
			Answer(answer, ltype, source, disp)

	commands = (
		(command_ping, "ping", 1,),
		(command_ping_stat, "pstat", 1,),
		(command_version, "version", 1,),
		(command_uptime, "uptime", 1,),
		(command_idle, "idle", 1,),
		(command_aflist, "list", 4,)
					)