Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Daniel Aberger 2016-03-14 22:53:51 +01:00
commit 8ef89b3178
12 changed files with 64 additions and 51 deletions

25
main.go
View File

@ -9,11 +9,12 @@ package main
import (
"flag"
"fmt"
"log"
"runtime"
"strings"
"time"
"code.dnix.de/an/xlog"
"github.com/nickvanw/ircx"
"github.com/sorcix/irc"
"github.com/sorcix/irc/ctcp"
@ -42,13 +43,15 @@ var (
func main() {
sayCh = make(chan string, 1024)
xlog.Init(xlog.INFO)
//bot := ircx.Classic(*server, *name)
cfg := ircx.Config{User: *name, MaxRetries: 1000}
bot := ircx.New(*server, *name, cfg)
log.Printf("%s started", SoftwareInfo())
log.Println("Logging in")
xlog.Info("%s started", SoftwareInfo())
xlog.Info("Logging in")
if err := bot.Connect(); err != nil {
log.Panicln("Unable to dial IRC Server:", err)
xlog.Fatal("Unable to dial IRC Server: %v", err)
}
//mods := strings.Split(*modules, ",")
@ -81,11 +84,11 @@ func main() {
RegisterHandlers(bot)
bot.HandleLoop()
log.Println("Exiting")
xlog.Info("Exiting")
}
func RegisterHandlers(bot *ircx.Bot) {
log.Println("Registering handlers")
xlog.Info("Registering handlers")
bot.HandleFunc(irc.RPL_WELCOME, ConnectHandler)
bot.HandleFunc(irc.PING, PingHandler)
bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler)
@ -93,14 +96,14 @@ func RegisterHandlers(bot *ircx.Bot) {
func ConnectHandler(s ircx.Sender, m *irc.Message) {
if *nspass != "" {
log.Printf("Authenticating with NickServ: %v, %v", *name, *nspass)
xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass)
s.Send(&irc.Message{
Command: irc.PRIVMSG,
Params: []string{*nsname},
Trailing: "IDENTIFY " + *name + " " + *nspass,
})
}
log.Printf("Joining channels: %v", *channels)
xlog.Info("Joining channels: %v", *channels)
for _, ch := range strings.Split(*channels, ",") {
s.Send(&irc.Message{
Command: irc.JOIN,
@ -108,12 +111,12 @@ func ConnectHandler(s ircx.Sender, m *irc.Message) {
})
}
time.Sleep(2 * time.Second)
msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf("running on %s", SoftwareInfo()))
msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
}
func PingHandler(s ircx.Sender, m *irc.Message) {
log.Println("PingPong")
xlog.Info("PingPong")
s.Send(&irc.Message{
Command: irc.PONG,
Params: m.Params,
@ -134,7 +137,7 @@ func HandleMessage(m *irc.Message) {
}
switch tok[0] {
case "!version":
msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf("running on %s", SoftwareInfo()))
msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
//sayCh <- fmt.Sprintf("%s\n%s", "*", SoftwareInfo())
default:

View File

@ -3,9 +3,10 @@
package modules
import (
"log"
"strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
@ -13,7 +14,7 @@ var ()
func init() {
MsgHandlers["announcements"] = anncouncementsHandleMessage
log.Println("Initializing announcements module")
xlog.Info("Announcements module initialized")
}
func anncouncementsHandleMessage(m *irc.Message) {

View File

@ -7,16 +7,17 @@
package modules
import (
"log"
"strings"
"time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
func init() {
MsgHandlers["coffee"] = coffeeHandleMessage
log.Println("Initializing coffee module")
xlog.Info("Coffee module initialized")
}
func coffeeHandleMessage(m *irc.Message) {

View File

@ -5,16 +5,17 @@ package modules
import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
func init() {
MsgHandlers["fortune"] = fortuneHandleMessage
log.Println("Initializing fortune module")
xlog.Info("Fortune module initialized")
}
func fortuneHandleMessage(m *irc.Message) {

View File

@ -4,16 +4,17 @@ package modules
import (
"fmt"
"log"
"strings"
"time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
func init() {
MsgHandlers["fuzzytime"] = fuzzytimeHandleMessage
log.Println("Initializing fuzzytime module")
xlog.Info("Fuzzytime module initialized")
}
func fuzzytimeHandleMessage(m *irc.Message) {

View File

@ -5,10 +5,11 @@
package modules
import (
"log"
"strings"
"time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
@ -17,8 +18,8 @@ var (
)
func init() {
MsgHandlers["fuzzytime"] = gogsHandleMessage
log.Println("Initializing gogs module")
MsgHandlers["gogs"] = gogsHandleMessage
xlog.Info("Gogs module initialized")
}
func gogsConfig() {

View File

@ -7,7 +7,6 @@
package modules
import (
"log"
"strings"
"github.com/sorcix/irc"
@ -24,7 +23,6 @@ func Init(ch chan string, params string) {
for _, param := range strings.Split(params, "!") {
kv := strings.Split(param, ":")
ModParams[kv[0]] = kv[1]
log.Println(kv[0], kv[1])
}
}

View File

@ -11,10 +11,11 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"time"
"code.dnix.de/an/xlog"
gorss "github.com/jteeuwen/go-pkg-rss"
"github.com/jteeuwen/go-pkg-xmlx"
"github.com/sorcix/irc"
@ -24,8 +25,8 @@ var hideOutput = true
func init() {
MsgHandlers["rss"] = rssHandleMessage
log.Printf("Initializing rss module")
go rssRun()
xlog.Info("RSS module initialized")
}
func rssRun() {
@ -33,7 +34,7 @@ func rssRun() {
path := ModParams["rss-feeds"]
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
xlog.Fatal(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
@ -41,7 +42,7 @@ func rssRun() {
go rssPollFeed(scanner.Text(), 5, rssCharsetReader)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
xlog.Fatal(err.Error())
}
go func() {
time.Sleep(60 * time.Second)
@ -55,9 +56,9 @@ func rssHandleMessage(m *irc.Message) {
func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) {
feed := gorss.New(timeout, true, rssChanHandler, rssItemHandler)
for {
log.Printf("Polling feed: %s", uri)
xlog.Info("Polling feed: %s", uri)
if err := feed.Fetch(uri, cr); err != nil {
log.Printf("[e] %s: %s", "*", uri, err)
xlog.Info("[e] %s: %s", "*", uri, err)
SayCh <- fmt.Sprintf("%s\n[RSS] Error %s: %s", "*", uri, err)
return
}

View File

@ -7,13 +7,14 @@ import (
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"code.dnix.de/an/xlog"
"flokatirc/util"
"github.com/sorcix/irc"
@ -48,8 +49,8 @@ var (
func init() {
MsgHandlers["sc"] = scHandleMessage
log.Println("Initializing sc module")
go scScrapeLoop()
xlog.Info("SC module initialized")
}
func scHandleMessage(m *irc.Message) {
@ -82,22 +83,22 @@ func scScrapeLoop() {
func scScraper() {
var data interface{}
log.Println("Scraping SC stats")
xlog.Info("Scraping SC stats")
resp, err := http.PostForm(API_URL+API_METHOD,
url.Values{"fans": {"true"}, "fleet": {"true"}, "funds": {"true"}})
if err != nil {
log.Printf("Error: %v", err)
xlog.Info("Error: %v", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error: %v", err)
xlog.Info("Error: %v", err)
return
}
err = json.Unmarshal(body, &data)
if err != nil {
log.Printf("Error: %v", err)
xlog.Info("Error: %v", err)
return
}
stats := data.(map[string]interface{})["data"].(map[string]interface{})
@ -135,13 +136,13 @@ func showScStats() {
func showCitizen(handle string) {
resp, err := http.Get(QUERY_CIT_URL + handle)
if err != nil {
log.Printf("Error: %v", err)
xlog.Info("Error: %v", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error: %v", err)
xlog.Info("Error: %v", err)
return
}
reName := regexp.MustCompile(RE_CIT_NAME)
@ -175,13 +176,13 @@ func showCitizen(handle string) {
func showOrganization(handle string) {
resp, err := http.Get(QUERY_ORG_URL + handle)
if err != nil {
log.Printf("Error: %v", err)
xlog.Info("Error: %v", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error: %v", err)
xlog.Info("Error: %v", err)
return
}
reName := regexp.MustCompile(RE_ORG_NAME)

View File

@ -7,6 +7,8 @@ import (
"fmt"
"strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
@ -521,6 +523,7 @@ var quotes = [][]string{
func init() {
MsgHandlers["stoll"] = stollHandleMessage
xlog.Info("Stoll module initialized")
}
func stollHandleMessage(m *irc.Message) {

View File

@ -10,11 +10,12 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"code.dnix.de/an/xlog"
"flokatirc/util"
"github.com/sorcix/irc"
@ -137,8 +138,8 @@ var (
func init() {
MsgHandlers["twitch"] = twitchHandleMessage
log.Println("Initializing twitch module")
go pollStreamData()
xlog.Info("Twitch module initialized")
}
func twitchHandleMessage(m *irc.Message) {
@ -208,11 +209,11 @@ func getTwitchStreamObject(streamname string) TwitchStreamObject {
var tobj TwitchStreamObject
resp, err := http.Get(twsurl)
if err != nil {
log.Println(err)
xlog.Error(err.Error())
} else {
reader, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
xlog.Error(err.Error())
} else {
defer resp.Body.Close()
json.Unmarshal(reader, &tobj)
@ -227,11 +228,11 @@ func getTwitchChannelObject(streamname string) TwitchChannelObject {
var tcobj TwitchChannelObject
resp2, err := http.Get(twcurl)
if err != nil {
log.Println(err)
xlog.Error(err.Error())
} else {
reader2, err := ioutil.ReadAll(resp2.Body)
if err != nil {
log.Println(err)
xlog.Error(err.Error())
} else {
defer resp2.Body.Close()
json.Unmarshal(reader2, &tcobj)

View File

@ -10,12 +10,13 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
@ -71,8 +72,8 @@ type WeatherObject struct {
func init() {
MsgHandlers["weather"] = weatherHandleMessage
log.Println("Initializing weather module")
go weatherConfig()
xlog.Info("Weather module initialized")
}
func weatherConfig() {
@ -105,19 +106,19 @@ func getWeather(query string) {
q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey
r, err := http.Get(q)
if err != nil {
log.Println(err)
xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
} else {
re, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
} else {
defer r.Body.Close()
var wo WeatherObject
err := json.Unmarshal(re, &wo)
if err != nil {
log.Println(err)
xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
} else {
var description string