diff --git a/dot_scripts/mail.tpl b/dot_scripts/mail.tpl new file mode 100644 index 0000000..9902c2d --- /dev/null +++ b/dot_scripts/mail.tpl @@ -0,0 +1,24 @@ +From: "#FROM_NAME" <#FROM_MAIL> +To: "#TO_NAME" <#TO_MAIL> +Subject: Convert +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="MULTIPART-MIXED-BOUNDARY" + +--MULTIPART-MIXED-BOUNDARY +Content-Type: multipart/alternative; boundary="MULTIPART-ALTERNATE-BOUNDARY" + +--MULTIPART-ALTERNATE-BOUNDARY +Content-Type: text/plain; charset="UTF-8" + +Hello #TO_NAME + +--MULTIPART-ALTERNATE-BOUNDARY +Content-Type: text/html; charset="UTF-8" +Content-Transfer-Encoding: quoted-printable +
Hello #TO_NAME
+ +--MULTIPART-ALTERNATE-BOUNDARY-- +--MULTIPART-MIXED-BOUNDARY +Content-Type: application/octet-stream; name="#ATTACHMENT_NAME" +Content-Disposition: attachment; filename="#ATTACHMENT_NAME" +Content-Transfer-Encoding: base64 diff --git a/dot_scripts/sendmail.sh b/dot_scripts/sendmail.sh new file mode 100755 index 0000000..e058c0c --- /dev/null +++ b/dot_scripts/sendmail.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +source "$HOME/.scripts/secrets.sh" + +function file_to_mail() { + local source_file="$1" + local attachment_name=$(basename "$1") + local target_file="$source_file-encoded" + base64 "$source_file" >"$target_file" + local target_file_content + + target_file_content=$(\cat "$target_file") + + local mail_template="$HOME/Code/dotfiles/dot_scripts/mail.tpl" + + local mail_from="$PRINCIPAL_ACCOUNT_EMAIL" + local mail_to=${2:-"$mail_from"} + local name_from="$PRINCIPAL_ACCOUNT_FULLNAME" + local name_to="$name_from" + + \cat "$mail_template" | + sed "s/#FROM_NAME/$name_from/g" | + sed "s/#FROM_MAIL/$mail_from/g" | + sed "s/#TO_NAME/$name_to/g" | + sed "s/#TO_MAIL/$mail_to/g" | + sed "s/#ATTACHMENT_NAME/$attachment_name/g" >/tmp/mailwithattachement.txt + # sed "s/#ATTACHMENT_CONTENT/$target_file_content/g" >/tmp/mailwithattachement.txt + # sed "s/#ATTACHMENT_CONTENT/$target_file_content/g" >/tmp/mailwithattachement.txt + echo "\n$target_file_content\n" >>/tmp/mailwithattachement.txt + echo "\n--MULTIPART-MIXED-BOUNDARY--" >>/tmp/mailwithattachement.txt + + curl -v --ssl-reqd --url 'smtps://smtp.gmail.com:465' --user "$GMAIL_APP_USER:$GMAIL_APP_PASSWORD" --mail-from "$mail_from" --mail-rcpt "$mail_to" --upload-file /tmp/mailwithattachement.txt +} diff --git a/dot_zsh_functions b/dot_zsh_functions index ee55959..efa0dc9 100644 --- a/dot_zsh_functions +++ b/dot_zsh_functions @@ -179,6 +179,45 @@ function suggest_aliases() { done < <(top_commands_full "" "$search_input_size" | awk '{ $1=""; $2=""; $3=""; print}' | awk 'NF' | awk '{$1=$1};1' | awk -v COUNT=1 'NF>COUNT' H "-$search_input_size") } +# Download management +function file_to_kindle() { + local source_file="$1" + if [[ "$source_file" =~ .*.epub$ ]]; then + if epub2mobi "$source_file" 2>/dev/null; then + source_file=$(echo $source_file | sed 's/.epub$/.mobi/') + fi + fi + file_to_mail "$source_file" "$KINDLE_ACCOUNT_EMAIL" +} +function getabook() { + local searchterm="$1" + if searchbook "$searchterm"; then + echo "new downloaded file" + grepsearchterm=$(echo "$searchterm" | sed 's/ /.*/g') + if find ~/Downloads/Books -mmin -2 | grip "$grepsearchterm.*(epub|mobi|pdf)$"; then + local matching_file=$(find ~/Downloads/Books -mmin -2 | grip "$grepsearchterm.*(epub|mobi|pdf)$" | head -1) + local downloaded_file=$(basename "$matching_file") + cd "$HOME/Downloads/Books" &>/dev/null || exit + success "file downloaded in your Books folder : $downloaded_file" + if file_to_kindle "$downloaded_file" 2>/dev/null; then + success "file sent to your kindle, please review your emails to approve the transfer" + mailperso + else + error "could not send your file $downloaded_file to your email address, please check your folder" + open ~/Downloads/Books + fi + else + error "could not find a matching file in your download folder" + fi + else + error ":'( the automatic download failed for $1, does this book exists ? check yourself or retry" + browse "https://libgen.lc/search.php?req=$1" + fi +} + +# Mail management +source $HOME/Code/dotfiles/dot_scripts/sendmail.sh + # Sound management # Inspired by https://apple.stackexchange.com/a/213048/231885 for switching devices # and https://coderwall.com/p/22p0ja/set-get-osx-volume-mute-from-the-command-line for volume management