2019-09-02 11:47:13 +00:00
|
|
|
import datetime, socket, struct
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
DEFAULT_PORT = 64738
|
|
|
|
|
2019-09-17 09:45:11 +00:00
|
|
|
def _parse(s):
|
|
|
|
host, _, port = s.partition(":")
|
|
|
|
if port:
|
|
|
|
if not port.isdigit():
|
|
|
|
return None
|
|
|
|
else:
|
2019-09-17 11:23:05 +00:00
|
|
|
port = str(DEFAULT_PORT)
|
2019-09-17 09:45:11 +00:00
|
|
|
return "%s:%s" % (host, port)
|
|
|
|
|
2019-09-17 09:58:02 +00:00
|
|
|
SETTING = utils.FunctionSetting(_parse, "mumble-server",
|
2019-09-17 09:45:11 +00:00
|
|
|
"Set the mumble server for this channel",
|
2019-09-17 09:58:02 +00:00
|
|
|
example="example.com:%s" % DEFAULT_PORT)
|
|
|
|
|
|
|
|
@utils.export("channelset", SETTING)
|
|
|
|
@utils.export("serverset", SETTING)
|
2019-09-02 11:47:13 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
@utils.hook("received.command.mumble")
|
|
|
|
@utils.kwarg("help", "Get user and bandwidth stats for a mumble server")
|
2019-09-17 09:45:11 +00:00
|
|
|
@utils.kwarg("usage", "[server[:<port>]]")
|
2019-09-02 11:47:13 +00:00
|
|
|
def mumble(self, event):
|
2019-09-17 09:45:11 +00:00
|
|
|
server = None
|
2019-09-17 09:58:02 +00:00
|
|
|
if not event["args"]:
|
|
|
|
server = event["target"].get_setting("mumble-server",
|
|
|
|
event["server"].get_setting("mumble-server", None))
|
2019-09-17 09:45:11 +00:00
|
|
|
elif event["args"]:
|
|
|
|
server = event["args_split"][0]
|
|
|
|
if not server:
|
|
|
|
raise utils.EventError("Please provide a server")
|
|
|
|
|
|
|
|
server, _, port = server.partition(":")
|
2019-09-02 11:47:13 +00:00
|
|
|
if port:
|
|
|
|
if not port.isdigit():
|
|
|
|
raise utils.EventError("Port must be numeric")
|
|
|
|
port = int(port)
|
|
|
|
else:
|
|
|
|
port = DEFAULT_PORT
|
|
|
|
|
2019-12-13 06:37:57 +00:00
|
|
|
timestamp = datetime.datetime.utcnow()
|
|
|
|
ping_packet = struct.pack(">iQ", 0, 123)
|
2019-09-02 11:47:13 +00:00
|
|
|
s = socket.socket(type=socket.SOCK_DGRAM)
|
2019-09-02 13:18:27 +00:00
|
|
|
s.settimeout(5)
|
2019-09-02 13:13:36 +00:00
|
|
|
|
2019-09-02 13:18:27 +00:00
|
|
|
with utils.deadline():
|
|
|
|
try:
|
|
|
|
s.sendto(ping_packet, (server, port))
|
|
|
|
except socket.gaierror as e:
|
|
|
|
raise utils.EventError(str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
pong_packet = s.recv(24)
|
|
|
|
except socket.timeout:
|
|
|
|
raise utils.EventError(
|
|
|
|
"Timed out waiting for response from %s:%d"
|
|
|
|
% (server, port))
|
2019-09-02 11:47:13 +00:00
|
|
|
|
|
|
|
pong = struct.unpack(">bbbbQiii", pong_packet)
|
|
|
|
|
2019-09-02 12:12:24 +00:00
|
|
|
version = ".".join(str(v) for v in pong[1:4])
|
2019-12-13 06:37:57 +00:00
|
|
|
ping = (datetime.datetime.utcnow()-timestamp).total_seconds()*1000
|
2019-09-02 11:47:13 +00:00
|
|
|
users = pong[5]
|
|
|
|
max_users = pong[6]
|
|
|
|
bandwidth = pong[7]/1000 # kbit/s
|
|
|
|
|
2019-09-02 12:12:24 +00:00
|
|
|
event["stdout"].write(
|
2019-09-17 09:45:11 +00:00
|
|
|
"%s:%d (v%s): %d/%d users, %.1fms ping, %dkbit/s bandwidth"
|
|
|
|
% (server, port, version, users, max_users, ping, bandwidth))
|