about summary refs log tree commit diff stats
path: root/src/luasocket/options.c
blob: 06ab58d4d1db7a2433af834b08128a5c64af5cf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*=========================================================================*\
* Common option interface
* LuaSocket toolkit
\*=========================================================================*/
#include "luasocket.h"
#include "auxiliar.h"
#include "options.h"
#include "inet.h"
#include <string.h>

/*=========================================================================*\
* Internal functions prototypes
\*=========================================================================*/
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name);
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
static int opt_getboolean(lua_State *L, p_socket ps, int level, int name);
static int opt_setint(lua_State *L, p_socket ps, int level, int name);
static int opt_getint(lua_State *L, p_socket ps, int level, int name);
static int opt_set(lua_State *L, p_socket ps, int level, int name,
        void *val, int len);
static int opt_get(lua_State *L, p_socket ps, int level, int name,
        void *val, int* len);

/*=========================================================================*\
* Exported functions
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Calls appropriate option handler
\*-------------------------------------------------------------------------*/
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
{
    const char *name = luaL_checkstring(L, 2);      /* obj, name, ... */
    while (opt->name && strcmp(name, opt->name))
        opt++;
    if (!opt->func) {
        char msg[57];
        sprintf(msg, "unsupported option `%.35s'", name);
        luaL_argerror(L, 2, msg);
    }
    return opt->func(L, ps);
}

int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
{
    const char *name = luaL_checkstring(L, 2);      /* obj, name, ... */
    while (opt->name && strcmp(name, opt->name))
        opt++;
    if (!opt->func) {
        char msg[57];
        sprintf(msg, "unsupported option `%.35s'", name);
        luaL_argerror(L, 2, msg);
    }
    return opt->func(L, ps);
}

// -------------------------------------------------------
/* enables reuse of local address */
int opt_set_reuseaddr(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
}

int opt_get_reuseaddr(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
}

// -------------------------------------------------------
/* enables reuse of local port */
int opt_set_reuseport(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
}

int opt_get_reuseport(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
}

// -------------------------------------------------------
/* disables the Nagle algorithm */
int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
}

int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
}

// -------------------------------------------------------
#ifdef TCP_KEEPIDLE

int opt_get_tcp_keepidle(lua_State *L, p_socket ps)
{
    return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
}

int opt_set_tcp_keepidle(lua_State *L, p_socket ps)
{
    return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
}

#endif

// -------------------------------------------------------
#ifdef TCP_KEEPCNT

int opt_get_tcp_keepcnt(lua_State *L, p_socket ps)
{
    return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
}

int opt_set_tcp_keepcnt(lua_State *L, p_socket ps)
{
    return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
}

#endif

// -------------------------------------------------------
#ifdef TCP_KEEPINTVL

int opt_get_tcp_keepintvl(lua_State *L, p_socket ps)
{
    return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
}

int opt_set_tcp_keepintvl(lua_State *L, p_socket ps)
{
    return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
}

#endif

// -------------------------------------------------------
int opt_set_keepalive(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
}

int opt_get_keepalive(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
}

// -------------------------------------------------------
int opt_set_dontroute(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
}

int opt_get_dontroute(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
}

// -------------------------------------------------------
int opt_set_broadcast(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
}

int opt_get_broadcast(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
}

// -------------------------------------------------------
int opt_set_recv_buf_size(lua_State *L, p_socket ps)
{
	return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF);
}

int opt_get_recv_buf_size(lua_State *L, p_socket ps)
{
	return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF);
}

// -------------------------------------------------------
int opt_get_send_buf_size(lua_State *L, p_socket ps)
{
	return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF);
}

int opt_set_send_buf_size(lua_State *L, p_socket ps)
{
	return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF);
}

// -------------------------------------------------------
int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps)
{
  return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
}

int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps)
{
  return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
}

// -------------------------------------------------------
int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps)
{
  return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
}

int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps)
{
  return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
}

// -------------------------------------------------------
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
}

int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
}

// -------------------------------------------------------
int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
}

int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
}

// -------------------------------------------------------
int opt_set_linger(lua_State *L, p_socket ps)
{
    struct linger li;                      /* obj, name, table */
    if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
    lua_pushstring(L, "on");
    lua_gettable(L, 3);
    if (!lua_isboolean(L, -1))
        luaL_argerror(L, 3, "boolean 'on' field expected");
    li.l_onoff = (u_short) lua_toboolean(L, -1);
    lua_pushstring(L, "timeout");
    lua_gettable(L, 3);
    if (!lua_isnumber(L, -1))
        luaL_argerror(L, 3, "number 'timeout' field expected");
    li.l_linger = (u_short) lua_tonumber(L, -1);
    return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
}

int opt_get_linger(lua_State *L, p_socket ps)
{
    struct linger li;                      /* obj, name */
    int len = sizeof(li);
    int err = opt_get(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, &len);
    if (err)
        return err;
    lua_newtable(L);
    lua_pushboolean(L, li.l_onoff);
    lua_setfield(L, -2, "on");
    lua_pushinteger(L, li.l_linger);
    lua_setfield(L, -2, "timeout");
    return 1;
}

// -------------------------------------------------------
int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
{
    return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
}

// -------------------------------------------------------
int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
{
    const char *address = luaL_checkstring(L, 3);    /* obj, name, ip */
    struct in_addr val;
    val.s_addr = htonl(INADDR_ANY);
    if (strcmp(address, "*") && !inet_aton(address, &val))
        luaL_argerror(L, 3, "ip expected");
    return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF,
        (char *) &val, sizeof(val));
}

int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
{
    struct in_addr val;
    socklen_t len = sizeof(val);
    if (getsockopt(*ps, IPPROTO_IP, IP_MULTICAST_IF, (char *) &val, &len) < 0) {
        lua_pushnil(L);
        lua_pushstring(L, "getsockopt failed");
        return 2;
    }
    lua_pushstring(L, inet_ntoa(val));
    return 1;
}

// -------------------------------------------------------
int opt_set_ip_add_membership(lua_State *L, p_socket ps)
{
    return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
}

int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
{
    return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
}

// -------------------------------------------------------
int opt_set_ip6_add_membership(lua_State *L, p_socket ps)
{
    return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP);
}

int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps)
{
    return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP);
}
// -------------------------------------------------------
int opt_get_ip6_v6only(lua_State *L, p_socket ps)
{
    return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
}

int opt_set_ip6_v6only(lua_State *L, p_socket ps)
{
    return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
}

// -------------------------------------------------------
int opt_get_error(lua_State *L, p_socket ps)
{
    int val = 0;
    socklen_t len = sizeof(val);
    if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
        lua_pushnil(L);
        lua_pushstring(L, "getsockopt failed");
        return 2;
    }
    lua_pushstring(L, socket_strerror(val));
    return 1;
}

/*=========================================================================*\
* Auxiliar functions
\*=========================================================================*/
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name)
{
    struct ip_mreq val;                   /* obj, name, table */
    if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
    lua_pushstring(L, "multiaddr");
    lua_gettable(L, 3);
    if (!lua_isstring(L, -1))
        luaL_argerror(L, 3, "string 'multiaddr' field expected");
    if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr))
        luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
    lua_pushstring(L, "interface");
    lua_gettable(L, 3);
    if (!lua_isstring(L, -1))
        luaL_argerror(L, 3, "string 'interface' field expected");
    val.imr_interface.s_addr = htonl(INADDR_ANY);
    if (strcmp(lua_tostring(L, -1), "*") &&
            !inet_aton(lua_tostring(L, -1), &val.imr_interface))
        luaL_argerror(L, 3, "invalid 'interface' ip address");
    return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
}

static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name)
{
    struct ipv6_mreq val;                   /* obj, opt-name, table */
    memset(&val, 0, sizeof(val));
    if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
    lua_pushstring(L, "multiaddr");
    lua_gettable(L, 3);
    if (!lua_isstring(L, -1))
        luaL_argerror(L, 3, "string 'multiaddr' field expected");
    if (!inet_pton(AF_INET6, lua_tostring(L, -1), &val.ipv6mr_multiaddr))
        luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
    lua_pushstring(L, "interface");
    lua_gettable(L, 3);
    /* By default we listen to interface on default route
     * (sigh). However, interface= can override it. We should
     * support either number, or name for it. Waiting for
     * windows port of if_nametoindex */
    if (!lua_isnil(L, -1)) {
        if (lua_isnumber(L, -1)) {
            val.ipv6mr_interface = (unsigned int) lua_tonumber(L, -1);
        } else
          luaL_argerror(L, -1, "number 'interface' field expected");
    }
    return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
}

static
int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len)
{
    socklen_t socklen = *len;
    if (getsockopt(*ps, level, name, (char *) val, &socklen) < 0) {
        lua_pushnil(L);
        lua_pushstring(L, "getsockopt failed");
        return 2;
    }
    *len = socklen;
    return 0;
}

static
int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
{
    if (setsockopt(*ps, level, name, (char *) val, len) < 0) {
        lua_pushnil(L);
        lua_pushstring(L, "setsockopt failed");
        return 2;
    }
    lua_pushnumber(L, 1);
    return 1;
}

static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
{
    int val = 0;
    int len = sizeof(val);
    int err = opt_get(L, ps, level, name, (char *) &val, &len);
    if (err)
        return err;
    lua_pushboolean(L, val);
    return 1;
}

static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
{
    int val = auxiliar_checkboolean(L, 3);             /* obj, name, bool */
    return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
}

static int opt_getint(lua_State *L, p_socket ps, int level, int name)
{
    int val = 0;
    int len = sizeof(val);
    int err = opt_get(L, ps, level, name, (char *) &val, &len);
    if (err)
        return err;
    lua_pushnumber(L, val);
    return 1;
}

static int opt_setint(lua_State *L, p_socket ps, int level, int name)
{
    int val = (int) lua_tonumber(L, 3);             /* obj, name, int */
    return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
}
an>const char *key = _get_key(pref); gboolean def = _get_default_boolean(pref); if (!g_key_file_has_key(prefs, group, key, NULL)) { return def; } return g_key_file_get_boolean(prefs, group, key, NULL); } void prefs_set_boolean(preference_t pref, gboolean value) { const char *group = _get_group(pref); const char *key = _get_key(pref); g_key_file_set_boolean(prefs, group, key, value); _save_prefs(); } char* prefs_get_string(preference_t pref) { const char *group = _get_group(pref); const char *key = _get_key(pref); char *def = _get_default_string(pref); char *result = g_key_file_get_string(prefs, group, key, NULL); if (result == NULL) { if (def) { return g_strdup(def); } else { return NULL; } } else { return result; } } void prefs_free_string(char *pref) { if (pref) { g_free(pref); } } void prefs_set_string(preference_t pref, char *value) { const char *group = _get_group(pref); const char *key = _get_key(pref); if (value == NULL) { g_key_file_remove_key(prefs, group, key, NULL); } else { g_key_file_set_string(prefs, group, key, value); } _save_prefs(); } gint prefs_get_gone(void) { return g_key_file_get_integer(prefs, PREF_GROUP_CHATSTATES, "gone", NULL); } void prefs_set_gone(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_CHATSTATES, "gone", value); _save_prefs(); } gint prefs_get_notify_remind(void) { return g_key_file_get_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", NULL); } void prefs_set_notify_remind(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", value); _save_prefs(); } gint prefs_get_max_log_size(void) { if (log_maxsize < PREFS_MIN_LOG_SIZE) return PREFS_MAX_LOG_SIZE; else return log_maxsize; } void prefs_set_max_log_size(gint value) { log_maxsize = value; g_key_file_set_integer(prefs, PREF_GROUP_LOGGING, "maxsize", value); _save_prefs(); } gint prefs_get_inpblock(void) { int val = g_key_file_get_integer(prefs, PREF_GROUP_UI, "inpblock", NULL); if (val == 0) { return INPBLOCK_DEFAULT; } else { return val; } } void prefs_set_inpblock(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_UI, "inpblock", value); _save_prefs(); } gint prefs_get_reconnect(void) { if (!g_key_file_has_key(prefs, PREF_GROUP_CONNECTION, "reconnect", NULL)) { return 30; } else { return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", NULL); } } void prefs_set_reconnect(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", value); _save_prefs(); } gint prefs_get_autoping(void) { if (!g_key_file_has_key(prefs, PREF_GROUP_CONNECTION, "autoping", NULL)) { return 60; } else { return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "autoping", NULL); } } void prefs_set_autoping(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "autoping", value); _save_prefs(); } gint prefs_get_autoping_timeout(void) { if (!g_key_file_has_key(prefs, PREF_GROUP_CONNECTION, "autoping.timeout", NULL)) { return 10; } else { return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "autoping.timeout", NULL); } } void prefs_set_autoping_timeout(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "autoping.timeout", value); _save_prefs(); } gint prefs_get_autoaway_time(void) { gint result = g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.awaytime", NULL); if (result == 0) { return 15; } else { return result; } } gint prefs_get_autoxa_time(void) { return g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.xatime", NULL); } void prefs_set_autoaway_time(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.awaytime", value); _save_prefs(); } void prefs_set_autoxa_time(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.xatime", value); _save_prefs(); } void prefs_set_occupants_size(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_UI, "occupants.size", value); _save_prefs(); } gint prefs_get_occupants_size(void) { gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "occupants.size", NULL); if (result > 99 || result < 1) { return 15; } else { return result; } } void prefs_set_roster_size(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.size", value); _save_prefs(); } gint prefs_get_roster_size(void) { gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.size", NULL); if (result > 99 || result < 1) { return 25; } else { return result; } } char prefs_get_otr_char(void) { char result = '~'; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_OTR, "otr.char", NULL); if (!resultstr) { result = '~'; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_otr_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_OTR, "otr.char", str); _save_prefs(); } char prefs_get_pgp_char(void) { char result = '~'; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_PGP, "pgp.char", NULL); if (!resultstr) { result = '~'; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_pgp_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_PGP, "pgp.char", str); _save_prefs(); } char prefs_get_roster_header_char(void) { char result = 0; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.header.char", NULL); if (!resultstr) { result = 0; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_roster_header_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.header.char", str); _save_prefs(); } void prefs_clear_roster_header_char(void) { g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.header.char", NULL); _save_prefs(); } char prefs_get_roster_contact_char(void) { char result = 0; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.contact.char", NULL); if (!resultstr) { result = 0; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_roster_contact_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.contact.char", str); _save_prefs(); } void prefs_clear_roster_contact_char(void) { g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.contact.char", NULL); _save_prefs(); } char prefs_get_roster_resource_char(void) { char result = 0; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.resource.char", NULL); if (!resultstr) { result = 0; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_roster_resource_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.resource.char", str); _save_prefs(); } void prefs_clear_roster_resource_char(void) { g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.resource.char", NULL); _save_prefs(); } char prefs_get_roster_private_char(void) { char result = 0; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.private.char", NULL); if (!resultstr) { result = 0; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_roster_private_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.private.char", str); _save_prefs(); } void prefs_clear_roster_private_char(void) { g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.private.char", NULL); _save_prefs(); } char prefs_get_roster_room_char(void) { char result = 0; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.rooms.char", NULL); if (!resultstr) { result = 0; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_roster_room_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.rooms.char", str); _save_prefs(); } void prefs_clear_roster_room_char(void) { g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.rooms.char", NULL); _save_prefs(); } char prefs_get_roster_room_private_char(void) { char result = 0; char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.rooms.private.char", NULL); if (!resultstr) { result = 0; } else { result = resultstr[0]; } free(resultstr); return result; } void prefs_set_roster_room_private_char(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.rooms.private.char", str); _save_prefs(); } void prefs_clear_roster_room_private_char(void) { g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.rooms.pruvate.char", NULL); _save_prefs(); } gint prefs_get_roster_contact_indent(void) { if (!g_key_file_has_key(prefs, PREF_GROUP_UI, "roster.contact.indent", NULL)) { return 2; } gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.contact.indent", NULL); if (result < 0) { result = 0; } return result; } void prefs_set_roster_contact_indent(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.contact.indent", value); _save_prefs(); } gint prefs_get_roster_resource_indent(void) { if (!g_key_file_has_key(prefs, PREF_GROUP_UI, "roster.resource.indent", NULL)) { return 2; } gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.resource.indent", NULL); if (result < 0) { result = 0; } return result; } void prefs_set_roster_resource_indent(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.resource.indent", value); _save_prefs(); } gint prefs_get_roster_presence_indent(void) { if (!g_key_file_has_key(prefs, PREF_GROUP_UI, "roster.presence.indent", NULL)) { return 2; } gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.presence.indent", NULL); if (result < -1) { result = 0; } return result; } void prefs_set_roster_presence_indent(gint value) { g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.presence.indent", value); _save_prefs(); } gboolean prefs_add_room_notify_trigger(const char * const text) { gboolean res = conf_string_list_add(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", text); _save_prefs(); if (res) { autocomplete_add(room_trigger_ac, text); } return res; } gboolean prefs_remove_room_notify_trigger(const char * const text) { gboolean res = conf_string_list_remove(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", text); _save_prefs(); if (res) { autocomplete_remove(room_trigger_ac, text); } return res; } GList* prefs_get_room_notify_triggers(void) { GList *result = NULL; gsize len = 0; gchar **triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL); int i; for (i = 0; i < len; i++) { result = g_list_append(result, strdup(triggers[i])); } g_strfreev(triggers); return result; } gboolean prefs_add_alias(const char *const name, const char *const value) { if (g_key_file_has_key(prefs, PREF_GROUP_ALIAS, name, NULL)) { return FALSE; } else { g_key_file_set_string(prefs, PREF_GROUP_ALIAS, name, value); _save_prefs(); return TRUE; } } char* prefs_get_alias(const char *const name) { return g_key_file_get_string(prefs, PREF_GROUP_ALIAS, name, NULL); } gboolean prefs_remove_alias(const char *const name) { if (!g_key_file_has_key(prefs, PREF_GROUP_ALIAS, name, NULL)) { return FALSE; } else { g_key_file_remove_key(prefs, PREF_GROUP_ALIAS, name, NULL); _save_prefs(); return TRUE; } } static gint _alias_cmp(gconstpointer *p1, gconstpointer *p2) { ProfAlias *alias1 = (ProfAlias*)p1; ProfAlias *alias2 = (ProfAlias*)p2; return strcmp(alias1->name, alias2->name); } GList* prefs_get_aliases(void) { if (!g_key_file_has_group(prefs, PREF_GROUP_ALIAS)) { return NULL; } else { GList *result = NULL; gsize len; gchar **keys = g_key_file_get_keys(prefs, PREF_GROUP_ALIAS, &len, NULL); int i; for (i = 0; i < len; i++) { char *name = keys[i]; char *value = g_key_file_get_string(prefs, PREF_GROUP_ALIAS, name, NULL); if (value) { ProfAlias *alias = malloc(sizeof(struct prof_alias_t)); alias->name = strdup(name); alias->value = strdup(value); free(value); result = g_list_insert_sorted(result, alias, (GCompareFunc)_alias_cmp); } } g_strfreev(keys); return result; } } gchar* prefs_get_inputrc(void) { gchar *xdg_config = xdg_get_config_home(); GString *inputrc_file = g_string_new(xdg_config); g_free(xdg_config); g_string_append(inputrc_file, "/profanity/inputrc"); if (g_file_test(inputrc_file->str, G_FILE_TEST_IS_REGULAR)) { gchar *result = strdup(inputrc_file->str); g_string_free(inputrc_file, TRUE); return result; } return NULL; } void _free_alias(ProfAlias *alias) { FREE_SET_NULL(alias->name); FREE_SET_NULL(alias->value); FREE_SET_NULL(alias); } void prefs_free_aliases(GList *aliases) { g_list_free_full(aliases, (GDestroyNotify)_free_alias); } static void _save_prefs(void) { gsize g_data_size; gchar *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL); gchar *xdg_config = xdg_get_config_home(); GString *base_str = g_string_new(xdg_config); g_string_append(base_str, "/profanity/"); gchar *true_loc = get_file_or_linked(prefs_loc, base_str->str); g_file_set_contents(true_loc, g_prefs_data, g_data_size, NULL); g_chmod(prefs_loc, S_IRUSR | S_IWUSR); g_free(xdg_config); free(true_loc); g_free(g_prefs_data); g_string_free(base_str, TRUE); } static gchar* _get_preferences_file(void) { gchar *xdg_config = xdg_get_config_home(); GString *prefs_file = g_string_new(xdg_config); g_string_append(prefs_file, "/profanity/profrc"); gchar *result = strdup(prefs_file->str); g_free(xdg_config); g_string_free(prefs_file, TRUE); return result; } // get the preference group for a specific preference // for example the PREF_BEEP setting ("beep" in .profrc, see _get_key) belongs // to the [ui] section. static const char* _get_group(preference_t pref) { switch (pref) { case PREF_SPLASH: case PREF_BEEP: case PREF_THEME: case PREF_VERCHECK: case PREF_TITLEBAR_SHOW: case PREF_TITLEBAR_GOODBYE: case PREF_FLASH: case PREF_INTYPE: case PREF_HISTORY: case PREF_OCCUPANTS: case PREF_OCCUPANTS_JID: case PREF_STATUSES: case PREF_STATUSES_CONSOLE: case PREF_STATUSES_CHAT: case PREF_STATUSES_MUC: case PREF_MUC_PRIVILEGES: case PREF_PRESENCE: case PREF_WRAP: case PREF_WINS_AUTO_TIDY: case PREF_TIME_CONSOLE: case PREF_TIME_CHAT: case PREF_TIME_MUC: case PREF_TIME_MUCCONFIG: case PREF_TIME_PRIVATE: case PREF_TIME_XMLCONSOLE: case PREF_TIME_STATUSBAR: case PREF_TIME_LASTACTIVITY: case PREF_ROSTER: case PREF_ROSTER_OFFLINE: case PREF_ROSTER_RESOURCE: case PREF_ROSTER_PRESENCE: case PREF_ROSTER_STATUS: case PREF_ROSTER_EMPTY: case PREF_ROSTER_BY: case PREF_ROSTER_ORDER: case PREF_ROSTER_UNREAD: case PREF_ROSTER_COUNT: case PREF_ROSTER_COUNT_ZERO: case PREF_ROSTER_PRIORITY: case PREF_ROSTER_WRAP: case PREF_ROSTER_RESOURCE_JOIN: case PREF_ROSTER_CONTACTS: case PREF_ROSTER_ROOMS: case PREF_ROSTER_ROOMS_POS: case PREF_ROSTER_ROOMS_BY: case PREF_ROSTER_ROOMS_ORDER: case PREF_ROSTER_ROOMS_UNREAD: case PREF_ROSTER_PRIVATE: case PREF_RESOURCE_TITLE: case PREF_RESOURCE_MESSAGE: case PREF_ENC_WARN: case PREF_INPBLOCK_DYNAMIC: case PREF_TLS_SHOW: case PREF_CONSOLE_MUC: case PREF_CONSOLE_PRIVATE: case PREF_CONSOLE_CHAT: return PREF_GROUP_UI; case PREF_STATES: case PREF_OUTTYPE: return PREF_GROUP_CHATSTATES; case PREF_NOTIFY_TYPING: case PREF_NOTIFY_TYPING_CURRENT: case PREF_NOTIFY_CHAT: case PREF_NOTIFY_CHAT_CURRENT: case PREF_NOTIFY_CHAT_TEXT: case PREF_NOTIFY_ROOM: case PREF_NOTIFY_ROOM_MENTION: case PREF_NOTIFY_ROOM_TRIGGER: case PREF_NOTIFY_ROOM_CURRENT: case PREF_NOTIFY_ROOM_TEXT: case PREF_NOTIFY_INVITE: case PREF_NOTIFY_SUB: return PREF_GROUP_NOTIFICATIONS; case PREF_CHLOG: case PREF_GRLOG: case PREF_LOG_ROTATE: case PREF_LOG_SHARED: return PREF_GROUP_LOGGING; case PREF_AUTOAWAY_CHECK: case PREF_AUTOAWAY_MODE: case PREF_AUTOAWAY_MESSAGE: case PREF_AUTOXA_MESSAGE: case PREF_LASTACTIVITY: return PREF_GROUP_PRESENCE; case PREF_CONNECT_ACCOUNT: case PREF_DEFAULT_ACCOUNT: case PREF_CARBONS: case PREF_RECEIPTS_SEND: case PREF_RECEIPTS_REQUEST: case PREF_TLS_CERTPATH: return PREF_GROUP_CONNECTION; case PREF_OTR_LOG: case PREF_OTR_POLICY: return PREF_GROUP_OTR; case PREF_PGP_LOG: return PREF_GROUP_PGP; default: return NULL; } } // get the key used in .profrc for the preference // for example the PREF_AUTOAWAY_MODE maps to "autoaway.mode" in .profrc static const char* _get_key(preference_t pref) { switch (pref) { case PREF_SPLASH: return "splash"; case PREF_BEEP: return "beep"; case PREF_THEME: return "theme"; case PREF_VERCHECK: return "vercheck"; case PREF_TITLEBAR_SHOW: return "titlebar.show"; case PREF_TITLEBAR_GOODBYE: return "titlebar.goodbye"; case PREF_FLASH: return "flash"; case PREF_INTYPE: return "intype"; case PREF_HISTORY: return "history"; case PREF_CARBONS: return "carbons"; case PREF_RECEIPTS_SEND: return "receipts.send"; case PREF_RECEIPTS_REQUEST: return "receipts.request"; case PREF_OCCUPANTS: return "occupants"; case PREF_OCCUPANTS_JID: return "occupants.jid"; case PREF_MUC_PRIVILEGES: return "privileges"; case PREF_STATUSES: return "statuses"; case PREF_STATUSES_CONSOLE: return "statuses.console"; case PREF_STATUSES_CHAT: return "statuses.chat"; case PREF_STATUSES_MUC: return "statuses.muc"; case PREF_STATES: return "enabled"; case PREF_OUTTYPE: return "outtype"; case PREF_NOTIFY_TYPING: return "typing"; case PREF_NOTIFY_TYPING_CURRENT: return "typing.current"; case PREF_NOTIFY_CHAT: return "message"; case PREF_NOTIFY_CHAT_CURRENT: return "message.current"; case PREF_NOTIFY_CHAT_TEXT: return "message.text"; case PREF_NOTIFY_ROOM: return "room"; case PREF_NOTIFY_ROOM_TRIGGER: return "room.trigger"; case PREF_NOTIFY_ROOM_MENTION: return "room.mention"; case PREF_NOTIFY_ROOM_CURRENT: return "room.current"; case PREF_NOTIFY_ROOM_TEXT: return "room.text"; case PREF_NOTIFY_INVITE: return "invite"; case PREF_NOTIFY_SUB: return "sub"; case PREF_CHLOG: return "chlog"; case PREF_GRLOG: return "grlog"; case PREF_AUTOAWAY_CHECK: return "autoaway.check"; case PREF_AUTOAWAY_MODE: return "autoaway.mode"; case PREF_AUTOAWAY_MESSAGE: return "autoaway.awaymessage"; case PREF_AUTOXA_MESSAGE: return "autoaway.xamessage"; case PREF_CONNECT_ACCOUNT: return "account"; case PREF_DEFAULT_ACCOUNT: return "defaccount"; case PREF_OTR_LOG: return "log"; case PREF_OTR_POLICY: return "policy"; case PREF_LOG_ROTATE: return "rotate"; case PREF_LOG_SHARED: return "shared"; case PREF_PRESENCE: return "presence"; case PREF_WRAP: return "wrap"; case PREF_WINS_AUTO_TIDY: return "wins.autotidy"; case PREF_TIME_CONSOLE: return "time.console"; case PREF_TIME_CHAT: return "time.chat"; case PREF_TIME_MUC: return "time.muc"; case PREF_TIME_MUCCONFIG: return "time.mucconfig"; case PREF_TIME_PRIVATE: return "time.private"; case PREF_TIME_XMLCONSOLE: return "time.xmlconsole"; case PREF_TIME_STATUSBAR: return "time.statusbar"; case PREF_TIME_LASTACTIVITY: return "time.lastactivity"; case PREF_ROSTER: return "roster"; case PREF_ROSTER_OFFLINE: return "roster.offline"; case PREF_ROSTER_RESOURCE: return "roster.resource"; case PREF_ROSTER_PRESENCE: return "roster.presence"; case PREF_ROSTER_STATUS: return "roster.status"; case PREF_ROSTER_EMPTY: return "roster.empty"; case PREF_ROSTER_BY: return "roster.by"; case PREF_ROSTER_ORDER: return "roster.order"; case PREF_ROSTER_UNREAD: return "roster.unread"; case PREF_ROSTER_COUNT: return "roster.count"; case PREF_ROSTER_COUNT_ZERO: return "roster.count.zero"; case PREF_ROSTER_PRIORITY: return "roster.priority"; case PREF_ROSTER_WRAP: return "roster.wrap"; case PREF_ROSTER_RESOURCE_JOIN: return "roster.resource.join"; case PREF_ROSTER_CONTACTS: return "roster.contacts"; case PREF_ROSTER_ROOMS: return "roster.rooms"; case PREF_ROSTER_ROOMS_POS: return "roster.rooms.pos"; case PREF_ROSTER_ROOMS_BY: return "roster.rooms.by"; case PREF_ROSTER_ROOMS_ORDER: return "roster.rooms.order"; case PREF_ROSTER_ROOMS_UNREAD: return "roster.rooms.unread"; case PREF_ROSTER_PRIVATE: return "roster.private"; case PREF_RESOURCE_TITLE: return "resource.title"; case PREF_RESOURCE_MESSAGE: return "resource.message"; case PREF_INPBLOCK_DYNAMIC: return "inpblock.dynamic"; case PREF_ENC_WARN: return "enc.warn"; case PREF_PGP_LOG: return "log"; case PREF_TLS_CERTPATH: return "tls.certpath"; case PREF_TLS_SHOW: return "tls.show"; case PREF_LASTACTIVITY: return "lastactivity"; case PREF_CONSOLE_MUC: return "console.muc"; case PREF_CONSOLE_PRIVATE: return "console.private"; case PREF_CONSOLE_CHAT: return "console.chat"; default: return NULL; } } // the default setting for a boolean type preference // if it is not specified in .profrc static gboolean _get_default_boolean(preference_t pref) { switch (pref) { case PREF_ENC_WARN: case PREF_AUTOAWAY_CHECK: case PREF_LOG_ROTATE: case PREF_LOG_SHARED: case PREF_NOTIFY_CHAT: case PREF_NOTIFY_CHAT_CURRENT: case PREF_NOTIFY_ROOM: case PREF_NOTIFY_ROOM_CURRENT: case PREF_NOTIFY_TYPING: case PREF_NOTIFY_TYPING_CURRENT: case PREF_NOTIFY_SUB: case PREF_NOTIFY_INVITE: case PREF_SPLASH: case PREF_OCCUPANTS: case PREF_MUC_PRIVILEGES: case PREF_PRESENCE: case PREF_WRAP: case PREF_WINS_AUTO_TIDY: case PREF_INPBLOCK_DYNAMIC: case PREF_RESOURCE_TITLE: case PREF_RESOURCE_MESSAGE: case PREF_ROSTER: case PREF_ROSTER_OFFLINE: case PREF_ROSTER_EMPTY: case PREF_ROSTER_COUNT_ZERO: case PREF_ROSTER_PRIORITY: case PREF_ROSTER_RESOURCE_JOIN: case PREF_ROSTER_CONTACTS: case PREF_ROSTER_ROOMS: case PREF_TLS_SHOW: case PREF_LASTACTIVITY: return TRUE; default: return FALSE; } } // the default setting for a string type preference // if it is not specified in .profrc static char* _get_default_string(preference_t pref) { switch (pref) { case PREF_AUTOAWAY_MODE: return "off"; case PREF_OTR_LOG: return "redact"; case PREF_OTR_POLICY: return "manual"; case PREF_STATUSES_CONSOLE: case PREF_STATUSES_CHAT: case PREF_STATUSES_MUC: return "all"; case PREF_ROSTER_BY: return "presence"; case PREF_ROSTER_COUNT: return "unread"; case PREF_ROSTER_ORDER: return "presence"; case PREF_ROSTER_UNREAD: return "after"; case PREF_ROSTER_ROOMS_POS: return "last"; case PREF_ROSTER_ROOMS_BY: return "none"; case PREF_ROSTER_ROOMS_ORDER: return "name"; case PREF_ROSTER_ROOMS_UNREAD: return "after"; case PREF_ROSTER_PRIVATE: return "room"; case PREF_TIME_CONSOLE: return "%H:%M:%S"; case PREF_TIME_CHAT: return "%H:%M:%S"; case PREF_TIME_MUC: return "%H:%M:%S"; case PREF_TIME_MUCCONFIG: return "%H:%M:%S"; case PREF_TIME_PRIVATE: return "%H:%M:%S"; case PREF_TIME_XMLCONSOLE: return "%H:%M:%S"; case PREF_TIME_STATUSBAR: return "%H:%M"; case PREF_TIME_LASTACTIVITY: return "%d/%m/%y %H:%M:%S"; case PREF_PGP_LOG: return "redact"; case PREF_CONSOLE_MUC: case PREF_CONSOLE_PRIVATE: case PREF_CONSOLE_CHAT: return "all"; default: return NULL; } }