///---------------------------------------------------------------------------- /// /// 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. /// ///---------------------------------------------------------------------------- #include // printf #include "AccCore.h" // AIMCC main header #include "AccSupport.h" // AIMCC C++ helper classes #ifdef _MSC_VER #pragma comment(lib, "acccore.lib") // aimcc main lib #pragma comment(lib, "accuuidlib.lib") // aimcc uuid lib #endif using namespace AIMCC; const char* kCSCapability = "{39BB91C6-5134-4662-AFB3-188D42FD95AC}"; class CSampleApp : public CAccEventSink { public: CSampleApp() { m_customSess = NULL; m_initiate = false; } HRESULT Init(const char* userName, const char* password, char* initiate) { // 1. create aimcc main object, hook up for events // 2. set information to identify this client // 3. specify username and password, and sign on HRESULT hr; if (SUCCEEDED(hr = AccCreateSession(IID_IAccSession, (void**)&m_sp)) && SUCCEEDED(hr = Advise(m_sp))) { CAccPtr spClientInfo; hr = m_sp->get_ClientInfo(&spClientInfo); if (initiate) m_initiate = true; if (SUCCEEDED(hr)) { CAccVariant desc("accsample (key=ju13LC0KMdgmkiO0)"); spClientInfo->put_Property(AccClientInfoProp_Description, desc); if (SUCCEEDED(hr = m_sp->put_Identity(CAccBstr(userName)))) hr = m_sp->SignOn(CAccBstr(password)); } } return hr; } // start a custom session with the buddy void StartCustomSession(xp_str buddy) { // for demo purposes we only support one session at a time if (m_customSess == NULL) { printf("Start Custom Session"); CAccVariant val = 0; val.vt = VT_INT; val.intVal = 0; const CAccBstr uuid_CAccBstr("{39BB91C6-5134-4662-AFB3-188D42FD95AC}"); xp_str uuid1(uuid_CAccBstr); HRESULT hr = m_sp->CreateCustomSession(uuid1, &m_customSess); m_customSess->put_Property(AccCustomSessionProp_Mode, val); m_customSess->Invite(buddy, OLESTR("Join me in this custom session"), &m_transid); } else printf("Custom Session already exists..."); } // send a test message to the buddy void SendCustomMessage(xp_kstr msg) { // since this session is a message only session // we can just send an IM. CAccPtr im; HRESULT hr = m_sp->CreateIm(msg, OLESTR("text/plain"), &im); m_customSess->SendData(NULL, 0, im); } HRESULT Run() { // run a message loop until AccPostQuit is called return AccMessageLoop(); } void Term() { // clean up events and aimcc object Unadvise(m_sp); m_sp = NULL; } void OnUserChange( IAccSession* session, IAccUser* oldUser, IAccUser* newUser, AccUserProp Property, AccResult hr) { } void OnStateChange( IAccSession* piSession, AccSessionState state, AccResult hr) { // quit when we go offline printf("state change: state=%d, hr=%08X\n", state, hr); if (state == AccSessionState_Offline) AccPostQuit(hr); if (state == AccSessionState_Online) { HRESULT hr = piSession->AddCapabilities(CAccVariant(kCSCapability)); printf("AddCapabilities, hr=%08X\n", hr); } if (state == AccSessionState_Online && m_initiate) { char command[255]; char buddy[255]; do { printf("Enter c:s to initiate a custom session with a buddy\n"); char buf[255]; char* x = fgets(buf, 255, stdin); printf("Command entered = %s\n", buf); sscanf(buf, "%s %s", command, buddy); printf("command = %s buddy = %s\n", command, buddy); if (!strcmp(command, "c:s")) { printf("StartCustomSession with %s\n", buddy); StartCustomSession(CAccBstr(buddy)); break; } } while (1); } } void OnNewSecondarySession(IAccSession* piSession, IAccSecondarySession* piSecSession, xp_int val) { printf("OnNewSecondarySession\n"); CAccPtr spiCsSession(piSecSession); m_spiCsSession = spiCsSession; } void OnSecondarySessionStateChange( IAccSession* piSession, IAccSecondarySession* piSecSession, AccSecondarySessionState state, AccResult hr) { printf("OnSecondarySessionStateChange %d\n", state); // always accept incoming IM sessions if (state == AccSecondarySessionState_ReceivedProposal) { CAccVariant v; piSecSession->get_Property(AccSecondarySessionProp_RemoteUserName, &v); AccSecondarySessionServiceId id; piSecSession->get_ServiceId(&id); CAccBstr x = CAccBstr(v.bstrVal); char tmp[255]; x.GetUtf8(tmp, 255); printf("{%s} has sent you a custom session proposal\n", tmp); printf("We are auto accepting the proposal for testing purposes...\n"); piSecSession->Accept(); } if (state == AccSecondarySessionState_Online) { printf("The secondary session is online we can now send/receive data.\n"); char command[255]; char message[255]; do { printf("Enter c:m to send a custom message\n"); char buf[255]; char* x = fgets(buf, 255, stdin); printf("Command entered = %s\n", buf); sscanf(buf, "%s %s", command, message); printf("command = %s message = %s\n", command, message); if (!strcmp(command, "c:m")) { printf("SendCustomMessage %s\n", message); SendCustomMessage(CAccBstr(message)); break; } } while (1); //if (m_initiate) //{ // SendCustomMessage(OLESTR("Test sending a custom message")); //} } } void OnImReceived( IAccSession* piSession, IAccImSession* piImSession, IAccParticipant* piSender, IAccIm* piIm) { // signoff when we get an IM that says "quit" CAccBstr text; piIm->GetConvertedText(OLESTR("text/plain"), &text); if (text == OLESTR("quit")) m_sp->SignOff(); } void OnCustomSendResult(IAccSession* session, IAccCustomSession* customSession, IAccParticipant* recipient, IAccIm* im, AccResult hr) { printf("Acustomsession sent data with result, hr={0}.\n", hr); } void OnCustomDataReceived(IAccSession* session, IAccCustomSession* customSession, IAccParticipant* sender, IAccIm* Data) { xp_str Name; HRESULT hr = sender->get_Name(&Name); xp_str text; hr = Data->get_Text(&text); printf("ACustomSession received data...\n"); CAccBstr x = CAccBstr(Name); char tmp[255]; x.GetUtf8(tmp, 255); x = CAccBstr(text); char tmp1[255]; x.GetUtf8(tmp1, 255); printf("{%s} sent the following data, {%s}.\n", tmp, tmp1); } void OnInviteResult(IAccSession* session, IAccSecondarySession* secondarySession, xp_kstr userName, AccTransId transId, AccResult hr) { CAccBstr x = CAccBstr(userName); char tmp[255]; x.GetUtf8(tmp, 255); if (hr != 0) { printf("Inviting {%s} to the custom session has failed, hr={%d}\n", tmp, hr); } else printf("Inviting {%s} to the custom session has succeeded, hr={%d}\n", tmp, hr); } void OnParticipantJoined(IAccSession* session, IAccSecondarySession* secondarySession, IAccParticipant* participant) { xp_str name; HRESULT hr = participant->get_Name(&name); CAccBstr x = CAccBstr(name); char tmp[255]; x.GetUtf8(tmp, 255); printf("{%s} has joined the custom session.\n", tmp); } void OnParticipantLeft(IAccSession* session, IAccSecondarySession* secondarySession, IAccParticipant* participant, AccResult method, xp_kstr by, xp_kstr reason) { xp_str name; HRESULT hr = participant->get_Name(&name); CAccBstr x = CAccBstr(name); char tmp[255]; x.GetUtf8(tmp, 255); printf("{%s} has left the custom session. They left with result {%d}, by {%s}, because {%s}", tmp, method, by, reason); } private: CAccPtr m_sp; CAccPtr m_customSess; xp_int m_transid; bool m_initiate; //xp_unichar* m_buddy; CAccPtr m_spiCsSession; }; int main(int argc, char* argv[]) { // check args if (argc < 3) { printf("usage: accsample username password initiateCSflag=true/false\n"); return -1; } // create the app object and sign on CAccPtr sp(new CSampleApp); HRESULT hr = (sp) ? sp->Init(argv[1], argv[2], argv[3]) : E_OUTOFMEMORY; if (FAILED(hr)) { printf("initialization error, hr=%08X\n", hr); return (int)hr; } // run the message loop hr = sp->Run(); sp->Term(); return (int)hr; }