1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinShare library 4 5 @copyright: 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> 6 @copyright: 2003-2006 Edgewall Software 7 @copyright: 2006 MoinMoin:AlexanderSchremmer 8 @license: GNU GPL (v2 or later), see COPYING.txt for details. 9 """ 10 11 from ContentTypeSupport import getContentPreferences 12 from DateSupport import getCurrentTime, getDateTimeFromRFC2822 13 from MoinSupport import * 14 from MoinMoin.support.htmlmarkup import HTMLParseError, HTMLSanitizer, Markup 15 from MoinMoin import wikiutil 16 from email.parser import Parser 17 from codecs import getwriter 18 19 try: 20 from cStringIO import StringIO 21 except ImportError: 22 from StringIO import StringIO 23 24 _getFragments = getFragments 25 26 __version__ = "0.1" 27 28 # More Moin 1.9 compatibility functions. 29 30 def has_member(request, groupname, username): 31 if hasattr(request.dicts, "has_member"): 32 return request.dicts.has_member(groupname, username) 33 else: 34 return username in request.dicts.get(groupname, []) 35 36 # Fragments employ a "moinshare" attribute. 37 38 fragment_attribute = "moinshare" 39 40 def getFragments(s): 41 42 "Return all fragments in 's' having the MoinShare fragment attribute." 43 44 fragments = [] 45 for format, attributes, body in _getFragments(s): 46 if attributes.has_key(fragment_attribute): 47 fragments.append((format, attributes, body)) 48 return fragments 49 50 def getPreferredOutputTypes(request, mimetypes): 51 52 """ 53 Using the 'request', perform content negotiation, obtaining mimetypes common 54 to the fragment (given by 'mimetypes') and the client (found in the Accept 55 header). 56 """ 57 58 accept = getHeader(request, "Accept", "HTTP") 59 if accept: 60 prefs = getContentPreferences(accept) 61 return prefs.get_preferred_types(mimetypes) 62 else: 63 return mimetypes 64 65 def getUpdatedTime(metadata): 66 67 """ 68 Return the last updated time based on the given 'metadata', using the 69 current time if no explicit last modified time is specified. 70 """ 71 72 # NOTE: We could attempt to get the last edit time of a fragment. 73 74 latest_timestamp = metadata.get("last-modified") 75 if latest_timestamp: 76 return latest_timestamp 77 else: 78 return getCurrentTime() 79 80 def getUpdateSources(request, sources_page): 81 82 """ 83 Using the 'request', return the update sources defined on the given 84 'sources_page'. 85 """ 86 87 # Remote sources are accessed via dictionary page definitions. 88 89 return getWikiDict(sources_page, request) 90 91 # Entry/update classes. 92 93 class Update: 94 95 "A feed update entry." 96 97 def __init__(self): 98 self.title = None 99 self.link = None 100 self.content = None 101 self.content_type = None 102 self.updated = None 103 104 # Page-related attributes. 105 106 self.fragment = None 107 self.preferred = None 108 109 # Message-related attributes. 110 111 self.message_number = None 112 self.parts = None 113 114 # Message- and page-related attributes. 115 116 self.page = None 117 118 # Identification. 119 120 self.path = [] 121 122 def unique_id(self): 123 return "moinshare-tab-%s-%s" % (self.message_number, "-".join(map(str, self.path))) 124 125 def __cmp__(self, other): 126 if self.updated is None and other.updated is not None: 127 return 1 128 elif self.updated is not None and other.updated is None: 129 return -1 130 else: 131 return cmp(self.updated, other.updated) 132 133 def copy(self, part_number=None): 134 update = Update() 135 update.title = self.title 136 update.link = self.link 137 update.updated = self.updated 138 update.fragment = self.fragment 139 update.preferred = self.preferred 140 update.message_number = self.message_number 141 update.page = self.page 142 update.path = self.path[:] 143 if part_number is not None: 144 update.path.append(part_number) 145 return update 146 147 # Update retrieval from pages. 148 149 def getUpdatesFromPage(page, request): 150 151 """ 152 Get updates from the given 'page' using the 'request'. A list of update 153 objects is returned. 154 """ 155 156 updates = [] 157 158 # NOTE: Use the updated datetime from the page for updates. 159 # NOTE: The published and updated details would need to be deduced from 160 # NOTE: the page history instead of being taken from the page as a whole. 161 162 metadata = getMetadata(page) 163 updated = getUpdatedTime(metadata) 164 165 # Get the fragment regions for the page. 166 167 for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): 168 169 update = Update() 170 171 # Produce a fragment identifier. 172 # NOTE: Choose a more robust identifier where none is explicitly given. 173 174 update.fragment = attributes.get("fragment", str(n)) 175 update.title = attributes.get("summary", "Update #%d" % n) 176 177 # Get the preferred content types available for the fragment. 178 179 update.preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) 180 181 # Try and obtain some suitable content for the entry. 182 # NOTE: Could potentially get a summary for the fragment. 183 184 update.content = None 185 186 if "text/html" in update.preferred: 187 parser_cls = getParserClass(request, format) 188 189 if format == "html": 190 update.content = body 191 elif hasattr(parser_cls, "formatForOutputType"): 192 update.content = formatTextForOutputType(body, request, parser_cls, "text/html") 193 else: 194 fmt = request.html_formatter 195 fmt.setPage(page) 196 update.content = formatText(body, request, fmt, parser_cls) 197 198 update.content_type = "text/html" 199 200 update.page = page 201 202 # NOTE: The anchor would be supported in the page, but this requires 203 # NOTE: formatter modifications for the regions providing updates. 204 205 update.link = page.url(request, anchor=update.fragment) 206 update.updated = updated 207 208 updates.append(update) 209 210 return updates 211 212 # Update retrieval from message stores. 213 214 def getUpdatesFromStore(page, request): 215 216 """ 217 Get updates from the message store associated with the given 'page' using 218 the 'request'. A list of update objects is returned. 219 """ 220 221 updates = [] 222 223 metadata = getMetadata(page) 224 updated = getUpdatedTime(metadata) 225 226 store = ItemStore(page, "messages", "message-locks") 227 228 for n, message_text in enumerate(iter(store)): 229 230 update = Update() 231 message = Parser().parse(StringIO(message_text)) 232 233 # Produce a fragment identifier. 234 235 update.fragment = update.updated = getDateTimeFromRFC2822(message.get("date")) 236 update.title = message.get("subject", "Update #%d" % n) 237 238 update.page = page 239 update.message_number = n 240 241 update.content, update.content_type, update.parts = getUpdateContentFromPart(message) 242 243 updates.append(update) 244 245 return updates 246 247 def getUpdateContentFromPart(part): 248 249 """ 250 Return decoded content, the content type and any subparts in a tuple for a 251 given 'part'. 252 """ 253 254 # Determine whether the part has several representations. 255 256 # For a single part, use it as the update content. 257 258 if not part.is_multipart(): 259 content, content_type = getPartContent(part) 260 return content, content_type, None 261 262 # For a collection of related parts, use the first as the update content 263 # and assume that the formatter will reference the other parts. 264 265 elif part.get_content_subtype() == "related": 266 main_part = part.get_payload()[0] 267 content, content_type = getPartContent(main_part) 268 return content, content_type, [main_part] 269 270 # Otherwise, just obtain the parts for separate display. 271 272 else: 273 return None, part.get_content_type(), part.get_payload() 274 275 def getPartContent(part): 276 277 "Decode the 'part', returning the decoded payload and the content type." 278 279 charset = part.get_content_charset() 280 payload = part.get_payload(decode=True) 281 return (charset and unicode(payload, charset) or payload), part.get_content_type() 282 283 def getUpdateFromPart(parent, part, part_number): 284 285 "Using the 'parent' update, return an update object for the given 'part'." 286 287 update = parent.copy(part_number) 288 update.content, update.content_type, update.parts = getUpdateContentFromPart(part) 289 return update 290 291 # Source management. 292 293 def getUpdateSources(pagename, request): 294 295 "Return the update sources from the given 'pagename' using the 'request'." 296 297 sources = {} 298 299 source_definitions = getWikiDict(pagename, request) 300 301 if source_definitions: 302 for name, value in source_definitions.items(): 303 sources[name] = getSourceParameters(value) 304 305 return sources 306 307 def getSourceParameters(source_definition): 308 309 "Return the parameters from the given 'source_definition' string." 310 311 parameters = {} 312 unqualified = ("type", "location") 313 314 for arg in source_definition.split(): 315 try: 316 argname, argvalue = arg.split("=", 1) 317 318 # Detect unlikely parameter names. 319 320 if not argname.isalpha(): 321 raise ValueError 322 323 parameters[argname] = argvalue 324 325 # Unqualified parameters are assumed to be one of a recognised set. 326 327 except ValueError: 328 for argname in unqualified: 329 if not parameters.has_key(argname): 330 parameters[argname] = arg 331 break 332 333 return parameters 334 335 # HTML parsing support. 336 337 class IncomingHTMLSanitizer(HTMLSanitizer): 338 339 "An HTML parser that rewrites references to attachments." 340 341 def __init__(self, out, request, page, message_number): 342 HTMLSanitizer.__init__(self, out) 343 self.request = request 344 self.message_number = message_number 345 self.page = page 346 347 def rewrite_reference(self, ref): 348 if ref.startswith("cid:"): 349 part = ref[len("cid:"):] 350 action_link = self.page.url(self.request, { 351 "action" : "ReadMessage", "doit" : "1", 352 "message" : self.message_number, "part" : part 353 }) 354 return action_link 355 else: 356 return ref 357 358 def handle_starttag(self, tag, attrs): 359 new_attrs = [] 360 for attrname, attrvalue in attrs: 361 if attrname in self.uri_attrs: 362 new_attrs.append((attrname, self.rewrite_reference(attrvalue))) 363 else: 364 new_attrs.append((attrname, attrvalue)) 365 HTMLSanitizer.handle_starttag(self, tag, new_attrs) 366 367 class IncomingMarkup(Markup): 368 369 "A special markup processor for incoming HTML." 370 371 def sanitize(self, request, page, message_number): 372 out = getwriter("utf-8")(StringIO()) 373 sanitizer = IncomingHTMLSanitizer(out, request, page, message_number) 374 sanitizer.feed(self.stripentities(keepxmlentities=True)) 375 return IncomingMarkup(unicode(out.getvalue(), "utf-8")) 376 377 class IncomingHTMLParser: 378 379 "Filters and rewrites incoming HTML content." 380 381 def __init__(self, raw, request, **kw): 382 self.raw = raw 383 self.request = request 384 self.message_number = None 385 self.page = None 386 387 def format(self, formatter, **kw): 388 389 "Send the text." 390 391 try: 392 self.request.write(formatter.rawHTML(IncomingMarkup(self.raw).sanitize(self.request, self.page, self.message_number))) 393 except HTMLParseError, e: 394 self.request.write(formatter.sysmsg(1) + 395 formatter.text(u'HTML parsing error: %s in "%s"' % (e.msg, 396 self.raw.splitlines()[e.lineno - 1].strip())) + 397 formatter.sysmsg(0)) 398 399 class MakeIncomingHTMLParser: 400 401 "A class that makes parsers configured for messages." 402 403 def __init__(self, page, message_number): 404 405 "Initialise with state that is used to configure instantiated parsers." 406 407 self.message_number = message_number 408 self.page = page 409 410 def __call__(self, *args, **kw): 411 parser = IncomingHTMLParser(*args, **kw) 412 parser.message_number = self.message_number 413 parser.page = self.page 414 return parser 415 416 def get_make_parser(page, message_number): 417 418 """ 419 Return a callable that will return a parser configured for the message from 420 the given 'page' with the given 'message_number'. 421 """ 422 423 return MakeIncomingHTMLParser(page, message_number) 424 425 # vim: tabstop=4 expandtab shiftwidth=4