from untwisted.network import xmap, spawn

#logcon(con) used to print all text coming.

class SpawnCmd(object):
    """ Protocol class a.k.a plugin.
        Description:It spawns events based on what
        appears on an irc channel. This protocol runs on top of
        'PRIVMSG' event. It branches 'PRIVMSG' into n random events.
        These events are determined by what a user who is sitting on an
        irc channel issues.
        It is better explained bellow.
        Consider a person who is joined to an irc channel named #X. 
        On #X we have the scenary.

        <person>Hello.

        When person sends "Hello." This protocol class will spawn
        an event whose identifier is the string 'Hello.' So, if you have
        callbacks mapped to 'Hello.' They will be called with the exact
        arguments that 'PRIVMSG' carries. It happens so cause i 
        explicitly defined so.

        Dependencies:
        uxirc.irc 
    """

    def __init__(self, spin):
        xmap(spin, 'PRIVMSG', self.spawn_user_cmd)

    def spawn_user_cmd(self, spin, nick, user, host, target, msg):
        spawn(spin, msg, nick, user, host, target, msg)


class DebugCmd(object):
    """ 
    Protocol class.
    Description:It is used to debug SpawnCmd. It is kind of silly but it examplifies
    what we could do when we have more complex plugins/protocols.
    Description:It executes irc commands based on pattern matching.
    Dependencies:
    SpawnCmd


    Example:
    If you are on #&math and runs a paquitabot and installs DebugCmd
    in the connection you will have this printed on your terminal(if you are
    using logcon().

    foo issued.
    :blackdawn!~Euler@189-127-55-48.i-next.psi.br PRIVMSG #&math :foo
    bar issued.
    :blackdawn!~Euler@189-127-55-48.i-next.psi.br PRIVMSG #&math :bar
    
    Notice that you have a different order in which the texts are printed.
    It is the kind of thing that doesn't help much explaining through
    concepts. The best way to understand that it would be playing around with
    untwisted tools. Anyway i will try to explain it through concepts.
    If you take a look at logcon implementation you will notice it maps
    a callback to print out everything that FOUND event carries. The Shrug
    protocol is the protocol that spawns FOUND when it finds '\r\n' on the socket
    flow. The uxirc.irc protocol runs on top of FOUND as well as logcon(that might be
    seen as a protocol too) So, whenever FOUND is spawned by Shrug our uxirc.irc
    callbacks run and spawn events. Our DebugCmd and SpawnCmd are users of those
    events. So, it is like logcon callbacks are off the main branch of events.
    Since i first install Shrug, uxirc.irc etc in the irc_connect function after
    creating the socket it happens that the callback in logcon to print data carried
    by FOUND is printed on the screen after all events generated by uxirc.irc protocol.
    """

    def __init__(self, spin):
        xmap(spin, 'bar', self.output, 'bar issued.')
        xmap(spin, 'foo', self.output, 'foo issued.')

    def output(self, spin, *args):
        data = args[-1]

        # data will be either 'bar issued' or 'foo issued'.
        # This is how you can pass extra arguments to callbacks.
        print data

