'
' Example script for 2BrightSparks SyncBackPro V8 that sends a Twitter message 
' after a profile runs (if the run is simulated then it does not).
'
' IMPORTANT, READ THE FOLLOWING:
'
' - THIS SCRIPT IS FOR REFERENCE ONLY. THIS SCRIPT NO LONGER WORKS.
' - You must first create an account at http://www.twitter.com/
'
' SBLang=Basic
'
' http://www.2BrightSparks.com/
'

'-----------------------------------------------------------------------------
'-DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING !-
'-----------------------------------------------------------------------------

'
' This is a profile configuration and runtime script 
'
Function Description(ByRef ScriptType)
  Description = "Send Twitter message after a profile run"
  ScriptType = SCRIPTTYPE_RUN + SCRIPTTYPE_CONFIG
End Function

'
' This routine is called after the profile has finished.
'
Sub RunProfileResult(ProfileResult, ErrMsg)
  Dim sUrl
  Dim sText
  Dim oXMLHTTP, sResult
  Dim SendTwitter

  ' Check to see if the profile has run
  If SBRunning.Simulated then
    Exit Sub
  End If

  ' Get profile result
  SendTwitter = True
  Select Case ProfileResult
  Case 0
    ' 0 = No result (ELR_None)
    SendTwitter = False
  Case 1
    ' 1 = Unknown profile (ELR_UnknownProfile)
    SendTwitter = False
  Case 2
    ' 2 = Profile is already running (ELR_AlreadyRunning)
    SendTwitter = False
  Case 3
    ' 3 = Profile has been imported and not run yet (ELR_Imported)
    SendTwitter = False
  Case 4
    ' 4 = Profile is running (ELR_Running)
    SendTwitter = False
  End Select

  ' Are we going to send a Twitter message?
  If SendTwitter = False then
    Exit Sub
  End If

  ' Build the Twitter message
  sText = "Profile " & SBRunning.Name & " finished on " & FormatDateTime(Now(), 0) & ":"
  Select Case ProfileResult
  Case 5
    sText = sText & "Internal error"
  Case 9
    sText = sText & "Restore aborted"
  Case 10
    sText = sText & "Restore failed"
  Case 11
    sText = sText & "Restore success"
  Case 12
    sText = sText & "Aborted"
  Case 13
    sText = sText & "Failed"
  Case 14
    sText = sText & "Success"
  Case 15
    sText = sText & "Network failure"
  Case 16
    sText = sText & "Scan failure"
  Case 17
    sText = sText & "Compare failure"
  Case 18
    sText = sText & "Run Before stopped profile"
  Case 19
    sText = sText & "Profile disabled"
  Case 20
    sText = sText & "SMART drive errors"
  Case 21
    sText = sText & "Could not email log"
  Case 22
    sText = sText & "Volume Shadow Copy snapshot failed"
  End Select

  ' The error message (ErrMsg) may make it too long so skip it

  ' Send Twitter message
  sUrl = "http://twitter.com/statuses/update.xml?status=" & Escape(sText)
  ' set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
  set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
  oXMLHTTP.open("POST", sUrl, false, CStr(SBVariables.GetProperty("TwitterUsername", "", FALSE)), CStr(SBVariables.GetProperty("TwitterPassword", "", FALSE)))
  oXMLHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  oXMLHTTP.send
  sResult = oXMLHTTP.responseText
  SBRunning.DebugOut("TwitterResult", sResult, 1)
  Set oXMLHTTP = nothing
End Sub

'
' Configuration part of the script
'

' Not for group profiles, and only display setup page if the profile
' is using the script.
Function ConfigWantSetupNode(IsGroup)
  ConfigWantSetupNode = not IsGroup and SBProfile.UsesScript
End Function

' Reset to factory defaults
Sub ConfigFactoryDefaults
  SBVariables.DeleteProperty("TwitterUsername")
  SBVariables.DeleteProperty("TwitterPassword")
End Sub

' Always allow the setup page to close
Function ConfigCanClose
  ConfigCanClose = TRUE
End Function

' Can revert to factory settings
Function ConfigCanRevert
  ConfigCanRevert = TRUE
End Function

' Setup node caption
Function ConfigNodeCaption
  ConfigNodeCaption = "Twitter Result"
End Function

' Create setup window display
Sub ConfigSetupDisplay
  SBProfile.AddLabel("1. You must first create an account at http://www.twitter.com/", 1)

  SBProfile.AddEdit("Twitter Username", 128, FALSE, FALSE, 2)
  SBProfile.AddEdit("Twitter Password", 128, FALSE, TRUE, 3)
End Sub

' Load settings. Return error message on failure.
Function ConfigLoadSettings
  ConfigLoadSettings = ""

  SBProfile.SetEdit(SBVariables.GetProperty("TwitterUsername", "", FALSE), 2)
  SBProfile.SetEdit(SBVariables.GetProperty("TwitterPassword", "", FALSE), 3)
End Function

' Save settings. Return error message on failure.
Function ConfigSaveSettings(Silent)
  ConfigSaveSettings = ""
 
  if SBProfile.GetEdit(2) = "" then
    ConfigSaveSettings = "The username cannot be empty!"
    Exit Function
  end if

  if SBProfile.GetEdit(3) = "" then
    ConfigSaveSettings = "The password cannot be empty!"
    Exit Function
  end if

  SBVariables.SetProperty("TwitterUsername", SBProfile.GetEdit(2))
  SBVariables.SetProperty("TwitterPassword", SBProfile.GetEdit(3))
End Function

' The settings have changed
Sub ConfigUpdateConditionals
End Sub
