Jabber Powershell

A user at PowerShellCommunity.org asked how to send a Jabber message using PowerShell.  There is a set of commercial cmdlets available from /n software that can do this (and a lot more), but they are a little pricey for the casual user.  As luck would have it, PowerShell makes it super easy to work with .NET stuff and I happened to know there was a few .NET libraries already in existence.  After reviewing what was available, I decided on Alex Gnauck’s excellent agsXMPP SDK.

It took a bit of work to translate the C# examples to PowerShell, but there was another guy at AG-Software forums who was working on this as well and he had an example I could work with.  Also, Alex was a great help in some troubleshooting.

Next step–a Jabber bot.  :)

So, without further ado, the code (script repository link):

   1: # Download XMPP SDK from http://www.ag-software.de/index.php?page=agsxmpp-sdk
   2: function Send-XmppMessage {
   3:     param (
   4:         $From = $( Throw "You must specify a Jabber ID for the sender." ),
   5:         $Password, # Leave blank to be prompted for password
   6:         $To = $( Throw "You must specify a Jabber ID for the recipient." ),
   7:         $Body = $( Throw "You must specify a body for the message." )
   8:     )
   9:     
  10:     # This function reads a string from the host while masking with *'s.
  11:     function Read-HostMasked( [string]$prompt="Password" ) {
  12:         $password = Read-Host -AsSecureString $prompt; 
  13:         $BSTR = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($password);
  14:         $password = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR);
  15:         [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR);
  16:         return $password;
  17:     }
  18:     # Set path accordingly.
  19:     $assemblyPath = $(resolve-path $profiledir\Assemblies\agsXMPP.dll)
  20:     [void][reflection.assembly]::LoadFrom( $assemblyPath )
  21:     $jidSender         = New-Object agsxmpp.jid( $From )
  22:     $jidReceiver     = New-Object agsxmpp.jid ( $To )
  23:     $xmppClient     = New-Object agsxmpp.XmppClientConnection( $jidSender.Server )
  24:     $Message         = New-Object agsXMPP.protocol.client.Message( $jidReceiver, $Body )
  25:     
  26:     # The following switches may assist in troubleshooting connection issues.
  27:     # If SSL and StartTLS are disabled, then you can use a network sniffer to inspect the XML
  28:     #$xmppClient.UseSSL                     = $FALSE
  29:     #$xmppClient.UseStartTLS                 = $FALSE
  30:     
  31:     # Since this function is only used to send a message, we don't care about doing the 
  32:     # normal discovery and requesting a roster.  Leave disabled to quicken the login period.
  33:     $xmppClient.AutoAgents                     = $FALSE
  34:     $xmppClient.AutoRoster                     = $FALSE
  35:     
  36:     # Use SRV lookups to determine correct XMPP server if different from the server
  37:     # portion of your JID.  e.g. user@gmail.com, the server is really talk.google.com
  38:     $xmppClient.AutoResolveConnectServer     = $TRUE
  39:     if ( !$password ) { $password = Read-HostMasked }
  40:     
  41:     # Open connection, then wait for it to be authenticated
  42:     $xmppClient.Open( $jidSender.User, $Password )
  43:         while ( !$xmppClient.Authenticated ) {
  44:             Write-Verbose $xmppClient.XmppConnectionState
  45:             Start-Sleep 1
  46:         }
  47:     # If server disconnects you, try enabling this
  48:     #$xmppClient.SendMyPresence()
  49:     $xmppClient.Send( $Message )
  50:     # Send is asynchronous, so we must wait a second before closing the connection
  51:     Start-Sleep 1
  52:     $xmppClient.Close()
  53: }

: http://halr9000.com/article/455

No comments yet.

  • Microblog

  • Recent Posts

  • Recent Comments

  • meta

  • PowerShell Blogroll