2 /********************************************************************************
3 * banana/nntp.inc.php : NNTP protocole handler
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/banana.inc.php';
11 require_once dirname(__FILE__
) . '/message.inc.php';
12 require_once dirname(__FILE__
) . '/nntpcore.inc.php';
13 require_once dirname(__FILE__
) . '/protocoleinterface.inc.php';
15 class BananaNNTP
extends BananaNNTPCore
implements BananaProtocoleInterface
17 private $groupname = null
;
18 private $description = null
;
19 private $ingroup = null
;
22 private $boxes = null
;
24 /** Build a protocole handler plugged on the given box
26 public function __construct($box = null
)
28 $url = parse_url(Banana
::$host);
29 if ($url['scheme'] == 'nntps' ||
$url['scheme'] == 'snntp') {
30 $url['host'] = 'ssl://' . $url['host'];
32 if (!isset($url['port'])) {
35 if (!isset($url['user'])) {
36 parent
::__construct($url['host'], $url['port']);
38 parent
::__construct($url['host'], $url['port'], 120, false
);
39 $this->authinfo($url['user'], $url['pass']);
41 $this->groupname
= $box;
44 /** Return the descript;ion of the current box
46 public function getDescription()
48 if ($this->description
) {
49 return $this->description
;
51 $descs = $this->xgtitle($this->groupname
);
52 if (isset($descs[$this->groupname
])) {
53 $this->description
= $descs[$this->groupname
];
55 return $this->description
;
58 /** Return the list of the boxes
59 * @param mode Kind of boxes to list
60 * @param since date of last check (for new boxes and new messages)
61 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
63 public function getBoxList($mode = Banana
::BOXES_ALL
, $since = 0, $withstats = false
)
65 if (!is_array($this->boxes
) ||
$this->mode
!= $mode) {
66 $descs = $this->xgtitle();
67 if ($mode == Banana
::BOXES_NEW
&& $since) {
68 $list = $this->newgroups($since);
70 $list = $this->listGroups();
71 if ($mode == Banana
::BOXES_SUB
) {
72 $sub = array_flip(Banana
::$profile['subscribe']);
73 $list = array_intersect_key($list, $sub);
76 $this->boxes
= array();
77 foreach ($list as $group=>&$infos) {
78 if (isset($descs[$group])) {
79 $desc = $descs[$group];
80 if (!is_utf8($desc)) {
81 $desc = utf8_encode($desc);
83 $this->boxes
[$group] = array('desc' => $desc);
85 $this->boxes
[$group] = array('desc' => null
);
91 foreach ($this->boxes
as $group=>&$desc) {
92 list($msgnum, $first, $last, $groupname) = $this->group($group);
93 $this->ingroup
= $group;
94 $new = count($this->newnews($group, $since));
95 $desc['msgnum'] = $msgnum;
96 $desc['unread'] = $new;
103 * @param id Id of the emssage (can be either an Message-id or a message index)
104 * @param msg_headers Headers to process
105 * @param is_msgid If is set, $id is en Message-Id
106 * @return A BananaMessage or null if the given id can't be retreived
108 public function getMessage($id, array $msg_headers = array(), $is_msgid = false
)
110 if (!$is_msgid && $this->groupname
!= $this->ingroup
) {
111 if (is_null(Banana
::$spool)) {
112 $this->group($this->groupname
);
113 $this->ingroup
= $this->groupname
;
115 $id = array_search($id, Banana
::$spool->ids
);
118 $data = $this->article($id);
119 if ($data !== false
) {
120 return new BananaMessage($data);
125 /** Return the indexes of the messages presents in the Box
126 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
128 public function getIndexes()
130 list($msgnum, $first, $last, $groupname) = $this->group($this->groupname
);
131 $this->ingroup
= $this->groupname
;
132 return array($msgnum, $first, $last);
135 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
136 * @return Array(id => array(headername => headervalue))
138 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
141 foreach ($msg_headers as $header) {
142 $headers = $this->xhdr($header, $firstid, $lastid);
143 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
144 $header = strtolower($header);
145 if ($header == 'date') {
146 $headers = array_map('strtotime', $headers);
148 foreach ($headers as $id=>&$value) {
149 if (!isset($messages[$id])) {
150 $messages[$id] = array();
152 $messages[$id][$header] =& $value;
158 /** Add protocole specific data in the spool
160 public function updateSpool(array &$messages)
165 /** Return the indexes of the new messages since the give date
166 * @return Array(MSGNUM of new messages)
168 public function getNewIndexes($since)
170 return $this->newnews($this->groupname
, $since);
173 /** Return true if can post
175 public function canSend()
177 return $this->isValid();
180 /** Return true if can cancel
182 public function canCancel()
184 return $this->isValid();
187 /** Return the list of requested header for a new post
189 public function requestedHeaders()
191 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
196 public function send(BananaMessage
&$message)
198 $sources = $message->get(true
);
199 return $this->post($sources);
202 /** Cancel the message
204 public function cancel(BananaMessage
&$message)
206 $headers = Array('From' => Banana
::$profile['From'],
207 'Newsgroups' => $this->groupname
,
208 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
209 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
210 $headers = array_merge($headers, Banana
::$custom_hdr);
211 $body = 'Message canceled with Banana';
212 $msg = BananaMessage
::newMessage($headers, $body);
213 return $this->send($msg);
216 /** Return the protocole name
218 public function name()
225 require_once dirname(__FILE__) . '/spool.inc.php';
226 $time = microtime(true);
227 $nntp = new BananaNNTP('xorg.promo.x2002');
228 if (!$nntp->isValid()) {
232 Banana::$protocole =& $nntp;
233 Banana::$spool =& BananaSpool::getSpool('xorg.promo.x2002');
234 $msg = $nntp->getMessage(3424);
236 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
237 <link rel="stylesheet" type="text/css" href="css/banana.css"/>
238 </head><body><table class="banana_msg"><tr><td>';
239 //echo $msg->getFormattedBody('plain');
240 echo $msg->getFormattedBody();
241 echo '</td></tr></table></body></html>', "\n";
242 $end = microtime(true);
243 echo ($end - $time) . "s\n";
246 // vim:set et sw=4 sts=4 ts=4: