3 # Copyright (c) 2009 Matej Cepl <mcepl () redhat ! com>
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.
26 my $pluginName = "open-chats-xmpp";
29 $botname = "USERSERV";
31 perl_api_version
=> 2,
32 name
=> "Perl: Open Chats from XMPP Bookmarks",
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 = ();
48 my @found = grep($_ eq "$element" , @requests_queue);
50 Purple
::Debug
::misc
("$pluginName",
51 "Element $element found in queue $count times.\n(@found)\n");
58 my @connected_signals = ();
66 $conn = Purple
::Connections
::get_handle
();
67 $consignal = Purple
::Signal
::connect($conn, "signed-on", $plugin,
69 push @connected_signals,$consignal;
71 my $jabber = Purple
::Find
::prpl
("prpl-jabber");
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
();
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);
91 foreach my $signal (@connected_signals) {
92 Purple
::Prefs
::disconnect_by_handle
($signal);
94 Purple
::Debug
::info
("$pluginName", "Login to $botname plugin unloaded\n");
99 my $account = $conn->get_account();
100 my $username = $account->get_username();
101 my $protocol_id = $account->get_protocol_id();
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");
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>';
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");
133 # if (\&queue_len() <= 0) {
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");
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");
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'>
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);
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);