Guest User

Untitled

a guest
May 16th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. #!/bin/bash
  2. #Requests registration of a new Mega account with user provided account name
  3. #(uses trashcanmail.com as mailer) and sets mega config file to new account
  4.  
  5. #Constants
  6. myDefaultPass='password'
  7. myDefaultRealName='put.your.desired.account.name'
  8. myDomain='@trashcanmail.com'
  9. myLinkPref='https://trashcanmail.com'
  10. myMailboxPref='https://trashcanmail.com/en/mailbox/'
  11. randLen='24'
  12. interval='.5'
  13. timeout='60'
  14. maxTime='30'
  15. ident1=' '
  16. ident2=' '
  17. megarc=~/.megarc
  18. msetuprc=~/.megasetuprc
  19.  
  20. #Set the input to a variable
  21. myInput="$1"
  22.  
  23. #Create a default config file if one does not exist
  24. if [ ! -f $msetuprc ]
  25. then
  26. exec 3<> $msetuprc
  27. printf "$myDefaultPass\n$myDefaultRealName" >&3
  28. exec 3>&-
  29. fi
  30.  
  31. #Read password and real name info from config file
  32. myPass="$(cat $msetuprc | cut -d$'\n' -f1)"
  33. myRealName="$(cat $msetuprc | cut -d$'\n' -f2)"
  34.  
  35. #Check for special conditions "user does not enter parameter", "request help",
  36. #"request reconfigure", "print configuration"
  37. if [ -z $myInput ]
  38. then
  39. >&2 printf "Error: You must specify an account name\n"
  40. printf "For help specify -h\n"
  41. exit 1
  42. elif [ $myInput = '-c' ] || [ $myInput = '-C' ]
  43. then
  44. #Set up config file for new account
  45. if [ -f $msetuprc ]
  46. then
  47. rm $msetuprc
  48. fi
  49. exec 3<> $msetuprc
  50. printf "Please enter desired password: "
  51. read myPass
  52. printf "$myPass\n" >&3
  53. printf "Please enter desired \"real name\": "
  54. read myRealName
  55. printf "$myRealName" >&3
  56. exec 3>&-
  57. printf "Configuration completed successfully!\n"
  58. exit 0
  59. elif [ $myInput = '-p' ] || [ $myInput = '-P' ]
  60. then
  61. #Print current configured password and real name info
  62. printf "Current values used for account creation:\nPassword: $myPass\n\"Real Name\": $myRealName\n"
  63. exit 0
  64. elif [ $myInput = '-h' ] || [ $myInput = '-H' ] || [ $myInput = '--help' ] || [ $myInput = '--Help' ]
  65. then
  66. printf "\nName:\n\n$ident1 megasetup.sh (recommended alias - msetup)\n"
  67. 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"
  68. 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"
  69. printf "\n$ident1 Examples (with msetup alias set as recommended):\n\n"
  70. printf "$ident2 msetup <Account Name>$ident1 |$ident1 Create/Setup Account\n"
  71. printf "$ident2 msetup -c$ident2$ident1 |$ident1 Configure Script\n"
  72. printf "$ident2 msetup -p$ident2$ident1 |$ident1 Print Configuration\n"
  73. printf "$ident2 msetup -r$ident2$ident1 |$ident1 Random Account Name\n\n"
  74. exit 0
  75. fi
  76.  
  77. #Exit with error if user enters an account name with whitespace
  78. if [ $# -gt 1 ] || [ "$myInput" != "${1%[[:space:]]*}" ]
  79. then
  80. >&2 printf "Error: Please specify an account name without spaces\n"
  81. printf "For help specify -h\n"
  82. exit 1
  83. fi
  84.  
  85. #Set random account name if user requested
  86. if [ $myInput = '-r' ] || [ $myInput = '-R' ]
  87. then
  88. myInput="$(date +%s | sha256sum | base64 | head -c $randLen | cut -d' ' -f1)"
  89. printf "Using random account name \"$myInput\"\n"
  90. fi
  91.  
  92. #Set email account name
  93. myEmail="$myInput$myDomain"
  94.  
  95. #Request registration for new account
  96. myVerCommand=$(megareg --register --scripted --name $myRealName --email $myEmail --password $myPass)
  97. if [ "$?" -ne "0" ]
  98. then
  99. #If registration failed, exit with error
  100. >&2 printf "Megareg registration returned $?, account setup failed!\n"
  101. exit 1
  102. else
  103. #If registration succeeds print success message
  104. printf "Account registration request successful, awaiting verification email\n"
  105. fi
  106.  
  107. #Gets address of confirmation email or delays until the email is available
  108. myMailAdr=""
  109. myCheckCounter="0"
  110. until [ -n "$myMailAdr" ]
  111. do
  112. sleep $interval
  113. myMailAdr="$(curl -sS "$myMailboxPref$myInput" | egrep "\/en\/mailbox\/(.+)mega\-email\-verification\-required\"" | cut -d\" -f2 -s | cut -d$'\n' -f1)"
  114. printf "."
  115. let "myCheckCounter=myCheckCounter+1"
  116. if [ "$myCheckCounter" -eq "$timeout" ]
  117. then
  118. printf "\nError: $maxTime second timeout reached - could not recieve verification link\n"
  119. exit 1
  120. fi
  121. done
  122. myMailAdr="$myLinkPref$myMailAdr"
  123. printf "\n"
  124.  
  125. #Gets the actual required verification link from the confirmation email
  126. myVerLink="$(curl -sS "$myMailAdr" | egrep "https\:\/\/mega\.nz\/\#confirm(.+)\"" | cut -d\" -f2 -s | cut -d$'\n' -f1)"
  127. if [ "$?" -ne "0" ]
  128. then
  129. #If fetching verification URL fails, exit with error
  130. >&2 printf "Link extraction error, account setup failed!\n"
  131. exit 1
  132. else
  133. #Print verification link extraction success message
  134. printf "Email recieved and verification link extracted!\n"
  135. fi
  136.  
  137. #Run the registration command with the given link
  138. eval "${myVerCommand/@LINK@/$myVerLink}"
  139. if [ "$?" -ne "0" ]
  140. then
  141. #If verificationx failed, exit with error
  142. >&2 printf "Megareg verification returned $?, account setup failed!\n"
  143. exit 1
  144. else
  145. #Otherwise, print login info for user
  146. printf "Username = $myEmail\n"
  147. printf "Password = $myPass\n"
  148. fi
  149.  
  150. #Set up config file for new account
  151. if [ -f $megarc ]
  152. then
  153. rm $megarc
  154. fi
  155. exec 3<> $megarc
  156. printf "[Login]\n" >&3
  157. printf "Username = $myEmail\n" >&3
  158. printf "Password = $myPass\n" >&3
  159. exec 3>&-
  160.  
  161. #Print completed message
  162. printf "Account creation and setup completed successfully!\n"
Add Comment
Please, Sign In to add comment