Browse Source

Première version du renouveau d’Edgard...

master
Matthieu Marcillaud 1 year ago
commit
22e75ef605
  1. 2
      .gitignore
  2. 13
      composer.json
  3. 645
      composer.lock
  4. 12
      config/app.dist.php
  5. 11
      public/index.php
  6. 23
      public/send.php
  7. 34
      src/AutoResponse.php
  8. 35
      src/Config.php
  9. 206
      src/Edgard.php

2
.gitignore

@ -0,0 +1,2 @@
/vendor
/config/app.php

13
composer.json

@ -0,0 +1,13 @@
{
"name": "spip/edgard-bot",
"description" : "Edgard bot for #spip",
"require": {
"hoa/irc": "~0.0",
"hoa/websocket": "~3.0"
},
"autoload": {
"psr-4": {
"Edgard\\": "src/"
}
}
}

645
composer.lock

@ -0,0 +1,645 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "5cb524ce5ef9cbe1f094b4dbea1d413e",
"packages": [
{
"name": "hoa/consistency",
"version": "1.17.05.02",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Consistency.git",
"reference": "fd7d0adc82410507f332516faf655b6ed22e4c2f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Consistency/zipball/fd7d0adc82410507f332516faf655b6ed22e4c2f",
"reference": "fd7d0adc82410507f332516faf655b6ed22e4c2f",
"shasum": ""
},
"require": {
"hoa/exception": "~1.0",
"php": ">=5.5.0"
},
"require-dev": {
"hoa/stream": "~1.0",
"hoa/test": "~2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Consistency\\": "."
},
"files": [
"Prelude.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Consistency library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"autoloader",
"callable",
"consistency",
"entity",
"flex",
"keyword",
"library"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Consistency",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Consistency/issues",
"source": "https://central.hoa-project.net/Resource/Library/Consistency"
},
"time": "2017-05-02T12:18:12+00:00"
},
{
"name": "hoa/event",
"version": "1.17.01.13",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Event.git",
"reference": "6c0060dced212ffa3af0e34bb46624f990b29c54"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Event/zipball/6c0060dced212ffa3af0e34bb46624f990b29c54",
"reference": "6c0060dced212ffa3af0e34bb46624f990b29c54",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/exception": "~1.0"
},
"require-dev": {
"hoa/test": "~2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Event\\": "."
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Event library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"event",
"library",
"listener",
"observer"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Event",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Event/issues",
"source": "https://central.hoa-project.net/Resource/Library/Event"
},
"time": "2017-01-13T15:30:50+00:00"
},
{
"name": "hoa/exception",
"version": "1.17.01.16",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Exception.git",
"reference": "091727d46420a3d7468ef0595651488bfc3a458f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Exception/zipball/091727d46420a3d7468ef0595651488bfc3a458f",
"reference": "091727d46420a3d7468ef0595651488bfc3a458f",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/event": "~1.0"
},
"require-dev": {
"hoa/test": "~2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Exception\\": "."
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Exception library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"exception",
"library"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Exception",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Exception/issues",
"source": "https://central.hoa-project.net/Resource/Library/Exception"
},
"time": "2017-01-16T07:53:27+00:00"
},
{
"name": "hoa/http",
"version": "1.17.01.13",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Http.git",
"reference": "6d3e114b48a63cf6b9532f9e9607cebade6314ef"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Http/zipball/6d3e114b48a63cf6b9532f9e9607cebade6314ef",
"reference": "6d3e114b48a63cf6b9532f9e9607cebade6314ef",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/exception": "~1.0",
"hoa/stream": "~1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Http\\": "."
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Http library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"http",
"library",
"request",
"response",
"rfc2295",
"rfc2324",
"rfc2518",
"rfc2774",
"rfc3229",
"rfc4918",
"rfc5842",
"rfc6585",
"rfc7231",
"rfc7232",
"rfc7233",
"rfc7235",
"rfc7237",
"runtime"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Http",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Http/issues",
"source": "https://central.hoa-project.net/Resource/Library/Http"
},
"time": "2017-01-13T15:28:17+00:00"
},
{
"name": "hoa/irc",
"version": "0.17.01.10",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Irc.git",
"reference": "dd602d1ecabf3ef487dbc43f80049c365ae96efa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Irc/zipball/dd602d1ecabf3ef487dbc43f80049c365ae96efa",
"reference": "dd602d1ecabf3ef487dbc43f80049c365ae96efa",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/event": "~1.0",
"hoa/exception": "~1.0",
"hoa/socket": "~1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Irc\\": "."
},
"files": [
"Socket.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Irc library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"client",
"irc",
"library"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Irc",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Irc/issues",
"source": "https://central.hoa-project.net/Resource/Library/Irc"
},
"time": "2017-01-10T10:42:10+00:00"
},
{
"name": "hoa/protocol",
"version": "1.17.01.14",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Protocol.git",
"reference": "5c2cf972151c45f373230da170ea015deecf19e2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Protocol/zipball/5c2cf972151c45f373230da170ea015deecf19e2",
"reference": "5c2cf972151c45f373230da170ea015deecf19e2",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/exception": "~1.0"
},
"require-dev": {
"hoa/test": "~2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Protocol\\": "."
},
"files": [
"Wrapper.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Protocol library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"library",
"protocol",
"resource",
"stream",
"wrapper"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Protocol",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Protocol/issues",
"source": "https://central.hoa-project.net/Resource/Library/Protocol"
},
"time": "2017-01-14T12:26:10+00:00"
},
{
"name": "hoa/socket",
"version": "1.17.05.16",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Socket.git",
"reference": "1a43f073d910d0f803de8cc9256779db1027906d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Socket/zipball/1a43f073d910d0f803de8cc9256779db1027906d",
"reference": "1a43f073d910d0f803de8cc9256779db1027906d",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/exception": "~1.0",
"hoa/stream": "~1.0"
},
"require-dev": {
"hoa/test": "~2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Socket\\": "."
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Socket library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"Socket",
"client",
"library",
"node",
"server",
"tcp",
"udp"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Socket",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Socket/issues",
"source": "https://central.hoa-project.net/Resource/Library/Socket"
},
"time": "2017-05-16T07:55:41+00:00"
},
{
"name": "hoa/stream",
"version": "1.17.02.21",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Stream.git",
"reference": "3293cfffca2de10525df51436adf88a559151d82"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Stream/zipball/3293cfffca2de10525df51436adf88a559151d82",
"reference": "3293cfffca2de10525df51436adf88a559151d82",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/event": "~1.0",
"hoa/exception": "~1.0",
"hoa/protocol": "~1.0"
},
"require-dev": {
"hoa/test": "~2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Stream\\": "."
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Stream library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"Context",
"bucket",
"composite",
"filter",
"in",
"library",
"out",
"protocol",
"stream",
"wrapper"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Stream",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Stream/issues",
"source": "https://central.hoa-project.net/Resource/Library/Stream"
},
"time": "2017-02-21T16:01:06+00:00"
},
{
"name": "hoa/websocket",
"version": "3.17.01.09",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Websocket.git",
"reference": "a1bd79cefb67278f71ac8a44873ad1416924e906"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Websocket/zipball/a1bd79cefb67278f71ac8a44873ad1416924e906",
"reference": "a1bd79cefb67278f71ac8a44873ad1416924e906",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/event": "~1.0",
"hoa/exception": "~1.0",
"hoa/http": "~1.0",
"hoa/socket": "~1.0"
},
"require-dev": {
"ext-iconv": "*",
"hoa/test": "~2.0"
},
"suggest": {
"ext-openssl": "To get a better security when exchanging messages."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"Hoa\\Websocket\\": "."
},
"files": [
"Socket.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name": "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"description": "The Hoa\\Websocket library.",
"homepage": "https://hoa-project.net/",
"keywords": [
"Socket",
"client",
"hybi",
"library",
"realtime",
"rfc3629",
"rfc3986",
"rfc6455",
"server",
"websocket"
],
"support": {
"docs": "https://central.hoa-project.net/Documentation/Library/Websocket",
"email": "support@hoa-project.net",
"forum": "https://users.hoa-project.net/",
"irc": "irc://chat.freenode.net/hoaproject",
"issues": "https://github.com/hoaproject/Websocket/issues",
"source": "https://central.hoa-project.net/Resource/Library/Websocket"
},
"time": "2017-01-09T13:53:08+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.0.0"
}

12
config/app.dist.php

@ -0,0 +1,12 @@
<?php
return new Edgard\Config([
#'irc' => 'irc://chat.freenode.net',
#'ws' => 'ws://127.0.0.1:8889',
#'channel' => '#spip',
#'nickname' => 'Edgard',
'wiki_file' => '~/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',
#'ping_faq' => 'https://edgard.spip.net/spip.php?page=mafaq&var_mode=calcul',
]);

11
public/index.php

@ -0,0 +1,11 @@
<?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);

23
public/send.php

@ -0,0 +1,23 @@
<?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.");
}
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);
}

34
src/AutoResponse.php

@ -0,0 +1,34 @@
<?php
namespace Edgard;
class AutoResponse {
protected $regexp;
protected $response;
protected $file;
protected $line_no;
public function __construct(string $regexp, string $response, string $file, int $line_no) {
$this->regexp = $regexp;
$this->response = $response;
$this->file = $file;
$this->line_no = $line_no;
}
public function getRegexp() {
return $this->regexp;
}
public function getResponse() {
return $this->response;
}
public function getFile() {
return $this->file;
}
public function getLineNo() {
return $this->line_no;
}
}

35
src/Config.php

@ -0,0 +1,35 @@
<?php
namespace Edgard;
class Config {
protected $irc = 'irc://chat.freenode.net';
protected $ws = 'ws://127.0.0.1:8889';
protected $channel = '#spip';
protected $nickname = 'Edgard';
/** @param string full path to wiki edgard file */
protected $wiki_edgard = '/tmp/edgard.txt';
/** @param string full path to wiki faq file */
protected $wiki_faq = '/tmp/faq.txt';
protected $ping_edgard = 'https://edgard.spip.net/spip.php?page=edgard&var_mode=calcul';
protected $ping_faq = 'https://edgard.spip.net/spip.php?page=mafaq&var_mode=calcul';
public function __construct(array $config) {
foreach ($config as $name => $value) {
if (!property_exists($this, $name)) {
throw new \Exception("Config $name unknown");
}
$this->$name = $value;
}
}
public function __get($name) {
if (property_exists($this, $name)) {
return $this->$name;
}
throw new \Exception("Property $name unknown");
}
}

206
src/Edgard.php

@ -0,0 +1,206 @@
<?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;
use stdClass;
class Edgard {
protected $config;
protected $client;
protected $server;
protected $group;
protected $autoResponses = [];
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;
$this->loadWiki();
$this->startClientEvents();
$this->startServerEvents();
$this->group->run();
}
public function loadWiki() {
$this->autoResponses = [];
$this->errors = [];
foreach ([$this->config->wiki_faq, $this->config->wiki_edgard] as $file) {
if (!$file) {
continue;
}
if (!file_exists($file)) {
continue;
}
$content = file_get_contents($file);
if (!$content) {
continue;
}
$filename = basename($file);
$lines = explode("\n", $content);
// false si fichier edgard.txt qui a un <html><pre>
$start = $file !== $this->config->wiki_edgard;
foreach ($lines as $line_no => $line) {
if (!$start and !$this->isWikiStartLine($line)) {
continue;
}
if (!$start) {
$start = true;
continue;
}
$line = $this->isWikiValidLine($line);
if (!$line) {
continue;
}
list ($regexp, $response) = explode(';', $line, 2);
$regexp = '#' . str_replace('#', '\#', $regexp) . '#';
if (@preg_match($regexp, '') !== false) {
$this->autoResponses[] = new AutoResponse($regexp, $response, $filename, $line_no + 1);
} else {
// regex was invalid and more info is in $exception->getMessage()
$this->errors[] = new AutoResponse($regexp, $response, $filename, $line_no + 1);
}
}
}
return count($this->autoResponses);
}
protected function isWikiStartLine(string $line): bool {
return trim($line) === '<html><pre>';
}
protected function isWikiValidLine(string $line): ?string {
if (!$line) {
return null;
}
$line = trim($line);
if (!$line) {
return null;
}
if (false === strpos($line, ';')) {
return null;
}
return $line;
}
public function reloadWiki() {
if ($this->config->ping_edgard) {
file_get_contents($this->config->ping_edgard);
}
if ($this->config->ping_faq) {
file_get_contents($this->config->ping_faq);
}
return $this->loadWiki();
}
public function autoResponse($message) {
foreach ($this->autoResponses as $autoResponse) {
if (preg_match($autoResponse->getRegexp(), $message, $m)) {
return $autoResponse->getResponse();
}
}
return null;
}
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) {
$message = $this->replacePlaceholders($this->getHello(), $bucket);
$bucket->getSource()->say($message);
return;
});
$this->client->on('message', function (Bucket $bucket) {
$data = $bucket->getData();
$message = $data['message']; // do something with that.
if (trim($message) === 'Edgard wiki') {
$bucket->getSource()->say("Je recharge le wiki, très bien… Faut toujours bosser ici !");
$n = $this->reloadWiki();
$bucket->getSource()->say("Wiki à jour... Je connais $n formules :)");
if ($this->errors) {
$bucket->getSource()->say(count($this->errors) . " regexp en erreurs :( dont : ");
$erreur = reset($this->errors);
$bucket->getSource()->say("Wiki " . $erreur->getFile() . ':' . $erreur->getLineNo() . ' sur ' . $erreur->getRegexp());
}
} elseif ($response = $this->autoResponse($message)) {
$bucket->getSource()->say($response);
}
return;
});
$this->client->on('mention', function (Bucket $bucket) {
$data = $bucket->getData();
$message = $data['message']; // do something with that.
$message = $this->replacePlaceholders($this->getMention(), $bucket);
$bucket->getSource()->say($message);
return;
});
}
protected function startServerEvents() {
$this->server->on('message', function (Bucket $bucket) {
$data = $bucket->getData();
$message = $data['message'];
$this->client->say($message);
return;
});
}
public function getClient() {
return $this->client;
}
public function getHello() {
$phrases = [
"Bonjour à tout·e·s !",
"Salutations",
"Welcome back :)",
"Hop",
"%me% est de retour !",
];
return $this->random($phrases);
}
public function getMention() {
$phrases = [
"Je ne suis qu’un bot %nick% !",
"Yep ?",
"Hé, un bot ne sais rien !",
"Un peu de silence %nick%...",
];
return $this->random($phrases);
}
protected function random(array $list) {
$i = random_int(0, count($list) - 1);
return $list[$i];
}
protected function replacePlaceholders(string $message, Bucket $bucket) {
$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);
}
}
Loading…
Cancel
Save