Skip to content

Commit

Permalink
Merge pull request #9 from imachug/irc-formatting
Browse files Browse the repository at this point in the history
Irc formatting
  • Loading branch information
podivilov authored Dec 29, 2021
2 parents 74a26d7 + 712e1df commit c361427
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bridge/irc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
rmsg.Text = string(output)
}

rmsg.Text = strings.Replace(rmsg.Text, "\x02", "**", -1)
rmsg.Text = strings.Replace(rmsg.Text, "\x1d", "*", -1)
rmsg.Text = strings.Replace(rmsg.Text, "\x1e", "~~", -1)

b.Log.Debugf("<= Sending message from %s on %s to gateway", event.Params[0], b.Account)
b.Remote <- rmsg
}
Expand Down
7 changes: 7 additions & 0 deletions bridge/irc/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"hash/crc32"
"io/ioutil"
"net"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -164,6 +165,12 @@ func (b *Birc) Send(msg config.Message) (string, error) {
var msgLines []string
if b.GetBool("StripMarkdown") {
msg.Text = stripmd.Strip(msg.Text)
} else {
re := regexp.MustCompile(`\b(\*\*|__)|(\*\*|__)\b`)
msg.Text = re.ReplaceAllString(msg.Text, "\x02")

re = regexp.MustCompile(`\b(\*|_)|(\*|_)\b`)
msg.Text = re.ReplaceAllString(msg.Text, "\x1d")
}

if b.GetBool("MessageSplit") {
Expand Down
28 changes: 23 additions & 5 deletions bridge/sshchat/sshchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,29 @@ func (b *Bsshchat) Send(msg config.Message) (string, error) {
return "", nil
}
b.Log.Debugf("=> Receiving %#v", msg)

prefix := msg.Username
if msg.Event == config.EventUserAction {
prefix += "/me "
}

string_message := ""
for _, line := range strings.Split(msg.Text, "\n") {
if strings.TrimSpace(line) != "" {
string_message += msg.Username + line + "\r\n"
if line[0] == '/' {
line = " " + line
}
string_message += prefix + line + "\r\n"
}
}
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
for _, line := range strings.Split(rmsg.Text, "\n") {
if strings.TrimSpace(line) != "" {
string_message += rmsg.Username + line + "\r\n"
if line[0] == '/' {
line = " " + line
}
string_message += prefix + line + "\r\n"
}
}
}
Expand Down Expand Up @@ -158,9 +170,15 @@ func (b *Bsshchat) handleSSHChat() error {
b.Log.Debugf("<= Message %#v", res)
if strings.HasPrefix(text, "** ") {
// Emote
res := strings.Split(text[3:], " ")
rmsg := config.Message{Username: res[0], Text: strings.TrimSpace(strings.Join(res[1:], " ")), Channel: "sshchat", Account: b.Account, UserID: "nick", Event: config.EventUserAction}
b.Remote <- rmsg
if text[3] == '"' {
res := strings.Split(text[3:], "\"")
rmsg := config.Message{Username: res[1], Text: strings.TrimSpace(strings.Join(res[2:], "\"")), Channel: "sshchat", Account: b.Account, UserID: "nick", Event: config.EventUserAction}
b.Remote <- rmsg
} else {
res := strings.Split(text[3:], " ")
rmsg := config.Message{Username: res[0], Text: strings.TrimSpace(strings.Join(res[1:], " ")), Channel: "sshchat", Account: b.Account, UserID: "nick", Event: config.EventUserAction}
b.Remote <- rmsg
}
} else {
// Normal message
rmsg := config.Message{Username: res[0], Text: strings.TrimSpace(strings.Join(res[1:], ":")), Channel: "sshchat", Account: b.Account, UserID: "nick"}
Expand Down

0 comments on commit c361427

Please sign in to comment.