Sample Program #3

Description
Logs in and watches for three contacts.


//Bone sample program #3
// A quick contacts demonstration

#include "bone.h"
#include <stdio.h>
#include "windows.h"

BONEHANDLE bhnd;

void callback_login(BONEHANDLE bhnd, int errorcode)
{
 
    if (errorcode == 0)
    {
        //lets setup our contact list
        if (bone_addcontact(bhnd, "ima"))
            printf("Unable to add contact (reason %d).\n", bone_getlasterror());
 
        if (bone_addcontact(bhnd, "faber"))
            printf("Unable to add contact (reason %d).\n", bone_getlasterror());
 
        if (bone_addcontact(bhnd, "invalidname"))
            printf("Unable to add contact (reason %d).\n", bone_getlasterror());
 
        if (bone_updatecontacts(bhnd))
            printf("Unable to send contacts (reason %d).\n", bone_getlasterror());
 
    }
    else
    {
        printf("Bone logon failed with code %d.\n", errorcode);
    }
}

void callback_badcontact(BONEHANDLE bhnd, const char *contact)
{
    if (contact)  printf("Username %s invalid.\n", contact);
}

void callback_contactisonline(BONEHANDLE bhnd, const char *contact, int onlinestatus)
{
    switch(onlinestatus)
    {
        case BONE_API_ISONLINE_USERPRESENT: printf("%s is online.\n", contact);  break;
        case BONE_API_ISONLINE_NOTPRESENT: printf("%s is offline.\n", contact);  break;
        case BONE_API_ISONLINE_ADMINPRESENT: printf("Admin %s is online.\n", contact);  break;
    }
}
 

int main(int argc, char* argv[])
{
    //Initialize the Bone SDK so we can use it
    if(bone_init(BONEBUILD))
    {
        printf("Unable to initialize the Bone SDK.\n");
    }
 
    // lets handle login results and incoming text here
    bone_setcallback(BONE_CALLBACK_LOGIN, callback_login);
    bone_setcallback(BONE_CALLBACK_BADCONTACT, callback_badcontact);
    bone_setcallback(BONE_CALLBACK_CONTACTISONLINE, callback_contactisonline);
 
    bhnd = bone_create();
    if (bhnd)
    {
        if (bone_login(bhnd, "testuser","pass"))
            printf("Login failed (reason %d).\n", bone_getlasterror());
    }
    else printf("Unable to create BONEHANDLE.\n");
 

    // the Bone SDK was made for Windows programs, which usually
    // have a windows message loop.
    // Console applications usually do not need one, but for Bone,
    // a message loop is required.
    // This message loop is needed in console applications only.
    // so here it comes:
    {
        MSG msg;
        while (GetMessage(&msg, NULL,0,0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
 
    return 0;
}