1/*
2 * @file xmppc.c
3 *
4 * vim: expandtab:ts=2:sts=2:sw=2
5 *
6 * @copyright
7 *
8 * Copyright (C) 2020 Anoxinon e.V.
9 *
10 * This file is part of xmppc.
11 *
12 * xmppc is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * xmppc is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * German
26 *
27 * Diese Datei ist Teil von xmppc.
28 *
29 * xmppc ist Freie Software: Sie können es unter den Bedingungen
30 * der GNU General Public License, wie von der Free Software Foundation,
31 * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
32 * veröffentlichten Version, weiter verteilen und/oder modifizieren.
33 *
34 * xmppc wird in der Hoffnung, dass es nützlich sein wird, aber
35 * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
36 * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
37 * Siehe die GNU General Public License für weitere Details.
38 *
39 * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
40 * Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
41 */
42
43#include "xmppc.h"
44#include <assert.h>
45#include <stdarg.h>
46#include <stdlib.h>
47
48/*! @file xmppc.c
49 *
50 * xmppc
51 *
52 */
53
54/*!
55 * Error logging
56 *
57 * \param xmppc the xmppc context structure
58 * \param fmt format of message
59 *
60 */
61void logError(xmppc_t *xmppc, const char *fmt, ...) {
62 va_list args;
63 va_start(args, fmt);
64 vfprintf(stderr, fmt, args);
65 va_end(args);
66}
67
68void logWarn(xmppc_t *xmppc, const char *fmt, ...) {
69 if (xmppc->loglevel) {
70 va_list args;
71 va_start(args, fmt);
72 vprintf(fmt, args);
73 va_end(args);
74 }
75}
76
77void logInfo(xmppc_t *xmppc, const char *fmt, ...) {
78 if (xmppc->loglevel) {
79 va_list args;
80 va_start(args, fmt);
81 vprintf(fmt, args);
82 va_end(args);
83 }
84}
85
86void logDebug(xmppc_t *xmppc, const char *fmt, ...) {
87 if (xmppc->loglevel) {
88 va_list args;
89 va_start(args, fmt);
90 vprintf(fmt, args);
91 va_end(args);
92 }
93}
94
95/*!
96 * \brief Setup the application context.
97 *
98 * \param xmppc
99 * \return 0 - ok
100 *
101 **/
102int xmppc_context(xmppc_t *xmppc, int level) {
103 assert(xmppc != NULL);
104 assert(xmppc->ctx == NULL);
105 xmpp_log_t *log = NULL;
106
107 if (level > TRACE) {
108 logError(xmppc, "Log level %d not supported. Max: %d.\n", level, TRACE);
109 exit(-1);
110 }
111
112 xmppc->loglevel = level;
113
114 if (level == ERROR) {
115 log = xmpp_get_default_logger(XMPP_LEVEL_ERROR);
116 } else if (level == WARN) {
117 log = xmpp_get_default_logger(XMPP_LEVEL_WARN);
118 } else if (level == INFO) {
119 log = xmpp_get_default_logger(XMPP_LEVEL_INFO);
120 } else if (level == DEBUG) {
121 log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
122 } else if (level == TRACE) {
123 log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
124 }
125
126 xmpp_ctx_t *ctx = xmpp_ctx_new(NULL, log);
127 xmppc->ctx = ctx;
128 return 0;
129}
130
131int xmppc_connect(xmppc_t *xmppc, char *jid, char *password) {
132 assert(xmppc != NULL);
133 assert(jid != NULL);
134 assert(password != NULL);
135 xmpp_conn_t *conn = xmpp_conn_new(xmppc->ctx);
136 assert(conn != NULL);
137
138 xmpp_conn_set_jid(conn, jid);
139 xmpp_conn_set_pass(conn, password);
140 xmppc->conn = conn;
141 return 0;
142}