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 def __cmp__(self, other): 119 if self.updated is None and other.updated is not None: 120 return 1 121 elif self.updated is not None and other.updated is None: 122 return -1 123 else: 124 return cmp(self.updated, other.updated) 125 126 # Update retrieval from pages. 127 128 def getUpdatesFromPage(page, request): 129 130 """ 131 Get updates from the given 'page' using the 'request'. A list of update 132 objects is returned. 133 """ 134 135 updates = [] 136 137 # NOTE: Use the updated datetime from the page for updates. 138 # NOTE: The published and updated details would need to be deduced from 139 # NOTE: the page history instead of being taken from the page as a whole. 140 141 metadata = getMetadata(page) 142 updated = getUpdatedTime(metadata) 143 144 # Get the fragment regions for the page. 145 146 for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): 147 148 update = Update() 149 150 # Produce a fragment identifier. 151 # NOTE: Choose a more robust identifier where none is explicitly given. 152 153 update.fragment = attributes.get("fragment", str(n)) 154 update.title = attributes.get("summary", "Update #%d" % n) 155 156 # Get the preferred content types available for the fragment. 157 158 update.preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) 159 160 # Try and obtain some suitable content for the entry. 161 # NOTE: Could potentially get a summary for the fragment. 162 163 update.content = None 164 165 if "text/html" in update.preferred: 166 parser_cls = getParserClass(request, format) 167 168 if format == "html": 169 update.content = body 170 elif hasattr(parser_cls, "formatForOutputType"): 171 update.content = formatTextForOutputType(body, request, parser_cls, "text/html") 172 else: 173 fmt = request.html_formatter 174 fmt.setPage(page) 175 update.content = formatText(body, request, fmt, parser_cls) 176 177 update.content_type = "text/html" 178 179 update.page = page 180 181 # NOTE: The anchor would be supported in the page, but this requires 182 # NOTE: formatter modifications for the regions providing updates. 183 184 update.link = page.url(request, anchor=update.fragment) 185 update.updated = updated 186 187 updates.append(update) 188 189 return updates 190 191 # Update retrieval from message stores. 192 193 def getUpdatesFromStore(page, request): 194 195 """ 196 Get updates from the message store associated with the given 'page' using 197 the 'request'. A list of update objects is returned. 198 """ 199 200 updates = [] 201 202 metadata = getMetadata(page) 203 updated = getUpdatedTime(metadata) 204 205 store = ItemStore(page, "messages", "message-locks") 206 207 for n, message_text in enumerate(iter(store)): 208 209 update = Update() 210 message = Parser().parse(StringIO(message_text)) 211 212 # Produce a fragment identifier. 213 214 update.fragment = update.updated = getDateTimeFromRFC2822(message.get("date")) 215 update.title = message.get("subject", "Update #%d" % n) 216 217 update.page = page 218 update.message_number = n 219 220 # Determine whether the message has several representations. 221 222 # For a single part, use it as the update content. 223 224 if not message.is_multipart(): 225 charset = message.get_content_charset() 226 payload = message.get_payload(decode=True) 227 update.content = charset and unicode(payload, charset) or payload 228 update.content_type = message.get_content_type() 229 230 # For a collection of related parts, use the first as the update content 231 # and assume that the formatter will reference the other parts. 232 233 elif message.get_content_subtype() == "related": 234 main_part = message.get_payload()[0] 235 charset = main_part.get_content_charset() 236 payload = main_part.get_payload(decode=True) 237 update.content = charset and unicode(payload, charset) or payload 238 update.content_type = main_part.get_content_type() 239 240 # Otherwise, just obtain the parts for separate display. 241 242 else: 243 update.parts = message.get_payload() 244 update.content_type = message.get_content_type() 245 246 updates.append(update) 247 248 return updates 249 250 # Source management. 251 252 def getUpdateSources(pagename, request): 253 254 "Return the update sources from the given 'pagename' using the 'request'." 255 256 sources = {} 257 258 source_definitions = getWikiDict(pagename, request) 259 260 if source_definitions: 261 for name, value in source_definitions.items(): 262 sources[name] = getSourceParameters(value) 263 264 return sources 265 266 def getSourceParameters(source_definition): 267 268 "Return the parameters from the given 'source_definition' string." 269 270 parameters = {} 271 unqualified = ("type", "location") 272 273 for arg in source_definition.split(): 274 try: 275 argname, argvalue = arg.split("=", 1) 276 277 # Detect unlikely parameter names. 278 279 if not argname.isalpha(): 280 raise ValueError 281 282 parameters[argname] = argvalue 283 284 # Unqualified parameters are assumed to be one of a recognised set. 285 286 except ValueError: 287 for argname in unqualified: 288 if not parameters.has_key(argname): 289 parameters[argname] = arg 290 break 291 292 return parameters 293 294 # HTML parsing support. 295 296 class IncomingHTMLSanitizer(HTMLSanitizer): 297 298 "An HTML parser that rewrites references to attachments." 299 300 def __init__(self, out, request, page, message_number): 301 HTMLSanitizer.__init__(self, out) 302 self.request = request 303 self.message_number = message_number 304 self.page = page 305 306 def rewrite_reference(self, ref): 307 if ref.startswith("cid:"): 308 part = ref[len("cid:"):] 309 action_link = self.page.url(self.request, { 310 "action" : "ReadMessage", "doit" : "1", 311 "message" : self.message_number, "part" : part 312 }) 313 return action_link 314 else: 315 return ref 316 317 def handle_starttag(self, tag, attrs): 318 new_attrs = [] 319 for attrname, attrvalue in attrs: 320 if attrname in self.uri_attrs: 321 new_attrs.append((attrname, self.rewrite_reference(attrvalue))) 322 else: 323 new_attrs.append((attrname, attrvalue)) 324 HTMLSanitizer.handle_starttag(self, tag, new_attrs) 325 326 class IncomingMarkup(Markup): 327 328 "A special markup processor for incoming HTML." 329 330 def sanitize(self, request, page, message_number): 331 out = getwriter("utf-8")(StringIO()) 332 sanitizer = IncomingHTMLSanitizer(out, request, page, message_number) 333 sanitizer.feed(self.stripentities(keepxmlentities=True)) 334 return IncomingMarkup(unicode(out.getvalue(), "utf-8")) 335 336 class IncomingHTMLParser: 337 338 "Filters and rewrites incoming HTML content." 339 340 def __init__(self, raw, request, **kw): 341 self.raw = raw 342 self.request = request 343 self.message_number = None 344 self.page = None 345 346 def format(self, formatter, **kw): 347 348 "Send the text." 349 350 try: 351 self.request.write(formatter.rawHTML(IncomingMarkup(self.raw).sanitize(self.request, self.page, self.message_number))) 352 except HTMLParseError, e: 353 self.request.write(formatter.sysmsg(1) + 354 formatter.text(u'HTML parsing error: %s in "%s"' % (e.msg, 355 self.raw.splitlines()[e.lineno - 1].strip())) + 356 formatter.sysmsg(0)) 357 358 class MakeIncomingHTMLParser: 359 360 "A class that makes parsers configured for messages." 361 362 def __init__(self, page, message_number): 363 364 "Initialise with state that is used to configure instantiated parsers." 365 366 self.message_number = message_number 367 self.page = page 368 369 def __call__(self, *args, **kw): 370 parser = IncomingHTMLParser(*args, **kw) 371 parser.message_number = self.message_number 372 parser.page = self.page 373 return parser 374 375 def get_make_parser(page, message_number): 376 377 """ 378 Return a callable that will return a parser configured for the message from 379 the given 'page' with the given 'message_number'. 380 """ 381 382 return MakeIncomingHTMLParser(page, message_number) 383 384 # vim: tabstop=4 expandtab shiftwidth=4