forked from horazont/muchopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.py
191 lines (155 loc) · 4.34 KB
/
request.py
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
#!/usr/bin/env python3
import asyncio
import getpass
import logging
import shlex
import aioxmpp
import aioxmpp.rsm.xso
import muchopper.bot.xso as xso
def print_item(item):
flags = []
if not item.anonymity_mode or item.anonymity_mode.value != "semi":
flags.append("non-anon")
print("{:4d} {!r} ({}){}{}".format(
item.nusers,
item.name,
item.address,
" " if flags else "",
" ".join(flags),
))
if item.description or item.language:
parts = [" "]
if item.description:
parts.append(item.description)
if item.language:
if parts:
parts.append(" ")
parts.append("(primary language: {})".format(item.language))
print("".join(parts))
async def amain(args, password):
client = aioxmpp.Client(
args.local_jid,
aioxmpp.make_security_layer(password)
)
async with client.connected() as stream:
form_xso = (await stream.send(
aioxmpp.IQ(
to=args.service_jid,
type_=aioxmpp.IQType.GET,
payload=xso.Search()
)
)).form
form_obj = xso.SearchForm.from_xso(form_xso)
form_obj.query.value = " ".join(map(shlex.quote, args.query))
form_obj.order_by.value = args.order_by
if args.min_users is not None:
form_obj.min_users.value = args.min_users
request = xso.Search()
request.form = form_obj.render_reply()
request.rsm = aioxmpp.rsm.xso.ResultSetMetadata()
if args.request_page_size is not None:
request.rsm.max_ = args.request_page_size
nresults = 0
while args.fetch_up_to is None or nresults < args.fetch_up_to:
reply = await stream.send(aioxmpp.IQ(
to=args.service_jid,
type_=aioxmpp.IQType.GET,
payload=request
))
for item in reply.items:
print_item(item)
if not reply.items:
break
nresults += len(reply.items)
if reply.rsm.max_ and len(reply.items) < reply.rsm.max_:
break
request.rsm.after = aioxmpp.rsm.xso.After()
request.rsm.after.value = reply.rsm.last.value
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--search-address",
dest="search_address",
default=True,
action="store_true",
)
parser.add_argument(
"--no-search-address",
dest="search_address",
action="store_false",
)
parser.add_argument(
"--search-name",
dest="search_name",
default=True,
action="store_true",
)
parser.add_argument(
"--no-search-name",
dest="search_name",
action="store_false",
)
parser.add_argument(
"--search-description",
dest="search_description",
default=True,
action="store_true",
)
parser.add_argument(
"--no-search-description",
dest="search_description",
action="store_false",
)
parser.add_argument(
"--request-page-size",
type=int,
default=None,
)
parser.add_argument(
"--min-users",
type=int,
default=None,
)
parser.add_argument(
"-n", "--fetch-up-to",
type=int,
default=None,
)
parser.add_argument(
"-s", "--order-by",
default="nusers",
)
parser.add_argument(
"local_jid",
type=aioxmpp.JID.fromstr,
)
parser.add_argument(
"service_jid",
type=aioxmpp.JID.fromstr,
)
parser.add_argument(
"query",
nargs="*",
)
parser.add_argument(
"-v",
action="count",
default=0,
dest="verbosity",
help="Increase verbosity (up to -vvv)"
)
args = parser.parse_args()
password = getpass.getpass("Password for {}:".format(args.local_jid))
logging.basicConfig(
level={
0: logging.ERROR,
1: logging.WARNING,
2: logging.INFO,
}.get(args.verbosity, logging.DEBUG),
)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(amain(args, password))
finally:
loop.close()