forked from seveas/hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway_identify.py
29 lines (26 loc) · 1.01 KB
/
gateway_identify.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
import dns.exception
import dns.resolver
import dns.reversename
import re
import socket
import struct
import xchat
__module_name__ = "gateway_identify"
__module_description__ = "Print the IP of users joining from gateways"
__module_version__ = "1.0"
hex_ip_regex = re.compile(r'^~?[0-9a-f]{8}$')
def make_ip(s):
return socket.inet_ntoa(struct.pack('!I',eval('0x'+s)))
def identify_gateway(word, word_eol, userdata):
id = word[0][1:]
nick, ident, host = re.split('[@!]', id)
if not (hex_ip_regex.match(ident) and host.startswith('gateway/')):
return
channel = word[-1][1:]
ip = make_ip(ident[-8:])
try:
host = dns.resolver.query(dns.reversename.from_address(ip), 'PTR').response.answer[0][0].to_text()[:-1]
except (dns.resolver.NXDOMAIN, dns.exception.Timeout):
host = 'unknown hostname'
xchat.find_context(channel=channel).emit_print('Server Notice', "%s is coming from %s (%s)" % (nick, ip, host))
xchat.hook_server('JOIN', identify_gateway, priority=xchat.PRI_LOWEST)