aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--mmfwd.service13
-rw-r--r--pyproject.toml4
-rw-r--r--src/mmfwd/__init__.py42
-rw-r--r--src/mmfwd/__main__.py7
5 files changed, 54 insertions, 13 deletions
diff --git a/.gitignore b/.gitignore
index cdaae18..5e9e52f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
__pycache__
/src/mmfwd.yaml
+/dist
diff --git a/mmfwd.service b/mmfwd.service
new file mode 100644
index 0000000..73a70f2
--- /dev/null
+++ b/mmfwd.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Forward calls and texts using ModemManager
+Requires=ModemManager.service
+After=postfix.service ModemManager.service network-online.target
+
+[Service]
+User=mmfwd
+Group=mmfwd
+Environment=MMFWD_CONFIG=/etc/mmfwd/mmfwd.yaml
+ExecStart=/usr/bin/env python3 -m mmfwd
+
+[Install]
+WantedBy=multi-user.target
diff --git a/pyproject.toml b/pyproject.toml
index 91073b0..b02720d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -10,8 +10,8 @@ authors = [
]
description = "Forward calls and texts using ModemManager framework"
readme = "README.md"
-requires-python = ">= 3.11" # TODO
-dependencies = [ "pyjson5", "PyYAML", "PyGObject" ]
+requires-python = ">= 3.6"
+dependencies = [ "PyYAML", "PyGObject" ]
classifiers = [
"Programming Language :: Python :: 3",
diff --git a/src/mmfwd/__init__.py b/src/mmfwd/__init__.py
index 0228ac6..26ca59e 100644
--- a/src/mmfwd/__init__.py
+++ b/src/mmfwd/__init__.py
@@ -29,11 +29,12 @@ class Forward:
self.mailto: list[str] = conf.get("mailto", [])
self.cmd: list[str] = conf.get("cmd", [])
- def post (self, doc):
+ def post_sms (self, doc):
cmd = list[str]()
for arg in self.cmd:
cmd.append(arg.format(
- sender = doc["sms"]["from"],
+ type = "sms",
+ origin = doc["sms"]["from"],
to = doc["sms"]["to"],
ts_req = doc["sms"]["ts-req"],
ts_del = doc["sms"]["ts-del"],
@@ -44,6 +45,21 @@ class Forward:
yaml.dump(doc, p.stdin, encoding = 'utf-8', allow_unicode = True)
p.stdin.close()
+ def post_call (self, doc):
+ cmd = list[str]()
+ for arg in self.cmd:
+ cmd.append(arg.format(
+ type = "call",
+ origin = doc["call"]["from"],
+ to = doc["call"]["to"],
+ multiparty = doc["call"]["multiparty"],
+ ))
+
+ with subprocess.Popen(cmd, stdin = subprocess.PIPE) as p:
+ p.stdin.write(("--" + os.linesep).encode())
+ yaml.dump(doc, p.stdin, encoding = 'utf-8', allow_unicode = True)
+ p.stdin.close()
+
class Instance:
def __init__ (self, conf: dict[str, Any]):
self.mobj: str = None
@@ -201,11 +217,6 @@ class Application:
def on_message_added (self, messaging, path, received, ud):
messaging.list(None, self.on_messages, ud)
- print('''[mmfwd] on_message_added: {a} {b} {c}'''.format(
- a = messaging,
- b = path,
- c = received,
- ))
def on_messages (self, messaging, task, ud):
for m in messaging.list_finish(task):
@@ -226,7 +237,7 @@ class Application:
print("--")
yaml.dump(doc, sys.stdout, allow_unicode = True)
- ud.instance.fwd.post(doc)
+ ud.instance.fwd.post_sms(doc)
messaging.delete(path, None, self.on_message_delete)
@@ -236,6 +247,19 @@ class Application:
def on_call_added (self, voice, path, ud):
voice.list_calls(None, self.on_calls, ud)
+ def on_incoming_call (self, call, ud):
+ doc = {
+ "call": {
+ "from": call.get_number(),
+ "to": ud.own_numbers,
+ "multiparty": call.get_multiparty(),
+ }
+ }
+
+ print("--")
+ yaml.dump(doc, sys.stdout, allow_unicode = True)
+ ud.instance.fwd.post_call(doc)
+
def on_calls (self, voice, task, ud = None):
for c in voice.list_calls_finish(task):
state = c.get_state()
@@ -246,6 +270,8 @@ class Application:
if state == ModemManager.CallState.ACTIVE:
c.hangup(None, self.on_call_hangup, nud)
elif state == ModemManager.CallState.RINGING_IN:
+ self.on_incoming_call(c, ud)
+
if True:
# FIXME
# just hang up for now
diff --git a/src/mmfwd/__main__.py b/src/mmfwd/__main__.py
index 427e512..3ca2856 100644
--- a/src/mmfwd/__main__.py
+++ b/src/mmfwd/__main__.py
@@ -11,11 +11,12 @@ def handle_signal (loop):
# load config
try:
- from yaml import CLoader as Loader, CDumper as Dumper
+ from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
- from yaml import Loader, Dumper
+ from yaml import Loader, Dumper
-conf = yaml.load(open(CONFIG_FILENAME), Loader)["mmfwd"]
+conf = yaml.load(
+ open(os.getenv("MMFWD_CONFIG") or CONFIG_FILENAME), Loader)["mmfwd"]
# instantiate the singleton objects
app = Application(conf)
main_loop = GLib.MainLoop()