helper-fns.go (1693B)
1 package main 2 3 import ( 4 "log" 5 "os" 6 "strings" 7 "time" 8 9 "github.com/go-irc/irc" 10 "github.com/joho/godotenv" 11 ) 12 13 func IsPM(receiver string) bool { 14 return !strings.HasPrefix(receiver, "#") 15 } 16 17 func GetMaintainers() []string { 18 err := godotenv.Load() 19 if err != nil { 20 log.Fatal(err) 21 } 22 MAINTAINERS := os.Getenv("MAINTAINERS") 23 maintainers := strings.Split(MAINTAINERS, ",") 24 return maintainers 25 } 26 27 func InformErrAndQuit(c *irc.Client, m *irc.Message) { 28 maintainers := GetMaintainers() 29 for _, maintainer := range maintainers { 30 SendMsg(c, maintainer, "Error") 31 } 32 time.Sleep(5 * time.Second) 33 os.Exit(1) 34 } 35 36 func HandlePM(msg string, sender string) { 37 maintainers := GetMaintainers() 38 if msg != "KILL" { 39 return 40 } 41 for _, maintainer := range maintainers { 42 if maintainer == sender { 43 os.Exit(1) 44 } 45 } 46 } 47 48 func GetChans() []string { 49 err := godotenv.Load() 50 if err != nil { 51 log.Fatal(err) 52 } 53 IRC_CHANNELS := os.Getenv("IRC_CHANNELS") 54 channels := strings.Split(IRC_CHANNELS, ",") 55 return channels 56 } 57 58 func GetIRCEnvVars() (string, string, string, string) { 59 err := godotenv.Load() 60 if err != nil { 61 log.Fatal(err) 62 } 63 IRC_NICK := os.Getenv("IRC_NICK") 64 IRC_PASS := os.Getenv("IRC_PASS") 65 IRC_USER := os.Getenv("IRC_USER") 66 IRC_NAME := os.Getenv("IRC_NAME") 67 return IRC_NICK, IRC_PASS, IRC_USER, IRC_NAME 68 } 69 70 func GetMsgDetails(m *irc.Message) (string, string, string) { 71 from := m.Name 72 to := m.Params[0] 73 msg := m.Params[1] 74 return from, to, msg 75 } 76 77 func JoinChannels(c *irc.Client) { 78 IRC_CHANNELS := GetChans() 79 for _, channel := range IRC_CHANNELS { 80 c.Writef("JOIN %s", channel) 81 } 82 } 83 84 func SendMsg(c *irc.Client, to string, msg string) { 85 c.Writef("%s %s %s", "PRIVMSG", to, msg) 86 }