diff --git a/emails.py b/emails.py index 032b532..4a6de1a 100755 --- a/emails.py +++ b/emails.py @@ -16,8 +16,6 @@ import smtplib import sys - - ############################################################################### # SETTINGS ############################################################################### @@ -99,7 +97,7 @@ repository_name = email_to[1].rsplit('@', 1)[0] request_subscribe = repository_name.endswith('+subscribe') request_unsubscribe = repository_name.endswith('+unsubscribe') -# Remove leading command: from address +# Remove command from address if request_subscribe: repository_name = repository_name[:-10] if request_unsubscribe: repository_name = repository_name[:-12] @@ -127,8 +125,8 @@ except: try: head_tree = repo.revparse_single('HEAD').tree except: - logging.error('Could not find HEAD ref: {}'.format(repository_path)) - exit() + logging.warning('Could not find HEAD ref: {}'.format(repository_path)) + head_tree = None try: subscribers = [] @@ -138,7 +136,7 @@ try: subscribers.append(addr) except: subscribers = [] - logging.info('Subscribers file not found in {}'.format(repository_path)) + logging.info('Subscribers file not found or invalid: {}'.format(repository_path)) @@ -147,14 +145,16 @@ except: # LISTS SUBSCRIPTION ############################################################################### -if request_subscribe \ -and email_from[1] in subscribers: +if request_subscribe and (email_from[1] in subscribers): # Already subscribed + + logging.info('Already subscribed to {}: {}'.format(repository_path, email_from)) exit() -if request_unsubscribe \ -and email_from[1] not in subscribers: - # No address to removed +if request_unsubscribe and (email_from[1] not in subscribers): + # No address to remove + + logging.info('Already unsubscribed from {}: {}'.format(repository_path, email_from)) exit() if request_subscribe or request_unsubscribe: @@ -170,19 +170,24 @@ if request_subscribe or request_unsubscribe: oid = repo.create_blob('\n'.join(subscribers).encode('UTF-8')) # Add the blob that we've just created to the HEAD tree - head_tree_builder = repo.TreeBuilder(head_tree) + head_tree_builder = repo.TreeBuilder(head_tree) if head_tree else repo.TreeBuilder() head_tree_builder.insert('subscribers', oid, pygit2.GIT_FILEMODE_BLOB) head_tree_oid = head_tree_builder.write() repo.create_commit( - repo.head.name, # reference name + 'HEAD', # reference name pygit2.Signature('CLIF', '-'), # author pygit2.Signature('CLIF', '-'), # committer commit_message, # message head_tree_oid, # tree of this commit - [ repo.head.target ] # parents commit + [] if repo.is_empty else [ repo.head.target ] # parents commit ) + if request_subscribe: + logging.info('Subscribed to {}: {}'.format(repository_path, email_from)) + if request_unsubscribe: + logging.info('Unsubscribed from {}: {}'.format(repository_path, email_from)) + exit() @@ -218,19 +223,24 @@ thread_title = '{} {} {}'.format( ) if email_in_reply_to: - # The hash of the email that is being replied to - parent_message_hash = hashlib.sha256(email_in_reply_to.encode('utf-8')).hexdigest()[:8] - - # Find the thread (tree) containing the parent message - for obj in head_tree: - if obj.type_str == 'tree' and parent_message_hash + '.email' in obj: - thread_tree = obj - thread_title = obj.name - break - - # We only accept emails in reply to existing messages - if not thread_tree: - logging.debug('In-Reply-To ID not found in repository: {}'.format(email_in_reply_to)) + try: + assert head_tree + + # The hash of the email that is being replied to + parent_message_hash = hashlib.sha256(email_in_reply_to.encode('utf-8')).hexdigest()[:8] + + # Find the thread (tree) containing the parent message + for obj in head_tree: + if obj.type_str == 'tree' and parent_message_hash + '.email' in obj: + thread_tree = obj + thread_title = obj.name + break + + assert thread_tree + + except: + # We only accept emails as reply to existing messages + logging.debug('In-Reply-To message ID not found in repository: {}'.format(email_in_reply_to)) exit() # Add the new email BLOB to the git store @@ -242,17 +252,17 @@ thread_tree_builder.insert(email_id_hash + '.email', message_oid, pygit2.GIT_FIL thread_tree_oid = thread_tree_builder.write() # Add the thread tree to the HEAD tree -head_tree_builder = repo.TreeBuilder(head_tree) +head_tree_builder = repo.TreeBuilder(head_tree) if head_tree else repo.TreeBuilder() head_tree_builder.insert(thread_title, thread_tree_oid, pygit2.GIT_FILEMODE_TREE) head_tree_oid = head_tree_builder.write() repo.create_commit( - repo.head.name, # reference name + 'HEAD', # reference name pygit2.Signature('CLIF', '-'), # author pygit2.Signature('CLIF', '-'), # committer 'New email.', # message head_tree_oid, # tree of this commit - [ repo.head.target ] # parents commit + [] if repo.is_empty else [ repo.head.target ] # parents commit )