@@ -12,6 +12,7 @@ include_once "XML/RSS.php";
12
12
include_once 'Services/Amazon.php ' ;
13
13
require_once 'XML/Unserializer.php ' ;
14
14
include_once "include/simple_html_dom.php " ;
15
+ include_once 'HTTP/OAuth/Consumer.php ' ;
15
16
16
17
require_once 'Zend/Loader.php ' ;
17
18
\Zend_Loader::loadClass ('Zend_Gdata ' );
@@ -20,6 +21,7 @@ require_once 'Zend/Loader.php';
20
21
//Zend_Loader::loadClass('Zend_Gdata_Docs');
21
22
\Zend_Loader::loadClass ('Zend_Gdata_Spreadsheets ' );
22
23
\Zend_Loader::loadClass ('Zend_Gdata_YouTube ' );
24
+ \Zend_Loader::loadClass ('Zend_Service_Twitter ' );
23
25
24
26
include_once "./bot/base.class.inc " ;
25
27
@@ -31,14 +33,20 @@ class tyobot_common extends tyobot_base {
31
33
var $ google_docs ;
32
34
var $ google_youtube ;
33
35
34
- function __construct () {
36
+ var $ twitter_oauth ;
37
+
38
+ var $ conn = null ;
39
+
40
+ function __construct ( &$ conn ) {
41
+ parent ::__construct ();
35
42
$ this ->amazon = new \Services_Amazon (AMAZON_API_ACCESS_KEY , AMAZON_API_SECRET_KEY , AMAZON_ASSOCIATE_TAG );
36
43
$ this ->amazon ->setLocale ('JP ' );
37
44
38
45
$ this ->google_client = \Zend_Gdata_ClientLogin::getHttpClient ( GOOGLE_USER , GOOGLE_PASS , \Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME );
39
46
//$this->google_docs = new \Zend_Gdata_Docs($this->google_client);
40
47
$ this ->google_spreadseets = new \Zend_Gdata_Spreadsheets ($ this ->google_client );
41
48
$ this ->google_youtube = new \Zend_Gdata_YouTube ();
49
+ $ this ->conn = $ conn ;
42
50
}
43
51
44
52
function _setCommand ( &$ irc ){
@@ -50,6 +58,9 @@ class tyobot_common extends tyobot_base {
50
58
// twitter
51
59
$ irc ->registerActionhandler (SMARTIRC_TYPE_CHANNEL , '^ ' . TWITTER_CMD . ' ' , $ this , COMMAND_TWITTER );
52
60
61
+ // facebook
62
+ $ irc ->registerActionhandler (SMARTIRC_TYPE_CHANNEL , '^ ' . FACEBOOK_CMD . ' ' , $ this , COMMAND_FACEBOOK );
63
+
53
64
// Google
54
65
$ irc ->registerActionhandler (SMARTIRC_TYPE_CHANNEL , '^ ' . GOOGLE_CMD . ' ' , $ this , COMMAND_GOOGLE );
55
66
@@ -58,9 +69,21 @@ class tyobot_common extends tyobot_base {
58
69
59
70
// amazon
60
71
$ 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 );
61
78
62
79
// inviteされた
63
80
$ 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 ' );
64
87
}
65
88
66
89
/**
@@ -131,12 +154,12 @@ class tyobot_common extends tyobot_base {
131
154
$ worksheet_feed = $ this ->google_spreadseets ->getWorksheetFeed ( $ query );
132
155
$ str = '【G-s】 ' .$ worksheet_feed ->title ->text ;
133
156
}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 ]);
136
159
$ str = '【Tw】 ' .$ status ->user ->name .'「 ' .$ status ->text .'」 ' ;
137
160
}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 ]);
140
163
$ 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 ;
141
164
}else {
142
165
$ head = $ heads [$ heads ['count ' ]];
@@ -178,17 +201,23 @@ class tyobot_common extends tyobot_base {
178
201
switch ($ e ->getCode ()) {
179
202
case 404 :
180
203
case 403 :
181
- $ this -> _notice ( $ irc , 'this twitter request is ' .$ e ->getMessage (), $ data -> channel );
204
+ $ str = 'this twitter request is ' .$ e ->getMessage ();
182
205
break ;
183
206
default :
184
- $ this -> _notice ( $ irc , 'this twitter request to code: ' .$ e ->getCode (), $ data -> channel );
207
+ $ str = 'this twitter request to code: ' .$ e ->getCode ();
185
208
}
186
209
}
187
210
return $ str ;
188
211
}
189
212
190
213
function google (&$ irc , &$ data ) {
191
214
$ 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
+ }
192
221
$ text = substr ($ message , 2 );
193
222
d (memory_get_usage ());
194
223
$ 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 {
215
244
216
245
function calculator (&$ irc , &$ data ) {
217
246
$ message = $ this ->_2utf8 ($ data ->message );
218
- $ text = substr ($ message , 2 );
247
+ $ text = substr ($ message , strlen ( CALCULATOR_CMD )+ 1 );
219
248
$ m = Array ();
220
- if (preg_match ('/[^0123456789()+-\/\*\%]/ ' , $ text , $ m )) {
249
+ if (preg_match ('/[^0123456789()+-\/\*\% ]/ ' , $ text , $ m )) {
221
250
$ this ->_notice ($ irc , CALCULATOR_MSG , $ data ->channel );
222
251
return ;
223
252
}
@@ -226,28 +255,26 @@ class tyobot_common extends tyobot_base {
226
255
}
227
256
228
257
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 ];
233
260
$ str = "" ;
234
261
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 ];
238
265
if ($ cnt > TWITTER_TIMELINE_MAX ) {
239
266
$ cnt = TWITTER_TIMELINE_MAX ;
240
267
}
241
- $ timelines = $ twitter ->statuses -> user_timeline (Array ('id ' => $ id , 'count ' => $ cnt ));
268
+ $ timelines = $ twitter ->status -> userTimeline (Array ('id ' => $ id , 'count ' => $ cnt ));
242
269
foreach ($ timelines as $ timeline ) {
243
270
$ str .= date ('m/d h:i ' , strtotime ($ timeline ->created_at )).': ' .$ timeline ->text ."\n" ;
244
271
}
245
272
} else {
246
- $ user = $ twitter ->users ->show ($ id );
273
+ $ user = $ twitter ->user ->show ($ id );
247
274
$ 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 ;
248
275
}
249
276
$ this ->_notice ($ irc , $ str , $ data ->channel );
250
- } catch (\ Services_Twitter_Exception $ e ) {
277
+ } catch ( Zend_Service_Twitter_Exception $ e ) {
251
278
switch ($ e ->getCode ()) {
252
279
case 404 :
253
280
case 403 :
@@ -259,10 +286,28 @@ class tyobot_common extends tyobot_base {
259
286
}
260
287
}
261
288
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
+
262
308
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 ];
266
311
$ heads = get_headers ($ url , 1 );
267
312
$ status = strstr ($ heads [0 ], ' ' );
268
313
if ((int ) $ status != 200 ) {
@@ -278,10 +323,9 @@ class tyobot_common extends tyobot_base {
278
323
*/
279
324
280
325
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 .' ' );
283
327
$ str = "" ;
284
- $ result = $ this ->amazon ->ItemSearch ('All ' , Array ('Keywords ' => $ text ));
328
+ $ result = $ this ->amazon ->ItemSearch ('All ' , Array ('Keywords ' => join ( ' ' , $ args ) ));
285
329
if (is_array ($ result )) {
286
330
foreach (array_splice ($ result ['Item ' ], 0 , AMAZON_CNT ) as $ item ) {
287
331
$ str .= '■[ ' .$ item ['ItemAttributes ' ]['ProductGroup ' ].'] ' .$ item ['ItemAttributes ' ]['Title ' ];
@@ -293,6 +337,79 @@ class tyobot_common extends tyobot_base {
293
337
$ this ->_notice ($ irc , $ str , $ data ->channel );
294
338
}
295
339
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
+
296
413
/*
297
414
* BOT を終了する
298
415
*
0 commit comments