Changeset 578e5b0


Ignore:
Timestamp:
2015-05-14T16:08:06Z (4 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
600e09c
Parents:
4225bec
Message:

Try to send settings as native types instead of always strings.

This is still ugly for most settings not owned by the plugin but that's
simply how this is implemented in BitlBee. Let's see how annoying and
painful this is really going to be.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • lib/misc.c

    r4225bec r578e5b0  
    427427}
    428428
    429 int is_bool(char *value)
     429int is_bool(const char *value)
    430430{
    431431        if (*value == 0) {
     
    453453}
    454454
    455 int bool2int(char *value)
     455int bool2int(const char *value)
    456456{
    457457        int i;
  • lib/misc.h

    r4225bec r578e5b0  
    137137G_MODULE_EXPORT void random_bytes(unsigned char *buf, int count);
    138138
    139 G_MODULE_EXPORT int is_bool(char *value);
    140 G_MODULE_EXPORT int bool2int(char *value);
     139G_MODULE_EXPORT int is_bool(const char *value);
     140G_MODULE_EXPORT int bool2int(const char *value);
    141141
    142142G_MODULE_EXPORT struct ns_srv_reply **srv_lookup(char *service, char *protocol, char *domain);
  • protocols/rpc/rpc.c

    r4225bec r578e5b0  
    120120}
    121121
    122 static JSON_Value *rpc_ser_settings(set_t **set) {
    123         const set_t *s;
    124         JSON_Value *ret = json_value_init_object();
    125        
    126         for (s = *set; s; s = s->next) {
    127                 json_object_set_string_or_null(json_object(ret), s->key, set_getstr(set, s->key));
    128         }
    129 
    130         return ret;
    131 }
     122static JSON_Value *rpc_ser_settings(set_t **set);
    132123
    133124static JSON_Value *rpc_ser_account(account_t *acc) {
     
    189180}
    190181
    191 static char *rpc_set_evaluator(set_t *set, char *value) {
     182set_eval rpc_type_set_eval(const set_t *set) {
    192183        JSON_Object *o = set->eval_data;
    193184        const char *type = json_object_get_string(o, "type");
    194185
    195         /* Just allow two simple int/bool evaluators with no protocol awareness. */
    196186        set_eval type_eval = NULL;
    197187        if (type == NULL) {
    198         } else if (strncmp(type, "int", 3) == 0) {
     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")) {
    199193                type_eval = set_eval_int;
    200         } else if (strncmp(type, "bool", 4) == 0) {
     194        } else if (g_str_has_prefix(type, "bool")) {
    201195                type_eval = set_eval_bool;
    202196        }
    203197
     198        return type_eval;
     199}
     200
     201static 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
     219static char *rpc_set_evaluator(set_t *set, char *value) {
     220        set_eval type_eval = rpc_type_set_eval(set);
    204221        if (type_eval) {
    205222                char *new = type_eval(set, value);
     
    215232                RPC_OUT_INIT("set_set");
    216233                json_array_append_string(params, set->key);
    217                 if (type_eval == set_eval_int) {
    218                         int num = 0;
    219                         /* Evaluator already did validation so ignore retval. */
    220                         sscanf(value, "%d", &num);
    221                         json_array_append_integer(params, num);
    222                 } else if (type_eval == set_eval_bool) {
    223                         json_array_append_boolean(params, bool2int(value));
    224                 } else {
    225                         json_array_append_string(params, value);
    226                 }
     234                json_array_append_value(params, set_make_json_value(rpc_type_set_eval(set), value));
    227235                rpc_send(acc->ic, rpc);
    228236        }
    229237
    230238        return value;
     239}
     240
     241static 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;
    231251}
    232252
     
    239259        struct rpc_connection *rd = ic->proto_data = g_new0(struct rpc_connection, 1);
    240260        struct rpc_plugin *pd = acc->prpl->data;
    241         imcb_log(ic, "Connecting to RPC server");
     261        imcb_log(ic, "Logging in via RPC server");
    242262        rd->fd = socket(pd->addr->sa_family, SOCK_STREAM, 0);
    243263        sock_make_nonblocking(rd->fd);
Note: See TracChangeset for help on using the changeset viewer.