Add subject in From link
[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);
156 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
157 $header = strtolower($header);
158 if ($header == 'date') {
159 $headers = array_map('strtotime', $headers);
160 }
161 foreach ($headers as $id=>&$value) {
162 if (!isset($messages[$id])) {
163 $messages[$id] = array();
164 }
165 $messages[$id][$header] =& $value;
166 }
167 }
168 return $messages;
169 }
170
171 /** Add protocole specific data in the spool
172 */
173 public function updateSpool(array &$messages)
174 {
175 return true;
176 }
177
178 /** Return the indexes of the new messages since the give date
179 * @return Array(MSGNUM of new messages)
180 */
181 public function getNewIndexes($since)
182 {
0e25d15d 183 return $this->newnews(Banana::$group, $since);
7027794f 184 }
185
186 /** Return true if can post
187 */
188 public function canSend()
189 {
190 return $this->isValid();
191 }
192
193 /** Return true if can cancel
194 */
195 public function canCancel()
196 {
197 return $this->isValid();
198 }
199
200 /** Return the list of requested header for a new post
201 */
202 public function requestedHeaders()
203 {
204 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
205 }
206
207 /** Send the message
208 */
209 public function send(BananaMessage &$message)
210 {
211 $sources = $message->get(true);
212 return $this->post($sources);
213 }
214
215 /** Cancel the message
216 */
217 public function cancel(BananaMessage &$message)
218 {
219 $headers = Array('From' => Banana::$profile['From'],
0e25d15d 220 'Newsgroups' => Banana::$group,
7027794f 221 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
222 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
e9360b11 223 $headers = array_merge($headers, Banana::$msgedit_headers);
7027794f 224 $body = 'Message canceled with Banana';
225 $msg = BananaMessage::newMessage($headers, $body);
226 return $this->send($msg);
227 }
228
229 /** Return the protocole name
230 */
231 public function name()
232 {
233 return 'NNTP';
234 }
7027794f 235
e9360b11 236 /** Return the filename for the spool
237 */
238 public function filename()
239 {
240 $url = parse_url(Banana::$nntp_host);
241 $file = '';
242 if (isset($url['host'])) {
243 $file .= $url['host'] . '_';
244 }
245 if (isset($url['port'])) {
246 $file .= $url['port'] . '_';
247 }
248 $file .= Banana::$group;
249 return $file;
250 }
7027794f 251}
7027794f 252
598a1c53 253// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
7027794f 254?>