'---------------------------------------------------------------------------- ' ' File Name: AvbBuddy.vb ' Copyright (c) 2007-2008, AOL LLC ' All rights reserved. ' Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ' Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. ' Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. ' Neither the name of the AOL LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ' LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ' A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR ' CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ' EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ' PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ' PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ' LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ' NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ' SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ' Open AIM Sample App for VB ' Adapted from AcshBuddy.cs using CSharpToVBConverter(1.2). ' CSharpToVBConverter by: Kamal Patel (http://www.KamalPatel.net) ' '---------------------------------------------------------------------------- Imports System Imports System.ComponentModel Imports System.Windows.Forms Imports System.Threading Imports System.Runtime.InteropServices Imports AccCoreLib Namespace avbbuddy Module avbbuddy Sub Main(ByVal args() As String) If args.Length <> 2 Then Console.WriteLine("usage: avbbuddy screenname password") Return End If Dim a As AvbApp = New AvbApp a.Run(args(0), args(1)) End Sub Class AvbApp Inherits Control '< PreserveSig = False)> _ Private Declare Function AccCreateSession Lib "acccore.dll" ( _ ByVal riid As Guid, _ ByRef session As Object _ ) As Integer Private s As AccSession Private transIdMap() As Integer = {0} Public Sub Run(ByVal username As String, ByVal password As String) Try ' create control to allow invokes CreateControl() ' create and init session Dim o As Object AccCreateSession(GetType(IAccSession).GUID, o) s = CType(o, AccSession) ' create our preference hook and then pass it ' to Open AIM Dim prefs As PrefsHook = New PrefsHook s.PrefsHook = prefs ' set up the listeners to AIM events. Visual Studio will help you with ' adding these functions by auto completing the functions. AddHandler s.OnStateChange, AddressOf s_OnStateChange AddHandler s.OnUserChange, AddressOf s_OnUserChange AddHandler s.OnSecondarySessionStateChange, AddressOf s_OnSecondarySessionStateChange AddHandler s.OnImReceived, AddressOf s_OnImReceived ' we need to set up a listener for getting logged into the expressions web page AddHandler s.OnRequestServiceResult, AddressOf s_OnRequestServiceResult ' here is where we put the name of our client and developer key. ' you will replace 'acshbuddy' with your client name and replace ' 'ju1yztKT86VJ0xj3' with your custom client key ' get your key at http://dev.aol.com/openaim s.ClientInfo.Property(AccClientInfoProp.AccClientInfoProp_Description) = "avbbuddy (key=ju1ggvPGo-RAdjC9)" 'set the user name on the session s.Identity = username 'sign on to AIM s.SignOn(password) ' start main loop Application.Run() Catch e As COMException Console.WriteLine(e.Message) End Try End Sub ' class to encapsulate console events Class InputEvent Inherits System.EventArgs Public Sub New(ByVal incommand As String) mycommand = incommand End Sub Public ReadOnly Property Command() As String Get Return mycommand End Get End Property Private ReadOnly mycommand As String End Class Private Delegate Sub InputHandler(ByVal o As Object, ByVal e As InputEvent) ' wait for a console command, but don't block the event loop Private Sub WaitForCommand() Dim mi As MethodInvoker = New MethodInvoker(AddressOf GetCommand) mi.BeginInvoke(Nothing, Nothing) End Sub Private Sub GetCommand() Dim command As String = Console.ReadLine() Dim e As InputEvent = New InputEvent(command) Dim list() As Object = {Me, e} BeginInvoke(New InputHandler(AddressOf HandleCommand), list) End Sub ' process a console command Private Sub HandleCommand(ByVal o As Object, ByVal e As InputEvent) Dim tokens() As String = e.Command.Split(" "c) Dim verb As String = tokens(0) Dim list As IAccBuddyList = s.BuddyList Select Case verb Case ":q" s.SignOff() Case "b:a" If tokens.Length <> 3 Then Console.WriteLine("Usage... b:a buddy group") Else Try Dim group As IAccGroup = list.GetGroupByName(tokens(2)) Try Dim buddy As IAccUser = group.InsertBuddy(tokens(1), -1) Console.WriteLine("ok, {0} has been added to {1}", buddy.Name, group.Name) Catch ex As System.Runtime.InteropServices.COMException Console.WriteLine("Sorry, that user could not be added to {0}, hr= {1}", group.Name, ex.ErrorCode) Console.WriteLine("Usage... b:a buddy group") End Try Catch ex As System.Runtime.InteropServices.COMException Console.WriteLine("That group does not exist, hr={0}.", ex.ErrorCode) Console.WriteLine("Usage... b:a buddy group") End Try End If Case "b:l" Dim lcount As Integer = list.GroupCount Dim i As Integer For i = 0 To lcount - 1 Dim group As IAccGroup = list.GetGroupByIndex(i) Console.WriteLine("{0}", group.Name) Dim gcount As Integer = group.BuddyCount Dim j As Integer For j = 0 To gcount - 1 Dim buddy As IAccUser = group.GetBuddyByIndex(j) Console.WriteLine(" {0}", buddy.Name) Next Next Case ":m" Dim ims As IAccImSession = s.CreateImSession(tokens(1), AccImSessionType.AccImSessionType_Im) Dim im As IAccIm = s.CreateIm(tokens(2), Nothing) ims.SendIm(im) Case "r:e" Try ' request a token for the expressions chooser web page and append &k=(devkey) to the URL dev key is the key you entered above Dim transId As Integer = s.RequestService("http://api.oscar.aol.com/aim/getExpressionsPage?f=html&language=en-us&k=ju1ggvPGo-RAdjC9", "") Dim i As Integer = transIdMap.Length transIdMap(0) = transId Catch ex As System.Runtime.InteropServices.COMException Console.WriteLine("There was an error getting the expressions page {0}.", ex.ErrorCode) End Try Case "s:p" If tokens.Length <> 3 Then Console.WriteLine("Usage... s:p key value") End If Try PrefsHook.m_prefsHook.SetValue(tokens(1), tokens(2)) Catch ex As System.Runtime.InteropServices.COMException Console.WriteLine("Setting pref {0} returned the following error {1}.", tokens(1), ex.ErrorCode) End Try Case "g:p" If tokens.Length <> 2 Then Console.WriteLine("Usage... g:p key") End If Try Dim val As String = CType(PrefsHook.m_prefsHook.GetValue(tokens(1)), String) Console.WriteLine("Getting pref {0} returned the following result {1}.", tokens(1), val) Catch ex As System.Runtime.InteropServices.COMException Console.WriteLine("Getting pref {0} returned the following error {1}.", tokens(1), ex.ErrorCode) End Try Case "o:v" Console.WriteLine("Client Version: avbbuddy 1.0") Case ":h" Console.WriteLine("b:a {buddy group} Add a buddy to group") Console.WriteLine("b:l List all buddies and groups in the buddy list.") Console.WriteLine(":m {buddy message} Send a message to specified buddy") Console.WriteLine("r:e Open the AIM Expressions Web Page by requesting an auth token to seemlessly log into Expression Web Page.") Console.WriteLine("s:p {spec value} Set a preference") Console.WriteLine("g:p {spec} Get a preference") Console.WriteLine("o:v Lists client version") Console.WriteLine(":q signs off of AIM and quits the test application") Case Else Console.WriteLine("Invalid command") End Select WaitForCommand() End Sub ' callbacks Private Sub s_OnStateChange(ByVal session As AccSession, ByVal state As AccSessionState, ByVal hr As AccResult) If state = AccSessionState.AccSessionState_Online Then Console.WriteLine("...Welcome to AOL Instant Messenger (SM)...") WaitForCommand() ElseIf state = AccSessionState.AccSessionState_Offline Then Application.Exit() End If End Sub Private Sub s_OnUserChange(ByVal session As AccSession, ByVal oldUser As IAccUser, ByVal newUser As IAccUser, ByVal prop As AccUserProp, ByVal hr As AccResult) Dim oldState = oldUser.Property(AccUserProp.AccUserProp_State) Dim newState = newUser.Property(AccUserProp.AccUserProp_State) If newState >= AccUserState.AccUserState_Online Then If oldState = AccUserState.AccUserState_Offline Then Console.WriteLine("** {0} has signed on.", newUser.Name) End If Else If oldState >= AccUserState.AccUserState_Online Then Console.WriteLine("** {0} has signed off.", oldUser.Name) End If End If End Sub Private Sub s_OnSecondarySessionStateChange(ByVal session As AccSession, ByVal piSecSession As IAccSecondarySession, ByVal State As AccSecondarySessionState, ByVal hr As AccResult) End Sub Private Sub s_OnImReceived(ByVal session As AccSession, ByVal piImSession As IAccImSession, ByVal piSender As IAccParticipant, ByVal piIm As IAccIm) Console.WriteLine("<*{0}*> {1}", piSender.Name, piIm.Text) End Sub Private Sub s_OnRequestServiceResult(ByVal session As AccSession, ByVal transId As Integer, ByVal hr As AccResult, ByVal host As String, ByVal port As Integer, ByVal cookie As Object) ' check the transId to make sure we made the request Dim i As Integer For Each i In transIdMap If transId = i Then ' we know we have our request, now load the URL in a new default browser ' window using the host param. System.Diagnostics.Process.Start(host) End If Next End Sub End Class End Module End Namespace