protocols/ircv3/purpleircv3source.c

changeset 42652
225762d4e206
parent 42568
31e8c7c92e2f
equal deleted inserted replaced
42651:5ad29b5bf1c7 42652:225762d4e206
1 /*
2 * Purple - Internet Messaging Library
3 * Copyright (C) Pidgin Developers <devel@pidgin.im>
4 *
5 * Purple is the legal property of its developers, whose names are too numerous
6 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * source distribution.
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this library; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23 #include <purple.h>
24
25 #include "purpleircv3source.h"
26
27 /******************************************************************************
28 * Public API
29 *****************************************************************************/
30 void
31 purple_ircv3_source_parse(const char *source, char **nick, char **user,
32 char **host)
33 {
34 GRegex *regex = NULL;
35 GMatchInfo *info = NULL;
36 gboolean matched = FALSE;
37
38 g_return_if_fail(!purple_strempty(source));
39 g_return_if_fail(nick != NULL || user != NULL || host != NULL);
40
41 /* If we find any \r \n or spaces in our source, it's invalid so we just
42 * bail immediately.
43 */
44 matched = g_regex_match_simple("[\r\n ]", source, G_REGEX_DEFAULT,
45 G_REGEX_MATCH_DEFAULT);
46 if(matched) {
47 return;
48 }
49
50 regex = g_regex_new("^(?P<nick>[^ \r\n!]+)(?:!(?P<user>[^@]+)(?:@(?P<host>[^!@]+))?)?$",
51 G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
52
53 matched = g_regex_match(regex, source, G_REGEX_MATCH_DEFAULT, &info);
54 if(!matched) {
55 if(nick != NULL) {
56 *nick = g_strdup(source);
57 }
58
59 g_clear_pointer(&info, g_match_info_unref);
60 g_clear_pointer(&regex, g_regex_unref);
61
62 return;
63 }
64
65 if(nick != NULL) {
66 *nick = g_match_info_fetch_named(info, "nick");
67 }
68
69 if(user != NULL) {
70 char *tmp = g_match_info_fetch_named(info, "user");
71
72 if(!purple_strempty(tmp)) {
73 *user = tmp;
74 } else {
75 g_free(tmp);
76 }
77 }
78
79 if(host != NULL) {
80 char *tmp = g_match_info_fetch_named(info, "host");
81
82 if(!purple_strempty(tmp)) {
83 *host = tmp;
84 } else {
85 g_free(tmp);
86 }
87 }
88
89 g_clear_pointer(&info, g_match_info_unref);
90 g_clear_pointer(&regex, g_regex_unref);
91 }

mercurial