libpurple/protocols/ircv3/purpleircv3parser.c

Sun, 23 Oct 2022 04:27:28 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Sun, 23 Oct 2022 04:27:28 -0500
changeset 41821
ec766a6f010a
parent 41813
86302ce7e3c1
child 41931
f35eb5adb2e0
permissions
-rw-r--r--

Properly namespace the message handlers and reorganize them

Testing Done:
Compiled

Reviewed at https://reviews.imfreedom.org/r/1949/

41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 /*
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
2 * Purple - Internet Messaging Library
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
3 * Copyright (C) Pidgin Developers <devel@pidgin.im>
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 *
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 * (at your option) any later version.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 *
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 * GNU General Public License for more details.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 *
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 * along with this program; if not, see <https://www.gnu.org/licenses/>.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 #include "purpleircv3parser.h"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
20
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 #include "purpleircv3core.h"
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
22 #include "purpleircv3messagehandlers.h"
41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
23
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
24 struct _PurpleIRCv3Parser {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
25 GObject parent;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
26
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
27 GRegex *regex_message;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28 GRegex *regex_tags;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
29
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
30 PurpleIRCv3MessageHandler fallback_handler;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
31 GHashTable *handlers;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
32 };
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
33
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
34 G_DEFINE_TYPE(PurpleIRCv3Parser, purple_ircv3_parser, G_TYPE_OBJECT)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
35
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
36 /******************************************************************************
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
37 * Helpers
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
38 *****************************************************************************/
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
39 static GHashTable *
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
40 purple_ircv3_parser_parse_tags(PurpleIRCv3Parser *parser,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
41 const gchar *tags_string, GError **error)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
42 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
43 GError *local_error = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
44 GHashTable *tags = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
45 GMatchInfo *info = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
46 gboolean matches = FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
47
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
48 tags = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
49
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
50 /* tags_string can never be NULL, because g_match_info_fetch_named always
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
51 * returns a string. So if we were passed an empty string, just return the
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
52 * empty hash table.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
53 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
54 if(*tags_string == '\0') {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
55 return tags;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
56 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
57
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
58 matches = g_regex_match_full(parser->regex_tags, tags_string, -1, 0, 0,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
59 &info, &local_error);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
60
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
61 if(local_error != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
62 g_propagate_error(error, local_error);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
64 g_match_info_unref(info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
65
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
66 return tags;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
68
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
69 if(!matches) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70 g_set_error_literal(error, PURPLE_IRCV3_DOMAIN, 0,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
71 "failed to parse tags: unknown error");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
72
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
73 g_match_info_unref(info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
74
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
75 return tags;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
77
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 while(g_match_info_matches(info)) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79 gchar *key = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
80 gchar *value = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82 key = g_match_info_fetch_named(info, "key");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83 value = g_match_info_fetch_named(info, "value");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
84
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
85 /* the hash table is created with destroy notifies for both key and
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86 * value, so there's no need to free the allocated memory right now.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
88 g_hash_table_insert(tags, key, value);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89 g_match_info_next(info, &local_error);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
90
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
91 if(local_error != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 g_propagate_error(error, local_error);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
94 break;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
95 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
96 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
97
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
98 g_match_info_unref(info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
99
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
100 return tags;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
101 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
102
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
103 static guint
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
104 purple_ircv3_parser_extract_params(PurpleIRCv3Parser *parser,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
105 GStrvBuilder *builder, const gchar *str)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
106 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
107 gchar *ptr = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
108 guint count = 0;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
109
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
110 /* Loop through str finding each space separated string. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
111 while(str != NULL && *str != '\0') {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
112 /* Look for a space. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
113 ptr = strchr(str, ' ');
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
114
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
115 /* If we found one, set it to null terminator and add the string to our
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
116 * builder.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
117 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
118 if(ptr != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
119 *ptr = '\0';
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
120 g_strv_builder_add(builder, str);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
121
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
122 /* Move str to the next character as we know there's another
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
123 * character which might be another null terminator.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
124 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
125 str = ptr + 1;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
126
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
127 /* And don't forget to increment the count... ah ah ah! */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
128 count++;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
129 } else {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
130 /* Add the remaining string. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
131 g_strv_builder_add(builder, str);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
132
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
133 /* Give the count another one, ah ah ah! */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
134 count++;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
135
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
136 /* Finally break out of the loop. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
137 break;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
138 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
139 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
140
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
141 return count;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
142 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
143
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
144 static GStrv
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
145 purple_ircv3_parser_build_params(PurpleIRCv3Parser *parser,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
146 const gchar *middle, const gchar *coda,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
147 const gchar *trailing, guint *n_params)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
148 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
149 GStrvBuilder *builder = g_strv_builder_new();
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
150 GStrv result = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
151 guint count = 0;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
152
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
153 *n_params = 0;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
154
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
155 count = purple_ircv3_parser_extract_params(parser, builder, middle);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
156 *n_params = *n_params + count;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
157
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
158 if(*coda != '\0') {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
159 g_strv_builder_add(builder, trailing);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
160 *n_params = *n_params + 1;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
161 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
162
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
163 result = g_strv_builder_end(builder);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
164
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
165 g_strv_builder_unref(builder);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
166
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
167 return result;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
168 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
169
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
170 static void
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
171 purple_ircv3_parser_add_handler(PurpleIRCv3Parser *parser,
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
172 const gchar *command,
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
173 PurpleIRCv3MessageHandler handler)
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
174 {
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
175 g_return_if_fail(PURPLE_IRCV3_IS_PARSER(parser));
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
176 g_return_if_fail(command != NULL);
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
177 g_return_if_fail(handler != NULL);
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
178
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
179 g_hash_table_insert(parser->handlers, g_strdup(command), handler);
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
180 }
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
181
41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
182 /******************************************************************************
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
183 * Handlers
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
184 *****************************************************************************/
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
185 static gboolean
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
186 purple_ircv3_fallback_handler(G_GNUC_UNUSED GHashTable *tags,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
187 G_GNUC_UNUSED const gchar *source,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
188 G_GNUC_UNUSED const gchar *command,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
189 G_GNUC_UNUSED guint n_params,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
190 G_GNUC_UNUSED GStrv params,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
191 GError **error,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
192 G_GNUC_UNUSED gpointer data)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
193 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
194 g_set_error(error, PURPLE_IRCV3_DOMAIN, 0, "no handler for command %s",
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
195 command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
196
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
197 return FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
198 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
199
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
200 /******************************************************************************
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
201 * GObject Implementation
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
202 *****************************************************************************/
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
203 static void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
204 purple_ircv3_parser_finalize(GObject *obj) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
205 PurpleIRCv3Parser *parser = PURPLE_IRCV3_PARSER(obj);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
206
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
207 g_clear_pointer(&parser->regex_message, g_regex_unref);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
208 g_clear_pointer(&parser->regex_tags, g_regex_unref);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
209
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
210 g_hash_table_destroy(parser->handlers);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
211
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
212 G_OBJECT_CLASS(purple_ircv3_parser_parent_class)->finalize(obj);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
213 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
214
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
215 static void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
216 purple_ircv3_parser_init(PurpleIRCv3Parser *parser) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
217 parser->regex_message = g_regex_new("(?:@(?<tags>[^ ]+) )?"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
218 "(?::(?<source>[^ ]+) +)?"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
219 "(?<command>[^ :]+)"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
220 "(?: +(?<middle>(?:[^ :]+(?: +[^ :]+)*)))*"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
221 "(?<coda> +:(?<trailing>.*)?)?",
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
222 0, 0, NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
223 g_assert(parser->regex_message != NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
224
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
225 parser->regex_tags = g_regex_new("(?:(?<key>[A-Za-z0-9-\\/]+)"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
226 "(?:=(?<value>[^\\r\\n;]*))?(?:;|$))",
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
227 0, 0, NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
228 g_assert(parser->regex_tags != NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
229
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
230 parser->fallback_handler = purple_ircv3_fallback_handler;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
231 parser->handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
232 NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
233 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
234
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
235 static void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
236 purple_ircv3_parser_class_init(PurpleIRCv3ParserClass *klass) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
237 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
238
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
239 obj_class->finalize = purple_ircv3_parser_finalize;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
240 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
241
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
242 /******************************************************************************
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
243 * Public API
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
244 *****************************************************************************/
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
245 PurpleIRCv3Parser *
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
246 purple_ircv3_parser_new(void) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
247 return g_object_new(PURPLE_IRCV3_TYPE_PARSER, NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
248 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
249
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
250 void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
251 purple_ircv3_parser_set_fallback_handler(PurpleIRCv3Parser *parser,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
252 PurpleIRCv3MessageHandler handler)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
253 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
254 g_return_if_fail(PURPLE_IRCV3_IS_PARSER(parser));
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
255
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
256 parser->fallback_handler = handler;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
257 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
258
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
259 gboolean
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
260 purple_ircv3_parser_parse(PurpleIRCv3Parser *parser, const gchar *buffer,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
261 GError **error, gpointer data)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
262 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
263 PurpleIRCv3MessageHandler handler = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
264 GError *local_error = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
265 GHashTable *tags = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
266 GMatchInfo *info = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
267 GStrv params = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
268 gchar *coda = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
269 gchar *command = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
270 gchar *middle = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
271 gchar *source = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
272 gchar *tags_string = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
273 gchar *trailing = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
274 gboolean matches = FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
275 gboolean result = FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
276 guint n_params = 0;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
277
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
278 g_return_val_if_fail(PURPLE_IRCV3_IS_PARSER(parser), FALSE);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
279 g_return_val_if_fail(buffer != NULL, FALSE);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
280
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
281 /* Check if the buffer matches our regex for messages. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
282 matches = g_regex_match(parser->regex_message, buffer, 0, &info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
283 if(!matches) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
284 g_set_error(error, PURPLE_IRCV3_DOMAIN, 0,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
285 "failed to parser buffer '%s'", buffer);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
286
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
287 g_match_info_unref(info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
288
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
289 return FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
290 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
291
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
292 /* Extract the command from the buffer, so we can find the handler. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
293 command = g_match_info_fetch_named(info, "command");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
294 handler = g_hash_table_lookup(parser->handlers, command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
295 if(handler == NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
296 if(parser->fallback_handler == NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
297 g_set_error(error, PURPLE_IRCV3_DOMAIN, 0,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
298 "no handler found for command %s and no default "
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
299 "handler set.", command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
300
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
301 g_free(command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
302 g_match_info_unref(info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
303
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
304 return FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
305 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
306
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
307 handler = parser->fallback_handler;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
308 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
309
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
310 /* If we made it this far, we have our handler, so lets get the rest of the
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
311 * parameters and call the handler.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
312 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
313 tags_string = g_match_info_fetch_named(info, "tags");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
314 tags = purple_ircv3_parser_parse_tags(parser, tags_string, &local_error);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
315 g_free(tags_string);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
316 if(local_error != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
317 g_propagate_error(error, local_error);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
318
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
319 g_free(command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
320 g_hash_table_destroy(tags);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
321 g_match_info_unref(info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
322
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
323 return FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
324 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
325
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
326 source = g_match_info_fetch_named(info, "source");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
327 middle = g_match_info_fetch_named(info, "middle");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
328 coda = g_match_info_fetch_named(info, "coda");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
329 trailing = g_match_info_fetch_named(info, "trailing");
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
330
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
331 params = purple_ircv3_parser_build_params(parser, middle, coda, trailing,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
332 &n_params);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
333
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
334 /* Call the handler. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
335 result = handler(tags, source, command, n_params, params, error, data);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
336
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
337 /* Cleanup everything. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
338 g_free(source);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
339 g_free(command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
340 g_free(middle);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
341 g_free(coda);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
342 g_free(trailing);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
343 g_strfreev(params);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
344 g_hash_table_destroy(tags);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
345 g_match_info_unref(info);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
346
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
347 return result;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
348 }
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
349
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
350 void
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
351 purple_ircv3_parser_add_default_handlers(PurpleIRCv3Parser *parser) {
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
352 g_return_if_fail(PURPLE_IRCV3_IS_PARSER(parser));
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
353
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
354 purple_ircv3_parser_set_fallback_handler(parser,
41821
ec766a6f010a Properly namespace the message handlers and reorganize them
Gary Kramlich <grim@reaperworld.com>
parents: 41813
diff changeset
355 purple_ircv3_message_handler_fallback);
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
356
41813
86302ce7e3c1 Add support for handling PRIVMSG and NOTICE messages from the server
Gary Kramlich <grim@reaperworld.com>
parents: 41807
diff changeset
357 purple_ircv3_parser_add_handler(parser, "CAP",
41821
ec766a6f010a Properly namespace the message handlers and reorganize them
Gary Kramlich <grim@reaperworld.com>
parents: 41813
diff changeset
358 purple_ircv3_message_handler_cap);
41813
86302ce7e3c1 Add support for handling PRIVMSG and NOTICE messages from the server
Gary Kramlich <grim@reaperworld.com>
parents: 41807
diff changeset
359 purple_ircv3_parser_add_handler(parser, "NOTICE",
86302ce7e3c1 Add support for handling PRIVMSG and NOTICE messages from the server
Gary Kramlich <grim@reaperworld.com>
parents: 41807
diff changeset
360 purple_ircv3_message_handler_privmsg);
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
361 purple_ircv3_parser_add_handler(parser, "PING",
41821
ec766a6f010a Properly namespace the message handlers and reorganize them
Gary Kramlich <grim@reaperworld.com>
parents: 41813
diff changeset
362 purple_ircv3_message_handler_ping);
41813
86302ce7e3c1 Add support for handling PRIVMSG and NOTICE messages from the server
Gary Kramlich <grim@reaperworld.com>
parents: 41807
diff changeset
363 purple_ircv3_parser_add_handler(parser, "PRIVMSG",
86302ce7e3c1 Add support for handling PRIVMSG and NOTICE messages from the server
Gary Kramlich <grim@reaperworld.com>
parents: 41807
diff changeset
364 purple_ircv3_message_handler_privmsg);
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
365 }

mercurial