|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xdaysaysay\CoreBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Phergie\Irc\Client\React\Client; |
|
6
|
|
|
use Phergie\Irc\Client\React\WriteStream; |
|
7
|
|
|
use Phergie\Irc\Connection; |
|
8
|
|
|
use Phergie\Irc\ConnectionInterface; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Xdaysaysay\CoreBundle\Entity\IRCServer; |
|
15
|
|
|
use Xdaysaysay\CoreBundle\Entity\Team; |
|
16
|
|
|
|
|
17
|
|
|
class BotCommand extends ContainerAwareCommand |
|
18
|
|
|
{ |
|
19
|
|
|
private static $ircServers = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
* |
|
24
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function configure() |
|
27
|
|
|
{ |
|
28
|
|
|
$this |
|
29
|
|
|
->setName('xdaysaysay:bot') |
|
30
|
|
|
->setDescription('Bot commands') |
|
31
|
|
|
->addArgument( |
|
32
|
|
|
'option', |
|
33
|
|
|
InputArgument::REQUIRED, |
|
34
|
|
|
'start|stop|restart|status' |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
|
|
$option = $input->getArgument('option'); |
|
44
|
|
|
switch ($option) { |
|
45
|
|
|
case 'start': |
|
46
|
|
|
$this->start($output); |
|
47
|
|
|
break; |
|
48
|
|
|
case 'stop': |
|
49
|
|
|
break; |
|
50
|
|
|
case 'restart': |
|
51
|
|
|
break; |
|
52
|
|
|
case 'status': |
|
53
|
|
|
break; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function initializeServers() |
|
58
|
|
|
{ |
|
59
|
|
|
$ircServers = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:IRCServer')->findAll(); |
|
60
|
|
|
foreach ($ircServers as $ircServer) { |
|
61
|
|
|
$teams = []; |
|
62
|
|
|
if (!$ircServer->getXdccnames()->isEmpty()) { |
|
63
|
|
|
foreach ($ircServer->getXdccnames() as $xdccName) { |
|
64
|
|
|
if ($xdccName->getXdcc()->getVisible()) { |
|
65
|
|
|
foreach ($ircServer->getTeams() as $team) { |
|
66
|
|
|
$teams[] = $team; |
|
67
|
|
|
} |
|
68
|
|
|
if (empty(self::$ircServers[$ircServer->getHost()])) { |
|
69
|
|
|
self::$ircServers[$ircServer->getHost()] = ['ircServer' => $xdccName->getIrcServer(), 'status' => 'offline']; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
if (!empty(self::$ircServers[$ircServer->getHost()])) { |
|
75
|
|
|
self::$ircServers[$ircServer->getHost()]['teams'] = $teams; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function start(OutputInterface $output) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->initializeServers(); |
|
83
|
|
|
// Test |
|
84
|
|
|
self::$ircServers = []; |
|
85
|
|
|
$ircServer = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:IRCServer')->find(7); |
|
86
|
|
|
$team = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:Team')->find(26); |
|
87
|
|
|
self::$ircServers[$ircServer->getHost()] = ['ircServer' => $ircServer, 'status' => 'offline', 'teams' => [$team]]; // Otaku-IRC |
|
88
|
|
|
// End Test |
|
89
|
|
|
foreach (self::$ircServers as $ircServer) { |
|
90
|
|
|
if ($ircServer['status'] === 'offline') { |
|
91
|
|
|
$this->startBot($output, $ircServer['ircServer']); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function startBot(OutputInterface $output, IRCServer $ircServer) |
|
97
|
|
|
{ |
|
98
|
|
|
$connection = new Connection(); |
|
99
|
|
|
|
|
100
|
|
|
$connection |
|
101
|
|
|
->setServerHostname($ircServer->getHost()) |
|
102
|
|
|
->setNickname('xdaysaysay') |
|
103
|
|
|
->setUsername('xdaysaysay') |
|
104
|
|
|
->setHostname('xdaysaysay') |
|
105
|
|
|
->setRealname('xdaysaysay') |
|
106
|
|
|
->setOption('write', new WriteStream()); |
|
107
|
|
|
if ($ircServer->getPortSsl()) { |
|
108
|
|
|
$connection->setOption('transport', 'ssl'); |
|
109
|
|
|
$connection->setServerPort($ircServer->getPortSsl()); |
|
110
|
|
|
} else { |
|
111
|
|
|
$connection->setServerPort($ircServer->getPort()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$output->writeln('Connection to '.$connection->getServerHostname()); |
|
115
|
|
|
|
|
116
|
|
|
$client = new Client(); |
|
117
|
|
|
|
|
118
|
|
|
$client->on('irc.received', function ($message, WriteStream $write, Connection $connection, LoggerInterface $logger) { |
|
|
|
|
|
|
119
|
|
|
if ($message['command'] !== 'JOIN') { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
$channel = $message['params']['channels']; |
|
123
|
|
|
$nick = $message['nick']; |
|
124
|
|
|
$write->ircPrivmsg($channel, 'Welcome '.$nick.'!'); |
|
125
|
|
|
}); |
|
126
|
|
|
|
|
127
|
|
|
$client->on('connect.after.each', function (Connection $connection, WriteStream $write) use ($output, $client) { |
|
128
|
|
|
$output->writeln('Connected to '.$connection->getServerHostname()); |
|
129
|
|
|
if ($write) { |
|
130
|
|
|
/** @var Team $team */ |
|
131
|
|
|
foreach (self::$ircServers[$connection->getServerHostname()]['teams'] as $team) { |
|
132
|
|
|
$client->addTimer(15, function() use($write, $team) { |
|
133
|
|
|
$join = $write->ircJoin($team->getChanNameStaff(), $team->getChanNameStaffPassword() ? : null); |
|
134
|
|
|
dump($join); |
|
135
|
|
|
$join = $write->ircJoin($team->getChanName(), $team->getChanNamePassword() ? : null); |
|
136
|
|
|
dump($join); |
|
137
|
|
|
}); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
}); |
|
141
|
|
|
|
|
142
|
|
|
$client->on('connect.error', function (\Exception $exception, ConnectionInterface $connection, LoggerInterface $logger) { |
|
143
|
|
|
dump('connect.error'); |
|
144
|
|
|
$logger->debug('Connection to '.$connection->getServerHostname().' lost: '.$exception->getMessage()); |
|
145
|
|
|
}); |
|
146
|
|
|
// $client->setTickInterval() |
|
|
|
|
|
|
147
|
|
|
$client->run($connection); |
|
148
|
|
|
} |
|
149
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.