Changeset f15553d


Ignore:
Timestamp:
2015-04-12T15:04:55Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
531eabd
Parents:
c5a7b8d
Message:

Common set evaluator that sends updates to the RPC server if online.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • protocols/rpc/rpc.c

    rc5a7b8d rf15553d  
    155155}
    156156
     157static char *rpc_set_evaluator(set_t *set, char *value);
     158
    157159static void rpc_init(account_t *acc) {
    158160        struct rpc_plugin *pd = acc->prpl->data;
     
    164166                 * terrible idea. As was JSON, but hey. */
    165167                set->flags |= (int) json_object_get_number(o, "flags");
    166                 const char *eval = json_object_get_string(o, "type");
    167                 if (eval == NULL) {
    168                 } else if (strcmp(eval, "int") == 0) {
    169                         set->eval = set_eval_int;
    170                 } else if (strcmp(eval, "bool") == 0) {
    171                         set->eval = set_eval_bool;
    172                 } else {
    173                         /* Default is string which means no evaluator. */
    174                 }
     168                set->eval = rpc_set_evaluator;
     169                set->eval_data = o;
    175170                /* eval_list turns out to be a memory leak so don't implement it
    176171                 * for now.
     
    184179
    185180        acc->flags |= pd->account_flags;
     181}
     182
     183static char *rpc_set_evaluator(set_t *set, char *value) {
     184        JSON_Object *o = set->eval_data;
     185        const char *type = json_object_get_string(o, "type");
     186
     187        /* Just allow two simple int/bool evaluators with no protocol awareness. */
     188        set_eval type_eval = NULL;
     189        if (type == NULL) {
     190        } else if (strncmp(type, "int", 3) == 0) {
     191                type_eval = set_eval_int;
     192        } else if (strncmp(type, "bool", 4) == 0) {
     193                type_eval = set_eval_bool;
     194        }
     195
     196        if (type_eval) {
     197                char *new = type_eval(set, value);
     198                if (new == SET_INVALID) {
     199                        return SET_INVALID;
     200                }
     201        }
     202
     203        account_t *acc = set->data;
     204        if (acc->ic) {
     205                /* But do send RPCs to the plugin for each changed setting so
     206                 * it always has up-to-date values. */
     207                RPC_OUT_INIT("set_set");
     208                json_array_append_string(params, set->key);
     209                if (type_eval == set_eval_int) {
     210                        int num = 0;
     211                        /* Evaluator already did validation so ignore retval. */
     212                        sscanf(value, "%d", &num);
     213                        json_array_append_number(params, num);
     214                } else if (type_eval == set_eval_bool) {
     215                        json_array_append_boolean(params, bool2int(value));
     216                } else {
     217                        json_array_append_string(params, value);
     218                }
     219                rpc_send(acc->ic, rpc);
     220        }
     221
     222        return value;
    186223}
    187224
  • python/implugin.py

    rc5a7b8d rf15553d  
    7777                                "oauth": {
    7878                                        "default": "off",
     79                                        "type": "bool",
    7980                                },
    8081                                "test": {
    8182                                        "default": "123",
     83                                        "type": "int",
    8284                                },
     85                                "stringetje": {
     86                                        "default": "testje",
     87                                        "flags": 0x04,
     88                                }
    8389                        },
    8490                }
     
    104110        def set_away(self, state, message):
    105111                print "You're a slacker: %s (%r)" % (state, message)
     112       
     113        def set_set(self, setting, value):
     114                print "Setting %s changed to %r" % (setting, value)
    106115
    107116def RunPlugin(plugin, debug=True):
Note: See TracChangeset for help on using the changeset viewer.