Merge pull request #215 from HDInnovations/IRC-Announce-Bot

(Feature) IRC Announce Bot
- closes #193
This commit is contained in:
HDVinnie
2018-03-04 15:40:46 -05:00
committed by GitHub
7 changed files with 318 additions and 11 deletions
+98
View File
@@ -0,0 +1,98 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
namespace App\Bots;
class IRCAnnounceBot
{
protected $socket = null;
private $channels = array();
private $username = null;
private $registered = false;
public function __construct($app) {
$this->app = $app;
$this->username = config('irc-bot.username');
$this->channels = config('irc-bot.channels');
$this->server = config('irc-bot.server');
$this->port = config('irc-bot.port');
$this->hostname = config('irc-bot.hostname');
$this->nickservpass = config('irc-bot.nickservpass');
$this->joinchannels = config('irc-bot.joinchannels');
$this->socket = fsockopen($this->server, $this->port);
$this->send_data("NICK {$this->username}");
$this->send_data("USER {$this->username} {$this->hostname} {$this->server} {$this->username}");
$this->connect();
}
public function __destruct()
{
if ($this->socket)
{
fclose($this->socket);
}
}
private function connect()
{
while ($data = fgets($this->socket))
{
flush();
$ex = explode(" ", $data);
if ($ex[0] == "PING")
{
$this->send_data("PONG " . $ex[1]);
if ($this->nickservpass)
$this->send_data("NICKSERV IDENTIFY {$this->nickservpass}");
return;
}
}
}
private function send_data($data)
{
fputs($this->socket, "$data\r\n");
}
private function say($channel, $string) {
$this->send_data("PRIVMSG $channel $string");
}
private function join($channel) {
$this->send_data("JOIN $channel");
}
public function message($channel, $message)
{
// Messages an specific IRC Channel
if ($this->joinchannels && preg_match('/#(\w*[a-zA-Z_0-9]+\w*)/', $channel))
{
$this->join($channel);
}
$this->say($channel, $message);
}
public function broadcast($message, $channels = null)
{
// Broadcast to all IRC Channels in config
$channels = (is_null($channels)) ? $this->channels : $channels;
foreach ($channels as $channel)
{
$this->message($channel, $message);
}
}
}
+80
View File
@@ -0,0 +1,80 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ircBroadcast extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'irc:broadcast';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Broadcast to all IRC Channels in config';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->info("Broadcasting: " . $this->argument('message'));
\Irc::broadcast($this->argument('message'));
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['message', InputArgument::REQUIRED, 'Message you would like to send'],
];
}
/**
* Get the console command options.
*
* @return array
*/
// protected function getOptions()
// {
// return array(
// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
// );
// }
}
+81
View File
@@ -0,0 +1,81 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ircMessage extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'irc:message';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Messages an IRC Channel';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->info('Messaging ' . $this->argument('channel') . ": " . $this->argument('message'));
\Irc::message($this->argument('channel'), $this->argument('message'));
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['channel', InputArgument::REQUIRED, 'Channel that you would like to message'],
['message', InputArgument::REQUIRED, 'Message you would like to send'],
];
}
/**
* Get the console command options.
*
* @return array
*/
// protected function getOptions()
// {
// return array(
// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
// );
// }
}
+3 -1
View File
@@ -27,6 +27,8 @@ class Kernel extends ConsoleKernel
\App\Console\Commands\removePersonalFreeleech::class,
\App\Console\Commands\removeFeaturedTorrent::class,
\App\Console\Commands\autoGraveyard::class,
\App\Console\Commands\ircBroadcast::class,
\App\Console\Commands\ircMessage::class,
];
/**
@@ -60,7 +62,7 @@ class Kernel extends ConsoleKernel
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
+22 -10
View File
@@ -279,16 +279,7 @@ class TorrentController extends Controller
$user->addProgress(new UserMade700Uploads(), 1);
$user->addProgress(new UserMade800Uploads(), 1);
$user->addProgress(new UserMade900Uploads(), 1);
if ($torrent->sd == 0) {
$appurl = config('app.url');
if ($torrent->anon == 0) {
Shoutbox::create(['user' => "1", 'mentions' => "1", 'message' => "User [url={$appurl}/" . $user->username . "." . $user->id . "]" . $user->username . "[/url] has uploaded [url={$appurl}/torrents/" . $torrent->slug . "." . $torrent->id . "]" . $torrent->name . "[/url] grab it now! :slight_smile:"]);
Cache::forget('shoutbox_messages');
} else {
Shoutbox::create(['user' => "1", 'mentions' => "1", 'message' => "An anonymous user has uploaded [url={$appurl}/torrents/" . $torrent->slug . "." . $torrent->id . "]" . $torrent->name . "[/url] grab it now! :slight_smile:"]);
Cache::forget('shoutbox_messages');
}
}
// check for trusted user and update torrent
if ($user->group->is_trusted) {
Torrent::approve($torrent->id);
@@ -318,6 +309,27 @@ class TorrentController extends Controller
// Activity Log
\LogActivity::addToLog("Member " . $user->username . " has uploaded " . $torrent->name . " .");
// Announce To Shoutbox
if ($torrent->sd == 0) {
$appurl = config('app.url');
if ($torrent->anon == 0) {
Shoutbox::create(['user' => "1", 'mentions' => "1", 'message' => "User [url={$appurl}/" . $user->username . "." . $user->id . "]" . $user->username . "[/url] has uploaded [url={$appurl}/torrents/" . $torrent->slug . "." . $torrent->id . "]" . $torrent->name . "[/url] grab it now! :slight_smile:"]);
Cache::forget('shoutbox_messages');
} else {
Shoutbox::create(['user' => "1", 'mentions' => "1", 'message' => "An anonymous user has uploaded [url={$appurl}/torrents/" . $torrent->slug . "." . $torrent->id . "]" . $torrent->name . "[/url] grab it now! :slight_smile:"]);
Cache::forget('shoutbox_messages');
}
}
// Announce To IRC
if (config('irc-bot.enabled') == true) {
if ($torrent->anon == 0) {
\Irc::message("#announce", "User " . $user->username . " has uploaded " . $torrent->name . " grab it now!");
} else {
\Irc::message("#announce", "An anonymous user has uploaded " . $torrent->name . " grab it now!");
}
}
return redirect()->route('download_check', ['slug' => $torrent->slug, 'id' => $torrent->id])->with(Toastr::success('Your torrent file is ready to be downloaded and seeded!', 'Yay!', ['options']));
}
}
+1
View File
@@ -240,6 +240,7 @@ return [
'Image' => Intervention\Image\Facades\Image::class,
'LogActivity' => App\Helpers\LogActivity::class,
'Irc' => App\Bots\IRCAnnounceBot::class,
],
];
+33
View File
@@ -0,0 +1,33 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
return [
/*
|--------------------------------------------------------------------------
| IRC Bot
|--------------------------------------------------------------------------
|
| IRC Bot Settings
|
*/
'enabled' => false,
'hostname' => 'example.com',
'server' => 'irc.example.com',
'port' => '6667',
'username' => 'UNIT3D',
'password' => '',
'channels' => ['#announce'],
'nickservpass' => false,
'joinchannels' => false
];