| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
############################################################################### |
| 4 |
# |
| 5 |
# LegacyClient Example |
| 6 |
# (c) Nicholas Perez 2006 - 2009. |
| 7 |
# Licensed under GPLv2 |
| 8 |
# |
| 9 |
# Please see the included |
| 10 |
# LICENSE file for details |
| 11 |
# |
| 12 |
# This example client script, instantiates a single PCJ object, connects to a |
| 13 |
# remote server, sends presence, and then begins sending messages to itself on |
| 14 |
# a small random interval |
| 15 |
# |
| 16 |
############################################################################### |
| 17 |
|
| 18 |
use warnings; |
| 19 |
use strict; |
| 20 |
|
| 21 |
use 5.010; |
| 22 |
use POE; #include POE constants |
| 23 |
use POE::Component::Jabber; #include PCJ |
| 24 |
use POE::Filter::XML::Node; #include to build nodes |
| 25 |
use Carp; |
| 26 |
|
| 27 |
# First we create our own session within POE to interact with PCJ |
| 28 |
POE::Session->create( |
| 29 |
options => { debug => 1, trace => 1}, |
| 30 |
inline_states => { |
| 31 |
_start => |
| 32 |
sub |
| 33 |
{ |
| 34 |
my ($kernel, $heap) = @_[KERNEL, HEAP]; |
| 35 |
$kernel->alias_set('Tester'); |
| 36 |
|
| 37 |
# Our PCJ instance is a fullblown object we should store |
| 38 |
# so we can access various bits of data during use |
| 39 |
|
| 40 |
$heap->{'component'} = |
| 41 |
POE::Component::Jabber->new( |
| 42 |
IP => 'localhost', |
| 43 |
Port => '5222', |
| 44 |
Hostname => 'localhost', |
| 45 |
Username => 'test01', |
| 46 |
Password => 'test01', |
| 47 |
Alias => 'COMPONENT', |
| 48 |
|
| 49 |
# Shown below are the various connection types included |
| 50 |
# from POE::Component::Jabber: |
| 51 |
|
| 52 |
# LEGACY is for pre-XMPP/Jabber connections |
| 53 |
# XMPP is for XMPP1.0 compliant connections |
| 54 |
# JABBERD14_COMPONENT is for connecting as a service on the |
| 55 |
# backbone of a jabberd1.4.x server |
| 56 |
# JABBERD20_COMPONENT is for connecting as a service on the |
| 57 |
# backbone of a jabberd2.0.x server |
| 58 |
|
| 59 |
ConnectionType => +LEGACY, |
| 60 |
#ConnectionType => +XMPP, |
| 61 |
#ConnectionType => +JABBERD14_COMPONENT, |
| 62 |
#ConnectionType => +JABBERD20_COMPONENT, |
| 63 |
Debug => '1', |
| 64 |
|
| 65 |
); |
| 66 |
|
| 67 |
# POE::Component::Jabber now uses POE::Component::PubSub to |
| 68 |
# manage event reporting including incoming packets. So in order |
| 69 |
# to get anything out of POE::Component::Jabber we need to |
| 70 |
# subscribe to the various events of which we have interest. |
| 71 |
|
| 72 |
# You can see a whole list of potential events (including |
| 73 |
# possible error states, but seeing the |
| 74 |
# POE::Component::Jabber::Events documentation. |
| 75 |
|
| 76 |
# PCJ_READY: Let's us know the connection is up and all of the |
| 77 |
# various layers of the protocol have been established. |
| 78 |
$kernel->post('COMPONENT', 'subscribe', +PCJ_READY, 'MyReadyEvent'); |
| 79 |
|
| 80 |
# PCJ_NODERECEIVED: Fires everytime we get a node down the pipe |
| 81 |
$kernel->post('COMPONENT', 'subscribe', +PCJ_NODERECEIVED, 'MyReceivedEvent'); |
| 82 |
|
| 83 |
# We could subscribe to all of the various error conditions or |
| 84 |
# even all of the various steps along the way so we could |
| 85 |
# report the status of the connection as it is building. But |
| 86 |
# for simplicity sake, this example will only cover the bare |
| 87 |
# minimum to get a connection up and running. |
| 88 |
|
| 89 |
# At this point, we have subscribed to the events we want and |
| 90 |
# are ready to tell the component to connect to the server |
| 91 |
|
| 92 |
$kernel->post('COMPONENT', 'connect'); |
| 93 |
|
| 94 |
}, |
| 95 |
|
| 96 |
_stop => |
| 97 |
sub |
| 98 |
{ |
| 99 |
$_[KERNEL]->alias_remove('Tester'); |
| 100 |
}, |
| 101 |
|
| 102 |
# This is the event with used to subscribe to the PCJ_READY event. |
| 103 |
# It will fire anytime a connection is fully initialized and ready for |
| 104 |
# use. It passes no arguments. |
| 105 |
MyReadyEvent => |
| 106 |
sub |
| 107 |
{ |
| 108 |
say '--- Connection is ready for use! ---'; |
| 109 |
|
| 110 |
# Now will we will send presence |
| 111 |
my $presence = POE::Filter::XML::Node->new('presence'); |
| 112 |
|
| 113 |
# The stored POE::Component::Jabber object has a number of |
| 114 |
# useful methods we can use outside of POE event posting, |
| 115 |
# including jid() |
| 116 |
$presence->setAttribute('from', $_[HEAP]->{'component'}->jid()); |
| 117 |
|
| 118 |
# Some of the event names have changed since the 2.x series. |
| 119 |
# 'output_handler' was replaced by plain old 'output' |
| 120 |
$_[KERNEL]->post('COMPONENT', 'output', $presence); |
| 121 |
|
| 122 |
# Now let's send ourselves some messages |
| 123 |
$_[KERNEL]->yield('MyMessageSendEvent'); |
| 124 |
}, |
| 125 |
|
| 126 |
# This is our event with which we subscribed to the PCJ_NODERECEIVED |
| 127 |
# event. Once the connection is up and running, our event will be |
| 128 |
# called once for every node received. ARG0 will contain the node |
| 129 |
MyReceivedEvent => |
| 130 |
sub |
| 131 |
{ |
| 132 |
say '--- Node received! ---'; |
| 133 |
say $_[ARG0]->toString(); |
| 134 |
say '----------------------'; |
| 135 |
|
| 136 |
}, |
| 137 |
|
| 138 |
# This is the event we call from our ready event to start send messages |
| 139 |
# to us. |
| 140 |
MyMessageSendEvent => |
| 141 |
sub |
| 142 |
{ |
| 143 |
my $message = POE::Filter::XML::Node->new |
| 144 |
( |
| 145 |
'message', |
| 146 |
[ |
| 147 |
'to', $_[HEAP]->{'component'}->jid() |
| 148 |
] |
| 149 |
); |
| 150 |
|
| 151 |
$_[KERNEL]->post('COMPONENT', 'output', $message); |
| 152 |
$_[KERNEL]->delay_set('MyMessageSendEvent', int(rand(6))); |
| 153 |
}, |
| 154 |
} |
| 155 |
); |
| 156 |
|
| 157 |
POE::Kernel->run(); |
| 158 |
|
| 159 |
exit 0; |