Year of the post
[banana.git] / banana / nntp.inc.php
CommitLineData
7027794f 1<?php
2/********************************************************************************
3* banana/nntp.inc.php : NNTP protocole handler
4* ------------------------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
10require_once dirname(__FILE__) . '/banana.inc.php';
11require_once dirname(__FILE__) . '/message.inc.php';
12require_once dirname(__FILE__) . '/nntpcore.inc.php';
13require_once dirname(__FILE__) . '/protocoleinterface.inc.php';
14
15class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
16{
7027794f 17 private $ingroup = null;
18
19 private $mode = null;
20 private $boxes = null;
21
22 /** Build a protocole handler plugged on the given box
23 */
0e25d15d 24 public function __construct()
7027794f 25 {
e9360b11 26 $url = parse_url(Banana::$nntp_host);
7027794f 27 if ($url['scheme'] == 'nntps' || $url['scheme'] == 'snntp') {
28 $url['host'] = 'ssl://' . $url['host'];
29 }
30 if (!isset($url['port'])) {
31 $url['port'] = 119;
32 }
33 if (!isset($url['user'])) {
34 parent::__construct($url['host'], $url['port']);
35 } else {
36 parent::__construct($url['host'], $url['port'], 120, false);
37 $this->authinfo($url['user'], $url['pass']);
38 }
7027794f 39 }
40
41 /** Return the descript;ion of the current box
42 */
43 public function getDescription()
44 {
0e25d15d 45 $descs = $this->xgtitle(Banana::$group);
46 if (isset($descs[Banana::$group])) {
cd58d954 47 return trim(utf8_encode($descs[Banana::$group]));
7027794f 48 }
cd58d954 49 return null;
7027794f 50 }
51
52 /** Return the list of the boxes
53 * @param mode Kind of boxes to list
54 * @param since date of last check (for new boxes and new messages)
55 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
56 */
57 public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false)
58 {
59 if (!is_array($this->boxes) || $this->mode != $mode) {
60 $descs = $this->xgtitle();
61 if ($mode == Banana::BOXES_NEW && $since) {
62 $list = $this->newgroups($since);
63 } else {
64 $list = $this->listGroups();
65 if ($mode == Banana::BOXES_SUB) {
66 $sub = array_flip(Banana::$profile['subscribe']);
67 $list = array_intersect_key($list, $sub);
68 }
69 }
70 $this->boxes = array();
71 foreach ($list as $group=>&$infos) {
72 if (isset($descs[$group])) {
73 $desc = $descs[$group];
74 if (!is_utf8($desc)) {
75 $desc = utf8_encode($desc);
76 }
77 $this->boxes[$group] = array('desc' => $desc);
78 } else {
79 $this->boxes[$group] = array('desc' => null);
80 }
81 }
82 ksort($this->boxes);
83 }
84 if ($withstats) {
85 foreach ($this->boxes as $group=>&$desc) {
86 list($msgnum, $first, $last, $groupname) = $this->group($group);
87 $this->ingroup = $group;
88 $new = count($this->newnews($group, $since));
89 $desc['msgnum'] = $msgnum;
90 $desc['unread'] = $new;
91 }
92 }
93 return $this->boxes;
94 }
95
96 /** Return a message
97 * @param id Id of the emssage (can be either an Message-id or a message index)
7027794f 98 * @return A BananaMessage or null if the given id can't be retreived
99 */
7a5823f9 100 public function &getMessage($id)
7027794f 101 {
f06f42dc 102 $message = null;
7a5823f9 103 if (is_numeric($id) && Banana::$group != $this->ingroup) {
7027794f 104 if (is_null(Banana::$spool)) {
0e25d15d 105 $this->group(Banana::$group);
106 $this->ingroup = Banana::$group;
7027794f 107 } else {
108 $id = array_search($id, Banana::$spool->ids);
109 }
110 }
111 $data = $this->article($id);
112 if ($data !== false) {
f06f42dc 113 $message = new BananaMessage($data);
7027794f 114 }
f06f42dc 115 return $message;
7027794f 116 }
117
7a5823f9 118 /** Return the sources of the message
119 */
120 public function getMessageSource($id)
121 {
122 if (is_numeric($id) && Banana::$group != $this->ingroup) {
123 if (is_null(Banana::$spool)) {
124 $this->group(Banana::$group);
125 $this->ingroup = Banana::$group;
126 } else {
127 $id = array_search($id, Banana::$spool->ids);
128 }
129 }
130 $data = $this->article($id);
131 if ($data !== false) {
132 return implode("\n", $data);
133 }
7d3f4749 134 $data = null;
135 return $data;
7a5823f9 136 }
137
7027794f 138 /** Return the indexes of the messages presents in the Box
139 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
140 */
141 public function getIndexes()
142 {
0e25d15d 143 list($msgnum, $first, $last, $groupname) = $this->group(Banana::$group);
144 $this->ingroup = Banana::$group;
7027794f 145 return array($msgnum, $first, $last);
146 }
147
148 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
149 * @return Array(id => array(headername => headervalue))
150 */
151 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
152 {
153 $messages = array();
154 foreach ($msg_headers as $header) {
155 $headers = $this->xhdr($header, $firstid, $lastid);
7027794f 156 $header = strtolower($header);
157 if ($header == 'date') {
158 $headers = array_map('strtotime', $headers);
345c3a85 159 } else {
160 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
7027794f 161 }
162 foreach ($headers as $id=>&$value) {
163 if (!isset($messages[$id])) {
164 $messages[$id] = array();
165 }
166 $messages[$id][$header] =& $value;
167 }
168 }
169 return $messages;
170 }
171
172 /** Add protocole specific data in the spool
173 */
174 public function updateSpool(array &$messages)
175 {
176 return true;
177 }
178
179 /** Return the indexes of the new messages since the give date
180 * @return Array(MSGNUM of new messages)
181 */
182 public function getNewIndexes($since)
183 {
0e25d15d 184 return $this->newnews(Banana::$group, $since);
7027794f 185 }
186
187 /** Return true if can post
188 */
189 public function canSend()
190 {
191 return $this->isValid();
192 }
193
194 /** Return true if can cancel
195 */
196 public function canCancel()
197 {
198 return $this->isValid();
199 }
200
201 /** Return the list of requested header for a new post
202 */
203 public function requestedHeaders()
204 {
205 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
206 }
207
208 /** Send the message
209 */
210 public function send(BananaMessage &$message)
211 {
212 $sources = $message->get(true);
213 return $this->post($sources);
214 }
215
216 /** Cancel the message
217 */
218 public function cancel(BananaMessage &$message)
219 {
220 $headers = Array('From' => Banana::$profile['From'],
0e25d15d 221 'Newsgroups' => Banana::$group,
7027794f 222 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
223 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
e9360b11 224 $headers = array_merge($headers, Banana::$msgedit_headers);
7027794f 225 $body = 'Message canceled with Banana';
226 $msg = BananaMessage::newMessage($headers, $body);
227 return $this->send($msg);
228 }
229
230 /** Return the protocole name
231 */
232 public function name()
233 {
234 return 'NNTP';
235 }
7027794f 236
e9360b11 237 /** Return the filename for the spool
238 */
239 public function filename()
240 {
241 $url = parse_url(Banana::$nntp_host);
242 $file = '';
243 if (isset($url['host'])) {
244 $file .= $url['host'] . '_';
245 }
246 if (isset($url['port'])) {
247 $file .= $url['port'] . '_';
248 }
249 $file .= Banana::$group;
250 return $file;
251 }
7027794f 252}
7027794f 253
598a1c53 254// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
7027794f 255?>