]> jfr.im git - solanum.git/blame - tests/client_util.c
um_callerid: Only people can have common channels
[solanum.git] / tests / client_util.c
CommitLineData
d2b5f411
SA
1/*
2 * client_util.c: Utility functions for making test clients
3 * Copyright 2017 Simon Arlott
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 * USA
19 */
20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include "tap/basic.h"
25
d2b5f411
SA
26#include "client_util.h"
27
60f1d711
SA
28#include "hash.h"
29#include "s_newconf.h"
958c354c
SA
30#include "parse.h"
31#include "listener.h"
60f1d711 32
d2b5f411
SA
33#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
34
958c354c
SA
35static struct Listener fake_listener = {
36 .next = NULL,
37 .name = "fake",
38 .F = NULL,
39 .ref_count = 0,
40 .active = 1,
41 .ssl = 1,
42 .defer_accept = 0,
43 .sctp = false,
44 .wsock = 0,
45 .addr = {
46 { .ss_family = AF_INET6 },
47 { .ss_family = AF_INET6 },
48 },
49 .vhost = { "fake" },
50};
51
52struct Client *make_local_unknown(void)
53{
54 struct Client *client;
55
56 client = make_client(NULL);
57 rb_dlinkMoveNode(&client->localClient->tnode, &unknown_list, &lclient_list);
58 client->servptr = &me;
59 rb_dlinkAdd(client, &client->lnode, &client->servptr->serv->users);
60 client->localClient->listener = &fake_listener;
61 client->preClient->auth.accepted = true;
5409fbc0 62 client->localClient->localflags |= LFLAGS_FAKE;
958c354c
SA
63
64 return client;
65}
66
d2b5f411
SA
67struct Client *make_local_person(void)
68{
69 return make_local_person_nick(TEST_NICK);
70}
71
72struct Client *make_local_person_nick(const char *nick)
73{
74 return make_local_person_full(nick, TEST_USERNAME, TEST_HOSTNAME, TEST_IP, TEST_REALNAME);
75}
76
77struct Client *make_local_person_full(const char *nick, const char *username, const char *hostname, const char *ip, const char *realname)
78{
79 struct Client *client;
80
958c354c 81 client = make_local_unknown();
d2b5f411 82 make_user(client);
60f1d711 83 SetClient(client);
d2b5f411 84
17809d2d 85 rb_inet_pton_sock(ip, &client->localClient->ip);
d2b5f411
SA
86 rb_strlcpy(client->name, nick, sizeof(client->name));
87 rb_strlcpy(client->username, username, sizeof(client->username));
88 rb_strlcpy(client->host, hostname, sizeof(client->host));
89 rb_inet_ntop_sock((struct sockaddr *)&client->localClient->ip, client->sockhost, sizeof(client->sockhost));
90 rb_strlcpy(client->info, realname, sizeof(client->info));
91
60f1d711
SA
92 add_to_client_hash(client->name, client);
93
d2b5f411
SA
94 return client;
95}
96
6af47466
SA
97void make_local_person_oper(struct Client *client)
98{
99 rb_dlinkAddAlloc(client, &local_oper_list);
100 rb_dlinkAddAlloc(client, &oper_list);
101 SetOper(client);
4af85fed
EK
102 struct PrivilegeSet *p = privilegeset_set_new("test", "test:test", 0);
103 client->user->privset = privilegeset_ref(p);
6af47466
SA
104}
105
d2b5f411
SA
106void remove_local_person(struct Client *client)
107{
108 exit_client(NULL, client, &me, "Test client removed");
109}
110
60f1d711
SA
111struct Client *make_remote_server(struct Client *uplink)
112{
113 return make_remote_server_name(uplink, TEST_SERVER_NAME);
114}
115
116struct Client *make_remote_server_name(struct Client *uplink, const char *name)
117{
118 return make_remote_server_full(uplink, name, "");
119}
120
121struct Client *make_remote_server_full(struct Client *uplink, const char *name, const char *id)
122{
123 struct Client *client;
124
125 client = make_client(NULL);
126 client->servptr = uplink;
5409fbc0 127 client->localClient->localflags |= LFLAGS_FAKE;
60f1d711
SA
128
129 attach_server_conf(client, find_server_conf(name));
130
131 rb_strlcpy(client->name, name, sizeof(client->name));
132 rb_strlcpy(client->id, id, sizeof(client->id));
133
134 rb_dlinkAdd(client, &client->lnode, &uplink->serv->servers);
135 rb_dlinkMoveNode(&client->localClient->tnode, &unknown_list, &serv_list);
136 rb_dlinkAddTailAlloc(client, &global_serv_list);
137
138 make_server(client);
139 SetServer(client);
140
141 add_to_client_hash(client->name, client);
e7010268
SA
142 if (strlen(id))
143 add_to_id_hash(client->id, client);
60f1d711
SA
144
145 return client;
146}
147
148struct Client *make_remote_person(struct Client *server)
149{
150 return make_remote_person_nick(server, TEST_REMOTE_NICK);
151}
152
153struct Client *make_remote_person_nick(struct Client *server, const char *nick)
154{
155 return make_remote_person_full(server, nick, TEST_USERNAME, TEST_HOSTNAME, TEST_IP, TEST_REALNAME);
156}
157
158struct Client *make_remote_person_full(struct Client *server, const char *nick, const char *username, const char *hostname, const char *ip, const char *realname)
159{
160 struct Client *client;
17809d2d 161 struct sockaddr_storage addr;
60f1d711
SA
162
163 client = make_client(server);
164 make_user(client);
165 SetRemoteClient(client);
166
167 client->servptr = server;
168 rb_dlinkAdd(server, &server->lnode, &server->servptr->serv->users);
169
170 rb_inet_pton_sock(ip, &addr);
171 rb_strlcpy(client->name, nick, sizeof(client->name));
172 rb_strlcpy(client->username, username, sizeof(client->username));
173 rb_strlcpy(client->host, hostname, sizeof(client->host));
17809d2d 174 rb_inet_ntop_sock((struct sockaddr *)&addr, client->sockhost, sizeof(client->sockhost));
60f1d711
SA
175 rb_strlcpy(client->info, realname, sizeof(client->info));
176
177 add_to_client_hash(nick, client);
178 add_to_hostname_hash(client->host, client);
179
180 return client;
181}
182
183void make_remote_person_oper(struct Client *client)
184{
185 rb_dlinkAddAlloc(client, &oper_list);
186 SetOper(client);
187}
188
189void remove_remote_person(struct Client *client)
190{
191 exit_client(client, client->servptr, client->servptr, "Test client removed");
192}
193
194void remove_remote_server(struct Client *server)
195{
196 exit_client(server, server, server->servptr, "Test server removed");
197}
198
54f75d36
SA
199struct Channel *make_channel(void)
200{
201 return allocate_channel(TEST_CHANNEL);
202}
60f1d711 203
d2b5f411
SA
204char *get_client_sendq(const struct Client *client)
205{
206 static char buf[EXT_BUFSIZE + sizeof(CRLF)];
207
208 if (rb_linebuf_len(&client->localClient->buf_sendq)) {
209 int ret;
210
211 memset(buf, 0, sizeof(buf));
212 ret = rb_linebuf_get(&client->localClient->buf_sendq, buf, sizeof(buf), 0, 1);
213
54f75d36 214 if (ok(ret > 0, MSG)) {
d2b5f411
SA
215 return buf;
216 } else {
217 return "<get_client_sendq error>";
218 }
219 }
220
221 return "";
222}
223
958c354c
SA
224void client_util_parse(struct Client *client, const char *message)
225{
226 char *copy = rb_strdup(message);
227
228 parse(client, copy, copy+strlen(copy));
229
230 rb_free(copy);
231}
232
d2b5f411
SA
233void client_util_init(void)
234{
235}
236
237void client_util_free(void)
238{
239}