예제 #1
0
파일: MsgDb.php 프로젝트: thefox/phpchat
 public function msgUpdate(Msg $msgNew)
 {
     $rv = false;
     if (isset($this->msgs[$msgNew->getId()])) {
         $msgOld = $this->msgs[$msgNew->getId()];
         if ($msgOld->getVersion() != $msgNew->getVersion()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: version'."\n";
             $msgOld->setVersion($msgNew->getVersion());
             $this->setDataChanged(true);
             $rv = true;
         }
         /*if($msgOld->getId() != $msgNew->getId()){
         			#print __CLASS__.'->'.__FUNCTION__.': changed: id'."\n";
         			$msgOld->setId($msgNew->getId());
         			$this->setDataChanged(true);
         			$rv = true;
         		}*/
         if ($msgOld->getSrcNodeId() != $msgNew->getSrcNodeId()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed srcNodeId: '.$msgOld->getSrcNodeId().' '.$msgNew->getSrcNodeId()."\n";
             $msgOld->setSrcNodeId($msgNew->getSrcNodeId());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getSrcSslKeyPub() != $msgNew->getSrcSslKeyPub()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: srcSslKeyPub'."\n";
             $msgOld->setSrcSslKeyPub($msgNew->getSrcSslKeyPub());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getDstNodeId() != $msgNew->getDstNodeId()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: dstNodeId'."\n";
             $msgOld->setDstNodeId($msgNew->getDstNodeId());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getDstSslPubKey() != $msgNew->getDstSslPubKey()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: dstSslPubKey'."\n";
             $msgOld->setDstSslPubKey($msgNew->getDstSslPubKey());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getSubject() != $msgNew->getSubject()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: subject'."\n";
             $msgOld->setSubject($msgNew->getSubject());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getBody() != $msgNew->getBody()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: text'."\n";
             $msgOld->setBody($msgNew->getBody());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getText() != $msgNew->getText()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: text'."\n";
             $msgOld->setText($msgNew->getText());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getPassword() != $msgNew->getPassword()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: password'."\n";
             $msgOld->setPassword($msgNew->getPassword());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getChecksum() != $msgNew->getChecksum()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: checksum'."\n";
             $msgOld->setChecksum($msgNew->getChecksum());
             $this->setDataChanged(true);
             $rv = true;
         }
         $msgOldSentNodes = $msgOld->getSentNodes();
         $msgNewSentNodes = $msgNew->getSentNodes();
         #if(count($msgOldSentNodes) < count($msgNewSentNodes)){
         if ($msgOldSentNodes != $msgNewSentNodes) {
             $msgOld->setSentNodes(array_unique(array_merge($msgOldSentNodes, $msgNewSentNodes)));
         }
         if ($msgOld->getRelayCount() != $msgNew->getRelayCount()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: relayCount'."\n";
             $msgOld->setRelayCount($msgNew->getRelayCount());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getForwardCycles() != $msgNew->getForwardCycles()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: forwardCycles'."\n";
             $msgOld->setForwardCycles($msgNew->getForwardCycles());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getEncryptionMode() != $msgNew->getEncryptionMode()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: encryptionMode, '.$msgOld->getEncryptionMode();
             #print ', '.$msgNew->getEncryptionMode()."\n";
             $msgOld->setEncryptionMode($msgNew->getEncryptionMode());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getStatus() != $msgNew->getStatus()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: status'."\n";
             $msgOld->setStatus($msgNew->getStatus());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getTimeCreated() != $msgNew->getTimeCreated()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: timeCreated'."\n";
             $msgOld->setTimeCreated($msgNew->getTimeCreated());
             $this->setDataChanged(true);
             $rv = true;
         }
         /*if($msgOld->getDataChanged() != $msgNew->getDataChanged()){
         			#print __CLASS__.'->'.__FUNCTION__.': changed: dataChanged'."\n";
         			$msgOld->setDataChanged($msgNew->getDataChanged());
         		}*/
     }
     return $rv;
 }
예제 #2
0
파일: MsgTest.php 프로젝트: thefox/phpchat
 public function testId()
 {
     $msg = new Msg();
     $this->assertTrue(Uuid::isValid($msg->getId()));
 }
예제 #3
0
파일: Client.php 프로젝트: thefox/phpchat
 public function sendMsg(Msg $msg)
 {
     $rid = (string) Uuid::uuid4();
     $this->requestAdd('msg', $rid, array('msg' => $msg));
     $data = array('rid' => $rid, 'version' => $msg->getVersion(), 'id' => $msg->getId(), 'srcNodeId' => $msg->getSrcNodeId(), 'srcSslKeyPub' => base64_encode($msg->getSrcSslKeyPub()), 'dstNodeId' => $msg->getDstNodeId(), 'body' => $msg->getBody(), 'password' => $msg->getPassword(), 'checksum' => $msg->getChecksum(), 'relayCount' => (int) $msg->getRelayCount() + 1, 'timeCreated' => (int) $msg->getTimeCreated(), 'hashcash' => $this->hashcashMint(static::HASHCASH_BITS_MAX));
     return $this->dataSend($this->msgCreate('msg', $data));
 }
예제 #4
0
 public function testMsgDbDefault()
 {
     $uuid1 = '10000000-1000-4001-8001-1000000000';
     $uuid2 = '20000000-2000-4002-8002-20000000';
     @unlink('test_data/bucket_root.yml');
     $runName = uniqid('', true);
     $prvFileName = 'testfile_cronjob_id_rsa_' . date('Ymd_His') . '_' . $runName . '.prv';
     $pubFileName = 'testfile_cronjob_id_rsa_' . date('Ymd_His') . '_' . $runName . '.pub';
     $settignsFileName = 'testfile_cronjob_settings_' . date('Ymd_His') . '_' . $runName . '.pub';
     file_put_contents('test_data/' . $prvFileName, static::NODE_LOCAL_SSL_KEY_PRV);
     file_put_contents('test_data/' . $pubFileName, static::NODE_LOCAL_SSL_KEY_PUB);
     $settings = new Settings('test_data/' . $settignsFileName);
     $settings->data['datadir'] = 'test_data';
     $settings->data['node']['id'] = Node::genIdHexStr(static::NODE_LOCAL_SSL_KEY_PUB);
     $settings->data['node']['sslKeyPrvPass'] = '******';
     $settings->data['node']['sslKeyPrvPath'] = 'test_data/' . $prvFileName;
     $settings->data['node']['sslKeyPubPath'] = 'test_data/' . $pubFileName;
     $settings->data['node']['bridge']['client']['enabled'] = false;
     #$settings->setDataChanged(true);
     #$settings->save();
     $localNode = new Node();
     $localNode->setIdHexStr($settings->data['node']['id']);
     $localNode->setUri($settings->data['node']['uriLocal']);
     $localNode->setSslKeyPub(file_get_contents($settings->data['node']['sslKeyPubPath']));
     $this->assertEquals(static::NODE_LOCAL_SSL_KEY_PUB, $localNode->getSslKeyPub());
     // @codingStandardsIgnoreStart
     #$this->assertEquals('FC_WwG2GdTmCLSKhpEmJso6pejm9c6oACjX', Node::genSslKeyFingerprint($localNode->getSslKeyPub()));
     $this->assertEquals('FC_6t6Z9dYVWEDfEzGQGDSAteLQsFE8SDwZFK2PoiQuM2ezFUA2yNpBPiT9oBwvFBfzDWZzZF5sxBtcSd', Node::genSslKeyFingerprint($localNode->getSslKeyPub()));
     // @codingStandardsIgnoreEnd
     $nodes = array();
     $nodes[0] = new Node();
     $nodes[0]->setIdHexStr($uuid1 . '00');
     $nodes[0]->setUri('tcp://127.0.0.0:25000');
     $nodes[0]->setSslKeyPub(static::NODE0_SSL_KEY_PUB);
     $nodes[1] = new Node();
     $nodes[1]->setIdHexStr($uuid1 . '01');
     $nodes[1]->setUri('tcp://127.0.0.1:25000');
     $nodes[2] = new Node();
     $nodes[2]->setIdHexStr($uuid1 . '02');
     $nodes[2]->setUri('tcp://127.0.0.2:25000');
     $nodes[2]->setSslKeyPub(static::NODE2_SSL_KEY_PUB);
     $nodes[3] = new Node();
     $nodes[3]->setIdHexStr($uuid1 . '03');
     $nodes[3]->setUri('tcp://127.0.0.3:25000');
     $nodes[4] = new Node();
     $nodes[4]->setIdHexStr($uuid1 . '04');
     $nodes[4]->setUri('tcp://127.0.0.4:25000');
     $nodes[4]->setSslKeyPub(static::NODE4_SSL_KEY_PUB);
     $nodes[5] = new Node();
     $nodes[5]->setIdHexStr($uuid1 . '05');
     $nodes[5]->setUri('tcp://127.0.0.5:25000');
     $nodes[5]->setSslKeyPub(static::NODE5_SSL_KEY_PUB);
     $table = new Table();
     $table->setDatadirBasePath($settings->data['datadir']);
     $table->setLocalNode($localNode);
     $table->nodeEnclose($nodes[0]);
     $table->nodeEnclose($nodes[1]);
     $table->nodeEnclose($nodes[2]);
     #$table->nodeEnclose($nodes[3]); // Test not in table.
     #$table->nodeEnclose($nodes[4]);
     #$table->nodeEnclose($nodes[5]);
     $msgs = array();
     for ($nodeNo = 1000; $nodeNo <= 1011; $nodeNo++) {
         $msg = new Msg();
         $msg->setId($uuid2 . $nodeNo);
         $this->assertEquals($uuid2 . $nodeNo, $msg->getId());
         $msg->setSrcNodeId($settings->data['node']['id']);
         $msg->setSrcSslKeyPub($table->getLocalNode()->getSslKeyPub());
         $msg->setSrcUserNickname($settings->data['user']['nickname']);
         $msg->setText('this is  a test. ' . date('Y-m-d H:i:s'));
         $msg->setSslKeyPrvPath($settings->data['node']['sslKeyPrvPath'], $settings->data['node']['sslKeyPrvPass']);
         $msg->setStatus('O');
         $msg->setDstNodeId($nodes[0]->getIdHexStr());
         $msg->setEncryptionMode('D');
         $msg->setDstSslPubKey($nodes[0]->getSslKeyPub());
         $msgs[$nodeNo] = $msg;
         #fwrite(STDOUT, __METHOD__.' msg setup: '.$nodeNo.''.PHP_EOL);
     }
     $msgs[1001]->setDstNodeId($nodes[1]->getIdHexStr());
     $msgs[1001]->setEncryptionMode('S');
     $msgs[1001]->setDstSslPubKey($table->getLocalNode()->getSslKeyPub());
     $this->assertEquals('S', $msgs[1001]->getEncryptionMode());
     $msgs[1002]->setDstNodeId($nodes[2]->getIdHexStr());
     $msgs[1002]->setEncryptionMode('S');
     $msgs[1002]->setDstSslPubKey($table->getLocalNode()->getSslKeyPub());
     $this->assertEquals('S', $msgs[1002]->getEncryptionMode());
     $msgs[1003]->setDstNodeId($nodes[3]->getIdHexStr());
     $msgs[1003]->setEncryptionMode('S');
     $msgs[1003]->setDstSslPubKey($table->getLocalNode()->getSslKeyPub());
     $this->assertEquals('S', $msgs[1003]->getEncryptionMode());
     $msgs[1004]->setSentNodes(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
     $this->assertEquals(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), $msgs[1004]->getSentNodes());
     $msgs[1005]->setStatus('U');
     $msgs[1006]->setStatus('S');
     $msgs[1007]->setStatus('S');
     $msgs[1007]->setSentNodes(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21));
     $msgs[1008]->setStatus('S');
     $msgs[1008]->setForwardCycles(110);
     $msgs[1008]->setSentNodes(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
     $msgs[1009]->setStatus('S');
     $msgs[1009]->setSentNodes(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, $uuid1 . '00'));
     $msgs[1010]->setRelayNodeId($uuid1 . '01');
     $msgs[1011]->setSrcNodeId($nodes[4]->getIdHexStr());
     $msgs[1011]->setDstNodeId($nodes[5]->getIdHexStr());
     $msgs[1011]->setStatus('U');
     $msgDb = new MsgDb();
     $msgDb->setDatadirBasePath($settings->data['datadir']);
     $cronjobLog = new Logger('cronjob');
     #$cronjobLog->pushHandler(new LoggerStreamHandler('php://stdout', Logger::DEBUG));
     $cronjob = new Cronjob();
     $cronjob->setLog($cronjobLog);
     $cronjob->setMsgDb($msgDb);
     $cronjob->setSettings($settings);
     $cronjob->setTable($table);
     // Encrypt
     foreach ($msgs as $msgId => $msg) {
         #fwrite(STDOUT, __METHOD__.' encrypt: '.$msgId.PHP_EOL);
         $encrypted = false;
         try {
             $encrypted = $msg->encrypt();
         } catch (Exception $e) {
             print 'ERROR: ' . $e->getMessage() . PHP_EOL;
         }
         $this->assertTrue($encrypted);
         $rv = $cronjob->getMsgDb()->msgAdd($msg);
     }
     $this->assertEquals(12, $cronjob->getMsgDb()->getMsgsCount());
     // Init Nodes
     #fwrite(STDOUT, __METHOD__.' init nodes'.PHP_EOL);
     $cronjob->msgDbInitNodes();
     $cronjobMsgs = $cronjob->getMsgDb()->getMsgs();
     #foreach($cronjobMsgs as $msgId => $msg){
     #	$logOut = $msg->getId().', '.$msg->getStatus().', '.$msg->getEncryptionMode();
     #	fwrite(STDOUT, __METHOD__.' cronjobMsgs: '.$logOut.PHP_EOL);
     #}
     $this->assertEquals(12, count($cronjobMsgs));
     $this->assertEquals($msgs[1000], $cronjobMsgs[$uuid2 . '1000']);
     $this->assertEquals($msgs[1001], $cronjobMsgs[$uuid2 . '1001']);
     $this->assertEquals($msgs[1002], $cronjobMsgs[$uuid2 . '1002']);
     $this->assertEquals($msgs[1003], $cronjobMsgs[$uuid2 . '1003']);
     $this->assertEquals($msgs[1004], $cronjobMsgs[$uuid2 . '1004']);
     $this->assertEquals($msgs[1005], $cronjobMsgs[$uuid2 . '1005']);
     $this->assertEquals($msgs[1006], $cronjobMsgs[$uuid2 . '1006']);
     $this->assertEquals($msgs[1007], $cronjobMsgs[$uuid2 . '1007']);
     $this->assertEquals($msgs[1008], $cronjobMsgs[$uuid2 . '1008']);
     $this->assertEquals($msgs[1009], $cronjobMsgs[$uuid2 . '1009']);
     $this->assertEquals($msgs[1010], $cronjobMsgs[$uuid2 . '1010']);
     $this->assertEquals($msgs[1011], $cronjobMsgs[$uuid2 . '1011']);
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1000']->getEncryptionMode());
     $this->assertEquals('S', $cronjobMsgs[$uuid2 . '1001']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1002']->getEncryptionMode());
     $this->assertEquals('S', $cronjobMsgs[$uuid2 . '1003']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1004']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1005']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1006']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1007']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1008']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1009']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1010']->getEncryptionMode());
     $this->assertEquals('D', $cronjobMsgs[$uuid2 . '1011']->getEncryptionMode());
     $updateMsgs = $cronjob->msgDbSendAll();
     /*foreach($updateMsgs as $msgId => $msg){
     			$logOut = '/'.$msg['obj']->getId().'/';
     			$logOut .= ' /'.$msg['obj']->getStatus().'/';
     			$logOut .= ' /'.$msg['obj']->getEncryptionMode().'/'.' '.count($msg['nodes']);
     			fwrite(STDOUT, __METHOD__.' update msg: '.$logOut.PHP_EOL);
     			#ve($msg['nodes']);
     		}*/
     $this->assertEquals(6, count($updateMsgs));
     $this->assertEquals('O', $msgs[1000]->getStatus());
     $this->assertEquals('O', $msgs[1002]->getStatus());
     $this->assertEquals('U', $msgs[1005]->getStatus());
     $this->assertEquals('S', $msgs[1006]->getStatus());
     $this->assertEquals('O', $msgs[1010]->getStatus());
     $this->assertEquals('U', $msgs[1011]->getStatus());
     $this->assertEquals('D', $msgs[1000]->getEncryptionMode());
     $this->assertEquals('D', $msgs[1002]->getEncryptionMode());
     $this->assertEquals('D', $msgs[1005]->getEncryptionMode());
     $this->assertEquals('D', $msgs[1006]->getEncryptionMode());
     $this->assertEquals('D', $msgs[1010]->getEncryptionMode());
     $this->assertEquals('D', $msgs[1011]->getEncryptionMode());
     $this->assertTrue(array_key_exists($uuid2 . '1000', $updateMsgs));
     $this->assertTrue(array_key_exists($uuid2 . '1002', $updateMsgs));
     $this->assertTrue(array_key_exists($uuid2 . '1005', $updateMsgs));
     $this->assertTrue(array_key_exists($uuid2 . '1006', $updateMsgs));
     $this->assertTrue(array_key_exists($uuid2 . '1010', $updateMsgs));
     $this->assertTrue(array_key_exists($uuid2 . '1011', $updateMsgs));
     $this->assertEquals(3, count($updateMsgs[$uuid2 . '1000']['nodes']));
     $this->assertEquals(3, count($updateMsgs[$uuid2 . '1002']['nodes']));
     $this->assertEquals(3, count($updateMsgs[$uuid2 . '1005']['nodes']));
     $this->assertEquals(3, count($updateMsgs[$uuid2 . '1006']['nodes']));
     $this->assertEquals(2, count($updateMsgs[$uuid2 . '1010']['nodes']));
     $this->assertEquals(3, count($updateMsgs[$uuid2 . '1011']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '00', $updateMsgs[$uuid2 . '1000']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '01', $updateMsgs[$uuid2 . '1000']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '02', $updateMsgs[$uuid2 . '1000']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '00', $updateMsgs[$uuid2 . '1002']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '01', $updateMsgs[$uuid2 . '1002']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '02', $updateMsgs[$uuid2 . '1002']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '00', $updateMsgs[$uuid2 . '1005']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '01', $updateMsgs[$uuid2 . '1005']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '02', $updateMsgs[$uuid2 . '1005']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '00', $updateMsgs[$uuid2 . '1006']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '01', $updateMsgs[$uuid2 . '1006']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '02', $updateMsgs[$uuid2 . '1006']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '00', $updateMsgs[$uuid2 . '1010']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '02', $updateMsgs[$uuid2 . '1010']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '00', $updateMsgs[$uuid2 . '1011']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '01', $updateMsgs[$uuid2 . '1011']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '02', $updateMsgs[$uuid2 . '1011']['nodes']));
 }
예제 #5
0
파일: Server.php 프로젝트: thefox/phpchat
 public function imapMailAdd(Msg $msg)
 {
     if ($this->getKernel() && $this->getKernel()->getIpcImapConnection()) {
         $version = $msg->getVersion();
         $id = $msg->getId();
         $srcNodeId = $msg->getSrcNodeId();
         $srcUserNickname = $msg->getSrcUserNickname();
         $dstNodeId = $msg->getDstNodeId();
         $subject = $msg->getSubject();
         $text = $msg->getText();
         $checksum = $msg->getChecksum();
         $relayCount = $msg->getRelayCount();
         $encryptionMode = $msg->getEncryptionMode();
         $status = $msg->getStatus();
         $timeCreated = $msg->getTimeCreated();
         $timeReceived = $msg->getTimeReceived();
         $args = array();
         $args[] = $version;
         $args[] = $id;
         $args[] = $srcNodeId;
         $args[] = $srcUserNickname;
         $args[] = $dstNodeId;
         $args[] = $subject;
         $args[] = $text;
         $args[] = $checksum;
         $args[] = $relayCount;
         $args[] = $encryptionMode;
         $args[] = $status;
         $args[] = $timeCreated;
         $args[] = $timeReceived;
         $this->getKernel()->getIpcImapConnection()->execAsync('mailAdd', $args);
     }
 }
예제 #6
0
파일: Console.php 프로젝트: thefox/phpchat
 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);
     }
 }
예제 #7
0
 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());
         }
     }
 }