-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
203 lines (195 loc) · 6.52 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
from time import sleep
from requests import Session
from stdiomask import getpass
from sys import argv, exit
from random import getrandbits
# Define the functions here
def remove():
sleep(2)
input("Press enter to continue or ctrl+c to quit")
file = open('userinfo.cfg', 'r')
userdata = file.read()
file.close()
try:
if userdata == '-':
opusername = input("Operator Username: ")
fromheader = input("Bot Email: ")
else:
userdata.split(',')
opusername = userdata[1]
fromheader = userdata[2]
username = userdata[3]
log = input("Logged in to: " + str(userdata) + " - Confirm? Y/N: ")
if log == "N":
opusername = input("Operator Username: ")
fromheader = input("Bot Email: ")
username = input("Bot Username: ")
except IndexError:
opusername = input("Operator Username: ")
fromheader = input("Bot Email: ")
username = input("Bot Username: ")
headers = {
'User-Agent': 'BOT: ' + opusername + '@TestWikiAutoInactive-v1',
'From': fromheader
}
S = Session()
URL = "https://publictestwiki.com/w/api.php"
# Step 1: Retrieve a login token
PARAMS_1 = {
"action": "query",
"meta": "tokens",
"type": "login",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_1, headers=headers)
DATA = R.json()
LOGIN_TOKEN = DATA["query"]["tokens"]["logintoken"]
# Step 2: Send a post request to log in. See
# https://www.mediawiki.org/wiki/Manual:Bot_passwords
sleep(5) # wait 5s to avoid throttling
password = getpass()
PARAMS_2 = {
"action": "login",
"lgname": username,
"lgpassword": password,
"lgtoken": LOGIN_TOKEN,
"format": "json"
}
# destroy the password + replace with random hash
password = getrandbits(125)
R = S.post(URL, data=PARAMS_2, headers=headers)
PARAMS_AUTH = {
"action": "query",
"format": "json",
"meta": "userinfo",
"uiprop": "email"
}
authres = S.post(URL, data=PARAMS_AUTH, headers=headers)
EMAIL = authres.json()
EMAIL = EMAIL["query"]["userinfo"]["email"]
if fromheader == EMAIL:
print("Email Authenticated!")
else:
fromheader = EMAIL
print("Your email was replaced with " + fromheader)
headers = {
'User-Agent': 'BOT: ' + opusername + '@TestWikiAutoInactive-v1',
'From': fromheader # rewrite header to user email
}
configfile = open('userinfo.cfg', 'w+')
configfile.write(
',' +
opusername +
',' +
fromheader +
',' +
username +
',')
configfile.close()
sleep(5) # hold for 5s to avoid throttling
# Step 3: Obtain a Userrights token
PARAMS_3 = {
"action": "query",
"format": "json",
"meta": "tokens",
"type": "userrights"
}
R = S.get(url=URL, params=PARAMS_3, headers=headers)
DATA = R.json()
USERRIGHTS_TOKEN = DATA["query"]["tokens"]["userrightstoken"]
users = input("How many users are being removed? ")
userlist = []
count = 0
while count < int(users):
usertemp = input("User to remove: ")
userlist.append(usertemp)
count = count + 1
sleep(0.5)
count = 0
while count < len(userlist):
inactiveuser = userlist[count]
sleep(10) # wait 10 seconds before write api
# Step 4: Request to add or remove a user from a group
PARAMS_4 = {
"action": "userrights",
"format": "json",
"user": inactiveuser,
"remove": "bot|sysop|bureaucrat|consul|testgroup|autopatrolled|confirmed|rollbacker|interface-admin|flow-bot|checkuser|interwiki-admin|oversight|steward",
"reason": "per [[TestWiki:Inactivity|Inactivity report]]",
"token": USERRIGHTS_TOKEN
}
count = count + 1
R = S.post(URL, data=PARAMS_4, headers=headers)
DATA = R.json()
print(DATA)
sleep(2)
print('Generating mass message text..')
print('{{subst:Inactivity|user=' + opusername + '}}')
sleep(5)
print("Thanks for using! Good bye.")
exit()
def notify():
sleep(10)
consul = input("What is your username? ")
removedate = input("Removal date: ")
date = input("Today's Date: ")
users = input("How many users are being removed? ")
userlist = []
count = 0
while count < int(users):
usertemp = input("User to remove: ")
userlist.append(usertemp)
count = count + 1
sleep(0.5)
print("Generating mass message list....")
sleep(2)
count = 0
while count < len(userlist):
print("User Talk:" + str(userlist[count]))
count = count + 1
sleep(0.5)
print("Generating mass message text....")
sleep(2)
print("{{subst:InactiveReminder|DATE=" + removedate +
"|sig=~~~ on behalf of [[User:" + consul + "|" + consul + "]]}}")
print("Generating Community Noticeboard post")
sleep(2)
print("==Inactive Rights Removal - " + date + "==")
print(
"The rights of the following users will be removed on or after " +
removedate +
" if they do not return to activity:")
count = 0
while count < len(userlist):
print("*{{RFP/User|" + userlist[count] + "}}")
count = count + 1
print("")
print("Thanks,")
print(":~~~")
print(":For the Consul Team")
print(":~~~~~")
sleep(5)
print("Thanks for using! Good bye.")
try:
if argv[1] == 'remove':
print("Running Script in Remove Mode")
print("Welcome to the TestWiki:Inactivity Script")
print("This script may only be used by consuls")
print("Please ensure notifications were sent > 7 days ago and the users are still inacitve")
remove()
elif argv[1] == 'notify':
print("Running Script in Notify Mode")
print("Before we begin, please run the findInactive script on https://publictestwiki.com")
print("The notification process will begin in 10 seconds")
notify()
elif argv[1] == 'help':
print("Commands are:")
print("remove - Removes rights from inactive users")
print("notify - Generates messages for inactive users")
print("help - Displays this help page")
else:
print("Unknown command. For help use 'main.py help'.")
except IndexError as e:
print(e)
print("Please specify an action (remove, notify)")
exit()