sexps in /xml

master
bqv 3 years ago
parent efffd451fa
commit 40af811d42
No known key found for this signature in database
GPG Key ID: 9E2FF3BDEBDFC910

@ -282,7 +282,7 @@ struct t_channel *channel__new(struct t_account *account,
enum t_channel_type type, enum t_channel_type type,
const char *id, const char *name) const char *id, const char *name)
{ {
struct t_channel *new_channel, *ptr_channel; struct t_channel *new_channel, *ptr_channel, *muc_channel;
struct t_gui_buffer *ptr_buffer; struct t_gui_buffer *ptr_buffer;
struct t_hook *typing_timer, *self_typing_timer; struct t_hook *typing_timer, *self_typing_timer;
@ -300,10 +300,10 @@ struct t_channel *channel__new(struct t_account *account,
return NULL; return NULL;
else if (type == CHANNEL_TYPE_PM) else if (type == CHANNEL_TYPE_PM)
{ {
ptr_channel = channel__search(account, jid(account->context, id).bare.data()); muc_channel = channel__search(account, jid(account->context, id).bare.data());
if (ptr_channel) if (muc_channel)
{ {
weechat_buffer_merge(ptr_buffer, ptr_channel->buffer); weechat_buffer_merge(ptr_buffer, muc_channel->buffer);
} }
} }
@ -893,11 +893,7 @@ void channel__update_topic(struct t_channel *channel,
void channel__update_name(struct t_channel *channel, void channel__update_name(struct t_channel *channel,
const char* name) const char* name)
{ {
if (channel->name) if (name)
free(channel->name);
channel->name = (name) ? strdup(name) : NULL;
if (channel->name)
weechat_buffer_set(channel->buffer, "short_name", name); weechat_buffer_set(channel->buffer, "short_name", name);
else else
weechat_buffer_set(channel->buffer, "short_name", ""); weechat_buffer_set(channel->buffer, "short_name", "");
@ -1149,6 +1145,23 @@ int channel__send_message(struct t_account *account, struct t_channel *channel,
{ {
std::string url { &*match[0].first, static_cast<size_t>(match[0].length()) }; std::string url { &*match[0].first, static_cast<size_t>(match[0].length()) };
//struct t_hashtable *options = weechat_hashtable_new (8,
// WEECHAT_HASHTABLE_STRING,
// WEECHAT_HASHTABLE_STRING,
// NULL,
// NULL);
//if (!options) { return; }
//weechat_hashtable_set(options, "nobody", "1");
//auto command = "url:" + url;
//const int timeout = 30000;
//struct t_hook *process_hook =
// weechat_hook_process_hashtable(command, options, timeout,
// int (*callback)(const void *pointer, void *data,
// const char *command,
// int return_code, const char *out, const char *err),
// const void *callback_pointer, void *callback_data);
//weechat_hashtable_free(options);
xmpp_stanza_t *message__x = xmpp_stanza_new(account->context); xmpp_stanza_t *message__x = xmpp_stanza_new(account->context);
xmpp_stanza_set_name(message__x, "x"); xmpp_stanza_set_name(message__x, "x");
xmpp_stanza_set_ns(message__x, "jabber:x:oob"); xmpp_stanza_set_ns(message__x, "jabber:x:oob");

@ -8,6 +8,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <iostream>
#include <sstream>
#include <weechat/weechat-plugin.h> #include <weechat/weechat-plugin.h>
#include "plugin.hh" #include "plugin.hh"
@ -17,6 +19,7 @@
#include "buffer.hh" #include "buffer.hh"
#include "message.hh" #include "message.hh"
#include "command.hh" #include "command.hh"
#include "sexp/driver.hh"
#define MAM_DEFAULT_DAYS 2 #define MAM_DEFAULT_DAYS 2
#define STR(X) #X #define STR(X) #X
@ -901,6 +904,29 @@ int command__xml(const void *pointer, void *data,
} }
if (argc > 1) if (argc > 1)
{
auto parse = [&](sexp::driver& sxml) {
std::stringstream ss;
std::string line;
try {
return sxml.parse(argv_eol[1], &ss);
}
catch (const std::invalid_argument& ex) {
while (std::getline(ss, line))
weechat_printf(nullptr, "%ssxml: %s", weechat_prefix("info"), line.data());
weechat_printf(nullptr, "%ssxml: %s", weechat_prefix("error"), ex.what());
return false;
}
};
if (sexp::driver sxml(ptr_account->context); parse(sxml))
{
for (auto *stanza : sxml.elements)
{
xmpp_send(ptr_account->connection, stanza);
xmpp_stanza_release(stanza);
}
}
else
{ {
stanza = xmpp_stanza_new_from_string(ptr_account->context, stanza = xmpp_stanza_new_from_string(ptr_account->context,
argv_eol[1]); argv_eol[1]);
@ -914,6 +940,7 @@ int command__xml(const void *pointer, void *data,
xmpp_send(ptr_account->connection, stanza); xmpp_send(ptr_account->connection, stanza);
xmpp_stanza_release(stanza); xmpp_stanza_release(stanza);
} }
}
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }

@ -974,6 +974,9 @@ int connection__iq_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userd
if (weechat_strcasecmp(type, "result") == 0) if (weechat_strcasecmp(type, "result") == 0)
{ {
xmpp_stanza_t *identity = xmpp_stanza_get_child_by_name(query, "identity"); xmpp_stanza_t *identity = xmpp_stanza_get_child_by_name(query, "identity");
if (identity)
{
std::string category; std::string category;
std::string name; std::string name;
std::string type; std::string type;
@ -1007,6 +1010,7 @@ int connection__iq_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userd
} }
} }
} }
}
query = xmpp_stanza_get_child_by_name_and_ns( query = xmpp_stanza_get_child_by_name_and_ns(
stanza, "query", "jabber:iq:private"); stanza, "query", "jabber:iq:private");
if (query && type) if (query && type)

@ -39,7 +39,7 @@ LDLIBS=-lstrophe \
$(shell pkg-config --libs gpgme) \ $(shell pkg-config --libs gpgme) \
$(shell pkg-config --libs libsignal-protocol-c) \ $(shell pkg-config --libs libsignal-protocol-c) \
-lgcrypt \ -lgcrypt \
-llmdb -llmdb -lfl
PREFIX ?= /usr/local PREFIX ?= /usr/local
LIBDIR ?= $(PREFIX)/lib LIBDIR ?= $(PREFIX)/lib
@ -82,16 +82,19 @@ SRCS=plugin.cpp \
DEPS=deps/diff/libdiff.a \ DEPS=deps/diff/libdiff.a \
deps/fmt/libfmt.a \ deps/fmt/libfmt.a \
sexp/sexp.a \
OBJS=$(patsubst %.cpp,.%.o,$(patsubst %.c,.%.o,$(patsubst xmpp/%.cpp,xmpp/.%.o,$(SRCS)))) OBJS=$(patsubst %.cpp,.%.o,$(patsubst %.c,.%.o,$(patsubst xmpp/%.cpp,xmpp/.%.o,$(SRCS))))
COVS=$(patsubst %.cpp,.%.cov.o,$(patsubst xmpp/%.cpp,xmpp/.%.cov.o,$(SRCS))) COVS=$(patsubst %.cpp,.%.cov.o,$(patsubst xmpp/%.cpp,xmpp/.%.cov.o,$(SRCS)))
SUFFIX=$(shell date +%s) SUFFIX=$(shell date +%s)
.PHONY: all
all: all:
make depend make depend
make weechat-xmpp && make test make weechat-xmpp && make test
.PHONY: weechat-xmpp release
weechat-xmpp: $(DEPS) xmpp.so weechat-xmpp: $(DEPS) xmpp.so
release: xmpp.so release: xmpp.so
cp xmpp.so .xmpp.so.$(SUFFIX) cp xmpp.so .xmpp.so.$(SUFFIX)
@ -102,6 +105,20 @@ xmpp.so: $(OBJS) $(DEPS) $(HDRS)
git ls-files | xargs ls -d | xargs tar cz | objcopy --add-section .source=/dev/stdin xmpp.so git ls-files | xargs ls -d | xargs tar cz | objcopy --add-section .source=/dev/stdin xmpp.so
#objcopy --dump-section .source=/dev/stdout xmpp.so | tar tz #objcopy --dump-section .source=/dev/stdout xmpp.so | tar tz
sexp/sexp.a: sexp/driver.o sexp/parser.o sexp/lexer.o
ar -r $@ $^
sexp/parser.o: sexp/parser.yy
cd sexp && bison -t -d -v parser.yy
$(CXX) $(CPPFLAGS) -c sexp/parser.tab.cc -o $@
sexp/lexer.o: sexp/lexer.l
cd sexp && flex -d --outfile=lexer.yy.cc lexer.l
$(CXX) $(CPPFLAGS) -c sexp/lexer.yy.cc -o $@
sexp/driver.o: sexp/driver.cpp
$(CXX) $(CPPFLAGS) -c $< -o $@
.%.o: %.c .%.o: %.c
$(eval GIT_REF=$(shell git describe --abbrev=6 --always --dirty 2>/dev/null || true)) $(eval GIT_REF=$(shell git describe --abbrev=6 --always --dirty 2>/dev/null || true))
$(CC) -DGIT_COMMIT=$(GIT_REF) $(CFLAGS) -c $< -o $@ $(CC) -DGIT_COMMIT=$(GIT_REF) $(CFLAGS) -c $< -o $@
@ -119,6 +136,7 @@ xmpp/.%.o: xmpp/%.cpp
xmpp/.%.cov.o: xmpp/%.cpp xmpp/.%.cov.o: xmpp/%.cpp
@$(CXX) --coverage $(CPPFLAGS) -O0 -c $< -o $@ @$(CXX) --coverage $(CPPFLAGS) -O0 -c $< -o $@
.PHONY: diff fmt
deps/diff/libdiff.a: deps/diff/libdiff.a:
git submodule update --init --recursive git submodule update --init --recursive
cd deps/diff && env -u MAKEFLAGS ./configure cd deps/diff && env -u MAKEFLAGS ./configure
@ -137,16 +155,20 @@ tests/xmpp.cov.so: $(COVS) $(DEPS) $(HDRS)
tests/run: $(COVS) tests/main.cc tests/xmpp.cov.so tests/run: $(COVS) tests/main.cc tests/xmpp.cov.so
env --chdir tests $(CXX) $(CPPFLAGS) -o run ./xmpp.cov.so main.cc $(LDLIBS) env --chdir tests $(CXX) $(CPPFLAGS) -o run ./xmpp.cov.so main.cc $(LDLIBS)
.PHONY: test
test: tests/run test: tests/run
env --chdir tests ./run -s env --chdir tests ./run -s
.PHONY: coverage
coverage: tests/run coverage: tests/run
gcov -m -abcfu -rqk -i .*.gcda xmpp/.*.gcda gcov -m -abcfu -rqk -i .*.gcda xmpp/.*.gcda
.PHONY: debug
debug: xmpp.so debug: xmpp.so
env LD_PRELOAD=$(DEBUG) gdb -ex "handle SIGPIPE nostop noprint pass" --args \ env LD_PRELOAD=$(DEBUG) gdb -ex "handle SIGPIPE nostop noprint pass" --args \
weechat -a -P 'alias,buflist,exec,irc,relay' -r '/plugin load ./xmpp.so' weechat -a -P 'alias,buflist,exec,irc,relay' -r '/plugin load ./xmpp.so'
.PHONY: depend
depend: $(SRCS) $(HDRS) depend: $(SRCS) $(HDRS)
$(RM) -f ./.depend $(RM) -f ./.depend
echo > ./.depend echo > ./.depend
@ -161,21 +183,29 @@ depend: $(SRCS) $(HDRS)
done done
sed -i 's/\.\([a-z]*\/\)/\1./' .depend sed -i 's/\.\([a-z]*\/\)/\1./' .depend
.PHONY: tidy
tidy: tidy:
$(FIND) . -name "*.o" -delete $(FIND) . -name "*.o" -delete
$(FIND) . -name "*.gcno" -delete $(FIND) . -name "*.gcno" -delete
$(FIND) . -name "*.gcda" -delete $(FIND) . -name "*.gcda" -delete
.PHONY: clean
clean: clean:
$(RM) -f $(OBJS) $(COVS) $(RM) -f $(OBJS) $(COVS) \
sexp/parser.tab.cc sexp/parser.tab.hh \
sexp/location.hh sexp/position.hh \
sexp/stack.hh sexp/parser.output sexp/parser.o \
sexp/lexer.o sexp/lexer.yy.cc sexp/sexp.a
$(MAKE) -C deps/diff clean || true $(MAKE) -C deps/diff clean || true
$(MAKE) -C deps/fmt clean || true $(MAKE) -C deps/fmt clean || true
git submodule foreach --recursive git clean -xfd || true git submodule foreach --recursive git clean -xfd || true
git submodule foreach --recursive git reset --hard || true git submodule foreach --recursive git reset --hard || true
.PHONY: distclean
distclean: clean distclean: clean
$(RM) *~ .depend $(RM) *~ .depend
.PHONY: install
install: xmpp.so install: xmpp.so
ifeq ($(shell id -u),0) ifeq ($(shell id -u),0)
mkdir -p $(DESTDIR)$(LIBDIR)/weechat/plugins mkdir -p $(DESTDIR)$(LIBDIR)/weechat/plugins
@ -187,8 +217,7 @@ else
chmod 755 ~/.weechat/plugins/xmpp.so chmod 755 ~/.weechat/plugins/xmpp.so
endif endif
.PHONY: all weechat-xmpp release test debug depend tidy clean distclean install check .PHONY: check
check: check:
clang-check --analyze *.c *.cc *.cpp clang-check --analyze *.c *.cc *.cpp

@ -0,0 +1,93 @@
#include <cctype>
#include <sstream>
#include <cassert>
#include <exception>
#include "driver.hh"
sexp::driver::~driver()
{
delete(scanner);
scanner = nullptr;
delete(parser);
parser = nullptr;
}
bool sexp::driver::parse(const char *text, std::ostream *debug = nullptr)
{
assert(text != nullptr);
std::istringstream stream{std::string(text)};
return parse(stream, debug);
}
bool sexp::driver::parse(std::istream &stream, std::ostream *debug = nullptr)
{
if(!stream.good() && stream.eof())
{
return false;
}
return parse_helper(stream, debug);
}
bool sexp::driver::parse_helper(std::istream &stream, std::ostream *debug)
{
delete(scanner);
try
{
scanner = new sexp::scanner(&stream);
}
catch(std::bad_alloc &ba)
{
throw std::runtime_error("Failed to allocate scanner");
}
delete(parser);
try
{
parser = new sexp::parser((*scanner) /* scanner */,
(*this) /* driver */);
}
catch(std::bad_alloc &ba)
{
throw std::runtime_error("Failed to allocate parser");
}
const int accept = 0;
if (debug)
{
parser->set_debug_level(1);
parser->set_debug_stream(*debug);
}
return parser->parse() == accept;
}
void sexp::driver::start_tag(const std::string &name)
{
auto *stanza = xmpp_stanza_new(context);
xmpp_stanza_set_name(stanza, name.data());
stack.push_back(stanza);
}
void sexp::driver::end_tag()
{
auto *stanza = stack.back();
stack.pop_back();
if (stack.empty())
elements.push_back(stanza);
else
xmpp_stanza_add_child_ex(stack.back(), stanza, false);
}
void sexp::driver::add_text(const std::string &text)
{
auto *stanza = xmpp_stanza_new(context);
xmpp_stanza_set_text(stanza, text.substr(1,text.length()-2).data());
xmpp_stanza_add_child_ex(stack.back(), stanza, false);
}
void sexp::driver::add_attr(const std::string &name, const std::string &value)
{
xmpp_stanza_set_attribute(stack.back(), name.data(),
value.substr(1,value.length()-2).data());
}

@ -0,0 +1,50 @@
#pragma once
#include <string>
#include <cstddef>
#include <istream>
#include <vector>
#include <strophe.h>
#include "scanner.hh"
#include "parser.tab.hh"
namespace sexp {
class driver {
public:
driver(xmpp_ctx_t *context) : context(context) {}
virtual ~driver();
/**
* parse - parse from a file
* @param text - valid string
*/
bool parse(const char *text, std::ostream *debug);
/**
* parse - parse from a c++ input stream
* @param is - std::istream&, valid input stream
*/
bool parse(std::istream &iss, std::ostream *debug);
void start_tag(const std::string &name);
void end_tag();
void add_text(const std::string &text);
void add_attr(const std::string &name, const std::string &value);
std::vector<xmpp_stanza_t*> elements;
private:
bool parse_helper(std::istream &stream, std::ostream *debug);
xmpp_ctx_t *context;
std::vector<xmpp_stanza_t*> stack;
sexp::parser *parser = nullptr;
sexp::scanner *scanner = nullptr;
};
}

@ -0,0 +1,56 @@
%option debug
%option nodefault
%option yyclass="sexp::scanner"
%option noyywrap
%option c++
%{
#include <string>
#include "scanner.hh"
#undef YY_DECL
#define YY_DECL int sexp::scanner::yylex(sexp::parser::semantic_type *const lval, sexp::parser::location_type *loc)
using token = sexp::parser::token;
// defaults to NULL
#define yyterminate() return token::END
// update location on matching
#define YY_USER_ACTION loc->step(); loc->columns(yyleng);
%}
/* Regular Expressions */
%%
%{
yylval = lval;
%}
"(" {
return token::LPAREN;
}
")" {
return token::RPAREN;
}
[\n \t\r]+ {
// Update line number
loc->lines();
return token::SPACE;
}
\"([^"\\]|\\.)*\" {
yylval->build<std::string>(yytext);
return token::STRING;
}
[^ "(:@)]+ {
yylval->build<std::string>(yytext);
return token::NAME;
}
. {
return *yytext;
}
%%

@ -0,0 +1,78 @@
%skeleton "lalr1.cc"
%require "3.0"
%debug
%defines
%define api.namespace {sexp}
%define api.parser.class {parser}
%code requires{
namespace sexp {
class driver;
class scanner;
}
}
%parse-param { sexp::scanner &scanner }
%parse-param { sexp::driver &driver }
%code{
#include <iostream>
#include <cstdlib>
#include <sstream>
/* include for all driver functions */
#include "driver.hh"
#undef yylex
#define yylex scanner.yylex
}
%define api.value.type variant
%define parse.assert
%define parse.error verbose
%token END 0 "end of file"
%token LPAREN
%token RPAREN
%token <std::string> NAME
%token <std::string> STRING
%token SPACE
%locations
%start input
%%
input : ws END | element input;
element : lparen attributeset rparenq
| lparen tag children rparen;
ws : | gap;
gap : SPACE;
lparen : ws LPAREN;
rparen : ws RPAREN { driver.end_tag(); };
rparenq : ws RPAREN;
tag : ws NAME { driver.start_tag($2); }
| ws NAME ':' NAME { driver.start_tag($4); };
attributeset : '@' attributes;
attributes : ws | gap attribute attributes;
attribute : lparen ws NAME gap STRING rparenq { driver.add_attr($3, $5); };
children : ws
| gap STRING children { driver.add_text($2); }
| gap element children;
%%
void sexp::parser::error(const sexp::parser::location_type &l, const std::string &err_message)
{
std::ostringstream ss;
ss << "parsing " << err_message << " at " << l;
throw std::invalid_argument(ss.str());
}

@ -0,0 +1,37 @@
#pragma once
#ifndef yyFlexLexer
#include <FlexLexer.h>
#endif
#include "parser.tab.hh"
#include "location.hh"
namespace sexp {
class scanner : public yyFlexLexer {
public:
scanner(std::istream *in) : yyFlexLexer(in)
{
};
virtual ~scanner() {
};
// get rid of override virtual function warning
using FlexLexer::yylex;
virtual
int yylex(sexp::parser::semantic_type *const lval,
sexp::parser::location_type *location);
// YY_DECL defined in lexer.l
// Method body created by flex in lexer.yy.cc
private:
/* yyval ptr */
sexp::parser::semantic_type *yylval = nullptr;
};
}
Loading…
Cancel
Save