Created
September 8, 2009 19:20
-
-
Save malcom/183157 to your computer and use it in GitHub Desktop.
Wikipedius
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// file charset: CP1250/Windows1250 | |
/* | |
Wikipedius v1.0 | |
Copyright (c) 2006 Malcom <malcom@.o2.pl> | |
http://projects.malcom.pl/scripts/tlen/wikipedius.xhtml | |
License: MIT | |
Starting Wikipedius | |
Wikipedius is started from the command line, unlike most PHP programs. | |
Here is a good example of how to run it: | |
php wikipedius.php &> error.log & | |
error.log will contain whatever PHP errors/warnings that are encountered, | |
and FILELOG will contain the all lten protocol transfer with XML stanzas | |
*/ | |
// config | |
define('USERNAME', 'wikipedius'); | |
define('PASSWORD', 'pass'); | |
define('RESOURCE', 'Wikipedius Bot v0.1'); | |
define('ADMIN', 'malcom@tlen.pl'); | |
define('FILELOG', 'C:\\wikipedius.log'); | |
$status = "Wikipedius się narodził :)\nDostęp do zasobów Wikipedii przez Tlena"; | |
require('class.tlen.php'); | |
$help = array( | |
'about' => 'Wyświetla informacje o Wikipediusie.', | |
'help' => 'Wyświetla pomoc, dostępne polecenia, czyli to co teraz widzisz :)', | |
'time' => 'Aktualna data i czas GMT miejsca pobytu Wikipediusa ;).', | |
'uptime' => 'Uptime bota Wikipedius oraz czas ostatniego uruchomienia skryptu.', | |
'stats' => 'Wyświetla statystyki. Obecnie w przygotowaniu :P.' | |
); | |
$about = 'Wikipedius jest pierwszym samodzielnym botem dostępnym dla wszystkich ' | |
.'użytkowników sieci komunikatora Tlen.pl. Wystarczy dodać do swojej listy ' | |
.'kontakt wikipedius@tlen.pl, aby korzystać z jego usług.' | |
."\n" | |
.'Jego głównym zadaniem jest łatwy dostęp do zasobów Wikipedii ' | |
.'[ www.wikipedia.pl ], pierwszej wolnej encyklopedii internetowej :)' | |
."\n\n" | |
.'Wikipedius powstał w marcu 2006 roku. Został stworzony przez MalComa ' | |
.'<malcom@o2.pl> jako pierwszy projekt wykorzystujący napisaną przez niego ' | |
.'klasę w php obsługującą protokół komunikatora Tlen.pl.' | |
."\n\n" | |
.'Więcej informacji o Wikipediusie można znależć na witrynie projektu ' | |
.'class.tlen.php pod adresem http://malcom.xorg.pl/ctp/'; | |
$upsince = time(); | |
$TLEN = new Tlen; | |
$TLEN->username = USERNAME; | |
$TLEN->password = PASSWORD; | |
$TLEN->resource = RESOURCE; | |
$TLEN->logging = TLEN_L_ALL; | |
$TLEN->log_filename = FILELOG; | |
$TLEN->Connect(); | |
$TLEN->SendAuth(); | |
$TLEN->SendStatus('available', $status); | |
while (1) { | |
$TLEN->CruiseControl(60); | |
if (!($TLEN->connected)) { | |
$TLEN->AddToLog('CHECK: Connection Dead... Reconnecting'); | |
$TLEN->Disconnect(); | |
sleep(1); | |
$TLEN->Connect(); | |
$TLEN->SendAuth(); | |
$TLEN->SendStatus('available', $status); | |
//sleep(5); | |
$TLEN->SendMessage(ADMIN, 'chat', "Reconnected..."); | |
} | |
} | |
function TlenHandlerPresence_subscribe($message) { | |
global $TLEN; | |
$from = $TLEN->GetInfoFromPresenceFrom($message); | |
$TLEN->Subscribe($from); | |
$TLEN->SubscriptionAcceptRequest($from); | |
} | |
function TlenHandlerPresence_subscribed($message) { | |
global $TLEN; | |
$from = $TLEN->GetInfoFromPresenceFrom($message); | |
preg_match('/(.*)@tlen.pl/Ui', $from, $temp); | |
$tid = $temp[1]; | |
$body = "Witaj $tid :)\n" | |
.'Nazywam sie Wikipedius i dziekuję Ci za autoryzację.' | |
."\n" | |
.'Jeśli chcesz się dowiedzieć więcej informacji o mnie wpisz "/about", ' | |
.'aby przejrzeć dostępne dla Ciebie polecenia wpisz "/help".' | |
."\n" | |
.'Może chcesz już skorzystać z zasobów Wikipedii?' | |
."\n" | |
.'Nic prostszego po prostu wpisz szukane hasło w rozmowie lub wiadomości '. | |
'a ja Ci przekaże ;)'; | |
//$TLEN->Subscribe($from); | |
$TLEN->SendMessage($from, 'chat', $body); | |
$TLEN->SendMessage($from, 'chat', 'aha... pozdrów też MalComa :D mojego boga :-]'); | |
} | |
function TlenHandlerMessage_chat($message) { | |
respond($message); | |
} | |
function TlenHandlerMessage_normal($message) { | |
respond($message); | |
} | |
function respond($message) { | |
global $TLEN, $about; | |
$type = $TLEN->GetInfoFromMessageType($message); | |
$from = $TLEN->GetInfoFromMessageFrom($message); | |
$body = $TLEN->GetInfoFromMessageBody($message); | |
// jesli wiadomosc od bota reklam z o2 to nic nie rob; | |
if ($from == 'b73@tlen.pl') | |
return; | |
if ($body{0} == '/') { | |
$param = explode(' ', $body); | |
$cmd = substr(array_shift($param), 1); | |
switch (strtolower($cmd)) { | |
case 'off': | |
if ($from == ADMIN) { | |
$TLEN->SendMessage($from, $type, "Zabijam swój proces...\nDisconnected..."); | |
$TLEN->Disconnect(); | |
exit; | |
} | |
$outmessage = 'What the fuck are you doing?!'; | |
break; | |
case 'about': | |
$outmessage = $about; | |
break; | |
case 'help': | |
$outmessage = help(@$param[0]); | |
break; | |
case 'time': | |
$outmessage = gmdate("D M j G:i:s T Y", time()); | |
break; | |
case 'uptime': | |
$outmessage = uptime(); | |
break; | |
case 'stats': | |
$outmessage = stats(); | |
break; | |
default : | |
$outmessage = '"'.$cmd.'" nie jest prawidłowym poleceniem.'."\n"; | |
$outmessage .= 'Wpisz "/help", aby wyświetlić dostępne polecenia.'; | |
} | |
$TLEN->SendMessage($from, $type, $outmessage); | |
} else { | |
// pobierz haslo z wikipedii | |
$outmessage = wikipedia($body); | |
if ($outmessage === false) | |
$outmessage = 'Brak strony dla hasła "'.$body.'"!'; | |
$TLEN->SendMessage($from, $type, $outmessage); | |
} | |
} | |
function help($command = NULL) { | |
global $help; | |
if (empty($command)) { | |
$usage = "Użycie: /help [polecenie]\nDostępne polecenia: "; | |
$usage .= implode(' ', array_keys($help)); | |
} else if (array_key_exists($command, $help)) { | |
$usage = $help[$command]; | |
} else { | |
$usage = 'Polecenie "'.$command.'" nie zostało zaimplementowane w aktualnej wersji.'; | |
} | |
return $usage; | |
} | |
function uptime() { | |
global $upsince; | |
$uptime = time() - $upsince; | |
$czas = gmdate('H:i:s', $uptime); | |
$dzien = gmdate('z', $uptime); | |
$dni = ($dzien > 1) ? $dzien .' dni i ' : ''; | |
$dni = ($dzien == 1) ? $dzien .' dzień i ' : $dni; | |
$uptimetxt = 'Zostałem uruchomiony ' . gmdate('D M j G:i:s T Y', $upsince)."\n"; | |
$uptimetxt .= 'uptime wynosi: ' . $dni . $czas . ' [' . $uptime . 's.]'; | |
return $uptimetxt; | |
} | |
function stats() { | |
return 'Error 503 Not implemented yet ;)'; | |
} | |
function wikipedia($keyword) { | |
// http://pl.wikipedia.org/wiki/Specjalna:Eksport/Komunikator_Tlen.pl | |
// $keyword = "Komunikator Tlen.pl"; | |
$keyword = str_replace(' ', '_', $keyword); | |
$opts = array( | |
'http' => array( | |
'method' => 'GET', | |
'header' => "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n". | |
"Connection: close\r\n" | |
) | |
); | |
$context = stream_context_create($opts); | |
$content = file_get_contents('http://pl.wikipedia.org/wiki/Specjalna:Eksport/'.$keyword, false, $context); | |
if (preg_match('|<text xml:space="preserve">#REDIRECT\s*\[\[(.*)\]\]\s*</text>|siU', $content, $matches)) | |
return $keyword.' przeniesiono do "'.$matches[1].'"!'; | |
// nie bedziemy sie bawic parserami XML, latwiej bedzie wyciagnac regexpem pozadane dane, | |
// czyli kilka akapitow artykulu, do napotkania naglowka, badz czegos innego | |
$regexp = "#<text xml:space=\"preserve\">(?:.*(?:}}|\]\])\s+)?('''.*)\s+(?:==|{{).*</text>#siU"; | |
if (preg_match($regexp, $content, $matches)) { | |
$patterns = array( | |
'/\[\[(?:[^\|]+\|)?([^\|]+)\]\]/sU', | |
'/\'+(.*)\'+/sU' | |
); | |
$replace = array('\\1', '\\1'); | |
$content = preg_replace($patterns, $replace, $matches[1]); | |
$content = strip_tags(html_entity_decode($content, ENT_COMPAT, 'UTF-8')); | |
// konwersja i link do reszty artykulu na wiki | |
$content = iconv('UTF-8', 'CP1250', $content); | |
$content .= "\n\nWięcej info na stronie: http://pl.wikipedia.org/wiki/$keyword"; | |
return $content; | |
} | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment