diff --git a/main.go b/main.go index 60595ef..07cc330 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,9 @@ package main import ( "fmt" "log" - "net" "strings" "crypto/tls" - "github.com/oschwald/geoip2-golang" "github.com/thoj/go-ircevent" "irc_bot/utils" ) @@ -19,9 +17,10 @@ func main() { log.Fatalf("Error loading config: %v", err) } + // Initialize the IRC bot irccon := irc.IRC(config.Nickname, config.Nickname) - irccon.VerboseCallbackHandler = true - irccon.Debug = true + irccon.VerboseCallbackHandler = config.Debug + irccon.Debug = config.Callback irccon.UseTLS = config.SSL irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true} @@ -36,6 +35,7 @@ func main() { } }) + // Read the messages to get the content and call the apropriate command if has irccon.AddCallback("PRIVMSG", func(e *irc.Event) { if len(e.Arguments) < 2 { return @@ -48,108 +48,25 @@ func main() { } cmd := strings.TrimPrefix(parts[0], "!") - if commandFunc, ok := commands[cmd]; ok { - commandFunc(irccon, e, parts) + switch { + // For each new command, add a new case + case cmd == "geoip": + // Call GeoIP command that return the query data + response, err := utils.HandleGeoIPCommand(parts[1], config.GeoIP_City, config.GeoIP_ASN) + if err != nil { + irccon.Privmsg(e.Arguments[0], fmt.Sprintf("%v", err)) + } else { + irccon.Privmsg(e.Arguments[0], response) + } + + case cmd == "commands": + // List which are the available commands to use + irccon.Privmsg(e.Arguments[0], "Current available commands: !geoip") + + default: + return } }) irccon.Loop() } - -type CommandFunc func(irccon *irc.Connection, e *irc.Event, args []string) - -var commands = map[string]CommandFunc{ - "geoip": handleGeoIPCommand, - // add more commands here -} - -func handleGeoIPCommand(irccon *irc.Connection, e *irc.Event, args []string) { - if len(args) < 2 { - irccon.Privmsg(e.Arguments[0], "Usage: !geoip ") - return - } - - ipOrDomain := args[1] - - config, err := LoadConfig("config.json") - if err != nil { - log.Fatalf("Error loading config: %v", err) - } - - cityDbPath := config.GeoIPDatabase // Update with actual path - asnDbPath := config.GeoIPASN // Update with actual path - - ip := net.ParseIP(ipOrDomain) - if ip == nil { - resolvedIP, err := net.LookupIP(ipOrDomain) - if err != nil || len(resolvedIP) == 0 { - irccon.Privmsg(e.Arguments[0], fmt.Sprintf("Invalid IP address or domain: %s", ipOrDomain)) - return - } - ip = resolvedIP[0] - } - - var ( - city = "N/A" - subdivision = "N/A" - country = "N/A" - latitude = "N/A" - longitude = "N/A" - asn = "N/A" - isp = "N/A" - ) - - // Lookup City Information - if cityDb, err := geoip2.Open(cityDbPath); err == nil { - defer cityDb.Close() - - if cityRecord, err := cityDb.City(ip); err == nil { - if name := cityRecord.City.Names["en"]; name != "" { - city = name - } - if len(cityRecord.Subdivisions) > 0 { - if name := cityRecord.Subdivisions[0].Names["en"]; name != "" { - subdivision = name - } - } - if name := cityRecord.Country.Names["en"]; name != "" { - country = name - } - if lat := cityRecord.Location.Latitude; lat != 0 { - latitude = fmt.Sprintf("%.6f", lat) - } - if lon := cityRecord.Location.Longitude; lon != 0 { - longitude = fmt.Sprintf("%.6f", lon) - } - } else { - log.Printf("City lookup error: %v", err) - } - } else { - log.Printf("Error opening City database: %v", err) - } - - // Lookup ASN Information - if asnDb, err := geoip2.Open(asnDbPath); err == nil { - defer asnDb.Close() - - if asnRecord, err := asnDb.ASN(ip); err == nil { - if asnNum := asnRecord.AutonomousSystemNumber; asnNum != 0 { - asn = fmt.Sprintf("%d", asnNum) - } - if org := asnRecord.AutonomousSystemOrganization; org != "" { - isp = org - } - } else { - log.Printf("ASN lookup error: %v", err) - } - } else { - log.Printf("Error opening ASN database: %v", err) - } - - response := fmt.Sprintf( - "IP: %s | Location: %s, %s, %s | Coordinates: %s, %s | ASN: %s | ISP: %s", - ip.String(), city, subdivision, country, latitude, longitude, asn, isp, - ) - - irccon.Privmsg(e.Arguments[0], response) -}