Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 177 lines (140 sloc) 5.346 kb
a642bb9 Rocco Caputo Several changes, including 0.08_06 version; fixing the samples
authored
1 #!/usr/bin/perl -w
a52b725 Rocco Caputo Initial revision
authored
2
3 # This is a fake login prompt I wrote after noticing that someone's
4 # IRC 'bot was probing telnet whenever I joined a particular channel.
5 # It wasn't originally going to be a POE test, but it turns out to be
6 # a good exercise for wheel event renaming.
7
8 use strict;
0901706 Rocco Caputo Clean up paths to point to the new lib/ directory.
authored
9 use lib '../lib';
86e4125 Rocco Caputo Changed IO::Socket::INET uses to IO::Socket in samples. Cleaned up poin...
authored
10 use IO::Socket;
a52b725 Rocco Caputo Initial revision
authored
11
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
12 use POE qw(
13 Wheel::SocketFactory Wheel::ReadWrite Driver::SysRW
14 Filter::Line Filter::Stream
15 );
a52b725 Rocco Caputo Initial revision
authored
16
17 #==============================================================================
18 # This is the login state group.
19
20 #------------------------------------------------------------------------------
21 # Enter the "login" prompt state. Prompt user, and wait for input.
22
23 sub login_login_start {
24 my ($session, $heap) = @_[SESSION, HEAP];
25
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
26 print "Session ", $session->ID, " - entering login state\n";
a52b725 Rocco Caputo Initial revision
authored
27 # switch the output filter to stream
14049a4 Rocco Caputo samples cleanup pre-0.12
authored
28 $heap->{wheel}->set_output_filter( POE::Filter::Stream->new );
a52b725 Rocco Caputo Initial revision
authored
29 # switch the input event to login_input
81bfed1 Rocco Caputo ported wheel use from XyzState to XyzEvent
authored
30 $heap->{wheel}->event( InputEvent => 'login_input' );
a52b725 Rocco Caputo Initial revision
authored
31 # display the prompt
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
32 $heap->{wheel}->put('login: ');
a52b725 Rocco Caputo Initial revision
authored
33 }
34
35 sub login_login_input {
36 my ($kernel, $session, $heap, $input) = @_[KERNEL, SESSION, HEAP, ARG0];
37
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
38 print "Session ", $session->ID, " - received login input\n";
a52b725 Rocco Caputo Initial revision
authored
39
40 if ($input ne '') {
41 $kernel->yield('password_start');
42 }
43 else {
44 $kernel->yield('login_start');
45 }
46 }
47
48 #==============================================================================
49 # This is the password state group.
50
51 sub login_password_start {
52 my ($session, $heap) = @_[SESSION, HEAP];
53
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
54 print "Session ", $session->ID, " - entering password state\n";
a52b725 Rocco Caputo Initial revision
authored
55
56 # switch output filter to stream
14049a4 Rocco Caputo samples cleanup pre-0.12
authored
57 $heap->{wheel}->set_output_filter( POE::Filter::Stream->new );
a52b725 Rocco Caputo Initial revision
authored
58 # switch input event to password_input
81bfed1 Rocco Caputo ported wheel use from XyzState to XyzEvent
authored
59 $heap->{wheel}->event( InputEvent => 'password_input' );
a52b725 Rocco Caputo Initial revision
authored
60 # display the prompt
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
61 $heap->{wheel}->put('Password: ');
a52b725 Rocco Caputo Initial revision
authored
62 }
63
64 sub login_password_input {
65 my ($kernel, $session, $heap, $input) = @_[KERNEL, SESSION, HEAP, ARG0];
66
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
67 print "Session ", $session->ID, " - received password input\n";
a52b725 Rocco Caputo Initial revision
authored
68
69 # switch output filter to line
14049a4 Rocco Caputo samples cleanup pre-0.12
authored
70 $heap->{wheel}->set_output_filter( POE::Filter::Line->new );
a52b725 Rocco Caputo Initial revision
authored
71 # display the response
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
72 $heap->{wheel}->put('Login incorrect');
a52b725 Rocco Caputo Initial revision
authored
73 # move to the login state
74 $kernel->yield('login_start');
75 }
76
77 sub login_error {
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
78 my ($session, $heap, $operation, $errnum, $errstr) =
79 @_[SESSION, HEAP, ARG0, ARG1, ARG2];
a52b725 Rocco Caputo Initial revision
authored
80
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
81 $errstr = 'Client closed connection' unless $errnum;
a52b725 Rocco Caputo Initial revision
authored
82
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
83 print(
84 "Session ", $session->ID,
85 ": login: $operation error $errnum: $errstr\n"
86 );
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
87
88 delete $heap->{wheel};
a52b725 Rocco Caputo Initial revision
authored
89 }
90
91 #==============================================================================
92 # This is the main entry point for the login session.
93
94 sub login_session_start {
95 my ($kernel, $session, $heap, $handle, $peer_addr, $peer_port) =
96 @_[KERNEL, SESSION, HEAP, ARG0, ARG1, ARG2];
97
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
98 print "Session ", $session->ID, " - received connection\n";
a52b725 Rocco Caputo Initial revision
authored
99
100 # start reading and writing
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
101 $heap->{wheel} = POE::Wheel::ReadWrite->new(
102 'Handle' => $handle,
103 'Driver' => POE::Driver::SysRW->new,
104 'Filter' => POE::Filter::Line->new,
105 'ErrorEvent' => 'error',
106 );
a52b725 Rocco Caputo Initial revision
authored
107 # hello, world!\n
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
108 $heap->{wheel}->put('FreeBSD (localhost) (ttyp2)', '', '');
a52b725 Rocco Caputo Initial revision
authored
109 $kernel->yield('login_start');
110 }
111
112 sub login_session_create {
113 my ($handle, $peer_addr, $peer_port) = @_[ARG0, ARG1, ARG2];
114
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
115 POE::Session->create(
116 inline_states => {
117 _start => \&login_session_start,
118 # general error handler
119 error => \&login_error,
120 # login prompt states
121 login_start => \&login_login_start,
122 login_input => \&login_login_input,
123 # password prompt states
124 password_start => \&login_password_start,
125 password_input => \&login_password_input,
126 },
127 # start parameters
128 args => [ $handle, $peer_addr, $peer_port],
129 );
a52b725 Rocco Caputo Initial revision
authored
130 undef;
131 }
132
133 #==============================================================================
134
135 package main;
136
14049a4 Rocco Caputo samples cleanup pre-0.12
authored
137 my $port = shift;
138 if (not defined $port) {
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
139 print(
140 "*** This program listens on port 23 by default. You can change\n",
141 "*** the port by putting a new one on the command line. For\n",
142 "*** example, to listen on port 10023:\n",
143 "*** $0 10023\n",
144 );
14049a4 Rocco Caputo samples cleanup pre-0.12
authored
145 $port = 23;
146 }
a52b725 Rocco Caputo Initial revision
authored
147
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
148 POE::Session->create(
149 inline_states => {
150 '_start' => sub {
151 my $heap = $_[HEAP];
152
153 $heap->{wheel} = POE::Wheel::SocketFactory->new(
154 BindPort => $port,
155 SuccessEvent => 'socket_ok',
156 FailureEvent => 'socket_error',
157 Reuse => 'yes',
158 );
a52b725 Rocco Caputo Initial revision
authored
159 },
160
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
161 'socket_error' => sub {
162 my ($session, $heap, $operation, $errnum, $errstr) =
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
163 @_[SESSION, HEAP, ARG0, ARG1, ARG2];
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
164 print(
165 "Session ", $session->ID,
166 ": listener: $operation error $errnum: $errstr\n"
167 );
a52b725 Rocco Caputo Initial revision
authored
168 },
169
170 'socket_ok' => \&login_session_create,
ccd406e Rocco Caputo Remove POE::Session->new() from the tarball samples. Resolves
authored
171 },
172 );
a52b725 Rocco Caputo Initial revision
authored
173
174 $poe_kernel->run();
33b6717 Rocco Caputo Split Filter into InputFilter and OutputFilter
authored
175
a52b725 Rocco Caputo Initial revision
authored
176 __END__
Something went wrong with that request. Please try again.