This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpserver.pl
111 lines (85 loc) · 2.55 KB
/
udpserver.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/perl
# NOTE: This script is an alternative to udpserver.py
# Do not use it unless udpserver.py fails
use warnings;
use strict;
use POE;
use IO::Socket::INET;
use POE::Component::IRC;
use constant DATAGRAM_MAXLEN => 1024;
select((select(STDOUT), $|=1)[0]);
# Initial variables
print "Nickname: ";
my $nickname = <>;
print "Channel: ";
my $channelname = <>;
print "Listen to UDP port: ";
my $udpportnumber = <>;
# Create the component that will represent an IRC network.
my ($irc) = POE::Component::IRC->spawn;
# Create the bot session. The new() call specifies the events the bot
# knows about and the functions that will handle those events.
POE::Session->create(
inline_states => {
_start => \&bot_start,
irc_001 => \&on_connect,
irc_public => \&on_public,
},
);
POE::Session->create(
inline_states => {
_start => \&server_start,
get_datagram => \&server_read,
}
);
$poe_kernel->run;
exit;
# UDP Server
sub server_start {
my $kernel = $_[KERNEL];
my $socket = IO::Socket::INET->new(
Proto => 'udp',
LocalPort => $udpportnumber,
);
die "Couldn't create server socket: $!" unless $socket;
$kernel->select_read( $socket, "get_datagram" );
}
sub server_read {
my ( $kernel, $socket ) = @_[ KERNEL, ARG0 ];
my $ircmessage = "";
recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 );
$message =~ /\[\[(.+)\]\]/s;
$ircmessage = $1;
$irc->yield( privmsg => $channelname, $ircmessage );
}
# IRC Server
# The bot session has started. Register this bot with the "magnet"
# IRC component. Select a nickname. Connect to a server.
sub bot_start {
my $kernel = $_[KERNEL];
my $heap = $_[HEAP];
my $session = $_[SESSION];
$irc->yield( register => "all" );
my $nick = $nickname;
$irc->yield( connect =>
{ Nick => $nick,
Username => $nickname,
Ircname => $nickname,
Server => 'irc.freenode.net',
Port => '6667',
}
);
}
# The bot has successfully connected to a server. Join a channel.
sub on_connect {
$irc->yield( join => $channelname );
}
# The bot has received a public message. Parse it for commands, and
# respond to interesting things.
sub on_public {
my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ];
my $nick = ( split /!/, $who )[0];
my $channel = $where->[0];
my $ts = localtime;
print " [$ts] <$nick:$channel> $msg\n";
}