@@ -2,6 +2,7 @@ package messages
2
2
3
3
import (
4
4
"encoding/gob"
5
+ "encoding/json"
5
6
"errors"
6
7
"fmt"
7
8
"io/ioutil"
@@ -544,8 +545,9 @@ func (sm *SessionManager) execCommand(command Command) {
544
545
}
545
546
case "leave" :
546
547
groupId := sm .currentReceiver
547
- if checkParam (command .Params , 1 ) {
548
- groupId = command .Params [0 ]
548
+ if strings .Index (groupId , GROUPSUFFIX ) < 0 {
549
+ sm .uiHandler .PrintText ("not a group" )
550
+ return
549
551
}
550
552
wac := sm .getConnection ()
551
553
var err error
@@ -554,6 +556,99 @@ func (sm *SessionManager) execCommand(command Command) {
554
556
sm .uiHandler .PrintText ("left group " + groupId )
555
557
}
556
558
sm .uiHandler .PrintError (err )
559
+ case "create" :
560
+ if ! checkParam (command .Params , 1 ) {
561
+ sm .printCommandUsage ("create" , "[user-id[] [user-id[] New Group Subject" )
562
+ sm .printCommandUsage ("create" , "New Group Subject" )
563
+ return
564
+ }
565
+ // first params are users if ending in CONTACTSUFFIX, rest is name
566
+ users := []string {}
567
+ idx := 0
568
+ size := len (command .Params )
569
+ for idx = 0 ; idx < size && strings .Index (command .Params [idx ], CONTACTSUFFIX ) > 0 ; idx ++ {
570
+ users = append (users , command .Params [idx ])
571
+ }
572
+ name := ""
573
+ if len (command .Params ) > idx {
574
+ name = strings .Join (command .Params [idx :], " " )
575
+ }
576
+ wac := sm .getConnection ()
577
+ var err error
578
+ var groupId <- chan string
579
+ groupId , err = wac .CreateGroup (name , users )
580
+ if err == nil {
581
+ sm .uiHandler .PrintText ("creating new group " + name )
582
+ resultInfo := <- groupId
583
+ //{"status":200,"gid":"[email protected] ","participants":[{"[email protected] ":{"code":"200"}},{"[email protected] ":{"code": "200"}}]}
584
+ var result map [string ]interface {}
585
+ json .Unmarshal ([]byte (resultInfo ), & result )
586
+ newChatId := result ["gid" ].(string )
587
+ sm .uiHandler .PrintText ("got new Id " + newChatId )
588
+ newChat := Chat {}
589
+ newChat .Id = newChatId
590
+ newChat .Name = name
591
+ newChat .IsGroup = true
592
+ sm .db .chats [newChatId ] = newChat
593
+ sm .uiHandler .SetChats (sm .db .GetChatIds ())
594
+ }
595
+ sm .uiHandler .PrintError (err )
596
+ case "add" :
597
+ groupId := sm .currentReceiver
598
+ if strings .Index (groupId , GROUPSUFFIX ) < 0 {
599
+ sm .uiHandler .PrintText ("not a group" )
600
+ return
601
+ }
602
+ if ! checkParam (command .Params , 1 ) {
603
+ sm .printCommandUsage ("add" , "[user-id[]" )
604
+ return
605
+ }
606
+ wac := sm .getConnection ()
607
+ var err error
608
+ _ , err = wac .AddMember (groupId , command .Params )
609
+ if err == nil {
610
+ sm .uiHandler .PrintText ("added new members for " + groupId )
611
+ }
612
+ sm .uiHandler .PrintError (err )
613
+ case "admin" :
614
+ groupId := sm .currentReceiver
615
+ if strings .Index (groupId , GROUPSUFFIX ) < 0 {
616
+ sm .uiHandler .PrintText ("not a group" )
617
+ return
618
+ }
619
+ if ! checkParam (command .Params , 1 ) {
620
+ sm .printCommandUsage ("admin" , "[user-id[]" )
621
+ return
622
+ }
623
+ wac := sm .getConnection ()
624
+ var err error
625
+ _ , err = wac .SetAdmin (groupId , command .Params )
626
+ if err == nil {
627
+ sm .uiHandler .PrintText ("added admin for " + groupId )
628
+ }
629
+ sm .uiHandler .PrintError (err )
630
+ case "subject" :
631
+ groupId := sm .currentReceiver
632
+ if strings .Index (groupId , GROUPSUFFIX ) < 0 {
633
+ sm .uiHandler .PrintText ("not a group" )
634
+ return
635
+ }
636
+ if ! checkParam (command .Params , 1 ) || groupId == "" {
637
+ sm .printCommandUsage ("subject" , "new-subject -> in group chat" )
638
+ return
639
+ }
640
+ name := strings .Join (command .Params , " " )
641
+ wac := sm .getConnection ()
642
+ var err error
643
+ _ , err = wac .UpdateGroupSubject (name , groupId )
644
+ if err == nil {
645
+ sm .uiHandler .PrintText ("updated subject for " + groupId )
646
+ }
647
+ newChat := sm .db .chats [groupId ]
648
+ newChat .Name = name
649
+ sm .db .chats [groupId ] = newChat
650
+ sm .uiHandler .SetChats (sm .db .GetChatIds ())
651
+ sm .uiHandler .PrintError (err )
557
652
case "colorlist" :
558
653
out := ""
559
654
for idx , _ := range tcell .ColorNames {
0 commit comments