Compare commits

...

5 Commits

Author SHA1 Message Date
normen
4486b976ad fixes & improvements
- fix background being wiped under text
- receive and store contacts if they're received
2020-11-16 20:05:40 +01:00
normen
bf8f8e77db receive contacts (in case they're sent) 2020-11-16 18:45:48 +01:00
normen
f787a4318c convert image/audio/video/doc messages to text 2020-11-16 17:20:10 +01:00
normen
463ab1421d improve readability 2020-11-16 16:37:21 +01:00
normen
a1897eb9fc improvements
- add /addname command to name senders not in contacts
- add some help info on startup
2020-11-16 15:52:54 +01:00
4 changed files with 120 additions and 29 deletions

View File

@@ -47,10 +47,9 @@ Things that work.
This is a WIP. Heres some things you might expect to work that don't. Plus some other things I should mention.
- Only lists contacts that have been messaged on phone
- Only lists contacts and groups that have been messaged on phone
- Only fetches a few messages for last contacted
- To display names they have to be entered through the `/name TheName` command for each contact
- Doesn't support naming people in groups
- To display names they have to be entered through the `/name` or `/addname` commands for each contact
- No support for images, videos, documents etc
- No incoming message notification / count
- Not configurable at all

136
main.go
View File

@@ -10,14 +10,16 @@ import (
"time"
)
type textHandler struct{}
type waMsg struct {
Wid string
Text string
}
var VERSION string = "v0.4.1"
var sendChannel chan waMsg
var textChannel chan whatsapp.TextMessage
var contactChannel chan whatsapp.Contact
var sndTxt string = ""
var currentReceiver string = ""
@@ -25,6 +27,8 @@ var textView *tview.TextView
var treeView *tview.TreeView
var textInput *tview.InputField
var topBar *tview.TextView
//var infoBar *tview.TextView
var connection *whatsapp.Conn
var msgStore messages.MessageDatabase
@@ -68,7 +72,11 @@ func main() {
topBar = tview.NewTextView()
topBar.SetDynamicColors(true)
topBar.SetText("[::b] WhatsCLI v0.2.1 [-][::d]Help: /name NewName = name current contact | /quit = exit app | /load = reload contacts | <Tab> = switch input/contacts | <Up/Dn> = scroll history")
topBar.SetText("[::b] WhatsCLI " + VERSION + " [-::d]Help: /name NewName | /addname 123456 NewName | /quit | <Tab> = contacts/message | <Up/Dn> = scroll")
//infoBar = tview.NewTextView()
//infoBar.SetDynamicColors(true)
//infoBar.SetText("🔋: ??%")
textView = tview.NewTextView().
SetDynamicColors(true).
@@ -78,6 +86,8 @@ func main() {
app.Draw()
})
fmt.Fprint(textView, "[::b]WhatsCLI "+VERSION+"\n\n[-][-::u]Commands:[-::-]\n/name NewName = name current contact\n/addname number NewName = name by number\n/load = reload contacts\n/quit = exit app\n\n[-::u]Keys:[-::-]\n<Tab> = switch input/contacts\n<Up/Dn> = scroll history")
//textView.SetBorder(true)
textInput = tview.NewInputField()
@@ -118,6 +128,7 @@ func main() {
})
gridLayout.AddItem(topBar, 0, 0, 1, 4, 0, 0, false)
//gridLayout.AddItem(infoBar, 0, 0, 1, 1, 0, 0, false)
gridLayout.AddItem(MakeTree(), 1, 0, 2, 1, 0, 0, false)
gridLayout.AddItem(textView, 1, 1, 1, 3, 0, 0, false)
gridLayout.AddItem(textInput, 2, 1, 1, 3, 0, 0, false)
@@ -148,6 +159,19 @@ func EnterCommand(key tcell.Key) {
app.Stop()
return
}
if strings.Index(sndTxt, "/addname ") == 0 {
//command
parts := strings.Split(sndTxt, " ")
if len(parts) < 3 {
fmt.Fprint(textView, "\nUse /addname 1234567 NewName")
return
}
messages.SetIdName(parts[1]+messages.CONTACTSUFFIX, strings.TrimPrefix(sndTxt, "/addname "+parts[1]+" "))
SetDisplayedContact(currentReceiver)
LoadContacts()
textInput.SetText("")
return
}
if currentReceiver == "" {
fmt.Fprint(textView, "\nNo recipient set")
return
@@ -229,26 +253,6 @@ func SetDisplayedContact(wid string) {
textView.SetText(msgStore.GetMessagesString(wid))
}
// HandleError implements the handler interface for go-whatsapp
func (t textHandler) HandleError(err error) {
// TODO : handle go routine here
fmt.Fprint(textView, "\nerror in textHandler : %v", err)
return
}
// HandleTextMessage implements the text message handler interface for go-whatsapp
func (t textHandler) HandleTextMessage(msg whatsapp.TextMessage) {
textChannel <- msg
if msg.Info.RemoteJid != currentReceiver {
return
}
PrintTextMessage(msg)
}
func PrintTextMessage(msg whatsapp.TextMessage) {
fmt.Fprint(textView, messages.GetTextMessageString(&msg))
}
// StartTextReceiver starts the handler for the text messages received
func StartTextReceiver() error {
var wac = GetConnection()
@@ -260,6 +264,7 @@ func StartTextReceiver() error {
wac.AddHandler(handler)
sendChannel = make(chan waMsg)
textChannel = make(chan whatsapp.TextMessage)
contactChannel = make(chan whatsapp.Contact)
for {
select {
case msg := <-sendChannel:
@@ -268,6 +273,8 @@ func StartTextReceiver() error {
if msgStore.AddTextMessage(rcvd) {
app.QueueUpdateDraw(LoadContacts)
}
case contact := <-contactChannel:
messages.SetIdName(contact.Jid, contact.Name)
}
}
fmt.Fprint(textView, "\n"+"closing the receiver")
@@ -297,3 +304,88 @@ func SendText(wid string, text string) {
//fmt.Fprint(textView, "\nSent msg with ID: %v", msgID)
}
}
type textHandler struct{}
// HandleError implements the handler interface for go-whatsapp
func (t textHandler) HandleError(err error) {
// TODO : handle go routine here
fmt.Fprint(textView, "\nerror in textHandler : %v", err)
return
}
// HandleTextMessage implements the text message handler interface for go-whatsapp
func (t textHandler) HandleTextMessage(msg whatsapp.TextMessage) {
textChannel <- msg
if msg.Info.RemoteJid != currentReceiver {
//fmt.Print("\a")
return
}
PrintTextMessage(msg)
}
func PrintTextMessage(msg whatsapp.TextMessage) {
fmt.Fprint(textView, messages.GetTextMessageString(&msg))
}
func (t textHandler) HandleImageMessage(message whatsapp.ImageMessage) {
msg := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: message.Info.RemoteJid,
SenderJid: message.Info.SenderJid,
FromMe: message.Info.FromMe,
Timestamp: message.Info.Timestamp,
},
Text: "[IMAGE] " + message.Caption,
}
t.HandleTextMessage(msg)
}
func (t textHandler) HandleDocumentMessage(message whatsapp.DocumentMessage) {
msg := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: message.Info.RemoteJid,
SenderJid: message.Info.SenderJid,
FromMe: message.Info.FromMe,
Timestamp: message.Info.Timestamp,
},
Text: "[DOCUMENT] " + message.Title,
}
t.HandleTextMessage(msg)
}
func (t textHandler) HandleVideoMessage(message whatsapp.VideoMessage) {
msg := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: message.Info.RemoteJid,
SenderJid: message.Info.SenderJid,
FromMe: message.Info.FromMe,
Timestamp: message.Info.Timestamp,
},
Text: "[VIDEO] " + message.Caption,
}
t.HandleTextMessage(msg)
}
func (t textHandler) HandleAudioMessage(message whatsapp.AudioMessage) {
msg := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: message.Info.RemoteJid,
SenderJid: message.Info.SenderJid,
FromMe: message.Info.FromMe,
Timestamp: message.Info.Timestamp,
},
Text: "[AUDIO]",
}
t.HandleTextMessage(msg)
}
func (t textHandler) HandleNewContact(contact whatsapp.Contact) {
contactChannel <- contact
}
//func (t textHandler) HandleBatteryMessage(msg whatsapp.BatteryMessage) {
// app.QueueUpdate(func() {
// infoBar.SetText("🔋: " + string(msg.Percentage) + "%")
// })
//}

View File

@@ -71,14 +71,14 @@ func GetTextMessageString(msg *whatsapp.TextMessage) string {
text := tview.Escape((*msg).Text)
tim := time.Unix(int64((*msg).Info.Timestamp), 0)
if (*msg).Info.FromMe { //msg from me
out += "\n[-](" + tim.Format("01-02-06 15:04:05") + ") [blue]Me: " + text
out += "\n[-::d](" + tim.Format("01-02-06 15:04:05") + ") [blue::b]Me: [-::-]" + text
} else if strings.Contains((*msg).Info.RemoteJid, GROUPSUFFIX) { // group msg
//(*msg).Info.SenderJid
userId := (*msg).Info.SenderJid
//userId := strings.Split(string((*msg).Info.RemoteJid), "-")[0] + CONTACTSUFFIX
out += "\n[-](" + tim.Format("01-02-06 15:04:05") + ") [green]" + GetIdName(userId) + ": " + text
out += "\n[-::d](" + tim.Format("01-02-06 15:04:05") + ") [green::b]" + GetIdName(userId) + ": [-::-]" + text
} else { // message from others
out += "\n[-](" + tim.Format("01-02-06 15:04:05") + ") [green]" + GetIdName((*msg).Info.RemoteJid) + ": " + text
out += "\n[-::d](" + tim.Format("01-02-06 15:04:05") + ") [green::b]" + GetIdName((*msg).Info.RemoteJid) + ": [-::-]" + text
}
return out
}

View File

@@ -68,7 +68,7 @@ func LoginWithConnection(wac *whatsapp.Conn) error {
return fmt.Errorf("error saving session: %v\n", err)
}
//<-time.After(3 * time.Second)
fmt.Fprint(textView, "\nlogin successful")
//fmt.Fprint(textView, "\nlogin successful")
return nil
}