#include <QDebug>
#include <QFile>
+#include <QDateTime>
#include <libpurple/purple.h>
#include <cstdio>
/* End verbatim copied code */
/* noops for now, add features later */
-static PurpleConversationUiOps ribbon_conversation_callbacks = {
- NULL, /* create_conversation */
- NULL, /* destroy_conversation */
- NULL, /* write_chat */
- NULL, /* write_im */
- NULL, /* write_conv */
- NULL, /* chat_add_users */
- NULL, /* chat_rename_user */
- NULL, /* chat_remove_users */
- NULL, /* chat_update_user */
- NULL, /* present */
- NULL, /* has_focus */
- NULL, /* custom_smiley_add */
- NULL, /* custom_smiley_write */
- NULL, /* custom_smiley_close */
- NULL, /* send_confirm */
- NULL,
- NULL,
- NULL,
- NULL
-};
+//static PurpleConversationUiOps ribbon_conversation_callbacks = {
+// NULL, /* create_conversation */
+// NULL, /* destroy_conversation */
+// NULL, /* write_chat */
+// NULL, /* write_im */
+// NULL, /* write_conv */
+// NULL, /* chat_add_users */
+// NULL, /* chat_rename_user */
+// NULL, /* chat_remove_users */
+// NULL, /* chat_update_user */
+// NULL, /* present */
+// NULL, /* has_focus */
+// NULL, /* custom_smiley_add */
+// NULL, /* custom_smiley_write */
+// NULL, /* custom_smiley_close */
+// NULL, /* send_confirm */
+// NULL,
+// NULL,
+// NULL,
+// NULL
+//};
static void connect_progress(PurpleConnection *gc, const char* text, size_t step, size_t step_count)
{
NULL
};
-static void signed_on(PurpleConnection *gc)
+// TODO: note that the void* data can be set at purple_signal_connect time, and
+// set it to the class instance to allow for convenient upcalls
+// Then, actually do upcalls and emit the appropriate signals.
+static void signed_on(PurpleConnection *gc, void* data)
{
+ Q_UNUSED(data);
PurpleAccount *account = purple_connection_get_account(gc);
printf("Account connected: \"%s\" (%s)\n", purple_account_get_username(account), purple_account_get_protocol_id(account));
}
static void received_im_msg(PurpleAccount *account, char *sender, char *message,
- PurpleConversation *conv, PurpleMessageFlags flags)
+ PurpleConversation *conv, PurpleMessageFlags flags, void* data)
{
- Q_UNUSED(flags);
- if (conv==NULL) {
- conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, sender);
- }
- printf("(%s) %s (%s): %s\n", purple_utf8_strftime("%H:%M:%S", NULL), sender, purple_conversation_get_name(conv), message);
+ Q_UNUSED(data);
+ RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+ r->received_im_msg(account, sender, message, conv, flags);
+}
+
+static void buddy_signed_on(PurpleBuddy* buddy, void* data)
+{
+ Q_UNUSED(data);
+ qDebug() << purple_buddy_get_contact_alias(buddy) << "signed on";
}
-void connect_to_signals(void)
+static void connect_to_signals(void)
{
static int handle;
- purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
+ void *blist_handle = purple_blist_get_handle();
+ void *connections_handle = purple_connections_get_handle();
+ void *conversations_handle = purple_conversations_get_handle();
+
+ purple_signal_connect(connections_handle, "signed-on", &handle,
PURPLE_CALLBACK(signed_on), NULL);
- purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", &handle,
+ purple_signal_connect(conversations_handle, "received-im-msg", &handle,
PURPLE_CALLBACK(received_im_msg), NULL);
+ purple_signal_connect(blist_handle, "buddy-signed-on", &handle,
+ PURPLE_CALLBACK(buddy_signed_on), NULL);
+ //purple_signal_connect(
}
RibbonManager::RibbonManager(QObject* parent) : QObject(parent)
{
}
-void RibbonManager::init_libpurple(void)
+void RibbonManager::init(void)
{
/* optional:
purple_util_set_user_dir(CUSTOM_USER_DIRECTORY);
return purple_core_get_version();
}
+void RibbonManager::received_im_msg(PurpleAccount *account, char *sender, char *message,
+ PurpleConversation *conv, PurpleMessageFlags flags) {
+ Q_UNUSED(flags);
+ if (conv==NULL) {
+ conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, sender);
+ }
+ qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << sender << purple_conversation_get_name(conv) << message;
+}
+
void RibbonManager::sign_in_user(void)
{
QFile file("account.txt");
qDebug() << "Couldn't load account information from account.txt!";
return;
}
- QByteArray protocol = file.readLine().trimmed();
+ QByteArray protocol = file.readLine().trimmed();//this could be prpl-aim, prpl-yahoo, prpl-msn, prpl-icq, etc.
QByteArray username = file.readLine().trimmed();
QByteArray password = file.readLine().trimmed();
// Allocate and enable an account.
- // PurpleAccount *account = purple_account_new("YOUR_IM_ACCOUNTS_USERNAME_HERE", "prpl-IM_NETWORK_HERE"); //this could be prpl-aim, prpl-yahoo, prpl-msn, prpl-icq, etc.
PurpleAccount *account = purple_account_new(username.data(), protocol.data());
- // purple_account_set_password(account, "YOUR_IM_ACCOUNTS_PASSWORD_HERE");
+ // ui_data needed for upcalls
+ account->ui_data = this;
+
purple_account_set_password(account, password.data());
purple_accounts_add(account);
purple_account_set_enabled(account, UI_ID, TRUE);
--- /dev/null
+#include "ribbonserver.h"
+#include "ribbonsocket.h"
+
+#include <QtNetwork/QTcpServer>
+#include <QtNetwork/QTcpSocket>
+#include <QDebug>
+
+RibbonServer::RibbonServer(QObject* parent) :
+ QObject(parent)
+{
+ server = new QTcpServer(this);
+ QObject::connect(server, SIGNAL(newConnection()),
+ this, SLOT(onNewConnection()));
+}
+
+RibbonServer::~RibbonServer()
+{
+}
+
+bool RibbonServer::listen(const QHostAddress& address, quint16 port)
+{
+ _addr = address;
+ _port = port;
+ return server->listen(_addr, _port);
+}
+
+void RibbonServer::onNewConnection()
+{
+ QTcpSocket* sock = server->nextPendingConnection();
+ qDebug() << "Accepted connection from" << sock->peerAddress() << ":" << sock->peerPort();
+ RibbonSocket* rsock = new RibbonSocket(sock, this);
+ sockets.append(rsock);
+ if (sockets.length() >= 1) {
+ server->close();
+ qDebug() << "Server closed";
+ }
+ QObject::connect(rsock, SIGNAL(disconnected(RibbonSocket*)),
+ this, SLOT(onDisconnection(RibbonSocket*)));
+}
+
+void RibbonServer::onDisconnection(RibbonSocket* r)
+{
+ sockets.removeOne(r);
+ qDebug() << "Socket disconnected:" << r->s();
+ if (!server->isListening()) {
+ server->listen(_addr, _port);
+ qDebug() << "Server listening on" << _addr << ":" << _port;
+ }
+}