-------------------------------------------------------------------------------

--таблица названий функций
--устанавливаем 0 - для отлючения функции, 1 - для включения
tMain={
  ["ranker"]=0,
  ["connector"]=0,
  ["chatbot"]=0,
  ["pmbot"]=0,
  ["statbot"]=0,
  ["spam"]=0,
}

-------------------------------------------------------------------------------

dcpp,tMainFunc,tSys={},{},{}
sVersion="0.8.5.7"
iStartTime=os.time() --время запуска скрипта

-------------------------------------------------------------------------------
function tSys:Func(f)
    if f and type(f)=="function" then return true end
end

function tSys:Table(t)
    if t and type(t)=="table" then return true end
end

function tSys:Number(t)
    if t and type(t)=="number" then return true end
end

function tSys:ToLower(s)
    local r,b=""
    if s then
     s=string.lower(s)
     for i=1,string.len(s) do
      b=string.byte(s,i)
      if b>=192 and b<224 then
       b=b+32
      elseif b==168 or b==184 then
       b=229
      end
      r=r..string.char(b)
     end
    end
    return r
end

function tSys:Tags(tUser,s,sData)
    if sData then s=string.gsub(s,"%[MSG%]",sData) end
    s=string.gsub(s,"%[NICK%]",tUser.NI)
    s=string.gsub(s,"%[DESC%]",tUser.DE)
    s=string.gsub(s,"%[EMAIL%]",tUser.EM)
    s=string.gsub(s,"%[IP%]",tUser.I4)
    s=string.gsub(s,"%[SHORTSHARE%]",tUser.SSshort)
    s=string.gsub(s,"%[SHARE%]",tUser.SS)
    s=string.gsub(s,"%[TAG%]",tUser.TA)
    s=string.gsub(s,"%[HUBNAME%]",tUser.HL)
    s=string.gsub(s,"%[CID%]",tUser.CID)
    return s
end

-------------------------------------------------------------------------------
for i,v in pairs(tMain) do
  if v==1 then
    tMainFunc[i]={}
    dofile(DC():GetAppPath().."scripts/func/"..i..".func")
  end
end

function dcpp.OnHubConnected(sHubUrl)
  for i,v in pairs(tMainFunc) do
    if tSys:Func(v.OnHubConnected) and v.OnHubConnected(sHubUrl) then return end
  end
end

function dcpp.OnHubDisconnected(sHubUrl)
  for i,v in pairs(tMainFunc) do
    if tSys:Func(v.OnHubDisconnected) and v.OnHubDisconnected(sHubUrl) then return end
  end
end

function dcpp.OnPublicMessage(cid,sData)
  if cid and sData then
    local tUser = DC():GetUserInfo(cid)
    sData=DC():FromUtf8(sData)
    if tUser then
      for i,v in pairs(tMainFunc) do
        if tSys:Func(v.OnPublicMessage) and v.OnPublicMessage(tUser,sData) then return end
      end
    end
  end
end

function dcpp.OnPrivateMessage(cid,sData)
  if cid and sData then
    local tUser = DC():GetUserInfo(cid)
    sData=DC():FromUtf8(sData)
    if tUser then
      for i,v in pairs(tMainFunc) do
        if tSys:Func(v.OnPrivateMessage) and v.OnPrivateMessage(cid,tUser,sData) then return end
      end
    end
  end
end

function dcpp.OnTimer()
  for i,v in pairs(tMainFunc) do
    if tSys:Func(v.OnTimer) and v.OnTimer() then return end
  end
end

local sBM=""
for i,v in pairs(tMainFunc) do
  sBM=sBM.."\r\n\t• "..i
end
DC():RunTimer(1)
DC():PrintDebug(DC():ToUtf8("MoonADC is running"))
-- DC():MessageBox(DC():ToUtf8("Программа: greylinkDC++ "..sVersion.."\r\nДиректория программы: "..DC():ToUtf8(DC():GetAppPath()).."\r\nЗапущены следующие боты:\r\n"..sBM), DC():ToUtf8("MoonADC++ Bot (Lua 5.1.3) by Setuper"), "ok", "warning")
