format file

This commit is contained in:
zhaoYangguang 2021-11-16 18:02:40 +08:00
parent b8da92fc1b
commit ec4dbf1e07
2 changed files with 58 additions and 72 deletions

View File

@ -2,14 +2,13 @@ package main
import ( import (
"bytes" "bytes"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"maunium.net/go/mautrix/patch" "maunium.net/go/mautrix/patch"
"encoding/hex" "encoding/hex"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
skype "github.com/kelaresg/go-skypeapi"
skypeExt "github.com/kelaresg/matrix-skype/skype-ext"
"html" "html"
"image" "image"
"image/gif" "image/gif"
@ -24,6 +23,9 @@ import (
"sync" "sync"
"time" "time"
skype "github.com/kelaresg/go-skypeapi"
skypeExt "github.com/kelaresg/matrix-skype/skype-ext"
"github.com/pkg/errors" "github.com/pkg/errors"
log "maunium.net/go/maulogger/v2" log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
@ -205,16 +207,16 @@ func (portal *Portal) handleMessage(msg PortalMessage) {
portal.HandleTextMessage(msg.source, data) portal.HandleTextMessage(msg.source, data)
case "RichText/UriObject": case "RichText/UriObject":
//portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false) //portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false)
portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType,nil, data,false) portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType, nil, data, false)
case "RichText/Media_Video": case "RichText/Media_Video":
//portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false) //portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false)
portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType,nil, data,false) portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType, nil, data, false)
case "RichText/Media_AudioMsg": case "RichText/Media_AudioMsg":
//portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false) //portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false)
portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType,nil, data,false) portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType, nil, data, false)
case "RichText/Media_GenericFile": case "RichText/Media_GenericFile":
//portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false) //portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Caption, 0, false)
portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType,nil, data,false) portal.HandleMediaMessageSkype(msg.source, data.Download, data.MessageType, nil, data, false)
case "RichText/Contacts": case "RichText/Contacts":
portal.HandleContactMessageSkype(msg.source, data) portal.HandleContactMessageSkype(msg.source, data)
case "RichText/Location": case "RichText/Location":
@ -297,7 +299,7 @@ func (portal *Portal) markHandledSkype(source *User, message *skype.Resource, mx
} }
msg.Content = message.Content msg.Content = message.Content
if len(message.Id)>0 { if len(message.Id) > 0 {
msg.ID = message.Id msg.ID = message.Id
} }
msg.Insert() msg.Insert()
@ -341,7 +343,7 @@ func (portal *Portal) getMessageIntentSkype(user *User, info skype.Resource) *ap
fmt.Println() fmt.Println()
fmt.Println("getMessageIntentSkype") fmt.Println("getMessageIntentSkype")
fmt.Println() fmt.Println()
return portal.bridge.GetPuppetByJID(info.SendId+skypeExt.NewUserSuffix).IntentFor(portal) return portal.bridge.GetPuppetByJID(info.SendId + skypeExt.NewUserSuffix).IntentFor(portal)
} }
func (portal *Portal) handlePrivateChatFromMe(fromMe bool) func() { func (portal *Portal) handlePrivateChatFromMe(fromMe bool) func() {
@ -1009,7 +1011,7 @@ func (portal *Portal) handleHistory(user *User, messages []skype.Resource) {
} }
t, _ := time.Parse(time.RFC3339, message.ComposeTime) t, _ := time.Parse(time.RFC3339, message.ComposeTime)
message.Timestamp = t.Unix() message.Timestamp = t.Unix()
portal.handleMessage(PortalMessage{ portal.Key.JID, user, message, uint64(message.Timestamp)}) portal.handleMessage(PortalMessage{portal.Key.JID, user, message, uint64(message.Timestamp)})
} }
} }
@ -1088,7 +1090,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
var metadata *skypeExt.GroupInfo var metadata *skypeExt.GroupInfo
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
puppet := portal.bridge.GetPuppetByJID(portal.Key.JID+skypeExt.NewUserSuffix) puppet := portal.bridge.GetPuppetByJID(portal.Key.JID + skypeExt.NewUserSuffix)
if portal.bridge.Config.Bridge.PrivateChatPortalMeta { if portal.bridge.Config.Bridge.PrivateChatPortalMeta {
portal.Name = puppet.Displayname portal.Name = puppet.Displayname
portal.AvatarURL = puppet.AvatarURL portal.AvatarURL = puppet.AvatarURL
@ -1288,7 +1290,7 @@ func (portal *Portal) IsStatusBroadcastRoom() bool {
func (portal *Portal) MainIntent() *appservice.IntentAPI { func (portal *Portal) MainIntent() *appservice.IntentAPI {
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
fmt.Println("IsPrivateChat") fmt.Println("IsPrivateChat")
return portal.bridge.GetPuppetByJID(portal.Key.JID+skypeExt.NewUserSuffix).DefaultIntent() return portal.bridge.GetPuppetByJID(portal.Key.JID + skypeExt.NewUserSuffix).DefaultIntent()
} }
fmt.Println("not IsPrivateChat") fmt.Println("not IsPrivateChat")
return portal.bridge.Bot return portal.bridge.Bot
@ -1491,7 +1493,7 @@ func (portal *Portal) HandleTextMessage(source *User, message skype.Resource) {
} }
func (portal *Portal) trySendMessage(intent *appservice.IntentAPI, eventType event.Type, content interface{}, source *User, message skype.Resource) (resp *mautrix.RespSendEvent, err error) { func (portal *Portal) trySendMessage(intent *appservice.IntentAPI, eventType event.Type, content interface{}, source *User, message skype.Resource) (resp *mautrix.RespSendEvent, err error) {
resp, err = portal.sendMessage(intent, eventType, content, message.Timestamp * 1000) resp, err = portal.sendMessage(intent, eventType, content, message.Timestamp*1000)
if err != nil { if err != nil {
portal.log.Errorfln("Failed to handle message %s: %v", message.Id, err) portal.log.Errorfln("Failed to handle message %s: %v", message.Id, err)
if strings.Index(err.Error(), "M_UNKNOWN_TOKEN (HTTP 401)") > -1 { if strings.Index(err.Error(), "M_UNKNOWN_TOKEN (HTTP 401)") > -1 {
@ -1499,7 +1501,7 @@ func (portal *Portal) trySendMessage(intent *appservice.IntentAPI, eventType eve
err, accessToken := source.UpdateAccessToken(puppet) err, accessToken := source.UpdateAccessToken(puppet)
if err == nil && accessToken != "" { if err == nil && accessToken != "" {
intent.AccessToken = accessToken intent.AccessToken = accessToken
resp, err = portal.sendMessage(intent, eventType, content, message.Timestamp * 1000) resp, err = portal.sendMessage(intent, eventType, content, message.Timestamp*1000)
if err != nil { if err != nil {
portal.log.Errorfln("Failed to handle message %s: %v", message.Id, err) portal.log.Errorfln("Failed to handle message %s: %v", message.Id, err)
} }
@ -1511,7 +1513,7 @@ func (portal *Portal) trySendMessage(intent *appservice.IntentAPI, eventType eve
UserID: intent.UserID, UserID: intent.UserID,
}) })
if err == nil { if err == nil {
resp, err = portal.sendMessage(intent, eventType, content, message.Timestamp * 1000) resp, err = portal.sendMessage(intent, eventType, content, message.Timestamp*1000)
} }
} }
} }
@ -1526,14 +1528,14 @@ func (portal *Portal) HandleLocationMessageSkype(source *User, message skype.Res
if intent == nil { if intent == nil {
return return
} }
locationMessage, err:= message.ParseLocation() locationMessage, err := message.ParseLocation()
if err != nil { if err != nil {
portal.log.Errorfln("Failed to parse contact message of %s: %v", message, err) portal.log.Errorfln("Failed to parse contact message of %s: %v", message, err)
return return
} }
latitude, _ := strconv.Atoi(locationMessage.Latitude) latitude, _ := strconv.Atoi(locationMessage.Latitude)
longitude, _:= strconv.Atoi(locationMessage.Longitude) longitude, _ := strconv.Atoi(locationMessage.Longitude)
geo := fmt.Sprintf("geo:%.6f,%.6f", float32(latitude)/1000000, float32(longitude)/1000000) geo := fmt.Sprintf("geo:%.6f,%.6f", float32(latitude)/1000000, float32(longitude)/1000000)
content := &event.MessageEventContent{ content := &event.MessageEventContent{
MsgType: event.MsgText, MsgType: event.MsgText,
@ -1567,7 +1569,7 @@ func (portal *Portal) HandleContactMessageSkype(source *User, message skype.Reso
if intent == nil { if intent == nil {
return return
} }
contactMessage, err:= message.ParseContact() contactMessage, err := message.ParseContact()
if err != nil { if err != nil {
portal.log.Errorfln("Failed to parse contact message of %s: %v", message, err) portal.log.Errorfln("Failed to parse contact message of %s: %v", message, err)
return return
@ -1723,13 +1725,7 @@ func (portal *Portal) HandleMediaMessageSkype(source *User, download func(conn *
} else { } else {
content.URL = uploaded.ContentURI.CUString() content.URL = uploaded.ContentURI.CUString()
} }
// portal.SetReplySkype(content, info)
fmt.Println()
fmt.Println("mediaMessage.UrlThumbnail", mediaMessage.UrlThumbnail)
fmt.Println()
fmt.Printf("%+v", mediaMessage)
fmt.Println()
thumbnail, err = skype.Download(mediaMessage.UrlThumbnail, source.Conn.Conn, 0) thumbnail, err = skype.Download(mediaMessage.UrlThumbnail, source.Conn.Conn, 0)
if err != nil { if err != nil {
portal.log.Errorfln("Failed to download thumbnail for %s: %v", err) portal.log.Errorfln("Failed to download thumbnail for %s: %v", err)
@ -1756,14 +1752,6 @@ func (portal *Portal) HandleMediaMessageSkype(source *User, download func(conn *
Height: thumbnailCfg.Height, Height: thumbnailCfg.Height,
MimeType: thumbnailMime, MimeType: thumbnailMime,
} }
fmt.Println("content.Info")
fmt.Printf("%+v", content)
fmt.Println()
fmt.Printf("%+v", *content.Info.ThumbnailInfo)
fmt.Println()
fmt.Println()
fmt.Printf("%+v", content.Info.ThumbnailInfo)
fmt.Println()
} }
} }
@ -1948,7 +1936,7 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
clientMessageId := currentTimeNanoStr + fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000)) clientMessageId := currentTimeNanoStr + fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000))
info := &skype.SendMessage{ info := &skype.SendMessage{
ClientMessageId: clientMessageId, ClientMessageId: clientMessageId,
Jid: portal.Key.JID,//receiver id(conversation id) Jid: portal.Key.JID, //receiver id(conversation id)
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
} }
@ -1960,7 +1948,7 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
a = strings.Replace(a, "8:", "", 1) a = strings.Replace(a, "8:", "", 1)
tsMs := strconv.FormatInt(time.Now().UnixNano()/1e6, 10) tsMs := strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
r := []rune(tsMs) r := []rune(tsMs)
ts := string(r[:len(r) - 3]) ts := string(r[:len(r)-3])
msg := portal.bridge.DB.Message.GetByMXID(content.RelatesTo.EventID) msg := portal.bridge.DB.Message.GetByMXID(content.RelatesTo.EventID)
if msg != nil && len(msg.JID) > 0 { if msg != nil && len(msg.JID) > 0 {
info.SkypeEditedId = msg.JID info.SkypeEditedId = msg.JID
@ -2020,7 +2008,7 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
conversation := msg.Chat.Receiver conversation := msg.Chat.Receiver
cuid := msg.JID cuid := msg.JID
r := []rune(messageId) r := []rune(messageId)
timestamp := string(r[:len(r) - 3]) timestamp := string(r[:len(r)-3])
quoteMessage := msg.Content quoteMessage := msg.Content
puppet := sender.bridge.GetPuppetByJID(msg.Sender) puppet := sender.bridge.GetPuppetByJID(msg.Sender)
@ -2056,11 +2044,10 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
if evt.Type == event.EventSticker { if evt.Type == event.EventSticker {
content.MsgType = event.MsgImage content.MsgType = event.MsgImage
} }
portal.log.Debugln("convertMatrixMessage content.MsgType: ", content.MsgType)
fmt.Println("convertMatrixMessage content.MsgType: ", content.MsgType) portal.log.Debugln("convertMatrixMessage content.Body: ", content.Body)
fmt.Println("convertMatrixMessage content.Body: ", content.Body) portal.log.Debugln("convertMatrixMessage content.NewBody: ", content.NewContent)
fmt.Println("convertMatrixMessage content.NewBody: ", content.NewContent) portal.log.Debugln("convertMatrixMessage content.FormattedBody: ", content.FormattedBody)
fmt.Println("convertMatrixMessage content.FormattedBody: ", content.FormattedBody)
info.Type = string(content.MsgType) info.Type = string(content.MsgType)
switch content.MsgType { switch content.MsgType {
case event.MsgText, event.MsgEmote, event.MsgNotice: case event.MsgText, event.MsgEmote, event.MsgNotice:
@ -2106,15 +2093,15 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
if len(content.FormattedBody) > 0 { if len(content.FormattedBody) > 0 {
info.SendTextMessage = &skype.SendTextMessage{ info.SendTextMessage = &skype.SendTextMessage{
Content : content.FormattedBody, Content: content.FormattedBody,
} }
} else { } else {
info.SendTextMessage = &skype.SendTextMessage{ info.SendTextMessage = &skype.SendTextMessage{
Content : content.Body, Content: content.Body,
} }
} }
case event.MsgImage: case event.MsgImage:
caption, fileSize , data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID) caption, fileSize, data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID)
//if media == nil { //if media == nil {
// return nil, sender, content // return nil, sender, content
//} //}
@ -2127,7 +2114,7 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
Duration: 0, Duration: 0,
} }
case event.MsgVideo: case event.MsgVideo:
_, fileSize , data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID) _, fileSize, data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID)
duration := uint32(content.GetInfo().Duration) duration := uint32(content.GetInfo().Duration)
info.SendMediaMessage = &skype.SendMediaMessage{ info.SendMediaMessage = &skype.SendMediaMessage{
FileName: content.Body, FileName: content.Body,
@ -2137,7 +2124,7 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
Duration: int(duration), Duration: int(duration),
} }
case event.MsgAudio: case event.MsgAudio:
_, fileSize , data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID) _, fileSize, data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID)
duration := uint32(content.GetInfo().Duration) duration := uint32(content.GetInfo().Duration)
info.SendMediaMessage = &skype.SendMediaMessage{ info.SendMediaMessage = &skype.SendMediaMessage{
FileName: content.Body, FileName: content.Body,
@ -2147,7 +2134,7 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
Duration: int(duration), Duration: int(duration),
} }
case event.MsgFile: case event.MsgFile:
_, fileSize , data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID) _, fileSize, data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID)
info.SendMediaMessage = &skype.SendMediaMessage{ info.SendMediaMessage = &skype.SendMediaMessage{
FileName: content.Body, FileName: content.Body,
FileType: content.GetInfo().MimeType, FileType: content.GetInfo().MimeType,
@ -2197,11 +2184,10 @@ func (portal *Portal) sendDeliveryReceipt(eventID id.EventID) {
var timeout = errors.New("message sending timed out") var timeout = errors.New("message sending timed out")
func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
fmt.Println("portal HandleMatrixMessage sender.JID: ", sender.JID) portal.log.Debugln("HandleMatrixMessage sender.JID: ", sender.JID)
fmt.Println("portal HandleMatrixMessage portal.Key.Receiver: ", portal.Key.Receiver) portal.log.Debugln("HandleMatrixMessage portal.Key.Receiver: ", portal.Key.Receiver)
fmt.Println("portal HandleMatrixMessage portal.Key.JID: ", portal.Key.JID) portal.log.Debugln("HandleMatrixMessage portal.Key.JID: ", portal.Key.JID)
if !portal.HasRelaybot() && ( if !portal.HasRelaybot() && ((portal.IsPrivateChat() && sender.JID != portal.Key.Receiver) ||
(portal.IsPrivateChat() && sender.JID != portal.Key.Receiver) ||
portal.sendMatrixConnectionError(sender, evt.ID)) { portal.sendMatrixConnectionError(sender, evt.ID)) {
return return
} }
@ -2222,7 +2208,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
fmt.Println("portal HandleMatrixMessage start markHandledSkype: ") fmt.Println("portal HandleMatrixMessage start markHandledSkype: ")
portal.markHandledSkype(sender, &skype.Resource{ portal.markHandledSkype(sender, &skype.Resource{
ClientMessageId: info.ClientMessageId, ClientMessageId: info.ClientMessageId,
Jid: portal.Key.JID,//receiver id(conversation id) Jid: portal.Key.JID, //receiver id(conversation id)
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
Content: content, Content: content,
}, evt.ID) }, evt.ID)
@ -2450,7 +2436,7 @@ func (portal *Portal) HandleMatrixLeave(sender *User) {
} }
func (portal *Portal) HandleMatrixKick(sender *User, evt *event.Event) { func (portal *Portal) HandleMatrixKick(sender *User, evt *event.Event) {
jid, _:= portal.bridge.ParsePuppetMXID(id.UserID(evt.GetStateKey())) jid, _ := portal.bridge.ParsePuppetMXID(id.UserID(evt.GetStateKey()))
puppet := portal.bridge.GetPuppetByJID(jid) puppet := portal.bridge.GetPuppetByJID(jid)
if puppet != nil { if puppet != nil {
jid = strings.Replace(jid, skypeExt.NewUserSuffix, "", 1) jid = strings.Replace(jid, skypeExt.NewUserSuffix, "", 1)
@ -2463,7 +2449,7 @@ func (portal *Portal) HandleMatrixKick(sender *User, evt *event.Event) {
} }
func (portal *Portal) HandleMatrixInvite(sender *User, evt *event.Event) { func (portal *Portal) HandleMatrixInvite(sender *User, evt *event.Event) {
jid, _:= portal.bridge.ParsePuppetMXID(id.UserID(evt.GetStateKey())) jid, _ := portal.bridge.ParsePuppetMXID(id.UserID(evt.GetStateKey()))
puppet := portal.bridge.GetPuppetByJID(jid) puppet := portal.bridge.GetPuppetByJID(jid)
if puppet != nil { if puppet != nil {
jid = strings.Replace(jid, skypeExt.NewUserSuffix, "", 1) jid = strings.Replace(jid, skypeExt.NewUserSuffix, "", 1)