-- search and replace "YourName@youremail.com" with your main email,  this will be the admin email for access
-- Search and Replace "YourNumber@yourmobilecomany.com" for SMS

using terms from application "iChat"
    global SMSAddress
    global administrator
    --------------------------------------------------------------------------------------------
    on received text invitation theText from theBuddy for TheChat
        set nomopsstatmsg to "Max will take your call"
        
        if status message begins with nomopsstatmsg then
            accept TheChat
            set buddyName to handle of theBuddy
            send "Hi," & buddyName & " this is Jude's MacBook Pro.  You can call me Maximillion." to TheChat
            delay 2
            send " Jude has put me in charge for now.  Would you like me to try to find him for you?" to TheChat
            delay 1
            send "I can send him an Email to his accounts, type 'email'" to TheChat
            send "He may be near by,  I can yell his name, type 'yell'" to TheChat
            send "I can also send him an SMS message, type 'SMS'" to TheChat
            send "Or type 'Exit' to end the chat" to TheChat
            --    send "or if its an emergency,  ill do everything, including paging his phone, type '911'" to TheChat
        else
            send "Im busy,  try back when my status changes" to TheChat
        end if
    end received text invitation
    
    --------------------------------------------------------------------------------------------
    on message received themessage from theBuddy for TheChat
        
        --
        
        set nomopsstatmsg to "Max will take your call"
        set OOO to "Out Of Order"
        set SMSBusyMsg to "Busy with SMS "
        set emailBusyMsg to "Busy with Email "
        set administrator to "YourName@youremail.com"
        set SMSAddress to "YourNumber@yourmobilecomany.com"
        
        --
        set validated to 0
        set exitflag to "FALSE"
        set buddyName to handle of theBuddy
        --
        if status message = SMSBusyMsg & buddyName & "" then
            if themessage begins with "Exit" then
                send "Cancelling SMS" to TheChat
                set status message to nomopsstatmsg
            else
                sendsms(buddyName, themessage)
                msgmenu(TheChat)
            end if
            
        end if
        --
        if status message = emailBusyMsg & buddyName & "" then
            sendmail(buddyName, themessage)
            msgmenu(TheChat)
        else if status message begins with emailBusyMsg then
            send "Looks like someone else may be sending me an email,  please try again when my status isnt busy" to TheChat
        end if
        
        -----------------------------------------------------------
        -- This is the beginning of the administrative area.  I specifically made it simple IF statements for each because I am paranoid about security.  What happens is that in order to get into the administrative menu,  you have to chat in from a specific chat account. So the "administrator" variable would be set to the handle of the account to which you would want to access as an administrator.--
        ----------------------------------------------------------------------------------
        --Here we enter the shutdown query,  so there is a confirmation that you want to shutdown the computer
        if (status message = OOO) and (buddyName = administrator) and (themessage = "Shutdown") then
            send "Are you sure you want to Shut Me Down?" to TheChat
            set status message to "ShutdownConfirm"
        end if
        --
        if (status message = "ShutdownConfirm") and (buddyName = administrator) and (themessage = "YES") then
            send "Ok,  I am shutting down,  talk to you later" to TheChat
            tell application "Finder" to shut down
            set status message to nomopsstatmsg
        else if (status message = "ShutdownConfirm") and (buddyName = administrator) and (themessage = "NO") then
            --set status message to OOO
            send "cancelling shutdown" to TheChat
            adminmenu(buddyName, themessage, TheChat)
        end if
        --  This statement calls a Automator app that I built which takes a picture using the computer camera and mails it to me
        if (status message = OOO) and (buddyName = administrator) and (themessage = "PIC") then
            send "Taking Picture" to TheChat
            activate application "Snapshot1.0"
            
        end if
        --  The "Cycle Email" command shuts down "Mail" and restarts it.  Sometimes it gets hung up
        if (status message = OOO) and (buddyName = administrator) and (themessage = "Cycle Email") then
            tell application "Mail"
                quit
            end tell
            delay 10
            tell application "Mail"
                activate
            end tell
            send "Ok,  Mail has been cycled" to TheChat
            
        end if
        --    the DiskUsage command outpus some disk status to the chat    
        if (status message = OOO) and (buddyName = administrator) and (themessage = "DiskUsage") then
            set output to do shell script "df"
            send "" & output & "" to TheChat
        end if
        --    the SystemInfo command dumps all of the "System_profiler" information to the chat,  this is a lot of information BTW.    
        if (status message = OOO) and (buddyName = administrator) and (themessage = "SystemInfo") then
            set themessage to do shell script "System_Profiler"
            sendmail(buddyName, themessage)
        end if
        --    The IP command sends the IP address of the computer to the chat
        if (status message = OOO) and (buddyName = administrator) and (themessage = "IP") then
            set output to do shell script "ipconfig getifaddr en1"
            send "" & output & "" to TheChat
            
        end if
        -- the End command gets you out of the admin menu and back to the nominal menu
        if (status message = OOO) and (buddyName = administrator) and (themessage = "END") then
            set status message to nomopsstatmsg
        end if
        -----------------------------------------------------------------------------------
        --This is the start of the nominal menu decsions
        if status message begins with nomopsstatmsg then
            if themessage = "email" then
                set status message to "Busy with email " & buddyName & ""
                send "Ok,  please enter a brief message and press enter" to TheChat
                --
            else if themessage = "yell" then
                say "hey Jude,, are you around?" & buddyName & " is looking for you on Ichat'"
                send "ok I just yelled for him,  we'll see if he shows up" to TheChat
                msgmenu(TheChat)
                --
            else if themessage = "SMS" then
                set validated to verifyuser(validated, buddyName)
                if validated = 1 then
                    send "ok,  please enter your text. Remember,  Jude gets charged per message,  type 'Exit' to cancel" to TheChat
                    set status message to "Busy with SMS " & buddyName & ""
                else
                    send "you are not authorized to send SMS" to TheChat
                    msgmenu(TheChat)
                end if
                --
            else if themessage begins with "Exit" then
                set exitflag to "TRUE"
                --
            else if themessage begins with "Admin" then
                adminmenu(buddyName, themessage, TheChat, administrator)
                
            else
                send "I dont understand what you said" to TheChat
                msgmenu(TheChat)
            end if
            --    
        else if status message = "Busy with email " & buddyName then
            set status message to nomopsstatmsg
        else if status message = "Busy with SMS " & buddyName then
            set status message to nomopsstatmsg
        end if
        --
        if exitflag = "TRUE" then
            --        set thewindow to front window
            --        close window (name of thewindow)
            close (1st window whose name contains full name of item 1 of (theBuddy)) --get participants of TheChat))
        end if
        
        
    end message received
    --------------------------------------------------------------------------------------------   
-- this is a list of users that can send SMS from the chat
    on verifyuser(validated, buddyName)
        if buddyName = "validatedperson1@email.com" then
            set validated to 1
        else if buddyName = "validatedperson1@email.com" then
            set validated to 1
        else if buddyName = "validatedperson1@email.com" then
            set validated to 1
        else if buddyName = "validatedperson1@email.com" then
            set validated to 1
        end if
        return validated
    end verifyuser
    
    --------------------------------------------------------------------------------------------    
    on sendmail(buddyName, themessage)
        tell application "Mail" to activate
        tell application "Mail"
            set theNewMessage to make new outgoing message with properties {subject:"" & buddyName & " sent you a message from ICHAT", content:"" & themessage & "", visible:false}
            tell theNewMessage
                make new to recipient at end of to recipients with properties {address:administrator}
                send
            end tell
            
        end tell
        
    end sendmail
    
    --------------------------------------------------------------------------------------------    
    on sendsms(buddyName, themessage)
        tell application "Mail" to activate
        tell application "Mail"
            set theNewMessage to make new outgoing message with properties {subject:"" & buddyName & "", content:"" & themessage & "", visible:false}
            tell theNewMessage
                make new to recipient at end of to recipients with properties {address:SMSAddress}
                send
            end tell
            
        end tell
        
    end sendsms
    
    -----------------------------------
    on msgmenu(TheChat)
        send "menu: 'email' 'SMS' 'Yell' 'Exit'" to TheChat
    end msgmenu
    ----------------------------------------------------------------
    on adminmenu(buddyName, themessage, TheChat)
        if buddyName = administrator then
            set status message to "Out Of Order"
            send "Menu: 'PIC' 'Shutdown' 'Cycle Email' 'SystemInfo' 'DiskUsage' 'IP' 'END'" to TheChat
        end if
    end adminmenu
    
end using terms from

--defaults write com.apple.ichatagent UserIdleTimeout x