summaryrefslogtreecommitdiff
path: root/notmuch-ssh-server
blob: 9454516f5b555d2621ebc19cfbb16828a7c5e7bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!@python@/bin/python

import os
import sys
import shlex
from xdg import BaseDirectory
from pathlib import Path
from configparser import ConfigParser
import logging
from logging.handlers import SysLogHandler


syslog = SysLogHandler( address = '/dev/log' )
logging.basicConfig(handlers = [syslog], level = logging.DEBUG)


config = ConfigParser()
config.optionxform = str
config_file = BaseDirectory.load_first_config('notmuch-ssh')
if config_file is not None:
    config.read(config_file)

original_command = os.environ.get('SSH_ORIGINAL_COMMAND')

if original_command is None:
    sys.exit(os.EX_USAGE)

original_command = shlex.split(original_command, comments = True)
logging.debug("Original command: %s", original_command)

if len(original_command) < 1:
    sys.exit(os.EX_USAGE)

config_section = original_command[0]
logging.debug("Config section: %s", config_section)

config_environment = config[config_section] if config.has_section(config_section) else config[config.default_section]
logging.info("Config environment: %s", {**config_environment})

logging.info("notmuch %s", original_command[1:])
os.execvpe('@notmuch@/bin/notmuch', ['notmuch'] + original_command[1:], {**os.environ, **config_environment})