From 3bd54a3e75715fa8c998db16b40390a33dbeea56 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 9 Oct 2017 22:32:41 -0700 Subject: [PATCH] offlineimap daemon added --- .config/Scripts/offlineimap-daemon.py | 93 ++++++++++++++++++++++++ .config/Scripts/offlineimap-daemonctl.sh | 40 ++++++++++ .config/i3/config | 3 + 3 files changed, 136 insertions(+) create mode 100755 .config/Scripts/offlineimap-daemon.py create mode 100755 .config/Scripts/offlineimap-daemonctl.sh diff --git a/.config/Scripts/offlineimap-daemon.py b/.config/Scripts/offlineimap-daemon.py new file mode 100755 index 00000000..fea76fcd --- /dev/null +++ b/.config/Scripts/offlineimap-daemon.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +import subprocess +import signal +import threading +import sys + +import dbus +from dbus.mainloop.glib import DBusGMainLoop +from gi.repository import GLib + + +class OfflineimapCtl(object): + def __init__(self): + self.daemon_proc = None + self.run_ev = threading.Event() + self.run_daemon = False + + def run(self): + t = threading.Thread(target=self._watch_daemon, daemon=True) + t.start() + + def _watch_daemon(self): + while True: + self.run_ev.wait() + self.run_ev.clear() + if self.run_daemon: + self.is_running = True + print('offlineimap is being started') + self._spawn_daemon() + print('offlineimap has stopped') + self.run_ev.set() # check state and restart if needed + + def _spawn_daemon(self): + self.daemon_proc = subprocess.Popen(['offlineimap', '-u', 'basic'], shell=False) + self.daemon_proc.wait() + self.daemon_proc = None + + def start(self): + print('starting offlineimap') + self.run_daemon = True + self.run_ev.set() + + def stop(self): + print('stopping offlineimap') + self.run_daemon = False + if self.daemon_proc: + try: + self.daemon_proc.send_signal(signal.SIGUSR2) + except OSError: + print('Unable to stop offlineimap') + + def restart(self): + print('restarting offlineimap') + if self.run_daemon: + self.stop() + self.start() + + def onConnectivityChanged(self, state): + # 70 means fully connected + if state == 70: + self.start() + else: + self.stop() + +def main(): + oi_ctl = OfflineimapCtl() + oi_ctl.run() + + try: + bus = dbus.SystemBus(mainloop=DBusGMainLoop()) + network_manager = bus.get_object( + 'org.freedesktop.NetworkManager', + '/org/freedesktop/NetworkManager') + network = dbus.Interface(network_manager, + dbus_interface='org.freedesktop.NetworkManager') + + network.connect_to_signal('StateChanged', oi_ctl.onConnectivityChanged) + + # send current state as first event + state = network.state() + oi_ctl.onConnectivityChanged(state) + + except dbus.exceptions.DBusException: + print('Unable to connect to dbus') + sys.exit(3) + + # start receiving events from dbus + loop = GLib.MainLoop() + loop.run() + +if __name__ == '__main__': + main() diff --git a/.config/Scripts/offlineimap-daemonctl.sh b/.config/Scripts/offlineimap-daemonctl.sh new file mode 100755 index 00000000..18daf3da --- /dev/null +++ b/.config/Scripts/offlineimap-daemonctl.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# this script runs offline imap as daemon (configured to check periodically) + +LOG=~/.offlineimap/sync.log +PIDFILE=~/.offlineimap/pid + +# if not present on PATH, those vars must point to proper locations +THIS_SCRIPT=offlineimap-daemonctl.sh +PYTHON_DAEMON=offlineimap-daemon.py + +daemon(){ + $PYTHON_DAEMON 2>&1 | + # add timestamps to logs + (while read line; do + echo `date` "$line" >> $LOG + done) +} + +stop(){ + kill -USR2 `cat $PIDFILE` +} + +refresh(){ + kill -USR1 `cat $PIDFILE` +} + +case "$1" in + '--daemon' | '-d' ) + nohup $THIS_SCRIPT < /dev/null > /dev/null 2>&1 & + ;; + '--kill' | '-k' ) + stop + ;; + '--refresh' | '-r' ) + refresh + ;; + * ) + daemon + ;; +esac \ No newline at end of file diff --git a/.config/i3/config b/.config/i3/config index d01cc942..03f96ad2 100644 --- a/.config/i3/config +++ b/.config/i3/config @@ -40,6 +40,9 @@ exec --no-startup-id python ~/.config/Scripts/shortcuts.py exec --no-startup-id ~/.config/Scripts/screen.sh v #Launch Polybar where appropriate: exec_always --no-startup-id ~/.config/polybar/launch.sh +#Start the offlineimap daemon; +exec --no-startup-id killall python ~/.config/Scripts/offlineimap-daemon.py +exec --no-startup-id python ~/.config/Scripts/offlineimap-daemon.py #Add wallpaper: #exec --no-startup-id feh --bg-scale ~/.config/wall.png exec_always --no-startup-id wal -c -i ~/.config/wall.png