Merge pull request #1 from korayustundag/patch-1

Update checking.py [Optimization, Simplification and Readability.]
This commit is contained in:
Yusuf İpek
2023-10-03 21:51:17 +03:00
committed by GitHub
2 changed files with 110 additions and 66 deletions
+70 -1
View File
@@ -1 +1,70 @@
TODO # Dead Man's Switch Python Script
This Python script provides a Dead Man's Switch functionality that sends reminder and activation emails based on a specified time interval.
## Table of Contents
- [Prerequisites](#prerequisites)
- [Configuration](#configuration)
- [Customization](#customization)
- [Usage](#usage)
- [Contributors](#contributors)
- [License](#license)
## Prerequisites
Before using the script, you need to have the following prerequisites in place:
- Python 3 installed on your system.
- Access to an SMTP server with the necessary credentials.
- Necessary files:
- `checkin_file.txt`: A file to store the last check-in timestamp.
- `counter_file.txt`: A file to keep track of the number of sent emails.
## Configuration
You need to configure the script with your SMTP server settings and email details. Open the script and modify the following variables in the "Configuration" section:
- `smtp_host`: SMTP server hostname.
- `smtp_port`: SMTP server port.
- `smtp_user`: Your SMTP server username.
- `smtp_password`: Your SMTP server password.
- `from_email`: Your email address (the sender).
- `to_email`: Recipient's email address.
- `checkin_file`: Path to the check-in timestamp file.
- `counter_file`: Path to the email counter file.
- `DAYS_REMINDER`: Number of days for the reminder period.
- `DAYS_DEADMAN`: Number of days for the Dead Man's Switch activation.
- `SECONDS_IN_A_DAY`: Number of seconds in a day.
- `count_sent_mail`: Number of dead man mails to send after activation.
- `family_members`: List of family members' email addresses.
- `files_to_attach`: List of files to attach to the emails.
## Customization
You can customize the email content by modifying the following variables in the script:
- `reminder_subject`: Subject for the reminder email.
- `reminder_message`: Message for the reminder email.
- `dead_man_subject`: Subject for the Dead Man's Switch activation email.
- `dead_man_message`: Message for the Dead Man's Switch activation email.
## Usage
1. Ensure that you have met the prerequisites and configured the script as described above.
2. Run the script using Python:
```shell
python checking.py
```
3. The script will check if a check-in is required based on the specified time intervals. It will send reminder emails if necessary and activate the Dead Man's Switch if the activation time has passed.
4. The Dead Man's Switch activation email will be sent to the specified recipients with attached files.
## Contributors
- [Yusuf İpek](https://github.com/yusufipk)
- [Koray Üstündağ](https://github.com/korayustundag)
## License
This script is provided under the GPL-3.0 License. You are free to use, modify, and distribute it as needed. See the [LICENSE](https://github.com/yusufipk/dead-man-message/blob/master/LICENSE) file for details.
+40 -65
View File
@@ -5,55 +5,59 @@ from email.mime.text import MIMEText
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
from email import encoders from email import encoders
# Necessary files. You should create these. # Configuration
checkin_file = "./checkin_file.txt" # replace with your checkin file path smtp_host = 'your_smpt_server' # SMTP server hostname
counter_file = "./counter_file.txt" # this is needed to stop spamming mails to the loved ones smtp_port = 587 # SMTP server port
count_sent_mail = 7 smtp_user = 'username' # SMTP server username
smtp_password = 'password' # SMTP server password
from_email = 'coming_from' # Sender's email address
to_email = 'going_to' # Recipient's email address
checkin_file = "./checkin_file.txt" # File path for check-in timestamp
counter_file = "./counter_file.txt" # File path for email counter
DAYS_REMINDER = 25 # Days for the reminder period
DAYS_DEADMAN = 30 # Days for the dead man's switch
SECONDS_IN_A_DAY = 86400 # Number of seconds in a day
count_sent_mail = 7 # Number of dead man mails to send after activation.
DAYS_REMINDER = 25 # Modify these as per your need family_members = ['[email protected]'] # List of family members' email addresses
DAYS_DEADMAN = 30 # Modify these as per your need
SECONDS_IN_A_DAY = 86400 # No need to modify this
# Replace these with your SMTP settings and email # Email content
smtp_host = 'your_smpt_server' reminder_subject = "REMINDER" # Subject for the reminder email
smtp_port = 587 reminder_message = "Please check-in" # Message for the reminder email
smtp_user = 'username'
smtp_password = 'password'
# Reminder message dead_man_activation_subject = "Dead Man's Switch Activated" # Subject for the activation email
from_email = 'coming_from' dead_man_activation_message = "You forgot to check-in" # Message for the activation email
to_email = 'going_to'
#Dead man message dead_man_subject = "I Am Gone" # Subject for the dead man's switch email
family_members = ['[email protected]'] # You can specify multiple email ids separated by commas dead_man_message = "I'm dead" # Message for the dead man's switch email
message = "I'm dead" files_to_attach = ['file.gpg'] # List of files to attach
files_to_attach = ['file.gpg'] # specify your files
dead_man_subject = "I Am Gone"
def send_email(subject, message): # Function to send an email
# Setup smtp def send_email(subject, message, to, attachments=[]):
s = smtplib.SMTP(host=smtp_host, port=smtp_port) s = smtplib.SMTP(host=smtp_host, port=smtp_port)
s.starttls() s.starttls()
s.login(smtp_user, smtp_password) s.login(smtp_user, smtp_password)
# Create message
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = from_email msg['From'] = from_email
msg['To'] = to_email msg['To'] = to
msg['Subject'] = subject msg['Subject'] = subject
# Add in the message body
msg.attach(MIMEText(message, 'plain')) msg.attach(MIMEText(message, 'plain'))
# Send the email for file in attachments:
s.send_message(msg) with open(file, 'rb') as file_in:
del msg part = MIMEBase('application', 'octet-stream')
part.set_payload(file_in.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename={file}")
msg.attach(part)
s.send_message(msg)
s.quit() s.quit()
# Function to execute the dead man's switch
def execute_deadman_switch(): def execute_deadman_switch():
# check if counter file exists
try: try:
with open(counter_file, "r") as file: with open(counter_file, "r") as file:
counter = int(file.read()) counter = int(file.read())
@@ -61,38 +65,12 @@ def execute_deadman_switch():
counter = 0 counter = 0
if counter < count_sent_mail: if counter < count_sent_mail:
s = smtplib.SMTP(host=smtp_host, port=smtp_port) send_email(dead_man_subject, dead_man_message, ', '.join(family_members), files_to_attach)
s.starttls()
s.login(smtp_user, smtp_password)
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = ', '.join(family_members)
msg['Subject'] = dead_man_subject
msg.attach(MIMEText(message, 'plain'))
# Attach multiple files
for file in files_to_attach:
with open(file, 'rb') as file_in:
part = MIMEBase('application', 'octet-stream')
part.set_payload(file_in.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {file}")
msg.attach(part)
# Send the email
s.send_message(msg)
del msg
s.quit()
# increment counter and write to file
with open(counter_file, "w") as file: with open(counter_file, "w") as file:
file.write(str(counter + 1)) file.write(str(counter + 1))
# Function to check the dead man's switch
def check_deadman(): def check_deadman():
with open(checkin_file, "r") as file: with open(checkin_file, "r") as file:
last_checkin_time = float(file.read().strip()) last_checkin_time = float(file.read().strip())
@@ -100,14 +78,11 @@ def check_deadman():
time_difference = time.time() - last_checkin_time time_difference = time.time() - last_checkin_time
if time_difference > DAYS_REMINDER * SECONDS_IN_A_DAY: if time_difference > DAYS_REMINDER * SECONDS_IN_A_DAY:
if time_difference > DAYS_DEADMAN * SECONDS_IN_A_DAY: if time_difference > DAYS_DEADMAN * SECONDS_IN_A_DAY:
send_email("Dead Man's Switch Activated", "You forgot to check-in") send_email(dead_man_activation_subject, dead_man_activation_message, to_email)
execute_deadman_switch() execute_deadman_switch()
else: else:
send_email("REMINDER", "Please check-in") send_email(reminder_subject, reminder_message, to_email)
if __name__ == '__main__': if __name__ == '__main__':
check_deadman() check_deadman()