Changeset 16652e9


Ignore:
Timestamp:
2015-04-06T01:12:29Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
f3af614
Parents:
bc73e2ba
Message:

More improvements: Settings, methods list in init.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/rpc/rpc.c

    rbc73e2ba r16652e9  
    77#include "parson.h"
    88
     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
    913static int next_rpc_id = 1;
    1014
     
    1216        struct sockaddr *addr;
    1317        socklen_t addrlen;
     18        JSON_Value *settings;
    1419};
    1520
     
    4348}
    4449
    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.
     51static void json_array_append_string_or_null(JSON_Array *array, const char *string) {
    4652        if (string)
    47                 json_array_append_string(params, string);
     53                json_array_append_string(array, string);
    4854        else
    49                 json_array_append_null(params);
     55                json_array_append_null(array);
     56}
     57
     58static 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);
    5063}
    5164
     
    8497
    8598        if (st != len) {
    86                 imcb_log(ic, "Write error");
     99                if (!(ic->flags & OPT_LOGGING_OUT))
     100                        imcb_log(ic, "Write error");
    87101                imc_logout(ic, TRUE);
    88102                return FALSE;
     
    92106}
    93107
    94 static JSON_Value *rpc_ser_account(const account_t *acc) {
     108static 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
     119static JSON_Value *rpc_ser_account(account_t *acc) {
    95120        JSON_Value *v = json_value_init_object();
    96121        JSON_Object *o = json_object(v);
     
    99124        if (acc->server)
    100125                json_object_set_string(o, "server", acc->server);
     126        json_object_set_value(o, "settings", rpc_ser_settings(&acc->set));
    101127        return v;
    102128}
    103129
    104130static 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        }
    106137}
    107138
    108139static gboolean rpc_login_cb(gpointer data, gint fd, b_input_condition cond);
    109140static gboolean rpc_in_event(gpointer data, gint fd, b_input_condition cond);
     141static JSON_Value *rpc_init_isup();
    110142
    111143static void rpc_login(account_t *acc) {
     
    127159        struct im_connection *ic = data;
    128160        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
    129168        RPC_OUT_INIT("login");
    130169        json_array_append_value(params, rpc_ser_account(ic->acc));
    131         rpc_send(ic, rpc);
     170        if (!rpc_send(ic, rpc))
     171                return FALSE;
    132172
    133173        ic->inpa = b_input_add(rd->fd, B_EV_IO_READ, rpc_in_event, ic);
     
    298338        json_array_append_string_or_null(params, nick);
    299339        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));
    301341        rpc_send(ic, rpc);
    302342
     
    584624#define RPC_ADD_FUNC(func) \
    585625        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
     628static JSON_Value *rpc_init_isup() {
     629        int i;
    594630
    595631        RPC_OUT_INIT("init");
     
    597633        json_object_set_string(json_object(d), "version_str", BITLBEE_VERSION);
    598634        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);
    599641        json_array_append_value(params, d);
     642
     643        return rpc;
     644}
     645
     646void 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();
    600654        char *s = json_serialize_to_string(rpc);
     655        json_value_free(rpc);
     656
    601657        int len = strlen(s);
    602658        s = g_realloc(s, len + 3);
     
    692748        ret->data = proto_data;
    693749
    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);
    699755
    700756        register_protocol(ret);
Note: See TracChangeset for help on using the changeset viewer.