From 38a4e494a419b0612fcaf60bd71b2808016d53a5 Mon Sep 17 00:00:00 2001 From: katianderic Date: Sun, 16 Feb 2020 22:34:33 -0800 Subject: [PATCH] make sms work via invitations too --- xwords4/linux/cursesboard.c | 47 ++++++++++++++++-------- xwords4/linux/cursesmain.c | 36 ++++++++---------- xwords4/linux/linuxmain.c | 3 +- xwords4/linux/linuxsms.c | 22 +++++------ xwords4/linux/main.h | 2 +- xwords4/linux/scripts/discon_ok2.py | 27 ++++++++------ xwords4/linux/scripts/show_sms_inplay.sh | 3 ++ 7 files changed, 77 insertions(+), 63 deletions(-) create mode 100755 xwords4/linux/scripts/show_sms_inplay.sh diff --git a/xwords4/linux/cursesboard.c b/xwords4/linux/cursesboard.c index f8c6272e3..e33d244f8 100644 --- a/xwords4/linux/cursesboard.c +++ b/xwords4/linux/cursesboard.c @@ -158,7 +158,9 @@ static gint inviteIdle( gpointer data ) { CursesBoardGlobals* bGlobals = (CursesBoardGlobals*)data; - if ( !!bGlobals->cGlobals.params->connInfo.relay.inviteeRelayIDs ) { + LaunchParams* params = bGlobals->cGlobals.params; + if ( !!params->connInfo.relay.inviteeRelayIDs + || !!params->connInfo.sms.inviteePhones ) { handleInvite( bGlobals, 0 ); } return FALSE; @@ -1177,6 +1179,31 @@ handleReplace( void* closure, int XP_UNUSED(key) ) return XP_TRUE; } /* handleReplace */ +static bool +inviteList( CommonGlobals* cGlobals, CommsAddrRec* addr, GSList* invitees, + bool useRelay ) +{ + bool haveAddressees = !!invitees; + if ( haveAddressees ) { + LaunchParams* params = cGlobals->params; + for ( int ii = 0; ii < g_slist_length(invitees); ++ii ) { + const XP_U16 nPlayers = 1; + gint forceChannel = ii + 1; + NetLaunchInfo nli = {0}; + nli_init( &nli, cGlobals->gi, addr, nPlayers, forceChannel ); + if ( useRelay ) { + uint64_t inviteeRelayID = (uint64_t)g_slist_nth_data( invitees, ii ); + relaycon_invite( params, (XP_U32)inviteeRelayID, NULL, &nli ); + } else { + const gchar* inviteePhone = (const gchar*)g_slist_nth_data( invitees, ii ); + linux_sms_invite( params, &nli, inviteePhone, + params->connInfo.sms.port ); + } + } + } + return haveAddressees; +} + static bool handleInvite( void* closure, int XP_UNUSED(key) ) { @@ -1199,20 +1226,10 @@ handleInvite( void* closure, int XP_UNUSED(key) ) /* Invite first based on an invitee provided. Otherwise, fall back to doing a send-to-self. Let the recipient code reject a duplicate if the user so desires. */ - } else if ( !!params->connInfo.sms.inviteePhone ) { - /* These should both be settable/derivable */ - linux_sms_invite( params, &nli, params->connInfo.sms.inviteePhone, - params->connInfo.sms.port ); - } else if ( !!params->connInfo.relay.inviteeRelayIDs ) { - GSList* inviteeRelayIDs = params->connInfo.relay.inviteeRelayIDs; - for ( int ii = 0; ii < g_slist_length(inviteeRelayIDs); ++ii ) { - XP_U16 nPlayers = 1; - gint forceChannel = ii + 1; - NetLaunchInfo nli = {0}; - nli_init( &nli, cGlobals->gi, &addr, nPlayers, forceChannel ); - uint64_t inviteeRelayID = (uint64_t)g_slist_nth_data(inviteeRelayIDs, ii); - relaycon_invite( params, (XP_U32)inviteeRelayID, NULL, &nli ); - } + } else if ( inviteList( cGlobals, &addr, params->connInfo.sms.inviteePhones, false ) ) { + /* do nothing */ + } else if ( inviteList( cGlobals, &addr, params->connInfo.relay.inviteeRelayIDs, true ) ) { + /* do nothing */ /* Try sending to self, using the phone number or relayID of this device */ } else if ( addr_hasType( &addr, COMMS_CONN_SMS ) ) { linux_sms_invite( params, &nli, addr.u.sms.phone, addr.u.sms.port ); diff --git a/xwords4/linux/cursesmain.c b/xwords4/linux/cursesmain.c index f8d7096d7..62a083cf2 100644 --- a/xwords4/linux/cursesmain.c +++ b/xwords4/linux/cursesmain.c @@ -1153,10 +1153,9 @@ onJoined( void* closure, const XP_UCHAR* connname, XWHostID hid ) #endif static void -relayInviteReceivedCurses( void* closure, NetLaunchInfo* invite ) +inviteReceivedCurses( CursesAppGlobals* aGlobals, const NetLaunchInfo* invite, + const CommsAddrRec* returnAddr ) { - CursesAppGlobals* aGlobals = (CursesAppGlobals*)closure; - sqlite3_int64 rowids[1]; int nRowIDs = VSIZE(rowids); getRowsForGameID( aGlobals->cag.params->pDb, invite->gameID, rowids, &nRowIDs ); @@ -1167,15 +1166,23 @@ relayInviteReceivedCurses( void* closure, NetLaunchInfo* invite ) doIt = 0 == cursesask( aGlobals->mainWin, question, VSIZE(buttons), buttons ); } if ( doIt ) { - CommsAddrRec returnAddr = {0}; - nli_makeAddrRec( invite, &returnAddr ); - cb_dims dims; figureDims( aGlobals, &dims ); - cb_newFor( aGlobals->cbState, invite, &returnAddr, &dims ); + cb_newFor( aGlobals->cbState, invite, returnAddr, &dims ); + } else { + XP_LOGFF( "%s", "Not accepting duplicate invitation" ); } } +static void +relayInviteReceivedCurses( void* closure, NetLaunchInfo* invite ) +{ + CursesAppGlobals* aGlobals = (CursesAppGlobals*)closure; + CommsAddrRec addr = {0}; + nli_makeAddrRec( invite, &addr ); + inviteReceivedCurses( aGlobals, invite, &addr ); +} + static void cursesGotBuf( void* closure, const CommsAddrRec* addr, const XP_U8* buf, XP_U16 len ) @@ -1211,20 +1218,7 @@ smsInviteReceivedCurses( void* closure, const NetLaunchInfo* nli, const CommsAddrRec* returnAddr ) { CursesAppGlobals* aGlobals = (CursesAppGlobals*)closure; - /* LaunchParams* params = aGlobals->cag.params; */ - /* CurGameInfo gi = {0}; */ - /* gi_copy( MPPARM(params->mpool) &gi, ¶ms->pgi ); */ - - /* gi_setNPlayers( &gi, invite->nPlayersT, invite->nPlayersH ); */ - /* gi.gameID = invite->gameID; */ - /* gi.dictLang = invite->lang; */ - /* gi.forceChannel = invite->forceChannel; */ - /* gi.serverRole = SERVER_ISCLIENT; /\* recipient of invitation is client *\/ */ - /* replaceStringIfDifferent( params->mpool, &gi.dictName, invite->dict ); */ - - cb_dims dims; - figureDims( aGlobals, &dims ); - cb_newFor( aGlobals->cbState, nli, returnAddr, &dims ); + inviteReceivedCurses( aGlobals, nli, returnAddr ); } static void diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index b03c748c5..253dabf22 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -2722,7 +2722,8 @@ main( int argc, char** argv ) addr_addType( &mainParams.addr, COMMS_CONN_SMS ); break; case CMD_INVITEE_SMSNUMBER: - mainParams.connInfo.sms.inviteePhone = optarg; + mainParams.connInfo.sms.inviteePhones = + g_slist_append( mainParams.connInfo.sms.inviteePhones, optarg ); addr_addType( &mainParams.addr, COMMS_CONN_SMS ); break; case CMD_SMSPORT: diff --git a/xwords4/linux/linuxsms.c b/xwords4/linux/linuxsms.c index c1026c696..dda037f62 100644 --- a/xwords4/linux/linuxsms.c +++ b/xwords4/linux/linuxsms.c @@ -115,15 +115,12 @@ write_fake_sms( LaunchParams* params, const void* buf, XP_U16 buflen, if ( skipWrite ) { nSent = buflen; - XP_LOGF( "%s(): dropping sms msg of len %d to phone %s", __func__, - nSent, phone ); + XP_LOGFF( "dropping sms msg of len %d to phone %s", nSent, phone ); } else { LinSMSData* storage = getStorage( params ); - XP_LOGF( "%s(phone=%s, port=%d, len=%d)", __func__, phone, - port, buflen ); + XP_LOGFF( "(phone=%s, port=%d, len=%d)", phone, port, buflen ); XP_ASSERT( !!storage ); - char path[256]; lock_queue( storage ); @@ -131,6 +128,7 @@ write_fake_sms( LaunchParams* params, const void* buf, XP_U16 buflen, gchar* str64 = g_base64_encode( buf, buflen ); #endif + char path[256]; formatQueuePath( phone, port, path, sizeof(path) ); /* Random-number-based name is fine, as we pick based on age. */ @@ -143,7 +141,7 @@ write_fake_sms( LaunchParams* params, const void* buf, XP_U16 buflen, XP_U16 smslen = sizeof(sms); binToSms( sms, &smslen, buf, buflen ); XP_ASSERT( smslen == strlen(sms) ); - XP_LOGF( "%s: writing msg to %s", __func__, path ); + XP_LOGFF( "writing msg to %s", path ); #ifdef DEBUG XP_ASSERT( !strcmp( str64, sms ) ); @@ -202,7 +200,7 @@ decodeAndDelete( LinSMSData* storage, const gchar* name, *strstr(eol, "\n" ) = '\0'; XP_U16 inlen = strlen(eol); /* skip \n */ - XP_LOGF( "%s(): decoding message from file %s", __func__, name ); + XP_LOGFF( "decoding message from file %s", name ); XP_U8 out[inlen]; XP_U16 outlen = sizeof(out); XP_Bool valid = smsToBin( out, &outlen, eol, inlen ); @@ -212,8 +210,7 @@ decodeAndDelete( LinSMSData* storage, const gchar* name, nRead = outlen; addr_setType( addr, COMMS_CONN_SMS ); XP_STRNCPY( addr->u.sms.phone, phone, sizeof(addr->u.sms.phone) ); - XP_LOGF( "%s: message came from phone: %s, port: %d", __func__, - phone, port ); + XP_LOGFF( " message came from phone: %s, port: %d", phone, port ); addr->u.sms.port = port; } } else { @@ -272,7 +269,7 @@ linux_sms_init( LaunchParams* params, const gchar* myPhone, XP_U16 myPort, storage->protoState = smsproto_init( MPPARM(params->mpool) params->dutil ); formatQueuePath( myPhone, myPort, storage->myQueue, sizeof(storage->myQueue) ); - XP_LOGF( "%s: my queue: %s", __func__, storage->myQueue ); + XP_LOGFF( " my queue: %s", storage->myQueue ); storage->myPort = params->connInfo.sms.port; (void)g_mkdir_with_parents( storage->myQueue, 0777 ); @@ -400,8 +397,7 @@ check_for_files_once( gpointer data ) snprintf( fullPath, sizeof(fullPath), "%s/%s", storage->myQueue, name ); int err = stat( fullPath, &statbuf ); if ( err != 0 ) { - XP_LOGF( "%s(); %d from stat (error: %s)", __func__, - err, strerror(errno) ); + XP_LOGF( "%d from stat (error: %s)", err, strerror(errno) ); XP_ASSERT( 0 ); } else { XP_Bool replace = !oldestFile[0]; /* always replace empty/unset :-) */ @@ -416,7 +412,7 @@ check_for_files_once( gpointer data ) if ( replace ) { oldestModTime = statbuf.st_mtim; if ( !!oldestFile[0] ) { - XP_LOGF( "%s(): replacing %s with older %s", __func__, oldestFile, name ); + XP_LOGFF( "replacing %s with older %s", oldestFile, name ); } snprintf( oldestFile, sizeof(oldestFile), "%s", name ); } diff --git a/xwords4/linux/main.h b/xwords4/linux/main.h index c127c1bff..b0635121e 100644 --- a/xwords4/linux/main.h +++ b/xwords4/linux/main.h @@ -160,7 +160,7 @@ typedef struct LaunchParams { #ifdef XWFEATURE_SMS struct { const char* myPhone; - const char* inviteePhone; + GSList* inviteePhones; int port; } sms; #endif diff --git a/xwords4/linux/scripts/discon_ok2.py b/xwords4/linux/scripts/discon_ok2.py index 817f397cc..cdf2e6064 100755 --- a/xwords4/linux/scripts/discon_ok2.py +++ b/xwords4/linux/scripts/discon_ok2.py @@ -253,7 +253,7 @@ class Device(): self.relaySeed = int(match.group(1)) self.relayID = match.group(2) - if not self.inviteeDevID: + if self.args.ADD_RELAY and not self.inviteeDevID: match = Device.sDevIDPat.match(line) if match: self.inviteeDevID = int(match.group(1), 16) @@ -292,16 +292,17 @@ class Device(): # the same order so channels will be assigned consistently. So # keep them in an array as they're encountered, and use in # that order - if not self.usePublic and self.order == 1 and self.inviteeDevID and not self.connected: - for peer in self.peers: - if peer.inviteeDevID and not peer == self: - if not peer.inviteeDevID in self.inviteeDevIDs: - self.inviteeDevIDs.append(peer.inviteeDevID) + if self.args.ADD_RELAY: + if not self.usePublic and self.order == 1 and self.inviteeDevID and not self.connected: + for peer in self.peers: + if peer.inviteeDevID and not peer == self: + if not peer.inviteeDevID in self.inviteeDevIDs: + self.inviteeDevIDs.append(peer.inviteeDevID) - if self.inviteeDevIDs: - args += [ '--force-invite' ] - for inviteeDevID in self.inviteeDevIDs: - args += ['--invitee-relayid', str(inviteeDevID)] + if self.inviteeDevIDs: + args += [ '--force-invite' ] + for inviteeDevID in self.inviteeDevIDs: + args += ['--invitee-relayid', str(inviteeDevID)] self.proc = subprocess.Popen(args, stdout = subprocess.DEVNULL, stderr = subprocess.PIPE, universal_newlines = True) @@ -464,8 +465,10 @@ def build_cmds(args): PARAMS += [ '--sms-number', PHONE_BASE + str(DEV - 1) ] if args.SMS_FAIL_PCT > 0: PARAMS += [ '--sms-fail-pct', args.SMS_FAIL_PCT ] - if DEV > 1: - PARAMS += [ '--server-sms-number', PHONE_BASE + '0' ] + if DEV == 1: + PARAMS += [ '--force-invite' ] + for dev in range(2, NDEVS + 1): + PARAMS += [ '--invitee-sms-number', PHONE_BASE + str(dev - 1) ] if args.UNDO_PCT > 0: PARAMS += ['--undo-pct', args.UNDO_PCT] diff --git a/xwords4/linux/scripts/show_sms_inplay.sh b/xwords4/linux/scripts/show_sms_inplay.sh new file mode 100755 index 000000000..2ea70f56b --- /dev/null +++ b/xwords4/linux/scripts/show_sms_inplay.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +ls -l /tmp/xw_sms