nxy/bot/plugins/ctcp.py

82 lines
2.4 KiB
Python
Raw Normal View History

2017-05-15 22:26:10 +00:00
# -*- coding: utf-8 -*-
2017-05-16 10:06:29 +00:00
import time
2017-06-30 12:09:46 +00:00
import irc3
2017-05-15 22:26:10 +00:00
from docopt import Dict as DocOptDict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
2017-05-16 05:45:10 +00:00
@irc3.plugin
2017-05-15 22:26:10 +00:00
class CTCP(Plugin):
requires = ['irc3.plugins.async',
'irc3.plugins.command']
2017-05-15 22:26:10 +00:00
2017-07-07 00:11:20 +00:00
# noinspection PyMethodMayBeStatic
def _ctcp(self, name: str, nick: str, reply: str):
2017-08-16 12:39:44 +00:00
return '\x02[{}]\x02 {}: {}'.format(name.upper(), nick, reply)
2017-05-15 22:26:10 +00:00
2017-06-01 15:38:34 +00:00
async def ctcp(self, name: str, mask: IrcString, args: DocOptDict):
2017-05-15 22:26:10 +00:00
nick = args.get('<nick>') or mask.nick
2017-06-01 15:38:34 +00:00
name = name.upper()
data = await self.bot.ctcp_async(nick, name)
if not data or data['timeout']:
2017-05-15 22:26:10 +00:00
reply = 'timeout'
2017-06-01 15:38:34 +00:00
elif not data['success']:
2017-07-07 00:11:20 +00:00
reply = 'Error: {}'.format(data['reply'])
2017-05-15 22:26:10 +00:00
else:
2017-06-01 15:38:34 +00:00
reply = data['reply']
return self._ctcp(name, nick, reply)
2017-05-15 22:26:10 +00:00
@command
2017-08-16 12:39:44 +00:00
async def ping(self, mask: IrcString, target: IrcString, args: DocOptDict):
2017-05-16 12:27:34 +00:00
"""Sends ping via CTCP to user and sends the time needed
2017-05-15 22:26:10 +00:00
%%ping [<nick>]
"""
nick = args.get('<nick>') or mask.nick
2017-07-07 00:11:20 +00:00
data = await self.bot.ctcp_async(nick, 'PING {}'.format(time.time()))
2017-06-01 15:38:34 +00:00
if not data or data['timeout']:
2017-05-15 22:26:10 +00:00
reply = 'timeout'
2017-06-01 15:38:34 +00:00
elif not data['success']:
2017-07-07 00:11:20 +00:00
reply = 'Error: {}'.format(data['reply'])
2017-05-15 22:26:10 +00:00
else:
2017-06-01 15:38:34 +00:00
delta = time.time() - float(data['reply'])
2017-05-16 10:49:06 +00:00
if delta < 1.0:
delta *= 1000
unit = 'ms'
else:
unit = 's'
2017-07-07 00:11:20 +00:00
reply = '{0:.3f} {1}'.format(delta, unit)
2017-06-01 15:38:34 +00:00
return self._ctcp('PING', nick, reply)
2017-05-15 22:26:10 +00:00
@command
2017-08-16 12:39:44 +00:00
async def finger(self, mask: IrcString, target: IrcString, args: DocOptDict):
2017-05-16 12:27:34 +00:00
"""Gets the client response for finger nick user via CTCP
2017-05-15 22:26:10 +00:00
%%finger [<nick>]
"""
return await self.ctcp('FINGER', mask, args)
@command
2017-08-16 12:39:44 +00:00
async def time(self, mask: IrcString, target: IrcString, args: DocOptDict):
2017-05-16 12:27:34 +00:00
"""Gets the client time from nick via CTCP
2017-05-15 22:26:10 +00:00
%%time [<nick>]
"""
return await self.ctcp('TIME', mask, args)
@command
async def ver(self, mask: IrcString, target: IrcString, args: DocOptDict):
2017-05-16 12:27:34 +00:00
"""Gets the client version from nick via CTCP
2017-05-15 22:26:10 +00:00
%%ver [<nick>]
"""
return await self.ctcp('VERSION', mask, args)