#!/bin/bash # This script will log into your OASIS account, download any new OCC ("official communication # channel") messages and also instruct the server to forward them to your email address. username="your-student-number" password="your-OASIS-password" email="email@address" dir=~/oasis_occ # Modify the following lines to execute suitable commands when login, download or forwarding errors # occur. alias login_error='kdialog --error "Could not log in to OASIS to retrieve OCC messages."' alias download_error='kdialog --error "OCC message could not be downloaded."' alias forward_error='kdialog --error "OCC message downloaded but could not be forwarded."' # ====== # The URL associated with the login button, and also the main OASIS page. main_url="https://portalb.curtin.edu.au/http://sm-portal.curtin.edu.au:8080/portal/dt?dt.isPortletRequest=true&dt.action=process&dt.provider=PortletWindowProcessChannel&dt.windowProvider.targetPortletChannel=portallogin_jsr168&dt.containerName=Welcome&dt.windowProvider.currentChannelMode=VIEW&dt.window.portletAction=ACTION" # This must be prepended to the OCC message URLs found in the main page. url_prefix="https://portalb.curtin.edu.au/redirect/" # The URL of the page where the user enters their email address, for message forwarding. forward_page_url="https://portalb.curtin.edu.au/http://sm-portal.curtin.edu.au:8903/oca/viewmessage.jsf" # The URL loaded when the user clicks the Send button. forward_button_url="https://portalb.curtin.edu.au/http://sm-portal.curtin.edu.au:8903/oca/forwardtoemail.jsf" mkdir -p "$dir" # Login and load the main OASIS page, and parse it to find the URLs of unread OCC messages. urls=$( wget --save-cookies "$dir/cookies.txt" \ --keep-session-cookies \ --post-data "Login.Token1=${username}&Login.Token2=${password}" \ --output-document - \ "$main_url" | \ sed -r "s/'(http[^']*)'/\n\1\n/" | \ grep '^http.*occId=' | \ uniq ) if [ $? -ne 0 ]; then login_error exit 1 elif [ -n $urls ]; then # Replace the @ in the email so that it can be embedded in POST data. email=${email/@/%40} for url in $urls; do # Pick a filename for the downloaded message. A more intelligent/useful technique might be # based on the sender and/or title of the message. This code just does it by date. filename=`date +%Y-%m-%d` i=1 while [ -e "$dir/$filename.html" ]; do filename=`date +%Y-%m-%d_$i` i=$(( i + 1 )) done message_file="$dir/$filename.html" # Retrieve and parse the message itself, in order to: # (a) save a copy, and # (b) get the "ViewState" parameter that we need in order to use the forward-to-email function. wget --load-cookies "$dir/cookies.txt" \ --save-cookies "$dir/cookies.txt" \ --keep-session-cookies \ --convert-links \ --span-hosts \ --output-document "$message_file" \ "$url_prefix$url" if [ $? -ne 0 ]; then download_error else # To forward an OCC message, we first need to load two URLs: # 1) the page where the user would normally enter their email address, and # 2) the URL associated with the "Send" button. # Both URLs must be supplied with a "ViewState" POST parameter, which (AFAIK) is a # serialised Java object used by the server to keep track of state. The first URL # cannot be skipped because the ViewState parameter required for the second can only be # obtained by loading the first. The ViewState parameter for the first can be extracted # from the original message page. function get_view_state() { # This function extracts and formats the "javax.faces.ViewState" POST parameter # from HTML input. sed -r 's/]*name="javax.faces.ViewState"[^>]*value="([^"]+)" *\/>/\nViewState \1\n/' | \ awk '/^ViewState/ { print gensub("=", "%3D", "g", $2); exit }' } # A note about the following two wget commands: they will appear to abort before # downloading is complete. This is because their output is piped into the commands sed, # awk and grep, which (in this case) will terminate when the necessary information # has been found. forward_page_view_state=$( cat "$message_file" | get_view_state ) forward_page_post_data="viewMailboxForm%3A_id65=Forward+to+email&viewMailboxForm_SUBMIT=1&viewMailboxForm%3A_idcl=&viewMailboxForm%3A_link_hidden_=&javax.faces.ViewState=${forward_page_view_state}" forward_button_view_state=$( wget --load-cookies "$dir/cookies.txt" \ --post-data "$forward_page_post_data" \ --output-document - \ "$forward_page_url" | \ get_view_state ) forward_button_post_data="forwardForm%3AforwardFormInit=true&forwardForm%3AemailAddress=${email}&forwardForm%3AformAction5=Send&forwardForm_SUBMIT=1&forwardForm%3A_link_hidden_=&forwardForm%3A_idcl=&javax.faces.ViewState=${forward_button_view_state}" wget --load-cookies "$dir/cookies.txt" \ --post-data "$forward_button_post_data" \ --output-document - \ "$forward_button_url" | \ grep -q "The Official Communication has been sent to the specified email address." if [ $? -ne 0 ]; then forward_error fi fi done fi