@ -1,17 +1,30 @@
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"gopkg.in/sorcix/irc.v2"
)
const PushoverAPI string = "https://api.pushover.net/1/messages.json"
func main ( ) {
conn , err := irc . DialTLS ( "<irc server>" , & tls . Config { } )
config , err := LoadConfig ( )
if err != nil {
log . Fatalf ( "Unable to configure: %s" , err )
}
//TODO (noah): probably should check for protocol + port or something
conn , err := irc . DialTLS ( config . Host , & tls . Config { } )
if err != nil {
log . Fatalf ( "Unable to connect to IRC server: %s" , err )
}
@ -29,27 +42,79 @@ func main() {
conn . Close ( )
os . Exit ( 0 )
} ( )
_ , err = conn . Write ( [ ] byte ( "PASS <password>" ) )
if err != nil {
log . Fatalf ( "Unable to authenticate to server: %s" , err )
if config . Pass != "" {
_ , err = conn . Write ( [ ] byte ( "PASS " + config . Pass ) )
if err != nil {
log . Fatalf ( "Unable to authenticate to server: %s" , err )
}
}
_ , err = conn . Write ( [ ] byte ( "NICK -testbot" ) )
_ , err = conn . Write ( [ ] byte ( "USER " + config . User ) )
if err != nil {
log . Fatalf ( "Unable to send message to server: %s" , err )
}
_ , err = conn . Write ( [ ] byte ( "USER -testbot" ) )
_ , err = conn . Write ( [ ] byte ( "NICK " + config . Nick ) )
if err != nil {
log . Fatalf ( "Unable to send message to server: %s" , err )
}
_ , err = conn . Write ( [ ] byte ( "JOIN #channel" ) )
_ , err = conn . Write ( [ ] byte ( "WHOIS " + config . Nick ) )
if err != nil {
log . Fatalf ( "Unable to send message to server: %s" , err )
}
away := false
for running {
raw_ message, err := conn . Decode ( )
message , err := conn . Decode ( )
if err != nil {
log . Printf ( "Unable to decode: %s" , err )
time . Sleep ( 1 * time . Second )
}
if message == nil {
continue
}
log . Printf ( "Message Prefix: %+v" , message . Prefix )
log . Printf ( "Message Command: %+v" , message . Command )
log . Printf ( "Message Params: %v" , message . Params )
if away && message . Command == irc . PRIVMSG {
log . Printf ( "Away and received message: %s, checking if contains search term: %s" , message . Params [ 1 ] , config . Match )
if strings . Contains ( message . Params [ 1 ] , config . Match ) {
log . Printf ( "Message contains search word: %s" , message . Params )
err = sendNotification ( config . UserToken , config . AppToken , message )
if err != nil {
log . Printf ( "Unable to send notification: %s" , err )
}
}
} else if message . Command == irc . RPL_NOWAWAY {
log . Printf ( "Marking as away, will notify" )
away = true
} else if message . Command == irc . RPL_UNAWAY {
log . Printf ( "No longer away, won't notify" )
away = false
} else if message . Command == irc . RPL_NAMREPLY {
//This is used to query for away status on startup
//TODO (noah): is this valid?
config . Nick = message . Params [ 0 ]
log . Printf ( "Set nick reference to: %s" , config . Nick )
} else if message . Command == irc . NICK {
config . Nick = message . Params [ 0 ]
log . Printf ( "Setting nick to: %s" , config . Nick )
}
log . Printf ( "%s" , raw_message )
}
}
func sendNotification ( user_token , app_token string , message * irc . Message ) error {
body := map [ string ] string {
"token" : app_token ,
"user" : user_token ,
"title" : fmt . Sprintf ( "%s on %s mentioned you" , message . Prefix . User , message . Params [ 0 ] ) ,
"message" : message . Params [ 1 ] ,
}
json_body , err := json . Marshal ( body )
if err != nil {
return err
}
resp , err := http . Post ( PushoverAPI , "application/json" , bytes . NewBuffer ( json_body ) )
if err != nil {
return err
}
log . Printf ( "Pushover API notify resp code: %d %s" , resp . StatusCode , resp . Status )
return nil
}