Changeset 0c85c08


Ignore:
Timestamp:
2010-08-31T23:18:21Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
934db064
Parents:
f5c0d8e
Message:

Pluginify this thing a little bit. Not so much in the dynamically loadable
sense of the word, more in a way that core files don't have to include otr.h.

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • bitlbee.h

    rf5c0d8e r0c85c08  
    142142#include "misc.h"
    143143#include "proxy.h"
    144 #include "otr.h"
    145144
    146145typedef struct global {
     
    154153        char *helpfile;
    155154        int restart;
    156         OtrlMessageAppOps otr_ops;   /* collects interface functions required by OTR */
    157155} global_t;
    158156
  • irc.c

    rf5c0d8e r0c85c08  
    2929
    3030GSList *irc_connection_list;
     31GSList *irc_plugins;
    3132
    3233static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond );
     
    4243        char *host = NULL, *myhost = NULL;
    4344        irc_user_t *iu;
     45        GSList *l;
    4446        set_t *s;
    4547        bee_t *b;
     
    164166        nogaim_init();
    165167       
     168        for( l = irc_plugins; l; l = l->next )
     169        {
     170                irc_plugin_t *p = l->data;
     171                if( p->irc_new )
     172                        p->irc_new( irc );
     173        }
     174       
    166175        return irc;
    167176}
     
    207216void irc_free( irc_t * irc )
    208217{
     218        GSList *l;
     219       
    209220        irc->status |= USTATUS_SHUTDOWN;
    210221       
    211222        log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd );
     223       
     224        for( l = irc_plugins; l; l = l->next )
     225        {
     226                irc_plugin_t *p = l->data;
     227                if( p->irc_free )
     228                        p->irc_free( irc );
     229        }
    212230       
    213231        if( irc->status & USTATUS_IDENTIFIED && set_getbool( &irc->b->set, "save_on_quit" ) )
     
    932950        return SET_INVALID;
    933951}
     952
     953void register_irc_plugin( const struct irc_plugin *p )
     954{
     955        irc_plugins = g_slist_prepend( irc_plugins, (gpointer) p );
     956}
  • irc.h

    rf5c0d8e r0c85c08  
    2626#ifndef _IRC_H
    2727#define _IRC_H
    28 
    29 #include "otr.h"
    3028
    3129#define IRC_MAX_LINE 512
     
    8886        gint login_source_id; /* To slightly delay some events at login time. */
    8987       
    90         otr_t *otr;            /* OTR state and book keeping */
     88        struct otr *otr; /* OTR state and book keeping, used by the OTR plugin.
     89                            TODO: Some mechanism for plugindata. */
    9190       
    9291        struct bee *b;
     
    221220} irc_channel_del_user_type_t;
    222221
     222/* These are a glued a little bit to the core/bee layer and a little bit to
     223   IRC. The first user is OTR, and I guess at some point we'll get to shape
     224   this a little bit more as other uses come up. */
     225typedef struct irc_plugin
     226{
     227        /* Called at the end of irc_new(). Can be used to add settings, etc. */
     228        gboolean (*irc_new)( irc_t *irc );
     229        /* At the end of irc_free(). */
     230        void (*irc_free)( irc_t *irc );
     231       
     232        /* Called by bee_irc_user_privmsg_cb(). Return NULL if you want to
     233           abort sending the msg. */
     234        char* (*filter_msg_out)( irc_user_t *iu, const char *msg, int flags );
     235        /* Called by bee_irc_user_msg(). Return NULL if you swallowed the
     236           message and don't want anything to go to the user. */
     237        char* (*filter_msg_in)( irc_user_t *iu, const char *msg, int flags );
     238} irc_plugin_t;
     239
     240extern GSList *irc_plugins; /* struct irc_plugin */
     241
    223242/* irc.c */
    224243extern GSList *irc_connection_list;
     
    245264
    246265void irc_umode_set( irc_t *irc, const char *s, gboolean allow_priv );
     266
     267void register_irc_plugin( const struct irc_plugin *p );
    247268
    248269/* irc_channel.c */
  • otr.c

    rf5c0d8e r0c85c08  
    109109} pair_t;       
    110110
     111static OtrlMessageAppOps otr_ops;   /* collects interface functions required by OTR */
     112
    111113
    112114/** misc. helpers/subroutines: **/
     
    174176OtrlPrivKey *match_privkey(irc_t *irc, const char **args);
    175177
     178/* functions to be called for certain events */
     179static const struct irc_plugin otr_plugin;
     180
    176181
    177182/*** routines declared in otr.h: ***/
     
    182187       
    183188        /* fill global OtrlMessageAppOps */
    184         global.otr_ops.policy = &op_policy;
    185         global.otr_ops.create_privkey = &op_create_privkey;
    186         global.otr_ops.is_logged_in = &op_is_logged_in;
    187         global.otr_ops.inject_message = &op_inject_message;
    188         global.otr_ops.notify = NULL;
    189         global.otr_ops.display_otr_message = &op_display_otr_message;
    190         global.otr_ops.update_context_list = NULL;
    191         global.otr_ops.protocol_name = NULL;
    192         global.otr_ops.protocol_name_free = NULL;
    193         global.otr_ops.new_fingerprint = &op_new_fingerprint;
    194         global.otr_ops.write_fingerprints = &op_write_fingerprints;
    195         global.otr_ops.gone_secure = &op_gone_secure;
    196         global.otr_ops.gone_insecure = &op_gone_insecure;
    197         global.otr_ops.still_secure = &op_still_secure;
    198         global.otr_ops.log_message = &op_log_message;
    199         global.otr_ops.max_message_size = &op_max_message_size;
    200         global.otr_ops.account_name = &op_account_name;
    201         global.otr_ops.account_name_free = NULL;
     189        otr_ops.policy = &op_policy;
     190        otr_ops.create_privkey = &op_create_privkey;
     191        otr_ops.is_logged_in = &op_is_logged_in;
     192        otr_ops.inject_message = &op_inject_message;
     193        otr_ops.notify = NULL;
     194        otr_ops.display_otr_message = &op_display_otr_message;
     195        otr_ops.update_context_list = NULL;
     196        otr_ops.protocol_name = NULL;
     197        otr_ops.protocol_name_free = NULL;
     198        otr_ops.new_fingerprint = &op_new_fingerprint;
     199        otr_ops.write_fingerprints = &op_write_fingerprints;
     200        otr_ops.gone_secure = &op_gone_secure;
     201        otr_ops.gone_insecure = &op_gone_insecure;
     202        otr_ops.still_secure = &op_still_secure;
     203        otr_ops.log_message = &op_log_message;
     204        otr_ops.max_message_size = &op_max_message_size;
     205        otr_ops.account_name = &op_account_name;
     206        otr_ops.account_name_free = NULL;
    202207       
    203208        root_command_add( "otr", 1, cmd_otr, 0 );
    204 }
    205 
    206 otr_t *otr_new(void)
    207 {
    208         otr_t *otr = g_new0(otr_t, 1);
    209 
    210         otr->us = otrl_userstate_create();
    211        
    212         return otr;
    213 }
    214 
    215 void otr_free(otr_t *otr)
    216 {
     209        register_irc_plugin( &otr_plugin );
     210}
     211
     212gboolean otr_irc_new(irc_t *irc)
     213{
     214        set_t *s;
     215        GSList *l;
     216       
     217        irc->otr = g_new0(otr_t, 1);
     218        irc->otr->us = otrl_userstate_create();
     219       
     220        s = set_add( &irc->b->set, "otr_color_encrypted", "true", set_eval_bool, irc );
     221       
     222        s = set_add( &irc->b->set, "otr_policy", "oppurtunistic", set_eval_list, irc );
     223        l = g_slist_prepend( NULL, "never" );
     224        l = g_slist_prepend( l, "opportunistic" );
     225        l = g_slist_prepend( l, "manual" );
     226        l = g_slist_prepend( l, "always" );
     227        s->eval_data = l;
     228       
     229        return TRUE;
     230}
     231
     232void otr_irc_free(irc_t *irc)
     233{
     234        otr_t *otr = irc->otr;
    217235        otrl_userstate_free(otr->us);
    218236        if(otr->keygen) {
     
    339357        }
    340358       
    341         ignore_msg = otrl_message_receiving(irc->otr->us, &global.otr_ops, ic,
     359        ignore_msg = otrl_message_receiving(irc->otr->us, &otr_ops, ic,
    342360                ic->acc->user, ic->acc->prpl->name, handle, msg, &newmsg,
    343361                &tlvs, NULL, NULL);
     
    393411        }
    394412       
    395         st = otrl_message_sending(irc->otr->us, &global.otr_ops, ic,
     413        st = otrl_message_sending(irc->otr->us, &otr_ops, ic,
    396414                ic->acc->user, ic->acc->prpl->name, handle,
    397415                msg, NULL, &otrmsg, NULL, NULL);
     
    409427                        return 1;
    410428                }
    411                 st = otrl_message_fragment_and_send(&global.otr_ops, ic, ctx,
     429                st = otrl_message_fragment_and_send(&otr_ops, ic, ctx,
    412430                        otrmsg, OTRL_FRAGMENT_SEND_ALL, NULL);
    413431                otrl_message_free(otrmsg);
     
    420438        return st;
    421439}
     440
     441static const struct irc_plugin otr_plugin =
     442{
     443        otr_irc_new,
     444        otr_irc_free,
     445};
    422446
    423447static void cmd_otr(irc_t *irc, char **args)
     
    663687        }
    664688       
    665         otrl_message_disconnect(irc->otr->us, &global.otr_ops,
     689        otrl_message_disconnect(irc->otr->us, &otr_ops,
    666690                u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, u->bu->handle);
    667691       
     
    721745                        "SMP already in phase %d, sending abort before reinitiating",
    722746                        ctx->smstate->nextExpected+1);
    723                 otrl_message_abort_smp(irc->otr->us, &global.otr_ops, u->bu->ic, ctx);
     747                otrl_message_abort_smp(irc->otr->us, &otr_ops, u->bu->ic, ctx);
    724748                otrl_sm_state_free(ctx->smstate);
    725749        }
     
    729753        if(ctx->smstate->secret == NULL) {
    730754                irc_usermsg(irc, "smp: initiating with %s...", u->nick);
    731                 otrl_message_initiate_smp(irc->otr->us, &global.otr_ops,
     755                otrl_message_initiate_smp(irc->otr->us, &otr_ops,
    732756                        u->bu->ic, ctx, (unsigned char *)args[2], strlen(args[2]));
    733757                /* smp is now in EXPECT2 */
     
    736760                   received the SMP1, so let's issue a response */
    737761                irc_usermsg(irc, "smp: responding to %s...", u->nick);
    738                 otrl_message_respond_smp(irc->otr->us, &global.otr_ops,
     762                otrl_message_respond_smp(irc->otr->us, &otr_ops,
    739763                        u->bu->ic, ctx, (unsigned char *)args[2], strlen(args[2]));
    740764                /* smp is now in EXPECT3 */
     
    10501074        irc_t *irc = ic->bee->ui_data;
    10511075        OtrlUserState us = irc->otr->us;
    1052         OtrlMessageAppOps *ops = &global.otr_ops;
     1076        OtrlMessageAppOps *ops = &otr_ops;
    10531077        OtrlTLV *tlv = NULL;
    10541078        ConnContext *context;
  • otr.h

    rf5c0d8e r0c85c08  
    7272void otr_init(void);
    7373
    74 /* called from irc_new()/irc_free() */
    75 otr_t *otr_new();
    76 void otr_free(otr_t *otr);
    77 
    7874/* called by storage_* functions */
    7975void otr_load(struct irc *irc);
     
    9894typedef void *OtrlMessageAppOps;
    9995
    100 #define otr_init() {}
    101 #define otr_new() (NULL)
    10296#define otr_free(otr) {}
    10397#define otr_load(irc) {}
  • storage.c

    rf5c0d8e r0c85c08  
    2828#define BITLBEE_CORE
    2929#include "bitlbee.h"
    30 #include "otr.h"
    3130
    3231extern storage_t storage_text;
     
    115114
    116115                status = st->load(irc, password);
    117                 if (status == STORAGE_OK) {
    118                         otr_load(irc);
     116                if (status == STORAGE_OK)
    119117                        return status;
    120                 }
     118               
    121119                if (status != STORAGE_NO_SUCH_USER)
    122120                        return status;
     
    139137                return STORAGE_NO_SUCH_USER;
    140138        }
    141 
    142         otr_save(irc);
     139       
    143140        st = ((storage_t *)global.storage->data)->save(irc, overwrite);
    144141       
     
    166163                        ret = status;
    167164        }
    168         if (ret == STORAGE_OK) {
    169                 otr_remove(nick);
    170         }
    171165       
    172166        return ret;
     
    182176        storage_t *primary_storage = gl->data;
    183177        irc_t *irc;
    184        
     178
    185179        /* First, try to rename in the current write backend, assuming onick
    186180         * is stored there */
    187181        status = primary_storage->rename(onick, nnick, password);
    188         if (status != STORAGE_NO_SUCH_USER) {
    189                 otr_rename(onick, nnick);
     182        if (status != STORAGE_NO_SUCH_USER)
    190183                return status;
    191         }
    192184
    193185        /* Try to load from a migration backend and save to the current backend.
     
    213205
    214206        storage_remove(onick, password);
    215         otr_rename(onick, nnick);
    216207
    217208        return STORAGE_OK;
Note: See TracChangeset for help on using the changeset viewer.