-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtransform_username.py
executable file
·176 lines (141 loc) · 4.77 KB
/
transform_username.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
#!/usr/bin/env python3
import argparse
import os
import re
class Rule:
def __str__(self):
return f'{self.action}: {self.arg1}, {self.arg2}'
def load_rules(filename):
lines = open(filename).read().splitlines()
for line in lines:
l = line
if not l or l.startswith('#'):
continue
data = re.split('( from | to )', l)
if len(data) != 5:
print(data)
print(f'Invalid rule line: {l}')
continue
r = Rule()
r.action = data[0].strip()
r.arg1 = data[2].strip()
r.arg2 = data[4].strip()
yield r
def apply_rule(username, rule):
results = set({username})
once_sign = False
if rule.action == 'replace-any-case':
start_pos = 0
while True:
try:
index = username.lower().index(rule.arg1.lower(), start_pos)
except:
break
left, right = username[:start_pos], username[start_pos:]
right = right.lower().replace(rule.arg1.lower(), rule.arg2, 1)
results.add(left+right)
start_pos = index + 1
if index+len(rule.arg1) > len(username):
break
elif rule.action == 'replace':
start_pos = 0
while True:
try:
index = username.index(rule.arg1, start_pos)
except:
break
left, right = username[:start_pos], username[start_pos:]
right = right.replace(rule.arg1, rule.arg2, 1)
results.add(left+right)
start_pos = index + 1
if index+len(rule.arg1) > len(username):
break
elif rule.action == 'change-case':
for i in range(len(username)):
new_name = username
new_name = new_name[:i] + new_name[i].swapcase() + new_name[i+1:]
results.add(new_name)
elif rule.action == 'append':
if rule.arg2 == 'right' and not username.endswith(rule.arg1):
results.add(username + rule.arg1)
elif rule.arg2 == 'left' and not username.startswith(rule.arg1):
results.add(rule.arg1 + username)
elif rule.arg2 == 'both':
name = username
if not username.endswith(rule.arg1):
name = rule.arg1 + name
if not username.startswith(rule.arg1):
name = name + rule.arg1
results.add(name)
once_sign = True
elif rule.action == 'remove-pos':
pos = int(rule.arg1)
new_username = username[:pos] + username[pos+1:]
results.add(new_username)
once_sign = True
return results, once_sign
def process_rules_recursive(usernames, rules, results):
for username in usernames:
for r in rules:
res, once_sign = apply_rule(username, r)
new_res = res.difference(results)
if once_sign:
results = results | new_res
continue
if not new_res:
continue
for result in new_res:
rec_res = process_rules_recursive(set({result}), rules, results | new_res)
results = results | rec_res | new_res
return results
def process_rules(usernames, rules):
return process_rules_recursive(usernames, rules, usernames)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Username mutation script",
)
username_group = parser.add_mutually_exclusive_group(required=True)
username_group.add_argument(
'--username',
help="Username to mutate",
)
username_group.add_argument(
'--username-list',
help="Filename of username list to mutate",
)
username_group.add_argument(
'-I',
'--username-input',
action='store_true',
default=False,
help="Get usernames from stdin",
)
parser.add_argument(
'rule_filename',
type=str,
help="Rule to mutate (see rules directory)",
)
parser.add_argument(
'--remove-known',
default=False,
action='store_true',
help="Remove known usernames from output",
)
args = parser.parse_args()
if args.username:
usernames = set({args.username})
elif args.username_list:
usernames = set(open(args.username_list).read().splitlines())
else:
import sys
usernames = set()
for line in sys.stdin:
usernames.add(line.strip())
rules = list(load_rules(args.rule_filename))
new_usernames = process_rules(usernames, rules)
if args.remove_known:
usernames = new_usernames.difference(usernames)
else:
usernames = new_usernames
print('\n'.join(usernames))