m4pl1mp/plugins/isup_plugin.py
2022-02-01 22:44:42 -06:00

86 lines
4.2 KiB
Python

# -*- coding: utf-8 -*-
from irc3.plugins.command import command
import irc3
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
###########################################################################################
###########################################################################################
@irc3.plugin
class Plugin:
#######################################################################################
#######################################################################################
def __init__(self, bot):
self.bot = bot
#######################################################################################
#######################################################################################
def isup_check(self, domain_or_ip):
msg=''
try:
url=f'http://{domain_or_ip}'
try:
req = Request(
url,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
}
)
response=urlopen(req,timeout=10).read().decode('utf-8')
except HTTPError as e:
msg='maybe -> could not complete the request. error code: {}'.format(e.code)
except URLError as e:
msg='down -> failed to reach the server. reason: {}'.format(e.reason)
else:
msg=f'up -> http://{domain_or_ip} is communicating'
except:
url=f'https://{domain_or_ip}'
try:
req = Request(
url,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
}
)
response=urlopen(req,timeout=10).read().decode('utf-8')
except HTTPError as e:
msg='maybe -> could not complete the request. error code: {}'.format(e.code)
except URLError as e:
msg='down -> failed to reach the server. reason: {}'.format(e.reason)
else:
msg=f'up -> https://{domain_or_ip} is communicating'
finally:
return msg
#######################################################################################
#######################################################################################
@command(permission='view')
def isup(self, mask, target, args):
"""isup domain
%%isup <domain>
"""
domain=args.get('<domain>')
domain_noise=domain
try:
domain_noise=domain.split('://')[1]
except:
pass
try:
if not domain_noise:
domain_noise=domain[:domain.find('.')+domain[domain.find('.')+1:].find('/')+1].split('://')[1]
except:
if not domain_noise.find('/')==-1:
domain_noise=domain[:domain.find('.')+domain[domain.find('.')+1:].find('/')+1].split('://')[0]
if domain_noise[-1]=="/": domain_noise=domain_noise[:-1]
if domain_noise[:domain_noise.find(':')].replace('.','').isnumeric() and (len(domain_noise[:domain_noise.find(':')])-3)==len(domain_noise[:domain_noise.find(':')].replace('.','')):
domain=domain_noise
elif domain_noise.replace('.','').isnumeric() and (len(domain_noise)-3) == len(domain_noise.replace('.','')):
domain=domain_noise
elif domain_noise.find('/') == -1 and not domain_noise.find('.') == -1 and domain_noise.replace('.','').isalnum():
domain=domain_noise
else:
self.bot.privmsg(target,self.bot.emo("{}: doesn't sanitize towards a valid domain/ip".format(domain)))
return
self.bot.privmsg(target,self.bot.emo("{}".format(self.isup_check(domain))))
###########################################################################################
###########################################################################################