Skip to content

Instantly share code, notes, and snippets.

@morsine
Created October 13, 2024 18:49
Show Gist options
  • Save morsine/4b4530ef5cf0df6f11d540d0b3c475b2 to your computer and use it in GitHub Desktop.
Save morsine/4b4530ef5cf0df6f11d540d0b3c475b2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# this is an example python code to be used as an eventhandler for smstools (smsd) under linux
# script output will be logged along with smsd enteries under smsd.log
import sys
def extract_number(file_path):
phone_number = None
with open(file_path, 'r', errors='ignore', encoding='iso-8859-1') as file:
for line in file:
if line.startswith('From:'):
phone_number = line.split(':')[1].strip()
break
return phone_number
def extract_text(file_path):
text = ""
with open(file_path, 'r', errors='ignore', encoding='iso-8859-1') as file:
for i, line in enumerate(file):
if i >= 13: # Start from the 14th line (index 13)
text += line
return text
def main():
global file_path
global text
if len(sys.argv) < 2:
print("Usage: python script.py <func> <file location>")
sys.exit(1)
if sys.argv[1] == "RECEIVED":
file_path = sys.argv[2]
phone_number = extract_number(file_path)
text = extract_text(file_path)
print("Phone number:", phone_number)
print("Text:", text)
else:
print("not doing anything")
sys.exit(0)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment