<commit_before>var util = require('util');

var logger = require('../logger')
  , config = require('../config')
  , doc = require('../doc.json');

var session = require('../app');

module.exports = {

    friendMsgHandler(steamID, msg, EChatEntryType) {
    // Message from friend
        var user = getUserName(steamID);
        logger.log('debug', "friendMsg event", { user, steamID, EChatEntryType });
        if (msg.length == 0)
            this.emit('isTyping', steamID, user, getUserState.call(this, steamID));
        else
            this.emit('message', steamID, user, getUserState.call(this, steamID), msg);
    },

    chatMsgHandler(chatID, msg, EChatEntryType, steamID) {
    // Message in group chat
        var user = getUserName(steamID);
        logger.log('debug', "chatMsg event", { chatID, user, steamID, EChatEntryType });

        clearTimeout(session.ghostTimer);
        session.ghostTimer = setTimeout(
            checkIfGhostChat.bind(this, chatID), 
            config.get('ghostcheck')
        );

        this.emit('groupMessage', chatID, user, getUserState.call(this, steamID), msg);
    },

    personaStateHandler(friend) {
    // Someone has made changes to their persona
        logger.log('silly', "personaState event", friend);

        if (friend.friendid == session.steamID) {
            session.steamNick = friend.player_name;
            this.emit('updateNick');
        }

        session.users[friend.friendid] = {
            persona_state: getPersonaState.call(this, friend.persona_state, friend.game_name),
            player_name: friend.player_name,
            game_name: friend.game_name
        };

        this.emit('listUpdate');
    },

    clanStateHandler(clanState) {
    // A group has changed their clan state
        logger.log('debug', "clanState event", clanState);

        var e = session.clans[clanState.steamid_clan];
        if (e === undefined) e = {};
        e.clan_name = clanState.name_info.clan_name;
        e.user_count = clanState.user_counts.chatting;
    },

    chatEnterHandler(chatID, EChatRoomEnterResponse) {
    // You just entered a group chat. On success, chatRooms is now populated with chatID.
        logger.log('debug', "chatEnter event", { chatID, EChatRoomEnterResponse });

        var res = this.Steam.EChatRoomEnterResponse;
        switch (EChatRoomEnterResponse) {
            case res.Success: 
                this.emit('enteredChat', chatID);
                var e = this.friends.chatRooms[chatID];
                var count = 0;
                for (var steamID in e) {
                    session.clans[chatID].members[steamID] = <commit_msg>Fix crash from incomplete clanState object by using safeGet<commit_after>var util = require('util');

var logger = require('../logger')
  , config = require('../config')
  , doc = require('../doc.json');

var session = require('../app')
  , { safeGet } = require('../helpers');

module.exports = {

    friendMsgHandler(steamID, msg, EChatEntryType) {
    // Message from friend
        var user = getUserName(steamID);
        logger.log('debug', "friendMsg event", { user, steamID, EChatEntryType });
        if (msg.length == 0)
            this.emit('isTyping', steamID, user, getUserState.call(this, steamID));
        else
            this.emit('message', steamID, user, getUserState.call(this, steamID), msg);
    },

    chatMsgHandler(chatID, msg, EChatEntryType, steamID) {
    // Message in group chat
        var user = getUserName(steamID);
        logger.log('debug', "chatMsg event", { chatID, user, steamID, EChatEntryType });

        clearTimeout(session.ghostTimer);
        session.ghostTimer = setTimeout(
            checkIfGhostChat.bind(this, chatID), 
            config.get('ghostcheck')
        );

        this.emit('groupMessage', chatID, user, getUserState.call(this, steamID), msg);
    },

    personaStateHandler(friend) {
    // Someone has made changes to their persona
        logger.log('silly', "personaState event", friend);

        if (friend.friendid == session.steamID) {
            session.steamNick = friend.player_name;
            this.emit('updateNick');
        }

        session.users[friend.friendid] = {
            persona_state: getPersonaState.call(this, friend.persona_state, friend.game_name),
            player_name: friend.player_name,
            game_name: friend.game_name
        };

        this.emit('listUpdate');
    },

    clanStateHandler(clanState) {
    // A group has changed their clan state
        logger.log('debug', "clanState event", clanState);

        const ID = clanState.steamid_clan;
        const name = safeGet(clanState, 'name_info', 'clan_name');
        const count = safeGet(clanState, 'user_counts', 'chatting');

        const clan = Object.assign({}, session.clans[ID]);
        session.clans[ID] = Object.assign({}, clan, {
            clan_name: name || safeGet(clan, 'clan_name'),
            user_count: count || safeGet(clan, 'user_count')
        });

    },

    chatEnterHandler(chatID, EChatRoomEnterResponse) {
    // You just entered a group chat. On success, chatRooms is now populated with chatID.
        logger.log('debug', "chatEnter event", { chatID, EChatRoomEnterResponse });

        var res = this.Steam.EChatRoomEnterResponse;
        switch (EChatRoomEnterResponse) {
            case res.Success: 
                this.emit('enteredChat', chatID);
                var e = this.friends.chatRooms[chatID];
                var count = 0;
                for (var steamID in e) {
                    session.clans[chatID].members[steamID] = 
{'fraction_non_alphanumeric': 0.12723054960742328, 'fraction_numerical': 0.0007137758743754461, 'mean_word_length': 2.487865588052271, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2238279', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1633, 'n_words': 361}