Einfacher IRC Chat Bot

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
ndklinux
Beiträge: 5
Registriert: 09.05.2007 16:56
Wohnort: Esch/Alzette

Einfacher IRC Chat Bot

Beitrag von ndklinux »

Hier ein keiner IRC Chat Bot. Soll Leute geben die so etwas suchen ;) Code ist noch ausbaufähig. Viel Spass.

Code: Alles auswählen

InitNetwork()

inifile.s = ProgramParameter()
Debug "Config File "+inifile.s
If inifile.s = ""
  inifile.s = "ibot.cfg"
EndIf

OpenPreferences(inifile.s)
  server.s = ReadPreferenceString("server", "localhost")
  port.l = ReadPreferenceLong("port", 6667)
  channels.s = ReadPreferenceString("channels", "")
  nickname.s = ReadPreferenceString("nickname", "iBot")
  name.s = ReadPreferenceString("name", "iBot")
  host.s = ReadPreferenceString("host", "localhost")
  nickpassword.s = ReadPreferenceString("nickpassword", "")
  admin.s = LCase(ReadPreferenceString("admin", "linux"))
  adminpassword.s = ReadPreferenceString("adminpassword", "admin")
  adminemail.s = ReadPreferenceString("adminemail", "admin@localhost")
  opadmin.l = ReadPreferenceLong("opadmin", 0)
  nickserv.s = ReadPreferenceString("nickserv", "localhost")
  identifystring.s = ReadPreferenceString("identifystring", "identify [password]")
  registerstring.s = ReadPreferenceString("registerstring", "register [password] [email]")
  registernick.l = ReadPreferenceLong("registernick", 0)
  scriptfile.s = ReadPreferenceString("scriptfile", "script.cfg")
ClosePreferences()

version.s = "iBot V1.0 by LiNuX"
adminhost.s = ""

Structure Basic 
  Field1.l
  Field2.s
  Field3.s
  Field4.s
  Field5.s
EndStructure

;NewList UserList.Basic()
NewList ChannelList.Basic()
;NewList ScriptList.Basic()

index.l = 1
any.s = StringField(channels.s, index.l, " ")
While any.s <> ""
  AddElement(ChannelList())
  ChannelList()\Field2.s = any.s
  index.l = index.l+1
  any.s = StringField(channels.s, index.l, " ")
Wend

EOL$ = Chr(13)+Chr(10)
*Buffer = AllocateMemory(4096)

firstconnection.l = 1

conid = OpenNetworkConnection(server.s, port.l, #PB_Network_TCP)
If conid <=0
  Debug "No Connection"
  End
EndIf

SendNetworkString(conid, "USER "+nickname.s+" "+host.s+" "+host.s+" :"+name.s+EOL$)
Debug "Send USER "+nickname.s+" "+host.s+" "+host.s+" :"+name.s
SendNetworkString(conid, "NICK "+nickname.s+" "+host.s+EOL$)
Debug "Send NICK "+nickname.s+" "+host.s

If registernick.l = 1 And nickpassword.s <> ""
  registerstring.s = ReplaceString(registerstring.s, "[EMAIL]", adminemail.s)
  registerstring.s = ReplaceString(registerstring.s, "[PASSWORD]", nickpassword.s)
  SendNetworkString(conid, "PRIVMSG "+nickserv.s+" "+registerstring.s+EOL$)
  Debug "Send PRIVMSG "+nickserv.s+" "+registerstring.s
EndIf
If nickpassword.s <> ""
  identifystring.s = ReplaceString(identifystring.s, "[PASSWORD]", nickpassword.s)
  SendNetworkString(conid, "PRIVMSG "+nickserv.s+" "+identifystring.s+EOL$)
  Debug "Send PRIVMSG "+nickserv.s+" "+identifystring.s
EndIf

While quit.l = 0

    Delay(150)
    
    NetEvent = NetworkClientEvent(conid)
    If NetEvent = #PB_NetworkEvent_Data
      ReceiveNetworkData(conid, *Buffer, 4096)
      intext.s = PeekS(*Buffer)
      Debug intext.s
      
      If FindString(UCase(intext.s), "PING :", 1) > 0
        SendNetworkString(conid, "PONG : "+Right(intext.s, Len(intext.s)-6)+EOL$)
        Debug "Send PONG : "+Right(intext.s, Len(intext.s)-6)
      EndIf
      
      fromname.s = StringField(intext.s, 1, " ")
      fromhost.s = StringField(fromname.s, 2, "!")
      fromname.s = StringField(fromname.s, 1, "!")
      fromname.s = Right(fromname.s, Len(fromname.s)-1)
      intextnew.s = ""
      index.l = 2
      innew.s = StringField(intext.s, index.l, " ")
      intextnew.s = intextnew.s + innew.s
      index.l = index.l+1
      While innew.s <> ""
        innew.s = StringField(intext.s, index.l, " ")
        intextnew.s = intextnew.s +" "+ innew.s
        index.l = index.l+1
      Wend
      
      Debug fromname.s
      Debug fromhost.s
      intextnew.s = Left(intextnew.s, Len(intextnew.s)-3)
      Debug intextnew.s
      
      Select StringField(intextnew.s, 1, " ")
        Case "PRIVMSG"
          Select StringField(intextnew.s, 2, " ")
            Case nickname.s
              If LCase(fromname.s) = admin.s
                
                If adminhost.s <> fromhost.s And Trim(UCase(StringField(intextnew.s, 3, " "))) = ":LOGIN"
                  If Trim(Left(StringField(intextnew.s, 4, " "), Len(adminpassword.s))) = adminpassword.s
                    SendNetworkString(conid, "PRIVMSG "+fromname.s+" Welcome Admin! Login successfull!"+EOL$)
                    adminhost.s = fromhost.s
                  Else
                    SendNetworkString(conid, "PRIVMSG "+fromname.s+" Login error!"+EOL$)
                  EndIf
                  
                ElseIf adminhost.s <> fromhost.s
                  SendNetworkString(conid, "PRIVMSG "+fromname.s+" Hello! Enter your Password! Syntax: LOGIN [PASSWORD] "+EOL$)
                ElseIf adminhost.s = fromhost.s And LCase(fromname.s) = admin.s
              
                  Select Trim(UCase(StringField(intextnew.s, 3, " ")))
                    Case ":LOGOUT"
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" ByeBye Admin! Logout successfull!"+EOL$)
                      adminhost.s = ""
                    Case ":SAY"
                      channel.s = StringField(intextnew.s, 4, " ")
                      text.s = Right(intextnew.s, Len(intextnew.s)-Len(channel.s)-14-Len(nickname.s))
                      SendNetworkString(conid, "PRIVMSG "+channel.s+" "+text.s+EOL$)
                    Case ":QUIT"
                      SendNetworkString(conid, "QUIT "+version.s+EOL$)
                      End
                    Case ":PART"
                      channel.s = StringField(intextnew.s, 4, " ")
                      SendNetworkString(conid, "PART "+channel.s+" "+EOL$)
                      ForEach ChannelList()
                        If ChannelList()\Field2.s = channel.s
                          DeleteElement(ChannelList())
                        EndIf
                      Next
                    Case ":REJOINALL"
                      ForEach ChannelList()
                        SendNetworkString(conid, "PART "+ChannelList()\Field2.s+" "+EOL$)
                        SendNetworkString(conid, "JOIN "+ChannelList()\Field2.s+" "+EOL$)
                      Next
                    Case ":REJOIN"
                      channel.s = StringField(intextnew.s, 4, " ")
                      SendNetworkString(conid, "PART "+channel.s+" "+EOL$)
                      SendNetworkString(conid, "JOIN "+channel.s+" "+EOL$)
                    Case ":JOIN"
                      channel.s = StringField(intextnew.s, 4, " ")
                      SendNetworkString(conid, "JOIN "+channel.s+" "+EOL$)
                      ForEach ChannelList()
                        If ChannelList()\Field2.s = channel.s
                          DeleteElement(ChannelList())
                        EndIf
                      Next
                      AddElement(ChannelList())
                      ChannelList()\Field2.s = channel.s
                    Case ":CHANNELLIST"
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" Channel Listing:"+EOL$)
                      ForEach ChannelList()
                        SendNetworkString(conid, "PRIVMSG "+fromname.s+" "+ChannelList()\Field2.s+EOL$)
                      Next
                    Case ":MODE"
                      channel.s = StringField(intextnew.s, 4, " ")
                      mode.s = StringField(intextnew.s, 5, " ")
                      who.s = StringField(intextnew.s, 6, " ")
                      SendNetworkString(conid, "MODE "+channel.s+" "+mode.s+" "+who.s+EOL$)
                    Case ":HELP"
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" ######## HELP ########"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" LOGOUT - Logout from your Account"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" REJOIN [CHANNEL] - Rejoin a Channel"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" REJOINALL - Rejoin all Channels"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" JOIN [CHANNEL] - Join a Channel"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" PART [CHANNEL] - Leave a Channel"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" CHANNELLIST - List all Channels"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" MODE [CHANNEL] [MODE] [NICKNAME] - Change Mode on a Channel"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" SAY [CHANNEL] [TEXT] - Send Text to the channel"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" QUIT - Kill Me"+EOL$)
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" ######## HELP ########"+EOL$)
                    Default 
                      SendNetworkString(conid, "PRIVMSG "+fromname.s+" Command not found! Enter HELP to get the command list!"+EOL$)
                  EndSelect
                
                EndIf
              EndIf
          EndSelect
      EndSelect
      
      FreeMemory(*Buffer)
      *Buffer = AllocateMemory(4096)
      intext.s = ""
    EndIf

  If firstconnection.l = 1
    ForEach ChannelList()
      SendNetworkString(conid, "JOIN "+ChannelList()\Field2.s+EOL$)
      Debug "JOIN Channel "+ChannelList()\Field2.s
    Next
    firstconnection.l = 0
  EndIf
  
Wend
------------------------------------------------------
ibot.cfg
------------------------------------------------------

Code: Alles auswählen

server = chat.nondikass.lu
port = 6667
channels = #lobby

nickname = iBot
name = Botname
host = nondikass.lu
nickpassword = testo

admin = linux
adminpassword = xxxx
adminemail = xxx@email.lu
opadmin = 1

nickserv = nickserv
identifystring = identify [PASSWORD]
registerstring = register [PASSWORD] [EMAIL]
registernick = 1

scriptfile = script.cfg; Default script
Zuletzt geändert von ndklinux am 10.05.2007 12:45, insgesamt 1-mal geändert.
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Beitrag von ts-soft »

Bitte Code-Tags setzen:
[ code ]hier code[ /code ]
ohne die spaces! Passwörter hier zu posten ist auch suboptional, ich hoffe die
sind nicht wichtig, auch google liest hier mit :mrgreen:
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
ndklinux
Beiträge: 5
Registriert: 09.05.2007 16:56
Wohnort: Esch/Alzette

Beitrag von ndklinux »

Danke für die Tips. Nein die Passwörter sind eh Falsch.
Antworten