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 | { |
17 | private $groupname = null; |
18 | private $description = null; |
19 | private $ingroup = null; |
20 | |
21 | private $mode = null; |
22 | private $boxes = null; |
23 | |
24 | /** Build a protocole handler plugged on the given box |
25 | */ |
26 | public function __construct($box = null) |
27 | { |
28 | $url = parse_url(Banana::$host); |
29 | if ($url['scheme'] == 'nntps' || $url['scheme'] == 'snntp') { |
30 | $url['host'] = 'ssl://' . $url['host']; |
31 | } |
32 | if (!isset($url['port'])) { |
33 | $url['port'] = 119; |
34 | } |
35 | if (!isset($url['user'])) { |
36 | parent::__construct($url['host'], $url['port']); |
37 | } else { |
38 | parent::__construct($url['host'], $url['port'], 120, false); |
39 | $this->authinfo($url['user'], $url['pass']); |
40 | } |
41 | $this->groupname = $box; |
42 | } |
43 | |
44 | /** Return the descript;ion of the current box |
45 | */ |
46 | public function getDescription() |
47 | { |
48 | if ($this->description) { |
49 | return $this->description; |
50 | } |
51 | $descs = $this->xgtitle($this->groupname); |
52 | if (isset($descs[$this->groupname])) { |
53 | $this->description = $descs[$this->groupname]; |
54 | } |
55 | return $this->description; |
56 | } |
57 | |
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) |
62 | */ |
63 | public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false) |
64 | { |
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); |
69 | } else { |
70 | $list = $this->listGroups(); |
71 | if ($mode == Banana::BOXES_SUB) { |
72 | $sub = array_flip(Banana::$profile['subscribe']); |
73 | $list = array_intersect_key($list, $sub); |
74 | } |
75 | } |
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); |
82 | } |
83 | $this->boxes[$group] = array('desc' => $desc); |
84 | } else { |
85 | $this->boxes[$group] = array('desc' => null); |
86 | } |
87 | } |
88 | ksort($this->boxes); |
89 | } |
90 | if ($withstats) { |
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; |
97 | } |
98 | } |
99 | return $this->boxes; |
100 | } |
101 | |
102 | /** Return a message |
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 |
107 | */ |
108 | public function getMessage($id, array $msg_headers = array(), $is_msgid = false) |
109 | { |
110 | if (!$is_msgid && $this->groupname != $this->ingroup) { |
111 | if (is_null(Banana::$spool)) { |
112 | $this->group($this->groupname); |
113 | $this->ingroup = $this->groupname; |
114 | } else { |
115 | $id = array_search($id, Banana::$spool->ids); |
116 | } |
117 | } |
118 | $data = $this->article($id); |
119 | if ($data !== false) { |
120 | return new BananaMessage($data); |
121 | } |
122 | return null; |
123 | } |
124 | |
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) |
127 | */ |
128 | public function getIndexes() |
129 | { |
130 | list($msgnum, $first, $last, $groupname) = $this->group($this->groupname); |
131 | $this->ingroup = $this->groupname; |
132 | return array($msgnum, $first, $last); |
133 | } |
134 | |
135 | /** Return the message headers (in BananaMessage) for messages from firstid to lastid |
136 | * @return Array(id => array(headername => headervalue)) |
137 | */ |
138 | public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array()) |
139 | { |
140 | $messages = 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); |
147 | } |
148 | foreach ($headers as $id=>&$value) { |
149 | if (!isset($messages[$id])) { |
150 | $messages[$id] = array(); |
151 | } |
152 | $messages[$id][$header] =& $value; |
153 | } |
154 | } |
155 | return $messages; |
156 | } |
157 | |
158 | /** Add protocole specific data in the spool |
159 | */ |
160 | public function updateSpool(array &$messages) |
161 | { |
162 | return true; |
163 | } |
164 | |
165 | /** Return the indexes of the new messages since the give date |
166 | * @return Array(MSGNUM of new messages) |
167 | */ |
168 | public function getNewIndexes($since) |
169 | { |
170 | return $this->newnews($this->groupname, $since); |
171 | } |
172 | |
173 | /** Return true if can post |
174 | */ |
175 | public function canSend() |
176 | { |
177 | return $this->isValid(); |
178 | } |
179 | |
180 | /** Return true if can cancel |
181 | */ |
182 | public function canCancel() |
183 | { |
184 | return $this->isValid(); |
185 | } |
186 | |
187 | /** Return the list of requested header for a new post |
188 | */ |
189 | public function requestedHeaders() |
190 | { |
191 | return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization'); |
192 | } |
193 | |
194 | /** Send the message |
195 | */ |
196 | public function send(BananaMessage &$message) |
197 | { |
198 | $sources = $message->get(true); |
199 | return $this->post($sources); |
200 | } |
201 | |
202 | /** Cancel the message |
203 | */ |
204 | public function cancel(BananaMessage &$message) |
205 | { |
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); |
214 | } |
215 | |
216 | /** Return the protocole name |
217 | */ |
218 | public function name() |
219 | { |
220 | return 'NNTP'; |
221 | } |
222 | } |
223 | |
224 | /* |
225 | require_once dirname(__FILE__) . '/spool.inc.php'; |
226 | $time = microtime(true); |
227 | $nntp = new BananaNNTP('xorg.promo.x2002'); |
228 | if (!$nntp->isValid()) { |
229 | echo "Beuh !\n"; |
230 | exit; |
231 | } |
232 | Banana::$protocole =& $nntp; |
233 | Banana::$spool =& BananaSpool::getSpool('xorg.promo.x2002'); |
234 | $msg = $nntp->getMessage(3424); |
235 | echo '<html><head> |
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"; |
244 | */ |
245 | |
246 | // vim:set et sw=4 sts=4 ts=4: |
247 | ?> |