Ejemplo n.º 1
0
func (t *textSecure) Activate(activate bool) (err error) {
	err = t.setupClient()

	if err != nil {
		return
	}

	if activate {
		go func() {
			status.Log(syslog.LOG_NOTICE, "starting TextSecure message listener for %s", t.number)
			err = textsecure.StartListening()

			if err != nil {
				status.Log(syslog.LOG_ERR, "failed to start TextSecure message listener: %v", err)
			}
		}()
	} else {
		status.Log(syslog.LOG_NOTICE, "stopping TextSecure message listener for %s", t.number)
		err = textsecure.StopListening()

		if err != nil {
			status.Log(syslog.LOG_ERR, "failed to stop TextSecure message listener: %v", err)
		}
	}

	return
}
Ejemplo n.º 2
0
// creates a curses based TUI for the textsecure library
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal("Error initializing curses:", err)
	}
	defer gc.End()
	configCurses(stdscr)

	client := &ts.Client{
		GetConfig:           getConfig,
		GetLocalContacts:    getLocalContacts,
		GetVerificationCode: getVerificationCode,
		GetStoragePassword:  passphraseUnlock,
		MessageHandler:      recieveMessage,
		RegistrationDone:    registrationDone,
	}
	err = ts.Setup(client)
	if err != nil {
		log.Fatal("Could not initialize textsecure library", err)
	}

	db = setupDatabase()

	contacts, err := ts.GetRegisteredContacts()
	if err != nil {
		log.Fatal("Could not get contacts: %s\n", err)
	}

	telToName = make(map[string]string)
	for _, c := range contacts {
		telToName[c.Tel] = c.Name
	}
	nameToTel = make(map[string]string)
	for _, c := range contacts {
		nameToTel[c.Name] = c.Tel
	}

	contactsWin, messageWinBorder, msgWin, inputBorderWin, inputWin := createMainWindows(stdscr)
	menu_items, contactMenu, contactsMenuWin := makeContactsMenu(contacts, contactsWin)
	globalInputWin = inputWin
	globalMsgWin = msgWin

	contactsMenuWin.Touch()
	contactMenu.Post()
	contactsWin.Refresh()
	messageWinBorder.Refresh()
	inputBorderWin.Refresh()
	msgWin.Refresh()
	msgWinSize_y, msgWinSize_x = msgWin.MaxYX()

	currentContact = getTel(contactMenu.Current(nil).Name())
	changeContact(contactsMenuWin, contactMenu)
	inputWin.Move(0, 0)
	go ts.StartListening()
	inputHandler(inputWin, stdscr, contactsMenuWin, contactMenu, msgWin)
	cleanup(menu_items, contactMenu)
}
Ejemplo n.º 3
0
func runBackend() {
	client := &textsecure.Client{
		GetConfig:           getConfig,
		GetPhoneNumber:      getPhoneNumber,
		GetVerificationCode: getVerificationCode,
		GetStoragePassword:  getStoragePassword,
		MessageHandler:      messageHandler,
		ReceiptHandler:      receiptHandler,
		RegistrationDone:    registrationDone,
	}

	if isPhone {
		client.GetLocalContacts = getAddressBookContactsFromContentHub
	} else {
		client.GetLocalContacts = getDesktopContacts
	}

	err := textsecure.Setup(client)
	if _, ok := err.(*strconv.NumError); ok {
		showError(fmt.Errorf("Switching to unencrypted session store, removing %s\nThis will reset your sessions and reregister your phone.\n", storageDir))
		os.RemoveAll(storageDir)
		os.Exit(1)
	}
	if err != nil {
		showError(err)
		return
	}

	api.PhoneNumber = config.Tel

	if exists(contactsFile) {
		api.HasContacts = true
		refreshContacts()
	}

	sendUnsentMessages()

	// Make sure to use names not numbers in session titles
	for _, s := range sessionsModel.sessions {
		s.Name = telToName(s.Tel)
	}

	for {
		if err := textsecure.StartListening(); err != nil {
			log.Println(err)
			time.Sleep(3 * time.Second)
		}
	}
}
Ejemplo n.º 4
0
func main() {
	flag.Parse()
	log.SetFlags(0)
	client := &textsecure.Client{
		GetConfig:           getConfig,
		GetLocalContacts:    getLocalContacts,
		GetVerificationCode: getVerificationCode,
		GetStoragePassword:  getStoragePassword,
		MessageHandler:      messageHandler,
		RegistrationDone:    registrationDone,
	}
	err := textsecure.Setup(client)
	if err != nil {
		log.Fatal(err)
	}

	if !echo {
		contacts, err := textsecure.GetRegisteredContacts()
		if err != nil {
			log.Printf("Could not get contacts: %s\n", err)
		}

		telToName = make(map[string]string)
		for _, c := range contacts {
			telToName[c.Tel] = c.Name
		}

		if newgroup != "" {
			s := strings.Split(newgroup, ":")
			textsecure.NewGroup(s[0], s[1:])
			return
		}
		if leavegroup != "" {
			textsecure.LeaveGroup(leavegroup)
			return
		}
		// If "to" matches a contact name then get its phone number, otherwise assume "to" is a phone number
		for _, c := range contacts {
			if strings.EqualFold(c.Name, to) {
				to = c.Tel
				break
			}
		}

		if to != "" {
			// Terminate the session with the peer
			if endsession {
				textsecure.EndSession(to, "TERMINATE")
				return
			}

			// Send attachment with optional message then exit
			if attachment != "" {
				f, err := os.Open(attachment)
				if err != nil {
					log.Fatal(err)
				}

				_, err = textsecure.SendAttachment(to, message, f)
				if err != nil {
					log.Fatal(err)
				}
				return
			}

			if stress > 0 {
				c := make(chan int, stress)
				for i := 0; i < stress; i++ {
					go func(i int) {
						sendMessage(false, to, fmt.Sprintf("Stress %d\n", i))
						c <- i
					}(i)
				}
				for i := 0; i < stress; i++ {
					<-c
				}
				return
			}

			// Send a message then exit
			if message != "" {
				err := sendMessage(group, to, message)
				if err != nil {
					log.Fatal(err)
				}
				return
			}

			// Enter conversation mode
			go conversationLoop(group)
		}
	}

	err = textsecure.StartListening()
	if err != nil {
		log.Println(err)
	}
}