Skip to content

Commit 5f23563

Browse files
committed
twitterのライブラリをpearのものからZend_Serviceのものに変更
facebook対応(暫定) エラー処理を一部書き換え aliasの実装 指定ファイル内のtextをIRCへpostする機能 omikuji 格ゲー関連機能のtwitter連携 トーナメント機能から派生させたランバト管理機能を実装 (シーズン情報の保持やポイントの管理など どすこいコマンドのkakuge_anon対応 トーナメントのマッチングのズレを修正
1 parent 8e9b85b commit 5f23563

10 files changed

+481
-110
lines changed

bot.conf.inc.sample

+13-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@ define("SERVER_PORT", 6662); // 適当なポート
3131
define("CHANNEL", "#test"); // BOT をログインさせるチャンネル名
3232
define("IRC_ENCODING", "ISO-2022-JP-MS"); // 文字コード
3333

34-
define("TWITTER_ID","");
35-
define("TWITTER_PASS","");
34+
//twitter
35+
define("TWITTER_APPS_CONSUMER_KEY",'');
36+
define("TWITTER_APPS_CONSUMER_SECRET",'');
3637

38+
define("TWITTER_ID","tyobot");
39+
define("TWITTER_APPS_ACCESS_TOKEN",'');
40+
define("TWITTER_APPS_ACCESS_TOKEN_SECRET",'');
3741

38-
//define("KKG_TWITTER_ID","");
39-
//define("KKG_TWITTER_PASS","");
42+
define("KKG_TWITTER_ID","SSF4_PS3");
43+
define("KKG_TWITTER_APPS_ACCESS_TOKEN",'');
44+
define("KKG_TWITTER_APPS_ACCESS_TOKEN_SECRET",'');
45+
46+
//facebook
47+
define("FACEBOOK_APP_ID","");
48+
define("FACEBOOK_APP_SECRET","");
4049

4150
?>

bot.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
$conn->PConnect(DATABASE_HOST, DATABASE_ID, DATABASE_PASS, DATABASE_NAME);
2020
$conn->Execute('set names utf8');
2121

22-
$bot_c = &new tyobot\tyobot_common();
22+
$bot_c = &new tyobot\tyobot_common( $conn );
2323
$bot_r = &new tyobot\tyobot_ranbat( $conn );
2424
$bot_d = &new tyobot\tyobot_dani( $conn );
2525

bot/base.class.inc

+15
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,20 @@ class tyobot_base extends common_base {
2222
function _setCommand( &$irc ){
2323
d('please override:_setCommand');
2424
}
25+
26+
/**
27+
* DBのエラーを出力する。
28+
*
29+
* @param mixed $irc
30+
* @access private
31+
* @return void
32+
*/
33+
protected function sendDBError( &$irc = null ){
34+
d( $this->conn->ErrorMsg() );
35+
if(is_null($irc)){
36+
$this->_notice($irc, DANI_DB_ERROR, $data->channel );
37+
}
38+
}
39+
2540
}
2641
?>

bot/common.class.inc

+142-25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include_once "XML/RSS.php";
1212
include_once 'Services/Amazon.php';
1313
require_once 'XML/Unserializer.php';
1414
include_once "include/simple_html_dom.php";
15+
include_once 'HTTP/OAuth/Consumer.php';
1516

1617
require_once 'Zend/Loader.php';
1718
\Zend_Loader::loadClass('Zend_Gdata');
@@ -20,6 +21,7 @@ require_once 'Zend/Loader.php';
2021
//Zend_Loader::loadClass('Zend_Gdata_Docs');
2122
\Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
2223
\Zend_Loader::loadClass('Zend_Gdata_YouTube');
24+
\Zend_Loader::loadClass('Zend_Service_Twitter');
2325

2426
include_once "./bot/base.class.inc";
2527

@@ -31,14 +33,20 @@ class tyobot_common extends tyobot_base {
3133
var $google_docs;
3234
var $google_youtube;
3335

34-
function __construct() {
36+
var $twitter_oauth;
37+
38+
var $conn = null;
39+
40+
function __construct( &$conn ) {
41+
parent::__construct();
3542
$this->amazon = new \Services_Amazon(AMAZON_API_ACCESS_KEY, AMAZON_API_SECRET_KEY, AMAZON_ASSOCIATE_TAG);
3643
$this->amazon->setLocale('JP');
3744

3845
$this->google_client = \Zend_Gdata_ClientLogin::getHttpClient( GOOGLE_USER, GOOGLE_PASS, \Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME );
3946
//$this->google_docs = new \Zend_Gdata_Docs($this->google_client);
4047
$this->google_spreadseets = new \Zend_Gdata_Spreadsheets($this->google_client);
4148
$this->google_youtube = new \Zend_Gdata_YouTube();
49+
$this->conn = $conn;
4250
}
4351

4452
function _setCommand( &$irc ){
@@ -50,6 +58,9 @@ class tyobot_common extends tyobot_base {
5058
// twitter
5159
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^'. TWITTER_CMD . ' ', $this, COMMAND_TWITTER );
5260

61+
// facebook
62+
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^'. FACEBOOK_CMD . ' ', $this, COMMAND_FACEBOOK );
63+
5364
// Google
5465
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^'. GOOGLE_CMD . ' ', $this, COMMAND_GOOGLE );
5566

@@ -58,9 +69,21 @@ class tyobot_common extends tyobot_base {
5869

5970
// amazon
6071
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^'. AMAZON_CMD . ' ', $this, COMMAND_AMAZON );
72+
73+
// calculator
74+
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^'. CALCULATOR_CMD . ' ', $this, COMMAND_CALCULATOR );
75+
76+
// alias
77+
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^'. ALIAS_CMD . ' ', $this, COMMAND_ALIAS );
6178

6279
// inviteされた
6380
$irc->registerActionhandler(SMARTIRC_TYPE_INVITE, '.*', $this, '__invited__' );
81+
82+
// omikujiされた
83+
//$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^!omikuji', $this, 'omikuji' );
84+
85+
// 外部システムとの連携用に特定ファイルに書き込まれた文字をircに書き込み
86+
$irc->registerTimehandler( 10000, $this, 'checkPost');
6487
}
6588

6689
/**
@@ -131,12 +154,12 @@ class tyobot_common extends tyobot_base {
131154
$worksheet_feed = $this->google_spreadseets->getWorksheetFeed( $query );
132155
$str = '【G-s】'.$worksheet_feed->title->text;
133156
}else if (preg_match_all(URL_TWITTER_STATUS_LINK_REGEX, $url, $matches)) {
134-
$twitter =& new \Services_Twitter(TWITTER_ID, TWITTER_PASS);
135-
$status = $twitter->statuses->show($matches[3][0]);
157+
$twitter = new \Zend_Service_Twitter($this->twitter_oauth);
158+
$status = $twitter->status->show($matches[5][0]);
136159
$str = '【Tw】'.$status->user->name.''.$status->text.'';
137160
}else if (preg_match_all(URL_TWITTER_USER_LINK_REGEX, $url, $matches)) {
138-
$twitter =& new \Services_Twitter(TWITTER_ID, TWITTER_PASS);
139-
$user = $twitter->users->show($matches[1][0]);
161+
$twitter = new \Zend_Service_Twitter($this->twitter_oauth);
162+
$user = $twitter->user->show($matches[3][0]);
140163
$str = $user->name.' ('.$user->screen_name.') '.$user->url.' http://twitter.com/'.$user->screen_name.' bio:'.to_short($user->description, 40)."\n"."followers:".$user->followers_count.' following:'.$user->friends_count.' status:'.$user->statuses_count.' fav:'.$user->favourites_count;
141164
}else{
142165
$head = $heads[$heads['count']];
@@ -178,17 +201,23 @@ class tyobot_common extends tyobot_base {
178201
switch ($e->getCode()) {
179202
case 404:
180203
case 403:
181-
$this->_notice($irc, 'this twitter request is '.$e->getMessage(), $data->channel);
204+
$str = 'this twitter request is '.$e->getMessage();
182205
break;
183206
default:
184-
$this->_notice($irc, 'this twitter request to code:'.$e->getCode(), $data->channel);
207+
$str = 'this twitter request to code:'.$e->getCode();
185208
}
186209
}
187210
return $str;
188211
}
189212

190213
function google(&$irc, &$data) {
191214
$message = $this->_2utf8($data->message );
215+
if( $data->nick == 'kakuge_anon' ){
216+
$matches = array();
217+
if( preg_match( '/(.*)\(CV\:.*\)/i', $message ,$matches ) ){
218+
$message = $matches[1];
219+
}
220+
}
192221
$text = substr($message, 2);
193222
d(memory_get_usage());
194223
$html = file_get_html('http://www.google.com/search?ls=ja&hl=ja&oe=utf-8&q='.urlencode($text));
@@ -215,9 +244,9 @@ class tyobot_common extends tyobot_base {
215244

216245
function calculator(&$irc, &$data) {
217246
$message = $this->_2utf8($data->message );
218-
$text = substr($message, 2);
247+
$text = substr($message, strlen(CALCULATOR_CMD)+1);
219248
$m = Array();
220-
if (preg_match('/[^0123456789()+-\/\*\%]/', $text, $m)) {
249+
if (preg_match('/[^0123456789()+-\/\*\% ]/', $text, $m)) {
221250
$this->_notice($irc, CALCULATOR_MSG, $data->channel);
222251
return;
223252
}
@@ -226,28 +255,26 @@ class tyobot_common extends tyobot_base {
226255
}
227256

228257
function twitter(&$irc, &$data) {
229-
$message = $this->_2utf8($data->message );
230-
$text = substr($message, 2);
231-
$c = explode(' ', $text);
232-
$id = $c[0];
258+
$args = $this->_command( $this->_2utf8($data->message), TWITTER_CMD.' ' );
259+
$id = $args[0];
233260
$str = "";
234261
try {
235-
$twitter =& new \Services_Twitter(TWITTER_ID, TWITTER_PASS);
236-
if (isset($c[1]) && is_numeric($c[1])) {
237-
$cnt = $c[1];
262+
$twitter = new \Zend_Service_Twitter($this->twitter_oauth);
263+
if (isset($args[1]) && is_numeric($args[1])) {
264+
$cnt = $args[1];
238265
if ($cnt > TWITTER_TIMELINE_MAX) {
239266
$cnt = TWITTER_TIMELINE_MAX;
240267
}
241-
$timelines = $twitter->statuses->user_timeline(Array('id' => $id, 'count' => $cnt));
268+
$timelines = $twitter->status->userTimeline(Array('id' => $id, 'count' => $cnt));
242269
foreach ($timelines as $timeline) {
243270
$str .= date('m/d h:i', strtotime($timeline->created_at)).':'.$timeline->text."\n";
244271
}
245272
} else {
246-
$user = $twitter->users->show($id);
273+
$user = $twitter->user->show($id);
247274
$str .= $user->name.' ('.$user->screen_name.') '.$user->url.' http://twitter.com/'.$user->screen_name.' bio:'.to_short($user->description, 40)."\n"."followers:".$user->followers_count.' following:'.$user->friends_count.' status:'.$user->statuses_count.' fav:'.$user->favourites_count;
248275
}
249276
$this->_notice($irc, $str, $data->channel);
250-
} catch (\Services_Twitter_Exception $e) {
277+
} catch ( Zend_Service_Twitter_Exception $e) {
251278
switch ($e->getCode()) {
252279
case 404:
253280
case 403:
@@ -259,10 +286,28 @@ class tyobot_common extends tyobot_base {
259286
}
260287
}
261288

289+
function facebook(&$irc, &$data) {
290+
$args = $this->_command( $this->_2utf8($data->message), FACEBOOK_CMD.' ' );
291+
$id = $args[0];
292+
$str = "";
293+
try {
294+
$user = file_get_contents( 'https://graph.facebook.com/'.$id.'?access_token='.FACEBOOK_APP_ACCESS_TOKEN);
295+
$user_data = json_decode( $user );
296+
if( !isset($user_data->error) ){
297+
$str = $id.'('.$user_data->name.') '. $user_data->link.' '.$user_data->gender.' '.mb_ereg_replace("\n",' ',$user_data->about);
298+
}else{
299+
$str = FACEBOOK_MSG;
300+
}
301+
$this->_notice($irc, $str, $data->channel);
302+
303+
} catch (FacebookApiException $e) {
304+
d($e);
305+
}
306+
}
307+
262308
function ustream(&$irc, &$data) {
263-
$message = $this->_2utf8($data->message );
264-
$text = substr($message, 2);
265-
$url = USTREAM_URL.$text;
309+
$args = $this->_command( $this->_2utf8($data->message), USTREAM_CMD.' ' );
310+
$url = USTREAM_URL.$args[0];
266311
$heads = get_headers($url, 1);
267312
$status = strstr($heads[0], ' ');
268313
if ((int) $status != 200) {
@@ -278,10 +323,9 @@ class tyobot_common extends tyobot_base {
278323
*/
279324

280325
function amazon(&$irc, &$data) {
281-
$message = $this->_2utf8($data->message );
282-
$text = substr($message, 2);
326+
$args = $this->_command( $this->_2utf8($data->message), AMAZON_CMD.' ' );
283327
$str = "";
284-
$result = $this->amazon->ItemSearch('All', Array('Keywords' => $text));
328+
$result = $this->amazon->ItemSearch('All', Array('Keywords' => join(' ',$args)));
285329
if (is_array($result)) {
286330
foreach (array_splice($result['Item'], 0, AMAZON_CNT) as $item) {
287331
$str .= '■['.$item['ItemAttributes']['ProductGroup'].']'.$item['ItemAttributes']['Title'];
@@ -293,6 +337,79 @@ class tyobot_common extends tyobot_base {
293337
$this->_notice($irc, $str, $data->channel );
294338
}
295339

340+
/**
341+
* Alias
342+
*
343+
*/
344+
function alias(&$irc, &$data) {
345+
$args = $this->_command( $this->_2utf8($data->message), ALIAS_CMD.' ' );
346+
$text = $args[0];
347+
if(strlen($text)){
348+
$rs = &$this->conn->Execute('select * from alias WHERE alias = '.$this->conn->qstr($text));
349+
d( 'select * from alias WHERE alias = '.$this->conn->qstr($text));
350+
if(!$rs){ d($this->conn->ErrorMsg());}
351+
else if( $rs->RecordCount() ){
352+
$row = $rs->FetchRow();
353+
$this->_notice( $irc, $row['string'], $data->channel );
354+
}else{
355+
$this->_notice( $irc, ALIAS_MSG_ERROR, $data->channel );
356+
}
357+
}
358+
359+
}
360+
361+
function checkPost( &$irc ){
362+
if( defined( 'AUTO_IRC_MSG_FILE' ) ){
363+
$fp = fopen(AUTO_IRC_MSG_FILE,'r+');
364+
if( !$fp ){ return; }
365+
$buffer = array();
366+
while ($data=fgets($fp, 10000)){
367+
$buffer[] = $data;
368+
}
369+
$first = array_shift( $buffer );
370+
371+
ftruncate($fp,0);
372+
fseek( $fp, 0 );
373+
374+
foreach( $buffer as $row ){
375+
fputs( $fp, $row );
376+
}
377+
378+
fclose( $fp );
379+
if( !is_null( $first ) ){
380+
$channel = substr( $first, 0, strpos( $first, ' ' ) );
381+
$str = substr( $first, strpos( $first, ' ' )+1 );
382+
$this->_notice( $irc, $str, $channel );
383+
}
384+
}
385+
}
386+
387+
function omikuji(&$irc, &$data) {
388+
$list = array(
389+
28 =>'【大吉】',
390+
42 => '【中吉】',
391+
56 => '【吉】',
392+
67 => '【小吉】',
393+
78 => '【末吉】',
394+
85 => '【凶】',
395+
92 => '【大凶】',
396+
94 => '【ぴょん吉】',
397+
96 => '【だん吉】',
398+
97 => '【とらきち】',
399+
98 => '【豚】',
400+
99=>'【神】',
401+
100=>'【女神】');
402+
$num = rand(0,100);
403+
foreach( $list as $key => $val){
404+
if( $num <= $key ){
405+
$str = $data->nick."さんは...".$val."です!";
406+
$this->_notice( $irc, $str, $data->channel );
407+
return;
408+
}
409+
}
410+
411+
}
412+
296413
/*
297414
* BOT を終了する
298415
*

0 commit comments

Comments
 (0)