close Warning: Failed to sync with repository "(default)": [Errno 12] Cannot allocate memory; repository information may be out of date. Look in the Trac log for more information including mitigation strategies.

Ticket #239: im_log.patch

File im_log.patch, 7.2 KB (added by Kristof@…, at 2007-05-17T22:27:03Z)

Adds logging of IM messages

  • im_log.c

     
     1  /********************************************************************\
     2  * BitlBee -- An IRC to other IM-networks gateway                     *
     3  *                                                                    *
     4  * Copyright 2002-2007 Wilmer van der Gaast and others                *
     5  \********************************************************************/
     6
     7/* Log IM messages to a text file                             */
     8
     9/*
     10  This program is free software; you can redistribute it and/or modify
     11  it under the terms of the GNU General Public License as published by
     12  the Free Software Foundation; either version 2 of the License, or
     13  (at your option) any later version.
     14
     15  This program is distributed in the hope that it will be useful,
     16  but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  GNU General Public License for more details.
     19
     20  You should have received a copy of the GNU General Public License with
     21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
     22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
     23  Suite 330, Boston, MA  02111-1307  USA
     24*/
     25
     26#define BITLBEE_CORE
     27#include <time.h>
     28#include "bitlbee.h"
     29#include "im_log.h"
     30
     31static FILE* out = NULL;
     32
     33static void open_log ( irc_t* irc );
     34static void close_log ( void );
     35
     36static void handle_error ( irc_t* irc )
     37{
     38    irc_usermsg ( irc, "There was a problem opening the logfile. Disabling logging" );
     39    set_setstr ( &irc->set, "enable_logging", "false" );
     40}
     41
     42static void open_log ( irc_t* irc )
     43{
     44    char logfile[512];
     45    char *nick;
     46    int ret;
     47
     48    nick = g_strdup( irc->nick );
     49    nick_lc( nick );
     50
     51    ret = g_snprintf ( logfile, sizeof ( logfile ), "%s%s%s", global.conf->configdir, nick, ".log");
     52    if ( ret < 0 )
     53    {
     54        handle_error ( irc );
     55        return;
     56    }
     57
     58    out = fopen ( logfile, "a+" );
     59
     60    if ( out == NULL )
     61        handle_error ( irc );
     62}
     63
     64static void close_log ( void )
     65{
     66    fclose ( out );
     67    out = NULL;
     68}
     69
     70static void write_message ( const char* who, const char* msg )
     71{
     72    time_t now = time ( NULL );
     73    char* timestamp = asctime ( localtime ( &now ) );
     74       
     75    /* Because asctime adds a '\n' */
     76    timestamp[strlen ( timestamp ) - 1 ] = 0;
     77       
     78    fprintf ( out, "%s %s %s\n", timestamp, who, msg );
     79    fflush ( out );
     80}
     81
     82void im_log_message (irc_t* irc, const char* who, const char* msg)
     83{
     84    if ( set_getbool ( &irc->set, "enable_logging") )
     85    {
     86        if ( ! out )
     87            open_log ( irc );
     88
     89        if ( out )
     90            /* Because open_log could fail */
     91            write_message ( who, msg );
     92    }
     93    else
     94    {
     95        if ( out )
     96            close_log ();
     97    }
     98}
     99
  • im_log.h

     
     1  /********************************************************************\
     2  * BitlBee -- An IRC to other IM-networks gateway                     *
     3  *                                                                    *
     4  * Copyright 2002-2007 Wilmer van der Gaast and others                *
     5  \********************************************************************/
     6
     7/* Log IM messages to a text file                             */
     8
     9/*
     10  This program is free software; you can redistribute it and/or modify
     11  it under the terms of the GNU General Public License as published by
     12  the Free Software Foundation; either version 2 of the License, or
     13  (at your option) any later version.
     14
     15  This program is distributed in the hope that it will be useful,
     16  but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  GNU General Public License for more details.
     19
     20  You should have received a copy of the GNU General Public License with
     21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
     22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
     23  Suite 330, Boston, MA  02111-1307  USA
     24*/
     25
     26#ifndef _IM_LOG_H
     27#define _IM_LOG_H
     28
     29void im_log_message (irc_t* irc, const char* nick, const char *message);
     30
     31#endif
  • Makefile

     
    99-include Makefile.settings
    1010
    1111# Program variables
    12 objects = account.o bitlbee.o conf.o crypting.o help.o ipc.o irc.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o $(STORAGE_OBJS) unix.o user.o
    13 headers = account.h bitlbee.h commands.h conf.h config.h crypting.h help.h ini.h ipc.h irc.h log.h nick.h query.h set.h sock.h storage.h url.h user.h protocols/http_client.h protocols/md5.h protocols/nogaim.h protocols/proxy.h protocols/sha.h protocols/ssl_client.h
     12objects = account.o bitlbee.o conf.o crypting.o help.o ipc.o irc.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o $(STORAGE_OBJS) unix.o user.o im_log.o
     13headers = account.h bitlbee.h commands.h conf.h config.h crypting.h help.h ini.h ipc.h irc.h log.h nick.h query.h set.h sock.h storage.h url.h user.h protocols/http_client.h protocols/md5.h protocols/nogaim.h protocols/proxy.h protocols/sha.h protocols/ssl_client.h im_log.h
    1414subdirs = lib protocols
    1515
    1616# Expansion of variables
  • bitlbee.h

     
    132132#include "sock.h"
    133133#include "misc.h"
    134134#include "proxy.h"
     135#include "im_log.h"
    135136
    136137typedef struct global {
    137138        /* In forked mode, child processes store the fd of the IPC socket here. */
  • irc.c

     
    145145        set_add( &irc->set, "strip_html", "true", NULL, irc );
    146146        set_add( &irc->set, "to_char", ": ", set_eval_to_char, irc );
    147147        set_add( &irc->set, "typing_notice", "false", set_eval_bool, irc );
    148        
     148        set_add ( &irc->set, "enable_logging", "false", set_eval_bool, irc );
     149
    149150        conf_loaddefaults( irc );
    150151       
    151152        return( irc );
  • protocols/nogaim.c

     
    193193        for( a = ic->irc->accounts; a; a = a->next )
    194194                if( a->prpl == ic->acc->prpl && a->ic != ic )
    195195                        break;
    196        
     196
     197        im_log_message ( ic->irc, ic->acc->prpl->name, text);
     198
    197199        /* If we found one, include the screenname in the message. */
    198200        if( a )
    199201                irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text );
     
    650652                        tmp = *nl;
    651653                        *nl = 0;
    652654                }
    653                
     655
     656                im_log_message ( irc, u->nick, msg );
    654657                irc_msgfrom( irc, u->nick, msg );
    655658               
    656659                /* Move on. */
     
    665668                        msg += 425;
    666669                }
    667670        }
     671
     672        im_log_message ( irc, u->nick, msg );
    668673        irc_msgfrom( irc, u->nick, msg );
    669674}
    670675
     
    725730{
    726731        struct im_connection *ic = c->ic;
    727732        user_t *u;
     733
     734        im_log_message ( ic->irc, who, msg );
    728735       
    729736        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
    730737        if( g_strcasecmp( who, ic->acc->user ) == 0 )
     
    945952                buf = escape_html( msg );
    946953                msg = buf;
    947954        }
    948        
     955
     956        im_log_message ( ic->irc, handle, msg );
    949957        st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags );
    950958        g_free( buf );
    951959