2009年2月2日 星期一

Karma守護者:Karma Keeper (By PHP)

警告:請慎用Karma守護者!過度濫用易導致眾叛親離。
 噗浪健康局關心您!
適用環境:Linux,Windows也可使用,只要可以執行PHP跟排程即可

◎執行用批次檔:plurk(下載)
◎主程式:plurk.php(下載)
◎訊息檔(00點):night(下載 )(可自行修改)
◎訊息檔(06點):morring(下載)(可自行修改)
◎訊息檔(12點):lunch(下載 )(可自行修改)
◎訊息檔(18點):dinner(下載 )(可自行修改)
◎訊息檔(隨機):rndmess(下載 )(可自行修改)

1.下載XMPPHP(下載 )
http://code.google.com/p/xmpphp/
2.解壓縮後第一層有許多範例檔及一個XMPHP範例檔
3.程式放在第一層
4.批次檔plurk內修改程式放至的路徑(EX:/usr/local/src/xmpphp/)
5.主程式plurk.php內修改Gtalk的帳號密碼
EX:
$GID = "GtalkID";
$GPW = "GtalkPW";
6.設定系統排程執行plurk批次檔即可運作

※如出現mbstring錯誤請安裝php-mbstring
yum install php-mbstring

運作說明:
預設規劃為一個小時執行一次,每逢0點、6點、12點、18點會從各自的訊息檔隨機抽選一行訊息發送,其他整點時間程式會以亂數決定是否發送訊息(大約十分之一的機率)

主程式:
<?php
$GID = "GtalkID";
$GPW = "GtalkPW";

function Modifying ($FileName, $Line, $LineStr = "\n")
{
$ReadStream = @fopen ($FileName, "r") or die ("無法建立讀取資料串流");
$Datas = @fread ($ReadStream, filesize ($FileName)) or die ("讀取檔案失敗");
fclose ($ReadStream);

$Body = explode ($LineStr, $Datas);
$Rnd = $Line%(count ($Body)-1);

return $Body[$Rnd];
}

function RndNum()
{
$password_len = 6;
$password = '';

$word = '123456789';
$len = strlen($word);

for ($i = 0; $i < $password_len; $i++) {
$password .= $word[rand() % $len];
}

return $password;
}

include 'XMPPHP/XMPP.php';

$ctime = "";
$ctime = date("H");

if ($ctime == "18")
{
$Mess = "dinner";
}
elseif ($ctime == "12")
{
$Mess = "lunch";
}
elseif ($ctime == "6")
{
$Mess = "morring";
}
elseif ($ctime == "0")
{
$Mess = "night";
}
elseif (rand(0, 9) == "5")
{
$Mess = "rndmess";
}
else
{
$Mess = "";
}


if ($Mess <> "")
{
$conn = new XMPPHP_XMPP('talk.google.com', 5222, $GID, $GPW, 'xmpphp', 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);

try {
$conn->connect();
$conn->processUntil('session_start');
$conn->presence();
$conn->message('bot@plurk.com', Modifying ($Mess, RndNum()));
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
}

?>

##EasyReadMore##