@ -10,14 +10,18 @@ import (
"bytes"
"golang.org/x/crypto/ssh/terminal"
"log"
"os"
"os/exec"
"os/user"
"path"
"syscall"
)
var app app2 . Application
var app * app2 . Application
var suggestions = [ ] prompt . Suggest {
{ Text : "new-profile" , Description : "create a new profile "} ,
{ Text : "new-profile" , Description : "create a new profile in ~/.cwtch/$USERNAME.json "} ,
{ Text : "load-profile" , Description : "load a new profile" } ,
{ Text : "quit" , Description : "quit cwtch" } ,
{ Text : "info" , Description : "show user info" } ,
@ -37,7 +41,7 @@ var suggestions = []prompt.Suggest{
}
var usages = map [ string ] string {
"new-profile" : "new-profile [name] [filename] ",
"new-profile" : "new-profile [name] ",
"load-profile" : "load-profile [filename]" ,
"quit" : "" ,
"servers" : "" ,
@ -163,8 +167,18 @@ func main() {
fmt . Printf ( "%v\n\n" , cwtch )
quit := false
app = app2 . Application { }
profilefile := ""
torPath , err := exec . LookPath ( "tor" )
if err != nil {
log . Fatal ( "tor could not be found on this system. Please install it in the system $PATH" )
}
usr , err := user . Current ( )
if err != nil {
log . Fatalf ( "\nError: could not load current user: %v\n" , err )
}
app , err = app2 . NewApp ( path . Join ( usr . HomeDir , ".cwtch" ) , torPath )
var history [ ] string
for ! quit {
profile := "unset"
@ -181,10 +195,10 @@ func main() {
history = append ( history , text )
switch commands [ 0 ] {
case "quit" :
app . Peer . Save ( profilefile )
app . Peer . Save ( )
quit = true
case "new-profile" :
if len ( commands ) == 3 {
if len ( commands ) == 2 {
fmt . Print ( "** WARNING: PASSWORDS CANNOT BE RECOVERED! **\n" )
password := ""
@ -209,8 +223,7 @@ func main() {
if failcount >= 3 {
fmt . Printf ( "Error creating profile for %v: Your password entries must match!\n" , commands [ 1 ] )
} else {
err := app . NewProfile ( commands [ 1 ] , commands [ 2 ] , password )
profilefile = commands [ 2 ]
err := app . NewProfile ( commands [ 1 ] , password )
if err == nil {
fmt . Printf ( "\nNew profile created for %v\n" , commands [ 1 ] )
} else {
@ -224,10 +237,9 @@ func main() {
if len ( commands ) == 2 {
fmt . Print ( "Enter a password to decrypt the profile: " )
bytePassword , err := terminal . ReadPassword ( int ( syscall . Stdin ) )
err = app . SetProfile ( commands [ 1 ] , string ( bytePassword ) )
err = app . SetProfile ( commands [ 1 ] + ".json" , string ( bytePassword ) )
if err == nil {
fmt . Printf ( "\nLoaded profile for %v\n" , commands [ 1 ] )
profilefile = commands [ 1 ]
} else {
fmt . Printf ( "Error loading profile for %v: %v\n" , commands [ 1 ] , err )
}
@ -288,7 +300,7 @@ func main() {
if err != nil {
fmt . Printf ( "Error: %v\n" , err )
} else {
app . Peer . Save ( profilefile )
app . Peer . Save ( )
group := app . Peer . GetGroup ( groupID )
if group == nil {
fmt . Printf ( "Error: group does not exist\n" )
@ -315,7 +327,7 @@ func main() {
id , _ , err := app . Peer . StartGroup ( commands [ 1 ] )
if err == nil {
fmt . Printf ( "New Group [%v] created for server %v\n" , id , commands [ 1 ] )
app . Peer . Save ( profilefile )
app . Peer . Save ( )
group := app . Peer . GetGroup ( id )
if group == nil {
fmt . Printf ( "Error: group does not exist\n" )
@ -378,7 +390,7 @@ func main() {
fmt . Printf ( "Error reading timeline from group, usage: %s\n" , usages [ "timeline" ] )
}
case "save" :
app . Peer . Save ( profilefile )
app . Peer . Save ( )
case "help" :
for _ , command := range suggestions {
fmt . Printf ( "%-18s%-56s%s\n" , command . Text , command . Description , usages [ command . Text ] )
@ -428,10 +440,8 @@ func main() {
}
}
}
if profilefile != "" {
if app . Peer != nil {
app . Peer . Save ( profilefile )
}
if app . Peer != nil {
app . Peer . Save ( )
}
}