Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
#41: Preliminary work
Browse files Browse the repository at this point in the history
  • Loading branch information
tb0hdan committed Feb 22, 2018
1 parent 7e1c619 commit 3c32d7d
Show file tree
Hide file tree
Showing 19 changed files with 192 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ _testmain.go
bin/*
build/*
src/github.com/*
src/go4.org
src/go4.org/*
src/golang.org/*
src/googlemaps.github.io/*
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PKGNAME = "torpedobot"
DEST = $(PKGNAME)
BUILD = $(shell git rev-parse HEAD)
BDATE = $(shell date -u '+%Y-%m-%d_%I:%M:%S%p_UTC')
GO_VERSION = $(shell go version|awk '{print $$3}')
VERSION = $(shell cat ./VERSION)
BUILD_CMD = go build

Expand Down Expand Up @@ -36,7 +37,7 @@ report_deps:
build: deps build_only

build_only:
@$(BUILD_CMD) -v -x -ldflags "-X main.BUILD=$(BUILD) -X main.BUILD_DATE=$(BDATE) -X main.VERSION=$(VERSION)" -o bin/$(DEST) $(PKGNAME)
@$(BUILD_CMD) -v -x -ldflags "-X main.BUILD=$(BUILD) -X main.BUILD_DATE=$(BDATE) -X main.GO_VERSION=$(GO_VERSION) -X main.VERSION=$(VERSION)" -o bin/$(DEST) $(PKGNAME)


clean:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.50
1.0.51
13 changes: 13 additions & 0 deletions src/torpedobot/chatinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"

"github.com/tb0hdan/torpedo_registry"
)

func ChatInfoProcessMessage(api *torpedo_registry.BotAPI, channel interface{}, incoming_message string) {
message := fmt.Sprintf("API info:%T\n", api.ProtocolAPI)
message += fmt.Sprintf("Chat info:%s\n", channel)
api.Bot.PostMessage(channel, message, api)
}
6 changes: 4 additions & 2 deletions src/torpedobot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"torpedobot/multibot"

"github.com/erikdubbelboer/gspt"
"github.com/tb0hdan/torpedo_registry"
common "github.com/tb0hdan/torpedo_common"
"github.com/tb0hdan/torpedo_registry"
)

const ProjectURL = "https://github.com/tb0hdan/torpedo"
Expand All @@ -17,6 +17,7 @@ const ProjectURL = "https://github.com/tb0hdan/torpedo"
var (
BUILD = "Not available"
BUILD_DATE = "Not available"
GO_VERSION = "Not available"
VERSION = "Not available"
)

Expand All @@ -30,9 +31,10 @@ func main() {
torpedo_registry.Config.RegisterHelpAndHandler("h", help_msg, HelpProcessMessage)
torpedo_registry.Config.RegisterHelpAndHandler("help", help_msg, HelpProcessMessage)
torpedo_registry.Config.RegisterHelpAndHandler("stats", "Just system stats, nothing interesting", StatsProcessMessage)
torpedo_registry.Config.RegisterHelpAndHandler("chatinfo", "Chat/DM information", ChatInfoProcessMessage)

bot := multibot.New()
bot.SetBuildInfo(BUILD, BUILD_DATE, VERSION, ProjectURL)
bot.SetBuildInfo(BUILD, BUILD_DATE, GO_VERSION, VERSION, ProjectURL)
// bot cfg
// plugins/protocols
torpedo_registry.Config.RegisterParser("slack", bot.ConfigureSlackBot, bot.ParseSlackBot)
Expand Down
21 changes: 17 additions & 4 deletions src/torpedobot/multibot/facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,40 @@ func (tb *TorpedoBot) ParseFacebookBot(cfg *torpedo_registry.ConfigStruct) {
}

func (tb *TorpedoBot) RunFacebookBot(apiKey, cmd_prefix string) {
account := &torpedo_registry.Account{
APIKey: apiKey,
CommandPrefix: cmd_prefix,
}
torpedo_registry.Accounts.AppendAccounts(account)
tb.RunFacebookBotAccount(account)
}

func (tb *TorpedoBot) RunFacebookBotAccount(account *torpedo_registry.Account) {
tb.Stats.ConnectedAccounts += 1
cu := &common.Utils{}
logger := cu.NewLog("facebook-bot")

tb.RegisteredProtocols["*messenger.Response"] = HandleFacebookMessage

pageToken := strings.Split(apiKey, ":")[0]
verifyToken := strings.Split(apiKey, ":")[1]
appSecret := strings.Split(apiKey, ":")[2]
pageToken := strings.Split(account.APIKey, ":")[0]
verifyToken := strings.Split(account.APIKey, ":")[1]
appSecret := strings.Split(account.APIKey, ":")[2]
client := messenger.New(messenger.Options{
AppSecret: appSecret,
Verify: true,
VerifyToken: verifyToken,
Token: pageToken,
})

account.API = client

client.HandleMessage(func(m messenger.Message, r *messenger.Response) {
logger.Printf("%v (Sent, %v)\n", m.Text, m.Time.Format(time.UnixDate))

botApi := &TorpedoBotAPI{}
botApi.API = r
botApi.Bot = tb
botApi.CommandPrefix = cmd_prefix
botApi.CommandPrefix = account.CommandPrefix
botApi.UserProfile = &torpedo_registry.UserProfile{ID: fmt.Sprintf("%v", m.Sender.ID)}
// FIXME: Get ID and remove hardcode
botApi.Me = "torpedobot"
Expand All @@ -105,6 +117,7 @@ func (tb *TorpedoBot) RunFacebookBot(apiKey, cmd_prefix string) {

logger.Printf("Serving messenger bot on %s\n", torpedo_registry.Config.GetConfig()["facebookincomingaddr"])

account.Connection.ReconnectCount += 1
if err := http.ListenAndServe(torpedo_registry.Config.GetConfig()["facebookincomingaddr"], client.Handler()); err != nil {
logger.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/torpedobot/multibot/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package multibot

import (
"flag"
log "github.com/sirupsen/logrus"
"net/http"

log "github.com/sirupsen/logrus"

"github.com/ant0ine/go-json-rest/rest"

common "github.com/tb0hdan/torpedo_common"
Expand Down
27 changes: 19 additions & 8 deletions src/torpedobot/multibot/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"fmt"

"log"
"os"
"os"

common "github.com/tb0hdan/torpedo_common"
"github.com/tb0hdan/torpedo_registry"
irc "github.com/thoj/go-ircevent"
Expand Down Expand Up @@ -78,24 +79,33 @@ func (tb *TorpedoBot) myIRC(nick, user string, log *log.Logger) *irc.Connection
}

func (tb *TorpedoBot) RunIRCBot(apiKey, cmd_prefix string) {
account := &torpedo_registry.Account{
APIKey: apiKey,
CommandPrefix: cmd_prefix,
}
torpedo_registry.Accounts.AppendAccounts(account)
tb.RunIRCBotAccount(account)
}

func (tb *TorpedoBot) RunIRCBotAccount(account *torpedo_registry.Account) {
var (
nick, server string
)
tb.Stats.ConnectedAccounts += 1
//cu := &common.Utils{}
logger := log.New(os.Stdout, "file-plugin: ", log.Lshortfile|log.LstdFlags) //cu.NewLog("irc-bot")
logger := log.New(os.Stdout, "irc-bot: ", log.Lshortfile|log.LstdFlags) //cu.NewLog("irc-bot")
tb.RegisteredProtocols["*multibot.IRCAPI"] = HandleIRCMessage

user_server := strings.Split(apiKey, ":")[0]
user_server := strings.Split(account.APIKey, ":")[0]
if len(strings.Split(user_server, "@")) == 2 {
nick = strings.Split(user_server, "@")[0]
server = strings.Split(user_server, "@")[1]
} else {
nick = "torpedobot"
server = user_server
}
port := strings.Split(apiKey, ":")[1]
usessl := strings.Split(apiKey, ":")[2]
port := strings.Split(account.APIKey, ":")[1]
usessl := strings.Split(account.APIKey, ":")[2]

irccon := tb.myIRC(nick, fmt.Sprintf("%s bot", nick), logger)
if torpedo_registry.Config.GetConfig()["debug"] == "yes" {
Expand All @@ -110,8 +120,8 @@ func (tb *TorpedoBot) RunIRCBot(apiKey, cmd_prefix string) {
}

// Password config
if len(strings.Split(apiKey, ":")) > 3 {
irccon.Password = strings.Split(apiKey, ":")[3]
if len(strings.Split(account.APIKey, ":")) > 3 {
irccon.Password = strings.Split(account.APIKey, ":")[3]
}

//welcome
Expand Down Expand Up @@ -156,7 +166,7 @@ func (tb *TorpedoBot) RunIRCBot(apiKey, cmd_prefix string) {
api := &IRCAPI{Connection: irccon, Event: event}
botApi.API = api
botApi.Bot = tb
botApi.CommandPrefix = cmd_prefix
botApi.CommandPrefix = account.CommandPrefix
botApi.UserProfile = &torpedo_registry.UserProfile{ID: fmt.Sprintf("%s@%s", event.User, server), Nick: event.Nick, Server: server}
botApi.Me = irccon.GetNick()

Expand All @@ -169,6 +179,7 @@ func (tb *TorpedoBot) RunIRCBot(apiKey, cmd_prefix string) {
tb.logger.Printf("Err %s", err)
return
}
account.Connection.ReconnectCount += 1
// blocking run here
irccon.Loop()

Expand Down
20 changes: 15 additions & 5 deletions src/torpedobot/multibot/jabber.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

common "github.com/tb0hdan/torpedo_common"

"github.com/mattn/go-xmpp"
"github.com/tb0hdan/torpedo_registry"
"gopkg.in/mgo.v2/bson"
"github.com/mattn/go-xmpp"
)

var JabberAPIKey *string
Expand Down Expand Up @@ -83,14 +83,24 @@ func (tb *TorpedoBot) ParseJabberBot(cfg *torpedo_registry.ConfigStruct) {
}

func (tb *TorpedoBot) RunJabberBot(apiKey, cmd_prefix string) {
account := &torpedo_registry.Account{
APIKey: apiKey,
CommandPrefix: cmd_prefix,
}
torpedo_registry.Accounts.AppendAccounts(account)
tb.RunJabberBotAccount(account)
}

func (tb *TorpedoBot) RunJabberBotAccount(account *torpedo_registry.Account) {
var talk *xmpp.Client
var err error
tb.Stats.ConnectedAccounts += 1
account.Connection.ReconnectCount += 1
cu := &common.Utils{}

logger := cu.NewLog("jabber-bot")
str_jid := strings.Split(apiKey, ":")[0]
password := strings.Split(apiKey, ":")[1]
str_jid := strings.Split(account.APIKey, ":")[0]
password := strings.Split(account.APIKey, ":")[1]
server := strings.Split(str_jid, "@")[1]
options := xmpp.Options{Host: server,
User: str_jid,
Expand All @@ -103,7 +113,7 @@ func (tb *TorpedoBot) RunJabberBot(apiKey, cmd_prefix string) {
}

talk, err = options.NewClient()

account.API = talk
tb.RegisteredProtocols["*xmpp.Client"] = HandleJabberMessage

if err != nil {
Expand Down Expand Up @@ -164,7 +174,7 @@ func (tb *TorpedoBot) RunJabberBot(apiKey, cmd_prefix string) {
botApi := &TorpedoBotAPI{}
botApi.API = talk
botApi.Bot = tb
botApi.CommandPrefix = cmd_prefix
botApi.CommandPrefix = account.CommandPrefix
botApi.UserProfile = &torpedo_registry.UserProfile{ID: v.Remote}
botApi.Me = GetStrippedJID(talk)
botApi.Type = v.Type
Expand Down
19 changes: 16 additions & 3 deletions src/torpedobot/multibot/kik.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"flag"
"fmt"
"io/ioutil"
log "github.com/sirupsen/logrus"
"net/http"
"strings"

log "github.com/sirupsen/logrus"

common "github.com/tb0hdan/torpedo_common"
"github.com/tb0hdan/torpedo_registry"
)
Expand Down Expand Up @@ -176,17 +177,29 @@ func (tb *TorpedoBot) ParseKikBot(cfg *torpedo_registry.ConfigStruct) {
}

func (tb *TorpedoBot) RunKikBot(apiKey, cmd_prefix string) {
account := &torpedo_registry.Account{
APIKey: apiKey,
CommandPrefix: cmd_prefix,
}
torpedo_registry.Accounts.AppendAccounts(account)
tb.RunKikBotAccount(account)
}

func (tb *TorpedoBot) RunKikBotAccount(account *torpedo_registry.Account) {
tb.Stats.ConnectedAccounts += 1
account.Connection.ReconnectCount += 1

cu := &common.Utils{}

logger := cu.NewLog("kik-bot")
api := &KikAPI{}
api.logger = logger
api.WebHook = torpedo_registry.Config.GetConfig()["kikwebhook"]
api.GetToken(strings.Split(apiKey, ":")[0], strings.Split(apiKey, ":")[1])
api.GetToken(strings.Split(account.APIKey, ":")[0], strings.Split(account.APIKey, ":")[1])
api.Configure()

account.API = api

tb.RegisteredProtocols["*multibot.KikAPI"] = HandleKikMessage

http.HandleFunc("/incoming", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -208,7 +221,7 @@ func (tb *TorpedoBot) RunKikBot(apiKey, cmd_prefix string) {
botApi := &TorpedoBotAPI{}
botApi.API = api
botApi.Bot = tb
botApi.CommandPrefix = cmd_prefix
botApi.CommandPrefix = account.CommandPrefix
botApi.UserProfile = &torpedo_registry.UserProfile{ID: message.From}
// FIXME: Remove hardcode
botApi.Me = "torpedobot"
Expand Down
17 changes: 14 additions & 3 deletions src/torpedobot/multibot/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,29 @@ func (tb *TorpedoBot) ParseLineBot(cfg *torpedo_registry.ConfigStruct) {
}

func (tb *TorpedoBot) RunLineBot(apiKey, cmd_prefix string) {
account := &torpedo_registry.Account{
APIKey: apiKey,
CommandPrefix: cmd_prefix,
}
torpedo_registry.Accounts.AppendAccounts(account)
tb.RunLineBotAccount(account)
}

func (tb *TorpedoBot) RunLineBotAccount(account *torpedo_registry.Account) {
tb.Stats.ConnectedAccounts += 1
account.Connection.ReconnectCount += 1

cu := &common.Utils{}

logger := cu.NewLog("line-bot")

bot, err := linebot.New(strings.Split(apiKey, ":")[0],
strings.Split(apiKey, ":")[1])
bot, err := linebot.New(strings.Split(account.APIKey, ":")[0],
strings.Split(account.APIKey, ":")[1])
if err != nil {
logger.Fatal(err)
}

account.API = bot
tb.RegisteredProtocols["*linebot.Client"] = HandleLineMessage

http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -86,7 +97,7 @@ func (tb *TorpedoBot) RunLineBot(apiKey, cmd_prefix string) {
botApi := &TorpedoBotAPI{}
botApi.API = bot
botApi.Bot = tb
botApi.CommandPrefix = cmd_prefix
botApi.CommandPrefix = account.CommandPrefix
botApi.UserProfile = &torpedo_registry.UserProfile{ID: channel}
botApi.Me = "torpedobot"

Expand Down
7 changes: 5 additions & 2 deletions src/torpedobot/multibot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package multibot

import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"strings"
"sync"
"time"

log "github.com/sirupsen/logrus"

"os/signal"
"syscall"

Expand Down Expand Up @@ -39,6 +40,7 @@ type TorpedoBot struct {
Build struct {
Build string
BuildDate string
GoVersion string
Version string
ProjectURL string
}
Expand Down Expand Up @@ -158,9 +160,10 @@ func (tb *TorpedoBot) RunBotsCSV(method func(apiKey, cmd_prefix string), CSV, cm
}
}

func (tb *TorpedoBot) SetBuildInfo(build, buildDate, version, projecturl string) {
func (tb *TorpedoBot) SetBuildInfo(build, buildDate, goversion, version, projecturl string) {
tb.Build.Build = build
tb.Build.BuildDate = buildDate
tb.Build.GoVersion = goversion
tb.Build.Version = version
tb.Build.ProjectURL = projecturl
return
Expand Down
Loading

0 comments on commit 3c32d7d

Please sign in to comment.