From 308e412ea378b3e3047a209a78dd6ce9994f2aff Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 1 Sep 2013 03:26:17 +0100 Subject: [PATCH] Build libprofanity and link with c plugin --- .gitignore | 6 ++ Makefile.am | 4 +- bootstrap.sh | 2 +- configure.ac | 2 +- plugins/test-c-plugin/Makefile | 5 +- plugins/test-c-plugin/test-c-plugin.c | 69 ++++++++++++--- src/plugins/api.c | 2 +- src/plugins/c_api.c | 116 ++++++++++++++++++++++++++ src/plugins/c_api.h | 28 +++++++ src/plugins/c_plugins.c | 7 ++ src/plugins/c_plugins.h | 2 + src/plugins/plugins.c | 6 +- src/plugins/profapi.c | 40 +++++++++ src/plugins/profapi.h | 41 +++++++++ 14 files changed, 310 insertions(+), 20 deletions(-) create mode 100644 src/plugins/c_api.c create mode 100644 src/plugins/c_api.h create mode 100644 src/plugins/profapi.c create mode 100644 src/plugins/profapi.h diff --git a/.gitignore b/.gitignore index 26eeccba..031fd43f 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,9 @@ _configs.sed gource.sh *_fingerprints.txt *_key.txt +.libs/ +libprofanity.la +libtool +m4/ +*.so +*.lo diff --git a/Makefile.am b/Makefile.am index defb6e83..9f3c8ae0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -31,9 +31,11 @@ profanity_SOURCES = \ src/plugins/python_api.h src/plugins/python_api.c \ src/plugins/c_plugins.h src/plugins/c_plugins.c \ src/plugins/ruby_plugins.h src/plugins/ruby_plugins.c \ - src/plugins/ruby_api.h src/plugins/ruby_api.c + src/plugins/ruby_api.h src/plugins/ruby_api.c \ + src/plugins/c_api.h src/plugins/c_api.c lib_LTLIBRARIES = libprofanity.la +libprofanity_la_LDFLAGS=-module -avoid-version -shared libprofanity_la_SOURCES = src/plugins/profapi.c library_includedir=$(includedir) diff --git a/bootstrap.sh b/bootstrap.sh index 239b1ee6..9b6b39f7 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,3 +1,3 @@ #!/bin/sh -autoreconf -f -i -Wall,no-obsolete +autoreconf -fiv diff --git a/configure.ac b/configure.ac index fc73501b..85d90e5c 100644 --- a/configure.ac +++ b/configure.ac @@ -3,12 +3,12 @@ AC_PREREQ([2.65]) AC_INIT([profanity], [0.4.0], [boothj5web@gmail.com]) +AC_CONFIG_AUX_DIR([build-aux]) LT_INIT PACKAGE_STATUS="development" AC_DEFINE_UNQUOTED([PACKAGE_STATUS], ["$PACKAGE_STATUS"], [Status of this build]) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([src/config.h]) -AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE([foreign subdir-objects]) AX_PREFIX_CONFIG_H([src/prof_config.h], [PROF], [src/config.h]) diff --git a/plugins/test-c-plugin/Makefile b/plugins/test-c-plugin/Makefile index 96779ecb..66ae3e66 100644 --- a/plugins/test-c-plugin/Makefile +++ b/plugins/test-c-plugin/Makefile @@ -1,12 +1,11 @@ - all: test-c-plugin.so %.so:%.o - $(CC) -shared -fpic -o $@ $^ + $(CC) -shared -fpic -lprofanity -o $@ $^ %.o:%.c $(CC) $(INCLUDE) -D_GNU_SOURCE -D_BSD_SOURCE -fpic -O3 -std=c99 \ - -Wall -Wextra -pedantic -c -o $@ $< + -Wall -Wextra -lprofanity -pedantic -c -o $@ $< clean: $(RM) test-c-plugin.so diff --git a/plugins/test-c-plugin/test-c-plugin.c b/plugins/test-c-plugin/test-c-plugin.c index c0f9d731..852fcb3f 100644 --- a/plugins/test-c-plugin/test-c-plugin.c +++ b/plugins/test-c-plugin/test-c-plugin.c @@ -3,28 +3,73 @@ #include #include +#include + void -prof_init (const char * const version, const char * const status) +cmd_c(char **args) { - fprintf (stderr, "called %s with args=<%s, %s>\n", __func__, version, status); + if (args[0] != NULL) { + char *start = "c-test: /c command called, arg = "; + char buf[strlen(start) + strlen(args[0]) + 1]; + sprintf(buf, "%s%s", start, args[0]); + prof_cons_show(buf); + } else { + prof_cons_show("c-test: /c command called with no arg"); + } + prof_cons_alert(); + prof_notify("c-test: notify", 2000, "Plugins"); + prof_send_line("/about"); + prof_cons_show("c-test: sent \"/about\" command"); } void -prof_on_start (void) +timer_test(void) { - fprintf (stderr, "called %s with no args\n", __func__); + prof_cons_show("c-test: timer fired."); + char *recipient = prof_get_current_recipient(); + if (recipient != NULL) { + char *start = " current recipient = "; + char buf[strlen(start) + strlen(recipient) + 1]; + sprintf(buf, "%s%s", start, recipient); + prof_cons_show(buf); + } + prof_cons_alert(); } void -prof_on_connect (const char * const account_name, const char * const fulljid) +prof_init(const char * const version, const char * const status) { - fprintf (stderr, "called %s with args=<%s, %s>\n", __func__, account_name, fulljid); + char *start = "c-test: init. "; + char buf[strlen(start) + strlen(version) + 2 + strlen(status) + 1]; + sprintf(buf, "%s%s, %s", start, version, status); + prof_cons_show(buf); + prof_register_command("/c", 0, 1, "/c", "c test", "c test", cmd_c); + prof_register_timed(timer_test, 10); +} + +void +prof_on_start(void) +{ + prof_cons_show("c-test: on_start"); +} + +void +prof_on_connect(const char * const account_name, const char * const fulljid) +{ + char *start = "c-test: on_connect, "; + char buf[strlen(start) + strlen(account_name) + 2 + strlen(fulljid) + 1]; + sprintf(buf, "%s%s, %s", start, account_name, fulljid); + prof_cons_show(buf); } char * -prof_on_message_received (const char * const jid, const char *message) +prof_on_message_received(const char * const jid, const char *message) { - fprintf (stderr, "called %s with args=<%s, %s>\n", __func__, jid, message); + char *start = "c-test: on_message_received, "; + char buf[strlen(start) + strlen(jid) + 2 + strlen(message) + 1]; + sprintf(buf, "%s%s, %s", start, jid, message); + prof_cons_show(buf); + prof_cons_alert(); char *result = malloc(strlen(message) + 4); sprintf(result, "%s%s", message, "[C]"); @@ -32,9 +77,13 @@ prof_on_message_received (const char * const jid, const char *message) } char * -prof_on_message_send (const char * const jid, const char *message) +prof_on_message_send(const char * const jid, const char *message) { - fprintf (stderr, "called %s with args=<%s, %s>\n", __func__, jid, message); + char *start = "c-test: on_message_send, "; + char buf[strlen(start) + strlen(jid) + 2 + strlen(message) + 1]; + sprintf(buf, "%s%s, %s", start, jid, message); + prof_cons_show(buf); + prof_cons_alert(); char *result = malloc(strlen(message) + 4); sprintf(result, "%s%s", message, "[C]"); diff --git a/src/plugins/api.c b/src/plugins/api.c index cc7b26ed..5db195a7 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -58,7 +58,7 @@ api_register_command(const char *command_name, int min_args, int max_args, command->short_help = short_help; command->long_help = long_help; command->callback = callback; - command->callback_func =callback_func; + command->callback_func = callback_func; callbacks_add_command(command); } diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c new file mode 100644 index 00000000..627c827c --- /dev/null +++ b/src/plugins/c_api.c @@ -0,0 +1,116 @@ +/* + * c_api.c + * + * Copyright (C) 2012, 2013 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include +#include + +#include "log.h" +#include "plugins/api.h" +#include "plugins/c_api.h" +#include "plugins/callbacks.h" +#include "plugins/profapi.h" + +typedef struct command_wrapper_t { + void(*func)(char **args); +} CommandWrapper; + +typedef struct timed_wrapper_t { + void(*func)(void); +} TimedWrapper; + +static void +c_api_cons_alert(void) +{ + api_cons_alert(); +} + +static void +c_api_cons_show(const char * const message) +{ + if (message != NULL) { + api_cons_show(message); + } +} + +static void +c_api_register_command(const char *command_name, int min_args, int max_args, + const char *usage, const char *short_help, const char *long_help, void(*callback)(char **args)) +{ + CommandWrapper *wrapper = malloc(sizeof(CommandWrapper)); + wrapper->func = callback; + api_register_command(command_name, min_args, max_args, usage, + short_help, long_help, wrapper, c_command_callback); +} + +static void +c_api_register_timed(void(*callback)(void), int interval_seconds) +{ + TimedWrapper *wrapper = malloc(sizeof(TimedWrapper)); + wrapper->func = callback; + api_register_timed(wrapper, interval_seconds, c_timed_callback); +} + +static void +c_api_notify(const char *message, int timeout_ms, const char *category) +{ + api_notify(message, category, timeout_ms); +} + +static void +c_api_send_line(char *line) +{ + api_send_line(line); +} + +static char * +c_api_get_current_recipient(void) +{ + return api_get_current_recipient(); +} + +void +c_command_callback(PluginCommand *command, gchar **args) +{ + CommandWrapper *wrapper = command->callback; + void(*f)(gchar **args) = wrapper->func; + f(args); +} + +void +c_timed_callback(PluginTimedFunction *timed_function) +{ + TimedWrapper *wrapper = timed_function->callback; + void(*f)(void) = wrapper->func; + f(); +} + +void +c_api_init(void) +{ + prof_cons_alert = c_api_cons_alert; + prof_cons_show = c_api_cons_show; + prof_register_command = c_api_register_command; + prof_register_timed = c_api_register_timed; + prof_notify = c_api_notify; + prof_send_line = c_api_send_line; + prof_get_current_recipient = c_api_get_current_recipient; +} diff --git a/src/plugins/c_api.h b/src/plugins/c_api.h new file mode 100644 index 00000000..f220e420 --- /dev/null +++ b/src/plugins/c_api.h @@ -0,0 +1,28 @@ +/* + * c_api.h + * + * Copyright (C) 2012, 2013 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include + +void c_api_init(void); + +void c_command_callback(PluginCommand *command, gchar **args); +void c_timed_callback(PluginTimedFunction *timed_function); diff --git a/src/plugins/c_plugins.c b/src/plugins/c_plugins.c index 378cda4d..e6a8ee59 100644 --- a/src/plugins/c_plugins.c +++ b/src/plugins/c_plugins.c @@ -11,8 +11,15 @@ #include "plugins/callbacks.h" #include "plugins/plugins.h" #include "plugins/c_plugins.h" +#include "plugins/c_api.h" #include "ui/ui.h" +void +c_env_init(void) +{ + c_api_init(); +} + ProfPlugin * c_plugin_create(const char * const filename) { diff --git a/src/plugins/c_plugins.h b/src/plugins/c_plugins.h index afa818b6..7d78f439 100644 --- a/src/plugins/c_plugins.h +++ b/src/plugins/c_plugins.h @@ -3,6 +3,8 @@ #include "plugins/plugins.h" +void c_env_init(void); + ProfPlugin* c_plugin_create(const char * const filename); void c_init_hook(ProfPlugin *plugin, const char * const version, const char * const status); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index e67dc83d..51e1009f 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -27,14 +27,13 @@ #include "plugins/callbacks.h" #include "plugins/plugins.h" #include "plugins/python_plugins.h" -#include "plugins/c_plugins.h" #include "plugins/python_api.h" +#include "plugins/c_plugins.h" +#include "plugins/c_api.h" #include "plugins/ruby_plugins.h" #include "plugins/ruby_api.h" #include "ui/ui.h" - - static GSList* plugins; void @@ -44,6 +43,7 @@ plugins_init(void) python_env_init(); ruby_env_init(); + c_env_init(); // load plugins gchar **plugins_load = prefs_get_plugins(); diff --git a/src/plugins/profapi.c b/src/plugins/profapi.c new file mode 100644 index 00000000..f04db4dd --- /dev/null +++ b/src/plugins/profapi.c @@ -0,0 +1,40 @@ +/* + * prof_api.c + * + * Copyright (C) 2012, 2013 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include + +#include "plugins/callbacks.h" + +void (*prof_cons_alert)(void) = NULL; + +void (*prof_cons_show)(const char * const message) = NULL; + +void (*prof_register_command)(const char *command_name, int min_args, int max_args, + const char *usage, const char *short_help, const char *long_help, void(*callback)(char **args)) = NULL; + +void (*prof_register_timed)(void(*callback)(void), int interval_seconds) = NULL; + +void (*prof_notify)(const char *message, int timeout_ms, const char *category) = NULL; + +void (*prof_send_line)(char *line) = NULL; + +char* (*prof_get_current_recipient)(void) = NULL; diff --git a/src/plugins/profapi.h b/src/plugins/profapi.h new file mode 100644 index 00000000..184abbe2 --- /dev/null +++ b/src/plugins/profapi.h @@ -0,0 +1,41 @@ +/* + * prof_api.h + * + * Copyright (C) 2012, 2013 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#ifndef PROF_API_H +#define PROF_API_H + +void (*prof_cons_alert)(void); + +void (*prof_cons_show)(const char * const message); + +void (*prof_register_command)(const char *command_name, int min_args, int max_args, + const char *usage, const char *short_help, const char *long_help, void(*callback)(char **args)); + +void (*prof_register_timed)(void(*callback)(void), int interval_seconds); + +void (*prof_notify)(const char *message, int timeout_ms, const char *category); + +void (*prof_send_line)(char *line); + +char* (*prof_get_current_recipient)(void); + +#endif