/
SmartIRC_JP.php
227 lines (193 loc) · 8.59 KB
/
SmartIRC_JP.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
include_once "Net/SmartIRC.php";
class Net_SmartIRC_JP extends Net_SmartIRC
{
var $_charset = 'auto';
/**
* 接続先IRCサーバーの内部文字コードを指定する。
* 現在はコマンドのチェック時のutf8への変換にしか使わない。
* 将来的にはSmartIRC内での文字コードをutf8に統一して、ライブラリの利用側では変換を意識しなくて良いようにすべきだと思う。
*
* @param string $charset
* @return void
* @access public
*/
function setCharset($charset){
$this->_charset = $charset;
}
/**
* tries to find a actionhandler for the received message ($ircdata) and calls it
* オーバーライド関数
* 正規表現のチェック時に入力をutf8に変換し、マルチバイトで行なう。
* (コマンドの設定も全てutf8で行なう事になる
*
* @param object $ircdata
* @return void
* @access private
*/
function _handleactionhandler(&$ircdata)
{
$handler = &$this->_actionhandler;
$handlercount = count($handler);
for ($i = 0; $i < $handlercount; $i++) {
$handlerobject = &$handler[$i];
if (substr($handlerobject->message, 0, 1) == '/') {
$regex = $handlerobject->message;
} else {
$regex = '/'.$handlerobject->message.'/';
}
if (($handlerobject->type & $ircdata->type) &&
(preg_match($regex, mb_convert_encoding( $ircdata->message, 'UTF-8', $this->_charset ) ) == 1)) {
$this->log(SMARTIRC_DEBUG_ACTIONHANDLER, 'DEBUG_ACTIONHANDLER: actionhandler match found for id: '.$i.' type: '.$ircdata->type.' message: "'.$ircdata->message.'" regex: "'.$regex.'"', __FILE__, __LINE__);
$methodobject = &$handlerobject->object;
$method = $handlerobject->method;
if (@method_exists($methodobject, $method)) {
$this->log(SMARTIRC_DEBUG_ACTIONHANDLER, 'DEBUG_ACTIONHANDLER: calling method "'.get_class($methodobject).'->'.$method.'"', __FILE__, __LINE__);
$methodobject->$method($this, $ircdata);
} else {
$this->log(SMARTIRC_DEBUG_ACTIONHANDLER, 'DEBUG_ACTIONHANDLER: method doesn\'t exist! "'.get_class($methodobject).'->'.$method.'"', __FILE__, __LINE__); }
}
}
}
/**
* registers a timehandler and returns the assigned id
*
* Registers a timehandler in Net_SmartIRC, which will be called in the specified interval.
* The timehandler id is needed for unregistering the timehandler.
*
* オーバーライド。
* 任意のオブジェクトを関数呼出しに付与する。
*
* @see example7.php
* @param integer $interval interval time in milliseconds
* @param object $object a reference to the objects of the method
* @param string $methodname the methodname that will be called when the handler happens
* @return integer assigned timehandler id
* @access public
*/
function registerTimehandler($interval, &$object, $methodname, &$args=null )
{
$id = $this->_timehandlerid++;
$newtimehandler = &new Net_SmartIRC_timehandler_ex();
$newtimehandler->id = $id;
$newtimehandler->interval = $interval;
$newtimehandler->object = &$object;
$newtimehandler->method = $methodname;
$newtimehandler->lastmicrotimestamp = $this->_microint();
$newtimehandler->args = &$args;
$this->_timehandler[] = &$newtimehandler;
$this->log(SMARTIRC_DEBUG_TIMEHANDLER, 'DEBUG_TIMEHANDLER: timehandler('.$id.') registered', __FILE__, __LINE__);
if (($interval < $this->_mintimer) || ($this->_mintimer == false)) {
$this->_mintimer = $interval;
}
return $id;
}
/**
* Checks the running timers and calls tee registered timehandler,
* when the interval is reached.
*
* チェック関数の拡張。
* 実行関数にオブジェクトを渡す。
*
* @return void
* @access private
*/
function _checktimer()
{
if (!$this->_loggedin) {
return;
}
// has to be count() because the array may change during the loop!
for ($i = 0; $i < count($this->_timehandler); $i++) {
$handlerobject = &$this->_timehandler[$i];
$microtimestamp = $this->_microint();
if ($microtimestamp >= ($handlerobject->lastmicrotimestamp+($handlerobject->interval/1000))) {
$methodobject = &$handlerobject->object;
$method = $handlerobject->method;
$handlerobject->lastmicrotimestamp = $microtimestamp;
if( is_null($methodobject) && is_object( $method ) ){
$this->log(SMARTIRC_DEBUG_TIMEHANDLER, 'DEBUG_TIMEHANDLER: calling Anonymous functions', __FILE__, __LINE__);
$method($this,$handlerobject->args);
}
else if (@method_exists($methodobject, $method)) {
$this->log(SMARTIRC_DEBUG_TIMEHANDLER, 'DEBUG_TIMEHANDLER: calling method "'.get_class($methodobject).'->'.$method.'"', __FILE__, __LINE__);
$methodobject->$method($this,$handlerobject->args);
}
}
}
}
/**
*
* 週一や1日1回といった形で実行したい関数を登録できるタイムハンドラーのカスタマイズ登録関数。
* 曜日を指定した場合は週一、曜日にnullを渡した場合は毎日指定時間に実行するようになる。
*
* @param integer $w 実行する曜日(1つしか指定できない)。 nullを指定された時は毎日。 日曜日を0とした数字をで指定する。
* @param integer $h 実行する時間。
* @param integer $m 実行する分。
* @param object $object a reference to the objects of the method
* @param string $methodname the methodname that will be called when the handler happens
* @return integer assigned timehandler id
* @access public
*/
function registerTimeScheduler( $w, $h, $m, &$object, $methodname, &$args=null)
{
if( is_null( $w ) ){
//1日分
$interval = 24*60*60;
$last_time = mktime( $h, $m, 0);
if( $last_time > time() ){ $last_time -= $interval; }
}else{
//1週間
$interval = 24*60*60*7;
$last_time = mktime( $h, $m, 0, date('n'), date('j') - ( date('w') - $w ) );
if( $last_time > time() ){ $last_time -= $interval; }
}
$id = $this->_timehandlerid++;
$newtimehandler = &new Net_SmartIRC_timehandler_ex();
$newtimehandler->id = $id;
$newtimehandler->interval = $interval*1000;
$newtimehandler->object = &$object;
$newtimehandler->method = $methodname;
$newtimehandler->lastmicrotimestamp = "$last_time.0000";//$this->_microint();
$newtimehandler->args = &$args;
$this->_timehandler[] = &$newtimehandler;
$this->log(SMARTIRC_DEBUG_TIMEHANDLER, 'DEBUG_TIMEHANDLER: timehandler('.$id.') registered', __FILE__, __LINE__);
if (($interval < $this->_mintimer) || ($this->_mintimer == false)) {
$this->_mintimer = $interval;
}
return $id;
}
/**
*
* 指定秒数後に1度だけ実行されるタスクを追加する。
*
* @param integer $s 実行を待つ秒数。
* @param string $method the methodname that will be called when the handler happens
* @return integer assigned timehandler id
* @access public
*/
function registerDilayAction( $s, $method, $args=null )
{
$id = null;
$id = $this->_timehandlerid++;
$newtimehandler = &new Net_SmartIRC_timehandler_ex();
$newtimehandler->id = $id;
$newtimehandler->interval = $s*1000;
$newtimehandler->object = null;
$newtimehandler->method = function($o) use($id,$method){
$this->unregisterTimeid($id);
$method($args);
};
$newtimehandler->lastmicrotimestamp = $this->_microint();
$newtimehandler->args = &$args;
$this->_timehandler[] = &$newtimehandler;
$this->log(SMARTIRC_DEBUG_TIMEHANDLER, 'DEBUG_TIMEHANDLER: timehandler('.$id.') registered', __FILE__, __LINE__);
if (($s < $this->_mintimer) || ($this->_mintimer == false)) {
$this->_mintimer = $s;
}
return $id;
}
}
class Net_SmartIRC_timehandler_ex extends Net_SmartIRC_timehandler{
var $args;
}