Project Description
A simple helper library and scripts that work with agsXMPP to allow you to send and receive messages via Jabber.

agsXmpp is a GPL .Net Xmpp library that is a project of AG Software -- they in no way are affiliated with PoshXmpp.

Here's a simple example ... don't forget you have to put both DLLs in the same place, and use InstallUtil PoshXmpp.dll and Add-PsSnapin PoshXmpp to get PoshXmpp loaded and ready
$jaykul = "Jaykul@im.Flosoft.biz"

# you'll be prompted ... and you'll have to enter a jabber login & password
# you could also pass in an HTTP polling server here, like: http://im.flosoft.biz:5280/http-poll/ 
PoshXmpp\New-Client (Get-Credential)
PoshXmpp\Send-Message $jaykul "Hi there Joel!"

# You'll be sitting, waiting for messages ... press any key to continue
PoshXmpp\Receive-Message -Loop

# This will make sure you're logged off ...
$PoshXmppClient.Close()


The $PoshXmppClient object is created by the New-Client cmdlet, which also returns it, so you can store it in a different variable in case you want to log into multiple servers. You can pass the -Client parameter to any of the commands, or they will use the default $PoshXmppClient client. If it becomes disconnected, you'll get an error, and can safely re-run New-Client.

Try this one, to listen to IRC (you need Out-Voice)

param (
    $JabberId = $( Read-Host "Bot's Jabber ID" )
   ,$Password = $( Read-Host "Bot's Password" -asSecure)
   ,$Chat[] = @("PowerShell%irc.FreeNode.net@irc.im.flosoft.biz") # Some IRC channels to join!
   ,$ChatNick = $("PowerBot$((new-object Random).Next(0,9999))")  # Your nickname in IRC
)

$global:PoshXmppClient = 
PoshXmpp\New-Client $JabberId $Password # http://im.flosoft.biz:5280/http-poll/
foreach($room in $Chat) {
   PoshXmpp\Connect-Chat $room $ChatNick
}

"PRESS ANY KEY TO STOP"
while(!$Host.UI.RawUI.KeyAvailable) {
   PoshXmpp\Receive-Message -All | foreach-object {
      "{0}-{1} said: {2}" -f ($_.From.User -split "%")[0], $_.From.Resource, $_.Body | Out-Voice -Wait -Pass
   }   
   [Threading.Thread]::Sleep( 100 )
}

PoshXmpp\Disconnect-Chat $Chat $ChatNick
$global:PoshXmppClient.Close();

Other cmdlets so far

  • New-Client -- create the initial connection -- errors if it can't log in
  • Add-Contact -- adds a contact, and/or accepts one who's added you
  • Get-Contact -- list or search your online contacts, takes a wildcard JabberId parameter
  • Send-Message -- takes the JabberId and a message (I'll fix this up later to take the message from the pipeline so you could use it to transfer text files)
  • Receive-Message -- read a message you've received. Has -All and -Loop parameters that let you read all messages you've received so far, or loop and wait for new messages.
  • Connect-Chat -- connect to MUC chats: try "PowerShell%irc.freenode.net@irc.im.flosoft.biz" to connect to the PowerShell IRC channel. NOTE: messages from these come the same way as regular chat messages... you have to Receive-Message to see anything. You could Receive-Message -Loop > log.txt ....
  • Disconnect-Chat -- disconnect from a MUC...
  • Start-Chat -- an attempt at an interactive chat on the command-line. Not very good.

Note that all of these cmdlets actually use methods exposed on the XmppClient object that the New-Client creates, or on it's members (particularly on the MessageManager, ContactManager, and ChatManager members). You can do even more than this using PowerShell script with those objects ...

Last edited Jan 12, 2008 at 2:59 PM by Jaykul, version 7