Changeset 769

Show
Ignore:
Timestamp:
01/05/08 18:31:59 (11 months ago)
Author:
mart
Message:

Make bots automatically accept roster subscription requests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/DJabberd/CHANGES

    r768 r769  
    1717  - Add POD for DJabberd::Agent, DJabberd::Component and a bunch of 
    1818    other related modules. 
     19 
     20  - Make bots automatically accept roster subscription requests. 
    1921 
    20220.83  2007-05-08 
  • trunk/DJabberd/lib/DJabberd/Bot.pm

    r587 r769  
    3030sub jid { 
    3131    return $_[0]->{jid}; 
     32} 
     33 
     34sub bound_jid { 
     35    return $_[0]->jid; 
     36} 
     37 
     38sub vhost { 
     39    return $_[0]->{vhost}; 
    3240} 
    3341 
     
    111119        return; 
    112120    } 
     121    elsif ($stanza->isa('DJabberd::Presence')) { 
     122        # Most presence-related stuff is handled by the server itself, However, we still 
     123        # need to handle presence subscription requests. 
     124        my $type = $stanza->attr('{}type'); 
     125         
     126        if ($type eq 'subscribe') { 
     127            $logger->debug("Accepting presence subscripton request from ", $stanza->from_jid->as_string); 
     128            my $response = DJabberd::Presence->new('jabber:client', 'presence', { 
     129                '{}type' => 'chat', 
     130                '{}to' => $stanza->from_jid->as_string, 
     131                '{}type' => 'subscribed', 
     132            }, []); 
     133            $self->send_stanza_as_client($response); 
     134        } 
     135 
     136    } 
     137 
     138} 
     139 
     140# Since DJabberd::Bot is acting like a client, it must deliver messages using 
     141# this method which does the same thing as a DJabberd::Connection::ClientIn would 
     142# do if a stanza was recieved from a real client. 
     143sub send_stanza_as_client { 
     144    my ($self, $stanza) = @_; 
     145    $self->vhost->hook_chain_fast( 
     146        "filter_incoming_client", 
     147        [ $stanza, $self ], 
     148        { 
     149            reject => sub { },  # just stops the chain 
     150        }, 
     151        \&filter_incoming_client_builtin, 
     152    ); 
     153} 
     154sub filter_incoming_client_builtin { 
     155    my ($vhost, $cb, $stanza, $self) = @_; 
     156 
     157    # if no from, we set our own 
     158    if (! $stanza->from_jid) { 
     159        my $bj = $self->jid; 
     160        $stanza->set_from($bj->as_string) if $bj; 
     161    } 
     162 
     163    $vhost->hook_chain_fast( 
     164        "switch_incoming_client", 
     165        [ $stanza ], 
     166        { 
     167            process => sub { }, # Do nothing, because we don't actually have a $conn object to pass into $stanza->process 
     168            deliver => sub { $stanza->deliver($vhost) }, 
     169        }, 
     170        sub { 
     171            $stanza->on_recv_from_client($self); 
     172        }, 
     173    ); 
    113174} 
    114175