Working with pidgin 2.6.0
[pidgin-purple-perl-plugins.git] / open-chats-from-xmpp-bookmarks.pl
blob3a78348aae99ad2da974de74127b51b3e20b7884
1 # MIT License
2 #
3 # Copyright (c) 2009 Matej Cepl <mcepl () redhat ! com>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 use Purple;
24 use Data::Dumper;
26 my $pluginName = "open-chats-xmpp";
27 my $plugin = "";
29 $botname = "USERSERV";
30 %PLUGIN_INFO = (
31 perl_api_version => 2,
32 name => "Perl: Open Chats from XMPP Bookmarks",
33 version => "0.1",
34 summary => "Opens all chats on XMPP server according to bookmarks stored there.",
35 description => "Opens all chats on XMPP server according to bookmarks stored there.",
36 author => "Matěj Cepl <mcepl () redhat ! com>",
37 url => "http://matej.ceplovi.cz",
38 load => "plugin_load",
39 unload => "plugin_unload",
42 # list of outstanding IDs for <iq>s we are waiting to get answer
43 my @requests_queue = ();
45 sub is_queued {
46 my $element = shift;
47 if ($element) {
48 my @found = grep($_ eq "$element" , @requests_queue);
49 my $count = @found;
50 Purple::Debug::misc("$pluginName",
51 "Element $element found in queue $count times.\n(@found)\n");
52 return $count >0;
53 } else {
54 return 0 ;
58 my @connected_signals = ();
59 my $blist;
62 sub plugin_load {
63 $plugin = shift;
64 my $connected_signal;
66 $conn = Purple::Connections::get_handle();
67 $consignal = Purple::Signal::connect($conn, "signed-on", $plugin,
68 \&signed_on, 0);
69 push @connected_signals,$consignal;
71 my $jabber = Purple::Find::prpl("prpl-jabber");
72 if (!$jabber) {
73 return NULL;
75 $consignal = Purple::Signal::connect($jabber,
76 "jabber-receiving-iq",$plugin,\&got_xml_cb, $randid);
77 push @connected_signals,$consignal;
79 #Initialize a buddy list
80 $blist = Purple::get_blist();
81 Purple::Debug::misc("$pluginName","Getting the current Buddy List\n" . Dumper($blist) . "\n");
82 my $node = Purple::BuddyList::get_root();
83 while ($node) {
84 Purple::Debug::misc("$pluginName", "Node:\n" . Dumper($node) . "\n");
85 # Purple::Debug::misc("$pluginName", "Type = " . $node->type . "\n" . Dumper($node) . "\n");
86 $node = Purple::BuddyList::Node::next($node,1);
90 sub plugin_unload {
91 foreach my $signal (@connected_signals) {
92 Purple::Prefs::disconnect_by_handle($signal);
94 Purple::Debug::info("$pluginName", "Login to $botname plugin unloaded\n");
97 sub signed_on {
98 my $conn = shift;
99 my $account = $conn->get_account();
100 my $username = $account->get_username();
101 my $protocol_id = $account->get_protocol_id();
102 my @buddies ;
103 Purple::Debug::misc("$pluginName", "signed-on $username @ $account ($protocol_id)\n");
104 if ($protocol_id eq "prpl-jabber") {
105 get_bookmarks($conn);
106 @buddies = Purple::Find::buddies($account,"");
107 # Purple::Debug::misc("$pluginName", "Buddies for this account:\n" . Dumper(@buddies) . "\n");
108 # Purple::Debug::misc("$pluginName", "==================\n");
109 # foreach my $buddy (@buddies) {
110 # Purple::Debug::misc("$pluginName",
111 # "budy =\n" . Dumper($buddy) . "\n");
116 sub get_bookmarks {
117 my $im_connection = shift; # current Connection
119 my $nodestr = '<iq type="get" id="RANDOM"><query xmlns="jabber:iq:private"><storage xmlns="storage:bookmarks"/></query></iq>';
120 chomp($nodestr);
121 my $randid = "bkmks-" . int(rand(100));
122 $nodestr =~ s/RANDOM/$randid/;
123 $nodestr =~ s/^\s+//gm;
124 Purple::Debug::misc("$pluginName","sending packet: $nodestr to $im_connection\n");
125 my $ret = Purple::Prpl::send_raw($im_connection,$nodestr);
126 push @requests_queue, $randid;
127 Purple::Debug::misc("$pluginName",
128 "enqueuing $randid, the queue is now: \"" . \
129 Dumper(@requests_queue) . "\"\n");
132 sub got_xml_cb {
133 # if (\&queue_len() <= 0) {
134 # return 1;
136 my $queue_length = @requests_queue;
137 Purple::Debug::misc("$pluginName","Queue is now: |@requests_queue|" . \
138 " (length: $queue_length)\n");
139 my $conn = shift; # connection
140 my $iq_type = shift; # what type of IQ is this?
141 my $iq_id = shift; # what's the ID of the incoming IQ?
142 my $iq_from = shift; # who is the IQ from?
143 my $packet = shift; # xmlnode
144 my $soughtid = shift; # random ID
145 Purple::Debug::misc("$pluginName",
146 "IQ received: \$iq_type = $iq_type, \$iq_id = $iq_id, \$iq_from = $iq_from\n\$packet = $packet\n");
148 my $room = "";
149 my $server = "";
150 my $handle = "";
151 my $handlenode;
152 my $blist_chat ;
154 if (($iq_type == 'get') && is_queued($iq_id)) {
155 shift @requests_queue;
156 my $packetstr = $packet->to_str();
157 Purple::Debug::info("$pluginName", "packet: $packetstr\n");
159 my $querynode = Purple::XMLNode::get_child($packet,"query");
160 my $storagenode = Purple::XMLNode::get_child($querynode,"storage");
161 my $curnode = Purple::XMLNode::get_child($storagenode,"conference");
162 while ($curnode) {
163 # if (($curnode->get_attrib('name') eq 'conference')
164 if ($curnode->get_attrib('autojoin') == 1){
165 # <conference minimize='0'
166 # jid='bash%irc.freenode.net@irc.ceplovi.cz'
167 # autojoin='0' name='bash'>
168 # <nick>mcepl</nick>
169 # </conference>
170 ($room,$server) = split(/@/,$curnode->get_attrib('jid'));
171 $handlenode = Purple::XMLNode::get_child($curnode,"nick");
172 $handle = Purple::XMLNode::get_data($handlenode);
173 $blist_chat = Purple::BuddyList::find_chat($conn,$room);
174 my %components = (
175 "room" => $room,
176 "server" => $server,
177 "handle" => $handle,
178 "password" => ""
180 @components = %components;
181 Purple::Debug::info("$pluginName",
182 "join_chat: " . Dumper(@components) . "\n");
184 Purple::Serv::join_chat($conn,\%components);
186 $curnode = Purple::XMLNode::get_next_twin($curnode);