| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import binascii |
| 4 |
import json |
| 5 |
import logging |
| 6 |
import re |
| 7 |
import sys |
| 8 |
from collections import defaultdict, namedtuple |
| 9 |
|
| 10 |
AnopeObject = namedtuple('AnopeObject', ('type', 'kv')) |
| 11 |
|
| 12 |
MASK_MAGIC_REGEX = re.compile(r'[*?!@]') |
| 13 |
|
| 14 |
def access_level_to_amode(level): |
| 15 |
# https://wiki.anope.org/index.php/2.0/Modules/cs_xop |
| 16 |
if level == 'QOP': |
| 17 |
return 'q' |
| 18 |
elif level == 'SOP': |
| 19 |
return 'a' |
| 20 |
elif level == 'AOP': |
| 21 |
return 'o' |
| 22 |
elif level == 'HOP': |
| 23 |
return 'h' |
| 24 |
elif level == 'VOP': |
| 25 |
return 'v' |
| 26 |
|
| 27 |
try: |
| 28 |
level = int(level) |
| 29 |
except: |
| 30 |
return None |
| 31 |
if level >= 10000: |
| 32 |
return 'q' |
| 33 |
elif level >= 9999: |
| 34 |
return 'a' |
| 35 |
elif level >= 5: |
| 36 |
return 'o' |
| 37 |
elif level >= 4: |
| 38 |
return 'h' |
| 39 |
elif level >= 3: |
| 40 |
return 'v' |
| 41 |
else: |
| 42 |
return None |
| 43 |
|
| 44 |
def to_unixnano(timestamp): |
| 45 |
return int(timestamp) * (10**9) |
| 46 |
|
| 47 |
def file_to_objects(infile): |
| 48 |
result = [] |
| 49 |
obj = None |
| 50 |
while True: |
| 51 |
line = infile.readline() |
| 52 |
if not line: |
| 53 |
break |
| 54 |
line = line.rstrip(b'\r\n') |
| 55 |
try: |
| 56 |
line = line.decode('utf-8') |
| 57 |
except UnicodeDecodeError: |
| 58 |
line = line.decode('utf-8', 'replace') |
| 59 |
logging.warning("line contained invalid utf8 data " + line) |
| 60 |
pieces = line.split(' ', maxsplit=2) |
| 61 |
if len(pieces) == 0: |
| 62 |
logging.warning("skipping blank line in db") |
| 63 |
continue |
| 64 |
if pieces[0] == 'END': |
| 65 |
result.append(obj) |
| 66 |
obj = None |
| 67 |
elif pieces[0] == 'OBJECT': |
| 68 |
obj = AnopeObject(pieces[1], {}) |
| 69 |
elif pieces[0] == 'DATA': |
| 70 |
obj.kv[pieces[1]] = pieces[2] |
| 71 |
elif pieces[0] == 'ID': |
| 72 |
# not sure what these do? |
| 73 |
continue |
| 74 |
else: |
| 75 |
raise ValueError("unknown command found in anope db", pieces[0]) |
| 76 |
return result |
| 77 |
|
| 78 |
ANOPE_MODENAME_TO_MODE = { |
| 79 |
'NOEXTERNAL': 'n', |
| 80 |
'TOPIC': 't', |
| 81 |
'INVITE': 'i', |
| 82 |
'NOCTCP': 'C', |
| 83 |
'AUDITORIUM': 'u', |
| 84 |
'SECRET': 's', |
| 85 |
} |
| 86 |
|
| 87 |
# verify that a certfp appears to be a hex-encoded SHA-256 fingerprint; |
| 88 |
# if it's anything else, silently ignore it |
| 89 |
def validate_certfps(certobj): |
| 90 |
certfps = [] |
| 91 |
for fingerprint in certobj.split(): |
| 92 |
try: |
| 93 |
dec = binascii.unhexlify(fingerprint) |
| 94 |
except: |
| 95 |
continue |
| 96 |
if len(dec) == 32: |
| 97 |
certfps.append(fingerprint) |
| 98 |
return certfps |
| 99 |
|
| 100 |
def convert(infile): |
| 101 |
out = { |
| 102 |
'version': 1, |
| 103 |
'source': 'anope', |
| 104 |
'users': defaultdict(dict), |
| 105 |
'channels': defaultdict(dict), |
| 106 |
} |
| 107 |
|
| 108 |
objects = file_to_objects(infile) |
| 109 |
|
| 110 |
lastmode_channels = set() |
| 111 |
|
| 112 |
for obj in objects: |
| 113 |
if obj.type == 'NickCore': |
| 114 |
username = obj.kv['display'] |
| 115 |
userdata = {'name': username, 'hash': obj.kv['pass'], 'email': obj.kv['email']} |
| 116 |
certobj = obj.kv.get('cert') |
| 117 |
if certobj: |
| 118 |
userdata['certfps'] = validate_certfps(certobj) |
| 119 |
out['users'][username] = userdata |
| 120 |
elif obj.type == 'NickAlias': |
| 121 |
username = obj.kv['nc'] |
| 122 |
nick = obj.kv['nick'] |
| 123 |
userdata = out['users'][username] |
| 124 |
if username.lower() == nick.lower(): |
| 125 |
userdata['registeredAt'] = to_unixnano(obj.kv['time_registered']) |
| 126 |
else: |
| 127 |
if 'additionalNicks' not in userdata: |
| 128 |
userdata['additionalNicks'] = [] |
| 129 |
userdata['additionalNicks'].append(nick) |
| 130 |
elif obj.type == 'ChannelInfo': |
| 131 |
chname = obj.kv['name'] |
| 132 |
founder = obj.kv['founder'] |
| 133 |
chdata = { |
| 134 |
'name': chname, |
| 135 |
'founder': founder, |
| 136 |
'registeredAt': to_unixnano(obj.kv['time_registered']), |
| 137 |
'topic': obj.kv['last_topic'], |
| 138 |
'topicSetBy': obj.kv['last_topic_setter'], |
| 139 |
'topicSetAt': to_unixnano(obj.kv['last_topic_time']), |
| 140 |
'amode': {founder: 'q',} |
| 141 |
} |
| 142 |
# DATA last_modes INVITE KEY,hunter2 NOEXTERNAL REGISTERED TOPIC |
| 143 |
last_modes = obj.kv.get('last_modes') |
| 144 |
if last_modes: |
| 145 |
modes = [] |
| 146 |
for mode_desc in last_modes.split(): |
| 147 |
if ',' in mode_desc: |
| 148 |
mode_name, mode_value = mode_desc.split(',', maxsplit=1) |
| 149 |
else: |
| 150 |
mode_name, mode_value = mode_desc, None |
| 151 |
if mode_name == 'KEY': |
| 152 |
chdata['key'] = mode_value |
| 153 |
else: |
| 154 |
modes.append(ANOPE_MODENAME_TO_MODE.get(mode_name, '')) |
| 155 |
chdata['modes'] = ''.join(modes) |
| 156 |
# prevent subsequent ModeLock objects from modifying the mode list further: |
| 157 |
lastmode_channels.add(chname) |
| 158 |
out['channels'][chname] = chdata |
| 159 |
elif obj.type == 'ModeLock': |
| 160 |
if obj.kv.get('set') != '1': |
| 161 |
continue |
| 162 |
chname = obj.kv['ci'] |
| 163 |
if chname in lastmode_channels: |
| 164 |
continue |
| 165 |
chdata = out['channels'][chname] |
| 166 |
modename = obj.kv['name'] |
| 167 |
if modename == 'KEY': |
| 168 |
chdata['key'] = obj.kv['param'] |
| 169 |
else: |
| 170 |
oragono_mode = ANOPE_MODENAME_TO_MODE.get(modename) |
| 171 |
if oragono_mode is not None: |
| 172 |
stored_modes = chdata.get('modes', '') |
| 173 |
stored_modes += oragono_mode |
| 174 |
chdata['modes'] = stored_modes |
| 175 |
elif obj.type == 'ChanAccess': |
| 176 |
chname = obj.kv['ci'] |
| 177 |
target = obj.kv['mask'] |
| 178 |
mode = access_level_to_amode(obj.kv['data']) |
| 179 |
if mode is None: |
| 180 |
continue |
| 181 |
if MASK_MAGIC_REGEX.search(target): |
| 182 |
continue |
| 183 |
chdata = out['channels'][chname] |
| 184 |
amode = chdata.setdefault('amode', {}) |
| 185 |
amode[target] = mode |
| 186 |
chdata['amode'] = amode |
| 187 |
|
| 188 |
# do some basic integrity checks |
| 189 |
for chname, chdata in out['channels'].items(): |
| 190 |
founder = chdata.get('founder') |
| 191 |
if founder not in out['users']: |
| 192 |
raise ValueError("no user corresponding to channel founder", chname, chdata.get('founder')) |
| 193 |
|
| 194 |
return out |
| 195 |
|
| 196 |
def main(): |
| 197 |
if len(sys.argv) != 3: |
| 198 |
raise Exception("Usage: anope2json.py anope.db output.json") |
| 199 |
with open(sys.argv[1], 'rb') as infile: |
| 200 |
output = convert(infile) |
| 201 |
with open(sys.argv[2], 'w') as outfile: |
| 202 |
json.dump(output, outfile) |
| 203 |
|
| 204 |
if __name__ == '__main__': |
| 205 |
logging.basicConfig() |
| 206 |
sys.exit(main()) |