public function getMsgsForDst(Node $node) { #print __CLASS__.'->'.__FUNCTION__.': dst '.$node->getIdHexStr()."\n"; $rv = array(); foreach ($this->msgs as $msgId => $msg) { #print __CLASS__.'->'.__FUNCTION__.': '.$msg->getId().', '.$msg->getDstNodeId()."\n"; if ($msg->getDstNodeId() == $node->getIdHexStr()) { $rv[$msgId] = $msg; } } return $rv; }
protected function execute(InputInterface $input, OutputInterface $output) { #$this->executePre($input, $output); #$bytesizeFormatter = new Binary(); #$bytesize = new ByteSize($bytesizeFormatter); $bytesize = new ByteSize(); if ($input->hasOption('name') && $input->getOption('name')) { print PhpChat::NAME; } elseif ($input->hasOption('name_lc') && $input->getOption('name_lc')) { print strtolower(PhpChat::NAME); } elseif ($input->hasOption('version_number') && $input->getOption('version_number')) { print PhpChat::VERSION; } elseif ($input->hasOption('connections') && $input->getOption('connections')) { print 'Live Connections' . PHP_EOL . PHP_EOL; $this->executePre($input, $output); $this->log = new Logger($this->getName()); #$this->log->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); $this->log->pushHandler(new StreamHandler($this->getLogfilePath(), Logger::DEBUG)); $this->initIpcKernelConnection(); $color = new Color(); $startTime = time(); $time = time(); $seconds = 0; $oldClients = array(); $clientsIdMax = 0; $tcols = (int) exec('tput cols'); $tlines = (int) exec('tput lines'); #print 'cols: '.$tcols.PHP_EOL; #print 'lines: '.$tlines.PHP_EOL; $baseLines = 5; print ' Traffic IN: N/A' . PHP_EOL; print ' Traffic OUT: N/A' . PHP_EOL; #print ' Traffic AVG: N/A'.PHP_EOL; print ' Clients: N/A' . PHP_EOL; #sleep(1); #print '---A'.PHP_EOL; print '' . PHP_EOL; #sleep(1); #print '---B'.PHP_EOL; print ' ' . PHP_EOL; #sleep(1); Console::cursorJumpToColumn(1); #sleep(1); Console::cursorUp($baseLines); #sleep(1); while (!$this->getExit()) { #$this->log->debug('run'); if (!$this->ipcKernelConnection->run()) { $this->log->info('Connection to kernel process end unexpected.'); $this->setExit(1); } $update = false; if ($time != time()) { $time = time(); $seconds++; $tcols = (int) exec('tput cols'); $tlines = (int) exec('tput lines'); $update = true; } $update = true; if ($update) { $clientsInfo = $this->ipcKernelConnection->execSync('serverClientsInfo'); $clientsId = $clientsInfo['clientsId']; $clientsChanged = 0; foreach ($clientsInfo['clients'] as $newClientId => $newClient) { if (isset($oldClients[$newClientId])) { $oldClient = $oldClients[$newClientId]; $changed = false; foreach (static::$CONNECTION_INFO_FIELDS as $fieldName) { if ($oldClient[$fieldName] != $newClient[$fieldName]) { $this->log->debug('update ' . $newClientId . ': ' . $fieldName . '=' . (int) $newClient[$fieldName]); $oldClients[$newClientId][$fieldName] = $newClient[$fieldName]; $oldClients[$newClientId]['lastUpdate'] = time(); } } if ($changed) { $clientsChanged++; } } else { $this->log->debug('new client: ' . $newClientId); $oldClients[$newClientId] = array('lastUpdate' => time(), 'hasId' => $newClient['hasId'], 'hasTalkRequest' => $newClient['hasTalkRequest'], 'hasTalk' => $newClient['hasTalk'], 'hasTalkClose' => $newClient['hasTalkClose'], 'hasShutdown' => $newClient['hasShutdown'], 'isChannelPeer' => $newClient['isChannelPeer'], 'isChannelLocal' => $newClient['isChannelLocal'], 'isOutbound' => $newClient['isOutbound'], 'isInbound' => $newClient['isInbound'], 'isBridgeServer' => $newClient['isBridgeServer'], 'isBridgeClient' => $newClient['isBridgeClient'], 'shutdown' => 0, 'status' => '.'); } } foreach ($oldClients as $oldClientId => $oldClient) { if (!isset($clientsInfo['clients'][$oldClientId]) || $oldClient['hasShutdown']) { if (!$oldClients[$oldClientId]['shutdown']) { $this->log->debug('update ' . $oldClientId . ': shutdown=1'); $oldClients[$oldClientId]['shutdown'] = time(); $oldClients[$oldClientId]['lastUpdate'] = time(); } } if ($oldClient['isOutbound']) { $oldClients[$oldClientId]['status'] = 'o'; } if ($oldClient['isInbound']) { $oldClients[$oldClientId]['status'] = 'i'; } if ($oldClient['isChannelPeer'] || $oldClient['isChannelLocal']) { $oldClients[$oldClientId]['status'] = 'c'; } if ($oldClient['isBridgeServer'] || $oldClient['isBridgeClient']) { $oldClients[$oldClientId]['status'] = 'b'; } if ($oldClient['hasTalkRequest']) { $oldClients[$oldClientId]['status'] = 't'; } if ($oldClient['hasTalk']) { $oldClients[$oldClientId]['status'] = 'T'; } if ($oldClient['hasTalkClose']) { $oldClients[$oldClientId]['status'] = 'X'; } if ($oldClient['shutdown']) { #$this->log->debug('client '.$oldClientId.' has shutdown: '.(time() - $oldClient['shutdown'])); $oldClients[$oldClientId]['status'] = 'x'; if ($oldClient['shutdown'] <= time() - 5) { unset($oldClients[$oldClientId]); } } } $oldClientsLen = count($oldClients); Console::cursorJumpToColumn(15); #sleep(1); print $bytesize->format($clientsInfo['traffic']['in']); #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1); Console::cursorJumpToColumn(15); #sleep(1); print $bytesize->format($clientsInfo['traffic']['out']); #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1); Console::cursorJumpToColumn(11); #sleep(1); print $oldClientsLen . ' / ' . $clientsId; #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1); /*Console::cursorJumpToColumn(15); #sleep(1); $trafficTotal = bcadd($clientsInfo['traffic']['in'], $clientsInfo['traffic']['out']); print $bytesize->format(bcdiv($trafficTotal, time() - $clientsInfo['timeCreated'])).'/s'; #print time() - $clientsInfo['timeCreated']; #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1);*/ print Console::cursorDown(); #sleep(1); Console::cursorJumpToColumn(2); #sleep(1); #print PHP_EOL.' '; #sleep(1); $line = 0; $lineClients = 0; foreach ($oldClients as $oldClientId => $oldClient) { #$this->log->debug('client '.$oldClientId.' print: '.(time() - $oldClient['lastUpdate'])); $output = $oldClient['status']; if ($oldClient['lastUpdate'] >= time() - 2) { $output = $color($oldClient['status'])->bg_green; } elseif ($oldClient['lastUpdate'] <= time() - 60) { #$output = $color($oldClient['status'])->dark; $output = $color($oldClient['status'])->bg_blue; } print $output; $lineClients++; if ($lineClients >= $tcols - 2) { $lineClients = 0; $line++; print PHP_EOL . ' '; } } Console::screenClearToBottom(); #sleep(1); print PHP_EOL; #sleep(1); Console::cursorJumpToColumn(1); #sleep(1); Console::cursorUp($line + $baseLines); #sleep(1); } usleep(static::LOOP_USLEEP); } #sleep(1); Console::cursorUp(); #sleep(1); Console::screenClearToBottom(); #sleep(1); $this->executePost(); $this->log->info('exit'); } else { $settings = $this->getSettings(); $localNode = new Node(); $localNode->setIdHexStr($settings->data['node']['id']); $localNode->setUri($settings->data['node']['uriLocal']); $localNode->setSslKeyPub(file_get_contents($settings->data['node']['sslKeyPubPath'])); $trafficIn = $bytesize->format($settings->data['node']['traffic']['in']); $trafficIn .= ' (' . $settings->data['node']['traffic']['in'] . ' byte)'; $trafficOut = $bytesize->format($settings->data['node']['traffic']['out']); $trafficOut .= ' (' . $settings->data['node']['traffic']['out'] . ' byte)'; print '--------' . PHP_EOL; print 'Informations about local node:' . PHP_EOL; print ' Version: ' . PhpChat::NAME . '/' . PhpChat::VERSION . ' (release ' . PhpChat::RELEASE . ')' . PHP_EOL; print ' ID: ' . $localNode->getIdHexStr() . PHP_EOL; print ' Public key fingerprint: ' . $localNode->getSslKeyPubFingerprint() . PHP_EOL; print ' Last public IP: ' . $settings->data['node']['uriPub'] . PHP_EOL; print ' Listen IP:Port: ' . $settings->data['node']['uriLocal'] . PHP_EOL; print ' Traffic IN: ' . $trafficIn . PHP_EOL; print ' Traffic OUT: ' . $trafficOut . PHP_EOL; print ' Nickname: ' . $settings->data['user']['nickname'] . PHP_EOL; print ' SSL version: ' . OPENSSL_VERSION_TEXT . PHP_EOL; print '--------' . PHP_EOL; print ' Pub Key Base64:' . PHP_EOL . base64_encode($localNode->getSslKeyPub()) . PHP_EOL; print '--------' . PHP_EOL; } #$this->executePost(); }
public function nodeEnclose(Node $node, $sortNodes = true, $level = 1) { if (!$this->getLocalNode()) { throw new RuntimeException('localNode not set.'); } $nodeEncloseReturnValue = $node; /*if($level >= 260){ fwrite(STDOUT, str_repeat("\t", $level).'ERROR: level '.$level.' is too deep'."\n"); throw new RuntimeException('enclose level too deep: '.$level); }*/ /*$printLevel = $level; if($printLevel >= 5){ $printLevel = 5; } $printPrefix = str_repeat("\t", $printLevel); */ if ($node->getIdHexStr() != '00000000-0000-4000-8000-000000000000') { $distance = $node->getDistance(); if (!$distance) { $distance = $this->getLocalNode()->distance($node); $node->setDistance($distance); } $maskByte = 0; $maskBit = 7; // Root MaskBit if ($this->getIsUpper() || $this->getIsLower()) { #fwrite(STDOUT, $printPrefix.'no root bucket'."\n"); $maskByte = $this->getMaskByte(); $maskBit = $this->getMaskBit(); } else { #fwrite(STDOUT, $printPrefix.'root bucket'."\n"); $this->setMaskByte($maskByte); $this->setMaskBit($maskBit); } $maskBitValue = 1 << $maskBit; #fwrite(STDOUT, $printPrefix.'level: '.$level."\n"); #fwrite(STDOUT, $printPrefix.'node: '.$node->getIdHexStr()."\n"); #fwrite(STDOUT, $printPrefix.'dist: '.intToBin($distance[$maskByte])."\n"); #fwrite(STDOUT, $printPrefix.'maskByte: '.$maskByte."\n"); #fwrite(STDOUT, $printPrefix.'maskBit: '.$maskBit."\n"); #fwrite(STDOUT, $printPrefix.'maskBitValue: '.intToBin($maskBitValue)."\n"); #fwrite(STDOUT, $printPrefix.'mask: /'.$maskByte.'/ /'.$maskBit.'/ /'.intToBin($maskBitValue).'/'."\n"); /* if($this->childBucketUpper){ #fwrite(STDOUT, $printPrefix.'upper: '.intToBin($this->childBucketUpper->getMaskBit())."\n"); } else{ #fwrite(STDOUT, $printPrefix.'upper: N/A'."\n"); } if($this->childBucketLower){ #fwrite(STDOUT, $printPrefix.'lower: '.intToBin($this->childBucketLower->getMaskBit())."\n"); } else{ #fwrite(STDOUT, $printPrefix.'lower: N/A'."\n"); } */ #timeStop('onode find start'); $onode = $this->nodeFind($node); #timeStop('onode find end'); if (!$onode) { #fwrite(STDOUT, $printPrefix.'old node not found'."\n"); if ($this->getNodesNum() < static::$SIZE_MAX && !$this->getIsFull()) { #fwrite(STDOUT, $printPrefix.'add node'."\n"); $this->nodeAdd($node, $sortNodes); $nodeEncloseReturnValue = $node; if ($this->isFull()) { #fwrite(STDOUT, $printPrefix.'FULL end'."\n"); #timeStop('nodesReEnclose start'); $this->nodesReEnclose($sortNodes, $level + 1); #timeStop('nodesReEnclose end'); } } else { #fwrite(STDOUT, $printPrefix.'FULL new'."\n"); $bucket = null; if ($distance[$maskByte] & $maskBitValue) { #fwrite(STDOUT, $printPrefix.'match: upper'."\n"); $this->setChildBucketUpper($distance[$maskByte]); $bucket = $this->childBucketUpper; } else { #fwrite(STDOUT, $printPrefix.'match: lower'."\n"); $this->setChildBucketLower($distance[$maskByte]); $bucket = $this->childBucketLower; } if ($bucket !== null) { #$msgOut = intToBin($bucket->getMaskBit()).' d='.intToBin($bucket->getDistance()).''; #fwrite(STDOUT, $printPrefix.'child bucket: '.$msgOut."\n"); $bucket->nodeEnclose($node, $sortNodes, $level + 1); } /*else{ #$msgOut = 'enclose: bucket is full: '; #$msgOut .= 'l='.$level.' '; #$msgOut .= 'd='.intToBin($distance[$maskByte]).' '; #$msgOut .= 'm='.intToBin($maskBitValue).' n='.$node->getIdHexStr().''; #fwrite(STDOUT, $printPrefix.$msgOut."\n"); #throw new RuntimeException($msgOut); # NOT_IMPLEMENTED: what happens when a bucket is not found? }*/ } } else { #fwrite(STDOUT, $printPrefix.'update existing node'."\n"); #usleep(1000000); #timeStop('onode update start'); $onode->update($node); #timeStop('onode update end'); $nodeEncloseReturnValue = $onode; } } return $nodeEncloseReturnValue; }
public function bootstrapNodesEncloseJson($json) { #fwrite(STDOUT, 'json: '.(int)$this->ipcKernelConnection.PHP_EOL); $settingsBridgeClient = $this->settings->data['node']['bridge']['client']['enabled']; $nodes = array(); if (isset($json['nodes']) && is_array($json['nodes'])) { foreach ($json['nodes'] as $node) { #$this->log->debug('node'); $nodeObj = new Node(); $active = false; if (isset($node['active'])) { $active = (bool) $node['active']; } if ($active) { if (isset($node['id'])) { $nodeObj->setIdHexStr($node['id']); } if (isset($node['uri'])) { $nodeObj->setUri($node['uri']); } if (isset($node['bridgeServer'])) { $nodeObj->setBridgeServer($node['bridgeServer']); } $this->log->debug('node: /' . $nodeObj->getIdHexStr() . '/ /' . $nodeObj->getUri() . '/'); if (!$nodeObj->isEqual($this->table->getLocalNode())) { if ($nodeObj->getIdHexStr() == '00000000-0000-4000-8000-000000000000') { /*if(!$nodeObj->getBridgeServer() && !$this->settings->data['node']['bridge']['client']['enabled']){ $this->log->debug('no bridge server'); }*/ if ((string) $nodeObj->getUri()) { #$logTmp = '('.(int)$nodeObj->getBridgeServer().','; #$logTmp .= (int)$this->settings->data['node']['bridge']['client']['enabled'].')'; #$this->log->debug(' NO ID, URI '.$logTmp); $noBridge = !$nodeObj->getBridgeServer() && !$settingsBridgeClient; $isBridgeServer = $nodeObj->getBridgeServer() && !$settingsBridgeClient; $isBridgeService = $nodeObj->getBridgeServer() && $settingsBridgeClient; if ($noBridge || $isBridgeServer || $isBridgeService) { $nodes[] = array('type' => 'connect', 'node' => $nodeObj); #$this->log->debug(' add connect'); } } /*else{ $this->log->debug(' NO ID, NO URI'); }*/ } else { if ((string) $nodeObj->getUri()) { #$this->log->debug(' ID, URI'); $nodes[] = array('type' => 'enclose', 'node' => $nodeObj); } else { #$this->log->debug(' ID, NO URI'); $nodes[] = array('type' => 'find', 'node' => $nodeObj); } } } /*else{ $this->log->debug(' ignore local node'); }*/ } } } foreach ($nodes as $nodeId => $node) { #$msgOut = $nodeId.' '.$node['type']; #$msgOut .= ' /'.$node['node']->getIdHexStr().'/ /'.$node['node']->getUri().'/'; #$this->log->debug('node: '.$msgOut); $functionName = ''; $functionArgs = array(); if ($node['type'] == 'enclose') { $functionName = 'tableNodeEnclose'; $functionArgs = array($node['node']); } elseif ($node['type'] == 'connect') { $functionName = 'nodesNewDbNodeAddConnect'; $functionArgs = array((string) $node['node']->getUri()); } elseif ($node['type'] == 'find') { $functionName = 'nodesNewDbNodeAddFind'; $functionArgs = array($node['node']->getIdHexStr()); } $functionArgs[] = $node['node']->getBridgeServer(); if ($this->ipcKernelConnection && $functionName) { $this->ipcKernelConnection->execAsync($functionName, $functionArgs); } } return $nodes; // Return only for tests. }
public function isEqual(Node $node) { return $this->getIdHexStr() == $node->getIdHexStr(); }
private function handleCommandMsg($line) { #print __CLASS__.'->'.__FUNCTION__.': "'.$line.'"'."\n"; $line = substr($line, 3); #print __CLASS__.'->'.__FUNCTION__.': get table'."\n"; $table = $this->ipcKernelConnection->execSync('getTable'); #print __CLASS__.'->'.__FUNCTION__.': get msgDb'."\n"; #$msgDb = $this->ipcKernelConnection->execSync('getMsgDb'); $msgs = $this->ipcKernelConnection->execSync('msgDbMsgGetMsgsForDst'); $msgsByIndex = array_keys($msgs); #ve($msgDb); #ve($msgs); #ve($msgsByIndex); /* if(!$msgDb){ print __CLASS__.'->'.__FUNCTION__.': get msgDb failed'."\n"; return; }*/ if ($line) { $line = substr($line, 1); $args = preg_split('/ /', $line); #ve($args); #print __CLASS__.'->'.__FUNCTION__.': rest "'.$line.'"'."\n"; #$this->printPs1(true, 'handleCommandMsg A'); if ($args[0] == 'new' || $args[0] == 'n') { if (Uuid::isValid($args[1])) { if ($args[1] != $table->getLocalNode()->getIdHexStr()) { $this->sttyExitIcanonMode(); $this->sttyEchoOn(); stream_set_blocking(STDIN, 1); print PHP_EOL . 'Subject: '; $subject = strtolower(substr(fgets(STDIN, 100), 0, -1)); if (!$subject) { $subject = 'No Subject'; } #print "Subject: '".$subject."'\n\n"; print PHP_EOL . 'Enter the text to send.' . PHP_EOL; print 'NOTE: end text with <RETURN>.<RETURN>' . PHP_EOL; $text = ''; while (!$this->getExit()) { $line = fgets(STDIN, 1024); #print "line: '".substr($line, 0, -1)."'\n"; if (substr($line, 0, -1) == '.') { break; } $text .= $line; #sleep(1); } if (!$this->getExit()) { $text = substr($text, 0, -1); print 'Send msg? [Y/n] '; $answer = strtolower(substr(fgets(STDIN, 100), 0, -1)); if (!$answer) { $answer = 'y'; } print "Answer: '" . $answer . "'" . PHP_EOL; #print "Text: '".$text."'\n"; stream_set_blocking(STDIN, 0); $this->sttyEnterIcanonMode(); $this->sttyEchoOff(); if ($answer == 'y') { $dstNodeId = $args[1]; #$dstNodeId = '42785b21-011b-4093-b61d-000000000001'; #$text = 'this is a test. '.date('Y-m-d H:i:s'); $table = $this->ipcKernelConnection->execSync('getTable'); $msg = new Msg(); $msg->setSrcNodeId($this->settings->data['node']['id']); $msg->setSrcSslKeyPub($table->getLocalNode()->getSslKeyPub()); $msg->setSrcUserNickname($this->userNickname); $dstNode = new Node(); $dstNode->setIdHexStr($dstNodeId); $msg->setDstNodeId($dstNode->getIdHexStr()); if ($oDstNode = $table->nodeFind($dstNode)) { #print 'found node in table'.PHP_EOL; $msg->setDstSslPubKey($oDstNode->getSslKeyPub()); } #else{ print 'node not found'.PHP_EOL; } $msg->setSubject($subject); $msg->setText($text); $msg->setSslKeyPrvPath($this->settings->data['node']['sslKeyPrvPath'], $this->settings->data['node']['sslKeyPrvPass']); $msg->setStatus('O'); $encrypted = false; #print 'DstSslPubKey: '.strlen($msg->getDstSslPubKey()).PHP_EOL; if ($msg->getDstSslPubKey()) { #print 'use dst key'.PHP_EOL; $msg->setEncryptionMode('D'); } else { // Encrypt with own public key // while destination public key is not available. #print 'use local key'.PHP_EOL; $msg->setEncryptionMode('S'); $msg->setDstSslPubKey($table->getLocalNode()->getSslKeyPub()); } try { $encrypted = $msg->encrypt(); if ($encrypted) { $this->ipcKernelConnection->execAsync('msgDbMsgAdd', array($msg)); $this->msgAdd('OK: msg created ' . $msg->getId(), true, true); } else { $this->msgAdd('ERROR: could not encrypt message.', true, true); } } catch (Exception $e) { $this->msgAdd('ERROR: ' . $e->getMessage(), true, true); } #$this->printPs1(true, 'handleCommandMsg B'); } else { print 'Nothing created, nothing sent.' . PHP_EOL; $this->printPs1(true, 'handleCommandMsg C'); } } } else { $this->msgAdd(); $this->msgAdd('Send a message to yourself?', false, true); } } else { $this->msgAdd(); $this->msgAdd('ERROR: "' . $args[1] . '" is not a UUID.', false, true); } } elseif ($args[0] == 'read' || $args[0] == 'r') { if (isset($args[1])) { $msg = null; if (Uuid::isValid($args[1])) { if (isset($msgs[$args[1]])) { $msg = $msgs[$args[1]]; } } else { $no = (int) $args[1] - 1; if (isset($msgsByIndex[$no])) { $msg = $msgs[$msgsByIndex[$no]]; } } if ($msg) { $msg->setDstSslPubKey($table->getLocalNode()->getSslKeyPub()); $sslKeyPrvPath = $this->settings->data['node']['sslKeyPrvPath']; $sslKeyPrvPass = $this->settings->data['node']['sslKeyPrvPass']; $msg->setSslKeyPrvPath($sslKeyPrvPath, $sslKeyPrvPass); #ve($msg); $text = null; try { $text = $msg->decrypt(); } catch (Exception $e) { $text = null; #print 'ERROR: decrypt: '.$e->getMessage().PHP_EOL; } $dateCreated = new DateTime(); $dateCreated->setTimestamp($msg->getTimeCreated()); $dateReceived = new DateTime(); $dateReceived->setTimestamp($msg->getTimeReceived()); $fromLine = ''; if ($msg->getSrcUserNickname()) { $fromLine .= $msg->getSrcUserNickname() . ' '; } $fromLine .= '<' . $msg->getSrcNodeId() . '>'; $toLine = ''; if ($table->getLocalNode()->getIdHexStr() == $msg->getDstNodeId()) { $toLine .= 'Me '; } $toLine .= '<' . $msg->getDstNodeId() . '>'; $this->msgAdd(); if (!$text) { $this->msgAdd('WARNING: could not decrypt text. Only meta data available.', false, false); } $this->msgAdd('----- MESSAGE BEGIN -----'); $this->msgAdd('Subject: ' . $msg->getSubject(), false, false); $this->msgAdd('From: ' . $fromLine, false, false); $this->msgAdd('To: ' . $toLine, false, false); $this->msgAdd('Msg ID: ' . $msg->getId(), false, false); $this->msgAdd('Status: ' . $msg->getStatusText(), false, false); $this->msgAdd('Created: ' . $dateCreated->format('Y-m-d H:i:s'), false, false); $this->msgAdd('Received: ' . $dateReceived->format('Y-m-d H:i:s'), false, false); if ($text) { $this->msgAdd(); $this->msgAdd($text, false, false); $msg->setStatus('R'); $this->ipcKernelConnection->execAsync('msgDbMsgUpdate', array($msg)); } $this->msgAdd('----- MESSAGE END -----', false, true); } else { $this->msgAdd(); $this->msgAdd('ERROR: could not read msg "' . $args[1] . '".', false, true); } } else { $this->msgAdd(); $this->msgAdd('ERROR: you must specify a msg number or ID.', false, true); } } } else { $format = '%3d %1s %36s %s %s'; $this->msgAdd(); $this->msgAdd(' # N FROM CRATED RECEIVED', false, true); $no = 0; foreach ($msgs as $msgId => $msg) { $no++; $dateCreated = new DateTime(); $dateCreated->setTimestamp($msg->getTimeCreated()); $dateReceived = new DateTime(); $dateReceived->setTimestamp($msg->getTimeReceived()); $line = sprintf($format, $no, $msg->getStatus() == 'U' ? '*' : ' ', $msg->getSrcNodeId(), $dateCreated->format('Y-m-d H:i:s'), $dateReceived->format('Y-m-d H:i:s')); $this->msgAdd($line, false, false); } $this->msgAdd('END OF LIST', false, true); } }
private function nodeRemove(Node $node) { $filesystem = new Filesystem(); $nodeId = $node->getIdHexStr(); if (isset($this->nodes[$nodeId])) { unset($this->nodes[$nodeId]); } #fwrite(STDOUT, 'node delete: '.$nodeId.' /'.$node->getFilePath().'/'.PHP_EOL); $filesystem->remove($node->getFilePath()); }
public function mailNew($event, $from, $rcpt, $mail) { #fwrite(STDOUT, 'mail new: /'.$from.'/ a/'.join('/ /', $rcpt).'/'."\n"); #$this->log->debug('mailNew: '.$event->getTrigger().' /'.$from.'/'); $settings = $this->getSettings(); $table = $this->ipcKernelConnection->execSync('getTable'); $text = $mail->getBody(); foreach ($rcpt as $dstNodeId) { $dstNodeId = substr($dstNodeId, 0, strpos($dstNodeId, '@')); #fwrite(STDOUT, 'to: /'.$dstNodeId.'/'."\n"); $msg = new Msg(); $msg->setSrcNodeId($settings->data['node']['id']); $msg->setSrcSslKeyPub($table->getLocalNode()->getSslKeyPub()); $msg->setSrcUserNickname($settings->data['user']['nickname']); $dstNode = new Node(); $dstNode->setIdHexStr($dstNodeId); $msg->setDstNodeId($dstNode->getIdHexStr()); if ($oDstNode = $table->nodeFind($dstNode)) { #print 'found node in table'.PHP_EOL; $msg->setDstSslPubKey($oDstNode->getSslKeyPub()); } #else{ print 'node not found'.PHP_EOL; } $msg->setSubject($mail->getSubject()); $msg->setText($text); $msg->setSslKeyPrvPath($settings->data['node']['sslKeyPrvPath'], $settings->data['node']['sslKeyPrvPass']); $msg->setStatus('O'); $encrypted = false; #print 'DstSslPubKey: '.strlen($msg->getDstSslPubKey()).PHP_EOL; if ($msg->getDstSslPubKey()) { #print 'use dst key'.PHP_EOL; $msg->setEncryptionMode('D'); } else { // Encrypt with own public key // while destination public key is not available. #print 'use local key'.PHP_EOL; $msg->setEncryptionMode('S'); $msg->setDstSslPubKey($table->getLocalNode()->getSslKeyPub()); } try { $encrypted = $msg->encrypt(); if ($encrypted) { $this->ipcKernelConnection->execAsync('msgDbMsgAdd', array($msg)); $this->log->debug('OK: msg created ' . $msg->getId()); } else { $this->log->error('Could not encrypt message.'); } } catch (Exception $e) { $this->log->error('ERROR: ' . $e->getMessage()); } } }
public function testUpdate() { $node_a = new Node(); $node_a->setIdHexStr('11111111-1111-4111-8111-111111111100'); $node_a->setUri('tcp://192.168.241.24:25000'); $node_b = new Node(); $node_b->setIdHexStr('11111111-1111-4111-8111-111111111101'); $node_b->setUri('tcp://192.168.241.25:25000'); $node_b->setBridgeServer(true); $node_b->setBridgeClient(true); $node_a->update($node_b); $this->assertEquals('11111111-1111-4111-8111-111111111100', $node_a->getIdHexStr()); $this->assertEquals('tcp://192.168.241.24:25000', (string) $node_a->getUri()); $this->assertFalse($node_a->getBridgeServer()); $this->assertFalse($node_a->getBridgeClient()); $this->assertFalse($node_a->getDataChanged()); $node_b->setTimeLastSeen(time()); $node_a->update($node_b); $this->assertEquals('11111111-1111-4111-8111-111111111100', $node_a->getIdHexStr()); $this->assertEquals('tcp://192.168.241.25:25000', (string) $node_a->getUri()); $this->assertTrue($node_a->getBridgeServer()); $this->assertTrue($node_a->getBridgeClient()); $this->assertTrue($node_a->getDataChanged()); }