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