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
|
/*
* tests-record.cpp - record and search in messages displayed
*
* Copyright (C) 2023-2024 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "tests-record.h"
extern "C"
{
#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H
#endif
#include "src/core/weechat.h"
#include "src/core/core-arraylist.h"
#include "src/core/core-hashtable.h"
#include "src/core/core-hook.h"
#include "src/core/core-string.h"
#include "src/gui/gui-color.h"
}
/* recording of messages: to test if a message is actually displayed */
int record_messages = 0;
struct t_arraylist *recorded_messages = NULL;
struct t_hook *record_hook_line = NULL;
/*
* Callback used to compare two recorded messages.
*/
int
record_cmp_cb (void *data, struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
/* make C++ compiler happy */
(void) data;
(void) arraylist;
return (pointer1 < pointer2) ?
-1 : ((pointer1 > pointer2) ? 1 : 0);
}
/*
* Callback used to free a recorded message.
*/
void
record_free_cb (void *data, struct t_arraylist *arraylist, void *pointer)
{
/* make C++ compiler happy */
(void) data;
(void) arraylist;
if (pointer)
hashtable_free ((struct t_hashtable *)pointer);
}
/*
* Callback for hook line, used when message are recorded.
*/
struct t_hashtable *
record_hook_line_cb (const void *pointer, void *data, struct t_hashtable *line)
{
struct t_hashtable *hashtable;
const char *ptr_string;
/* make C++ compiler happy */
(void) pointer;
(void) data;
hashtable = hashtable_dup (line);
ptr_string = (const char *)hashtable_get (hashtable, "prefix");
hashtable_set (
hashtable,
"prefix_no_color",
(ptr_string) ? gui_color_decode (ptr_string, NULL) : NULL);
ptr_string = (const char *)hashtable_get (hashtable, "message");
hashtable_set (
hashtable,
"message_no_color",
(ptr_string) ? gui_color_decode (ptr_string, NULL) : NULL);
arraylist_add (recorded_messages, hashtable);
return NULL;
}
/*
* Starts recording of messages displayed.
*/
void
record_start ()
{
record_messages = 1;
if (recorded_messages)
{
arraylist_clear (recorded_messages);
}
else
{
recorded_messages = arraylist_new (16, 0, 1,
&record_cmp_cb, NULL,
&record_free_cb, NULL);
}
if (!record_hook_line)
{
record_hook_line = hook_line (NULL, "*", NULL, NULL,
&record_hook_line_cb, NULL, NULL);
}
}
/*
* Stops recording of messages displayed.
*/
void
record_stop ()
{
record_messages = 0;
if (record_hook_line)
{
unhook (record_hook_line);
record_hook_line = NULL;
}
}
/*
* Checks if a recorded message field matches a value.
*
* Returns:
* 1: value matches
* 0: value does NOT match
*/
int
record_match (struct t_hashtable *recorded_msg,
const char *field, const char *value)
{
const char *ptr_value;
ptr_value = (const char *)hashtable_get (recorded_msg, field);
return ((!ptr_value && !value)
|| (ptr_value && value && (strcmp (ptr_value, value) == 0)));
}
/*
* Searches if a prefix/message has been displayed in a buffer.
*
* Returns pointer to hashtable with the message found, NULL if the message
* has NOT been displayed.
*/
struct t_hashtable *
record_search (const char *buffer, const char *prefix, const char *message,
const char *tags)
{
int i, size;
struct t_hashtable *rec_msg;
size = arraylist_size (recorded_messages);
for (i = 0; i < size; i++)
{
rec_msg = (struct t_hashtable *)arraylist_get (recorded_messages, i);
if (!rec_msg)
continue;
if (record_match (rec_msg, "buffer_name", buffer)
&& record_match (rec_msg, "prefix_no_color", prefix)
&& record_match (rec_msg, "message_no_color", message)
&& (!tags || !tags[0] || record_match (rec_msg, "tags", tags)))
{
return rec_msg;
}
}
/* message not displayed */
return NULL;
}
/*
* Adds all recorded messages to the dynamic string "msg".
*/
void
record_dump (char **msg)
{
struct t_hashtable *rec_msg;
const char *ptr_buffer_name, *ptr_prefix, *ptr_msg, *ptr_tags;
int i, size;
size = arraylist_size (recorded_messages);
for (i = 0; i < size; i++)
{
rec_msg = (struct t_hashtable *)arraylist_get (recorded_messages, i);
if (!rec_msg)
continue;
ptr_buffer_name = (const char *)hashtable_get (rec_msg, "buffer_name");
ptr_prefix = (const char *)hashtable_get (rec_msg, "prefix_no_color");
ptr_msg = (const char *)hashtable_get (rec_msg, "message_no_color");
ptr_tags = (const char *)hashtable_get (rec_msg, "tags");
string_dyn_concat (msg, " ", -1);
string_dyn_concat (msg, ptr_buffer_name, -1);
string_dyn_concat (msg, ": prefix=\"", -1);
string_dyn_concat (msg, ptr_prefix, -1);
string_dyn_concat (msg, "\", message=\"", -1);
string_dyn_concat (msg, ptr_msg, -1);
string_dyn_concat (msg, "\", tags=\"", -1);
string_dyn_concat (msg, ptr_tags, -1);
string_dyn_concat (msg, "\"\n", -1);
}
}
|