unrealircd

- supernets unrealircd source & configuration
git clone git://git.acid.vegas/unrealircd.git
Log | Files | Refs | Archive | README | LICENSE

channel.c (5798B)

      1 /* channel.* RPC calls
      2  * (C) Copyright 2022-.. Bram Matthys (Syzop) and the UnrealIRCd team
      3  * License: GPLv2 or later
      4  */
      5 
      6 #include "unrealircd.h"
      7 
      8 ModuleHeader MOD_HEADER
      9 = {
     10 	"rpc/channel",
     11 	"1.0.5",
     12 	"channel.* RPC calls",
     13 	"UnrealIRCd Team",
     14 	"unrealircd-6",
     15 };
     16 
     17 /* Forward declarations */
     18 void rpc_channel_list(Client *client, json_t *request, json_t *params);
     19 void rpc_channel_get(Client *client, json_t *request, json_t *params);
     20 void rpc_channel_set_mode(Client *client, json_t *request, json_t *params);
     21 void rpc_channel_set_topic(Client *client, json_t *request, json_t *params);
     22 void rpc_channel_kick(Client *client, json_t *request, json_t *params);
     23 
     24 MOD_INIT()
     25 {
     26 	RPCHandlerInfo r;
     27 
     28 	MARK_AS_OFFICIAL_MODULE(modinfo);
     29 
     30 	memset(&r, 0, sizeof(r));
     31 	r.method = "channel.list";
     32 	r.loglevel = ULOG_DEBUG;
     33 	r.call = rpc_channel_list;
     34 	if (!RPCHandlerAdd(modinfo->handle, &r))
     35 	{
     36 		config_error("[rpc/channel] Could not register RPC handler");
     37 		return MOD_FAILED;
     38 	}
     39 	memset(&r, 0, sizeof(r));
     40 	r.method = "channel.get";
     41 	r.loglevel = ULOG_DEBUG;
     42 	r.call = rpc_channel_get;
     43 	if (!RPCHandlerAdd(modinfo->handle, &r))
     44 	{
     45 		config_error("[rpc/channel] Could not register RPC handler");
     46 		return MOD_FAILED;
     47 	}
     48 	memset(&r, 0, sizeof(r));
     49 	r.method = "channel.set_mode";
     50 	r.call = rpc_channel_set_mode;
     51 	if (!RPCHandlerAdd(modinfo->handle, &r))
     52 	{
     53 		config_error("[rpc/channel] Could not register RPC handler");
     54 		return MOD_FAILED;
     55 	}
     56 	memset(&r, 0, sizeof(r));
     57 	r.method = "channel.set_topic";
     58 	r.call = rpc_channel_set_topic;
     59 	if (!RPCHandlerAdd(modinfo->handle, &r))
     60 	{
     61 		config_error("[rpc/channel] Could not register RPC handler");
     62 		return MOD_FAILED;
     63 	}
     64 	memset(&r, 0, sizeof(r));
     65 	r.method = "channel.kick";
     66 	r.call = rpc_channel_kick;
     67 	if (!RPCHandlerAdd(modinfo->handle, &r))
     68 	{
     69 		config_error("[rpc/channel] Could not register RPC handler");
     70 		return MOD_FAILED;
     71 	}
     72 
     73 	return MOD_SUCCESS;
     74 }
     75 
     76 MOD_LOAD()
     77 {
     78 	return MOD_SUCCESS;
     79 }
     80 
     81 MOD_UNLOAD()
     82 {
     83 	return MOD_SUCCESS;
     84 }
     85 
     86 void rpc_channel_list(Client *client, json_t *request, json_t *params)
     87 {
     88 	json_t *result, *list, *item;
     89 	Channel *channel;
     90 	int details;
     91 
     92 	OPTIONAL_PARAM_INTEGER("object_detail_level", details, 1);
     93 	if (details >= 5)
     94 	{
     95 		rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Using an 'object_detail_level' of >=5 is not allowed in this call");
     96 		return;
     97 	}
     98 
     99 	result = json_object();
    100 	list = json_array();
    101 	json_object_set_new(result, "list", list);
    102 
    103 	for (channel = channels; channel; channel=channel->nextch)
    104 	{
    105 		item = json_object();
    106 		json_expand_channel(item, NULL, channel, details);
    107 		json_array_append_new(list, item);
    108 	}
    109 
    110 	rpc_response(client, request, result);
    111 	json_decref(result);
    112 }
    113 
    114 void rpc_channel_get(Client *client, json_t *request, json_t *params)
    115 {
    116 	json_t *result, *item;
    117 	const char *channelname;
    118 	Channel *channel;
    119 	int details;
    120 
    121 	REQUIRE_PARAM_STRING("channel", channelname);
    122 	OPTIONAL_PARAM_INTEGER("object_detail_level", details, 3);
    123 
    124 	if (!(channel = find_channel(channelname)))
    125 	{
    126 		rpc_error(client, request, JSON_RPC_ERROR_NOT_FOUND, "Channel not found");
    127 		return;
    128 	}
    129 
    130 	result = json_object();
    131 	json_expand_channel(result, "channel", channel, details);
    132 	rpc_response(client, request, result);
    133 	json_decref(result);
    134 }
    135 
    136 void rpc_channel_set_mode(Client *client, json_t *request, json_t *params)
    137 {
    138 	json_t *result, *item;
    139 	const char *channelname, *modes, *parameters;
    140 	MessageTag *mtags = NULL;
    141 	Channel *channel;
    142 
    143 	REQUIRE_PARAM_STRING("channel", channelname);
    144 	REQUIRE_PARAM_STRING("modes", modes);
    145 	REQUIRE_PARAM_STRING("parameters", parameters);
    146 
    147 	if (!(channel = find_channel(channelname)))
    148 	{
    149 		rpc_error(client, request, JSON_RPC_ERROR_NOT_FOUND, "Channel not found");
    150 		return;
    151 	}
    152 
    153 	mtag_add_issued_by(&mtags, client, NULL);
    154 	set_channel_mode(channel, mtags, modes, parameters);
    155 	safe_free_message_tags(mtags);
    156 
    157 	/* Simply return success */
    158 	result = json_boolean(1);
    159 	rpc_response(client, request, result);
    160 	json_decref(result);
    161 }
    162 
    163 void rpc_channel_set_topic(Client *client, json_t *request, json_t *params)
    164 {
    165 	json_t *result, *item;
    166 	const char *channelname, *topic, *set_by=NULL, *str;
    167 	Channel *channel;
    168 	time_t set_at = 0;
    169 	MessageTag *mtags = NULL;
    170 
    171 	REQUIRE_PARAM_STRING("channel", channelname);
    172 	REQUIRE_PARAM_STRING("topic", topic);
    173 	OPTIONAL_PARAM_STRING("set_by", set_by);
    174 	OPTIONAL_PARAM_STRING("set_at", str);
    175 	if (str)
    176 		set_at = server_time_to_unix_time(str);
    177 
    178 	if (!(channel = find_channel(channelname)))
    179 	{
    180 		rpc_error(client, request, JSON_RPC_ERROR_NOT_FOUND, "Channel not found");
    181 		return;
    182 	}
    183 
    184 	mtag_add_issued_by(&mtags, client, NULL);
    185 	set_channel_topic(&me, channel, mtags, topic, set_by, set_at);
    186 	safe_free_message_tags(mtags);
    187 
    188 	/* Simply return success */
    189 	result = json_boolean(1);
    190 	rpc_response(client, request, result);
    191 	json_decref(result);
    192 }
    193 
    194 void rpc_channel_kick(Client *client, json_t *request, json_t *params)
    195 {
    196 	json_t *result, *item;
    197 	const char *channelname, *nick, *reason;
    198 	MessageTag *mtags = NULL;
    199 	Channel *channel;
    200 	Client *acptr;
    201 	time_t set_at = 0;
    202 
    203 	REQUIRE_PARAM_STRING("channel", channelname);
    204 	REQUIRE_PARAM_STRING("nick", nick);
    205 	REQUIRE_PARAM_STRING("reason", reason);
    206 
    207 	if (!(channel = find_channel(channelname)))
    208 	{
    209 		rpc_error(client, request, JSON_RPC_ERROR_NOT_FOUND, "Channel not found");
    210 		return;
    211 	}
    212 
    213 	if (!(acptr = find_user(nick, NULL)))
    214 	{
    215 		rpc_error(client, request, JSON_RPC_ERROR_NOT_FOUND, "Nickname not found");
    216 		return;
    217 	}
    218 
    219 	mtag_add_issued_by(&mtags, client, NULL);
    220 	kick_user(mtags, channel, &me, acptr, reason);
    221 	safe_free_message_tags(mtags);
    222 
    223 	/* Simply return success
    224 	 * TODO: actually we can do a find_member() check and such to see if the user is kicked!
    225 	 * that is, assuming single channel ;)
    226 	 */
    227 	result = json_boolean(1);
    228 	rpc_response(client, request, result);
    229 	json_decref(result);
    230 }