Changeset 16652e9
- Timestamp:
- 2015-04-06T01:12:29Z (10 years ago)
- Children:
- f3af614
- Parents:
- bc73e2ba
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/rpc/rpc.c
rbc73e2ba r16652e9 7 7 #include "parson.h" 8 8 9 #define JSON_O_FOREACH(o, k, v) \ 10 const char *k; const JSON_Value *v; int __i; \ 11 for (__i = 0; json_object_get_tuple(o, __i, &k, &v); __i++) 12 9 13 static int next_rpc_id = 1; 10 14 … … 12 16 struct sockaddr *addr; 13 17 socklen_t addrlen; 18 JSON_Value *settings; 14 19 }; 15 20 … … 43 48 } 44 49 45 static void json_array_append_string_or_null(JSON_Array *params, const char *string) { 50 // Might have liked to have this one in the library for optional values/etc. 51 static void json_array_append_string_or_null(JSON_Array *array, const char *string) { 46 52 if (string) 47 json_array_append_string( params, string);53 json_array_append_string(array, string); 48 54 else 49 json_array_append_null(params); 55 json_array_append_null(array); 56 } 57 58 static void json_object_set_string_or_null(JSON_Object *object, const char *key, const char *string) { 59 if (string) 60 json_object_set_string(object, key, string); 61 else 62 json_object_set_null(object, key); 50 63 } 51 64 … … 84 97 85 98 if (st != len) { 86 imcb_log(ic, "Write error"); 99 if (!(ic->flags & OPT_LOGGING_OUT)) 100 imcb_log(ic, "Write error"); 87 101 imc_logout(ic, TRUE); 88 102 return FALSE; … … 92 106 } 93 107 94 static JSON_Value *rpc_ser_account(const account_t *acc) { 108 static JSON_Value *rpc_ser_settings(set_t **set) { 109 const set_t *s; 110 JSON_Value *ret = json_value_init_object(); 111 112 for (s = *set; s; s = s->next) { 113 json_object_set_string_or_null(json_object(ret), s->key, set_getstr(set, s->key)); 114 } 115 116 return ret; 117 } 118 119 static JSON_Value *rpc_ser_account(account_t *acc) { 95 120 JSON_Value *v = json_value_init_object(); 96 121 JSON_Object *o = json_object(v); … … 99 124 if (acc->server) 100 125 json_object_set_string(o, "server", acc->server); 126 json_object_set_value(o, "settings", rpc_ser_settings(&acc->set)); 101 127 return v; 102 128 } 103 129 104 130 static void rpc_init(account_t *acc) { 105 // Add settings. Probably should not RPC at all. 131 struct rpc_plugin *pd = acc->prpl->data; 132 133 JSON_O_FOREACH(json_object(pd->settings), name, value) { 134 JSON_Object *o = json_object(value); 135 set_t *set = set_add(&acc->set, name, json_object_get_string(o, "default"), NULL, acc); 136 } 106 137 } 107 138 108 139 static gboolean rpc_login_cb(gpointer data, gint fd, b_input_condition cond); 109 140 static gboolean rpc_in_event(gpointer data, gint fd, b_input_condition cond); 141 static JSON_Value *rpc_init_isup(); 110 142 111 143 static void rpc_login(account_t *acc) { … … 127 159 struct im_connection *ic = data; 128 160 struct rpc_connection *rd = ic->proto_data; 161 162 /* Need to repeat this since each IM connection means an actual new 163 * RPC session. */ 164 JSON_Value *init = rpc_init_isup(); 165 if (!rpc_send(ic, init)) 166 return FALSE; 167 129 168 RPC_OUT_INIT("login"); 130 169 json_array_append_value(params, rpc_ser_account(ic->acc)); 131 rpc_send(ic, rpc); 170 if (!rpc_send(ic, rpc)) 171 return FALSE; 132 172 133 173 ic->inpa = b_input_add(rd->fd, B_EV_IO_READ, rpc_in_event, ic); … … 298 338 json_array_append_string_or_null(params, nick); 299 339 json_array_append_string_or_null(params, password); 300 //json_array_append_value(params, rpc_ser_sets(sets));340 json_array_append_value(params, rpc_ser_settings(sets)); 301 341 rpc_send(ic, rpc); 302 342 … … 584 624 #define RPC_ADD_FUNC(func) \ 585 625 if (g_hash_table_contains(methods, #func)) \ 586 ret->func = rpc_ ## func 587 588 void rpc_initmodule_sock(struct sockaddr *address, socklen_t addrlen) { 589 int st, fd, i; 590 591 fd = socket(address->sa_family, SOCK_STREAM, 0); 592 if (fd == -1 || connect(fd, address, addrlen) == -1) 593 return; 626 ret->func = rpc_ ## func 627 628 static JSON_Value *rpc_init_isup() { 629 int i; 594 630 595 631 RPC_OUT_INIT("init"); … … 597 633 json_object_set_string(json_object(d), "version_str", BITLBEE_VERSION); 598 634 json_object_set_number(json_object(d), "version", BITLBEE_VERSION_CODE); 635 636 JSON_Value *ml = json_value_init_array(); 637 for (i = 0; methods[i].name; i++) { 638 json_array_append_string(json_array(ml), methods[i].name); 639 } 640 json_object_set_value(json_object(d), "method_list", ml); 599 641 json_array_append_value(params, d); 642 643 return rpc; 644 } 645 646 void rpc_initmodule_sock(struct sockaddr *address, socklen_t addrlen) { 647 int st, fd, i; 648 649 fd = socket(address->sa_family, SOCK_STREAM, 0); 650 if (fd == -1 || connect(fd, address, addrlen) == -1) 651 return; 652 653 JSON_Value *rpc = rpc_init_isup(); 600 654 char *s = json_serialize_to_string(rpc); 655 json_value_free(rpc); 656 601 657 int len = strlen(s); 602 658 s = g_realloc(s, len + 3); … … 692 748 ret->data = proto_data; 693 749 694 JSON_Array *settings = json_object_get_array(isup, "settings");695 for (i = 0; i < json_array_get_count(settings); i++) {696 //JSON_Object *set = json_array_get_object(settings, i);697 // set..name, set..type, set..default, set..flags ?698 }750 /* Keep a full copy of the settings list, we can only use it when we 751 * have an account to work on. */ 752 JSON_Value *settings = json_object_get_value(isup, "settings"); 753 if (json_type(settings) == JSONObject) 754 proto_data->settings = json_value_deep_copy(settings); 699 755 700 756 register_protocol(ret);
Note: See TracChangeset
for help on using the changeset viewer.