PoshCode Archive  Artifact Content

Artifact ae5c11c6191c774922d755475d9a08100a81c0d77283cd4fddbf1a2f21fd05f0:

  • File Send-XmppMessage.ps1 — part of check-in [0241e535a5] at 2018-06-10 12:56:17 on branch trunk — Sends an XMPP (Jabber) instant message. These parameters are mandatory: From, To, Body. Password can be supplied on the command line, or if omitted, you will be prompted for it. (user: halr9000 size: 2927)

# encoding: ascii
# api: powershell
# title: Send-XmppMessage
# description: Sends an XMPP (Jabber) instant message.  These parameters are mandatory: From, To, Body.  Password can be supplied on the command line, or if omitted, you will be prompted for it.
# version: 0.1
# type: function
# author: halr9000
# license: CC0
# function: Send-XmppMessage
# x-poshcode-id: 103
# x-archived: 2017-02-21T07:23:35
# x-published: 2008-01-07T13:42:00
#
#
# Download XMPP SDK from http://www.ag-software.de/index.php?page=agsxmpp-sdk
function Send-XmppMessage {
	param (
		$From = $( Throw "You must specify a Jabber ID for the sender." ),
		$Password, # Leave blank to be prompted for password
		$To = $( Throw "You must specify a Jabber ID for the recipient." ),
		$Body = $( Throw "You must specify a body for the message." )
	)
	
	# This function reads a string from the host while masking with *'s.
	function Read-HostMasked( [string]$prompt="Password" ) {
		$password = Read-Host -AsSecureString $prompt; 
		$BSTR = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($password);
		$password = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR);
		[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR);
		return $password;
	}
	# Set path accordingly.
	$assemblyPath = $(resolve-path $profiledir\Assemblies\agsXMPP.dll)
	[void][reflection.assembly]::LoadFrom( $assemblyPath )
	$jidSender 		= New-Object agsxmpp.jid( $From )
	$jidReceiver 	= New-Object agsxmpp.jid ( $To )
	$xmppClient 	= New-Object agsxmpp.XmppClientConnection( $jidSender.Server )
	$Message 		= New-Object agsXMPP.protocol.client.Message( $jidReceiver, $Body )
	
	# The following switches may assist in troubleshooting connection issues.
	# If SSL and StartTLS are disabled, then you can use a network sniffer to inspect the XML
	#$xmppClient.UseSSL 					= $FALSE
	#$xmppClient.UseStartTLS 				= $FALSE
	
	# Since this function is only used to send a message, we don't care about doing the 
	# normal discovery and requesting a roster.  Leave disabled to quicken the login period.
	$xmppClient.AutoAgents 					= $FALSE
	$xmppClient.AutoRoster 					= $FALSE
	
	# Use SRV lookups to determine correct XMPP server if different from the server
	# portion of your JID.  e.g. user@gmail.com, the server is really talk.google.com
	$xmppClient.AutoResolveConnectServer 	= $TRUE
	if ( !$password ) { $password = Read-HostMasked }
	
	# Open connection, then wait for it to be authenticated
	$xmppClient.Open( $jidSender.User, $Password )
		while ( !$xmppClient.Authenticated ) {
			Write-Verbose $xmppClient.XmppConnectionState
			Start-Sleep 1
		}
	# If server disconnects you, try enabling this
	#$xmppClient.SendMyPresence()
	$xmppClient.Send( $Message )
	# Send is asynchronous, so we must wait a second before closing the connection
	Start-Sleep 1
	$xmppClient.Close()
}