Coverage Report

Created: 2025-11-09 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/src/fe-common/irc/fe-irc-server.c
Line
Count
Source
1
/*
2
 fe-irc-server.c : irssi
3
4
    Copyright (C) 1999-2000 Timo Sirainen
5
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
10
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
16
    You should have received a copy of the GNU General Public License along
17
    with this program; if not, write to the Free Software Foundation, Inc.,
18
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
*/
20
21
#include "module.h"
22
#include <irssi/src/fe-common/irc/module-formats.h>
23
#include <irssi/src/core/signals.h>
24
#include <irssi/src/core/commands.h>
25
#include <irssi/src/core/misc.h>
26
27
#include <irssi/src/core/servers-setup.h>
28
29
#include <irssi/src/core/levels.h>
30
#include <irssi/src/irc/core/irc-chatnets.h>
31
#include <irssi/src/irc/core/irc-servers.h>
32
#include <irssi/src/irc/core/irc-channels.h>
33
#include <irssi/src/core/servers-reconnect.h>
34
#include <irssi/src/irc/core/irc-servers-setup.h>
35
36
#include <irssi/src/fe-common/core/fe-windows.h>
37
#include <irssi/src/fe-common/core/printtext.h>
38
39
const char *get_visible_target(IRC_SERVER_REC *server, const char *target)
40
301k
{
41
301k
  IRC_CHANNEL_REC *channel;
42
43
301k
  if (*target == '!') {
44
    /* visible_name of !channels is different - don't bother
45
       checking other types for now, they'll just slow up */
46
134k
    channel = irc_channel_find(server, target);
47
134k
    if (channel != NULL)
48
107k
      return channel->visible_name;
49
134k
  }
50
51
194k
  return target;
52
301k
}
53
54
/* SYNTAX: SERVER ADD|MODIFY [-4 | -6] [-cap | -nocap] [-tls_cert <cert>] [-tls_pkey <pkey>]
55
                             [-tls_pass <password>] [-tls_verify] [-tls_cafile <cafile>]
56
                             [-tls_capath <capath>] [-tls_ciphers <list>] [-tls | -notls]
57
                             [-starttls | -nostarttls | -disallow_starttls | -nodisallow_starttls]
58
                             [-auto | -noauto] [-network <network>] [-host <hostname>]
59
                             [-cmdspeed <ms>] [-cmdmax <count>] [-port <port>] <address> [<port>
60
                             [<password>]] */
61
/* NOTE: -network replaces the old -ircnet flag. */
62
static void sig_server_add_fill(IRC_SERVER_SETUP_REC *rec,
63
        GHashTable *optlist)
64
0
{
65
0
        IRC_CHATNET_REC *ircnet;
66
0
  char *value;
67
68
0
  value = g_hash_table_lookup(optlist, "network");
69
  /* For backwards compatibility, also allow the old name 'ircnet'.
70
     But of course only if -network was not given. */
71
0
  if (!value)
72
0
    value = g_hash_table_lookup(optlist, "ircnet");
73
74
0
  if (value != NULL) {
75
0
    g_free_and_null(rec->chatnet);
76
0
    if (*value != '\0') {
77
0
      ircnet = ircnet_find(value);
78
0
      rec->chatnet = ircnet != NULL ?
79
0
        g_strdup(ircnet->name) : g_strdup(value);
80
0
    }
81
0
  }
82
83
0
  value = g_hash_table_lookup(optlist, "cmdspeed");
84
0
  if (value != NULL && *value != '\0') rec->cmd_queue_speed = atoi(value);
85
0
  value = g_hash_table_lookup(optlist, "cmdmax");
86
0
  if (value != NULL && *value != '\0') rec->max_cmds_at_once = atoi(value);
87
0
  value = g_hash_table_lookup(optlist, "querychans");
88
0
  if (value != NULL && *value != '\0') rec->max_query_chans = atoi(value);
89
0
  if (g_hash_table_lookup(optlist, "nodisallow_starttls") ||
90
0
      g_hash_table_lookup(optlist, "nostarttls"))
91
0
    rec->starttls = STARTTLS_NOTSET;
92
0
  if (g_hash_table_lookup(optlist, "disallow_starttls"))
93
0
    rec->starttls = STARTTLS_DISALLOW;
94
0
  if (g_hash_table_lookup(optlist, "starttls")) {
95
0
    rec->starttls = STARTTLS_ENABLED;
96
0
    rec->use_tls = 0;
97
0
  }
98
0
  if (g_hash_table_lookup(optlist, "nocap"))
99
0
    rec->no_cap = 1;
100
0
  if (g_hash_table_lookup(optlist, "cap"))
101
0
    rec->no_cap = 0;
102
0
}
103
104
static void sig_server_waiting_info(IRC_SERVER_REC *server, const char *version)
105
0
{
106
0
  if (!IS_IRC_SERVER(server))
107
0
    return;
108
109
0
  printformat(server, NULL, MSGLEVEL_CLIENTCRAP, IRCTXT_SERVER_WAITING_CAP_LS, server,
110
0
              version);
111
0
}
112
113
/* SYNTAX: SERVER LIST */
114
static void cmd_server_list(const char *data)
115
0
{
116
0
  GString *str;
117
0
  GSList *tmp;
118
119
0
  str = g_string_new(NULL);
120
0
  printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, IRCTXT_SETUPSERVER_HEADER);
121
0
  for (tmp = setupservers; tmp != NULL; tmp = tmp->next) {
122
0
    IRC_SERVER_SETUP_REC *rec = tmp->data;
123
124
0
    if (!IS_IRC_SERVER_SETUP(rec))
125
0
                        continue;
126
127
0
    g_string_truncate(str, 0);
128
0
    if (rec->password != NULL)
129
0
      g_string_append(str, "(pass), ");
130
0
    if (rec->autoconnect)
131
0
      g_string_append(str, "autoconnect, ");
132
0
    if (rec->no_proxy)
133
0
      g_string_append(str, "noproxy, ");
134
0
    if (rec->no_cap)
135
0
      g_string_append(str, "nocap, ");
136
0
    if (rec->starttls == STARTTLS_DISALLOW)
137
0
      g_string_append(str, "disallow_starttls, ");
138
0
    if (rec->starttls == STARTTLS_ENABLED)
139
0
      g_string_append(str, "starttls, ");
140
0
    if (rec->use_tls)
141
0
      g_string_append(str, "tls, ");
142
0
    if (rec->tls_cert) {
143
0
      g_string_append_printf(str, "tls_cert: %s, ", rec->tls_cert);
144
0
      if (rec->tls_pkey)
145
0
        g_string_append_printf(str, "tls_pkey: %s, ", rec->tls_pkey);
146
0
      if (rec->tls_pass)
147
0
        g_string_append_printf(str, "(pass), ");
148
0
    }
149
0
    if (!rec->tls_verify)
150
0
      g_string_append(str, "notls_verify, ");
151
0
    if (rec->tls_cafile)
152
0
      g_string_append_printf(str, "tls_cafile: %s, ", rec->tls_cafile);
153
0
    if (rec->tls_capath)
154
0
      g_string_append_printf(str, "tls_capath: %s, ", rec->tls_capath);
155
0
    if (rec->tls_ciphers)
156
0
      g_string_append_printf(str, "tls_ciphers: %s, ", rec->tls_ciphers);
157
0
    if (rec->tls_pinned_cert)
158
0
      g_string_append_printf(str, "tls_pinned_cert: %s, ", rec->tls_pinned_cert);
159
0
    if (rec->tls_pinned_pubkey)
160
0
      g_string_append_printf(str, "tls_pinned_pubkey: %s, ",
161
0
                             rec->tls_pinned_pubkey);
162
163
0
    if (rec->max_cmds_at_once > 0)
164
0
      g_string_append_printf(str, "cmdmax: %d, ", rec->max_cmds_at_once);
165
0
    if (rec->cmd_queue_speed > 0)
166
0
      g_string_append_printf(str, "cmdspeed: %d, ", rec->cmd_queue_speed);
167
0
    if (rec->max_query_chans > 0)
168
0
      g_string_append_printf(str, "querychans: %d, ", rec->max_query_chans);
169
0
    if (rec->own_host != NULL)
170
0
      g_string_append_printf(str, "host: %s, ", rec->own_host);
171
172
0
    if (str->len > 1) g_string_truncate(str, str->len-2);
173
0
    printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, IRCTXT_SETUPSERVER_LINE,
174
0
          rec->address, rec->port,
175
0
          rec->chatnet == NULL ? "" : rec->chatnet,
176
0
          str->str);
177
0
  }
178
0
  printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, IRCTXT_SETUPSERVER_FOOTER);
179
0
  g_string_free(str, TRUE);
180
0
}
181
182
void fe_irc_server_init(void)
183
2
{
184
2
  signal_add("server add fill", (SIGNAL_FUNC) sig_server_add_fill);
185
2
  signal_add("server waiting cap ls", (SIGNAL_FUNC) sig_server_waiting_info);
186
2
  command_bind("server list", NULL, (SIGNAL_FUNC) cmd_server_list);
187
188
2
  command_set_options("server add",
189
2
                      "-ircnet -network -cmdspeed -cmdmax -querychans starttls "
190
2
                      "nostarttls disallow_starttls nodisallow_starttls cap nocap");
191
2
  command_set_options("server modify",
192
2
                      "-ircnet -network -cmdspeed -cmdmax -querychans starttls nostarttls "
193
2
                      "disallow_starttls nodisallow_starttls cap nocap");
194
2
}
195
196
void fe_irc_server_deinit(void)
197
0
{
198
0
  signal_remove("server add fill", (SIGNAL_FUNC) sig_server_add_fill);
199
0
  signal_remove("server waiting cap ls", (SIGNAL_FUNC) sig_server_waiting_info);
200
  command_unbind("server list", (SIGNAL_FUNC) cmd_server_list);
201
0
}