Line data Source code
1 : /* TLD library -- TLD, domain name, and sub-domain extraction
2 : * Copyright (c) 2011-2022 Made to Order Software Corp. All Rights Reserved
3 : *
4 : * Permission is hereby granted, free of charge, to any person obtaining a
5 : * copy of this software and associated documentation files (the
6 : * "Software"), to deal in the Software without restriction, including
7 : * without limitation the rights to use, copy, modify, merge, publish,
8 : * distribute, sublicense, and/or sell copies of the Software, and to
9 : * permit persons to whom the Software is furnished to do so, subject to
10 : * the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included
13 : * in all copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 : */
23 : #ifndef LIB_TLD_H
24 : #define LIB_TLD_H
25 : /** \file
26 : * \brief The public header of the libtld library.
27 : *
28 : * This file declares all the functions, objects, structures, etc. publicly
29 : * available from the libtld library.
30 : */
31 :
32 : #if ( defined(LIBTLD_DLL) || defined(_WINDLL) ) && ( defined(_WINDOWS) || defined(WINDOWS) )
33 : #ifdef tld_EXPORTS
34 : #define LIBTLD_EXPORT __declspec(dllexport)
35 : #else
36 : #define LIBTLD_EXPORT __declspec(dllimport)
37 : #endif
38 : #else
39 : #define LIBTLD_EXPORT
40 : #endif
41 :
42 : #ifdef __cplusplus
43 : extern "C" {
44 : #endif
45 :
46 : #define LIBTLD_VERSION_MAJOR 2
47 : #define LIBTLD_VERSION_MINOR 0
48 : #define LIBTLD_VERSION_PATCH 1
49 : #define LIBTLD_VERSION "2.0.1"
50 :
51 : // use tags instead
52 : enum tld_category
53 : {
54 : TLD_CATEGORY_INTERNATIONAL,
55 : TLD_CATEGORY_PROFESSIONALS,
56 : TLD_CATEGORY_LANGUAGE,
57 : TLD_CATEGORY_GROUP,
58 : TLD_CATEGORY_REGION,
59 : TLD_CATEGORY_TECHNICAL,
60 : TLD_CATEGORY_COUNTRY, // TODO: remove: TLD_CATEGORY_LOCATION + TLD_REGION_COUNTRY
61 : TLD_CATEGORY_LOCATION,
62 : TLD_CATEGORY_ENTREPRENEURIAL,
63 : TLD_CATEGORY_BRAND,
64 : TLD_CATEGORY_UNDEFINED
65 : };
66 :
67 : enum tld_status
68 : {
69 : TLD_STATUS_VALID,
70 : TLD_STATUS_PROPOSED,
71 : TLD_STATUS_DEPRECATED,
72 : TLD_STATUS_UNUSED,
73 : TLD_STATUS_RESERVED,
74 : TLD_STATUS_INFRASTRUCTURE,
75 : TLD_STATUS_EXAMPLE,
76 : TLD_STATUS_UNDEFINED,
77 :
78 : /* The following status is never returned. It is used internally to handle
79 : * exceptions (i.e. usually 2nd level domain names that should not be.)
80 : */
81 : TLD_STATUS_EXCEPTION = 100
82 : };
83 :
84 : enum tld_result
85 : {
86 : TLD_RESULT_SUCCESS,
87 : TLD_RESULT_INVALID, /* TLD not acceptable (reserved, deprecated, etc.) */
88 : TLD_RESULT_NULL, /* URI is a NULL pointer */
89 : TLD_RESULT_NO_TLD, /* no '.' in the URI */
90 : TLD_RESULT_BAD_URI, /* URI includes two '.' one after another or some other problem */
91 : TLD_RESULT_NOT_FOUND
92 : };
93 :
94 : struct tld_info
95 : {
96 : enum tld_category f_category;
97 : enum tld_status f_status;
98 : char f_country[48]; /* current longest is "South Georgia and the South Sandwich Islands" */
99 : const char * f_tld; /* pointer within your URI string */
100 : int f_offset;
101 : int f_tld_index;
102 : };
103 :
104 : struct tld_tag_definition
105 : {
106 : const char * f_name;
107 : int f_name_length;
108 : const char * f_value;
109 : int f_value_length;
110 : };
111 :
112 : #define VALID_URI_ASCII_ONLY 0x0001
113 : #define VALID_URI_NO_SPACES 0x0002
114 :
115 :
116 : extern LIBTLD_EXPORT const char *tld_version();
117 :
118 :
119 : extern LIBTLD_EXPORT void tld_clear_info(struct tld_info *info);
120 : extern LIBTLD_EXPORT enum tld_result tld(const char *uri, struct tld_info *info);
121 : extern LIBTLD_EXPORT enum tld_result tld_load_tlds(const char *filename, int fallback);
122 : extern LIBTLD_EXPORT void tld_free_tlds();
123 : extern LIBTLD_EXPORT enum tld_result tld_check_uri(const char *uri, struct tld_info *info, const char *protocols, int flags);
124 : extern LIBTLD_EXPORT char * tld_domain_to_lowercase(const char *domain);
125 : extern LIBTLD_EXPORT int tld_tag_count(struct tld_info *info);
126 : extern LIBTLD_EXPORT enum tld_result tld_get_tag(struct tld_info *info, int tag_idx, struct tld_tag_definition *tag);
127 : extern LIBTLD_EXPORT const char * tld_status_to_string(enum tld_status status);
128 : extern LIBTLD_EXPORT enum tld_category tld_word_to_category(const char *word, int n);
129 :
130 :
131 :
132 : struct tld_email
133 : {
134 : const char * f_group;
135 : const char * f_original_email;
136 : const char * f_fullname;
137 : const char * f_username;
138 : const char * f_domain;
139 : const char * f_email_only;
140 : const char * f_canonicalized_email;
141 : };
142 :
143 : enum tld_email_field_type
144 : {
145 : TLD_EMAIL_FIELD_TYPE_INVALID = -1,
146 : TLD_EMAIL_FIELD_TYPE_UNKNOWN,
147 : TLD_EMAIL_FIELD_TYPE_MAILBOX_LIST,
148 : TLD_EMAIL_FIELD_TYPE_MAILBOX,
149 : TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST,
150 : TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST_OPT
151 : };
152 :
153 : struct tld_email_list;
154 :
155 : extern LIBTLD_EXPORT struct tld_email_list *tld_email_alloc();
156 : extern LIBTLD_EXPORT void tld_email_free(struct tld_email_list *list);
157 : extern LIBTLD_EXPORT enum tld_result tld_email_parse(struct tld_email_list *list, const char *emails, int flags);
158 : extern LIBTLD_EXPORT int tld_email_count(struct tld_email_list *list);
159 : extern LIBTLD_EXPORT void tld_email_rewind(struct tld_email_list *list);
160 : extern LIBTLD_EXPORT int tld_email_next(struct tld_email_list *list, struct tld_email *e);
161 :
162 :
163 : #ifdef __cplusplus
164 : }
165 : #endif
166 :
167 : #ifdef __cplusplus
168 : /* For C++ users */
169 : #include <string>
170 : #include <vector>
171 : #include <stdexcept>
172 :
173 :
174 12 : struct invalid_domain : public std::runtime_error
175 : {
176 12 : invalid_domain(const char *what_str = "this tld object is not currently valid") : runtime_error(what_str) {}
177 : };
178 :
179 :
180 7 : class LIBTLD_EXPORT tld_object
181 : {
182 : public:
183 : tld_object(const char *domain_name = NULL);
184 : tld_object(const std::string& domain_name);
185 : void set_domain(const char *domain_name);
186 : void set_domain(const std::string& domain_name);
187 : tld_result result() const;
188 : tld_status status() const;
189 : bool is_valid() const;
190 : std::string domain() const;
191 : std::string sub_domains() const;
192 : std::string full_domain() const;
193 : std::string domain_only() const;
194 : std::string tld_only() const;
195 : tld_category category() const;
196 : std::string country() const;
197 : private:
198 : std::string f_domain = std::string();
199 : tld_info f_info = tld_info();
200 : tld_result f_result = TLD_RESULT_INVALID;
201 : };
202 :
203 :
204 626 : struct LIBTLD_EXPORT tld_email_list
205 : {
206 : public:
207 2581 : struct tld_email_t
208 : {
209 : tld_result parse(const std::string& email);
210 : tld_result parse_group(const std::string& group);
211 :
212 : std::string f_group = std::string();
213 : std::string f_original_email = std::string();
214 : std::string f_fullname = std::string();
215 : std::string f_username = std::string();
216 : std::string f_domain = std::string();
217 : std::string f_email_only = std::string();
218 : std::string f_canonicalized_email = std::string();
219 : };
220 : typedef std::vector<tld_email_t> tld_email_list_t;
221 :
222 : tld_email_list();
223 : tld_result parse(const std::string& emails, int flags);
224 : static std::string quote_string(const std::string& name, char quote);
225 : int count() const;
226 : void rewind() const;
227 : bool next(tld_email_t& e) const;
228 : bool next(tld_email *e) const;
229 :
230 : static tld_email_field_type email_field_type(const std::string& name);
231 :
232 : private:
233 : void parse_all_emails();
234 :
235 : std::string f_input = std::string();
236 : int f_flags = 0;
237 : tld_result f_result = TLD_RESULT_INVALID;
238 : std::string f_last_group = std::string();
239 : mutable int f_pos = 0;
240 : tld_email_list_t f_email_list = tld_email_list_t();
241 : };
242 : #endif
243 : /*#ifdef __cplusplus*/
244 :
245 : #endif
246 : /*#ifndef LIB_TLD_H*/
247 : /* vim: ts=4 sw=4 et
248 : */
|