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 ItemSupport import ItemStore 15 from MoinMessage import GPG, is_encrypted, is_signed, MoinMessageError 16 from MoinMessageSupport import get_homedir 17 from MoinMoin.support.htmlmarkup import HTMLParseError, HTMLSanitizer, Markup 18 from MoinMoin import wikiutil 19 from email.parser import Parser 20 from codecs import getwriter 21 22 try: 23 from cStringIO import StringIO 24 except ImportError: 25 from StringIO import StringIO 26 27 _getFragments = getFragments 28 29 __version__ = "0.1" 30 31 # More Moin 1.9 compatibility functions. 32 33 def has_member(request, groupname, username): 34 if hasattr(request.dicts, "has_member"): 35 return request.dicts.has_member(groupname, username) 36 else: 37 return username in request.dicts.get(groupname, []) 38 39 # Fragments employ a "moinshare" attribute. 40 41 fragment_attribute = "moinshare" 42 43 def getFragments(s): 44 45 "Return all fragments in 's' having the MoinShare fragment attribute." 46 47 fragments = [] 48 for format, attributes, body in _getFragments(s): 49 if attributes.has_key(fragment_attribute): 50 fragments.append((format, attributes, body)) 51 return fragments 52 53 def getPreferredOutputTypes(request, mimetypes): 54 55 """ 56 Using the 'request', perform content negotiation, obtaining mimetypes common 57 to the fragment (given by 'mimetypes') and the client (found in the Accept 58 header). 59 """ 60 61 accept = getHeader(request, "Accept", "HTTP") 62 if accept: 63 prefs = getContentPreferences(accept) 64 return prefs.get_preferred_types(mimetypes) 65 else: 66 return mimetypes 67 68 def getUpdatedTime(metadata): 69 70 """ 71 Return the last updated time based on the given 'metadata', using the 72 current time if no explicit last modified time is specified. 73 """ 74 75 # NOTE: We could attempt to get the last edit time of a fragment. 76 77 latest_timestamp = metadata.get("last-modified") 78 if latest_timestamp: 79 return latest_timestamp 80 else: 81 return getCurrentTime() 82 83 # Entry/update classes. 84 85 class Update: 86 87 "A feed update entry." 88 89 def __init__(self): 90 self.title = None 91 self.link = None 92 self.content = None 93 self.content_type = None 94 self.updated = None 95 96 # Page-related attributes. 97 98 self.fragment = None 99 self.preferred = None 100 101 # Message-related attributes. 102 103 self.message_number = None 104 self.parts = None 105 106 # Message- and page-related attributes. 107 108 self.page = None 109 110 # Identification. 111 112 self.path = [] 113 114 def unique_id(self): 115 return "moinshare-tab-%s-%s" % (self.message_number, "-".join(map(str, self.path))) 116 117 def __cmp__(self, other): 118 if self.updated is None and other.updated is not None: 119 return 1 120 elif self.updated is not None and other.updated is None: 121 return -1 122 else: 123 return cmp(self.updated, other.updated) 124 125 def copy(self, part_number=None): 126 update = Update() 127 update.title = self.title 128 update.link = self.link 129 update.updated = self.updated 130 update.fragment = self.fragment 131 update.preferred = self.preferred 132 update.message_number = self.message_number 133 update.page = self.page 134 update.path = self.path[:] 135 if part_number is not None: 136 update.path.append(part_number) 137 return update 138 139 # Update retrieval from pages. 140 141 def getUpdatesFromPage(page, request): 142 143 """ 144 Get updates from the given 'page' using the 'request'. A list of update 145 objects is returned. 146 """ 147 148 updates = [] 149 150 # NOTE: Use the updated datetime from the page for updates. 151 # NOTE: The published and updated details would need to be deduced from 152 # NOTE: the page history instead of being taken from the page as a whole. 153 154 metadata = getMetadata(page) 155 updated = getUpdatedTime(metadata) 156 157 # Get the fragment regions for the page. 158 159 for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): 160 161 update = Update() 162 163 # Produce a fragment identifier. 164 # NOTE: Choose a more robust identifier where none is explicitly given. 165 166 update.fragment = attributes.get("fragment", str(n)) 167 update.title = attributes.get("summary", "Update #%d" % n) 168 169 # Get the preferred content types available for the fragment. 170 171 update.preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) 172 173 # Try and obtain some suitable content for the entry. 174 # NOTE: Could potentially get a summary for the fragment. 175 176 update.content = None 177 178 if "text/html" in update.preferred: 179 parser_cls = getParserClass(request, format) 180 181 if format == "html": 182 update.content = body 183 elif hasattr(parser_cls, "formatForOutputType"): 184 update.content = formatTextForOutputType(body, request, parser_cls, "text/html") 185 else: 186 fmt = request.html_formatter 187 fmt.setPage(page) 188 update.content = formatText(body, request, fmt, parser_cls) 189 190 update.content_type = "text/html" 191 192 update.page = page 193 194 # NOTE: The anchor would be supported in the page, but this requires 195 # NOTE: formatter modifications for the regions providing updates. 196 197 update.link = page.url(request, anchor=update.fragment) 198 update.updated = updated 199 200 updates.append(update) 201 202 return updates 203 204 # Update retrieval from message stores. 205 206 def getUpdatesFromStore(page, request): 207 208 """ 209 Get updates from the message store associated with the given 'page' using 210 the 'request'. A list of update objects is returned. 211 """ 212 213 updates = [] 214 215 metadata = getMetadata(page) 216 updated = getUpdatedTime(metadata) 217 218 store = ItemStore(page, "messages", "message-locks") 219 220 for n, message_text in enumerate(iter(store)): 221 update = getUpdateFromMessageText(message_text, n, request) 222 update.page = page 223 updates.append(update) 224 225 return updates 226 227 def getUpdateFromMessageText(message_text, message_number, request): 228 229 "Return an update for the given 'message_text' and 'message_number'." 230 231 update = Update() 232 message = Parser().parse(StringIO(message_text)) 233 234 # Produce a fragment identifier. 235 236 update.fragment = update.updated = getDateTimeFromRFC2822(message.get("date")) 237 update.title = message.get("subject", "Update #%d" % message_number) 238 239 update.message_number = message_number 240 241 update.content, update.content_type, update.parts = getUpdateContentFromPart(message, request) 242 return update 243 244 def getUpdateContentFromPart(part, request): 245 246 """ 247 Return decoded content, the content type and any subparts in a tuple for a 248 given 'part'. 249 """ 250 251 # Determine whether the part has several representations. 252 253 # For a single part, use it as the update content. 254 255 if not part.is_multipart(): 256 content, content_type = getPartContent(part) 257 return content, content_type, None 258 259 # For a collection of related parts, use the first as the update content 260 # and assume that the formatter will reference the other parts. 261 262 elif part.get_content_subtype() == "related": 263 main_part = part.get_payload()[0] 264 content, content_type = getPartContent(main_part) 265 return content, content_type, [main_part] 266 267 # Encrypted content cannot be meaningfully separated. 268 269 elif part.get_content_subtype() == "encrypted": 270 try: 271 part = getDecryptedParts(part, request) 272 return getUpdateContentFromPart(part, request) 273 except MoinMessageError: 274 return None, part.get_content_type(), part.get_payload() 275 276 # Otherwise, just obtain the parts for separate display. 277 278 else: 279 return None, part.get_content_type(), part.get_payload() 280 281 def getDecryptedParts(part, request): 282 283 "Decrypt the given 'part', returning the decoded content." 284 285 homedir = get_homedir(request) 286 gpg = GPG(homedir) 287 288 # Decrypt the part. 289 290 if is_encrypted(part): 291 text = gpg.decryptMessage(part) 292 part = Parser().parse(StringIO(text)) 293 294 # Extract any signature details. 295 # NOTE: Incorporate the signature into the output. 296 297 if is_signed(part): 298 result = gpg.verifyMessage(part) 299 if result: 300 fingerprint, identity, content = result 301 return content 302 303 return part 304 305 def getPartContent(part): 306 307 "Decode the 'part', returning the decoded payload and the content type." 308 309 charset = part.get_content_charset() 310 payload = part.get_payload(decode=True) 311 return (charset and unicode(payload, charset) or payload), part.get_content_type() 312 313 def getUpdateFromPart(parent, part, part_number, request): 314 315 "Using the 'parent' update, return an update object for the given 'part'." 316 317 update = parent.copy(part_number) 318 update.content, update.content_type, update.parts = getUpdateContentFromPart(part, request) 319 return update 320 321 def getUpdatesForFormatting(update, request): 322 323 "Get a list of updates for formatting given 'update'." 324 325 updates = [] 326 327 # Handle multipart/alternative and other non-related multiparts. 328 329 if update.parts: 330 for n, part in enumerate(update.parts): 331 update_part = getUpdateFromPart(update, part, n, request) 332 updates += getUpdatesForFormatting(update_part, request) 333 else: 334 updates.append(update) 335 336 return updates 337 338 # Update formatting. 339 340 def getFormattedUpdate(update, request, fmt): 341 342 """ 343 Return the formatted form of the given 'update' using the given 'request' 344 and 'fmt'. 345 """ 346 347 # NOTE: Some control over the HTML and XHTML should be exercised. 348 349 if update.content: 350 if update.content_type == "text/html" and update.message_number is not None: 351 parsers = [get_make_parser(update.page, update.message_number)] 352 else: 353 parsers = getParsersForContentType(request.cfg, update.content_type) 354 355 if parsers: 356 for parser_cls in parsers: 357 if hasattr(parser_cls, "formatForOutputType"): 358 return formatTextForOutputType(update.content, request, parser_cls, "text/html") 359 else: 360 return formatText(update.content, request, fmt, parser_cls=parser_cls) 361 break 362 else: 363 return None 364 else: 365 return None 366 367 def formatUpdate(update, request, fmt): 368 369 "Format the given 'update' using the given 'request' and 'fmt'." 370 371 result = [] 372 append = result.append 373 374 updates = getUpdatesForFormatting(update, request) 375 single = len(updates) == 1 376 377 # Format some navigation tabs. 378 379 if not single: 380 append(fmt.div(on=1, css_class="moinshare-alternatives")) 381 382 first = True 383 384 for update_part in updates: 385 append(fmt.url(1, "#%s" % update_part.unique_id())) 386 append(fmt.text(update_part.content_type)) 387 append(fmt.url(0)) 388 389 first = False 390 391 append(fmt.div(on=0)) 392 393 # Format the content. 394 395 first = True 396 397 for update_part in updates: 398 399 # Encapsulate each alternative if many exist. 400 401 if not single: 402 css_class = first and "moinshare-default" or "moinshare-other" 403 append(fmt.div(on=1, css_class="moinshare-alternative %s" % css_class, id=update_part.unique_id())) 404 405 # Include the content. 406 407 append(formatUpdatePart(update_part, request, fmt)) 408 409 if not single: 410 append(fmt.div(on=0)) 411 412 first = False 413 414 return "".join(result) 415 416 def formatUpdatePart(update, request, fmt): 417 418 "Format the given 'update' using the given 'request' and 'fmt'." 419 420 _ = request.getText 421 422 result = [] 423 append = result.append 424 425 # Encapsulate the content. 426 427 append(fmt.div(on=1, css_class="moinshare-content")) 428 text = getFormattedUpdate(update, request, fmt) 429 if text: 430 append(text) 431 else: 432 append(fmt.text(_("Update cannot be shown for content of type %s.") % update.content_type)) 433 append(fmt.div(on=0)) 434 435 return "".join(result) 436 437 # Source management. 438 439 def getUpdateSources(pagename, request): 440 441 "Return the update sources from the given 'pagename' using the 'request'." 442 443 sources = {} 444 445 source_definitions = getWikiDict(pagename, request) 446 447 if source_definitions: 448 for name, value in source_definitions.items(): 449 sources[name] = getSourceParameters(value) 450 451 return sources 452 453 def getSourceParameters(source_definition): 454 455 "Return the parameters from the given 'source_definition' string." 456 457 return parseDictEntry(source_definition, ("type", "location")) 458 459 # HTML parsing support. 460 461 class IncomingHTMLSanitizer(HTMLSanitizer): 462 463 "An HTML parser that rewrites references to attachments." 464 465 def __init__(self, out, request, page, message_number): 466 HTMLSanitizer.__init__(self, out) 467 self.request = request 468 self.message_number = message_number 469 self.page = page 470 471 def rewrite_reference(self, ref): 472 if ref.startswith("cid:"): 473 part = ref[len("cid:"):] 474 action_link = self.page.url(self.request, { 475 "action" : "ReadMessage", "doit" : "1", 476 "message" : self.message_number, "part" : part 477 }) 478 return action_link 479 else: 480 return ref 481 482 def handle_starttag(self, tag, attrs): 483 new_attrs = [] 484 for attrname, attrvalue in attrs: 485 if attrname in self.uri_attrs: 486 new_attrs.append((attrname, self.rewrite_reference(attrvalue))) 487 else: 488 new_attrs.append((attrname, attrvalue)) 489 HTMLSanitizer.handle_starttag(self, tag, new_attrs) 490 491 class IncomingMarkup(Markup): 492 493 "A special markup processor for incoming HTML." 494 495 def sanitize(self, request, page, message_number): 496 out = getwriter("utf-8")(StringIO()) 497 sanitizer = IncomingHTMLSanitizer(out, request, page, message_number) 498 sanitizer.feed(self.stripentities(keepxmlentities=True)) 499 return IncomingMarkup(unicode(out.getvalue(), "utf-8")) 500 501 class IncomingHTMLParser: 502 503 "Filters and rewrites incoming HTML content." 504 505 def __init__(self, raw, request, **kw): 506 self.raw = raw 507 self.request = request 508 self.message_number = None 509 self.page = None 510 511 def format(self, formatter, **kw): 512 513 "Send the text." 514 515 try: 516 self.request.write(formatter.rawHTML(IncomingMarkup(self.raw).sanitize(self.request, self.page, self.message_number))) 517 except HTMLParseError, e: 518 self.request.write(formatter.sysmsg(1) + 519 formatter.text(u'HTML parsing error: %s in "%s"' % (e.msg, 520 self.raw.splitlines()[e.lineno - 1].strip())) + 521 formatter.sysmsg(0)) 522 523 class MakeIncomingHTMLParser: 524 525 "A class that makes parsers configured for messages." 526 527 def __init__(self, page, message_number): 528 529 "Initialise with state that is used to configure instantiated parsers." 530 531 self.message_number = message_number 532 self.page = page 533 534 def __call__(self, *args, **kw): 535 parser = IncomingHTMLParser(*args, **kw) 536 parser.message_number = self.message_number 537 parser.page = self.page 538 return parser 539 540 def get_make_parser(page, message_number): 541 542 """ 543 Return a callable that will return a parser configured for the message from 544 the given 'page' with the given 'message_number'. 545 """ 546 547 return MakeIncomingHTMLParser(page, message_number) 548 549 # vim: tabstop=4 expandtab shiftwidth=4