Limit the number of simultaneous connect attempts passed to libpurple.
[thrasher.git] / perl / lib / Thrasher / Backend / Test.pm
blob2de02a4602d7d4b329d7a92f56998a639034f935
1 package Thrasher::Backend::Test;
2 use strict;
3 use warnings;
5 use base 'Thrasher::Backend';
6 use Thrasher::Callbacks qw(callbacks);
8 =head1 NAME
10 Thrasher::Backend::Test - a test backed
12 =head1 DESCRIPTION
14 This module implements a test backend for Thrasher, suitable for
15 unit testing without reference to a real storage backend.
17 This module accepts a simple hashref mapping from JIDs to the
18 hashrefs returned by C<registered>, and stores them if you
19 register. Very simple.
21 =cut
23 sub new {
24 my $class = shift;
25 my $self = $class->SUPER::new();
27 my $args = shift || {};
29 my $registered = $args->{registered} || {};
30 $self->{registered} = $registered;
32 $self->{legacy_to_jid} = {};
33 $self->{jid_to_legacy} = {};
34 $self->{roster} = {};
36 return $self;
39 sub registered {
40 my $self = shift;
41 my $jid = shift;
43 return $self->{registered}->{$jid};
46 sub register {
47 my $self = shift;
48 my $jid = shift;
49 my $registration_info = shift;
51 $self->{registered}->{$jid} = $registration_info;
54 sub remove {
55 my $self = shift;
56 my $jid = shift;
58 delete $self->{registered}->{$jid};
59 delete $self->{legacy_to_jid}->{$jid};
60 delete $self->{jid_to_legacy}->{$jid};
63 sub retrieve_legacy_name_to_jid {
64 my $self = shift;
65 my $user_jid = shift;
66 my $legacy_name = shift;
67 return $self->{legacy_to_jid}->{$user_jid}->{$legacy_name};
70 sub jid_to_legacy_name {
71 my $self = shift;
72 my $user_jid = shift;
73 my $jid = shift;
74 my $stored_name = $self->{jid_to_legacy}->{$user_jid}->{$jid};
75 return $stored_name if defined($stored_name);
77 # Otherwise, our best guess is "chop off everything after @".
78 return $self->fake_up_a_legacy_name($user_jid, $jid);
81 sub jid_has_legacy_name {
82 my $self = shift;
83 my $user_jid = shift;
84 my $jid = shift;
85 return $self->{jid_to_legacy}->{$user_jid}->{$jid};
88 sub store_username_mapping {
89 my $self = shift;
90 my $user_jid = shift;
91 my $legacy = shift;
92 my $mapped_jid = shift;
94 $self->{jid_to_legacy}->{$user_jid}->{$mapped_jid} = $legacy;
95 $self->{legacy_to_jid}->{$user_jid}->{$legacy} = $mapped_jid;
98 sub all_mappings {
99 my $self = shift;
100 my $user_jid = shift;
101 return $self->{legacy_to_jid}->{$user_jid};
104 sub set_avatar {
105 my $self = shift;
106 my $user_jid = shift;
107 my $legacy_username = shift;
108 $self->{avatars}->{$user_jid}->{$legacy_username} = shift;
111 sub get_avatar {
112 my $self = shift;
113 my $user_jid = shift;
114 my $legacy_username = shift;
115 return $self->{avatars}->{$user_jid}->{$legacy_username};
118 sub all_avatars {
119 my $self = shift;
120 my $user_jid = shift;
121 return $self->{avatars}->{$user_jid};
124 sub all_jids {
125 my $self = shift;
126 my @jids = keys %{$self->{registered}};
127 return \@jids;
130 sub get_roster {
131 my $self = shift;
132 my $user_jid = shift;
133 return $self->{roster}->{$user_jid} || {};
136 sub set_roster {
137 my $self = shift;
138 my $user_jid = shift;
139 my $new_roster = shift;
141 $self->{roster}->{$user_jid} = $new_roster;
144 sub set_roster_user_state {
145 my $self = shift;
146 my $user_jid = shift;
147 my $legacy_username = shift;
148 my $state = shift;
150 if ($state == $self->unsubscribed) {
151 delete $self->{roster}->{$user_jid}->{$legacy_username};
152 } else {
153 $self->{roster}->{$user_jid}->{$legacy_username} = $state;
157 sub set_misc {
158 my $self = shift;
159 my $user_jid = shift;
160 my $key = shift;
161 my $value = shift;
163 $self->{misc}->{$user_jid}->{$key} = $value;
166 sub get_misc {
167 my $self = shift;
168 my $user_jid = shift;
169 my $key = shift;
171 return $self->{misc}->{$user_jid}->{$key};
174 sub all_misc {
175 my $self = shift;
176 my $user_jid = shift;
178 return $self->{misc}->{$user_jid};