Skip to content

Instantly share code, notes, and snippets.

@mimicmod
Last active February 7, 2016 10:26
Show Gist options
  • Save mimicmod/1596c56ff133bcd0e7a7 to your computer and use it in GitHub Desktop.
Save mimicmod/1596c56ff133bcd0e7a7 to your computer and use it in GitHub Desktop.
Database of released files - script for the Globster direct connect client
#!/usr/bin/perl
# Settings
my $dbfile = "/etc/globster/releases.db";
my $dbusaddr = "unix:abstract=/tmp/dbus-X47loh59IQ,guid=d5dcec9511d7062467a016ff0000002e";
my $cmdprefix = "+!-";
# Hubs to link (Globster hub IDs)
my @hubs = (1,3);
# End of settings
# Code
use strict;
use warnings;
use Net::DBus::Reactor;
use Net::DBus;
use DBI;
sub dbexecute
{
my $db = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "", {RaiseError => 1, AutoCommit => 1});
$db->do($_[0]);
$db->disconnect();
}
sub dbselect
{
my $db = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "", {RaiseError => 1, AutoCommit => 1});
my $data = $db->selectall_arrayref($_[0]);
$db->disconnect();
return $data;
}
sub parsecmd
{
my @cmd = split(/( -t | -c | -d | -h )/, $_[0]);
my $cmdlen = scalar(@cmd);
my %parsed = ();
my $parsed = \%parsed;
$parsed->{'prefix'} = $cmd[0];
for (my $i=1;$i<$cmdlen; $i+=2)
{
if($cmd[$i] eq ' -t ')
{
$parsed->{'title'} = $cmd[$i+1];
}
elsif($cmd[$i] eq ' -c ')
{
$parsed->{'category'} = $cmd[$i+1];
}
elsif($cmd[$i] eq ' -d ')
{
$parsed->{'description'} = $cmd[$i+1];
}
elsif($cmd[$i] eq ' -h ')
{
$parsed->{'hash'} = $cmd[$i+1];
}
}
return $parsed;
}
$ENV{DBUS_SESSION_BUS_ADDRESS} = $dbusaddr ? $dbusaddr : $ENV{DBUS_SESSION_BUS_ADDRESS};
dbexecute ("CREATE TABLE IF NOT EXISTS releases(id INTEGER PRIMARY KEY,title CHAR NOT NULL,description TEXT,category CHAR NOT NULL,tth CHAR NOT NULL,created TIMESTAMP DEFAULT (DATETIME('NOW')));");
sub is_cmd {
my $msg = $_[0];
my @str = split('', $cmdprefix);
my $commandprefix = "^[\\" . (join("\\", @str)) . "][a-zA-Z]+";
if ($msg =~ /$commandprefix/)
{
return 1;
}
return 0;
}
my $g = Net::DBus->find->get_service("net.blicky.Globster")->get_object("/net/blicky/Globster");
my %objs = map +($_, $g->get_child_object("/Hub/$_")), @hubs;
for my $hub (@hubs) {
$objs{$hub}->connect_to_signal('ReceiveChat', sub {
my($group, $user, $message, $me) = @_;
return if $group == -1 || $user == -1 || !$user; # Do not reply to own messages and chat messages
my $username = [$objs{$hub}->UserInfo($user, [ 1 ])]->[1]{$user}[0];
my $userlevel = [$objs{$hub}->UserInfo($user, [ 14 ])]->[1]{$user}[0];
if (is_cmd $message)
{
$message =~ s/^.//s;
if ($userlevel >= 4)
{
my $cmd = parsecmd $message;
if($message =~ /^reladd .+/)
{
dbexecute ("INSERT INTO releases (title, description, category, tth) VALUES('$cmd->{'title'}', ".($cmd->{'description'} ? "'$cmd->{'description'}', " : "'', ")."'$cmd->{'category'}', '$cmd->{'hash'}');");
$objs{$hub}->SendChat($user, "New release added.", 0);
}
if($message =~ /^relmod \d+/)
{
my @prefix = split(' ', $cmd->{'prefix'});
my @stmt;
for my $key ( keys %{$cmd} ) {
push(@stmt,"$key='$cmd->{$key}'") if ($key ne "prefix");
}
my $stmt = join(', ', @stmt);
dbexecute ("UPDATE releases SET $stmt WHERE id=$prefix[1];");
$objs{$hub}->SendChat($user, "Release $prefix[1] updated.", 0);
}
if ($message =~ /^reldel .\d+$/)
{
my @prefix = split(' ', $cmd->{'prefix'});
dbexecute ("DELETE FROM releases WHERE $prefix[1];");
$objs{$hub}->SendChat($user, "Release $prefix[1] deleted.", 0);
}
}
if ($message =~ /^releases\s*.*$/)
{
my @cmd = split(' ', $message);
my $cmdlen = scalar(@cmd);
my $releases = {};
if ($cmdlen < 2)
{
$releases = dbselect ("SELECT * FROM releases ORDER BY id ASC;");
}
else
{
$releases = dbselect ("SELECT * FROM releases WHERE category='$cmd[1]' ORDER BY id ASC;");
}
my $str = "Releases:\n";
foreach my $row (@{$releases})
{
my ($id, $title, $description, $category, $tth, $created) = @$row;
$str .= "ID: $id\nTitle: $title\nCategory: $category\nDescription: $description\nMagnet link: magnet:?xt=urn:tree:tiger:$tth\nAdded: $created\n\n";
}
my $rowcount = scalar(@{$releases});
$str .= "No releases found." if ($rowcount < 1);
$objs{$hub}->SendChat($user, $str, 0);
}
if ($message =~ /^relcat$/)
{
my $releases = dbselect ("SELECT DISTINCT category FROM releases ORDER BY category ASC;");
my $str = "Available release categories:\n";
foreach my $row (@{$releases})
{
my $category = @{$row}[0];
$str .= "$category\n";
}
$objs{$hub}->SendChat($user, $str, 0);
}
if ($message =~ /^relhelp$/)
{
my $str = "\n!releases [<category>] Show releases. Category is optional if none is given, all relaeses will be shown.\n";
$str .= "!relcat Show release categories.\n";
if ($userlevel > 4)
{
$str .= "!reladd -c <category> -t <title> -h <TTH> [-d <description>] Add new release.\n";
$str .= "!relmod <release id> [-c <category>] [-t <title>] [-h <TTH>] [-d <description>] Modify existing release. Use at least one of the options.\n";
$str .= "!reldel <release id> Delete existing release.\n";
}
$objs{$hub}->SendChat($user, $str, 0);
}
}
});
}
Net::DBus::Reactor->main->run;
# End of code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment