]> jfr.im git - irc/thales.git/blob - src/irc.c
Fix bugs in inheritance.
[irc/thales.git] / src / irc.c
1 /* Main irc module of GNU Thales. Copyright (C)
2 2012 Free Software Foundation, Inc. This file is part of GNU Thales.
3
4 GNU Thales is free software; you can redistribute it and/or modify it under the
5 terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 3 of the License, or (at your option) any later
7 version.
8
9 GNU Make is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16 #include "irc.h"
17 #include <stdio.h>
18 #include <xalloc.h>
19 #include <envz.h>
20 #include <string.h>
21
22 static inline bool
23 config_error(const char *msg)
24 {
25 printf("config error: %s\n");
26 return false;
27 }
28
29 static inline int
30 count_occurences(const char *str, char c)
31 {
32 int count = 0;
33
34 for (const char *p = str; *p; ++p)
35 count += *p == c;
36 return count;
37 }
38 static char**
39 split_at(const char *str, char delim)
40 {
41 char **result;
42 const char *delim_ptr = str;
43 /* Alloc NULL-terminated array of strings */
44 result = xcalloc (count_occurences(str, delim)+2, sizeof(char *));
45
46 for (int index = 0; *(delim_ptr = strchr(str, delim)); str = delim)
47 result[index] = strndup(str, delim_ptr - str -1);
48
49 return result;
50 }
51
52 bool
53 parse_irc_config(struct irc_options *irc_opts, const struct envz *env)
54 {
55 const char *host = envz_get(env->envz, env->envz_len, "host");
56 const char *str_port = envz_get(env->envz, env->envz_len, "port");
57 const char *str_channels = envz_get(env->envz, env->envz_len, "channels");
58 irc_opts->host = host;
59 irc_opts->port = str_port ? atoi(str_port) : 0;
60
61 if (!irc_opts->host)
62 return config_error("no host specified");
63 if (!irc_opts->port)
64 return config_error("incorrect port");
65 if (!str_channels)
66 return config_error("No channels specified");
67 irc_opts->channels = split_at(str_channels, ',');
68
69 return true;
70 }