ã¯ããã«
ãæ¬ç¨¿ã§ã¯Rubyã使ã£ã¦ã·ã³ãã«ãªPOP3ãµã¼ããä½æãã¾ãã
ãPOP3ã¯ããããããã¡ã¼ã«ã®åä¿¡ãã®ã¨ãã«ä½¿ããããããã³ã«ï¼éä¿¡è¦ç´ï¼ã§ããæ¬ç¨¿ã§ã¯ããã®POP3ã§ãµã¼ãã¹ãæä¾ãããµã¼ãã®ä½æãéãã¦ã以ä¸ã®ãã¨ãå¦ã³ã¾ãã
- ãããã¯ã¼ã¯ããã°ã©ãã³ã°ã®åºç¤
- POP3ã®ä»çµã¿
- Rubyã«ãããããã¯ã¼ã¯ããã°ã©ãã³ã°
- Rubyã«ããUNIXã·ã¹ãã ããã°ã©ãã³ã°
POPdã®æ¦è¦
ãæ¬ç¨¿ã§ä½æããPOP3ãµã¼ãï¼POPdï¼ã¯ãã¤ã³ãã©ããããªã©ã®ä¿¡é ¼ã§ãããããã¯ã¼ã¯ãåæã¨ãã¦ä½æãã¾ãããã®ãããã»ãã¥ãªãã£ã¸ã®é æ ®ã¯æä½éã«ã¨ã©ããã§ããã ãã·ã³ãã«ãªæ§é ãå¿æãã¾ãããã ããæä½éã®é æ ®ãã¨ãã¦ããã¹ã¯ã¼ããå¹³æã§æµããªãããã«ãããããPOP3ã®ä¸ã§ãç¹ã«APOPã¨ããèªè¨¼æ¹æ³ãç¨ãã¾ãã
ãã¾ããé度ãã¡ã¢ãªå®¹éã«é¢ãã¦ããã¾ãé æ ®ãã¾ãããããããæ°äººããæ°å人ãæ¥å¸¸çã«ä½¿ããç¨åº¦ã®é度ãåºãã°ãããã®ã¨ãã¾ãã
ãããã¦æå¾ã«ãã¡ã¼ã«ããã¯ã¹ï¼ã¡ã¼ã«ãããã¦ãããã¼ã¿ãã¼ã¹ï¼ã«ã¯ãMaildirã¨ããå½¢å¼ãæ¡ç¨ãã¾ããMaildirã¯å ã¯qmailã¨ããã¡ã¼ã«é éã·ã¹ãã ã§ä½¿ããã¦ããã¡ã¼ã«ããã¯ã¹å½¢å¼ã§ãã·ã³ãã«ã§ãããªããé«ãä¿¡é ¼æ§ãèªãã¾ãã
ãç§ããã®ããã°ã©ã ãæ¸ããã®ã¯ãæ¢åã®POPãµã¼ãã§APOPã¨Maildirã使ãè¨å®ãããã®ãé常ã«é¢åã ã£ããã¨ãåå ã§ããããã®ãããã®è¨å®ã決ãæã¡ã«ãããã¨ã§è¨å®ã¨å®è£ ãç°¡æ½ã«ãã¦ãå°å ¥ãç°¡åã«ãã¦ãããã¨ããã®ããã°ã©ã ã®æ大ã®ç¹è²ã§ãããã®ãããªã¹ã¯ãªããè¨èªãããæ軽ãã¨å®ç¨æ§ãæãã¦ããã ããã°ã¨æãã¾ãã
対象èªè
ãæ¬ç¨¿ã¯æ¬¡ã®ãããªèªè ã対象ã«ãã¦ãã¾ãã
- Rubyããã°ã©ãã³ã°ãä¸éãç解ãã¦ãã
- Linuxã®åºæ¬çãªã³ãã³ãã使ãã
ãRubyãã¾ã£ãã使ã£ããã¨ã®ãªãèªè ã¯å¯¾è±¡ã¨ãã¾ãããæä½ã§ããèªåã§ã¯ã©ã¹ãå®ç¾©ã§ããããã«ãªã£ã¦ããã¦ãã ããã
ãã¾ããLinuxã®ã³ãã³ãã©ã¤ã³ã使ã£ãçµé¨ããããcdãlsãcpãmvãªã©ã®åºæ¬ã³ãã³ããä¸éã使ãããã¨ãåæã¨ãã¾ãã
å¿ è¦ãªç°å¢
ãæ¬ç¨¿ã§ã¯ä»¥ä¸ã®ç°å¢ãåæã¨ãã¾ãã
- Ruby 1.8以ä¸
- Linuxãªã©ã®UNIXç³»ãªãã¬ã¼ãã£ã³ã°ã·ã¹ãã
ãRubyã¯å®å®ç1.8ç³»ã®ãã§ããã ãææ°ã®ãã¼ã¸ã§ã³ã使ã£ã¦ãã ãããå稿å·çæç¹ã§ã®ææ°ãã¼ã¸ã§ã³ã¯1.8.5ã§ããã¾ããçè ã¯ä»¥ä¸ã®ç°å¢ã§åä½ã確èªãã¾ããã
- Ruby 1.8.5ãå®å®çææ°ã¹ãããã·ã§ãããéçºçææ°ã¹ãããã·ã§ãã
- Linux 2.6.8 / x86-64
- Cygwin 1.5 / Windows XP Professional / x86
ã¤ã³ã¿ã¼ããããµã¼ãããã°ã©ãã³ã°ã®åºç¤
ããã®ç¯ã§ã¯ãããã¯ã¼ã¯ããã°ã©ãã³ã°ãç¹ã«ã¤ã³ã¿ã¼ããããµã¼ããä½æããã¨ãã«å¿ è¦ãªç¥èã解説ãã¾ããããåæ©çãªå 容ãªã®ã§ããããã¯ã¼ã¯ããã°ã©ãã³ã°ã®çµé¨ãããæ¹ã¯èªã¿é£ã°ãã¦ãã ããã
ã¤ã³ã¿ã¼ããã
ããããã¯ã¼ã¯éä¿¡ã¨ã¯ãï¼é常ã¯ï¼è¤æ°ã®ã³ã³ãã¥ã¼ã¿ã§åãã¦ãããè¤æ°ã®ããã»ã¹ãéä¿¡ãããã¨ã§ãã1ã¤ã®ã³ã³ãã¥ã¼ã¿ã§åãã¦ããããã»ã¹å士ã§ããããã¯ã¼ã¯éä¿¡ã¯ã§ãã¾ãããå ¸åçãªç¶æ³ã¨ã¯è¨ãã¾ããã
ãã¡ãªã¿ã«ããããã¯ã¼ã¯ã®è©±ãããã¨ãã¯ãã³ã³ãã¥ã¼ã¿ãã®ãã¨ããã¹ãï¼hostï¼ãããã¼ãï¼nodeï¼ã¨è¨ãã¾ããæ¬ç¨¿ã§ã¯ããã¹ããã§çµ±ä¸ãã¾ãã
ããã¦ãå®ã¯ãããã¯ã¼ã¯ã«ããããã種é¡ãããã®ã§ãããç¾å¨ã¯ãããã¯ã¼ã¯ã¨è¨ãã°æ®éããã¤ã³ã¿ã¼ãããããæãã¾ããæ¬ç¨¿ã§è©±ãPOP3ãã¤ã³ã¿ã¼ãããä¸ã§ä½¿ããããããã³ã«ã§ãã
ãã§ã¯ã¤ã³ã¿ã¼ãããã¨ã¯ä½ã§ãããããã¤ã³ã¿ã¼ãããã¨ã¯ããã¹ãå士ãã¤ã³ã¿ã¼ããããããã³ã«ï¼IPï¼Internet Protocolï¼ã¨ãããããã³ã«ï¼éä¿¡è¦ç´ï¼ã使ã£ã¦éä¿¡ãããããã¯ã¼ã¯ã®ãã¨ã§ãã
ãIPã®æ大ã®ç¹å¾´ã¯ããã±ããï¼packetï¼ãåºæ¬ã¨ãããããã³ã«ã§ãããã¨ã§ãããã±ããã¨ããã®ã¯ããã¼ã¿ã®åã端ã®ãã¨ã§ããæè¿ã¯æºå¸¯ã¡ã¼ã«ã«ã¤ãã¦è©±ãããéãããã±ä»£ãâ¦â¦ãã¨è¨ã£ãããã¾ããããã®ããã±ãããã±ããã®ãã±ã§ããã¤ã³ã¿ã¼ãããã«åå ãã¦ãããã¹ãã¯ããã®ãã±ãããæããã£ã¦éä¿¡ãã¾ãã
ããã±ãããæãããã¨è¨ã£ã¦ãããã æããã ãã§ã¯ãã©ã®ãã¹ãããã©ã®ãã¹ãã¸åãã¦æãããã±ãããªã®ãåããã¾ãããããã§IPã®ãã±ããã«ã¯ãæãããã¹ãã¨å®å ã®ãã¹ããããããã®IPã¢ãã¬ã¹ï¼IP addressï¼ãæ¸ãè¾¼ã¾ãã¦ãã¾ããIPã¢ãã¬ã¹ã¯4ãã¤ãã®ãã¤ãåã§ãé常ã¯ãã¤ããã¨ã«ããªãªãã§åºåã£ã¦ãä¾ãã°ã192.168.1.1ãã®ããã«è¡¨è¨ãã¾ããã¡ãªã¿ã«ããã®ã4ãã¤ããã¨ãããµã¤ãºã足ããªãã¨ãããã¨ã§éçºãããã®ãIPv6ï¼Internet Protocol version 6ï¼ã§ããããã«å¯¾ãã¦ãç¾å¨æãåºãæ®åãã¦ããIPã¯ãã¼ã¸ã§ã³4ãããªãã¡IPv4ï¼Internet Protocol version 4ï¼ã§ãã
ããã¦ãIPã¢ãã¬ã¹ãåããã°ãã¹ããç¹å®ã§ãã¾ãããIPã¢ãã¬ã¹ã¯ãã ã®æ°å¤ã§ãããã人éã«ã¨ã£ã¦ã¯ä¸ä¾¿ãªãã¨ãã®ããããã¾ãããããã§ãIPã¢ãã¬ã¹ï¼æ°å¤ï¼ã¨ã¯å¥ã«ãã¹ãã«ååãä»ãããã¨ãã§ãã¾ãããã®ååããã¡ã¤ã³åï¼domain nameï¼ã§ããçããããªãã¿ã®ãwww.example.comããªã©ããã¡ã¤ã³åã§ããã¤ã³ã¿ã¼ãããã«åå ãã¦ãããã¹ãã¯ãDNSï¼Domain Name Systemï¼ã¨ããã·ã¹ãã ã使ã£ã¦ãã¡ã¤ã³åãIPã¢ãã¬ã¹ã«å¤æã§ãã¾ãã
TCPã¨ã½ã±ãã
ããã¾èª¬æããããã«ãIPã§ããã¨ãã§ããã®ã¯ãã±ããã ãã§ããã¨ãããå°ã£ããã¨ã«ãIPã§ã¯ãã®ãã±ããã®å±ãé åºã決ã¾ã£ã¦ãã¾ãããããã©ãããããã±ãããã¡ããã¨å±ããã©ãããåããã¾ãããä½ãäºæ ãèµ·ããã¨æãããã±ããããããªãæ¶å¤±ããå ´åãããã®ã§ãããã®ãããªãããã¯ã¼ã¯ã§ã¯å®å¿ãã¦éä¿¡ãã§ãã¾ãããä¾ãã°ãã¦ã§ããããã¦ã³ãã¼ããããã¨ããHTMLãç»åã®ãã¼ã¿ãã¨ããã©ããæãã¦ããããé åºããã©ãã©ã«ãªã£ã¦ããããããé常ã«å°ãã§ãããã
ãããã§ä½¿ãããã®ãTCPï¼Transmission Control Protocolï¼ã§ããTCPã¯IPã®ãã±ããã使ã£ã¦ããã«ä»æããåãããã¨ã§ããã¡ã¤ã«ã®èªã¿æ¸ãã®ããã«ãé åºã®æ確ãªå ¥åºåãè¡ããã¨ãã§ãã¾ããããããéä¿¡ã¨ã©ã¼ãæ¤åºããä»çµã¿ãç¨æãã¦ããã¼ã¿ã確å®ã«å±ããã¨ãä¿è¨¼ãã¦ããã¾ãã
ãããã«ãTCPã§ã¯1ã¤ã®ãã¹ãã§ãè¤æ°ã®ããã»ã¹ãåæã«éä¿¡ã§ããããããã¼ãçªå·ï¼port numberï¼ãå°å ¥ãã¾ããä¾ãã°ãwww.example.comã¨ãããã¹ã1ã¤ã§ãã1çªãã¼ãã2çªãã¼ãã3çªãã¼ãâ¦â¦ã§åæå¹³è¡çã«éä¿¡ã§ããããã§ããéã«è¨ãã¨ãTCPã使ã£ã¦éä¿¡ããã¨ãã¯ãéä¿¡ãããç¸æã®IPã¢ãã¬ã¹ã¨ãã¼ãçªå·ãã両æ¹æå®ããå¿ è¦ãããã¨ãããã¨ã§ãã
ãTCPéä¿¡ã®ããã®çµè·¯ã¯ãUNIXä¸ã§ã¯ã½ã±ããï¼socketï¼ã¨ãããã®ã§è¡¨ç¾ããã¾ããã½ã±ããã¯éãããã¡ã¤ã«ã®ãããªãã®ã§ããã¡ã¤ã«ã¨åããreadã·ã¹ãã ã³ã¼ã«ãwriteã·ã¹ãã ã³ã¼ã«ã§èªã¿æ¸ããã§ãã¾ããRubyã¬ãã«ã§ã¯ãã½ã±ããã¯TCPSocket
ãªãã¸ã§ã¯ããTCPServer
ãªãã¸ã§ã¯ãã§è¡¨ç¾ããã¦ãããFile
ãªãã¸ã§ã¯ãã¨åãããã«read
ã¡ã½ãããwrite
ã¡ã½ããã使ã£ã¦å
¥åºåãããã¨ãã§ãã¾ãã
ãµã¼ãã¨ã¯ã©ã¤ã¢ã³ã
ããã£ããTCPã®éä¿¡ãå§ã¾ã£ãããTCPã使ã£ã¦éä¿¡ããããã»ã¹ã¯å¯¾çã«æ±ããã¾ããã¤ã¾ããã©ã¡ãã®ããã»ã¹ãããåãããã«èªãã ãæ¸ãããã§ãã¾ããããããTCPã«ããæ¥ç¶ãå®äºããã¾ã§ã¯ç«å ´ã«éããããã¾ãã
ãTCPã§ã¯ãæ¥ç¶ãå¾ ã¤å´ã¨ãæ¥ç¶ãã«ããå´ãã¯ã£ããåºå¥ããã¾ããåè ã®ãã¨ãTCPãµã¼ãï¼TCP serverï¼ãå¾è ã®ãã¨ãTCPã¯ã©ã¤ã¢ã³ãï¼TCP clientï¼ã¨è¨ãã¾ãã
ãTCPã®ãµã¼ãã»ã¯ã©ã¤ã¢ã³ãã¨åãããã«ãTCPãå©ç¨ããããä¸ä½ã®ãµã¼ãã¹ã«ããã¦ãããµã¼ãã¨ã¯ã©ã¤ã¢ã³ããåºå¥ããããã¨ãå¤ã ããã¾ããä¾ãã°ãPOP3ãµã¼ãã¹ã§ããµã¼ãã¨ã¯ã©ã¤ã¢ã³ãã®åºå¥ãããã¾ããã¡ã¼ã«ãæä¾ããå´ãããããä½ãPOP3ãµã¼ãã§ãããã¡ã¼ã«ãåä¿¡ããå´ãPOP3ã¯ã©ã¤ã¢ã³ãã§ãã
ãä¸è¬ã«ã¯ãä¸ä½ã®ãµã¼ãã¹ï¼ä¾ãã°POP3ï¼ã®ãµã¼ãã§ããããã¨è¨ã£ã¦TCPã¬ãã«ã§ããµã¼ãã§ããã¨ã¯éãã¾ãããããããå°ãªãã¨ãPOP3ã«éã£ã¦è¨ãã°ãPOP3ã®ãµã¼ãã¯åæã«TCPãµã¼ãã§ãããã¾ããã¤ã¾ãPOP3ãµã¼ãã¯TCPæ¥ç¶ãå¾ ã¡åããå´ã«ãªããªããã°ããã¾ããã
ãã¼ã¢ã³
ã次ã«ãã¼ã¢ã³ï¼daemonï¼ã«ã¤ãã¦èª¬æãã¾ãã
ããµã¼ãã«ã¯ãã¤ã¯ã©ã¤ã¢ã³ãããæ¥ç¶è¦æ±ãæ¥ããåããã¾ããããã24æé365æ¥èµ·åãã¦æ¥ç¶ãå¾ ã¡ç¶ããå¿ è¦ãããã¾ããããèªä½ã¯ç°¡åã§ããaccept()ã¨ããã·ã¹ãã ã³ã¼ã«ã使ãã°TCPæ¥ç¶ãå®äºããã¾ã§å¾ ã¤ãã¨ãã§ããã®ã§ããã¡ãã¯åã«accept()ãå¼ãã§ããã°ããããã§ãã
ããã ããµã¼ãã§åé¡ã«ãªãã®ã端æ«ã¨ã®ã¤ãªããã§ãããµã¼ãã誰ãã«èµ·åãã¦ããããªããã°ããã¾ããããããããã®å ´åããµã¼ããèµ·åããã誰ããããã°ã¢ã¦ãããã¨ããã®ç«¯æ«ããèµ·åãããããã»ã¹ããã¹ã¦åæ¢ãã¦ãã¾ãã¾ãããã®ä»çµã¿ã¯ã誰ã«ã使ããã¦ããªãããã»ã¹ããããã¾ãªãã¨ããç¹ããè¦ãã°ä¾¿å©ã§ããããµã¼ãã«ã¨ã£ã¦ã¯å¤§åé¡ã§ãããµã¼ããèµ·åãã人ãæ°¸ä¹ ã«ãã°ã¢ã¦ãã§ããªããªã£ã¦ãã¾ãããã§ãã
ãããã§ãã¼ã¢ã³ã使ããã¾ãããã¼ã¢ã³ã¨ã¯ã端æ«ããåãé¢ãããããã»ã¹ã®ãã¨ã§ãããã¼ã¢ã³ã«ãªã£ãããã»ã¹ã¯ããã®ããã»ã¹ãèµ·åãã人ããã°ã¢ã¦ããã¦ãåæ¢ããã¾ããã
ããã®ãããªäºæ ãããã¤ã³ã¿ã¼ããããµã¼ããä½ãã¨ãã¯ãã¼ã¢ã³ã«ãªãæ©è½ãã»ã¼å¿ é ã«ãªãã¾ãããã¡ãããRubyããã°ã©ã ã§ããã¼ã¢ã³ã«ãªããã¨ãã§ãã¾ããããããããä½æããPOP3ãµã¼ãã«ãèªåã§ãã¼ã¢ã³ã«ãªãæ©è½ãå®è£ ãã¾ãã
inetd
ããã¼ã¢ã³ã«é¢ãã¦ã¯åé ã®éãã§ãããèªåã§ãã¼ã¢ã³ã«ãªãã®ã¯ãã¯ãå¤å°é¢åã§ãã
ãããã«ããµã¼ãã¨ãã£ã¦ãå¿ããããã°ã©ã ã°ããã§ã¯ããã¾ããããã£ã¨æ¥ç¶ãå¾ ã£ã¦ããã ãã®ãããªãæãªãµã¼ããããããããã¾ãããã®ãããªããã æ¥ç¶ãå¾ ã¤ã ãã®ããã»ã¹ããã£ã¨åããã¦ããã®ã¯ã¡ã¢ãªã®ç¡é§é£ãã§ã¯ãªãã§ããããã
ãããã§æ¬¡ã®ãã¼ã¯ã¼ããinetdãç»å ´ãã¾ããinetdã¨ã¯ãè¤æ°ã®ãµã¼ãããã°ã©ã ã®èº«ä»£ããã¨ãªã£ã¦ãTCPæ¥ç¶ãæ¥ãã®ãã¾ã¨ãã¦å¾ ã£ã¦ãã¦ããããµã¼ãã§ããinetdã«TCPæ¥ç¶è¦æ±ãæ¥ãã¨ããã®æç¹ã§åãã¦å®éã®ãµã¼ãããã°ã©ã ãèµ·åããTCPæ¥ç¶ãå¼ã渡ãã¦ããã¾ãã
ãinetdããèµ·åãããããã°ã©ã ã¯æ¨æºå ¥åºåãã½ã±ããã«ã¤ãªãã£ã¦ãã¦ãæ¨æºå ¥åºåãç¸æã«ãã¦ããã¨ãããã ãã§ãããã¯ã¼ã¯éä¿¡ãè¡ãã¾ããRubyã§è¨ããªããSTDINããå ¥åãã¦ãSTDOUTã«åºåããã°ããã®ã§ãã
ãã¾ããinetdããèµ·åãããããã°ã©ã ã¯èªåçã«ãã¼ã¢ã³ã«ãªãã®ã§ãèªåã§ãã¼ã¢ã³ã«ãªãå¿ è¦ãããã¾ããããã®ç¹ãinetdã使ã大ããªã¡ãªããã§ãã
ããã®ããã«ãinetdã使ããã¨ä¾¿å©ãªç¹ãããã®ã§ãããããä½æããPOP3ãµã¼ãï¼POPdï¼ã§ãinetdã«å¯¾å¿ãããã¨ã«ãã¾ãã