| 1 |
/* JSONRPC Library |
| 2 |
* |
| 3 |
* Copyright (c) 2014 Atheme Development Group |
| 4 |
* Please read COPYING and README for further details. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <mowgli.h> |
| 9 |
#include "atheme.h" |
| 10 |
#include "jsonrpclib.h" |
| 11 |
|
| 12 |
void jsonrpc_process(char *buffer, void *userdata) |
| 13 |
{ |
| 14 |
if (!buffer) |
| 15 |
{ |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
mowgli_json_t *parsed = mowgli_json_parse_string(buffer); |
| 20 |
|
| 21 |
if (parsed == NULL) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
mowgli_json_tag_t tag = MOWGLI_JSON_TAG(parsed); |
| 26 |
|
| 27 |
//JSON RPC works with JSON objects only, anything else can't be correct. |
| 28 |
|
| 29 |
if (tag != MOWGLI_JSON_TAG_OBJECT) |
| 30 |
{ |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
mowgli_patricia_t *obj = MOWGLI_JSON_OBJECT(parsed); |
| 35 |
|
| 36 |
if (parsed == NULL) |
| 37 |
{ |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
mowgli_json_t *method = mowgli_patricia_retrieve(obj, "method"); |
| 42 |
mowgli_json_t *params = mowgli_patricia_retrieve(obj, "params"); |
| 43 |
mowgli_json_t *id = mowgli_patricia_retrieve(obj, "id"); |
| 44 |
|
| 45 |
char *method_str, *id_str; |
| 46 |
mowgli_list_t *params_list; |
| 47 |
|
| 48 |
if (id == NULL || params == NULL || method == NULL) |
| 49 |
{ |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if (MOWGLI_JSON_TAG(method) != MOWGLI_JSON_TAG_STRING || |
| 54 |
MOWGLI_JSON_TAG(id) != MOWGLI_JSON_TAG_STRING || |
| 55 |
MOWGLI_JSON_TAG(params) != MOWGLI_JSON_TAG_ARRAY) |
| 56 |
{ |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
method_str = MOWGLI_JSON_STRING_STR(method); |
| 61 |
id_str = MOWGLI_JSON_STRING_STR(id); |
| 62 |
params_list = MOWGLI_JSON_ARRAY(params); |
| 63 |
|
| 64 |
mowgli_json_t *param; |
| 65 |
mowgli_node_t *n; |
| 66 |
|
| 67 |
jsonrpc_method_t call_method = get_json_method(method_str); |
| 68 |
|
| 69 |
MOWGLI_LIST_FOREACH(n, params_list->head) |
| 70 |
{ |
| 71 |
param = n->data; |
| 72 |
|
| 73 |
if (MOWGLI_JSON_TAG(param) != MOWGLI_JSON_TAG_STRING) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
mowgli_list_t *params_str = mowgli_list_create(); |
| 80 |
|
| 81 |
MOWGLI_LIST_FOREACH(n, params_list->head) |
| 82 |
{ |
| 83 |
param = n->data; |
| 84 |
|
| 85 |
char *param_str = MOWGLI_JSON_STRING_STR(param); |
| 86 |
mowgli_node_add(param_str, mowgli_node_create(), params_str); |
| 87 |
} |
| 88 |
|
| 89 |
if (call_method != NULL) { |
| 90 |
call_method(userdata, params_str, id_str); |
| 91 |
} else { |
| 92 |
jsonrpc_failure_string(userdata, fault_badparams, "Invalid command", id_str); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
void jsonrpc_success_string(void *conn, const char *result, const char *id) |
| 99 |
{ |
| 100 |
mowgli_json_t *obj = mowgli_json_create_object(); |
| 101 |
|
| 102 |
mowgli_patricia_t *patricia = MOWGLI_JSON_OBJECT(obj); |
| 103 |
|
| 104 |
mowgli_json_t *resultobj = mowgli_json_create_string(result); |
| 105 |
mowgli_json_t *idobj = mowgli_json_create_string(id); |
| 106 |
|
| 107 |
mowgli_patricia_add(patricia, "result", resultobj); |
| 108 |
mowgli_patricia_add(patricia, "id", idobj); |
| 109 |
mowgli_patricia_add(patricia, "error", mowgli_json_null); |
| 110 |
|
| 111 |
mowgli_string_t *str = mowgli_string_create(); |
| 112 |
|
| 113 |
mowgli_json_serialize_to_string(obj, str, 0); |
| 114 |
|
| 115 |
jsonrpc_send_data(conn, str->str); |
| 116 |
} |
| 117 |
|
| 118 |
void jsonrpc_failure_string(void *conn, int code, const char *error, const char *id) |
| 119 |
{ |
| 120 |
mowgli_json_t *obj = mowgli_json_create_object(); |
| 121 |
mowgli_json_t *errorobj = mowgli_json_create_object(); |
| 122 |
|
| 123 |
mowgli_patricia_t *patricia = MOWGLI_JSON_OBJECT(errorobj); |
| 124 |
|
| 125 |
mowgli_patricia_add(patricia, "code", mowgli_json_create_integer(code)); |
| 126 |
mowgli_patricia_add(patricia, "message", mowgli_json_create_string(error)); |
| 127 |
|
| 128 |
patricia = MOWGLI_JSON_OBJECT(obj); |
| 129 |
|
| 130 |
mowgli_json_t *idobj = mowgli_json_create_string(id); |
| 131 |
|
| 132 |
mowgli_patricia_add(patricia, "result", mowgli_json_null); |
| 133 |
mowgli_patricia_add(patricia, "id", idobj); |
| 134 |
mowgli_patricia_add(patricia, "error", errorobj); |
| 135 |
|
| 136 |
mowgli_string_t *str = mowgli_string_create(); |
| 137 |
|
| 138 |
mowgli_json_serialize_to_string(obj, str, 0); |
| 139 |
|
| 140 |
jsonrpc_send_data(conn, str->str); |
| 141 |
} |
| 142 |
|
| 143 |
char *jsonrpc_normalizeBuffer(const char *buf) |
| 144 |
{ |
| 145 |
char *newbuf; |
| 146 |
int i, len, j = 0; |
| 147 |
|
| 148 |
len = strlen(buf); |
| 149 |
newbuf = (char *)smalloc(sizeof(char) * len + 1); |
| 150 |
|
| 151 |
for (i = 0; i < len; i++) |
| 152 |
{ |
| 153 |
switch (buf[i]) |
| 154 |
{ |
| 155 |
/* ctrl char */ |
| 156 |
case 1: |
| 157 |
break; |
| 158 |
/* Bold ctrl char */ |
| 159 |
case 2: |
| 160 |
break; |
| 161 |
/* Color ctrl char */ |
| 162 |
case 3: |
| 163 |
/* If the next character is a digit, its also removed */ |
| 164 |
if (isdigit((unsigned char)buf[i + 1])) |
| 165 |
{ |
| 166 |
i++; |
| 167 |
|
| 168 |
/* not the best way to remove colors |
| 169 |
* which are two digit but no worse then |
| 170 |
* how the Unreal does with +S - TSL |
| 171 |
*/ |
| 172 |
if (isdigit((unsigned char)buf[i + 1])) |
| 173 |
{ |
| 174 |
i++; |
| 175 |
} |
| 176 |
|
| 177 |
/* Check for background color code |
| 178 |
* and remove it as well |
| 179 |
*/ |
| 180 |
if (buf[i + 1] == ',') |
| 181 |
{ |
| 182 |
i++; |
| 183 |
|
| 184 |
if (isdigit((unsigned char)buf[i + 1])) |
| 185 |
{ |
| 186 |
i++; |
| 187 |
} |
| 188 |
/* not the best way to remove colors |
| 189 |
* which are two digit but no worse then |
| 190 |
* how the Unreal does with +S - TSL |
| 191 |
*/ |
| 192 |
if (isdigit((unsigned char)buf[i + 1])) |
| 193 |
{ |
| 194 |
i++; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
break; |
| 200 |
/* tabs char */ |
| 201 |
case 9: |
| 202 |
break; |
| 203 |
/* line feed char */ |
| 204 |
case 10: |
| 205 |
break; |
| 206 |
/* carrage returns char */ |
| 207 |
case 13: |
| 208 |
break; |
| 209 |
/* Reverse ctrl char */ |
| 210 |
case 22: |
| 211 |
break; |
| 212 |
/* Underline ctrl char */ |
| 213 |
case 31: |
| 214 |
break; |
| 215 |
/* A valid char gets copied into the new buffer */ |
| 216 |
default: |
| 217 |
/* All valid <32 characters are handled above. */ |
| 218 |
if (buf[i] > 31) |
| 219 |
{ |
| 220 |
newbuf[j] = buf[i]; |
| 221 |
j++; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/* Terminate the string */ |
| 227 |
newbuf[j] = 0; |
| 228 |
|
| 229 |
return (newbuf); |
| 230 |
} |
| 231 |
|