IRCãããã®å®è£ ã§è¦ãã¹ã¯ãªããè¨èª
PHP+SmartIRCã§IRCããããæ¸ãããã¨ããã話ã§ï¼SmartIRCãªãã©ã¤ãã©ãªã®åå¨ãå§ãã¦ç¥ã£ãï¼ä»ã®è¨èªã«ããï¼IRCã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã¯å¹¾ã¤ãç¥ã£ã¦ãããã©ï¼ããããåããã®ã¯ååãããã§ï¼ä½¿ãæ¹ã¯ãã£ã±ãã ï¼
ãããªããããæ©ã«ä½¿ãæ¹ï¼è¨èªãã©ã¤ãã©ãªãï¼ãæ¶ãããã¨è¨ããã¨ã§ï¼æåãªã¹ã¯ãªããè¨èªã§IRCããããå®è£
ãã¦ããã²ã¨ããã¢ã¼ãéå¬ãããã¨ã«ããã®ã ã£ãï¼ãã¼ãã¼ï¼
IRCãããã®ä»æ§
ä»æ§ã¯ä»¥ä¸ã®éãï¼
- 4æ¡ãªãã5æ¡ã®ãã¼ãçªå·ãçºè¨ããã¨ãããã¯ã¼ã¯å¯¾æ¦ã²ã¼ã ã®åéæãNOTICEçºè¨ãã
- "good night, jewel."ã¨çºè¨ããã¨è½ã¡ã
IRCãããã©ã¤ãã©ãªã®åºæ¥ã ãã§ã¯ãªãï¼DNSãªã¾ã«ãã©ã¤ãã©ãªã®æç¡ãåºæ¥ã¾ã§åããããã¨ã«ãªãã®ã¯æ³å®å¤ã ã£ããã©ï¼ãªããªã楽ãããã¢ã¼ã«ãªã£ãï¼
PHP
PHPã¯``<?php"ã¨``?>"ã§æã¾ããé åãï¼PHPã®ããã°ã©ã ã³ã¼ãã¨ãã¦è§£éããï¼ãã以å¤ã®é åã¯ãã®ã¾ã¾åºåããããã«ãªã£ã¦ããï¼ä¸»ã«HTMLã½ã¼ã¹ã«åãè¾¼ãã§ä½¿ããããï¼ãã®ä½¿ç¨æ³ããããã¨ï¼HTMLã®ããªããã»ããµã¨ããä½ç½®ã¥ããªã®ããç¥ããªãï¼
æ¬é¡ã®IRCãããã®å®è£
ã ãã©ï¼PHPã§ã¯Net_SmartIRCã¨ããã©ã¤ãã©ãªã使ç¨ãããã¨ã«ãã£ã¦ï¼ç°¡åã«IRCããããä½æãããã¨ãåºæ¥ãï¼
Net_SmartIRC is a PHP class for communication with IRC networks
PEAR - Net_SmartIRC
æ£è¦è¡¨ç¾ã®æ¡ä»¶ã«ãã£ã¦ï¼æå®ããã«ã¼ãã³ãå¼ã³åºãæ©è½ãå®è£
ããã¦ããã®ã§ï¼èªåã§ãã£ã¹ãããããæéãçããã¨ãåºæ¥ãï¼ãããï¼ãã®æ©è½ã§ã¯è¤éãªèµ·åæ¡ä»¶ãæå®ãããã¨ã¯åºæ¥ãï¼ãããè¡ãã«ã¯ï¼èªåã§ãã£ã¹ãããããå¿
è¦ãããï¼
ã¾ãï¼ãã¿ã¼ã³ãããã³ã°ã«ãããæå符å·åæ¹å¼ã®åé¡ãåå¨ããï¼Net_SmartIRCã¯ï¼åä¿¡ããæååã符å·åæ¹å¼å¤æããï¼ãã®ã¾ã¾ãã¿ã¼ã³ãããã³ã°å¦çã«æããããã«ãªã£ã¦ããï¼ã½ã¼ã¹ã³ã¼ãã®ç¬¦å·åæ¹å¼ã¨ç°ãªãæååããã¿ã¼ã³ãããã³ã°ãã¦ä¸è´ãããã«ã¯ï¼mb_convert_encodingçãç¨ãã¦ï¼æ£è¦è¡¨ç¾æååã®ç¬¦å·åæ¹å¼ãåä¿¡æååã«åããããï¼ã½ã¼ã¹ã³ã¼ãã®ç¬¦å·åæ¹å¼ãåä¿¡æååã«åãããå¿
è¦ãããï¼
IRCã§æ¥æ¬èªæãéä¿¡ããéï¼freenodeã§ã¯UTF-8ï¼IRCNetã§ã¯iso-2022-jpã¨ï¼ãããã¯ã¼ã¯ã«ãã£ã¦æ§ã
ãªç¬¦å·åæ¹å¼ã使ç¨ããã¦ãããã©ï¼å¾è
ã®iso-2022-jpã«mb_convert_encodingã§å¤æãã¦ç¨ãããã¨ããã¨ï¼æ£è¦è¡¨ç¾ã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªã£ã¦ãã¾ãï¼ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã®ä¸ã«æ£è¦è¡¨ç¾ã§ã¯ç¹å¥ãªæå³ãæã¤æ¬å¼§ãå«ã¾ãã¦ããããã ï¼
// ã½ã¼ã¹ã®ç¬¦å·åæ¹å¼ãSJISã®ã¨ãã // ããã¯æ£è¦è¡¨ç¾ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªã $irc->registerActionHandler(SMARTIRC_TYPE_CHANNEL, $bot->encode('ä½ãæ¥æ¬èª'), $bot, 'onSomeMacro');
ãããåé¿ããã«ã¯ï¼å
¨ã¦ã®çºè¨ã«åå¿ããå¦çãç»é²ãï¼ããã§æååå¤æãè¡ã£ã¦ï¼èªåã§ãã£ã¹ãããããå¿
è¦ãããï¼ããã§ã¯ãã®æ©è½ãç¡æå³åãã¦ãã¾ãã»ã»ã»ã¨ã¯ãã£ã¦ãã©ããããããªãã®ã§ï¼æ¥æ¬èªã使ãå ´åã¯ãã®æ¹æ³ã§è¡ããã¨ã«ããï¼
ãã¹ãåããIPã¢ãã¬ã¹ãå¾ãã«ã¯Net_DNSã©ã¤ãã©ãªãç¨ããï¼
Resolver library used to communicate with a DNS server.
PEAR - Net_DNS
ããã¥ã¡ã³ãã貧弱ã§ï¼ä½¿ãæ¹ããããã«ããï¼èª¿ã¹ãçµæï¼ã¨ãããã次ã®ã³ã¼ããç解ã§ããã¾ã§ã«ã¯ãªã£ãï¼DNS_SERVER_HOSTã§æå®ããDNSãµã¼ãã«$hostã«æç¸ããããã¹ãåãåãåããã¦ï¼çµæããAã¬ã³ã¼ãã«å«ã¾ããã¢ãã¬ã¹ï¼ã¤ã¾ãæ£å¼ããããIPã¢ãã¬ã¹ãåå¾ããï¼
include_once "Net/DNS.php"; define("DNS_SERVER_HOST", "DNSãµã¼ãã®ãã¹ãå"); $result = null; // ãªã¾ã«ãã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæ $resolver = new Net_DNS_Resolver(array( 'nameservers' => array(DNS_SERVER_HOST) )); // åãåããã if ($response = $resolver->query($host)) { // åãåããçµæããAã¬ã³ã¼ããæ¢ã foreach ($response->answer as $rr) { if ($rr->type === 'A') { $result = $rr->address; break; } ï½ }
ãããã®ã©ã¤ãã©ãªãçµã¿åããã¦ç®çç©ãããã£ã¨ä»ä¸ããï¼
<?php include_once "Net/SmartIRC.php"; include_once "Net/DNS.php"; // ã·ã¹ãã ç°å¢æååãShift_JISï¼Windowsä»æ§ï¼ã¨è¦ãªã mb_internal_encoding("sjis-win"); define("IRC_ENCODING", "iso-2022-jp"); define("DNS_SERVER_HOST", "192.168.3.1"); class MyIRCBot { // ãã¹ãåã«å¯¾å¿ããIPã¢ãã¬ã¹ãåå¾ãã function dnsLookup($host) { $result = null; $resolver = new Net_DNS_Resolver(array( 'nameservers' => array(DNS_SERVER_HOST) )); if ($response = $resolver->query($host)) { foreach ($response->answer as $rr) { if ($rr->type === 'A') { $result = $rr->address; break; } } } return $result; } // åéãã¯ãã®ã«ã¼ãã³ function onGameOffer(&$irc, &$data) { // çºè¨è ã®IPã¢ãã¬ã¹ãå¼ã if ($address = $this->dnsLookup($data->host)) { // NOTICEã§åéè¦é ãçºè¨ãã $irc->message(SMARTIRC_TYPE_NOTICE, $data->channel, $this->encode( $data->nick . "ããã" . $address . ":" . $data->message . "ã§åéä¸ã ãã") ); } } // ãããè½ã¨ããã¯ãã®ã«ã¼ãã³ function onQueryQuit(&$irc, &$data) { $irc->quit($this->encode("good bye.")); } // PHPã·ã¹ãã ç°å¢æååâIRCæ¥æ¬èªæååã®å¤æ function encode($str) { return mb_convert_encoding($str, IRC_ENCODING); } // IRCæ¥æ¬èªæååâPHPã·ã¹ãã ç°å¢æååã®å¤æ function decode($str) { return mb_convert_encoding($str, mb_internal_encoding(), IRC_ENCODING); } } // å種ã¯ã©ã¹ãã¤ã³ã¹ã¿ã³ã¹å $bot = &new MyIRCBot(); $irc = &new Net_SmartIRC(); // ãããã°æ å ±ãå ¨é¨è¡¨ç¤ºãã $irc->setDebug(SMARTIRC_DEBUG_ALL); // ãã¤ãã£ãã½ã±ããã使ãããã«ãã $irc->setUseSockets(TRUE); // åéãã¯ããç»é² $irc->registerActionHandler(SMARTIRC_TYPE_CHANNEL, $bot->encode('^[1-9]\d{3,4}$'), $bot, 'onGameOffer'); // ãããè½ã¨ããã¯ããç»é² $irc->registerActionHandler(SMARTIRC_TYPE_CHANNEL, $bot->encode('^good night, jewel.$'), $bot, 'onQueryQuit'); // IRCãµã¼ãã«æ¥ç¶ï¼ãã°ã¤ã³ãã $irc->connect('ircnet.eversible.com', 6667); $irc->login('anon_193', 'RealName', 0, 'anon_193'); // æ¥ç¶ã§ããããã£ã³ãã«ã«å ¥ã $irc->join(array($bot->encode('#some_irc_room'))); // ã¡ã¤ã³ã«ã¼ãã«å ¥ã $irc->listen(); ?>
ããæ¸ãã¦ã¿ãã¨ï¼çµæ§ç°¡ç´ ã«æ¸ãããã¨ãåãã£ãï¼DNSãµã¼ãã¨ãã¦Windowããã©ã«ãã®ãå¼ãã¦ãããã°è¯ããã ãã©ã»ã»ã»
æ¸ãã¦åãã£ããã¨ã¯ï¼
- ã©ã¤ãã©ãªã®ãã¼ãã¯include_once "ã©ã¤ãã©ãªå"
- å®æ°ã®å®ç¾©ã¯define("å®æ°å", å®æ°å¤)
- ã¯ã©ã¹ã®å®ç¾©ã¯class ã¯ã©ã¹å { å®ç¾© }
- é¢æ°ã®å®ç¾©ã¯function é¢æ°å(ä»®å¼æ°) { å®ç¾© }
- æ¡ä»¶åå²ã¯if (æ¡ä»¶å¼) { thenç¯ }
- åæã¯foreach (åæå¯è½ãªãªãã¸ã§ã¯ã as è¦ç´ ) { æ }
- å¤æ°ã®æ¥é è¾ã¯$
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã³ãã®ã¢ã¯ã»ã¹ã¯->ãç¨ãã
- é£æ³é åã¯array(ãã¼ => å¤)
- æååã¯''ï¼""ã®æ¸ãæ¹ããã
- åç §æ¸¡ãï¼ã«æ¥é è¾&ããã
å®æ°ã®å®ç¾©ãä¸æè°ã ãï¼ãã以å¤ã¯JavascriptãRubyï¼C++çã主ã«ä½¿ã£ã¦ããæçºãï¼å²ã¨è¦ªãã¿ã®æã¦ãæ触ãã ã£ãï¼ããã«ï¼ç´ ç´ã«æ¸ããã®ã¯éè¦ãªãã¨ã ï¼
Ruby
æ¢ã«è²ã
使ã£ã¦ããã®ã§ä½ãè¨ããã¨ã¯ãªãï¼
Rubyã§IRCããããæ¸ãã«ã¯ï¼net/ircã¨è¨ãã©ã¤ãã©ãªã使ãï¼IRCã¯ã©ã¤ã¢ã³ãã¯ã©ã¹Net::IRC::Clientãç¶æ¿ãã¦ï¼å種ã¤ãã³ããã³ãã©ãå®ç¾©ï¼ãããå¦çã®ã«ã¼ãã³ã«ãã£ã¹ãããããã¨ããå½¢ã§å®è£
ããï¼
ãã¹ãåããIPã¢ãã¬ã¹ãå¼ãæ¹æ³ã¯å¹¾ã¤ããããã©ï¼IPSocket#getaddressãæãç°¡åã«å¼ããã¨ãã§ããï¼å
·ä½çãªã³ã¼ãã¯ä»¥ä¸ã®éãï¼
require 'socket' address = IPSocket.getaddress("ãã¹ãå")
ãã¹ããè¤æ°ã®ã¢ãã¬ã¹ãæã¤å ´åã®æåã¯èª¿ã¹ã¦ããªãï¼ä¾å¤ãçºçããããç¥ããªãï¼
æµç³ã¡ã¤ã³ã§ä½¿ã£ã¦ããè¨èªã ãå¨ã£ã¦å®è£
ã¯ãã£ã¨ããéã«åºæ¥ä¸ãã£ãï¼
#!ruby -Ks IRC_ENCODING='iso-2022-jp' require 'rubygems' require 'net/irc' require 'pp' require 'socket' class IRCBot < Net::IRC::Client def initialize(*args) super end def on_rpl_welcome(m) post JOIN, "#some_irc_room".encode(IRC_ENCODING).force_encoding('external') end def on_privmsg(m) msg = m[1].encode('external') if msg =~ /^[1-9]\d{3,4}$/ then on_gameoffer m elsif msg =~ /^good night, jewel.$/ then on_queryquit m end end def on_gameoffer(m) if (address = IPSocket.getaddress(m.prefix.host)) != nil then post NOTICE, m[0], (m.prefix.nick + "ããã" + address + ":" + m[1] + "ã§åéä¸ã ãã").encode(IRC_ENCODING).force_encoding('external') end end def on_queryquit(m) post QUIT, "good bye." end end IRCBot.new("ircnet.eversible.com", 6667, { :nick => "anon_193", :user => "anon_193", :real => "anon_193" }).start
ã³ã¼ãããåãããã¨ã¯
- å®æ°ã®å®ç¾©ã¯å¤§æåï¼
- ã¯ã©ã¹ã®å®ç¾©ã¯class ã¯ã©ã¹å < ç¶æ¿å ã¯ã©ã¹ å®ç¾© end
- é¢æ°ã®å®ç¾©ã¯def é¢æ°å(ä»®å¼æ°) å®ç¾© end
- æ¡ä»¶åå²ã¯if æ¡ä»¶å¼ thenç¯ [elsif æ¡ä»¶å¼ thenç¯] end
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã³ãã¸ã®ã¢ã¯ã»ã¹ã¯.(åè§ããªãªã)
- é£æ³é åã¯{ :ãã¼ => å¤ }
- æ£è¦è¡¨ç¾ãªãã©ã«ããã
- æ£è¦è¡¨ç¾ã«ãããã¿ã¼ã³ãããã³ã°ã¯=~æ¼ç®å
使ã£ã¦ããã®ã§æ £ãããã®ã ãã©ï¼elsifãã¡ãã£ã¨ä¸æºããªï¼elsifã¯ã¾ã¨ã¾ã£ã¦ããã¨ããç¹ã§è¯ããã©ï¼å¿æ çã«else ifã®æ¹ã好ãï¼
Python
人æ°ãªã®ãæµ·å¤ã§ããè¦ãã¹ã¯ãªããè¨èªï¼ä»ã®è¨èªã§ã¯è¦ãç®ãæ´ããç¨åº¦ã®æå³ãããªãã¤ã³ãã³ããï¼ãã®è¨èªã§ã¯ãããã¯ã表ãçéè¦ãªæå³ãæã¤ãããï¼Lispã¯æ¬å¼§å°çã§ãã©ã¤ã¨ãæã£ã¦ããã©ï¼ALGOLç³»ã®è¨èªã大æ¦æ¬å¼§å¯¾å¿ã§ãããï¼ãã®ç
©ããããã解æ¾ãããã¨ããç¹ã§ã¯ï¼è¯ãè¨èªä»æ§ãããªããããã¨æè¿æãå§ãã¦ããï¼ã¾ãï¼ãã®è¨èªã«ã¯ä¸ã¤ã®ãã¨ã«ã¯ä¸ã¤ã®æ¹æ³ã¨ãè¨ãä¿¡æ¡ãããããããã©ï¼è§¦ããã°ãããªã®ã§æ·±ããã¨ã¯ä½ãããåãããªãï¼
ãã¦ï¼Pythonã§IRCããããæ¸ãã«ã¯ï¼python-irclibã¨ããã©ã¤ãã©ãªã使ãï¼Rubyã¨åãããã«IRCã¯ã©ã¤ã¢ã³ãã¯ã©ã¹SingleServerIRCBotãç¶æ¿ãã¦ï¼ãã®ã¤ãã³ããã³ãã©ãè¨è¿°ããã¨ããå½¢ã«ãªãï¼ããããã®python-irclibï¼ããã¥ã¡ã³ãã®ä¸æ´åæ¯ããã²ã©ããã®ã§ï¼æçµçã«ï¼Webä¸ã§å
¬éããã¦ããå
¬å¼ã§ãªããµã³ãã«ã¨ã©ã¤ãã©ãªã®ã½ã¼ã¹ã³ã¼ããããã¥ã¢ã«ä»£ããã«ãããã¡ã«ãªã£ãï¼ããçªããæãæ¨ã¦ããï¼
ãã¹ãåããIPã¢ãã¬ã¹ãå¼ãã«ã¯ï¼python-pydnsã¨è¨ãã©ã¤ãã©ãªã使ãï¼ãããpython-irclibã«å¼ããåããï¼ããã¥ã¡ã³ãã®ä¸æ´åæ¯ããç®ã«ä»ãï¼ã½ã¼ã¹ã³ã¼ããããã¥ã¢ã«ã¨è¨ããã°ããã®ç·æ°æº¢ããã©ã¤ãã©ãªã¯è¨èªã®èªè§£ååä¸ã«ä¸å½¹è²·ã£ã¦ããããã©ï¼ããã¨å¼ãæ¿ãã«ãã£ããæ´»åã¨ãã«ã®ã¼ã¨æ´»åæéãæã£ã¦è¡ãããã®ã¯ãã¤ãï¼
pydnsã¯DNSããã±ã¼ã¸ã®ï¼ããã±ã¼ã¸ã§æ§æããã¦ããï¼DNSãµã¼ããªã¹ãã¯åæç¶æ
ã§ã¯ç©ºã§ï¼ãªã¾ã«ãã¯ã©ã¹ã®çæã«å¿ãã¦ã©ãããããã¼ããã¦ããããªãã¦æ©è½ã¯ãªãï¼DNS.DiscoverNameServersã¡ã½ãããæ示çã«å¼ã³åºããã¨ã«ãã£ã¦ï¼ãµã¼ããªã¹ãããã¼ããããï¼ãã®ã¡ã½ããã¯åä½ãã©ãããã©ã¼ã ã«ãã£ã¦åä½ãåãæ¿ãã¦ãã¦ï¼Windowsã§ã¯ã¬ã¸ã¹ããªããï¼ãã®ä»ã§ã¯resolv.confããå¾ãããã«ãªã£ã¦ããï¼
pydnsãç¨ãã¦æå®ãããã¹ãåããIPã¢ãã¬ã¹ãå¾ãã³ã¼ãã¯æ¬¡ã®ããã«ãªãï¼
def dns_lookup(host): DNS.DiscoverNameServers() ans = DNS.Request(host).req().answers for rec in ans: if rec['typename'] == 'A': return rec['data'] return null
req()ã¡ã½ããã§ãªã¯ã¨ã¹ããçºè¡ããï¼req()ã¡ã½ããã®è¿ãå¤ãæã¤answersã¡ã³ããåæå¯è½ãªã¬ã¹ãã³ã¹ã表ãã¦ããï¼ãããï¼åè¦ç´ ã¯é£æ³é
åã§typenameãã¼ãæã¡ï¼Aã¬ã³ã¼ãã®å ´åã¯ï¼dataãã¼ã«IPã¢ãã¬ã¹ãä¿æãã¦ããï¼ãããï¼
å®è£
ã¯Rubyã¨ä¼¼ããããªæãã§ãã£ããã¨å®è£
ã§ããï¼
# coding: shift_jis IRC_SERVER = "ircnet.eversible.com" IRC_PORT = 6667 IRC_NICK = "anon_193" import re import DNS from irclib import nm_to_n from irclib import nm_to_h from ircbot import SingleServerIRCBot class IRCBot(SingleServerIRCBot): def __init__(self): print ("connecting.") SingleServerIRCBot.__init__(self, [(IRC_SERVER, IRC_PORT)], IRC_NICK, IRC_NICK) self.channel = u"#some_irc_room".encode("iso-2022-jp") def on_welcome(self, irc, e): print ("on_welcome") irc.join(self.channel) def on_privmsg(self, irc, e): try: nick = nm_to_n(e.source()) host = nm_to_h(e.source()) msg = unicode(e.arguments()[0], "iso-2022-jp") except Exception, e: print(str(e)) return e.nick = nick e.host = host e.msg = msg if re.match("^[1-9]\d{3,4}$", msg): self.on_gameoffer(e) elif re.match("^good night, jewel.$", msg): self.on_queryquit(e) def on_gameoffer(self, e): addr = self.dns_lookup(e.host) if addr: msg = e.nick + u"ã" + addr + u":" + e.msg + u"ã§åéä¸ã ãã" self.connection.notice(self.channel, msg.encode("iso-2022-jp")) def on_queryquit(self, e): self.connection.quit("good bye.") def dns_lookup(self, host): DNS.DiscoverNameServers() ans = DNS.Request(host).req().answers for rec in ans: if rec['typename'] == 'A': return rec['data'] return null on_pubmsg = on_privmsg bot = IRCBot() bot.start()
ã³ã¼ããæ¸ãã¦åãã£ããã¨ã¯ï¼
- ã©ã¤ãã©ãªã®ã¤ã³ãã¼ãã¯import ã©ã¤ãã©ãªå
- from ã©ã¤ãã©ãªå import èå¥åãã§åå¥ã«ã¤ã³ãã¼ããããã¨ãåºæ¥ã
- ã¯ã©ã¹ã®å®ç¾©ã¯class ã¯ã©ã¹å(ç¶æ¿å ã¯ã©ã¹): ã¤ã³ãã³ãä¸ãã§å®ç¾©å 容
- é¢æ°ã®å®ç¾©ã¯def é¢æ°å(ä»®å¼æ°): ã¤ã³ãã³ãä¸ãã§å®ç¾©å 容
- åæ㯠for è¦ç´ in ã³ã¬ã¯ã·ã§ã³: ã¤ã³ãã³ãä¸ãã§ç¹°ãè¿ãæ
- æ¡ä»¶åå²ã¯ if æ¡ä»¶å¼: ã¤ã³ãã³ãä¸ãã§thenç¯ [elif æ¡ä»¶å¼: ã¤ã³ãã³ãä¸ãã§thenç¯]
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã³ãã¸ã®ã¢ã¯ã»ã¹ã¯.(åè§ããªãªã)
- æååãªãã©ã«ã¯''ã¨""
- æ¥æ¬èªæååãªãã©ã«ã¯æååãªãã©ã«ã®åã«uãã¤ããï¼unicodeã®ç¥ï¼ï¼
- ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¯__init__é¢æ°
- thiså¤æ°ã®åå¨ãé¢æ°ã®å¼æ°ã¨ãã¦æè¨ãã¦ãã
- ææ«è¨å·ããªãï¼ææ«ï¼æ¹è¡ï¼
ã¨ã«ããã¤ã³ãã³ããã³ã¼ãã®ãããã¯ã¨ãã¦æå³ãæã£ã¦ããï¼æ¬å¼§ãã³ã¼ãã®ãããã¯ã«ãã¦ããè¨èªã¨æ¯ã¹ã¦ã¿ãã¨ï¼ç¢ºãã«ãã£ãããã¦ããããç¥ããªãï¼ãããã¯ã®çµããã«ã¡ãã£ã¨éåæãæãããã©ï¼
Perl
å¤æ
è¨èªï¼è¤ãè¨è
è¨è¨å½åã¯BASICçãªä½ããç®æãã¦ããã¨èãã¦ãããã»ã»ã»ãªãã¸ã§ã¯ãå¨ãã®è¨è¿°ãããªãããªã¼ãã ã ã£ãè¨æ¶ãå¹½ãã«ããï¼C++ã©ã¤ã¯ãªæ¸ãæ¹ããSmalltalkçãªã¡ãã»ã¼ã¸ããã·ã³ã°é¢¨ã®æ¸ãæ¹ã¾ã§ï¼è¦ãããªï¼
Perlã§IRCå¨ããä¸è©±ããã«ã¯ï¼POEã使ãï¼
POE is a framework for creating event-driven cooperative multitasking programs in Perl.
POE: Perl Object Environment
POEã¯Perlã§æ¸ãããã¤ãã³ãé§åã¢ããªã±ã¼ã·ã§ã³ã®ããã®ãã¬ã¼ã ã¯ã¼ã¯ã§ï¼æ§ã
ãªãµã¼ãã¹ï¼ã³ã³ãã¼ãã³ãï¼ããã®ä¸ã§åãããã¨ãã§ãããããï¼POEã³ã³ãã¼ãã³ãï¼PoCoï¼ã®ã²ã¨ã¤ãPOE::Component::IRCãIRCã¯ã©ã¤ã¢ã³ãã¨ãã¦ã®å½¹å²ãæããã¦ãããï¼POEã®æ§é ã®ãããã§ï¼æç¶ããä»ã®è¨èªã¨æ¯ã¹ã¦å°ã
è¤éã«ãªã£ã¦ããï¼
ãã¹ãåããIPã¢ãã¬ã¹ãå¼ãã«ã¯Net::DNSã使ãï¼ä½¿ãæ¹ã¯ä»ã®è¨èª(Rubyé¤ãï¼ã¨ã»ã¼åãããã ã£ãï¼
POEã®æçºã§ãããããã£ãã®ã§ï¼ãµã³ãã«ãè¦ãªããè¦ããè¦ã¾ãã§å®è£
ããï¼
use utf8; use strict; use Encode; use Net::DNS; use POE qw(Component::IRC); my $ircserver = 'ircnet.eversible.com'; my $ircport = 6667; my $nickname = 'anon_193'; my $ircencoding = 'iso-2022-jp-ext'; my $irc = POE::Component::IRC->spawn( nick => $nickname, username => $nickname, ircname => $nickname, server => $ircserver, port => $ircport ) or die "$!"; POE::Session->create( package_states => [ 'main' => [ qw(_start irc_001 irc_public) ], ], heap => { irc => $irc }, ); $poe_kernel->run(); sub _start { my ($kernel, $heap) = @_[KERNEL, HEAP]; my $irc_session = $heap->{irc}->session_id(); $kernel->post( $irc_session => register => 'all'); $kernel->post( $irc_session => connect => {} ); } sub irc_001 { my ($kernel, $sender) = @_[KERNEL, SENDER]; my $channel = irc_encode("#some_irc_room"); $kernel->post( $sender => join => $channel); } sub irc_public { my ($kernel, $sender, $who, $where, $what) = @_[KERNEL, SENDER, ARG0, ARG1, ARG2]; if ( $what =~ /^[1-9]\d{3,4}$/ ) { irc_gameoffer($kernel, $sender, $who, $where, $what); } elsif ( $what =~ /^good night, jewel.$/ ) { irc_queryquit($kernel, $sender, $who, $where, $what); } } sub irc_gameoffer { my ($kernel, $sender, $who, $where, $what) = @_; my $nick = ( split /!/, $who )[0]; my $host = ( split /@/, $who )[1]; my $channel = $where->[0]; my $addr = dns_lookup($host); if ( $addr ) { my $msg = $nick . "ããã" . $addr . ":" . $what . "ã§åéä¸ã ãã"; $kernel->post( $sender => notice => $channel => irc_encode($msg) ); } } sub irc_queryquit { my ($kernel, $sender) = @_; $kernel->post( $sender => quit => "good bye." ); } sub irc_decode { my ($str) = @_; Encode::decode($ircencoding, $str); } sub irc_encode { my ($str) = @_; Encode::encode($ircencoding, $str); } sub dns_lookup { my ($host) = @_; my $res = Net::DNS::Resolver->new; if ( my $query = $res->search($host, 'A') ) { my @arec = grep($_->type eq 'A', $query->answer); return $arec[0]->address if @arec > 0; } }
ã³ã¼ããæ¸ãã¦åãã£ããã¨
- é¢æ°å®ç¾©ã¯sub é¢æ°å { å®ç¾© }ãã§ä»®å¼æ°æå®ã¯ãªã
- é¢æ°å¼æ°ã®ãã§ãã㯠my (å¤æ°, ...) = @_;
- å±æå¤æ°å®ç¾©ã¯my å¤æ°å [= åæå¤];
- å¤æ°ã®æ¥é è¾ã¯$ï¼ã¹ã«ã©ï¼é åã¢ã¯ã»ã¹æï¼, @ï¼é åï¼
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã³ãã®ã¢ã¯ã»ã¹ã¯->
- ååä»ãå¼æ°ï¼ã¯( å¼æ°å => å¼æ°å¤, ... )
- èå¥åãªã¹ãï¼ã¯qw( èå¥å ... )
- æååã¯''ã""
- æ£è¦è¡¨ç¾ãªãã©ã«ããã
- æ£è¦è¡¨ç¾ã«ãããã¿ã¼ã³ãããã³ã°ã¯=~
- æååçµåã¯.(åè§ããªãªã)
_startãã³ãã©ã§ï¼IRCã³ã³ãã¼ãã³ãããããèµ·ããå¿ è¦ãããã®ãï¼ä»ã®è¨èªã¨ã¯ä¸å³éãæããããï¼è¨èªä»æ§ã®è¦ç¹ã§ã¯ï¼å¤æ°ã®æ¥é è¾ãæ¶ããããªãã¦å°ãï¼é ååãã«$#ãªãã¦ã®ããããããï¼æ ¼å¤åã¿ãããªãã®ã ã¨æãã°ãããã ã¨æããã©ï¼ãã£ã±ãåãä»ãã«ããï¼ãã¨ï¼ã¡ããã¨ããæå³è«ããã¿åããªããã®ã§ãï¼ç´æçã«ãããªæãããªï¼ã¨åããç¹ã§ï¼ãã®è¨èªã¯èå³æ·±ãï¼æå·çã ãã©ï¼
ä»åå®è£ ããªãã£ãè¨èªãã¡
groovy
Javaã®ä¸ã§åãã¹ã¯ãªããè¨èªï¼Javaã®IRCã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã使ãããããªã®ã§ï¼ããã§ãå®è£ ã§ããã.
Tcl
Tcl/Tkã¨è¨ãååã§ããç¥ããªãï¼IRKã¨ããã©ã¤ãã©ãªããããã©ï¼Î±ãªãªã¼ã¹ã§ï¼æçµæ´æ°ã7å¹´åï¼å®è£ ã§ãããã©ããæªããï¼
OCaml
ã¹ã¯ãªããè¨èªã§ã¯ãªããã©ï¼ä½¿ã£ã¦ã¿ãæãã§ã¯ã¹ã¯ãªããè¨èªçãªé°å²æ°ãããï¼ã©ã¤ãã©ãªã¯ãªããã©ï¼ã¨ããæå°IRCã¯ã©ã¤ã¢ã³ãã®å®è£ ã§ï¼åå©ç¨å¯è½ãªã³ã³ãã¼ãã³ããå«ã¾ãã¦ããï¼ä¸åº¦ç®ãéããæãã§ã¯ï¼ã¡ãã£ã¨ã©ããã®å±¤ãèãã ãã©ï¼ããã使ãã°ãªãã¨ãæ¸ãããï¼
ãã¢ã¼ã¯å®¶ã«çãã¾ã§ããã¢ã¼
ãã¦ï¼åè¨èªè¦ã¦åã£ã¦ãããã©ï¼å£éè¦ããè¨èªä»æ§ã¨ããè¦ç¹ã§ã¯ï¼ä¼¼ã¦ããç¹ãããã®ãè¦ã¦ï¼ããã¯ã©ã®æ§ãªçµç·¯ã§å°å
¥ãããã®ãï¼ã¨è¨ããã¨ãæ¯è¼ããããªã£ãï¼å¤åãããªãã ãããã©ï¼
æ¬é¡ã§ããï¼IRCããããæ¸ãã¨è¨ãè¦ç¹ã§ã¯ï¼Python, Ruby, PHPã¯ã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«ãã³ã¼ãã®å¤è¦³ãä½ãå¤ãããªãããã«æããï¼ã¨è¨ããäºå®ããã ã£ãï¼Perlã¯å°ã
ä¾å¤çï¼ä»ï¼IRCããããæ¸ããªãã©ãããªã¹ã¹ã¡ããªï¼ã¨ä»äººã«åããããï¼ãã£ã¨ï¼ããã¿ãã¡ã¤ã³ã¨ãã¦ä½ç½®ã¥ãã¦ããLLã§æ¸ãã¨è¯ããï¼ãã¨çããã ããï¼ã¡ã¤ã³Perl以å¤ãªãï¼
Python, Ruby, PHPã®ã©ã¤ãã©ãªãããããåãã¹ã¿ã¤ã«ãæ¡ã£ãã®ã¯ï¼å¥ã«éºä¼åãããã£ãããã§ã¯ãªãï¼ãã®ç°å¢ä¸ã§ã¯ãã®æ¹ãè¦éããè¯ããããããªã£ãï¼ã¨ããå¹³è¡é²åã¿ãããªãã®ã ã¨æãï¼ã ããã©ããªãã ã£ã¦è¨ãã¨ï¼åã«å¹³è¡é²åã£ã¦æ¸ãããã£ãã ãã ãã©ï¼ä½ãã®æ©ä¼ã§ä»ã®è¨èªã«ç§»ã£ã¦ï¼IRCããããæ¸ããã¨æãç«ã£ãã¨ãï¼æã®è¨èªã¨åãã¹ã¿ã¤ã«ã§æ¸ãããªãï¼ãã£ã¨ï¼ããã»ã©è¯ããã¨ã¯ãªãã ãããï¼æ°ããã¹ã¿ã¤ã«ãæ¶ããå´åãå¿
è¦ãªãã®ã ããï¼
çµè«ã¨ãã¦ã¯ï¼åè¨èªã¨ãï¼IRCããããæ¸ãã¨ããç¹ã§æ ¼å·®ã¯æããããï¼ããããã¡ããã¨ããIRCããããæ¸ãã«å¤ããç°å¢ã ã¨æã£ãï¼
Ruby以å¤ã¯å
¨ãã®ç´ 人ãªãã ãã©ï¼æããã¦ãã®çµè«ã¯æ£ããã®ã ãããã»ã»ã»ãã¨ï¼ã¨ãã¨ãããªãæ¸ããæçºã§ï¼æ¸ãã¦ãããã¨ããã£ã¡ãããã£ã¡ãããªæ°ãããªãã§ããªãï¼