Skip to content

Instantly share code, notes, and snippets.

@ekollof
Created June 3, 2022 11:28
Show Gist options
  • Save ekollof/617996557d48d1de3f2ae1cecb994ce4 to your computer and use it in GitHub Desktop.
Save ekollof/617996557d48d1de3f2ae1cecb994ce4 to your computer and use it in GitHub Desktop.
Python script that checks plans with finger
#!/usr/bin/env python3
import sys
import os
import socket
import hashlib
import subprocess
CONFIG=os.path.expanduser("~/.config/planmon/config")
CACHEDIR=os.path.expanduser("~/.cache/planmon")
def finger(address):
user, host = address.split("@")
server = ( host, 79 )
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
data = ""
try:
sock.connect(server)
sock.sendall((user + "\n").encode())
data = sock.recv(8192)
data = data.decode()
except Exception as ex:
print(f"error: {ex}")
return data
def checkdiff(address, text):
if not os.path.exists(CACHEDIR):
os.makedirs(CACHEDIR)
texthash = hashlib.sha1(text.encode())
digest = texthash.hexdigest()
if os.path.exists(CACHEDIR + "/" + address):
fp = open(CACHEDIR + "/" + address)
if digest == fp.read():
fp.close()
return False
else: # plan has changed
fp = open(CACHEDIR + "/" + address, "w")
fp.write(digest) #update hash
fp.close()
subprocess.run(['notify-send',
f'Plan update from {address}', f'{text}'],
check=True)
return True
else: # new entry!
fp = open(CACHEDIR + "/" + address, "w")
fp.write(digest) #update hash
fp.close()
subprocess.run(['notify-send',
f'Plan cached from {address}', f'{text}'],
check=True)
return True
def main():
try:
config = open(CONFIG, "r")
except Exception as ex:
print(f"Can't open config {ex}")
for address in config.readlines():
address = address.strip()
text = finger(address)
if checkdiff(address,text):
print("Something changed...")
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment