Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Requests registration of a new Mega account with user provided account name
- #(uses trashcanmail.com as mailer) and sets mega config file to new account
- #Constants
- myDefaultPass='password'
- myDefaultRealName='put.your.desired.account.name'
- myDomain='@trashcanmail.com'
- myLinkPref='https://trashcanmail.com'
- myMailboxPref='https://trashcanmail.com/en/mailbox/'
- randLen='24'
- interval='.5'
- timeout='60'
- maxTime='30'
- ident1=' '
- ident2=' '
- megarc=~/.megarc
- msetuprc=~/.megasetuprc
- #Set the input to a variable
- myInput="$1"
- #Create a default config file if one does not exist
- if [ ! -f $msetuprc ]
- then
- exec 3<> $msetuprc
- printf "$myDefaultPass\n$myDefaultRealName" >&3
- exec 3>&-
- fi
- #Read password and real name info from config file
- myPass="$(cat $msetuprc | cut -d$'\n' -f1)"
- myRealName="$(cat $msetuprc | cut -d$'\n' -f2)"
- #Check for special conditions "user does not enter parameter", "request help",
- #"request reconfigure", "print configuration"
- if [ -z $myInput ]
- then
- >&2 printf "Error: You must specify an account name\n"
- printf "For help specify -h\n"
- exit 1
- elif [ $myInput = '-c' ] || [ $myInput = '-C' ]
- then
- #Set up config file for new account
- if [ -f $msetuprc ]
- then
- rm $msetuprc
- fi
- exec 3<> $msetuprc
- printf "Please enter desired password: "
- read myPass
- printf "$myPass\n" >&3
- printf "Please enter desired \"real name\": "
- read myRealName
- printf "$myRealName" >&3
- exec 3>&-
- printf "Configuration completed successfully!\n"
- exit 0
- elif [ $myInput = '-p' ] || [ $myInput = '-P' ]
- then
- #Print current configured password and real name info
- printf "Current values used for account creation:\nPassword: $myPass\n\"Real Name\": $myRealName\n"
- exit 0
- elif [ $myInput = '-h' ] || [ $myInput = '-H' ] || [ $myInput = '--help' ] || [ $myInput = '--Help' ]
- then
- printf "\nName:\n\n$ident1 megasetup.sh (recommended alias - msetup)\n"
- printf "\nDescription:\n\n$ident1 Uses Megatools to register/verify a new Megashare account and configures\n$ident1 .megarc file for the new account. You will not need to include the -u\n$ident1 username -p password flags for Megatools programs to access your new\n$ident1 account after the script has finished executing unless .megarc is edited\n$ident1 again.\n"
- printf "\nUsage:\n\n$ident1 Specify a new account name (uses trashcanmail.com as mailer, do not\n$ident1 include domain or use spaces), all Megatools programs will be configured\n$ident1 for the new account. For ease of use the password and \"real name\" used to\n$ident1 create new accounts are saved inside configuration file ~/.megasetuprc.\n\n$ident1 Specify the -c flag to configure the script.\n$ident1 Specify the -p flag to print the current configuration.\n$ident1 Specify the -r flag to use a randomly generated account name.\n"
- printf "\n$ident1 Examples (with msetup alias set as recommended):\n\n"
- printf "$ident2 msetup <Account Name>$ident1 |$ident1 Create/Setup Account\n"
- printf "$ident2 msetup -c$ident2$ident1 |$ident1 Configure Script\n"
- printf "$ident2 msetup -p$ident2$ident1 |$ident1 Print Configuration\n"
- printf "$ident2 msetup -r$ident2$ident1 |$ident1 Random Account Name\n\n"
- exit 0
- fi
- #Exit with error if user enters an account name with whitespace
- if [ $# -gt 1 ] || [ "$myInput" != "${1%[[:space:]]*}" ]
- then
- >&2 printf "Error: Please specify an account name without spaces\n"
- printf "For help specify -h\n"
- exit 1
- fi
- #Set random account name if user requested
- if [ $myInput = '-r' ] || [ $myInput = '-R' ]
- then
- myInput="$(date +%s | sha256sum | base64 | head -c $randLen | cut -d' ' -f1)"
- printf "Using random account name \"$myInput\"\n"
- fi
- #Set email account name
- myEmail="$myInput$myDomain"
- #Request registration for new account
- myVerCommand=$(megareg --register --scripted --name $myRealName --email $myEmail --password $myPass)
- if [ "$?" -ne "0" ]
- then
- #If registration failed, exit with error
- >&2 printf "Megareg registration returned $?, account setup failed!\n"
- exit 1
- else
- #If registration succeeds print success message
- printf "Account registration request successful, awaiting verification email\n"
- fi
- #Gets address of confirmation email or delays until the email is available
- myMailAdr=""
- myCheckCounter="0"
- until [ -n "$myMailAdr" ]
- do
- sleep $interval
- myMailAdr="$(curl -sS "$myMailboxPref$myInput" | egrep "\/en\/mailbox\/(.+)mega\-email\-verification\-required\"" | cut -d\" -f2 -s | cut -d$'\n' -f1)"
- printf "."
- let "myCheckCounter=myCheckCounter+1"
- if [ "$myCheckCounter" -eq "$timeout" ]
- then
- printf "\nError: $maxTime second timeout reached - could not recieve verification link\n"
- exit 1
- fi
- done
- myMailAdr="$myLinkPref$myMailAdr"
- printf "\n"
- #Gets the actual required verification link from the confirmation email
- myVerLink="$(curl -sS "$myMailAdr" | egrep "https\:\/\/mega\.nz\/\#confirm(.+)\"" | cut -d\" -f2 -s | cut -d$'\n' -f1)"
- if [ "$?" -ne "0" ]
- then
- #If fetching verification URL fails, exit with error
- >&2 printf "Link extraction error, account setup failed!\n"
- exit 1
- else
- #Print verification link extraction success message
- printf "Email recieved and verification link extracted!\n"
- fi
- #Run the registration command with the given link
- eval "${myVerCommand/@LINK@/$myVerLink}"
- if [ "$?" -ne "0" ]
- then
- #If verificationx failed, exit with error
- >&2 printf "Megareg verification returned $?, account setup failed!\n"
- exit 1
- else
- #Otherwise, print login info for user
- printf "Username = $myEmail\n"
- printf "Password = $myPass\n"
- fi
- #Set up config file for new account
- if [ -f $megarc ]
- then
- rm $megarc
- fi
- exec 3<> $megarc
- printf "[Login]\n" >&3
- printf "Username = $myEmail\n" >&3
- printf "Password = $myPass\n" >&3
- exec 3>&-
- #Print completed message
- printf "Account creation and setup completed successfully!\n"
Add Comment
Please, Sign In to add comment