Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 21c99f473c
Fetching contributors…

Cannot retrieve contributors at this time

file 110 lines (97 sloc) 3.197 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/*
* This file is part of HXMPP.
* Copyright (c)2009 http://www.disktree.net
*
* HXMPP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HXMPP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with HXMPP. If not, see <http://www.gnu.org/licenses/>.
*/
package xmpp;

using xmpp.XMLUtil;

/**
<a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of Binary</a><br/>
*/
class BOB {

public static var XMLNS = 'urn:xmpp:bob';

/** Content ID */
public var cid : String;
/** Content type. fe: image/png */
public var type : String;
/** Suggested period for caching time of data, 0 for 'no cache' */
public var max_age : Int;
/** File type */
public var data : String;

public function new( cid : String, ?type : String, ?max_age : Int = -1, ?data : String ) {
this.cid = cid;
this.type = type;
this.max_age = max_age;
this.data = data;
}

public function toXml() : Xml {
var x = Xml.createElement( "data" );
x.ns( XMLNS );
x.set( "cid", cid );
x.set( "type", type );
if( max_age >= 0 ) x.set( "max-age", Std.string( max_age ) );
if( data != null ) x.addChild( Xml.createPCData( data ) );
return x;
}

public static function parse( x : Xml ) : xmpp.BOB {
var b = new xmpp.BOB( x.get( "cid" ), x.get( "type" ), Std.parseInt( x.get( "max-age" ) ) );
if( x.firstChild() != null )
b.data = x.firstChild().toString();
return b;
}

/**
Parses cids ('algo+hash@bob.xmpp.org') from the given string.
*/
public static function parseCID( t : String ) : { algo : String, hash : String } {
//TODO return : Array<{algo:String,hash:String}>
var algo : String = null;
var hash : String = null;
~/cid:(.*?)\+(.*?)@bob\.xmpp\.org/.customReplace( t, function(r) {
algo = r.matched( 1 );
hash = r.matched( 2 );
return null;
} );
return { algo : algo, hash : hash };
}

/**
*/
public static function getCIDParts( cid : String ) : Array<String> {
var i1 = cid.indexOf( "+" );
var i2 = cid.lastIndexOf( "@bob.xmpp.org" );
return if( i1 == -1 || i2 == -1 ) null;
else [ cid.substr( 0, i1 ), cid.substr( i1+1, i2-i1-1 ) ];
}

/**
*/
public static function createCID( algo : String, hash : String ) : String {
#if (neko||cpp)
var b = new StringBuf();
b.add( algo );
b.add( "+" );
b.add( hash );
b.add( "@bob.xmpp.org" );
return b.toString();
#else
return algo+"+"+hash+"@bob.xmpp.org";
#end
}

/*
Create BOB data elements to use for direct messages.
public function createElement( bytes : haxe.io.Bytes, type : String, maxage : Int, data : String ) {
var hash : String = crypt.SHA1.encode( bytes.toString() );
return new xmpp.BOB( xmpp.BOB.createCID( "sha1", hash ), type, maxage, data );
}
*/

}
Something went wrong with that request. Please try again.