📄 x437.htm
字号:
<HTML
><HEAD
><TITLE
>A Simple Script</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.64
"><LINK
REL="HOME"
TITLE="Programming Jabber"
HREF="book1.htm"><LINK
REL="UP"
TITLE="A Taste of Things to Come"
HREF="c324.htm"><LINK
REL="PREVIOUS"
TITLE="Imaginary Conversation"
HREF="x332.htm"><LINK
REL="NEXT"
TITLE="Inside Jabber"
HREF="c445.htm"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Programming Jabber</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x332.htm"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 1. A Taste of Things to Come</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c445.htm"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="JABTDG-CH-1-SECT-1.2"
>A Simple Script</A
></H1
><P
>Before moving on, let's have a look how simple it is to interact with
Jabber. <A
HREF="x437.htm#JABTDG-CH-1-EX-1"
>Example 1-1</A
> shows a simple Perl script
that connects to a Jabber server, authenticates, checks who's online,
and sends those people a reminder message. It uses the Net::Jabber
library, which provides a high-level API to many Jabber-related functions
such as handling the connection to the server (this is via another
library that Net::Jabber uses—XML::Stream), authentication, events,
and all the mechanisms to parse and create Jabber traffic.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="JABTDG-CH-1-EX-1"
></A
><P
><B
>Example 1-1. A simple Jabber script</B
></P
><PRE
CLASS="SCREEN"
>#!/usr/bin/perl
use Net::Jabber qw(Client);
use strict;
# List of addressees for our reminder
our @addressees;
# What we want to send
my $reminder = $ARGV[0] or die "No reminder!";
# Connect to our Jabber server
my $c= Net::Jabber::Client->new();
$c->Connect('hostname' => 'yak',
'port' => 5222);
# Authenticate
$c->AuthSend('username' => 'reminder',
'password' => 'secret',
'resource' => 'reminder');
# Set handler to deal with presence packets
# that might (will) be pushed to us (we're
# not interested in any other type of packet)
$c->SetCallBacks('presence' => \&handle_presence);
# Send out our own presence, and run an
# event loop for up to 5 seconds to
# catch any packets pushed to us
$c->PresenceSend();
$c->Process(5);
# Create a new message with our reminder text
my $m = Net::Jabber::Message->new();
$m->SetBody($reminder);
# Send the message to each of the addressees collected
# in the handle_presence() subroutine
foreach my $jid (@addressees) {
$m->SetTo($jid);
$c->Send($m);
}
# Disconnect from the Jabber server and exit
$c->Disconnect;
exit(0);
# Deal with presence packets
sub handle_presence {
my ($sid, $presence) = @_;
# Get the presence
my $show = $presence->GetShow() || 'online';
# If the user is around, add to addressee list
# 'around' here means 'online' or 'chat'
push @addressees, $presence->GetFrom()
if $show eq 'online' or $show eq 'chat';
}</PRE
></DIV
><P
>The script is fairly self-explanatory.
For now, we'll leave the script's description to the comments
embedded within it; by the end of the book, you should have a good
understanding of how to put together complete applications and utilities
using Jabber libraries in Perl, Python, and Java.</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x332.htm"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.htm"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c445.htm"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Imaginary Conversation</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c324.htm"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Inside Jabber</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -