You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.5 KiB
C

3 years ago
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <stdlib.h>
#include <string.h>
#include <strophe.h>
#include <weechat/weechat-plugin.h>
3 years ago
#include "plugin.h"
#include "config.h"
3 years ago
#include "account.h"
#include "channel.h"
3 years ago
#include "connection.h"
3 years ago
3 years ago
void connection__init()
3 years ago
{
xmpp_initialize();
}
3 years ago
3 years ago
int version_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
{
xmpp_stanza_t *reply, *query, *name, *version, *text;
const char *ns;
3 years ago
struct t_account *account = (struct t_account *)userdata;
const char *weechat_name = "weechat";
const char *weechat_version = weechat_info_get("version", NULL);
3 years ago
weechat_printf(NULL, "Received version request from %s", xmpp_stanza_get_from(stanza));
reply = xmpp_stanza_reply(stanza);
xmpp_stanza_set_type(reply, "result");
3 years ago
query = xmpp_stanza_new(account->context);
3 years ago
xmpp_stanza_set_name(query, "query");
ns = xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza));
if (ns) {
xmpp_stanza_set_ns(query, ns);
}
3 years ago
name = xmpp_stanza_new(account->context);
3 years ago
xmpp_stanza_set_name(name, "name");
xmpp_stanza_add_child(query, name);
xmpp_stanza_release(name);
3 years ago
text = xmpp_stanza_new(account->context);
xmpp_stanza_set_text(text, weechat_name);
3 years ago
xmpp_stanza_add_child(name, text);
xmpp_stanza_release(text);
3 years ago
version = xmpp_stanza_new(account->context);
3 years ago
xmpp_stanza_set_name(version, "version");
3 years ago
xmpp_stanza_add_child(query, weechat_version);
3 years ago
xmpp_stanza_release(version);
3 years ago
text = xmpp_stanza_new(account->context);
xmpp_stanza_set_text(text, version);
3 years ago
xmpp_stanza_add_child(version, text);
xmpp_stanza_release(text);
xmpp_stanza_add_child(reply, query);
xmpp_stanza_release(query);
xmpp_send(conn, reply);
xmpp_stanza_release(reply);
3 years ago
if (version)
free (version);
3 years ago
return 1;
}
int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
{
3 years ago
struct t_account *account = (struct t_account *)userdata;
3 years ago
xmpp_stanza_t *body, *reply;
3 years ago
const char *type, *from, *from_jid;
3 years ago
char *intext, *replytext;
int quit = 0;
body = xmpp_stanza_get_child_by_name(stanza, "body");
if (body == NULL)
return 1;
type = xmpp_stanza_get_type(stanza);
if (type != NULL && strcmp(type, "error") == 0)
return 1;
3 years ago
from = xmpp_stanza_get_from(stanza);
from_jid = xmpp_jid_bare(account->context, from);
3 years ago
intext = xmpp_stanza_get_text(body);
3 years ago
struct t_channel *channel = channel__search(account, from_jid);
if (!channel)
channel = channel__new(account, CHANNEL_TYPE_PM, from_jid, from_jid);
weechat_printf(channel->buffer, "%s: %s", from_jid, intext);
3 years ago
reply = xmpp_stanza_reply(stanza);
if (xmpp_stanza_get_type(reply) == NULL)
xmpp_stanza_set_type(reply, "chat");
3 years ago
replytext = (char *)malloc(strlen(" received!") + strlen(intext) + 1);
strcpy(replytext, intext);
strcat(replytext, " received!");
xmpp_free(account->context, intext);
3 years ago
xmpp_message_set_body(reply, replytext);
xmpp_send(conn, reply);
xmpp_stanza_release(reply);
3 years ago
weechat_printf(channel->buffer, "%s: %s",
weechat_config_string(account->options[ACCOUNT_OPTION_JID]),
replytext);
3 years ago
free(replytext);
return 1;
}
3 years ago
void connection__handler(xmpp_conn_t *conn, xmpp_conn_event_t status,
3 years ago
int error, xmpp_stream_error_t *stream_error,
void *userdata)
{
3 years ago
struct t_account *account = (struct t_account *)userdata;
3 years ago
(void)error;
(void)stream_error;
if (status == XMPP_CONN_CONNECT) {
xmpp_stanza_t *pres;
3 years ago
weechat_printf(account->buffer, "DEBUG: connected");
3 years ago
xmpp_handler_add(conn, version_handler, "jabber:iq:version", "iq", NULL,
3 years ago
account);
xmpp_handler_add(conn, message_handler, NULL, "message", NULL, account);
3 years ago
/* Send initial <presence/> so that we appear online to contacts */
3 years ago
pres = xmpp_presence_new(account->context);
3 years ago
xmpp_send(conn, pres);
xmpp_stanza_release(pres);
} else {
3 years ago
weechat_printf(account->buffer, "DEBUG: disconnected");
//xmpp_stop(account->context);
3 years ago
}
3 years ago
}
3 years ago
int connection__connect(struct t_account *account, xmpp_conn_t **connection,
const char* jid, const char* password, int tls)
3 years ago
{
3 years ago
*connection = xmpp_conn_new(account->context);
3 years ago
xmpp_conn_set_jid(*connection, jid);
xmpp_conn_set_pass(*connection, password);
3 years ago
3 years ago
auto flags = xmpp_conn_get_flags(*connection);
switch (tls)
{
case 0:
flags |= XMPP_CONN_FLAG_DISABLE_TLS;
break;
case 1:
break;
case 2:
flags |= XMPP_CONN_FLAG_TRUST_TLS;
break;
default:
break;
}
xmpp_conn_set_flags(*connection, flags);
3 years ago
3 years ago
if (xmpp_connect_client(*connection, NULL, 0, &connection__handler, account)
3 years ago
!= XMPP_EOK)
{
weechat_printf(
NULL,
_("%s%s: error connecting to %s"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
jid);
return 0;
}
3 years ago
3 years ago
return 1;
3 years ago
}
3 years ago
void connection__process(xmpp_ctx_t *context, xmpp_conn_t *connection,
const unsigned long timeout)
3 years ago
{
3 years ago
if (connection)
3 years ago
{
3 years ago
xmpp_run_once(context ? context : xmpp_conn_get_context(connection),
timeout);
3 years ago
}
3 years ago
}