Skip to content

Latest commit

 

History

History
295 lines (239 loc) · 11.3 KB

README.md

File metadata and controls

295 lines (239 loc) · 11.3 KB

MFTP Scripts

mftp.py continuously monitors the CDC Noticeboard and forwards incoming notices to the configured email address, whether it's an individual account or a Google Group.

Table of Contents

Using docker

To set up a local instance of the application using docker, follow the steps below.

Prerequisites

The following requirements are to be satisfied for the project to function properly:

(back to top)

Installation

  1. Get the docker image

    You can get the docker image from either docker-hub or by buiilding it locally:

    • Pull from docker-hub
      sudo docker pull metakgporg/mftp
    • Build from Dockerfile
      • Clone the repository and cd into it
        git clone https://github.com/metakgp/mftp
        cd mftp/mftp
      • Build the image
        sudo docker build -t metakgporg/mftp .
  2. Create a directory which will contain your tokens and env.py, name it as mftp_config

  3. Follow the steps to configure mail sending. Skip this step if you wish to use method not involving mails, for example, ntfy

  4. Follow the steps to configure env variables

(back to top)

Usage

Docker Compose

It is mandatory to provide both of the following env variables before the docker-compose command.

  • MFTP_CONFIG: Absolute path to mftp_config directory
  • MFTP_MODE: Mode of sending mail - --smtp, --gmail-api and --ntfy
sudo MFTP_CONFIG=/path/to/mftp_config MFTP_MODE=--smtp docker-compose up -d # Using SMTP for sending mails
sudo MFTP_CONFIG=/path/to/mftp_config MFTP_MODE=--gmail-api docker-compose up -d # Using Gmail API for sending mails
sudo MFTP_CONFIG=/path/to/mftp_config MFTP_MODE=--ntfy docker-compose up -d # Using Gmail API for sending mails

(back to top)

Docker Command

It is mandatory to provide either of the following flags to the execution command.

  • --smtp: Using SMTP for sending mails

    sudo docker run -d \
    -v /path/to/mftp_config/env.py:/mftp/env.py \
    -v /path/to/mftp_config/token.json:/mftp/token.json \
    -v /path/to/mftp_config/credentials.json:/mftp/credentials.json \
    -v /path/to/mftp_config/mail_send_token.json:/mftp/mail_send_token.json \
    -v /path/to/mftp_config/mail_send_creds.json:/mftp/mail_send_creds.json \
    -v /path/to/mftp_config/.lsnif:/mftp/.lsnif \
    -v /path/to/mftp_config/.ntfy.lsnsf:/mftp/.ntfy.lsnsf \
    -v /path/to/mftp_config/.session:/mftp/.session \
    --restart=unless-stopped \
    --name mftp \
    metakgporg/mftp --smtp
  • --gmail-api: Using Gmail API for sending mails

    sudo docker run -d \
    -v /path/to/mftp_config/env.py:/mftp/env.py \
    -v /path/to/mftp_config/token.json:/mftp/token.json \
    -v /path/to/mftp_config/credentials.json:/mftp/credentials.json \
    -v /path/to/mftp_config/mail_send_token.json:/mftp/mail_send_token.json \
    -v /path/to/mftp_config/mail_send_creds.json:/mftp/mail_send_creds.json \
    -v /path/to/mftp_config/.lsnif:/mftp/.lsnif \
    -v /path/to/mftp_config/.ntfy.lsnsf:/mftp/.ntfy.lsnsf \
    -v /path/to/mftp_config/.session:/mftp/.session \
    --restart=unless-stopped \
    --name mftp \
    metakgporg/mftp --gmail-api
  • --ntfy: Using NTFY to serve notification

    sudo docker run -d \
    -v /path/to/mftp_config/env.py:/mftp/env.py \
    -v /path/to/mftp_config/token.json:/mftp/token.json \
    -v /path/to/mftp_config/credentials.json:/mftp/credentials.json \
    -v /path/to/mftp_config/mail_send_token.json:/mftp/mail_send_token.json \
    -v /path/to/mftp_config/mail_send_creds.json:/mftp/mail_send_creds.json \
    -v /path/to/mftp_config/.lsnif:/mftp/.lsnif \
    -v /path/to/mftp_config/.ntfy.lsnsf:/mftp/.ntfy.lsnsf \
    -v /path/to/mftp_config/.session:/mftp/.session \
    --restart=unless-stopped \
    --name mftp \
    metakgporg/mftp --ntfy

(back to top)

As a cronjob

It is also possible to run these docker containers as a cronjob:

  • With Docker Compose
    • Comment out the line restart: unless-stopped
    • Append --cron into the MFTP_MODE env variable. For example:
      • Using Cronjob to run container and SMTP to send mails
        sudo MFTP_CONFIG=/path/to/mftp_config MFTP_MODE="--smtp --cron" docker-compose up -d
      • Using Cronjob to run container and Gmail API to send mails
        sudo MFTP_CONFIG=/path/to/mftp_config MFTP_MODE="--gmail-api --cron" docker-compose up -d
      • Using Cronjob to run container and ntfy to send mails
        sudo MFTP_CONFIG=/path/to/mftp_config MFTP_MODE="--ntfy --cron" docker-compose up -d
  • With Docker Command
    • Remove --restart=unless-stopped flag & append --cron at the end of any of these commands. For example:
        sudo docker run -d \
        -v /path/to/mftp_config/env.py:/mftp/env.py \
        -v /path/to/mftp_config/token.json:/mftp/token.json \
        -v /path/to/mftp_config/credentials.json:/mftp/credentials.json \
        -v /path/to/mftp_config/mail_send_token.json:/mftp/mail_send_token.json \
        -v /path/to/mftp_config/mail_send_creds.json:/mftp/mail_send_creds.json \
        -v /path/to/mftp_config/.lsnif:/mftp/.lsnif \
        -v /path/to/mftp_config/.ntfy.lsnsf:/mftp/.ntfy.lsnsf \
        -v /path/to/mftp_config/.session:/mftp/.session \
        --name mftp \
        metakgporg/mftp --gmail-api --cron
  • Add the updated command with desired cron expression into your cronjob using crontab -e

(back to top)

Without using docker

To set up a local instance of the application without using docker, follow the steps below.

Prerequisites

The following requirements are to be satisfied for the project to function properly:

  • python3 >=python3.10
    sudo apt update
    sudo apt install python3
  • This project depends on ERP Login module by Arpit Bhardwaj for the ERP Login workflow. Read its documentation and setup your OTP fetching token mentioned in second point (OTP_CHECK_INTERVAL) of optional arguments.

(back to top)

Installation

Now that the environment has been set up and configured to properly compile and run the project, the next step is to download and configure the project locally on your system.

  1. Clone the repository
    git clone https://github.com/metakgp/mftp
    cd mftp/mftp
  2. Install required dependencies
    pip3 install -r requirements.txt
  3. Sending Emails

Note

Since, port 465 (SMTP with SSL) on campus LAN is blocked and if you want to host mftp on an internal server on the campus LAN, it will need another method then SMTP.
However, it is preferred to use SMTP when hosting on external server as SMTP is the convenient of the two.

The tool provides two methods of sending emails.

  • Using SMTP

    --smtp

    • Create an app password for the senders' email.
    • After creating app password use it as your FROM_EMAIL_PASS value in next step.
  • Using GMAIL API

    --gmail-api

    • Follow this quick start guide to configure gmail api on the senders' mail.
    • After successfull configuration of gmail api, you can leave the value of FROM_EMAIL_PASS as it is in the next step.
    • Save the generated token as mail_send_token.json
  1. Configuring environment variables

    • Copy env.example.py as env.py. It looks like this:
      # ERP Credentials (MUST)
      ROLL_NUMBER = "XXYYXXXXX" # Institute Roll Number
      PASSWORD = "**********" # ERP Password
      SECURITY_QUESTIONS_ANSWERS = { # ERP Secret Questions and their Answers
          "Q1" : "A1",
          "Q2" : "A2",
          "Q3" : "A3",
      }
      
      # EMAIL (via SMTP)
      ## Senders' Credentials
      FROM_EMAIL = "abc@gmail.com" # Notification Sender Email-id
      FROM_EMAIL_PASS = "**********" # App password for the above email-id
      ## EMAIL - Receiver's Address
      BCC_EMAIL_S = ["xyz@googlegroups.com", "abc@googlegroups.com"] # Multiple mails for bcc
      # BCC_EMAIL_S = ["xyz@googlegroups.com"] # This is how you can set single mail in a list
      
      # NTFY
      NTFY_BASE_URL = "https://ntfy.sh"
      NTFY_TOPIC = "mftp"
      ## Optional: only if you want a custom icon
      NTFY_TOPIC_ICON = "https://miro.medium.com/v2/resize:fit:600/1*O94LHxqfD_JGogOKyuBFgA.jpeg"
      ## Optional: only if the topic is restricted
      NTFY_USER = "testuser"
      NTFY_PASS = "fakepassword"
    • Update the values inside the double quotes ("). DO NOT CHANGE VAR NAMES.

(back to top)

Usage

It is mandatory to provide either of the following flags to the execution command:

  • --smtp: Using SMTP for sending mails

    python3 mftp.py --smtp
  • --gmail-api: Using GMAIL API for sending mails

    python3 mftp.py --gmail-api
  • --ntfy: Using NTFY for serving notifications

    python3 mftp.py --ntfy

It is also possible to bypass the continuous loop - which executes the code after every 2 minutes - using the --cron argument:

python3 mftp.py --smtp --cron
python3 mftp.py --gmail-api --cron
python3 mftp.py --ntfy --cron

(back to top)