Skip to content

Commit 9c93ca9

Browse files
committed
put the code into a proper Perl module
1 parent 614e6ab commit 9c93ca9

18 files changed

+317
-69
lines changed

Changes

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Revision history for Perl extension App::LolBot.
2+
3+
0.01 Fri Jan 20 12:32:51 2012
4+
- original version; created by h2xs 1.23 with options
5+
-XAn App::LolBot
6+

MANIFEST

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Changes
2+
Makefile.PL
3+
MANIFEST
4+
README
5+
t/App::LolBot.t
6+
lib/App/LolBot.pm
7+
lib/App/LolBot/Bot.pm
8+
lib/App/LolBot/Irc.pm
9+
lib/App/LolBot/Stats.pm
10+
lib/App/LolBot/User.pm
11+

Makefile.PL

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use 5.012003;
2+
use ExtUtils::MakeMaker;
3+
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
4+
# the contents of the Makefile that is written.
5+
WriteMakefile(
6+
NAME => 'App::LolBot::Bot',
7+
VERSION_FROM => 'lib/App/LolBot.pm', # finds $VERSION
8+
PREREQ_PM => {}, # e.g., Module::Name => 1.1
9+
($] >= 5.005 ? ## Add these new keywords supported since 5.005
10+
(ABSTRACT_FROM => 'lib/App/LolBot.pm', # retrieve abstract from module
11+
AUTHOR => 'Stéphanie Ouillon <[email protected]>') : ()),
12+
);

README

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
App::LolBot version 0.01
2+
=======================
3+
4+
This module provides some features to run an IRC bot.
5+
At the moment, it does not much more than connecting itself on a IRC chan, memorizing who's there or who joins
6+
and (killer feature) counting how many "lol" are said on the chan (by each user).
7+
8+
Features to come: more elaborate stats and a web interface
9+
10+
11+
INSTALLATION
12+
13+
To install this module type the following:
14+
15+
perl Makefile.PL
16+
make
17+
make test # does nothing, I'll re-implement the tests
18+
make install
19+
20+
DEPENDENCIES
21+
22+
This module requires these other modules and libraries:
23+
24+
#no dependencies at the moment
25+
26+
COPYRIGHT AND LICENCE
27+
28+
29+
Copyright (C) 2012 by Stéphani Ouillon
30+
31+
This library is free software; you can redistribute it and/or modify
32+
it under the same terms as Perl itself, either Perl version 5.12.3 or,
33+
at your option, any later version of Perl 5 you may have available.
34+
35+

bot.pl

-53
This file was deleted.

lib/App/LolBot.pm

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package App::LolBot;
2+
3+
use 5.012003;
4+
use strict;
5+
use warnings;
6+
7+
use App::LolBot::Bot;
8+
use App::LolBot::Irc;
9+
use App::LolBot::Stats;
10+
use App::LolBot::User;
11+
12+
require Exporter;
13+
14+
our @ISA = qw(Exporter);
15+
16+
# Items to export into callers namespace by default. Note: do not export
17+
# names by default without a very good reason. Use EXPORT_OK instead.
18+
# Do not simply export all your public functions/methods/constants.
19+
20+
# This allows declaration use App::LolBot ':all';
21+
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
22+
# will save memory.
23+
our %EXPORT_TAGS = ( 'all' => [ qw(
24+
25+
) ] );
26+
27+
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
28+
29+
our @EXPORT = qw(
30+
31+
);
32+
33+
our $VERSION = '0.01';
34+
35+
36+
# Preloaded methods go here.
37+
38+
1;
39+
__END__
40+
# Below is stub documentation for your module. You'd better edit it!
41+
42+
=head1 NAME
43+
44+
App::LolBot - Perl extension for blah blah blah
45+
46+
=head1 SYNOPSIS
47+
48+
use App::LolBot;
49+
blah blah blah
50+
51+
=head1 DESCRIPTION
52+
53+
Stub documentation for App::LolBot, created by h2xs. It looks like the
54+
author of the extension was negligent enough to leave the stub
55+
unedited.
56+
57+
Blah blah blah.
58+
59+
=head2 EXPORT
60+
61+
None by default.
62+
63+
64+
65+
=head1 SEE ALSO
66+
67+
Mention other useful documentation such as the documentation of
68+
related modules or operating system documentation (such as man pages
69+
in UNIX), or any relevant external documentation such as RFCs or
70+
standards.
71+
72+
If you have a mailing list set up for your module, mention it here.
73+
74+
If you have a web site set up for your module, mention it here.
75+
76+
=head1 AUTHOR
77+
78+
Stéphanie Ouillon E<lt>[email protected]<gt>
79+
80+
=head1 COPYRIGHT AND LICENSE
81+
82+
Copyright (C) 2012 by Stéphanie Ouillon
83+
84+
This library is free software; you can redistribute it and/or modify
85+
it under the same terms as Perl itself, either Perl version 5.12.3 or,
86+
at your option, any later version of Perl 5 you may have available.
87+
88+
89+
=cut

lib/App/LolBot/Bot.pm

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#! /usr/bin/env perl;
2+
package App::LolBot::Bot;
3+
use strict;
4+
use warnings;
5+
use App::LolBot::Irc;
6+
use App::LolBot::Stats;
7+
8+
9+
sub new {
10+
11+
my ($class) = @_;
12+
my ($this) = {
13+
'host' => "irc.minet.net",
14+
'port' => 6667,
15+
'nick' => "LolBot",
16+
'chan' => "#pourlesbots",
17+
};
18+
19+
bless($this, $class);
20+
return $this;
21+
}
22+
23+
sub run {
24+
25+
my ($this) = @_;
26+
27+
my $host = $this->{'host'};
28+
my $port = $this->{'port'};
29+
my $nick = $this->{'nick'};
30+
my $chan = $this->{'chan'};
31+
my $server = App::LolBot::Irc->new($host, $port);
32+
my $stats = App::LolBot::Stats->new();
33+
34+
while(1){
35+
36+
while(my ($prefix, $cmd, @args) = $server->recv()){
37+
38+
#Identify the bot on the server
39+
if ($cmd eq "NOTICE" && $args[0] eq "AUTH"){
40+
$server->send("NICK", $nick);
41+
$server->send("USER", ($nick, $nick, $host, ":$nick"));
42+
}
43+
44+
$server->send("JOIN", ($chan)) if ($cmd eq "376");
45+
46+
#Answer to the ping of the server to stay connected
47+
$server->send("PONG", ($args[@args-1])) if ($cmd eq "PING");
48+
49+
#Deal with new nicknames
50+
$stats->InitNickList(@args) if ($cmd eq "353");
51+
$stats->newJoin($prefix) if ($cmd eq "JOIN");
52+
$stats->changeNick($args[1]) if ($cmd eq "NICK");
53+
54+
55+
if ($cmd eq "PRIVMSG"){
56+
57+
my ($chan, $msg) = @args;
58+
my $userNick = "";
59+
if ($prefix =~ m/:(\w+)!/){
60+
$userNick = $1;
61+
}
62+
63+
$stats->recLol($userNick,$msg);
64+
}
65+
66+
$stats->log($cmd);
67+
}
68+
}
69+
}
70+
71+
1;

Irc.pm lib/App/LolBot/Irc.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Originally written by Guilhem Tiennot ##
22
## Excepting some slight committed changes ##
33

4-
package Irc;
4+
package App::LolBot::Irc;
55
use strict;
66
use warnings;
77
use IO::Socket;

Stats.pm lib/App/LolBot/Stats.pm

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
package Stats;
2-
use User;
1+
package App::LolBot::Stats;
2+
use App::LolBot::User;
33
use strict;
44
use warnings;
55
use POSIX qw(strftime);
@@ -80,7 +80,7 @@ sub log(){
8080
$present = TRUE;
8181
}
8282
}
83-
my $newNickObject = User->new($newNick);
83+
my $newNickObject = App::LolBot::User->new($newNick);
8484
push((@{$this->{'nickList'}}), $newNickObject) if ($present == FALSE);
8585
}
8686

@@ -111,36 +111,36 @@ sub printNickList{
111111

112112
sub InitNickList{
113113

114-
my ($foo, @nicklist) = @_;
115-
$stats->listNick($nicklist[0]);
116-
$stats->printNickList();
114+
my ($this, $foo, @nicklist) = @_;
115+
$this->listNick($nicklist[0]);
116+
$this->printNickList();
117117
}
118118

119119
sub newJoin{
120120

121-
my ($prefix) = @_;
121+
my ($this,$prefix) = @_;
122122
my $userNick = "";
123123
if ($prefix =~ m/:(.*)!/){
124124
$userNick = $1;
125125
}
126-
$stats->addNick($userNick);
127-
$stats->printNickList();
126+
$this->addNick($userNick);
127+
$this->printNickList();
128128
}
129129

130130
sub changeNick{
131131

132-
my ($userNick) = @_;
132+
my ($this,$userNick) = @_;
133133
if($userNick =~ m/:(.*)/){
134134
$userNick = $1;
135135
}
136-
$stats->addNick($userNick);
137-
$stats->printNickList();
136+
$this->addNick($userNick);
137+
$this->printNickList();
138138
}
139139

140140
sub recLol{
141141

142-
my ($userNick,$msg) = @_;
143-
my @nickList = (@{$stats->{'nickList'}});
142+
my ($this,$userNick,$msg) = @_;
143+
my @nickList = (@{$this->{'nickList'}});
144144
for (my $i=0; $i< @nickList; $i++){
145145
if ($userNick eq $nickList[$i]->{'name'}){
146146
$nickList[$i]->findLol($msg);

User.pm lib/App/LolBot/User.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package User;
1+
package App::LolBot::User;
22
use strict;
33
use warnings;
44

scripts/script.pl

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#! /usr/bin/env perl;
2+
3+
use App::LolBot::Bot;
4+
use strict;
5+
use warnings;
6+
7+
#use File::Basename 'dirname';
8+
#use File::Spec;
9+
10+
#use lib join '/', File::Spec->splitdir(dirname(__FILE__)), 'lib';
11+
#use lib join '/', File::Spec->splitdir(dirname(__FILE__)), '..', 'lib';
12+
13+
14+
#$ENV{LOLBOT_APP} ||= "LolBot";
15+
16+
my $bot = App::LolBot::Bot->new();
17+
$bot->run();

0 commit comments

Comments
 (0)