Browse Source

On sépare Edgard et Shiraaz...

master
Matthieu Marcillaud 1 year ago
parent
commit
d0dc7a8734
  1. 3
      .gitignore
  2. 4
      README.md
  3. 2
      config/app.dist.php
  4. 11
      public/edgard.php
  5. 11
      public/index.php
  6. 6
      public/sendToEdgard.php
  7. 23
      public/sendToShiraaz.php
  8. 11
      public/shiraaz.php
  9. 90
      src/Bot.php
  10. 68
      src/Edgard.php
  11. 6
      src/Shiraaz.php

3
.gitignore

@ -1,2 +1,3 @@
/vendor
/config/app.php
/config/edgard.php
/config/shiraaz.php

4
README.md

@ -15,11 +15,11 @@ Edgard est le bot de #spip
# Exécution
# serveur (se connecte, reste connecté et à l’écoute...)
php public/index.php
php public/edgard.php
# envoyer des messages
echo "Mon beau message" | php public/send.php
php public/send.php "Mon beau message"
php public/sendToEdgard.php "Mon beau message"

2
config/app.dist.php

@ -5,6 +5,8 @@ return new Edgard\Config([
#'ws' => 'ws://127.0.0.1:8889',
#'channel' => '#spip',
#'nickname' => 'Edgard',
# For Wiki Bot
'wiki_edgard' => '~/edgard.spip.net/tmp/edgard.txt',
'wiki_faq' => '~/edgard.spip.net/tmp/faq.txt',
#'ping_edgard' => 'https://edgard.spip.net/spip.php?page=edgard&var_mode=calcul',

11
public/edgard.php

@ -0,0 +1,11 @@
<?php
require dirname(__DIR__).'/vendor/autoload.php';
if (file_exists(dirname(__DIR__).'/config/edgard.php')) {
$config = include dirname(__DIR__).'/config/edgard.php';
} else {
throw new \Exception("You have to copy app.dist.php to edgard.php and edit it’s config.");
}
$edgard = new Edgard\Edgard($config);

11
public/index.php

@ -1,11 +0,0 @@
<?php
require dirname(__DIR__).'/vendor/autoload.php';
if (file_exists(dirname(__DIR__).'/config/app.php')) {
$config = include dirname(__DIR__).'/config/app.php';
} else {
throw new \Exception("You have to copy app.dist.php to app.php and edit it’s config.");
}
$botty = new Edgard\Edgard($config);

6
public/send.php → public/sendToEdgard.php

@ -2,10 +2,10 @@
require dirname(__DIR__).'/vendor/autoload.php';
if (file_exists(dirname(__DIR__).'/config/app.php')) {
$config = include dirname(__DIR__).'/config/app.php';
if (file_exists(dirname(__DIR__).'/config/edgard.php')) {
$config = include dirname(__DIR__).'/config/edgard.php';
} else {
throw new \Exception("You have to copy app.dist.php to app.php and edit it’s config.");
throw new \Exception("You have to copy app.dist.php to edgard.php and edit it’s config.");
}
array_shift($argv);

23
public/sendToShiraaz.php

@ -0,0 +1,23 @@
<?php
require dirname(__DIR__).'/vendor/autoload.php';
if (file_exists(dirname(__DIR__).'/config/shiraaz.php')) {
$config = include dirname(__DIR__).'/config/shiraaz.php';
} else {
throw new \Exception("You have to copy app.dist.php to shiraaz.php and edit it’s config.");
}
array_shift($argv);
$content = implode(" ", $argv);
if (!$content) {
$content = file_get_contents('php://stdin');
}
if ($content) {
$client = new Hoa\Websocket\Client(
new Hoa\Socket\Client($config->ws)
);
$client->setHost('localhost');
$client->connect();
$client->send($content);
}

11
public/shiraaz.php

@ -0,0 +1,11 @@
<?php
require dirname(__DIR__).'/vendor/autoload.php';
if (file_exists(dirname(__DIR__).'/config/shiraaz.php')) {
$config = include dirname(__DIR__).'/config/shiraaz.php';
} else {
throw new \Exception("You have to copy app.dist.php to shiraaz.php and edit it’s config.");
}
$shiraaz = new Edgard\Shiraaz($config);

90
src/Bot.php

@ -0,0 +1,90 @@
<?php
namespace Edgard;
use Hoa\Socket\Connection\Group;
use Hoa\Irc\Client as IrcClient;
use Hoa\Socket\Client as SocketClient;
use Hoa\Socket\Server as SocketServer;
use Hoa\Websocket\Server as WsServer;
use Hoa\Event\Bucket;
class Bot {
protected $config;
protected $client;
protected $client_notifications;
protected $server;
protected $group;
protected $wiki;
protected $responses;
public function __construct(Config $config) {
$this->config = $config;
$this->group = new Group();
$this->client = new IrcClient(new SocketClient($config->irc));
$this->server = new WsServer(new SocketServer($config->ws));
$this->group[] = $this->server;
$this->group[] = $this->client;
// nos réponses aléatoires
$this->responses = new Responses();
$this->startClientEvents();
$this->startServerEvents();
$this->group->run();
}
protected function startClientEvents() {
$this->client->on('open', function (Bucket $bucket) {
$bucket->getSource()->join($this->config->nickname, $this->config->channel);
return;
});
$this->client->on('join', function (Bucket $bucket) {
$this->say($bucket, $this->responses->getHello());
return;
});
}
protected function startServerEvents() {
$this->server->on('message', function (Bucket $bucket) {
$message = $bucket->getData()['message'];
$this->client->say($message);
return;
});
}
/**
* Say, qui remplace les placeholders...
*/
protected function say(Bucket $bucket, string $message) {
$message = $this->replacePlaceholders($bucket, $message);
$bucket->getSource()->say($message);
}
/**
* Replace placeholders (%nick%, %me% ...) in message
*
* @param Bucket $bucket
* @param string $message
* @return string
*/
protected function replacePlaceholders(Bucket $bucket, string $message) {
$myNick = $bucket->getSource()->getConnection()->getCurrentNode()->getUsername();
$data = $bucket->getData();
$replaces = [];
$replaces['%me%'] = $myNick;
$replaces['%nick%'] = $data['from']['nick'] ?? '';
return str_replace(array_keys($replaces), $replaces, $message);
}
public function getClient() {
return $this->client;
}
}

68
src/Edgard.php

@ -9,46 +9,21 @@ use Hoa\Socket\Server as SocketServer;
use Hoa\Websocket\Server as WsServer;
use Hoa\Event\Bucket;
class Edgard {
class Edgard extends Bot {
protected $config;
protected $client;
protected $server;
protected $group;
protected $wiki;
protected $responses;
public function __construct(Config $config) {
$this->config = $config;
$this->group = new Group();
$this->client = new IrcClient(new SocketClient($config->irc));
$this->server = new WsServer(new SocketServer($config->ws));
$this->group[] = $this->server;
$this->group[] = $this->client;
// nos réponses aléatoires
$this->responses = new Responses();
// notre super Wiki de regexp...
$this->wiki = new Wiki($config);
$this->wiki->load();
$this->startClientEvents();
$this->startServerEvents();
$this->group->run();
parent::__construct($config);
}
protected function startClientEvents() {
$this->client->on('open', function (Bucket $bucket) {
$bucket->getSource()->join($this->config->nickname, $this->config->channel);
return;
});
$this->client->on('join', function (Bucket $bucket) {
$this->say($bucket, $this->responses->getHello());
return;
});
parent::startClientEvents();
$this->client->on('message', function (Bucket $bucket) {
if ($this->wikiResponse($bucket)) {
@ -70,14 +45,6 @@ class Edgard {
});
}
protected function startServerEvents() {
$this->server->on('message', function (Bucket $bucket) {
$message = $bucket->getData()['message'];
$this->client->say($message);
return;
});
}
/** Si le wiki trouve une réponse pour ce message, l’envoyer */
protected function wikiResponse(Bucket $bucket): bool {
$message = $bucket->getData()['message'];
@ -105,34 +72,5 @@ class Edgard {
}
return false;
}
/**
* Say, qui remplace les placeholders...
*/
protected function say(Bucket $bucket, string $message) {
$message = $this->replacePlaceholders($bucket, $message);
$bucket->getSource()->say($message);
}
/**
* Replace placeholders (%nick%, %me% ...) in message
*
* @param Bucket $bucket
* @param string $message
* @return string
*/
protected function replacePlaceholders(Bucket $bucket, string $message) {
$myNick = $bucket->getSource()->getConnection()->getCurrentNode()->getUsername();
$data = $bucket->getData();
$replaces = [];
$replaces['%me%'] = $myNick;
$replaces['%nick%'] = $data['from']['nick'] ?? '';
return str_replace(array_keys($replaces), $replaces, $message);
}
public function getClient() {
return $this->client;
}
}

6
src/Shiraaz.php

@ -0,0 +1,6 @@
<?php
namespace Edgard;
class Shiraaz extends Bot {}
Loading…
Cancel
Save