Sample Program #2

Description
Logs in, prints messages received, joins a channel, outputs a test message, and watches for a contact.


// Bone sample program #2

#include "bone.h"
#include <stdio.h>
#include "windows.h"
// windows.h is required since Bone requires the program
// to have a windows message loop (see below)

BONEHANDLE bhnd;

void CallbackSendText(BONEHANDLE bhnd, int errorcode, const char *target)
{
    printf("Text could not be sent to %s (reason %s).\n", target, bone_geterrortext(errorcode));
}

void CallbackChannelSub(BONEHANDLE bhnd, const char *channel, int errorcode)  // only called if subscribe or unsubscribe failed
{
    printf("Channel %s could not be subscribed: <%s>\n", channel, bone_geterrortext(errorcode));
}

void CallbackChannelAddUser(BONEHANDLE bhnd, const char *channel, const char *username) // these are caused by both by entering a channel and by others joining it. the own name is returned too
{
    printf("%s just appeared on %s\n", username, channel);
}

void CallbackChannelDelUser(BONEHANDLE bhnd, const char *channel, const char *username) // note that leaving a channel does not cause deluser to be send. the complete list invalidates implicitly. the own name is the last one to see until the next join
{
    printf("%s just left %s\n", username, channel);
}


void CallbackChannelText(BONEHANDLE bhnd, const char *origin, const char *channel, const char *message)
{
    // a text message was sent to us, just print it out on the screen
    printf("Message on %s from %s: %s\n", channel, origin, message);
}

void CallbackBadContact(BONEHANDLE bhnd, const char *contact)  // this is called once for each refused contact. a NULL pointers marks end of names
{
    if (contact) printf("Contact refused: %s\n", contact);
}

void CallbackContactIsOnline(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;
    }
}

// callback_login is called if a login attempt was successful or returned an error
void CallbackLogin(BONEHANDLE bhnd, int errorcode)
{
    // now that we are logged in, lets
    // do some bone stuff, such as joining channels or setting contacts
    if (errorcode == 0)
    {
        // the following message pingpongs back to us if we are the one being logged on as "testuser"
        bone_sendtext(bhnd, "testuser", "Welcome to the Bone Console Sample");
        bone_joinchannel(bhnd, "#SampleTester", 0, "");
        bone_sendtext(bhnd, "#SampleTester", "I just compiled and ran a Bone sample program. I do not know what i am doing, and I hit you all with a large trout.");
        bone_addcontact(bhnd, "Faber");
        bone_addcontact(bhnd, "Invalid");
        bone_updatecontacts(bhnd);
    }
    else
    {
        printf("Bone logon failed due to the following error: %s\n", bone_geterrortext(errorcode));
    }
}

// callback_text is called on an incoming text message
void CallbackText(BONEHANDLE bhnd, const char *origin, const char *message)
{
    // a text message was sent to us, just print it out on the screen
    printf("Message from %s: %s\n", origin, message);
}

int main(int argc, char* argv[])
{
    bone_init(BONEBUILD);
    // lets handle login results and incoming text here
    bone_setcallback(BONE_CALLBACK_LOGIN, CallbackLogin);
    bone_setcallback(BONE_CALLBACK_TEXT, CallbackText);
    bone_setcallback(BONE_CALLBACK_SENDTEXT,CallbackSendText);
    bone_setcallback(BONE_CALLBACK_CHANNELSUB,CallbackChannelSub);
    bone_setcallback(BONE_CALLBACK_CHANNELADDUSER,CallbackChannelAddUser);
    bone_setcallback(BONE_CALLBACK_CHANNELDELUSER,CallbackChannelDelUser);
    bone_setcallback(BONE_CALLBACK_BADCONTACT,CallbackBadContact);
    bone_setcallback(BONE_CALLBACK_CONTACTISONLINE,CallbackContactIsOnline);
    bone_setcallback(BONE_CALLBACK_CHANNELTEXT, CallbackChannelText);

    bhnd = bone_create();
    if (bhnd)
    {
        bone_login(bhnd, "testuser","pass");
    }
    else printf("Unable to create BONEHANDLE, error %s.\n", bone_geterrortext(bone_getlasterror()));
    // 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;
}