fixed build with gcc-12.1.1
[hatexmpp.git] / hatexmpp.c
blobd54f00a2145662fce4b940979d62a674509d5fe8
1 #include "common.h"
3 GMainLoop *main_loop;
4 GMainContext *context;
5 GHashTable *config;
6 GHashTable *roster;
8 GArray *LogBuf;
9 int fd_events;
10 gchar *events_file;
11 time_t last_activity_time;
12 enum connection_state_e connection_state;
14 gchar *eventstr(gchar *str) /* TODO const */
16 #ifdef EVENTS
17 if (g_hash_table_lookup(config, "events")) {
18 if (fd_events <= 0) {
19 fd_events = open(events_file, O_WRONLY | O_NONBLOCK);
20 #ifdef DEBUG
21 logf("open fd_events = %d errno = %d", fd_events, errno);
22 #endif
24 #ifdef DEBUG
25 logf("Event: fd_events = %d, str = %s", fd_events, str);
26 #endif
27 if (fd_events != -1) {
28 write(fd_events, str, strlen(str) + 1);
29 #ifdef DEBUG
30 logf("write to fd_events = %d errno = %d", fd_events, errno);
31 #endif
34 #endif
35 return str;
38 inline void logs(const char *msg, size_t len)
40 g_array_append_vals(LogBuf, msg, len);
43 gchar * logstr(gchar *msg) /* TODO const */
45 size_t len;
47 g_printf("LOGF: %s", msg);
48 len = strlen(msg);
49 logs(msg, len);
51 return msg;
54 void destroy_resource(resourceitem *resi)
56 if (resi) {
57 if (resi->name) g_free(resi->name);
58 g_free(resi);
62 rosteritem *addri(const gchar *jid, GHashTable *resources, unsigned type)
64 rosteritem *ri;
66 logf("Adding %s to roster\n", jid);
67 eventf("add_ri %s %s", jid, (type == MUC) ? "MUC" : "BUDDY");
68 ri = g_new(rosteritem, 1);
69 if (ri) {
70 ri->jid = g_strdup(jid);
71 if (resources)
72 ri->resources = resources; /* TODO */
73 else
74 ri->resources = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify) destroy_resource);
75 ri->log = g_array_new(FALSE, FALSE, 1);
76 ri->type = type;
77 g_hash_table_insert(roster, g_strdup(jid), ri);
78 ri->self_resource = g_new(resourceitem, 1);
80 return ri;
83 void destroy_ri(rosteritem *RI)
85 if (!RI) return;
86 eventf("del_ri %s", RI->jid);
87 if (RI->jid) g_free(RI->jid);
88 if (RI->resources) g_hash_table_destroy(RI->resources);
89 if (RI->log) g_array_free(RI->log, TRUE);
90 if (RI->self_resource) g_free(RI->self_resource);
91 g_free(RI);
94 void free_all() // trying to make a general cleanup
96 g_hash_table_destroy(roster);
97 g_array_free(LogBuf, TRUE);
98 g_hash_table_destroy(config);
101 void init_config()
103 config = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
104 const struct cfg_s {
105 char *name;
106 char *value;
107 } cfg[] = {
108 { "username", NULL },
109 { "password", NULL },
110 { "register", NULL },
111 { "resource", "hatexmpp" },
112 { "priority", "0" },
113 { "show", NULL },
114 { "status", NULL },
115 { "muc_default_nick", "hatexmpp" },
116 { "jiv_name", NULL },
117 { "jiv_os", NULL },
118 { "jiv_version", NULL },
119 { "send_receipts", "1" },
120 #ifdef PROXY
121 { "proxy_server", NULL },
122 { "proxy_port", NULL },
123 { "proxy_username", NULL },
124 { "proxy_password", NULL },
125 #endif
126 { NULL, NULL },
128 const struct cfg_s *cfgp = cfg;
130 do {
131 void *v = cfgp->value ? g_strdup(cfgp->value) : NULL;
132 g_hash_table_insert(config, cfgp->name, v);
133 } while ((++cfgp)->name);
136 int main(int argc, char **argv)
138 if (!lm_ssl_is_supported()) {
139 fprintf(stderr, "Your loudmouth distribution doesn't support SSL. hatexmpp won't work properly. Poke the author if you'd like to be able to run it in plaintext-only mode. Exiting.\n");
140 exit(1);
142 LogBuf = g_array_sized_new(FALSE, FALSE, 1, 512);
143 logf("hatexmpp v%s is going up\n", HateXMPP_ver);
144 roster = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify) destroy_ri);
145 context = g_main_context_new();
146 main_loop = g_main_loop_new(context, FALSE);
147 init_config();
148 xmpp_init();
150 // Do something with this!!!!!
151 if (argc) {
152 events_file = g_strdup_printf("%sevents", argv[1]);
155 logf("Events FIFO: %s\n", events_file);
157 return fuseinit(argc, argv);