nxy/bot/plugins/ctcp.py

75 lines
2.1 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-08-22 13:57:57 +00:00
from docopt import Dict
2017-05-15 22:26:10 +00:00
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-08-22 12:56:29 +00:00
TIMEOUT = 5
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-08-22 13:57:57 +00:00
async def ctcp(self, name: str, mask: IrcString, args: Dict):
nick = args.get('<nick>', mask.nick)
2017-08-22 12:56:29 +00:00
data = await self.bot.ctcp_async(nick, name.upper(), self.TIMEOUT)
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
reply = data['reply']
return self._ctcp(name, nick, reply)
2017-05-15 22:26:10 +00:00
@command
2017-08-22 13:57:57 +00:00
async def ping(self, mask: IrcString, target: IrcString, args: Dict):
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>', 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
2017-08-22 12:56:29 +00:00
return self._ctcp('ping', nick, reply)
2017-05-15 22:26:10 +00:00
@command
2017-08-22 13:57:57 +00:00
async def time(self, mask: IrcString, target: IrcString, args: Dict):
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>]
"""
2017-08-22 12:56:29 +00:00
return await self.ctcp('time', mask, args)
2017-05-15 22:26:10 +00:00
@command
2017-08-22 13:57:57 +00:00
async def ver(self, mask: IrcString, target: IrcString, args: Dict):
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>]
"""
2017-08-22 12:56:29 +00:00
return await self.ctcp('version', mask, args)