Skip to content

Commit

Permalink
v0.4.1 msmtp + ssmtp
Browse files Browse the repository at this point in the history
I decided to bring back `ssmtp`, together with msmtp. Older devices will only have ssmtp and even though they will probably never get updates, they could eventually get security updates, which would be valuable to know about. I am glad the msmtp implementation was already using different parameters. Cheers!
  • Loading branch information
tavinus authored Nov 9, 2022
1 parent 9724e85 commit a4aac6a
Showing 1 changed file with 58 additions and 14 deletions.
72 changes: 58 additions & 14 deletions opkg-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Gustavo Arnosti Neves
#
# Created: May / 2017
# Updated: Dec / 2018
# Updated: Nov / 2022
#
# Upgrade packages listed by:
# opkg list-upgradable
Expand All @@ -16,8 +16,9 @@


### Initialization
OPKGUPVERSION="0.4.0"
OPKGUPVERSION="0.4.1"
OPKGBIN="$(command -v opkg 2>/dev/null)"
SSMTPBIN="$(command -v ssmtp 2>/dev/null)"
MSMTPBIN="$(command -v msmtp 2>/dev/null)"
BANNERSTRING="Simple OPKG Updater v$OPKGUPVERSION"
TIMESTAMP="$(date '+%Y/%m/%d %H:%M:%S' 2>/dev/null)"
Expand All @@ -36,6 +37,7 @@ CHECK_UPDATES_FLAG=$TRUE
FORCE_FLAG=$FALSE
JUST_CHECK_FLAG=$FALSE
JUST_PRINT_FLAG=$FALSE
SSMTP_SEND_FLAG=$FALSE
MSMTP_SEND_FLAG=$FALSE
SEND_TO=""
ALWAYS_SEND_FLAG=$FALSE
Expand Down Expand Up @@ -82,7 +84,7 @@ main() {
upgrade_check # may exit here

local uplist="$(list_upgrades)"
if should_send_msmtp || just_print_html; then
if should_send_ssmtp || should_send_msmtp || just_print_html; then
if opkg_has_update || should_always_send || just_print_html; then
QUIET_MODE=$FALSE
local email_data=''
Expand All @@ -96,7 +98,11 @@ main() {
echo -e "$email_data"
exit 0
else
echo -e "$email_data" | "$MSMTPBIN" "$SEND_TO"
if should_send_ssmtp; then
echo -e "$email_data" | "$SSMTPBIN" "$SEND_TO"
elif should_send_msmtp; then
echo -e "$email_data" | "$MSMTPBIN" "$SEND_TO"
fi
exit $?
fi
fi
Expand Down Expand Up @@ -190,6 +196,8 @@ get_options() {
QUIET_MODE=$TRUE ; JUST_PRINT_FLAG=$TRUE ; shift ;;
-e|--email-list|--email-List|--emaillist|--emailList)
QUIET_MODE=$TRUE ; JUST_PRINT_HTML_FLAG=$TRUE ; shift ;;
-s|--ssmtp)
ssmtp_check "$2" ; shift ; shift ;;
-m|--msmtp)
msmtp_check "$2" ; shift ; shift ;;
-a|--always-send|--always-Send|--alwayssend|--alwaysSend)
Expand Down Expand Up @@ -260,6 +268,8 @@ Options:
-l, --list-upgrades Prints the list of available updates and exits
-e, --email-list Prints the list of updates in html email format
Includes subject, mime type and html formated data
-s, --ssmtp <email> Use the system's ssmtp to send update reports
You need to install and configure ssmtp beforehand
-m, --msmtp <email> Use the system's msmtp to send update reports
You need to install and configure msmtp beforehand
-a, --always-send Send e-mail even if there are no updates
Expand All @@ -273,16 +283,16 @@ Options:
Notes:
- Short options should not be grouped. You must pass each parameter on its own.
- You must have a working msmtp install to use the msmtp functionality. Make
sure you can send e-mails from it before trying from opkg-upgrade.
- You must have a working ssmtp or msmtp install to use the email functionality.
Make sure you can send e-mails from it before trying from opkg-upgrade.
Examples:
$OPKGUP_NAME -n -f # run without updating listings and asking for upgrade
$OPKGUP_NAME --install # install to /usr/sbin/opkg-upgrade
$OPKGUP_NAME -l # just print upgrades available
$OPKGUP_NAME -e # just print html formatted email
$OPKGUP_NAME -s '[email protected]' # mail upgrade report if have updates
$OPKGUP_NAME -a -s '[email protected]' # mail upgrade report even if NO updates
$OPKGUP_NAME -a -m '[email protected]' # mail upgrade report even if NO updates
$OPKGUP_NAME -u && echo 'upgrades are available' || echo 'no upgrades available'
"
Expand Down Expand Up @@ -475,6 +485,16 @@ is_valid_email() {
return $TRUE
}

# check if email is valid or break execution
validate_email() {
if ! is_valid_email "$1"; then
print_banner 'error'
print_error "Error! You need to specify a valid target e-mail address!"
print_error "Invalid address -> $1"
exit 30
fi
}

# returns $TRUE if it is a valid file, $FALSE otherwise
is_file() {
[ -f "$1" ] && return $TRUE
Expand Down Expand Up @@ -552,6 +572,11 @@ just_print() {
return $JUST_PRINT_FLAG
}

# returns $TRUE if we should send email with ssmtp, $FALSE otherwise
should_send_ssmtp() {
return $SSMTP_SEND_FLAG
}

# returns $TRUE if we should send email with msmtp, $FALSE otherwise
should_send_msmtp() {
return $MSMTP_SEND_FLAG
Expand All @@ -569,6 +594,30 @@ just_print_html() {



###### SSMTP functions

# Finds and checks for ssmtp executable, returns $TRUE if found, $FALSE otherwise
find_ssmtp() {
is_executable "$SSMTPBIN" && return $TRUE
SSMTPBIN='/usr/sbin/ssmtp'
is_executable "$SSMTPBIN" && return $TRUE
return $FALSE
}

# Checks for ssmtp program, validates the target email and sets globals for emails
ssmtp_check() {
if ! find_ssmtp; then
print_banner 'error'
print_error "Error! Could not find or run the SSMTP executable, make sure it is installed!"
exit 33
fi
validate_email "$1"
SSMTP_SEND_FLAG=$TRUE
SEND_TO="$1"
QUIET_MODE=$TRUE
}


###### MSMTP functions

# Finds and checks for msmtp executable, returns $TRUE if found, $FALSE otherwise
Expand All @@ -584,14 +633,9 @@ msmtp_check() {
if ! find_msmtp; then
print_banner 'error'
print_error "Error! Could not find or run the MSMTP executable, make sure it is installed!"
exit 30
fi
if ! is_valid_email "$1"; then
print_banner 'error'
print_error "Error! You need to specify a valid target e-mail address!"
print_error "Invalid address -> $1"
exit 30
exit 34
fi
validate_email "$1"
MSMTP_SEND_FLAG=$TRUE
SEND_TO="$1"
QUIET_MODE=$TRUE
Expand Down

0 comments on commit a4aac6a

Please sign in to comment.