1 /*
2  * uhub - A tiny ADC p2p connection hub
3  * Copyright (C) 2007-2014, Jan Vidar Krey
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include "plugin_api/handle.h"
21 #include "util/memory.h"
22 
23 struct user_info
24 {
25 	sid_t sid;
26 	int warnings;
27 };
28 
29 struct chat_restrictions_data
30 {
31 	size_t num_users;        // number of users tracked.
32 	size_t max_users;        // max users (hard limit max 1M users due to limitations in the SID (20 bits)).
33 	struct user_info* users; // array of max_users
34 
35 	enum auth_credentials allow_privchat;   // minimum credentials to allow using private chat
36 	enum auth_credentials allow_op_contact; // minimum credentials to allow private chat to operators (including super and admins).
37 	enum auth_credentials allow_mainchat;   // minimum credentials to allow using main chat
38 };
39 
parse_config(struct plugin_handle * plugin,const char * line)40 static struct chat_data* parse_config(struct plugin_handle* plugin, const char* line)
41 {
42 	struct chat_data* data = (struct chat_data*) hub_malloc(sizeof(struct chat_data));
43 	struct cfg_tokens* tokens = cfg_tokenize(line);
44 	char* token = cfg_token_get_first(tokens);
45 
46 	// defaults
47 	data->num_users = 0;
48 	data->max_users = 512;
49 	data->users = hub_malloc_zero(sizeof(struct user_info) * data->max_users);
50 	data->allow_mainchat = auth_cred_guest;
51 	data->allow_op_contact = auth_cred_guest;
52 	data->allow_privchat = auth_cred_guest;
53 
54 	while (token)
55 	{
56 		struct cfg_settings* setting = cfg_settings_split(token);
57 
58 		if (!setting)
59 		{
60 			set_error_message(plugin, "Unable to parse startup parameters");
61 			cfg_tokens_free(tokens);
62 			hub_free(data);
63 			return 0;
64 		}
65 
66 		if (strcmp(cfg_settings_get_key(setting), "allow_privchat") == 0)
67 		{
68 			if (!string_to_boolean(cfg_settings_get_value(setting), &data->allow_privchat))
69 				data->allow_privchat = 0;
70 		}
71 		else if (strcmp(cfg_settings_get_key(setting), "minimum_access") == 0)
72 		{
73 		}
74 		else
75 		{
76 			set_error_message(plugin, "Unknown startup parameters given");
77 			cfg_tokens_free(tokens);
78 			cfg_settings_free(setting);
79 			hub_free(data);
80 			return 0;
81 		}
82 
83 		cfg_settings_free(setting);
84 		token = cfg_token_get_next(tokens);
85 	}
86 	cfg_tokens_free(tokens);
87 
88 	return data;
89 }
90 
get_user_info(struct chat_data * data,sid_t sid)91 static struct user_info* get_user_info(struct chat_data* data, sid_t sid)
92 {
93 	struct user_info* u;
94 
95 	// resize buffer if needed.
96 	if (sid >= data->max_users)
97 	{
98 		u = hub_malloc_zero(sizeof(struct user_info) * (sid + 1));
99 		memcpy(u, data->users, data->max_users);
100 		hub_free(data->users);
101 		data->users = u;
102 		data->max_users = sid;
103 		u = NULL;
104 	}
105 
106 	u = &data->users[sid];
107 
108 	// reset counters if the user was not previously known.
109 	if (!u->sid)
110 	{
111 		u->sid = sid;
112 		u->warnings = 0;
113 		data->num_users++;
114 	}
115 	return u;
116 }
117 
on_user_login(struct plugin_handle * plugin,struct plugin_user * user)118 static void on_user_login(struct plugin_handle* plugin, struct plugin_user* user)
119 {
120 	struct chat_data* data = (struct chat_data*) plugin->ptr;
121 	/*struct user_info* info = */
122 	get_user_info(data, user->sid);
123 }
124 
on_user_logout(struct plugin_handle * plugin,struct plugin_user * user,const char * reason)125 static void on_user_logout(struct plugin_handle* plugin, struct plugin_user* user, const char* reason)
126 {
127 	struct chat_data* data = (struct chat_data*) plugin->ptr;
128 	struct user_info* info = get_user_info(data, user->sid);
129 	if (info->sid)
130 		data->num_users--;
131 	info->warnings = 0;
132 	info->sid = 0;
133 }
134 
on_chat_msg(struct plugin_handle * plugin,struct plugin_user * from,const char * message)135 plugin_st on_chat_msg(struct plugin_handle* plugin, struct plugin_user* from, const char* message)
136 {
137 	struct chat_data* data = (struct chat_data*) plugin->ptr;
138 	if (from->credentials >=
139 	return st_default;
140 }
141 
142 plugin_st on_private_msg(struct plugin_handle* plugin, struct plugin_user* from, struct plugin_user* to, const char* message)
143 {
144 	return st_default;
145 }
146 
147 
148 int plugin_register(struct plugin_handle* plugin, const char* config)
149 {
150 	PLUGIN_INITIALIZE(plugin, "Privileged chat hub", "1.0", "Only registered users can send messages on the main chat.");
151 	plugin->ptr = cip_initialize();
152 
153 	plugin->funcs.on_user_login = on_user_login;
154 	plugin->funcs.on_user_logout = on_user_logout;
155 	plugin->funcs.on_chat_msg = on_chat_msg;
156 	plugin->funcs.on_private_msg = on_private_msg;
157 
158 	return 0;
159 }
160 
161 int plugin_unregister(struct plugin_handle* plugin)
162 {
163 	struct chat_data* data = (struct chat_data*) plugin->ptr;
164 	if (data)
165 	{
166 		hub_free(data->users);
167 		hub_free(data);
168 	}
169 	return 0;
170 }
171 
172