diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mmfwd/__init__.py | 42 | ||||
-rw-r--r-- | src/mmfwd/__main__.py | 7 |
2 files changed, 38 insertions, 11 deletions
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() |