2011-01-21 05:18:22 -05:00
|
|
|
# Copyright (c) 2008 Duncan Fordyce
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
|
|
|
""" contains helper functions for common irc commands """
|
|
|
|
|
2022-03-19 19:48:19 -04:00
|
|
|
import logging
|
2011-01-21 05:18:22 -05:00
|
|
|
import random
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
PchumLog = logging.getLogger("pchumLogger")
|
|
|
|
|
2022-09-06 07:26:14 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def msg(cli, user, msg):
|
2022-10-07 16:51:40 -04:00
|
|
|
for line in msg.split("\n"):
|
2011-01-21 05:18:22 -05:00
|
|
|
cli.send("PRIVMSG", user, ":%s" % line)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-02-03 01:20:37 -05:00
|
|
|
def names(cli, *channels):
|
2022-10-07 16:51:40 -04:00
|
|
|
tmp = __builtins__["list"](channels)
|
2011-02-03 01:20:37 -05:00
|
|
|
msglist = []
|
|
|
|
while len(tmp) > 0:
|
|
|
|
msglist.append(tmp.pop())
|
|
|
|
if len(",".join(msglist)) > 490:
|
|
|
|
tmp.append(msglist.pop())
|
|
|
|
cli.send("NAMES %s" % (",".join(msglist)))
|
|
|
|
msglist = []
|
|
|
|
if len(msglist) > 0:
|
|
|
|
cli.send("NAMES %s" % (",".join(msglist)))
|
2011-01-27 21:21:02 -05:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-02-04 16:17:27 -05:00
|
|
|
def channel_list(cli):
|
|
|
|
cli.send("LIST")
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-06-13 16:37:07 -04:00
|
|
|
def kick(cli, handle, channel, reason=""):
|
|
|
|
cli.send("KICK %s %s %s" % (channel, handle, reason))
|
2011-02-06 01:02:39 -05:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-02-06 19:50:21 -05:00
|
|
|
def mode(cli, channel, mode, options=None):
|
2021-08-24 09:49:50 -04:00
|
|
|
PchumLog.debug("mode = " + str(mode))
|
|
|
|
PchumLog.debug("options = " + str(options))
|
2011-02-06 19:50:21 -05:00
|
|
|
cmd = "MODE %s %s" % (channel, mode)
|
|
|
|
if options:
|
|
|
|
cmd += " %s" % (options)
|
|
|
|
cli.send(cmd)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-06-29 13:19:22 -04:00
|
|
|
def ctcp(cli, handle, cmd, msg=""):
|
2022-10-07 16:51:40 -04:00
|
|
|
# Space breaks protocol if msg is absent
|
|
|
|
if msg == "":
|
2022-06-02 17:15:45 -04:00
|
|
|
cli.send("PRIVMSG", handle, "\x01%s\x01" % (cmd))
|
|
|
|
else:
|
|
|
|
cli.send("PRIVMSG", handle, "\x01%s %s\x01" % (cmd, msg))
|
|
|
|
|
2011-06-29 13:19:22 -04:00
|
|
|
|
|
|
|
def ctcp_reply(cli, handle, cmd, msg=""):
|
|
|
|
notice(cli, str(handle), "\x01%s %s\x01" % (cmd.upper(), msg))
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-06-04 19:08:38 -04:00
|
|
|
def metadata(cli, target, subcommand, *params):
|
|
|
|
# IRC metadata draft specification
|
|
|
|
# https://gist.github.com/k4bek4be/92c2937cefd49990fbebd001faf2b237
|
|
|
|
cli.send("METADATA", target, subcommand, *params)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-06-05 20:05:00 -04:00
|
|
|
def cap(cli, subcommand, *params):
|
|
|
|
# Capability Negotiation
|
|
|
|
# https://ircv3.net/specs/extensions/capability-negotiation.html
|
|
|
|
cli.send("CAP", subcommand, *params)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def msgrandom(cli, choices, dest, user=None):
|
|
|
|
o = "%s: " % user if user else ""
|
|
|
|
o += random.choice(choices)
|
|
|
|
msg(cli, dest, o)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def _makeMsgRandomFunc(choices):
|
|
|
|
def func(cli, dest, user=None):
|
|
|
|
msgrandom(cli, choices, dest, user)
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
return func
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
|
|
|
msgYes = _makeMsgRandomFunc(["yes", "alright", "ok"])
|
|
|
|
msgOK = _makeMsgRandomFunc(["ok", "done"])
|
|
|
|
msgNo = _makeMsgRandomFunc(["no", "no-way"])
|
2011-01-21 05:18:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
def ns(cli, *args):
|
|
|
|
msg(cli, "NickServ", " ".join(args))
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def cs(cli, *args):
|
|
|
|
msg(cli, "ChanServ", " ".join(args))
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def identify(cli, passwd, authuser="NickServ"):
|
|
|
|
msg(cli, authuser, "IDENTIFY %s" % passwd)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2021-03-25 18:08:08 -04:00
|
|
|
def quit(cli, msg):
|
2022-07-05 15:45:16 -04:00
|
|
|
cli.send("QUIT %s" % (msg))
|
2011-01-21 05:18:22 -05:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-07-05 15:45:16 -04:00
|
|
|
def user(cli, username, realname):
|
2022-10-07 16:51:40 -04:00
|
|
|
cli.send("USER", username, "0", "*", ":" + realname)
|
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
|
|
|
|
_simple = (
|
2022-10-07 16:51:40 -04:00
|
|
|
"join",
|
|
|
|
"part",
|
|
|
|
"nick",
|
|
|
|
"notice",
|
|
|
|
"invite",
|
2011-01-21 05:18:22 -05:00
|
|
|
)
|
2022-10-07 16:51:40 -04:00
|
|
|
|
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def _addsimple():
|
|
|
|
import sys
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def simplecmd(cmd_name):
|
|
|
|
def f(cli, *args):
|
|
|
|
cli.send(cmd_name, *args)
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
return f
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
m = sys.modules[__name__]
|
|
|
|
for t in _simple:
|
|
|
|
setattr(m, t, simplecmd(t.upper()))
|
2022-10-07 16:51:40 -04:00
|
|
|
|
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
_addsimple()
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def _addNumerics():
|
|
|
|
import sys
|
|
|
|
from oyoyo import ircevents
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def numericcmd(cmd_num, cmd_name):
|
|
|
|
def f(cli, *args):
|
|
|
|
cli.send(cmd_num, *args)
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
return f
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
m = sys.modules[__name__]
|
2021-03-23 17:36:43 -04:00
|
|
|
for num, name in ircevents.numeric_events.items():
|
2011-01-21 05:18:22 -05:00
|
|
|
setattr(m, name, numericcmd(num, name))
|
2011-05-19 09:49:21 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
_addNumerics()
|