1 | #include <sys/socket.h> |
---|
2 | #include <sys/un.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <dirent.h> |
---|
5 | |
---|
6 | #include "bitlbee.h" |
---|
7 | #include "bee.h" |
---|
8 | #include "nogaim.h" |
---|
9 | #include "parson.h" |
---|
10 | |
---|
11 | #define JSON_O_FOREACH(o, k, v) \ |
---|
12 | const char *k; const JSON_Value *v; int __i; \ |
---|
13 | for (__i = 0; json_object_get_tuple(o, __i, &k, &v); __i++) |
---|
14 | |
---|
15 | static int next_rpc_id = 1; |
---|
16 | |
---|
17 | struct rpc_plugin { |
---|
18 | /* Socket address of the RPC server. */ |
---|
19 | struct sockaddr *addr; |
---|
20 | socklen_t addrlen; |
---|
21 | /* Full copy of the "settings" section of the init message. This info |
---|
22 | * can only be applied later on, when an account is created (but well |
---|
23 | * before logging in). */ |
---|
24 | JSON_Value *settings; |
---|
25 | /* Supported away states returned by the away_states() function. Since |
---|
26 | * RPC servers can't do return values, just get this info at init time. |
---|
27 | * This means the list of possible away states is static from init time |
---|
28 | * which hopefully won't be a problem. If NULL, the away_states function |
---|
29 | * will not be set on this protocol. */ |
---|
30 | GList *away_states; |
---|
31 | /* Account flags. See account.h. Plugin lib should provide constants. */ |
---|
32 | int account_flags; |
---|
33 | }; |
---|
34 | |
---|
35 | struct rpc_connection { |
---|
36 | int fd; |
---|
37 | char *buf; |
---|
38 | int buflen; |
---|
39 | GHashTable *groupchats; |
---|
40 | }; |
---|
41 | |
---|
42 | struct rpc_groupchat { |
---|
43 | int id; |
---|
44 | struct groupchat *gc; |
---|
45 | }; |
---|
46 | |
---|
47 | static JSON_Value *jsonrpc_error(int code, const char *msg) { |
---|
48 | JSON_Value *ret = json_value_init_object(); |
---|
49 | json_object_set_null(json_object(ret), "result"); |
---|
50 | if (TRUE) { |
---|
51 | /* Format from http://jsonrpc.org/historical/json-rpc-1-1-alt.html. |
---|
52 | * Not sure whether to use it. */ |
---|
53 | JSON_Value *error = json_value_init_object(); |
---|
54 | json_object_set_integer(json_object(error), "code", code); |
---|
55 | json_object_set_string(json_object(error), "message", msg); |
---|
56 | json_object_set_value(json_object(ret), "error", error); |
---|
57 | } else { |
---|
58 | json_object_set_string(json_object(ret), "error", msg); |
---|
59 | } |
---|
60 | |
---|
61 | return ret; |
---|
62 | } |
---|
63 | |
---|
64 | // Might have liked to have this one in the library for optional values/etc. |
---|
65 | static void json_array_append_string_or_null(JSON_Array *array, const char *string) { |
---|
66 | if (string) |
---|
67 | json_array_append_string(array, string); |
---|
68 | else |
---|
69 | json_array_append_null(array); |
---|
70 | } |
---|
71 | |
---|
72 | static void json_object_set_string_or_null(JSON_Object *object, const char *key, const char *string) { |
---|
73 | if (string) |
---|
74 | json_object_set_string(object, key, string); |
---|
75 | else |
---|
76 | json_object_set_null(object, key); |
---|
77 | } |
---|
78 | |
---|
79 | static JSON_Value *rpc_out_new(const char *method, JSON_Array **params_) { |
---|
80 | JSON_Value *rpc = json_value_init_object(); |
---|
81 | json_object_set_string(json_object(rpc), "method", method); |
---|
82 | json_object_set_integer(json_object(rpc), "id", next_rpc_id++); |
---|
83 | |
---|
84 | JSON_Value *params = json_value_init_array(); |
---|
85 | json_object_set_value(json_object(rpc), "params", params); |
---|
86 | |
---|
87 | if (params_) |
---|
88 | *params_ = json_array(params); |
---|
89 | |
---|
90 | return rpc; |
---|
91 | } |
---|
92 | |
---|
93 | #define RPC_OUT_INIT(method) \ |
---|
94 | JSON_Array *params; \ |
---|
95 | JSON_Value *rpc = rpc_out_new(method, ¶ms); |
---|
96 | |
---|
97 | /** Sends an RPC object. Takes ownership (i.e. frees it when done). */ |
---|
98 | static gboolean rpc_send(struct im_connection *ic, JSON_Value *rpc) { |
---|
99 | struct rpc_connection *rd = ic->proto_data; |
---|
100 | char *buf = json_serialize_to_string(rpc); |
---|
101 | int len = strlen(buf); |
---|
102 | int st; |
---|
103 | |
---|
104 | buf = g_realloc(buf, len + 3); |
---|
105 | strcpy(buf + len, "\r\n"); |
---|
106 | len += 2; |
---|
107 | |
---|
108 | st = write(rd->fd, buf, len); |
---|
109 | g_free(buf); |
---|
110 | json_value_free(rpc); |
---|
111 | |
---|
112 | if (st != len) { |
---|
113 | if (!(ic->flags & OPT_LOGGING_OUT)) |
---|
114 | imcb_log(ic, "Write error"); |
---|
115 | imc_logout(ic, TRUE); |
---|
116 | return FALSE; |
---|
117 | } |
---|
118 | |
---|
119 | return TRUE; |
---|
120 | } |
---|
121 | |
---|
122 | static JSON_Value *rpc_ser_settings(set_t **set); |
---|
123 | |
---|
124 | static JSON_Value *rpc_ser_account(account_t *acc) { |
---|
125 | JSON_Value *v = json_value_init_object(); |
---|
126 | JSON_Object *o = json_object(v); |
---|
127 | json_object_set_string(o, "user", acc->user); |
---|
128 | json_object_set_string(o, "pass", acc->pass); |
---|
129 | if (acc->server) |
---|
130 | json_object_set_string(o, "server", acc->server); |
---|
131 | json_object_set_value(o, "settings", rpc_ser_settings(&acc->set)); |
---|
132 | return v; |
---|
133 | } |
---|
134 | |
---|
135 | static JSON_Value *rpc_ser_bee_user(bee_user_t *bu) { |
---|
136 | JSON_Value *v = json_value_init_object(); |
---|
137 | JSON_Object *o = json_object(v); |
---|
138 | json_object_set_string_or_null(o, "handle", bu->handle); |
---|
139 | json_object_set_string_or_null(o, "fullname", bu->fullname); |
---|
140 | json_object_set_string_or_null(o, "nick", bu->nick); |
---|
141 | json_object_set_string_or_null(o, "group", bu->group ? bu->group->name : NULL); |
---|
142 | json_object_set_integer(o, "flags", bu->flags); |
---|
143 | json_object_set_string_or_null(o, "status", bu->status); |
---|
144 | json_object_set_string_or_null(o, "status_msg", bu->status_msg); |
---|
145 | json_object_set_integer(o, "login_time", bu->login_time); |
---|
146 | json_object_set_integer(o, "idle_time", bu->idle_time); |
---|
147 | return v; |
---|
148 | } |
---|
149 | |
---|
150 | static char *rpc_set_evaluator(set_t *set, char *value); |
---|
151 | |
---|
152 | static void rpc_init(account_t *acc) { |
---|
153 | struct rpc_plugin *pd = acc->prpl->data; |
---|
154 | |
---|
155 | JSON_O_FOREACH(json_object(pd->settings), name, value) { |
---|
156 | JSON_Object *o = json_object(value); |
---|
157 | char *defs = NULL; |
---|
158 | JSON_Value *defv = json_object_get_value(o, "default"); |
---|
159 | if (json_type(defv) == JSONString) |
---|
160 | defs = g_strdup(json_string(defv)); |
---|
161 | else if(json_type(defv) == JSONInteger) |
---|
162 | defs = g_strdup_printf("%lld", (long long) json_integer(defv)); |
---|
163 | else if(json_type(defv) == JSONBoolean) |
---|
164 | defs = g_strdup(json_boolean(defv) ? "true" : "false"); |
---|
165 | set_t *set = set_add(&acc->set, name, defs, NULL, acc); |
---|
166 | g_free(defs); |
---|
167 | set->flags |= json_object_get_integer(o, "flags"); |
---|
168 | set->eval = rpc_set_evaluator; |
---|
169 | set->eval_data = o; |
---|
170 | /* eval_list turns out to be a memory leak so don't implement it |
---|
171 | * for now. |
---|
172 | * Allowing a plugin to define its own evaluator is not really |
---|
173 | * possible without having BitlBee block on it responding which |
---|
174 | * I don't want to do. |
---|
175 | * Should a module want to override a user's setting, it can |
---|
176 | * use set_setstr(). */ |
---|
177 | } |
---|
178 | |
---|
179 | acc->flags |= pd->account_flags; |
---|
180 | } |
---|
181 | |
---|
182 | set_eval rpc_type_set_eval(const set_t *set) { |
---|
183 | JSON_Object *o = set->eval_data; |
---|
184 | const char *type = json_object_get_string(o, "type"); |
---|
185 | |
---|
186 | set_eval type_eval = NULL; |
---|
187 | if (type == NULL) { |
---|
188 | /* Try to do something sane for settings that aren't ours. */ |
---|
189 | if (set->eval == set_eval_int || set->eval == set_eval_bool) { |
---|
190 | type_eval = set->eval; |
---|
191 | } |
---|
192 | } else if (g_str_has_prefix(type, "int")) { |
---|
193 | type_eval = set_eval_int; |
---|
194 | } else if (g_str_has_prefix(type, "bool")) { |
---|
195 | type_eval = set_eval_bool; |
---|
196 | } |
---|
197 | |
---|
198 | return type_eval; |
---|
199 | } |
---|
200 | |
---|
201 | static JSON_Value *set_make_json_value(set_eval type, const char *value) { |
---|
202 | JSON_Value *ret; |
---|
203 | |
---|
204 | if (value == NULL) { |
---|
205 | ret = json_value_init_null(); |
---|
206 | } else if (type == set_eval_int) { |
---|
207 | long long num = 0; |
---|
208 | /* Evaluator already did validation so ignore retval. */ |
---|
209 | sscanf(value, "%lld", &num); |
---|
210 | ret = json_value_init_integer(num); |
---|
211 | } else if (type == set_eval_bool) { |
---|
212 | ret = json_value_init_boolean(bool2int(value)); |
---|
213 | } else { |
---|
214 | ret = json_value_init_string(value); |
---|
215 | } |
---|
216 | return ret; |
---|
217 | } |
---|
218 | |
---|
219 | static char *rpc_set_evaluator(set_t *set, char *value) { |
---|
220 | set_eval type_eval = rpc_type_set_eval(set); |
---|
221 | if (type_eval) { |
---|
222 | char *new = type_eval(set, value); |
---|
223 | if (new == SET_INVALID) { |
---|
224 | return SET_INVALID; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | account_t *acc = set->data; |
---|
229 | if (acc->ic) { |
---|
230 | /* But do send RPCs to the plugin for each changed setting so |
---|
231 | * it always has up-to-date values. */ |
---|
232 | RPC_OUT_INIT("set_set"); |
---|
233 | json_array_append_string(params, set->key); |
---|
234 | json_array_append_value(params, set_make_json_value(rpc_type_set_eval(set), value)); |
---|
235 | rpc_send(acc->ic, rpc); |
---|
236 | } |
---|
237 | |
---|
238 | return value; |
---|
239 | } |
---|
240 | |
---|
241 | static JSON_Value *rpc_ser_settings(set_t **set) { |
---|
242 | const set_t *s; |
---|
243 | JSON_Value *ret = json_value_init_object(); |
---|
244 | |
---|
245 | for (s = *set; s; s = s->next) { |
---|
246 | JSON_Value *v = set_make_json_value(rpc_type_set_eval(s), set_value(s)); |
---|
247 | json_object_set_value(json_object(ret), s->key, v); |
---|
248 | } |
---|
249 | |
---|
250 | return ret; |
---|
251 | } |
---|
252 | |
---|
253 | static gboolean rpc_login_cb(gpointer data, gint fd, b_input_condition cond); |
---|
254 | static gboolean rpc_in_event(gpointer data, gint fd, b_input_condition cond); |
---|
255 | static JSON_Value *rpc_init_isup(); |
---|
256 | |
---|
257 | static void rpc_login(account_t *acc) { |
---|
258 | struct im_connection *ic = imcb_new(acc); |
---|
259 | struct rpc_connection *rd = ic->proto_data = g_new0(struct rpc_connection, 1); |
---|
260 | struct rpc_plugin *pd = acc->prpl->data; |
---|
261 | imcb_log(ic, "Logging in via RPC server"); |
---|
262 | rd->fd = socket(pd->addr->sa_family, SOCK_STREAM, 0); |
---|
263 | sock_make_nonblocking(rd->fd); |
---|
264 | if (connect(rd->fd, pd->addr, pd->addrlen) == -1) { |
---|
265 | closesocket(rd->fd); |
---|
266 | imcb_error(ic, "RPC server unreachable"); |
---|
267 | imc_logout(ic, TRUE); |
---|
268 | return; |
---|
269 | } |
---|
270 | ic->inpa = b_input_add(rd->fd, B_EV_IO_WRITE, rpc_login_cb, ic); |
---|
271 | rd->groupchats = g_hash_table_new(g_int_hash, g_int_equal); |
---|
272 | } |
---|
273 | |
---|
274 | static gboolean rpc_login_cb(gpointer data, gint fd, b_input_condition cond) { |
---|
275 | struct im_connection *ic = data; |
---|
276 | struct rpc_connection *rd = ic->proto_data; |
---|
277 | |
---|
278 | /* Need to repeat this since each IM connection means an actual new |
---|
279 | * RPC session. */ |
---|
280 | JSON_Value *init = rpc_init_isup(); |
---|
281 | if (!rpc_send(ic, init)) |
---|
282 | return FALSE; |
---|
283 | |
---|
284 | RPC_OUT_INIT("login"); |
---|
285 | json_array_append_value(params, rpc_ser_account(ic->acc)); |
---|
286 | if (!rpc_send(ic, rpc)) |
---|
287 | return FALSE; |
---|
288 | |
---|
289 | ic->inpa = b_input_add(rd->fd, B_EV_IO_READ, rpc_in_event, ic); |
---|
290 | |
---|
291 | return FALSE; |
---|
292 | } |
---|
293 | |
---|
294 | static void rpc_keepalive(struct im_connection *ic) { |
---|
295 | RPC_OUT_INIT("keepalive"); |
---|
296 | rpc_send(ic, rpc); |
---|
297 | } |
---|
298 | |
---|
299 | static void rpc_groupchat_free(struct groupchat *gc); |
---|
300 | |
---|
301 | static void rpc_logout(struct im_connection *ic) { |
---|
302 | RPC_OUT_INIT("logout"); |
---|
303 | if (!rpc_send(ic, rpc)) |
---|
304 | return; |
---|
305 | |
---|
306 | struct rpc_connection *rd = ic->proto_data; |
---|
307 | |
---|
308 | GHashTableIter iter; |
---|
309 | struct rpc_groupchat *rc; |
---|
310 | g_hash_table_iter_init(&iter, rd->groupchats); |
---|
311 | while (g_hash_table_iter_next(&iter, NULL, (gpointer*)&rc)) { |
---|
312 | rpc_groupchat_free(rc->gc); |
---|
313 | } |
---|
314 | |
---|
315 | b_event_remove(ic->inpa); |
---|
316 | closesocket(rd->fd); |
---|
317 | g_free(rd->buf); |
---|
318 | g_hash_table_destroy(rd->groupchats); |
---|
319 | g_free(rd); |
---|
320 | } |
---|
321 | |
---|
322 | static int rpc_buddy_msg(struct im_connection *ic, char *to, char *message, int flags) { |
---|
323 | RPC_OUT_INIT("buddy_msg"); |
---|
324 | json_array_append_string(params, to); |
---|
325 | json_array_append_string(params, message); |
---|
326 | json_array_append_integer(params, flags); |
---|
327 | return rpc_send(ic, rpc); |
---|
328 | } |
---|
329 | |
---|
330 | static void rpc_set_away(struct im_connection *ic, char *state, char *message) { |
---|
331 | RPC_OUT_INIT("set_away"); |
---|
332 | json_array_append_string_or_null(params, state); |
---|
333 | json_array_append_string_or_null(params, message); |
---|
334 | rpc_send(ic, rpc); |
---|
335 | } |
---|
336 | |
---|
337 | static int rpc_send_typing(struct im_connection *ic, char *who, int flags) { |
---|
338 | RPC_OUT_INIT("send_typing"); |
---|
339 | json_array_append_string(params, who); |
---|
340 | json_array_append_integer(params, flags); |
---|
341 | return rpc_send(ic, rpc); |
---|
342 | } |
---|
343 | |
---|
344 | static void rpc_add_buddy(struct im_connection *ic, char *name, char *group) { |
---|
345 | RPC_OUT_INIT("add_buddy"); |
---|
346 | json_array_append_string(params, name); |
---|
347 | json_array_append_string_or_null(params, group); |
---|
348 | rpc_send(ic, rpc); |
---|
349 | } |
---|
350 | |
---|
351 | static void rpc_remove_buddy(struct im_connection *ic, char *name, char *group) { |
---|
352 | RPC_OUT_INIT("remove_buddy"); |
---|
353 | json_array_append_string(params, name); |
---|
354 | json_array_append_string_or_null(params, group); |
---|
355 | rpc_send(ic, rpc); |
---|
356 | } |
---|
357 | |
---|
358 | static void rpc_add_permit(struct im_connection *ic, char *who) { |
---|
359 | RPC_OUT_INIT("add_permit"); |
---|
360 | json_array_append_string(params, who); |
---|
361 | rpc_send(ic, rpc); |
---|
362 | } |
---|
363 | |
---|
364 | static void rpc_add_deny(struct im_connection *ic, char *who) { |
---|
365 | RPC_OUT_INIT("add_deny"); |
---|
366 | json_array_append_string(params, who); |
---|
367 | rpc_send(ic, rpc); |
---|
368 | } |
---|
369 | |
---|
370 | static void rpc_rem_permit(struct im_connection *ic, char *who) { |
---|
371 | RPC_OUT_INIT("rem_permit"); |
---|
372 | json_array_append_string(params, who); |
---|
373 | rpc_send(ic, rpc); |
---|
374 | } |
---|
375 | |
---|
376 | static void rpc_rem_deny(struct im_connection *ic, char *who) { |
---|
377 | RPC_OUT_INIT("rem_deny"); |
---|
378 | json_array_append_string(params, who); |
---|
379 | rpc_send(ic, rpc); |
---|
380 | } |
---|
381 | |
---|
382 | static void rpc_get_info(struct im_connection *ic, char *who) { |
---|
383 | RPC_OUT_INIT("get_info"); |
---|
384 | json_array_append_string(params, who); |
---|
385 | rpc_send(ic, rpc); |
---|
386 | } |
---|
387 | |
---|
388 | static void rpc_chat_invite(struct groupchat *gc, char *who, char *message) { |
---|
389 | RPC_OUT_INIT("chat_invite"); |
---|
390 | struct rpc_groupchat *rc = gc->data; |
---|
391 | json_array_append_integer(params, rc->id); |
---|
392 | json_array_append_string(params, who); |
---|
393 | json_array_append_string_or_null(params, message); |
---|
394 | rpc_send(gc->ic, rpc); |
---|
395 | } |
---|
396 | |
---|
397 | static void rpc_chat_kick(struct groupchat *gc, char *who, const char *message) { |
---|
398 | RPC_OUT_INIT("chat_kick"); |
---|
399 | struct rpc_groupchat *rc = gc->data; |
---|
400 | json_array_append_integer(params, rc->id); |
---|
401 | json_array_append_string(params, who); |
---|
402 | json_array_append_string_or_null(params, message); |
---|
403 | rpc_send(gc->ic, rpc); |
---|
404 | } |
---|
405 | |
---|
406 | static void rpc_chat_leave(struct groupchat *gc) { |
---|
407 | RPC_OUT_INIT("chat_leave"); |
---|
408 | struct rpc_groupchat *rc = gc->data; |
---|
409 | json_array_append_integer(params, rc->id); |
---|
410 | rpc_send(gc->ic, rpc); |
---|
411 | rpc_groupchat_free(gc); |
---|
412 | } |
---|
413 | |
---|
414 | static void rpc_chat_msg(struct groupchat *gc, char *msg, int flags) { |
---|
415 | RPC_OUT_INIT("chat_msg"); |
---|
416 | struct rpc_groupchat *rc = gc->data; |
---|
417 | json_array_append_integer(params, rc->id); |
---|
418 | json_array_append_string(params, msg); |
---|
419 | json_array_append_integer(params, flags); |
---|
420 | rpc_send(gc->ic, rpc); |
---|
421 | } |
---|
422 | |
---|
423 | static struct rpc_groupchat *rpc_groupchat_new(struct im_connection *ic, const char *handle) { |
---|
424 | struct rpc_connection *rd = ic->proto_data; |
---|
425 | struct groupchat *gc = imcb_chat_new(ic, handle); |
---|
426 | struct rpc_groupchat *rc = gc->data = g_new0(struct rpc_groupchat, 1); |
---|
427 | rc->id = next_rpc_id++; |
---|
428 | rc->gc = gc; |
---|
429 | g_hash_table_insert(rd->groupchats, &rc->id, rc); |
---|
430 | return rc; // TODO: RETVAL HERE AND BELOW |
---|
431 | } |
---|
432 | |
---|
433 | static struct rpc_groupchat *rpc_groupchat_by_id(struct im_connection *ic, int id) { |
---|
434 | struct rpc_connection *rd = ic->proto_data; |
---|
435 | struct rpc_groupchat *rc = g_hash_table_lookup(rd->groupchats, &id); |
---|
436 | |
---|
437 | return rc; |
---|
438 | } |
---|
439 | |
---|
440 | static void rpc_groupchat_free(struct groupchat *gc) { |
---|
441 | g_free(gc->data); |
---|
442 | imcb_chat_free(gc); |
---|
443 | } |
---|
444 | |
---|
445 | /* Boilerplate for all incoming RPCs (where groupchat is identified using |
---|
446 | * numeric ID). */ |
---|
447 | #define SET_GROUPCHAT(rc) \ |
---|
448 | do { \ |
---|
449 | rc = rpc_groupchat_by_id(ic, json_array_get_integer(params, 0)); \ |
---|
450 | if (rc == NULL) \ |
---|
451 | return jsonrpc_error(ENOENT, "No groupchat with that id."); \ |
---|
452 | } while (0) |
---|
453 | |
---|
454 | static struct groupchat *rpc_chat_with(struct im_connection *ic, char *who) { |
---|
455 | RPC_OUT_INIT("chat_with"); |
---|
456 | struct rpc_groupchat *rc = rpc_groupchat_new(ic, who); |
---|
457 | json_array_append_integer(params, rc->id); |
---|
458 | json_array_append_string(params, who); |
---|
459 | rpc_send(ic, rpc); |
---|
460 | |
---|
461 | return rc->gc; |
---|
462 | } |
---|
463 | |
---|
464 | static struct groupchat *rpc_chat_join(struct im_connection *ic, const char *room, const char *nick, |
---|
465 | const char *password, set_t **sets) { |
---|
466 | RPC_OUT_INIT("chat_join"); |
---|
467 | struct rpc_groupchat *rc = rpc_groupchat_new(ic, room); |
---|
468 | json_array_append_integer(params, rc->id); |
---|
469 | json_array_append_string(params, room); |
---|
470 | json_array_append_string_or_null(params, nick); |
---|
471 | json_array_append_string_or_null(params, password); |
---|
472 | json_array_append_value(params, rpc_ser_settings(sets)); |
---|
473 | rpc_send(ic, rpc); |
---|
474 | |
---|
475 | return rc->gc; |
---|
476 | } |
---|
477 | |
---|
478 | static void rpc_chat_topic(struct groupchat *gc, char *topic) { |
---|
479 | RPC_OUT_INIT("chat_topic"); |
---|
480 | struct rpc_groupchat *rc = gc->data; |
---|
481 | json_array_append_integer(params, rc->id); |
---|
482 | json_array_append_string(params, topic); |
---|
483 | rpc_send(gc->ic, rpc); |
---|
484 | } |
---|
485 | |
---|
486 | static GList *rpc_away_states(struct im_connection *ic) { |
---|
487 | struct rpc_plugin *pd = ic->acc->prpl->data; |
---|
488 | return pd->away_states; |
---|
489 | } |
---|
490 | |
---|
491 | static JSON_Value *rpc_cmd_in(struct im_connection *ic, const char *cmd, JSON_Array *params); |
---|
492 | |
---|
493 | static gboolean rpc_in(struct im_connection *ic, JSON_Object *rpc) { |
---|
494 | const char *cmd = json_object_get_string(rpc, "method"); |
---|
495 | JSON_Value *id = json_object_get_value(rpc, "id"); |
---|
496 | JSON_Value *error = json_object_get_value(rpc, "error"); |
---|
497 | JSON_Array *params = json_object_get_array(rpc, "params"); |
---|
498 | |
---|
499 | /* Removed checks for result/error/etc. as it's all too free-form and |
---|
500 | * at least for now this code is not going to care about retvals as |
---|
501 | * they come in late anyway. */ |
---|
502 | if (!id) { |
---|
503 | imcb_log(ic, "Received invalid JSON-RPC object."); |
---|
504 | imc_logout(ic, TRUE); |
---|
505 | return FALSE; |
---|
506 | } |
---|
507 | |
---|
508 | if (cmd) { |
---|
509 | JSON_Value *resp = rpc_cmd_in(ic, cmd, params); |
---|
510 | if (!resp) { |
---|
511 | resp = json_value_init_object(); |
---|
512 | json_object_set_boolean(json_object(resp), "result", TRUE); |
---|
513 | } |
---|
514 | json_object_set_value(json_object(resp), "id", json_value_deep_copy(id)); |
---|
515 | return rpc_send(ic, resp); |
---|
516 | } else if (error && json_type(error) != JSONNull) { |
---|
517 | char *error_str = json_serialize_to_string(error); |
---|
518 | /* Maybe sanitise/truncate? Though really that should be done at |
---|
519 | * a different layer. */ |
---|
520 | imcb_error(ic, "RPC Error: %s", error_str); |
---|
521 | g_free(error_str); |
---|
522 | } |
---|
523 | |
---|
524 | return TRUE; |
---|
525 | } |
---|
526 | |
---|
527 | static gboolean rpc_in_event(gpointer data, gint fd, b_input_condition cond) { |
---|
528 | struct im_connection *ic = data; |
---|
529 | struct rpc_connection *rd = ic->proto_data; |
---|
530 | char buf[2048]; |
---|
531 | int st; |
---|
532 | |
---|
533 | while ((st = read(rd->fd, buf, sizeof(buf))) > 0) { |
---|
534 | rd->buf = g_realloc(rd->buf, rd->buflen + st + 1); |
---|
535 | memcpy(rd->buf + rd->buflen, buf, st); |
---|
536 | rd->buflen += st; |
---|
537 | } |
---|
538 | |
---|
539 | if (st == 0 || (st == -1 && !(sockerr_again() || errno == EAGAIN))) { |
---|
540 | imcb_log(ic, "Lost RPC connection"); |
---|
541 | imc_logout(ic, TRUE); |
---|
542 | return FALSE; |
---|
543 | } |
---|
544 | rd->buf[rd->buflen] = '\0'; |
---|
545 | |
---|
546 | JSON_Value *parsed; |
---|
547 | const char *end; |
---|
548 | while ((parsed = json_parse_first(rd->buf, &end))) { |
---|
549 | st = rpc_in(ic, json_object(parsed)); |
---|
550 | json_value_free(parsed); |
---|
551 | |
---|
552 | if (!st) |
---|
553 | return FALSE; |
---|
554 | |
---|
555 | if (end == rd->buf + rd->buflen) { |
---|
556 | g_free(rd->buf); |
---|
557 | rd->buf = NULL; |
---|
558 | } else { |
---|
559 | int newlen = rd->buf + rd->buflen - end; |
---|
560 | char new[newlen]; |
---|
561 | memcpy(new, end, newlen); |
---|
562 | rd->buf = g_realloc(rd->buf, newlen + 1); |
---|
563 | memcpy(rd->buf, new, newlen); |
---|
564 | rd->buflen = newlen; |
---|
565 | rd->buf[rd->buflen] = '\0'; |
---|
566 | } |
---|
567 | } |
---|
568 | |
---|
569 | return TRUE; |
---|
570 | } |
---|
571 | |
---|
572 | static JSON_Value *rpc_imcb_log(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
573 | void (*func)(struct im_connection*, const char*, ...) = func_; |
---|
574 | func(ic, "%s", json_array_get_string(params, 0)); |
---|
575 | return NULL; |
---|
576 | } |
---|
577 | |
---|
578 | static JSON_Value *rpc_imcb_connected(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
579 | void (*func)(struct im_connection*) = func_; |
---|
580 | func(ic); |
---|
581 | return NULL; |
---|
582 | } |
---|
583 | |
---|
584 | static JSON_Value *rpc_imc_logout(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
585 | void (*func)(struct im_connection*, gboolean) = func_; |
---|
586 | func(ic, json_array_get_boolean(params, 0)); |
---|
587 | return NULL; |
---|
588 | } |
---|
589 | |
---|
590 | static JSON_Value *rpc_imcb_add_buddy(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
591 | void (*func)(struct im_connection*, const char*, const char*) = func_; |
---|
592 | func(ic, json_array_get_string(params, 0), json_array_get_string(params, 1)); |
---|
593 | return NULL; |
---|
594 | } |
---|
595 | |
---|
596 | static JSON_Value *rpc_imcb_buddy_status(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
597 | void (*func)(struct im_connection*, const char*, int, const char*, const char*) = func_; |
---|
598 | func(ic, json_array_get_string(params, 0), json_array_get_integer(params, 1), |
---|
599 | json_array_get_string(params, 2), json_array_get_string(params, 3)); |
---|
600 | return NULL; |
---|
601 | } |
---|
602 | |
---|
603 | static JSON_Value *rpc_imcb_buddy_times(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
604 | void (*func)(struct im_connection*, const char*, int, int) = func_; |
---|
605 | func(ic, json_array_get_string(params, 0), json_array_get_integer(params, 1), |
---|
606 | json_array_get_integer(params, 2)); |
---|
607 | return NULL; |
---|
608 | } |
---|
609 | |
---|
610 | static JSON_Value *rpc_imcb_buddy_msg(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
611 | void (*func)(struct im_connection*, const char*, const char*, int, int) = func_; |
---|
612 | func(ic, json_array_get_string(params, 0), json_array_get_string(params, 1), |
---|
613 | json_array_get_integer(params, 2), json_array_get_integer(params, 3)); |
---|
614 | return NULL; |
---|
615 | } |
---|
616 | |
---|
617 | static JSON_Value *rpc_imcb_buddy_typing(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
618 | void (*func)(struct im_connection*, const char*, int) = func_; |
---|
619 | func(ic, (char*) json_array_get_string(params, 0), json_array_get_integer(params, 1)); |
---|
620 | return NULL; |
---|
621 | } |
---|
622 | |
---|
623 | static JSON_Value *rpc_imcb_get_local_contacts(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
624 | JSON_Value *resp = json_value_init_object(); |
---|
625 | JSON_Value *arr = json_value_init_array(); |
---|
626 | GSList *contacts = imcb_get_local_contacts(ic); |
---|
627 | GSList *c; |
---|
628 | for (c = contacts; c; c = c->next) { |
---|
629 | json_array_append_string(json_array(arr), (const char*) c->data); |
---|
630 | } |
---|
631 | g_slist_free(contacts); |
---|
632 | json_object_set_value(json_object(resp), "result", arr); |
---|
633 | return resp; |
---|
634 | } |
---|
635 | |
---|
636 | static JSON_Value *rpc_imcb_chat_new(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
637 | struct rpc_groupchat *rc = rpc_groupchat_new(ic, json_array_get_string(params, 0)); |
---|
638 | JSON_Value *resp = json_value_init_object(); |
---|
639 | json_object_set_integer(json_object(resp), "result", rc->id); |
---|
640 | return resp; |
---|
641 | } |
---|
642 | |
---|
643 | static JSON_Value *rpc_imcb_chat_name_hint(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
644 | void (*func)(struct groupchat*, const char*) = func_; |
---|
645 | struct rpc_groupchat *rc; |
---|
646 | SET_GROUPCHAT(rc); |
---|
647 | func(rc->gc, json_array_get_string(params, 1)); |
---|
648 | return NULL; |
---|
649 | } |
---|
650 | |
---|
651 | static JSON_Value *rpc_imcb_chat_msg(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
652 | void (*func)(struct groupchat*, const char*, const char*, guint32, time_t) = func_; |
---|
653 | struct rpc_groupchat *rc; |
---|
654 | SET_GROUPCHAT(rc); |
---|
655 | func(rc->gc, json_array_get_string(params, 1), json_array_get_string(params, 2), |
---|
656 | json_array_get_integer(params, 3), json_array_get_integer(params, 4)); |
---|
657 | return NULL; |
---|
658 | } |
---|
659 | |
---|
660 | static JSON_Value *rpc_imcb_chat_log(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
661 | void (*func)(struct groupchat*, const char*, ...) = func_; |
---|
662 | struct rpc_groupchat *rc; |
---|
663 | SET_GROUPCHAT(rc); |
---|
664 | func(rc->gc, "%s", json_array_get_string(params, 1)); |
---|
665 | return NULL; |
---|
666 | } |
---|
667 | |
---|
668 | static JSON_Value *rpc_imcb_chat_topic(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
669 | void (*func)(struct groupchat*, const char*, const char*, time_t) = func_; |
---|
670 | struct rpc_groupchat *rc; |
---|
671 | SET_GROUPCHAT(rc); |
---|
672 | func(rc->gc, json_array_get_string(params, 1), json_array_get_string(params, 2), |
---|
673 | json_array_get_integer(params, 3)); |
---|
674 | return NULL; |
---|
675 | } |
---|
676 | |
---|
677 | static JSON_Value *rpc_imcb_chat_remove_buddy(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
678 | void (*func)(struct groupchat*, const char*, const char*) = func_; |
---|
679 | struct rpc_groupchat *rc; |
---|
680 | SET_GROUPCHAT(rc); |
---|
681 | func(rc->gc, json_array_get_string(params, 1), json_array_get_string(params, 2)); |
---|
682 | return NULL; |
---|
683 | } |
---|
684 | |
---|
685 | static JSON_Value *rpc_imcb_chat_invite(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
686 | void (*func)(struct groupchat*, const char*, const char*, const char*) = func_; |
---|
687 | struct rpc_groupchat *rc; |
---|
688 | SET_GROUPCHAT(rc); |
---|
689 | func(rc->gc, json_array_get_string(params, 1), json_array_get_string(params, 2), |
---|
690 | json_array_get_string(params, 3)); |
---|
691 | return NULL; |
---|
692 | } |
---|
693 | |
---|
694 | static JSON_Value *rpc_set_getstr(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
695 | char *rets = set_getstr(&ic->acc->set, json_array_get_string(params, 0)); |
---|
696 | JSON_Value *ret = json_value_init_object(); |
---|
697 | if (rets) |
---|
698 | json_object_set_string(json_object(ret), "result", rets); |
---|
699 | else |
---|
700 | json_object_set_null(json_object(ret), "result"); |
---|
701 | return ret; |
---|
702 | } |
---|
703 | |
---|
704 | static JSON_Value *rpc_set_setstr(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
705 | /* Sadly use of const is very poor in BitlBee. :-( */ |
---|
706 | char *newval = g_strdup(json_array_get_string(params, 1)); |
---|
707 | set_setstr(&ic->acc->set, json_array_get_string(params, 0), newval); |
---|
708 | g_free(newval); |
---|
709 | return rpc_set_getstr(ic, func_, params); |
---|
710 | } |
---|
711 | |
---|
712 | static JSON_Value *rpc_set_reset(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
713 | set_reset(&ic->acc->set, json_array_get_string(params, 0)); |
---|
714 | return rpc_set_getstr(ic, func_, params); |
---|
715 | } |
---|
716 | |
---|
717 | static JSON_Value *rpc_bee_user_by_handle(struct im_connection *ic, void *func_, JSON_Array *params) { |
---|
718 | bee_user_t *bu = bee_user_by_handle(ic->bee, ic, json_array_get_string(params, 0)); |
---|
719 | JSON_Value *ret = json_value_init_object(); |
---|
720 | if (bu) |
---|
721 | json_object_set_value(json_object(ret), "result", rpc_ser_bee_user(bu)); |
---|
722 | else |
---|
723 | json_object_set_value(json_object(ret), "error", jsonrpc_error(ENOENT, "Contact not found")); |
---|
724 | return ret; |
---|
725 | } |
---|
726 | |
---|
727 | struct rpc_in_method { |
---|
728 | char *name; |
---|
729 | void *func; |
---|
730 | JSON_Value* (* wfunc) (struct im_connection *ic, void *cmd, JSON_Array *params); |
---|
731 | char args[8]; |
---|
732 | }; |
---|
733 | |
---|
734 | static const struct rpc_in_method methods[] = { |
---|
735 | /* All these RPCs are equivalent of BitlBee C functions but with the |
---|
736 | * struct im_connection* removed as this is in the object context. */ |
---|
737 | { "imcb_log", imcb_log, rpc_imcb_log, "s" }, |
---|
738 | { "imcb_error", imcb_error, rpc_imcb_log, "s" }, |
---|
739 | { "imcb_connected", imcb_connected, rpc_imcb_connected, "" }, |
---|
740 | { "imc_logout", imc_logout, rpc_imc_logout, "b" }, |
---|
741 | { "imcb_add_buddy", imcb_add_buddy, rpc_imcb_add_buddy, "ss" }, |
---|
742 | { "imcb_remove_buddy", imcb_remove_buddy, rpc_imcb_add_buddy, "ss" }, |
---|
743 | { "imcb_rename_buddy", imcb_rename_buddy, rpc_imcb_add_buddy, "ss" }, |
---|
744 | { "imcb_buddy_nick_hint", imcb_buddy_nick_hint, rpc_imcb_add_buddy, "ss" }, |
---|
745 | { "imcb_buddy_status", imcb_buddy_status, rpc_imcb_buddy_status, "siss" }, |
---|
746 | { "imcb_buddy_status_msg", imcb_buddy_status_msg, rpc_imcb_add_buddy, "ss" }, |
---|
747 | { "imcb_buddy_times", imcb_buddy_times, rpc_imcb_buddy_times, "sii" }, |
---|
748 | { "imcb_buddy_msg", imcb_buddy_msg, rpc_imcb_buddy_msg, "ssii" }, |
---|
749 | { "imcb_buddy_typing", imcb_buddy_typing, rpc_imcb_buddy_typing, "si" }, |
---|
750 | { "imcb_get_local_contacts", NULL, rpc_imcb_get_local_contacts, "" }, |
---|
751 | { "imcb_chat_new", NULL, rpc_imcb_chat_new, "s" }, |
---|
752 | |
---|
753 | /* RPCs below are equivalent, but with the struct groupchat* replaced |
---|
754 | * with the numeric id of the chat. */ |
---|
755 | { "imcb_chat_name_hint", imcb_chat_name_hint, rpc_imcb_chat_name_hint, "is" }, |
---|
756 | { "imcb_chat_msg", imcb_chat_msg, rpc_imcb_chat_msg, "issii" }, |
---|
757 | { "imcb_chat_log", imcb_chat_log, rpc_imcb_chat_log, "is" }, |
---|
758 | { "imcb_chat_topic", imcb_chat_topic, rpc_imcb_chat_topic, "issi" }, |
---|
759 | { "imcb_chat_add_buddy", imcb_chat_add_buddy, rpc_imcb_chat_name_hint, "is" }, |
---|
760 | { "imcb_chat_remove_buddy", imcb_chat_remove_buddy, rpc_imcb_chat_remove_buddy, "iss" }, |
---|
761 | { "imcb_chat_invite", imcb_chat_invite, rpc_imcb_chat_invite, "isss" }, |
---|
762 | |
---|
763 | /* These are not imcb* functions but should still be exported. */ |
---|
764 | /* Setting functions. Starting with just providing access to account |
---|
765 | * settings. See later whether access to chat/chan settings is necessary. |
---|
766 | * All of these will return the (new) value of given setting. */ |
---|
767 | { "set_getstr", NULL, rpc_set_getstr, "s" }, |
---|
768 | { "set_setstr", NULL, rpc_set_setstr, "ss" }, |
---|
769 | { "set_reset", NULL, rpc_set_reset, "s" }, |
---|
770 | |
---|
771 | { "bee_user_by_handle", NULL, rpc_bee_user_by_handle, "s" }, |
---|
772 | |
---|
773 | { NULL }, |
---|
774 | }; |
---|
775 | |
---|
776 | static JSON_Value *rpc_cmd_in(struct im_connection *ic, const char *cmd, JSON_Array *params) { |
---|
777 | int i; |
---|
778 | |
---|
779 | for (i = 0; methods[i].name; i++) { |
---|
780 | if (strcmp(cmd, methods[i].name) == 0) { |
---|
781 | if (json_array_get_count(params) != strlen(methods[i].args)) { |
---|
782 | imcb_error(ic, "Invalid argument count to method %s: %d, wanted %zd", cmd, (int) json_array_get_count(params), strlen(methods[i].args)); |
---|
783 | return jsonrpc_error(E2BIG, "Invalid integer of arguments"); |
---|
784 | } |
---|
785 | int j; |
---|
786 | for (j = 0; methods[i].args[j]; j++) { |
---|
787 | JSON_Value_Type type = json_value_get_type(json_array_get_value(params, j)); |
---|
788 | gboolean ok = FALSE; |
---|
789 | switch (methods[i].args[j]) { |
---|
790 | case 's': |
---|
791 | ok = type == JSONString || type == JSONNull; |
---|
792 | break; |
---|
793 | case 'i': |
---|
794 | ok = type == JSONInteger; |
---|
795 | break; |
---|
796 | case 'o': |
---|
797 | ok = type == JSONObject; |
---|
798 | break; |
---|
799 | case 'a': |
---|
800 | ok = type == JSONArray; |
---|
801 | break; |
---|
802 | case 'b': |
---|
803 | ok = type == JSONBoolean; |
---|
804 | break; |
---|
805 | } |
---|
806 | if (!ok) { |
---|
807 | // This error sucks, but just get your types right! |
---|
808 | imcb_error(ic, "Invalid argument type, %s parameter %d: %d not %c", cmd, j, type, methods[i].args[j]); |
---|
809 | return jsonrpc_error(EINVAL, "Invalid argument type"); |
---|
810 | } |
---|
811 | } |
---|
812 | return methods[i].wfunc(ic, methods[i].func, params); |
---|
813 | } |
---|
814 | } |
---|
815 | return jsonrpc_error(ENOSYS, "Function not implemented"); |
---|
816 | } |
---|
817 | |
---|
818 | #define RPC_ADD_FUNC(func) \ |
---|
819 | ret->func = rpc_ ## func |
---|
820 | #define RPC_ADD_OPT_FUNC(func) \ |
---|
821 | if (g_hash_table_lookup(methods, #func)) \ |
---|
822 | RPC_ADD_FUNC(func) |
---|
823 | |
---|
824 | static JSON_Value *rpc_init_isup() { |
---|
825 | int i; |
---|
826 | |
---|
827 | RPC_OUT_INIT("init"); |
---|
828 | JSON_Value *d = json_value_init_object(); |
---|
829 | json_object_set_string(json_object(d), "version_str", BITLBEE_VERSION); |
---|
830 | json_object_set_integer(json_object(d), "version", BITLBEE_VERSION_CODE); |
---|
831 | |
---|
832 | JSON_Value *ml = json_value_init_array(); |
---|
833 | for (i = 0; methods[i].name; i++) { |
---|
834 | json_array_append_string(json_array(ml), methods[i].name); |
---|
835 | } |
---|
836 | json_object_set_value(json_object(d), "method_list", ml); |
---|
837 | json_array_append_value(params, d); |
---|
838 | |
---|
839 | return rpc; |
---|
840 | } |
---|
841 | |
---|
842 | gboolean rpc_initmodule_sock(struct sockaddr *address, socklen_t addrlen) { |
---|
843 | int st, fd, i; |
---|
844 | |
---|
845 | fd = socket(address->sa_family, SOCK_STREAM, 0); |
---|
846 | if (fd == -1 || connect(fd, address, addrlen) == -1) { |
---|
847 | log_message(LOGLVL_WARNING, "Failed to connect to RPC server: %s", strerror(errno)); |
---|
848 | if (fd != -1) |
---|
849 | closesocket(fd); |
---|
850 | return FALSE; |
---|
851 | } |
---|
852 | |
---|
853 | JSON_Value *rpc = rpc_init_isup(); |
---|
854 | char *s = json_serialize_to_string(rpc); |
---|
855 | json_value_free(rpc); |
---|
856 | |
---|
857 | int len = strlen(s); |
---|
858 | s = g_realloc(s, len + 3); |
---|
859 | strcpy(s + len, "\r\n"); |
---|
860 | len += 2; |
---|
861 | |
---|
862 | if ((st = write(fd, s, len)) != len) { |
---|
863 | log_message(LOGLVL_WARNING, "Error while writing to RPC server: %s", strerror(errno)); |
---|
864 | return FALSE; |
---|
865 | } |
---|
866 | g_free(s); |
---|
867 | |
---|
868 | char *resp = NULL; |
---|
869 | int buflen = 4096, resplen = 0; |
---|
870 | JSON_Value *parsed; |
---|
871 | do { |
---|
872 | fd_set rfds; |
---|
873 | struct timeval to; |
---|
874 | |
---|
875 | FD_ZERO(&rfds); |
---|
876 | FD_SET(fd, &rfds); |
---|
877 | to.tv_sec = 1; |
---|
878 | to.tv_usec = 0; |
---|
879 | st = select(fd + 1, &rfds, NULL, NULL, &to); |
---|
880 | |
---|
881 | if (st == 0) { |
---|
882 | log_message(LOGLVL_WARNING, "Error while reading from RPC server: %s", strerror(errno)); |
---|
883 | closesocket(fd); |
---|
884 | return FALSE; |
---|
885 | } |
---|
886 | |
---|
887 | if (resplen >= buflen) |
---|
888 | buflen *= 2; |
---|
889 | resp = g_realloc(resp, buflen + 1); |
---|
890 | st = read(fd, resp + resplen, buflen - resplen); |
---|
891 | if (st == -1) { |
---|
892 | if (sockerr_again()) |
---|
893 | continue; |
---|
894 | log_message(LOGLVL_WARNING, "Error while reading from RPC server: %s", strerror(errno)); |
---|
895 | closesocket(fd); |
---|
896 | return FALSE; |
---|
897 | } |
---|
898 | resplen += st; |
---|
899 | resp[resplen] = '\0'; |
---|
900 | } |
---|
901 | while (!(parsed = json_parse_string(resp))); |
---|
902 | closesocket(fd); |
---|
903 | |
---|
904 | JSON_Object *isup = json_object_get_object(json_object(parsed), "result"); |
---|
905 | if (isup == NULL) { |
---|
906 | log_message(LOGLVL_WARNING, "Error while parsing RPC server response"); |
---|
907 | return FALSE; |
---|
908 | } |
---|
909 | |
---|
910 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
911 | struct rpc_plugin *proto_data = g_new0(struct rpc_plugin, 1); |
---|
912 | proto_data->addr = g_memdup(address, addrlen); |
---|
913 | proto_data->addrlen = addrlen; |
---|
914 | ret->name = g_strdup(json_object_get_string(isup, "name")); |
---|
915 | ret->data = proto_data; |
---|
916 | |
---|
917 | proto_data->account_flags = json_object_get_integer(isup, "account_flags"); |
---|
918 | |
---|
919 | /* Keep a full copy of the settings list, we can only use it when we |
---|
920 | * have an account to work on. */ |
---|
921 | JSON_Value *settings = json_object_get_value(isup, "settings"); |
---|
922 | if (json_type(settings) == JSONObject) |
---|
923 | proto_data->settings = json_value_deep_copy(settings); |
---|
924 | |
---|
925 | JSON_Array *aways_a = json_object_get_array(isup, "away_state_list"); |
---|
926 | for (i = 0; i < json_array_get_count(aways_a); ++i) { |
---|
927 | JSON_Value *state = json_array_get_value(aways_a, i); |
---|
928 | if (json_type(state) == JSONString) |
---|
929 | proto_data->away_states = |
---|
930 | g_list_append(proto_data->away_states, |
---|
931 | g_strdup(json_string(state))); |
---|
932 | } |
---|
933 | |
---|
934 | JSON_Array *methods_a = json_object_get_array(isup, "method_list"); |
---|
935 | GHashTable *methods = g_hash_table_new(g_str_hash, g_str_equal); |
---|
936 | for (i = 0; i < json_array_get_count(methods_a); i++) { |
---|
937 | gpointer func = (void*) json_array_get_string(methods_a, i); |
---|
938 | g_hash_table_replace(methods, func, func); |
---|
939 | } |
---|
940 | |
---|
941 | ret->init = rpc_init; |
---|
942 | RPC_ADD_FUNC(login); |
---|
943 | RPC_ADD_OPT_FUNC(keepalive); |
---|
944 | RPC_ADD_FUNC(logout); |
---|
945 | RPC_ADD_FUNC(buddy_msg); |
---|
946 | RPC_ADD_OPT_FUNC(set_away); |
---|
947 | RPC_ADD_OPT_FUNC(send_typing); |
---|
948 | RPC_ADD_OPT_FUNC(add_buddy); |
---|
949 | RPC_ADD_OPT_FUNC(remove_buddy); |
---|
950 | RPC_ADD_OPT_FUNC(add_permit); |
---|
951 | RPC_ADD_OPT_FUNC(add_deny); |
---|
952 | RPC_ADD_OPT_FUNC(rem_permit); |
---|
953 | RPC_ADD_OPT_FUNC(rem_deny); |
---|
954 | RPC_ADD_OPT_FUNC(get_info); |
---|
955 | RPC_ADD_OPT_FUNC(chat_invite); |
---|
956 | RPC_ADD_OPT_FUNC(chat_kick); |
---|
957 | RPC_ADD_OPT_FUNC(chat_leave); |
---|
958 | RPC_ADD_OPT_FUNC(chat_msg); |
---|
959 | RPC_ADD_OPT_FUNC(chat_with); |
---|
960 | RPC_ADD_OPT_FUNC(chat_join); |
---|
961 | RPC_ADD_OPT_FUNC(chat_topic); |
---|
962 | if (proto_data->away_states) |
---|
963 | ret->away_states = rpc_away_states; |
---|
964 | |
---|
965 | g_hash_table_destroy(methods); |
---|
966 | |
---|
967 | // TODO: Property for a few standard nickcmp implementations. |
---|
968 | ret->handle_cmp = g_ascii_strcasecmp; |
---|
969 | |
---|
970 | register_protocol(ret); |
---|
971 | |
---|
972 | return TRUE; |
---|
973 | } |
---|
974 | |
---|
975 | #define PDIR "/tmp/rpcplugins" |
---|
976 | |
---|
977 | /* YA RLY :-/ */ |
---|
978 | #ifndef UNIX_PATH_MAX |
---|
979 | struct sockaddr_un sizecheck; |
---|
980 | #define UNIX_PATH_MAX sizeof(sizecheck.sun_path) |
---|
981 | #endif |
---|
982 | |
---|
983 | void rpc_initmodule() { |
---|
984 | DIR *pdir = opendir(PDIR); |
---|
985 | struct dirent *de; |
---|
986 | |
---|
987 | if (!pdir) |
---|
988 | return; |
---|
989 | |
---|
990 | while ((de = readdir(pdir))) { |
---|
991 | if (de->d_type != DT_SOCK && de->d_type != DT_UNKNOWN) |
---|
992 | continue; |
---|
993 | |
---|
994 | char *fn = g_build_filename(PDIR, de->d_name, NULL); |
---|
995 | struct sockaddr_un su; |
---|
996 | |
---|
997 | strncpy(su.sun_path, fn, UNIX_PATH_MAX); |
---|
998 | |
---|
999 | #if 0 |
---|
1000 | struct stat fdata; |
---|
1001 | if (stat(fn, &fdata) == -1) { |
---|
1002 | log_message(LOGLVL_WARNING, "Could not stat %s: %s", fn, strerror(errno)); |
---|
1003 | g_free(fn); |
---|
1004 | continue; |
---|
1005 | } |
---|
1006 | /* For now just skip anything that is not a Unix domain socket. */ |
---|
1007 | if (!S_ISSOCK(fdata.st_mode)) |
---|
1008 | continue; |
---|
1009 | #endif |
---|
1010 | |
---|
1011 | su.sun_path[UNIX_PATH_MAX-1] = '\0'; |
---|
1012 | su.sun_family = AF_UNIX; |
---|
1013 | gboolean st = rpc_initmodule_sock((struct sockaddr*) &su, sizeof(su)); |
---|
1014 | g_free(fn); |
---|
1015 | if (!st) |
---|
1016 | log_message(LOGLVL_WARNING, "Failed to register protocol %s", fn); |
---|
1017 | /* Idea: Also support textfiles containing a host:port tuple to |
---|
1018 | * connect to. Not that remote RPC'ing would be a great idea, |
---|
1019 | * but maybe some jsonrpc libs don't support Unix domain sockets. */ |
---|
1020 | } |
---|
1021 | closedir(pdir); |
---|
1022 | } |
---|
1023 | |
---|