diff --git a/git.py b/git.py index 255ce858..c2b71c76 100644 --- a/git.py +++ b/git.py @@ -9,8 +9,26 @@ __status__ = "Production" import os -def extractPatch(baseDir: str, nickname: str, domain: str, - subject: str, content: str) -> bool: +def getGitProjectName(baseDir: str, nickname: str, domain: str, + subject: str, content: str) -> str: + """Returns the project name for a git patch + The project name should be contained within the subject line + and should match against a list of projects which the account + holder wants to receive + """ + gitProjectsFilename = \ + baseDir + '/accounts/' + nickname + '@' + domain + '/gitprojects.txt' + if not os.path.isfile(gitProjectsFilename): + return None + projectName = None + for word in subject.lower().split(' '): + if word + '\n' in open(gitProjectsFilename).read(): + return word + return projectName + + +def isGitPatch(baseDir: str, nickname: str, domain: str, + subject: str, content: str) -> bool: """Is the given post content a git patch? """ # must have a subject line @@ -30,17 +48,21 @@ def extractPatch(baseDir: str, nickname: str, domain: str, return False if '\n' not in content: return False - gitProjectsFilename = \ - baseDir + '/accounts/' + nickname + '@' + domain + '/gitprojects.txt' - if not os.path.isfile(gitProjectsFilename): - return False - projectName = None - for word in subject.lower().split(' '): - if word + '\n' in open(gitProjectsFilename).read(): - projectName = word - break + projectName = \ + getGitProjectName(baseDir, nickname, domain, + subject, content) if not projectName: return False + return True + + +def receiveGitPatch(baseDir: str, nickname: str, domain: str, + subject: str, content: str) -> bool: + """Receive a git patch + """ + if not isGitPatch(baseDir, nickname, domain, + subject, content): + return False patchLines = content.split('\n') patchFilename = None patchDir = None @@ -49,6 +71,9 @@ def extractPatch(baseDir: str, nickname: str, domain: str, if line.startswith('Subject:'): patchSubject = \ line.replace('Subject:', '').replace('/', '|').strip() + projectName = \ + getGitProjectName(baseDir, nickname, domain, + subject, content) patchDir = \ baseDir + '/accounts/' + nickname + '@' + domain + \ '/patches/' + projectName diff --git a/inbox.py b/inbox.py index c36df802..eb638ee9 100644 --- a/inbox.py +++ b/inbox.py @@ -58,6 +58,8 @@ from webinterface import individualPostAsHtml from webinterface import getIconsDir from question import questionUpdateVotes from media import replaceYouTube +from git import isGitPatch +from git import receiveGitPatch def storeHashTags(baseDir: str, nickname: str, postJsonObject: {}) -> None: @@ -1578,6 +1580,10 @@ def validPostContent(baseDir: str, nickname: str, domain: str, return False if 'Z' not in messageJson['object']['published']: return False + if isGitPatch(baseDir, nickname, domain, + messageJson['object']['summary'], + messageJson['object']['content']): + return True # check for bad html invalidStrings = ('