libpurple/protocols/ircv3/purpleircv3parser.c

Sat, 26 Nov 2022 01:31:13 -0600

author
Gary Kramlich <grim@reaperworld.com>
date
Sat, 26 Nov 2022 01:31:13 -0600
changeset 41941
355bd17297f0
parent 41931
f35eb5adb2e0
child 41951
00c472cd0fff
permissions
-rw-r--r--

Set the C standard to C99 and set warning_level to 2 for the IRCv3 Protocol Plugin

Testing Done:
Compiled and verified the warnings were gone.

I left the capabilities warnings for now as I'm fixing them in another review request.

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

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 *****************************************************************************/
41931
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
39 static char *
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
40 purple_ircv3_parser_unescape_tag_value(const char *value) {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
41 GString *unescaped = g_string_new("");
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
42 gboolean escaping = FALSE;
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
43
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
44 /* Walk the string and replace escaped values according to
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
45 * https://ircv3.net/specs/extensions/message-tags.html#escaping-values
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
46 */
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
47 for(int i = 0; value[i] != '\0'; i++) {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
48 if(escaping) {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
49 /* Set the replacement to the current character which will fall
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
50 * through for everything, including '\\'
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
51 */
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
52 char replacement = value[i];
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
53
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
54 if(value[i] == ':') {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
55 replacement = ';';
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
56 } else if(value[i] == 's') {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
57 replacement = ' ';
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
58 } else if(value[i] == 'r') {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
59 replacement = '\r';
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
60 } else if(value[i] == 'n') {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
61 replacement = '\n';
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
62 }
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
63
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
64 g_string_append_c(unescaped, replacement);
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
65 escaping = FALSE;
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
66 } else {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
67 if(value[i] == '\\') {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
68 escaping = TRUE;
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
69 } else {
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
70 g_string_append_c(unescaped, value[i]);
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
71 }
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
72 }
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
73 }
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
74
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
75 return g_string_free(unescaped, FALSE);
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
76 }
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
77
41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 static GHashTable *
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79 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
80 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
81 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82 GError *local_error = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83 GHashTable *tags = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
84 GMatchInfo *info = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
85 gboolean matches = FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 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
88
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89 /* 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
90 * 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
91 * empty hash table.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93 if(*tags_string == '\0') {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
94 return tags;
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 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
98 &info, &local_error);
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 if(local_error != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
101 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
102
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
103 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
104
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
105 return tags;
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
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
108 if(!matches) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
109 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
110 "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
111
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
112 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
113
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
114 return tags;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
115 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
116
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
117 while(g_match_info_matches(info)) {
41931
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
118 char *key = NULL;
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
119 char *value = NULL;
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
120 char *unescaped = NULL;
41774
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 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
123 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
124
41931
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
125 unescaped = purple_ircv3_parser_unescape_tag_value(value);
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
126 g_free(value);
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
127
41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
128 /* 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
129 * 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
130 */
41931
f35eb5adb2e0 Unescape tag values in IRCv3
Gary Kramlich <grim@reaperworld.com>
parents: 41821
diff changeset
131 g_hash_table_insert(tags, key, unescaped);
41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
132 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
133
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
134 if(local_error != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
135 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
136
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 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
142
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
143 return tags;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
144 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
145
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
146 static guint
41941
355bd17297f0 Set the C standard to C99 and set warning_level to 2 for the IRCv3 Protocol Plugin
Gary Kramlich <grim@reaperworld.com>
parents: 41931
diff changeset
147 purple_ircv3_parser_extract_params(G_GNUC_UNUSED PurpleIRCv3Parser *parser,
41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
148 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
149 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
150 gchar *ptr = 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 /* 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
154 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
155 /* Look for a space. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
156 ptr = strchr(str, ' ');
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 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
159 * builder.
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
160 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
161 if(ptr != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
162 *ptr = '\0';
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
163 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
164
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
165 /* 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
166 * 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
167 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
168 str = ptr + 1;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
169
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
170 /* 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
171 count++;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
172 } else {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
173 /* Add the remaining string. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
174 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
175
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
176 /* 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
177 count++;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
178
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
179 /* 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
180 break;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
181 }
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
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
184 return count;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
185 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
186
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
187 static GStrv
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
188 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
189 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
190 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
191 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
192 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
193 GStrv result = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
194 guint count = 0;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
195
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
196 *n_params = 0;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
197
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
198 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
199 *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
200
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
201 if(*coda != '\0') {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
202 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
203 *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
204 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
205
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
206 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
207
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
208 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
209
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
210 return result;
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
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
213 static void
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
214 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
215 const gchar *command,
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
216 PurpleIRCv3MessageHandler handler)
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
217 {
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
218 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
219 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
220 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
221
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
222 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
223 }
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
224
41774
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
225 /******************************************************************************
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
226 * Handlers
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
227 *****************************************************************************/
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
228 static gboolean
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
229 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
230 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
231 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
232 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
233 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
234 GError **error,
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
235 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
236 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
237 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
238 command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
239
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
240 return FALSE;
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 /******************************************************************************
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
244 * GObject Implementation
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
245 *****************************************************************************/
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
246 static void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
247 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
248 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
249
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
250 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
251 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
252
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
253 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
254
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
255 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
256 }
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 static void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
259 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
260 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
261 "(?::(?<source>[^ ]+) +)?"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
262 "(?<command>[^ :]+)"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
263 "(?: +(?<middle>(?:[^ :]+(?: +[^ :]+)*)))*"
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
264 "(?<coda> +:(?<trailing>.*)?)?",
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
265 0, 0, NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
266 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
267
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
268 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
269 "(?:=(?<value>[^\\r\\n;]*))?(?:;|$))",
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
270 0, 0, NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
271 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
272
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
273 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
274 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
275 NULL);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
276 }
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 static void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
279 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
280 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
281
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
282 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
283 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
284
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
285 /******************************************************************************
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
286 * Public API
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
287 *****************************************************************************/
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
288 PurpleIRCv3Parser *
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
289 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
290 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
291 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
292
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
293 void
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
294 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
295 PurpleIRCv3MessageHandler handler)
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
296 {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
297 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
298
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
299 parser->fallback_handler = handler;
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
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
302 gboolean
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
303 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
304 GError **error, gpointer data)
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 PurpleIRCv3MessageHandler handler = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
307 GError *local_error = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
308 GHashTable *tags = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
309 GMatchInfo *info = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
310 GStrv params = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
311 gchar *coda = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
312 gchar *command = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
313 gchar *middle = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
314 gchar *source = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
315 gchar *tags_string = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
316 gchar *trailing = NULL;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
317 gboolean matches = FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
318 gboolean result = FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
319 guint n_params = 0;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
320
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
321 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
322 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
323
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
324 /* 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
325 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
326 if(!matches) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
327 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
328 "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
329
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
330 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
331
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
332 return FALSE;
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
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
335 /* 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
336 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
337 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
338 if(handler == NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
339 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
340 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
341 "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
342 "handler set.", command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
343
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
344 g_free(command);
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 FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
348 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
349
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
350 handler = parser->fallback_handler;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
351 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
352
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
353 /* 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
354 * 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
355 */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
356 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
357 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
358 g_free(tags_string);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
359 if(local_error != NULL) {
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
360 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
361
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
362 g_free(command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
363 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
364 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
365
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
366 return FALSE;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
367 }
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
368
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
369 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
370 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
371 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
372 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
373
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
374 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
375 &n_params);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
376
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
377 /* Call the handler. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
378 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
379
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
380 /* Cleanup everything. */
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
381 g_free(source);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
382 g_free(command);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
383 g_free(middle);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
384 g_free(coda);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
385 g_free(trailing);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
386 g_strfreev(params);
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
387 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
388 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
389
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
390 return result;
170078e728c0 Implement a parser for ircv3 and add unit tests to it.
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
391 }
41796
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
392
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
393 void
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
394 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
395 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
396
ebe4ff278b02 Use the parser on ircv3 messages and handle pings
Gary Kramlich <grim@reaperworld.com>
parents: 41774
diff changeset
397 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
398 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
399
41813
86302ce7e3c1 Add support for handling PRIVMSG and NOTICE messages from the server
Gary Kramlich <grim@reaperworld.com>
parents: 41807
diff changeset
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407 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
408 }

mercurial