import { APIClient } from '@wireapp/api-client' ; import { ClientType , RegisteredClient } from '@wireapp/api-client/src/client/' ; import { BackendErrorLabel } from '@wireapp/api-client/src/http/' ; import { Account } from '@wireapp/core' ; import { PayloadBundleType } from '@wireapp/core/src/main/conversation/' ; import { FileEngine } from '@wireapp/store-engine-fs' ; import { program as commander } from 'commander' ; import dotenv from 'dotenv' ; import fs from 'fs-extra' ; import os from 'os' ; import path from 'path' ; import type { AxiosError } from 'axios' ; import { MessageBuilder } from '@wireapp/core/src/main/conversation/message/MessageBuilder' ; dotenv . config ( ) ; const { bin , description , version , } = require ( '../package.json' ) ; commander . name ( Object . keys ( bin ) [ 0 ] ) . version ( version ) . description ( description ) . option ( '-e, --email <address>' , 'Your email address' ) . option ( '-p, --password <password>' , 'Your password' ) . option ( '-c, --conversation <conversationId>' , 'The conversation to write in' ) . parse ( process . argv ) ; const { conversation , email , password } = commander . opts ( ) ; const loginData = { clientType : ClientType . PERMANENT , email : email || process . env . WIRE_LOGIN_EMAIL , password : password || process . env . WIRE_LOGIN_PASSWORD , } ; if ( ! loginData . email || ! loginData . password ) { console . error ( 'Email and password both need to be set' ) ; commander . help ( ) ; } const conversationId = conversation || process . env . WIRE_CONVERSATION_ID ; const directory = path . join ( os . homedir ( ) , '.wire-cli' , loginData . email ) ; const storeEngineProvider = async ( storeName ) => { const engine = new FileEngine ( directory ) ; await engine . init ( storeName , { fileExtension : '.json' } ) ; return engine ; } ; const apiClient = new APIClient ( { urls : APIClient . BACKEND . PRODUCTION } ) ; const account = new Account ( apiClient , storeEngineProvider ) ; account . on ( PayloadBundleType . TEXT , textMessage => { console . info ( `Received message from user ID "${ textMessage . from }" in conversation ID "${ textMessage . conversation }": ${ textMessage . content }` ) ; } ) ; ( async ( ) => { try { await account . login ( loginData ) ; await account . listen ( ) ; } catch ( error ) { const data = ( error as AxiosError ) . response ?. data ; const errorLabel = data ?. label ; if ( errorLabel === BackendErrorLabel . TOO_MANY_CLIENTS ) { const clients = await apiClient . client . api . getClients ( ) ; const client = clients [ 0 ] ; await apiClient . client . api . deleteClient ( client . id , loginData . password ) ; await account . logout ( ) ; await fs . remove ( directory ) ; await account . login ( loginData ) ; await account . listen ( ) ; } throw error ; } const { clientId , userId } = apiClient . context ! ; console . info ( `Connected to Wire — User ID "${ userId }" — Client ID "${ clientId }"` ) ; const stdin = process . openStdin ( ) ; stdin . addListener ( 'data' , async ( data ) => { const message = data . toString ( ) . trim ( ) ; if ( account . service ) { const payload = MessageBuilder . createText ( { conversationId , from : userId , text : message } ) . build ( ) ; await account . service . conversation . send ( { payloadBundle : payload } ) ; } } ) ; } ) ( ) . catch ( ( error ) => { console . error ( error . message , error ) ; process . exit ( 1 ) ; } ) ;