2009/Nodos/Dojo de programación/Documentación

De Hackmeeting

Código inicial

#!/usr/bin/perl
# este es un bot de irc preparado para hacer de moderador en asambleas

use warnings;
use strict;

package Bot;
use base 'Bot::BasicBot';

# Recibir mensages enviados en publico a un canal
sub said {
    my ($self, $msg) = @_;

    $self->say(channel => $msg->{channel}, body => "Poco me importa que " . $msg->{body});
}

# Recibir mensages enviados con /me
sub emoted {
    my ($self, $msg) = @_;

    $self->emote(channel => $msg->{channel}, body => "le importa un carajo que " . $msg->{body});
}


package main;

# información de conexion
my $nickname = 'moderador';
my $server   = 'irc.freenode.net';
my $port     = '6667';
my $channel  = '#hackmeeting';

my $bot = Bot->new(
    server    => $server,
    port      => $port,
    channels  => [$channel],
    nick      => $nickname,
);
$bot->run();

Codigo final

#!/usr/bin/perl
# este es un bot de irc preparado para hacer de moderador en asambleas

use warnings;
use strict;

package Bot;
use base 'Bot::BasicBot';
my @turnos;
my $currentUser='';

# Evento que se lanza cuando alguien escribe en el canal.                                   
sub said {
    my ($self, $msg) = @_;

    if ($msg->{body} eq "." && $msg->{who} eq $currentUser)  {
    	if (@turnos) {
	     $currentUser = shift(@turnos);
       	     $self->say(channel => $msg->{channel}, body => "Cambio de turno. Ahora le toca a $currentUser");
	} else {
	     $self->say(channel => $msg->{channel}, body => "No hay mas palabras pedidas.");
	}
	    
    }
    else{
    	if ($msg->{body} eq "."){
		$self->say(channel => $msg->{channel}, body => "No hackees el bot");
    	}
    }

    # El bot responde.
    $self->say(channel => $msg->{channel}, body => "Poco me importa que $msg->{who} diga $msg->{body}");
}

# Rutina para añadir un usuario a la lista de turnos
sub adduser {
    my ($self, $user, $channel) = @_;
    #Si nadie tiene la palabra, se le da la palabra directamente 
    if ($currentUser eq "") {
    	$self->say(channel => $channel, body => "$currentUser");
    	$currentUser = $user;
	$self->say(channel => $channel, body => "Tiene la palabra $currentUser");
	return;
    }
    
    foreach (@turnos) {
       if ($_ eq $user) {
       	   $self->say(channel => $channel, body => "No seas pesado");
	   return;
       }
    }
    push(@turnos, $user);
    $self->say(channel => $channel, body => "Turnos: @turnos");
}

# Evento que se lanza cuando alguien escribe con /me en el canal.
sub emoted {
    my ($self, $msg) = @_;

    # El bot escribe un /me.
    if ($msg->{body} eq 'pide palabra') {
        $self->say(channel => $msg->{channel}, body => "$msg->{who} ha pedido palabra");
	$self->adduser($msg->{who}, $msg->{channel});
        # push(@turnos, $msg->{who});        	
    } else {
        $self->emote(channel => $msg->{channel}, body => "se rasca la tripa cuando $msg->{who} $msg->{body}");
    }
}



package main;

# informacion de conexion
my $nickname = 'moderador';
my $server   = 'irc.localhost';
my $port     = '6667';
my $channel  = '#hackmeeting';

my $bot = Bot->new(
    server    => $server,
    port      => $port, 
    channels  => [$channel],			
    nick      => $nickname,
);

$bot->run();
wiki-navigation
project-navigation