Fixes vim mode line.
[banana.git] / banana / nntpcore.inc.php
index 071ae5f..3632b31 100644 (file)
@@ -29,6 +29,7 @@ class BananaNNTPCore
 
     /** debug mode */
     private $debug = false;
+    private $bt    = array();
 
     /** constructor
      * @param $host STRING NNTP host
@@ -49,7 +50,7 @@ class BananaNNTPCore
         }
 
         $this->checkState();
-        $this->posting = ($this->lastresultcode == '200'); 
+        $this->posting = ($this->lastresultcode == '200');
         if ($reader && $this->posting) {
             $this->execLine('MODE READER');
             $this->posting = ($this->lastresultcode == '200');
@@ -78,39 +79,46 @@ class BananaNNTPCore
 
     public function lastError()
     {
-        return $this->lasterrortext;
+        if (!is_utf8($this->lasterrortext)) {
+            return utf8_encode($this->lasterrortext);
+        } else {
+            return $this->lasterrortext;
+        }
+    }
+
+    public function backtrace()
+    {
+        if ($this->debug) {
+            return $this->bt;
+        } else {
+            return null;
+        }
     }
 
 # Socket functions
 
     /** get a line from server
-     * @return STRING 
+     * @return STRING
      */
-    private function getLine()
+    private function &getLine()
     {
-        return rtrim(fgets($this->ns, 1200));
+        $data = rtrim(@fgets($this->ns, 1200), "\r\n");
+        return $data;
     }
 
     /** fetch data (and on delimitor)
      * @param STRING $delim string indicating and of transmission
      */
-    private function fetchResult($callback = null)
+    private function &fetchResult()
     {
         $array = Array();
-        while (($result = $this->getLine()) != '.') {
-            if (!is_null($callback)) {
-                list($key, $result) = call_user_func($callback, $result);
-                if (is_null($result)) {
-                    continue;
-                }
-                if (is_null($key)) {
-                    $array[] = $result;
-                } else {
-                    $array[$key] = $result;
-                }
-            } else {
-                $array[] = $result;
-            }
+        while (($result =& $this->getLine()) != '.') {
+            $array[] =& $result;
+        }
+        if ($this->debug && $this->bt) {
+            $trace =& $this->bt[count($this->bt) - 1];
+            $trace['response'] = count($array);
+            $trace['time']     = microtime(true) - $trace['start'];
         }
         return $array;
     }
@@ -121,14 +129,14 @@ class BananaNNTPCore
     private function putLine($line, $format = false)
     {
         if ($format) {
-            $line = str_replace(array("\r", "\n"), '', $line);  
+            $line = str_replace(array("\r", "\n"), '', $line);
             $line .= "\r\n";
         }
         if ($this->debug) {
             $db_line = preg_replace('/PASS .*/', 'PASS *******', $line);
-            echo $db_line;
+            $this->bt[] = array('action' => $db_line, 'start' => microtime(true));
         }
-        return fputs($this->ns, $line, strlen($line));
+        return @fputs($this->ns, $line, strlen($line));
     }
 
     /** put a message (multiline)
@@ -139,6 +147,7 @@ class BananaNNTPCore
             $message = join("\n", $_message);
         }
         if ($message) {
+            $message = preg_replace("/(^|\n)\./", '\1..', $message);
             $this->putLine("$message\r\n", false);
         }
         return $this->execLine('.');
@@ -162,11 +171,15 @@ class BananaNNTPCore
     private function checkState($strict = true)
     {
         $result = $this->getLine();
-        if ($this->debug) {
-            echo "$result\n";
-        }
         $this->lastresultcode = substr($result, 0, 3);
         $this->lastresulttext = substr($result, 4);
+        if ($this->debug && $this->bt) {
+            $trace =& $this->bt[count($this->bt) - 1];
+            $trace['time']     = microtime(true) - $trace['start'];
+            $trace['code']     = $this->lastresultcode;
+            $trace['message']  = $this->lastresulttext;
+            $trace['response'] = 0;
+        }
         $c = $this->lastresultcode{0};
         if ($c == '2' || (($c == '1' || $c == '3') && !$strict)) {
             return true;
@@ -185,7 +198,7 @@ class BananaNNTPCore
      * @param $pass INTEGER password
      * @return BOOLEAN true if authentication was successful
      */
-    protected function authinfo($user, $pass)
+    public function authinfo($user, $pass)
     {
         if ($this->execLine("AUTHINFO USER $user", false)) {
             return $this->execline("AUTHINFO PASS $pass");
@@ -195,15 +208,15 @@ class BananaNNTPCore
 
     /** retrieves an article
      * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
-     * server-dependent ID (see X-Ref on many servers) and retriving 
+     * server-dependent ID (see X-Ref on many servers) and retriving
      * an article by this way will change the current article pointer.
-     * If an error occur, false is returned. 
+     * If an error occur, false is returned.
      * @param $_msgid STRING MSGID or MSGNUM of article
      * @return ARRAY lines of the article
      * @see body
      * @see head
      */
-    protected function article($msgid = "")
+    public function article($msgid = "")
     {
         if (!$this->execLine("ARTICLE $msgid")) {
             return false;
@@ -214,9 +227,9 @@ class BananaNNTPCore
     /** post a message
      * if an error occur, false is returned
      * @param $_message STRING message to post
-     * @return STRING MSGID of article 
+     * @return STRING MSGID of article
      */
-    protected function post($message)
+    public function post($message)
     {
         if (!$this->execLine("POST ", false)) {
             return false;
@@ -238,7 +251,7 @@ class BananaNNTPCore
      * @see article
      * @see head
      */
-    protected function body($msgid = '')
+    public function body($msgid = '')
     {
         if ($this->execLine("BODY $msgid")) {
             return false;
@@ -253,7 +266,7 @@ class BananaNNTPCore
      * @see article
      * @see body
      */
-    protected function head($msgid = '')
+    public function head($msgid = '')
     {
         if (!$this->execLine("HEAD $msgid")) {
             return false;
@@ -262,10 +275,10 @@ class BananaNNTPCore
     }
 
     /** set current group
-     * @param $_group STRING 
+     * @param $_group STRING
      * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
      */
-    protected function group($group)
+    public function group($group)
     {
         if (!$this->execLine("GROUP $group")) {
             return false;
@@ -281,7 +294,7 @@ class BananaNNTPCore
      * @return STRING MSGID of article
      * @see next
      */
-    protected function last()
+    public function last()
     {
         if (!$this->execLine("LAST ")) {
             return false;
@@ -297,7 +310,7 @@ class BananaNNTPCore
      * @see last
      */
 
-    protected function next()
+    public function next()
     {
         if (!$this->execLine('NEXT ')) {
             return false;
@@ -314,7 +327,7 @@ class BananaNNTPCore
      * @see article
      * @see body
      */
-    protected function nntpstat($msgid)
+    public function nntpstat($msgid)
     {
         if (!$this->execLine("STAT $msgid")) {
             return false;
@@ -334,7 +347,7 @@ class BananaNNTPCore
         $groups = array();
         foreach ($list as $result) {
             list($group, $last, $first, $p) = explode(' ', $result, 4);
-            if (!is_null(Banana::$boxpattern) || preg_match('@' . Banana::$boxpattern . '@i', $group)) {
+            if (!Banana::$boxpattern || preg_match('@' . Banana::$boxpattern . '@i', $group)) {
                 $groups[$group] = array(intval($last), intval($first), $p);
             }
         }
@@ -345,7 +358,7 @@ class BananaNNTPCore
      * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
      * @see newgroups
      */
-    protected function listGroups()
+    public function listGroups()
     {
         if (!$this->execLine('LIST')) {
             return false;
@@ -361,30 +374,30 @@ class BananaNNTPCore
         return gmdate("ymd His", $since) . ' GMT';
     }
 
-    /** get information about recent newsgroups 
+    /** get information about recent newsgroups
      * same as list, but information are limited to newgroups created after $_since
      * @param $_since INTEGER unix timestamp
-     * @param $_distributions STRING distributions 
+     * @param $_distributions STRING distributions
      * @return ARRAY same format as liste
      * @see liste
      */
-    protected function newgroups($since, $distributions = '')
+    public function newgroups($since, $distributions = '')
     {
         if (!($since = $this->formatDate($since))) {
             return false;
         }
         if (!$this->execLine("NEWGROUPS $since $distributions")) {
             return false;
-        }    
+        }
         return $this->filterGroups();
     }
 
     /** gets a list of new articles
      * @param $_since INTEGER unix timestamp
-     * @parma $_groups STRING pattern of intersting groups 
+     * @parma $_groups STRING pattern of intersting groups
      * @return ARRAY MSGID of new articles
      */
-    protected function newnews($groups = '*', $since = 0, $distributions = '')
+    public function newnews($groups = '*', $since = 0, $distributions = '')
     {
         if (!($since = $this->formatDate($since))) {
             return false;
@@ -398,7 +411,7 @@ class BananaNNTPCore
     /** Tell the remote server that I am not a user client, but probably another news server
      * @return BOOLEAN true if sucessful
      */
-    protected function slave()
+    public function slave()
     {
         return $this->execLine("SLAVE ");
     }
@@ -406,9 +419,9 @@ class BananaNNTPCore
     /** implements IHAVE method
      * @param $_msgid STRING MSGID of article
      * @param $_message STRING article
-     * @return BOOLEAN 
+     * @return BOOLEAN
      */
-    protected function ihave($msgid, $message = false)
+    public function ihave($msgid, $message = false)
     {
         if (!$this->execLine("IHAVE $msgid ")) {
             return false;
@@ -418,7 +431,7 @@ class BananaNNTPCore
 
     /** closes connection to server
      */
-    protected function quit()
+    public function quit()
     {
         $this->execLine('QUIT');
         fclose($this->ns);
@@ -429,10 +442,10 @@ class BananaNNTPCore
 # NNTP Extensions [RFC 2980]
 
     /** Returns the date on the remote server
-     * @return INTEGER timestamp 
+     * @return INTEGER timestamp
      */
 
-    protected function date()
+    public function date()
     {
         if (!$this->execLine('DATE ', false)) {
             return false;
@@ -448,15 +461,15 @@ class BananaNNTPCore
      * @return ARRAY group name => description
      */
 
-    protected function xgtitle($pattern = '*')
+    public function xgtitle($pattern = '*')
     {
         if (!$this->execLine("XGTITLE $pattern ")) {
             return false;
         }
-        $array  = $this->fetchResult();
+        $array  =& $this->fetchResult();
         $groups = array();
-        foreach ($array as $result) {
-            list($group, $desc) = split("[ \t]", $result, 2);
+        foreach ($array as &$result) {
+            @list($group, $desc) = preg_split('/[ \t]/', $result, 2);
             $groups[$group] = $desc;
         }
         return $groups;
@@ -464,10 +477,10 @@ class BananaNNTPCore
 
     /** obtain the header field $hdr for all the messages specified
      * @param $_hdr STRING name of the header (eg: 'From')
-     * @param $_range STRING range of articles 
+     * @param $_range STRING range of articles
      * @return ARRAY MSGNUM => header value
      */
-    protected function xhdr($hdr, $first = null, $last = null)
+    public function xhdr($hdr, $first = null, $last = null)
     {
         if (is_null($first) && is_null($last)) {
             $range = "";
@@ -477,7 +490,7 @@ class BananaNNTPCore
         if (!$this->execLine("XHDR $hdr $range ")) {
             return false;
         }
-        $array   = $this->fetchResult();
+        $array   =& $this->fetchResult();
         $headers = array();
         foreach ($array as &$result) {
             @list($head, $value) = explode(' ', $result, 2);
@@ -488,16 +501,16 @@ class BananaNNTPCore
 
     /** obtain the header field $_hdr matching $_pat for all the messages specified
      * @param $_hdr STRING name of the header (eg: 'From')
-     * @param $_range STRING range of articles 
+     * @param $_range STRING range of articles
      * @param $_pat STRING pattern
      * @return ARRAY MSGNUM => header value
      */
-    protected function xpat($_hdr, $_range, $_pat)
+    public function xpat($_hdr, $_range, $_pat)
     {
         if (!$this->execLine("XPAT $hdr $range $pat")) {
             return false;
         }
-        $array   = $this->fetchResult();
+        $array   =& $this->fetchResult();
         $headers = array();
         foreach ($array as &$result) {
             list($head, $value) = explode(' ', $result, 2);
@@ -507,5 +520,5 @@ class BananaNNTPCore
     }
 }
 
-// vim:set et sw=4 sts=4 ts=4 
+// vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
 ?>