Ticket #2706: tracdiscussion-anti-spam-patch.diff
| File tracdiscussion-anti-spam-patch.diff, 3.9 kB (added by mickem, 9 months ago) |
|---|
-
setup.py
old new 14 14 'TracDiscussion.timeline = tracdiscussion.timeline', 15 15 'TracDiscussion.admin = tracdiscussion.admin', 16 16 'TracDiscussion.search = tracdiscussion.search', 17 'TracDiscussion.spam = tracdiscussion.spam', 17 18 'TracDiscussion.notification = tracdiscussion.notification']}, 18 19 install_requires = ['TracWebAdmin'], 19 20 keywords = 'trac discussion', -
tracdiscussion/core.py
old new 17 17 implements(INavigationContributor, IRequestHandler, ITemplateProvider, 18 18 IPermissionRequestor) 19 19 20 discussion_manipulators = ExtensionPoint(IDiscussionManipulator) 21 20 22 title = Option('discussion', 'title', 'Discussion', 21 23 'Main navigation bar button title.') 22 24 -
tracdiscussion/api.py
old new 9 9 from trac.util.text import to_unicode 10 10 import time 11 11 12 class InvalidDiscussionPost(TracError): 13 """Exception raised when a ticket fails validation.""" 14 15 class IDiscussionManipulator(Interface): 16 """Miscellaneous manipulation of forum posts.""" 17 18 def validate_message(self, req, author, body): 19 """Validate a new message post in a topic. 20 21 Must return a list of `(field, message)` tuples, one for each problem 22 detected. `field` can be `None` to indicate an overall problem with the 23 ticket. Therefore, a return value of `[]` means everything is OK.""" 24 25 def validate_topic(self, req, author, subject, body): 26 """Validate a new topic. 27 28 Must return a list of `(field, message)` tuples, one for each problem 29 detected. `field` can be `None` to indicate an overall problem with the 30 ticket. Therefore, a return value of `[]` means everything is OK.""" 31 32 12 33 class DiscussionApi(object): 13 34 def __init__(self, component, req): 14 35 self.env = component.env 15 36 self.log = component.log 37 self.discussion_manipulators = component.discussion_manipulators 16 38 17 39 # Main request processing function 18 40 … … 531 553 new_author = req.args.get('author') 532 554 new_body = req.args.get('body') 533 555 new_time = int(time.time()) 556 for manipulator in self.discussion_manipulators: 557 for field, message in manipulator.validate_topic(req, new_author, new_subject, new_body): 558 if field: 559 raise InvalidDiscussionPost("The field %s in message is invalid: %s" % 560 (field, message)) 561 else: 562 raise InvalidDiscussionPost("Invalid post: %s" % message) 534 563 535 564 # Add topic. 536 565 self.add_topic(cursor, forum['id'], new_subject, new_time, … … 641 670 new_author = req.args.get('author') 642 671 new_body = req.args.get('body') 643 672 new_time = int(time.time()) 673 # Custom validation rules 674 for manipulator in self.discussion_manipulators: 675 for field, message in manipulator.validate_message(req, new_author, new_body): 676 if field: 677 raise InvalidDiscussionPost("The field %s in message is invalid: %s" % 678 (field, message)) 679 else: 680 raise InvalidDiscussionPost("Invalid post: %s" % message) 644 681 645 682 # Add message. 646 683 if message:
