;======================================================================================================================================================= ;= Snarl.au3 udf-file by Matthias Kirchhoff on Dec.04 2015 = ;= modified from an article on = ;= https://www.autoitscript.com/forum/topic/127870-using-snarl-for-fancy-notifications-via-tcpip/ = ;= by StromyNP = ;= Thank you for the code examples = ;= = ;= To send a message to an snarl-target use the following command: = ;= = ;= _send_snarl(Target_IP, ; IP of the target computer where snarl is installed = ;= MessageClass (d,s or w), ; d = detailed notifications, s = summary notifications, w = warning notifications = ;= Title, ; Title of the snarl popup = ;= Text, ; Messagetext = ;= Timeout) ; Timeout for showing the message (10 is default) = ;= = ;= @error is '0' if message is sent and '-1' if an error occurs = ;= returns the komplete error-message if @error is -1 = ;======================================================================================================================================================= ;======================================================================================================================================================= ;= Functionname: _send_snarl = ;= Parameters: = ;= $ip, ; IP-address of target system where snarl is running = ;= $class, ; Message-Class (d = detailed, s = summary, w = warning = ;= $title, ; Title of snarl message = ;= $text, ; Messagetext = ;= $timeout, ; Timeout for showing the message (default is 10 seconds, 0 means sticky message) = ;= = ;= @error is '0' if message is sent and '-1' if an error occurs = ;= returns the komplete error-message if @error is -1 = ;======================================================================================================================================================= Func _send_snarl($ip, $class, $title, $text, $timeout=10) global $snarl_ip = "127.0.0.1" global $snarl_port = 9887 global $snarl_command_type = "type=SNP#?version=1.0#?" global $snarl_command_action_register = "action=register#?" global $snarl_command_action_add_class = "action=add_class#?" global $snarl_command_action_notification = "action=notification#?" global $snarl_command_action_unregister = "action=unregister#?" global $snarl_app = "app=AutoIt Script" global $snarl_socket = -1 Global $snarl_class_detail = "Detailed notifications" global $snarl_class_summary = "Summary notifications" global $snarl_class_warnings = "Warning notifications" If ConnectToSnarl($ip) <> 1 Then SetError(-1) Return "Canīt connect to target device" EndIf switch(StringLower($class)) case "d" $sendclass = $snarl_class_detail case "s" $sendclass = $snarl_class_summary case "w" $sendclass = $snarl_class_warnings Case Else SetError(-1) return "Unknown message-class (not d,s or w)" EndSwitch ; This would be more for an initial Setup with Snarl. You only need to run this once ; per application you want Snarl to know about. Otherwise, no need to keep running this. ;RegisterWithSnarl() ; Code to unregister (or you can just unregister app from Snarl's own App settings) ;UnRegisterWithSnarl() ; Let's test the messages and different message classes for the app! ; Note, a 0 for timeout makes the notification "Sticky" if SnarlMessage( $title, $text, $sendclass, $timeout ) = -1 Then SetError(-1) return "Error while sending the message" EndIf Disconnect() EndFunc ;======================================================================================================================================================= ;= Functionname: ConnectToSnarl = ;= Parameters: = ;= $target_ip ; IP of the target system where snarl is running = ;= Returns -1 if there is a connection error = ;======================================================================================================================================================= Func ConnectToSnarl($target_ip) $rc = TCPStartup() If $rc <> 1 Then Return -1 EndIf $snarl_socket = TCPConnect( $target_ip, $snarl_port ) If @error Then MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error) Return -1 EndIf Return 1 EndFunc ;======================================================================================================================================================= ;= Functionname: Disconnect = ;= Parameters: = ;= none = ;======================================================================================================================================================= Func Disconnect() $rc = TCPShutdown() EndFunc ;======================================================================================================================================================= ;= Functionname: SnarlMessage = ;= Parameters: = ;= $in_title, ; Title of the snarl message = ;= $in_text, ; Messagetext = ;= $in_classnum, ; Message-Class = ;= $in_timeout ; Timeout for showing the message = ;======================================================================================================================================================= Func SnarlMessage( $in_title, $in_text, $in_classnum, $in_timeout ) $msg = $snarl_command_action_notification & $snarl_app & "#?class=" & $in_classnum & "#?title=" & $in_title & "#?text=" & $in_text & "#?timeout=" & $in_timeout If SendToSnarl( $msg ) <> 1 Then Return -1 EndIf Sleep( 1500 ) EndFunc ;======================================================================================================================================================= ;= Functionname: SendToSnarl = ;= Parameters: = ;= $in_msg ; komplete commandline = ;======================================================================================================================================================= Func SendToSnarl( $in_msg ) TCPSend( $snarl_socket, $snarl_command_type & $in_msg & @CRLF ) If @error Then MsgBox( 4112, "Failure talking to Snarl", $in_msg ) Return -1 EndIf Return 1 EndFunc ;======================================================================================================================================================= ;= Functionname: RegisterWithSnarl = ;= Parameters: = ;= none = ;======================================================================================================================================================= Func RegisterWithSnarl() ; register the application so it shows in Snarl tray Apps settings ; I find the Sleep delay is necessary to give the Snarl registration a chance to keep up $msg = $snarl_command_action_register & $snarl_app If SendToSnarl( $msg ) <> 1 Then Return -1 EndIf Sleep( 1500 ) ; add additional classes for the app so user has control over what messages they see ; I just added 3 different categories $msg = $snarl_command_action_add_class & $snarl_app & "#?class=" & $snarl_class_detail If SendToSnarl( $msg ) <> 1 Then Return -1 EndIf Sleep( 1500 ) $msg = $snarl_command_action_add_class & $snarl_app & "#?class=" & $snarl_class_summary If SendToSnarl( $msg ) <> 1 Then Return -1 EndIf Sleep( 1500 ) $msg = $snarl_command_action_add_class & $snarl_app & "#?class=" & $snarl_class_warnings If SendToSnarl( $msg ) <> 1 Then Return -1 EndIf Sleep( 1500 ) Return 1 EndFunc ;======================================================================================================================================================= ;= Functionname: UnregisterWithSnarl = ;= Parameters: = ;= none = ;======================================================================================================================================================= Func UnregisterWithSnarl() $msg = $snarl_command_action_unregister & $snarl_app If SendToSnarl( $msg ) <> 1 Then Return -1 EndIf Return 1 EndFunc ;=======================================================================================================================================================