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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
##############################################################################
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
#
##############################################################################
package Net::XMPP::PrivacyLists;
=head1 NAME
Net::XMPP::PrivacyLists - XMPP Privacy Lists Object
=head1 SYNOPSIS
This module is not yet complete. Do not use.
=head1 DESCRIPTION
=head2 Basic Functions
=head2 Advanced Functions
=head1 METHODS
=head2 Basic Functions
=head2 Advanced Functions
=head1 AUTHOR
Originally authored by Ryan Eatmon.
Previously maintained by Eric Hacker.
Currently maintained by Darian Anthony Patrick.
=head1 COPYRIGHT
This module is free software, you can redistribute it and/or modify it
under the LGPL 2.1.
=cut
require 5.003;
use strict;
use warnings;
use Carp;
use XML::Stream;
sub new
{
my $proto = shift;
my $self = { };
my %args;
while($#_ >= 0) { $args{ lc(pop(@_)) } = pop(@_); }
$self->{CONNECTION} = $args{connection};
bless($self, $proto);
$self->init();
return $self;
}
##############################################################################
#
# init - initialize the module to use the privacy lists.
#
##############################################################################
sub init
{
my $self = shift;
$self->{CONNECTION}-> SetXPathCallBacks('/iq[@type="result" or @type="set"]/query[@xmlns="jabber:iq:privacy"]'=>sub{ $self->handler(@_) });
}
##############################################################################
#
# debug - print out a representation of the privacy lists.
#
##############################################################################
sub debug
{
my $self = shift;
&XML::Stream::printData("\$self->{LISTS}",$self->{LISTS});
}
##############################################################################
#
# addItem - add list item to a list.
#
##############################################################################
sub addItem
{
my $self = shift;
my ($list,%item) = @_;
my $order = delete($item{order});
$self->{LISTS}->{$list}->{$order} = \%item;
}
###############################################################################
#
# clear - delete all of the JIDs from the DB completely.
#
###############################################################################
sub clear
{
my $self = shift;
$self->{CONNECTION}->{DEBUG}->Log3("PrivacyLists::clear: clearing the database");
foreach my $list ($self->lists())
{
$self->remove($list);
}
$self->{CONNECTION}->{DEBUG}->Log3("PrivacyLists::clear: database is empty");
}
##############################################################################
#
# exists - allows you to query if the JID exists in the Roster DB.
#
##############################################################################
sub exists
{
my $self = shift;
my $list = shift;
return unless exists($self->{LISTS});
return unless exists($self->{LISTS}->{$list});
return 1;
}
##############################################################################
#
# fetch - fetch the privacy lists from the server and populate the database.
#
##############################################################################
sub fetch
{
my $self = shift;
my $iq = $self->{CONNECTION}->PrivacyListsGet();
$self->handleIQ($iq);
}
##############################################################################
#
# fetchList - fetch the privacy list from the server and populate the database.
#
##############################################################################
sub fetchList
{
my $self = shift;
my $list = shift;
my $iq = $self->{CONNECTION}->PrivacyListsGet(list=>$list);
$self->handleIQ($iq);
}
##############################################################################
#
# lists - returns a list of the current privacy lists.
#
##############################################################################
sub lists
{
my $self = shift;
return () unless exists($self->{LISTS});
return () if (scalar(keys(%{$self->{LISTS}})) == 0);
return keys(%{$self->{LISTS}});
}
##############################################################################
#
# items - returns a list of all of the items in the specified privacy list.
#
##############################################################################
sub items
{
my $self = shift;
my $list = shift;
my @items;
return () unless $self->exists($list);
foreach my $order (sort{ $a <=> $b } keys(%{$self->{LISTS}->{$list}}))
{
my %item = %{$self->{LISTS}->{$list}->{$order}};
$item{order} = $order;
push(@items,\%item);
}
return @items;
}
##############################################################################
#
# handler - takes a packet and calls the correct handler.
#
##############################################################################
sub handler
{
my $self = shift;
my $sid = shift;
my $packet = shift;
$self->handleIQ($packet) if ($packet->GetTag() eq "iq");
}
##############################################################################
#
# handleIQ - takes an iq packet that contains roster, parses it, and puts
# the roster into the Roster DB.
#
##############################################################################
sub handleIQ
{
my $self = shift;
my $iq = shift;
print "handleIQ: iq(",$iq->GetXML(),")\n";
my $type = $iq->GetType();
return unless (($type eq "set") || ($type eq "result"));
if ($type eq "result")
{
my $query = $iq->GetChild("jabber:iq:privacy");
my @lists = $query->GetLists();
return unless ($#lists > -1);
my @items = $lists[0]->GetItems();
if (($#lists == 0) && ($#items > -1))
{
$self->parseList($lists[0]);
}
elsif ($#lists >= -1)
{
$self->parseLists(\@lists);
}
}
}
sub parseList
{
my $self = shift;
my $list = shift;
my $name = $list->GetName();
foreach my $item ($list->GetItems())
{
my %item = $item->GetItem();
$self->addItem($name,%item);
}
}
sub parseLists
{
my $self = shift;
my $lists = shift;
foreach my $list (@{$lists})
{
my $name = $list->GetName();
$self->fetchList($name);
}
}
##############################################################################
#
# reload - clear and refetch the privacy lists.
#
##############################################################################
sub reload
{
my $self = shift;
$self->clear();
$self->fetch();
}
##############################################################################
#
# remove - removes the list from the database.
#
##############################################################################
sub remove
{
my $self = shift;
my $list = shift;
if ($self->exists($list))
{
$self->{CONNECTION}->{DEBUG}->Log3("PrivacyLists::remove: deleting $list from the DB");
delete($self->{LISTS}->{$list});
delete($self->{LISTS}) if (scalar(keys(%{$self->{LISTS}})) == 0);
}
}
sub save
{
my $self = shift;
foreach my $list ($self->lists())
{
$self->saveList($list);
}
}
sub saveList
{
my $self = shift;
my $list = shift;
my @items = $self->items($list);
$self->{CONNECTION}->PrivacyListsSet(list=>$list,
items=>\@items);
}
1;
|