<commit_before>api = require './qqapi'
Log = require 'log'
Dispatcher = require './dispatcher'

log = new Log('debug')
jsons = JSON.stringify

###
 cookie , auth 登录需要参数
 config:  配置信息，将 config.yaml
   plugins: 插件
###
class QQBot
    constructor: (@cookies, @auth, @config) ->
        @buddy_info = {}
        @group_info = {}
        @groupmember_info = {}

        api.cookies @cookies
        @api = api
        @dispatcher = new Dispatcher(@config.plugins)

    # @format PROTOCOL `用户分组信息格式`
    save_buddy_info: (@buddy_info)->

    # @format PROTOCOL `群分组信息格式`
    save_group_info: (@group_info) ->

    # @format PROTOCOL `群用户信息`
    save_group_member: (group,info)->
        @groupmember_info[group.gid] = info

    # 获取用户信息
    # @return {nick,uin,flag,face}
    get_user: (uin) ->
        # TODO:加速查询
        users = @buddy_info.info.filter (item)-> item.uin == uin
        users.pop()

    # 获取群用户信息
    get_user_ingroup: (uin, gid)->
        info = @groupmember_info[gid]
        users = info.minfo.filter (item)-> item.uin == uin
        users.pop()


    # 获取群信息，只支持群 ，支持多关键词搜索
    # @options {key:value}
    # @return {gid,code,name,flag}
    get_group: (options)->
        groups = @group_info.gnamelist.filter (item)->
            for key ,value of options
                return item[key] == value
        groups.pop()

    # 获取群列表
    # @callback {ret:bool,error}
    update_group_list: (callback)->
      @api.get_group_list @auth, (ret , e)=>
          log.error e  if e
          @save_group_info ret.result if ret.retcode == 0
          callback( ret.retcode == 0, e || 'retcode isnot 0' ) if callback

    # 获取好友列表
    # @callback (ret:bool, error)
    update_buddy_list: (callback)->
        @api.get_buddy_list @auth , (ret,e)=>
            @save_buddy_info ret.result if ret.retcode == 0
            callback( ret.retcode == 0, e || 'retcode isnot 0' ) if callback

    # 更新群成员， 似乎获取不到群ID
    # @options {key:value} or group obj
    # @callback (ret:bool , error)
    update_group_member: (options, callback)->
        group = if options.code then options else @get_group(options)
        api.get_group_member group.code , @auth , (ret,e)=>
            if ret.retcode == 0
                @save_group_member(group,ret.result)
            callback(ret.retcode == 0 , e) if callback

    # 更新所有群成员
    # @callback (success,total_count,success_count)
    update_all_group_member: (callback)->
      finished = successed = 0
      groups = @group_info.gnamelist || []
      all = groups.length
      for group in groups
        @update_group_member group , (ret,error)->
          finished += 1; successed += ret
          log.debug "groupmember all#{all} fin#{finished} succ#{successed}"
          callback(successed == all, finished ,successed) if finished == all

    # 更新好友和群成员
    # @callback (ret?,actions) 是否全部更新成功 , actions每个状态
    update_all_members: (callback)->
      # [0,0] -> [finished,success]
      actions= buddy:[0,0],group:[0,0],groupmember:[0,0]
      check = ->
        finished = successed = 0 ; all = Object.keys(actions).length
        stats = (value for key,value of actions)
        for item in stats
          finished += item[0] ; successed += item[1]
        log.debug("updating all: all #{all} finished #{finished} success #{successed}")
        callback(successed==all) if finished == all

      # fetching...
      log.info 'fetching buddy list...'
      @update_buddy_list (ret)->
        actions.buddy = [1, ret]
        check()

      log.info 'fetching group list...'
      @update_group_list (ret)=>
        actions.group = [1, ret]
        unless ret
          callback(false)
          return

        log.info 'fetching all groupmember...'
        @update_all_group_member (ret,all,successed)->
          actions.groupmember = [1,ret]
          check()


    # 长轮询
    # @callback
    runloop: (callback)->
      @api.long_poll @auth , (ret,e)=>
          @handle_poll_responce ret,e
          callback(ret,e) if callback

    # 回复消息
    # @param message 收到的message
    # @param content:string 回复信息
    # @callback ret, error
    reply_message: (message, content, callback)->
        log.info "发送消息：",content
        if message.type == 'group'
            @api.send_msg_2group  message.from_gid , content , @auth, (ret,e)->
                callback(ret,e) if callback
        else if message.type == 'buddy'
            @api.send_msg_2buddy message.from_uin , content , @auth , (ret,e)->
                callback(ret,e) if callback

    # 发送群消息
    send_message_to_group: (gid, content, callback)-><commit_msg>update send message to group method's params<commit_after>api = require './qqapi'
Log = require 'log'
Dispatcher = require './dispatcher'

log = new Log('debug')
jsons = JSON.stringify

###
 cookie , auth 登录需要参数
 config:  配置信息，将 config.yaml
   plugins: 插件
###
class QQBot
    constructor: (@cookies, @auth, @config) ->
        @buddy_info = {}
        @group_info = {}
        @groupmember_info = {}

        api.cookies @cookies
        @api = api
        @dispatcher = new Dispatcher(@config.plugins)

    # @format PROTOCOL `用户分组信息格式`
    save_buddy_info: (@buddy_info)->

    # @format PROTOCOL `群分组信息格式`
    save_group_info: (@group_info) ->

    # @format PROTOCOL `群用户信息`
    save_group_member: (group,info)->
        @groupmember_info[group.gid] = info

    # 获取用户信息
    # @return {nick,uin,flag,face}
    get_user: (uin) ->
        # TODO:加速查询
        users = @buddy_info.info.filter (item)-> item.uin == uin
        users.pop()

    # 获取群用户信息
    get_user_ingroup: (uin, gid)->
        info = @groupmember_info[gid]
        users = info.minfo.filter (item)-> item.uin == uin
        users.pop()


    # 获取群信息，只支持群 ，支持多关键词搜索
    # @options {key:value}
    # @return {gid,code,name,flag}
    get_group: (options)->
        groups = @group_info.gnamelist.filter (item)->
            for key ,value of options
                return item[key] == value
        groups.pop()

    # 获取群列表
    # @callback {ret:bool,error}
    update_group_list: (callback)->
      @api.get_group_list @auth, (ret , e)=>
          log.error e  if e
          @save_group_info ret.result if ret.retcode == 0
          callback( ret.retcode == 0, e || 'retcode isnot 0' ) if callback

    # 获取好友列表
    # @callback (ret:bool, error)
    update_buddy_list: (callback)->
        @api.get_buddy_list @auth , (ret,e)=>
            @save_buddy_info ret.result if ret.retcode == 0
            callback( ret.retcode == 0, e || 'retcode isnot 0' ) if callback

    # 更新群成员， 似乎获取不到群ID
    # @options {key:value} or group obj
    # @callback (ret:bool , error)
    update_group_member: (options, callback)->
        group = if options.code then options else @get_group(options)
        api.get_group_member group.code , @auth , (ret,e)=>
            if ret.retcode == 0
                @save_group_member(group,ret.result)
            callback(ret.retcode == 0 , e) if callback

    # 更新所有群成员
    # @callback (success,total_count,success_count)
    update_all_group_member: (callback)->
      finished = successed = 0
      groups = @group_info.gnamelist || []
      all = groups.length
      for group in groups
        @update_group_member group , (ret,error)->
          finished += 1; successed += ret
          log.debug "groupmember all#{all} fin#{finished} succ#{successed}"
          callback(successed == all, finished ,successed) if finished == all

    # 更新好友和群成员
    # @callback (ret?,actions) 是否全部更新成功 , actions每个状态
    update_all_members: (callback)->
      # [0,0] -> [finished,success]
      actions= buddy:[0,0],group:[0,0],groupmember:[0,0]
      check = ->
        finished = successed = 0 ; all = Object.keys(actions).length
        stats = (value for key,value of actions)
        for item in stats
          finished += item[0] ; successed += item[1]
        log.debug("updating all: all #{all} finished #{finished} success #{successed}")
        callback(successed==all) if finished == all

      # fetching...
      log.info 'fetching buddy list...'
      @update_buddy_list (ret)->
        actions.buddy = [1, ret]
        check()

      log.info 'fetching group list...'
      @update_group_list (ret)=>
        actions.group = [1, ret]
        unless ret
          callback(false)
          return

        log.info 'fetching all groupmember...'
        @update_all_group_member (ret,all,successed)->
          actions.groupmember = [1,ret]
          check()


    # 长轮询
    # @callback
    runloop: (callback)->
      @api.long_poll @auth , (ret,e)=>
          @handle_poll_responce ret,e
          callback(ret,e) if callback

    # 回复消息
    # @param message 收到的message
    # @param content:string 回复信息
    # @callback ret, error
    reply_message: (message, content, callback)->
        log.info "发送消息：",content
        if message.type == 'group'
            @api.send_msg_2group  message.from_gid , content , @auth, (ret,e)->
                callback(ret,e) if callback
        else if message.type == 'buddy'
            @api.send_msg_2buddy message.from_uin , content , @auth , (ret,e)->
                callback(ret,e) if callback

    # 发送群消息
    # @param gid_or_group 
    # @callback (ret,e)
    send_message_to_group: (gid_or_group, content, callback)->
      gid = if typeof gid_or_group is 'object' then gid_or_group.gid else gid_or_group
{'fraction_non_alphanumeric': 0.12980047200171638, 'fraction_numerical': 0.0055782021025531005, 'mean_word_length': 2.3463747307968412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, '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': '6153377', 'n_tokens_mistral': 3227, 'n_tokens_neox': 3238, 'n_words': 717}