From c57393b1cab86602943ae4d18a15476c9d95a0dd Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 09 Jul 2012 18:19:59 +0000
Subject: [PATCH] Przeportowanie modułu ort do nowej wersji API
---
/dev/null | 76 -------------------------
modules/70_ort/init.php | 36 ++++++++++++
modules/70_ort/handler.php | 58 +++++++++++++++++++
3 files changed, 94 insertions(+), 76 deletions(-)
diff --git a/modules/70_ort.php b/modules/70_ort.php
deleted file mode 100644
index 5c23271..0000000
--- a/modules/70_ort.php
+++ /dev/null
@@ -1,76 +0,0 @@
-<?php
-class ort implements module {
- static function register_cmd() {
- return array(
- 'o' => 'cmd_ort',
- 'ort' => 'cmd_ort',
- );
- }
-
- static function help($cmd=NULL) {
- if($cmd === NULL) {
- GGapi::putRichText('ort ', TRUE);
- GGapi::putRichText('słowo', FALSE, TRUE);
- GGapi::putRichText("\n".' Słownik ortograficzny'."\n");
- }
- else
- {
- GGapi::putRichText('ort ', TRUE);
- GGapi::putRichText('słowo', FALSE, TRUE);
- GGapi::putRichText(' (alias: ');
- GGapi::putRichText('o', TRUE);
- GGapi::putRichText(')'."\n".' Sprawdza w słowniku ortograficznym ');
- GGapi::putRichText('słowo', FALSE, TRUE);
- }
- }
-
- static function cmd_ort($name, $arg) {
- if(empty($arg)) {
- GGapi::putText('Funkcja ');
- GGapi::putRichText('ort', TRUE);
- GGapi::putRichText(' wymaga argumentu ');
- GGapi::putRichText('slowo'."\n\n", FALSE, TRUE);
- GGapi::putRichText('Przykłady', FALSE, FALSE, TRUE);
- GGapi::putRichText("\n".'ort grzegżółka'."\n".'ort warsawa');
-
- return;
- }
-
- $proc = proc_open('aspell --lang=pl --encoding=utf-8 --ignore-case=true pipe', array(array('pipe', 'r'), array('pipe', 'w'), array('file', '/dev/null', 'w')), $pipe);
-
- fwrite($pipe[0], trim($arg)."\n");
- fclose($pipe[0]);
-
- $status = proc_get_status($proc);
- while($status['running']) {
- usleep(1);
- $status = proc_get_status($proc);
- }
-
- fgets($pipe[1], 1024);
- $spell = fread($pipe[1], 4096);
-
- if(substr($spell, 0, 1)=='*') {
- GGapi::putRichText('Pisownia poprawna', FALSE, FALSE, FALSE, 0, 150, 0);
- }
- elseif(substr($spell, 0, 1)=='#') {
- GGapi::putText('Brak propozycji poprawnej pisowni');
- }
- else
- {
- $spell = explode(': ', $spell, 2);
- $spell = explode(',', $spell[1]);
-
- $txt = 'Prawdopobnie chodziło ci o:';
- foreach($spell as $val) {
- $txt .= "\n".'- '.trim($val);
- }
-
- GGapi::putText($txt);
- }
-
- fclose($pipe[1]);
- proc_close($proc);
- }
-}
-?>
\ No newline at end of file
diff --git a/modules/70_ort/handler.php b/modules/70_ort/handler.php
new file mode 100644
index 0000000..8619f08
--- /dev/null
+++ b/modules/70_ort/handler.php
@@ -0,0 +1,58 @@
+<?php
+class bot_ort_module implements BotModule {
+ function handle($msg, $params) {
+ $args = trim($msg->args);
+
+ if(empty($args)) {
+ return new BotMsg('Funkcja <b>ort</b> wymaga argumentu.<br />'
+ . '<br />'."\n"
+ . '<u>Przykład:</u><br />'."\n"
+ . 'ort grzegżółka<br />'."\n"
+ . 'ort warsawa');
+ }
+
+ $args = strtr($args, array("\r\n" => ' ', "\r" => ' ', "\n" => ' '));
+
+ $proc = proc_open('aspell --lang=pl --encoding=utf-8 --ignore-case=true pipe', array(array('pipe', 'r'), array('pipe', 'w'), array('file', '/dev/null', 'w')), $pipe);
+
+ fwrite($pipe[0], $args."\n");
+ fclose($pipe[0]);
+
+ do {
+ usleep(1);
+ $status = proc_get_status($proc);
+ } while($status['running']);
+
+ fgets($pipe[1], 1024);
+ $spell = fgets($pipe[1], 4096);
+ fclose($pipe[1]);
+
+ proc_close($proc);
+
+ if(empty($spell)) {
+ return new BotMsg('Błąd podczas sprawdzania słowa w słowniku. Przepraszamy.');
+ }
+ elseif(substr($spell, 0, 1)=='*') {
+ return new BotMsg('<span style="color:#060;">Pisownia poprawna.</span>');
+ }
+ elseif(substr($spell, 0, 1)=='#') {
+ return new BotMsg('Brak propozycji poprawnej pisowni.');
+ }
+ else
+ {
+ $spell = explode(': ', $spell, 2);
+ $spell = explode(',', $spell[1]);
+
+ $txt = '<p>Prawdopobnie chodziło ci o:</p>'."\n"
+ . '<ul>'."\n";
+
+ foreach($spell as $val) {
+ $txt .= '<li>'.htmlspecialchars(trim($val)).'</li>'."\n";
+ }
+ $txt .= '</ul>';
+
+ return new BotMsg($txt);
+ }
+ }
+}
+?>
\ No newline at end of file
diff --git a/modules/70_ort/init.php b/modules/70_ort/init.php
new file mode 100644
index 0000000..1c128e2
--- /dev/null
+++ b/modules/70_ort/init.php
@@ -0,0 +1,36 @@
+<?php
+class bot_ort_init implements BotModuleInit {
+ function register() {
+ $handler = array(
+ array(
+ 'file' => 'handler.php',
+ 'class' => 'bot_ort_module',
+ 'method' => 'handle',
+ )
+ );
+
+ return array(
+ 'ort' => $handler,
+ 'o' => $handler,
+ );
+ }
+
+ function help($params = NULL) {
+ if($params === NULL) {
+ return new BotMsg('<b>ort</b> <i>słowo</i><br />'."\n"
+ . ' Słownik ortograficzny.<br />'."\n");
+ }
+ else
+ {
+ return new BotMsg('<b>ort</b> <i>słowo</i> (alias: <b>o</b>)<br />'."\n"
+ . ' Sprawdza <i>słowo</i> w słowniku ortograficznym. W przypadku jego nie odnalezienia zwraca propozycje poprawnej pisowni.<br />'."\n"
+ . '<br />'."\n"
+ . '<u>Przykład:</u><br />'."\n"
+ . 'ort grzegżółka<br />'."\n"
+ . 'ort warsawa');
+ }
+ }
+}
+
+return 'bot_ort_init';
+?>
\ No newline at end of file
--
Gitblit v1.9.1