Skip to content

Instantly share code, notes, and snippets.

@mimicmod
Last active January 31, 2016 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mimicmod/abd321314aba2dabfb81 to your computer and use it in GitHub Desktop.
Save mimicmod/abd321314aba2dabfb81 to your computer and use it in GitHub Desktop.
Notify operators about incomming private message - script for the Globster direct connect client
#!/usr/bin/perl
use strict;
use warnings;
use Net::DBus;
use XML::Simple;
use Net::DBus::Reactor;
# create xml object
my $xml = new XML::Simple;
# read XML file
my $config = $xml->XMLin($ARGV[0], ForceArray => ['hub','ignored','optout'], SearchPath => [ '/etc/globster' ]);
$ENV{DBUS_SESSION_BUS_ADDRESS} = $config->{'dbusaddr'} ? $config->{'dbusaddr'} : $ENV{DBUS_SESSION_BUS_ADDRESS};
my @hubs = @{$config->{'hub'}};
my $g = Net::DBus->find->get_service("net.blicky.Globster")->get_object("/net/blicky/Globster");
my %objs = map +($_->{'hubid'}, $g->get_child_object("/Hub/$_->{'hubid'}")), @hubs;
my @userfields = ('', qw|nick description client mail slots sharedfiles sharesize conn hubs ip4 ip6 as cid flags|);
my %userfields = map +($userfields[$_], $_), 1..$#userfields;
sub userstruct2hash {
my($fields, $struct) = @_;
+{ map +($userfields[$fields->[$_]], $struct->[$_]), 0..$#{$struct} };
}
sub send_pm {
my($hubid, $user, $str) = @_;
$objs{$hubid}->SendChat($user, $str, 0);
}
sub in_array {
my ($pattern, $array) = @_;
for my $item (@{$array}) {
if ($item eq $pattern) {
return 1;
}
}
return 0;
}
for my $hub (@hubs) {
$hub->{'destination'} = 0 if (!$hub->{'opchat'});
$objs{$hub->{'hubid'}}->connect_to_signal('ReceiveChat', sub {
my($group, $user, $message, $me) = @_;
return if $group != $user || $group == -1 || $user == -1 || !$user; # Do not reply to own messages, main chat or custom chatroom messages
my $username = [$objs{$hub->{'hubid'}}->UserInfo($user, [ 1 ])]->[1]{$user}[0];
if ($hub->{'state'} == 1 && !in_array($username, $hub->{'ignored'})) {
if ($hub->{'response'}) {
my $r = $hub->{'response'};
$r =~ s/%\[userNI\]/$username/g;
send_pm($hub->{'hubid'}, $group, $r);
}
my($fields, $list) = $objs{$hub->{'hubid'}}->UserInfo(-1, [ values %userfields ]);
my @u = map +{ id => $_, %{ userstruct2hash($fields, $list->{$_}) } }, keys %$list;
my $pm = $hub->{'message'};
$pm =~ s/%\[userNI\]/$username/g;
$pm =~ s/%\[message\]/$message/g;
if ($hub->{'destination'} == 0) {
for my $usr (@u) {
if ($usr->{'flags'} & 4 && !in_array($usr->{'nick'}, $hub->{'ignored'}) && !in_array($usr->{'nick'}, $hub->{'optout'})) {
if (!$hub->{'opchat'} || ($hub->{'opchat'} && $usr->{'nick'} ne $hub->{'opchat'})) {
sleep $hub->{'timeout'} if $hub->{'timeout'};
send_pm($hub->{'hubid'}, $usr->{'id'}, $pm);
}
}
}
} else {
for my $usr (@u) {
if ($usr->{'nick'} eq $hub->{'opchat'}) {
sleep $hub->{'timeout'} if $hub->{'timeout'};
send_pm($hub->{'hubid'}, $usr->{'id'}, $pm);
}
}
}
}
});
}
my $reactor = Net::DBus::Reactor->main;
$reactor->run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment