1# Copyright (c) 2008 Duncan Fordyce
2# Permission is hereby granted, free of charge, to any person obtaining a copy
3# of this software and associated documentation files (the "Software"), to deal
4# in the Software without restriction, including without limitation the rights
5# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6# copies of the Software, and to permit persons to whom the Software is
7# furnished to do so, subject to the following conditions:
8# The above copyright notice and this permission notice shall be included in
9#  all copies or substantial portions of the Software.
10# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16# THE SOFTWARE.
17
18""" contains helper functions for common irc commands """
19
20import random
21
22
23def join(client, channel):
24    # Dummy function - replaced later
25    pass
26
27
28def part(client, channel):
29    # Dummy function - replaced later
30    pass
31
32
33def nick(client, nick):
34    # Dummy function - replaced later
35    pass
36
37
38def msg(cli, user, msg):
39    for line in msg.split('\n'):
40        cli.send("PRIVMSG", user, ":%s" % line)
41
42
43def msgrandom(cli, choices, dest, user=None):
44    o = "%s: " % user if user else ""
45    o += random.choice(choices)
46    msg(cli, dest, o)
47
48
49def _makeMsgRandomFunc(choices):
50    def func(cli, dest, user=None):
51        msgrandom(cli, choices, dest, user)
52    return func
53
54msgYes = _makeMsgRandomFunc(['yes', 'alright', 'ok'])
55msgOK = _makeMsgRandomFunc(['ok', 'done'])
56msgNo = _makeMsgRandomFunc(['no', 'no-way'])
57
58
59def ns(cli, *args):
60    msg(cli, "NickServ", " ".join(args))
61
62
63def cs(cli, *args):
64    msg(cli, "ChanServ", " ".join(args))
65
66
67def identify(cli, passwd, authuser="NickServ"):
68    msg(cli, authuser, "IDENTIFY %s" % passwd)
69
70
71def quit(cli, msg='gone'):
72    cli.send("QUIT :%s" % msg)
73    cli._end = 1
74
75
76def user(cli, username, realname=None):
77    cli.send("USER", username, cli.host, cli.host, realname or username)
78
79
80_simple = (
81    'join',
82    'part',
83    'nick',
84    'notice',
85)
86
87
88def _addsimple():
89    import sys
90    def simplecmd(cmd_name):
91        def f(cli, *args):
92            cli.send(cmd_name, *args)
93        return f
94    m = sys.modules[__name__]
95    for t in _simple:
96        setattr(m, t, simplecmd(t.upper()))
97
98
99_addsimple()
100
101
102# noinspection PyPep8Naming
103def _addNumerics():
104    import sys
105    from oyoyo import ircevents
106
107    def numericcmd(cmd_num, cmd_name):
108        def f(cli, *args):
109            cli.send(cmd_num, *args)
110        return f
111
112    m = sys.modules[__name__]
113    for num, name in ircevents.numeric_events.items():
114        setattr(m, name, numericcmd(num, name))
115
116
117_addNumerics()
118