<!--{{{-->
<link rel='alternate' type='application/rss+xml' title='RSS' href='index.xml' />
<!--}}}-->
Background: #fff
Foreground: #000
PrimaryPale: #8cf
PrimaryLight: #18f
PrimaryMid: #04b
PrimaryDark: #014
SecondaryPale: #ffc
SecondaryLight: #fe8
SecondaryMid: #db4
SecondaryDark: #841
TertiaryPale: #eee
TertiaryLight: #ccc
TertiaryMid: #999
TertiaryDark: #666
Error: #f88
/*{{{*/
body {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];}

a {color:[[ColorPalette::PrimaryMid]];}
a:hover {background-color:[[ColorPalette::PrimaryMid]]; color:[[ColorPalette::Background]];}
a img {border:0;}

h1,h2,h3,h4,h5,h6 {color:[[ColorPalette::SecondaryDark]]; background:transparent;}
h1 {border-bottom:2px solid [[ColorPalette::TertiaryLight]];}
h2,h3 {border-bottom:1px solid [[ColorPalette::TertiaryLight]];}

.button {color:[[ColorPalette::PrimaryDark]]; border:1px solid [[ColorPalette::Background]];}
.button:hover {color:[[ColorPalette::PrimaryDark]]; background:[[ColorPalette::SecondaryLight]]; border-color:[[ColorPalette::SecondaryMid]];}
.button:active {color:[[ColorPalette::Background]]; background:[[ColorPalette::SecondaryMid]]; border:1px solid [[ColorPalette::SecondaryDark]];}

.header {background:[[ColorPalette::PrimaryMid]];}
.headerShadow {color:[[ColorPalette::Foreground]];}
.headerShadow a {font-weight:normal; color:[[ColorPalette::Foreground]];}
.headerForeground {color:[[ColorPalette::Background]];}
.headerForeground a {font-weight:normal; color:[[ColorPalette::PrimaryPale]];}

.tabSelected{color:[[ColorPalette::PrimaryDark]];
	background:[[ColorPalette::TertiaryPale]];
	border-left:1px solid [[ColorPalette::TertiaryLight]];
	border-top:1px solid [[ColorPalette::TertiaryLight]];
	border-right:1px solid [[ColorPalette::TertiaryLight]];
}
.tabUnselected {color:[[ColorPalette::Background]]; background:[[ColorPalette::TertiaryMid]];}
.tabContents {color:[[ColorPalette::PrimaryDark]]; background:[[ColorPalette::TertiaryPale]]; border:1px solid [[ColorPalette::TertiaryLight]];}
.tabContents .button {border:0;}

#sidebar {}
#sidebarOptions input {border:1px solid [[ColorPalette::PrimaryMid]];}
#sidebarOptions .sliderPanel {background:[[ColorPalette::PrimaryPale]];}
#sidebarOptions .sliderPanel a {border:none;color:[[ColorPalette::PrimaryMid]];}
#sidebarOptions .sliderPanel a:hover {color:[[ColorPalette::Background]]; background:[[ColorPalette::PrimaryMid]];}
#sidebarOptions .sliderPanel a:active {color:[[ColorPalette::PrimaryMid]]; background:[[ColorPalette::Background]];}

.wizard {background:[[ColorPalette::PrimaryPale]]; border:1px solid [[ColorPalette::PrimaryMid]];}
.wizard h1 {color:[[ColorPalette::PrimaryDark]]; border:none;}
.wizard h2 {color:[[ColorPalette::Foreground]]; border:none;}
.wizardStep {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];
	border:1px solid [[ColorPalette::PrimaryMid]];}
.wizardStep.wizardStepDone {background:[[ColorPalette::TertiaryLight]];}
.wizardFooter {background:[[ColorPalette::PrimaryPale]];}
.wizardFooter .status {background:[[ColorPalette::PrimaryDark]]; color:[[ColorPalette::Background]];}
.wizard .button {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::SecondaryLight]]; border: 1px solid;
	border-color:[[ColorPalette::SecondaryPale]] [[ColorPalette::SecondaryDark]] [[ColorPalette::SecondaryDark]] [[ColorPalette::SecondaryPale]];}
.wizard .button:hover {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::Background]];}
.wizard .button:active {color:[[ColorPalette::Background]]; background:[[ColorPalette::Foreground]]; border: 1px solid;
	border-color:[[ColorPalette::PrimaryDark]] [[ColorPalette::PrimaryPale]] [[ColorPalette::PrimaryPale]] [[ColorPalette::PrimaryDark]];}

.wizard .notChanged {background:transparent;}
.wizard .changedLocally {background:#80ff80;}
.wizard .changedServer {background:#8080ff;}
.wizard .changedBoth {background:#ff8080;}
.wizard .notFound {background:#ffff80;}
.wizard .putToServer {background:#ff80ff;}
.wizard .gotFromServer {background:#80ffff;}

#messageArea {border:1px solid [[ColorPalette::SecondaryMid]]; background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]];}
#messageArea .button {color:[[ColorPalette::PrimaryMid]]; background:[[ColorPalette::SecondaryPale]]; border:none;}

.popupTiddler {background:[[ColorPalette::TertiaryPale]]; border:2px solid [[ColorPalette::TertiaryMid]];}

.popup {background:[[ColorPalette::TertiaryPale]]; color:[[ColorPalette::TertiaryDark]]; border-left:1px solid [[ColorPalette::TertiaryMid]]; border-top:1px solid [[ColorPalette::TertiaryMid]]; border-right:2px solid [[ColorPalette::TertiaryDark]]; border-bottom:2px solid [[ColorPalette::TertiaryDark]];}
.popup hr {color:[[ColorPalette::PrimaryDark]]; background:[[ColorPalette::PrimaryDark]]; border-bottom:1px;}
.popup li.disabled {color:[[ColorPalette::TertiaryMid]];}
.popup li a, .popup li a:visited {color:[[ColorPalette::Foreground]]; border: none;}
.popup li a:hover {background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]]; border: none;}
.popup li a:active {background:[[ColorPalette::SecondaryPale]]; color:[[ColorPalette::Foreground]]; border: none;}
.popupHighlight {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];}
.listBreak div {border-bottom:1px solid [[ColorPalette::TertiaryDark]];}

.tiddler .defaultCommand {font-weight:bold;}

.shadow .title {color:[[ColorPalette::TertiaryDark]];}

.title {color:[[ColorPalette::SecondaryDark]];}
.subtitle {color:[[ColorPalette::TertiaryDark]];}

.toolbar {color:[[ColorPalette::PrimaryMid]];}
.toolbar a {color:[[ColorPalette::TertiaryLight]];}
.selected .toolbar a {color:[[ColorPalette::TertiaryMid]];}
.selected .toolbar a:hover {color:[[ColorPalette::Foreground]];}

.tagging, .tagged {border:1px solid [[ColorPalette::TertiaryPale]]; background-color:[[ColorPalette::TertiaryPale]];}
.selected .tagging, .selected .tagged {background-color:[[ColorPalette::TertiaryLight]]; border:1px solid [[ColorPalette::TertiaryMid]];}
.tagging .listTitle, .tagged .listTitle {color:[[ColorPalette::PrimaryDark]];}
.tagging .button, .tagged .button {border:none;}

.footer {color:[[ColorPalette::TertiaryLight]];}
.selected .footer {color:[[ColorPalette::TertiaryMid]];}

.sparkline {background:[[ColorPalette::PrimaryPale]]; border:0;}
.sparktick {background:[[ColorPalette::PrimaryDark]];}

.error, .errorButton {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::Error]];}
.warning {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::SecondaryPale]];}
.lowlight {background:[[ColorPalette::TertiaryLight]];}

.zoomer {background:none; color:[[ColorPalette::TertiaryMid]]; border:3px solid [[ColorPalette::TertiaryMid]];}

.imageLink, #displayArea .imageLink {background:transparent;}

.annotation {background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]]; border:2px solid [[ColorPalette::SecondaryMid]];}

.viewer .listTitle {list-style-type:none; margin-left:-2em;}
.viewer .button {border:1px solid [[ColorPalette::SecondaryMid]];}
.viewer blockquote {border-left:3px solid [[ColorPalette::TertiaryDark]];}

.viewer table, table.twtable {border:2px solid [[ColorPalette::TertiaryDark]];}
.viewer th, .viewer thead td, .twtable th, .twtable thead td {background:[[ColorPalette::SecondaryMid]]; border:1px solid [[ColorPalette::TertiaryDark]]; color:[[ColorPalette::Background]];}
.viewer td, .viewer tr, .twtable td, .twtable tr {border:1px solid [[ColorPalette::TertiaryDark]];}

.viewer pre {border:1px solid [[ColorPalette::SecondaryLight]]; background:[[ColorPalette::SecondaryPale]];}
.viewer code {color:[[ColorPalette::SecondaryDark]];}
.viewer hr {border:0; border-top:dashed 1px [[ColorPalette::TertiaryDark]]; color:[[ColorPalette::TertiaryDark]];}

.highlight, .marked {background:[[ColorPalette::SecondaryLight]];}

.editor input {border:1px solid [[ColorPalette::PrimaryMid]];}
.editor textarea {border:1px solid [[ColorPalette::PrimaryMid]]; width:100%;}
.editorFooter {color:[[ColorPalette::TertiaryMid]];}
.readOnly {background:[[ColorPalette::TertiaryPale]];}

#backstageArea {background:[[ColorPalette::Foreground]]; color:[[ColorPalette::TertiaryMid]];}
#backstageArea a {background:[[ColorPalette::Foreground]]; color:[[ColorPalette::Background]]; border:none;}
#backstageArea a:hover {background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]]; }
#backstageArea a.backstageSelTab {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];}
#backstageButton a {background:none; color:[[ColorPalette::Background]]; border:none;}
#backstageButton a:hover {background:[[ColorPalette::Foreground]]; color:[[ColorPalette::Background]]; border:none;}
#backstagePanel {background:[[ColorPalette::Background]]; border-color: [[ColorPalette::Background]] [[ColorPalette::TertiaryDark]] [[ColorPalette::TertiaryDark]] [[ColorPalette::TertiaryDark]];}
.backstagePanelFooter .button {border:none; color:[[ColorPalette::Background]];}
.backstagePanelFooter .button:hover {color:[[ColorPalette::Foreground]];}
#backstageCloak {background:[[ColorPalette::Foreground]]; opacity:0.6; filter:'alpha(opacity=60)';}
/*}}}*/
/*{{{*/
* html .tiddler {height:1%;}

body {font-size:.75em; font-family:arial,helvetica; margin:0; padding:0;}

h1,h2,h3,h4,h5,h6 {font-weight:bold; text-decoration:none;}
h1,h2,h3 {padding-bottom:1px; margin-top:1.2em;margin-bottom:0.3em;}
h4,h5,h6 {margin-top:1em;}
h1 {font-size:1.35em;}
h2 {font-size:1.25em;}
h3 {font-size:1.1em;}
h4 {font-size:1em;}
h5 {font-size:.9em;}

hr {height:1px;}

a {text-decoration:none;}

dt {font-weight:bold;}

ol {list-style-type:decimal;}
ol ol {list-style-type:lower-alpha;}
ol ol ol {list-style-type:lower-roman;}
ol ol ol ol {list-style-type:decimal;}
ol ol ol ol ol {list-style-type:lower-alpha;}
ol ol ol ol ol ol {list-style-type:lower-roman;}
ol ol ol ol ol ol ol {list-style-type:decimal;}

.txtOptionInput {width:11em;}

#contentWrapper .chkOptionInput {border:0;}

.externalLink {text-decoration:underline;}

.indent {margin-left:3em;}
.outdent {margin-left:3em; text-indent:-3em;}
code.escaped {white-space:nowrap;}

.tiddlyLinkExisting {font-weight:bold;}
.tiddlyLinkNonExisting {font-style:italic;}

/* the 'a' is required for IE, otherwise it renders the whole tiddler in bold */
a.tiddlyLinkNonExisting.shadow {font-weight:bold;}

#mainMenu .tiddlyLinkExisting,
	#mainMenu .tiddlyLinkNonExisting,
	#sidebarTabs .tiddlyLinkNonExisting {font-weight:normal; font-style:normal;}
#sidebarTabs .tiddlyLinkExisting {font-weight:bold; font-style:normal;}

.header {position:relative;}
.header a:hover {background:transparent;}
.headerShadow {position:relative; padding:4.5em 0 1em 1em; left:-1px; top:-1px;}
.headerForeground {position:absolute; padding:4.5em 0 1em 1em; left:0px; top:0px;}

.siteTitle {font-size:3em;}
.siteSubtitle {font-size:1.2em;}

#mainMenu {position:absolute; left:0; width:10em; text-align:right; line-height:1.6em; padding:1.5em 0.5em 0.5em 0.5em; font-size:1.1em;}

#sidebar {position:absolute; right:3px; width:16em; font-size:.9em;}
#sidebarOptions {padding-top:0.3em;}
#sidebarOptions a {margin:0 0.2em; padding:0.2em 0.3em; display:block;}
#sidebarOptions input {margin:0.4em 0.5em;}
#sidebarOptions .sliderPanel {margin-left:1em; padding:0.5em; font-size:.85em;}
#sidebarOptions .sliderPanel a {font-weight:bold; display:inline; padding:0;}
#sidebarOptions .sliderPanel input {margin:0 0 0.3em 0;}
#sidebarTabs .tabContents {width:15em; overflow:hidden;}

.wizard {padding:0.1em 1em 0 2em;}
.wizard h1 {font-size:2em; font-weight:bold; background:none; padding:0; margin:0.4em 0 0.2em;}
.wizard h2 {font-size:1.2em; font-weight:bold; background:none; padding:0; margin:0.4em 0 0.2em;}
.wizardStep {padding:1em 1em 1em 1em;}
.wizard .button {margin:0.5em 0 0; font-size:1.2em;}
.wizardFooter {padding:0.8em 0.4em 0.8em 0;}
.wizardFooter .status {padding:0 0.4em; margin-left:1em;}
.wizard .button {padding:0.1em 0.2em;}

#messageArea {position:fixed; top:2em; right:0; margin:0.5em; padding:0.5em; z-index:2000; _position:absolute;}
.messageToolbar {display:block; text-align:right; padding:0.2em;}
#messageArea a {text-decoration:underline;}

.tiddlerPopupButton {padding:0.2em;}
.popupTiddler {position: absolute; z-index:300; padding:1em; margin:0;}

.popup {position:absolute; z-index:300; font-size:.9em; padding:0; list-style:none; margin:0;}
.popup .popupMessage {padding:0.4em;}
.popup hr {display:block; height:1px; width:auto; padding:0; margin:0.2em 0;}
.popup li.disabled {padding:0.4em;}
.popup li a {display:block; padding:0.4em; font-weight:normal; cursor:pointer;}
.listBreak {font-size:1px; line-height:1px;}
.listBreak div {margin:2px 0;}

.tabset {padding:1em 0 0 0.5em;}
.tab {margin:0 0 0 0.25em; padding:2px;}
.tabContents {padding:0.5em;}
.tabContents ul, .tabContents ol {margin:0; padding:0;}
.txtMainTab .tabContents li {list-style:none;}
.tabContents li.listLink { margin-left:.75em;}

#contentWrapper {display:block;}
#splashScreen {display:none;}

#displayArea {margin:1em 17em 0 14em;}

.toolbar {text-align:right; font-size:.9em;}

.tiddler {padding:1em 1em 0;}

.missing .viewer,.missing .title {font-style:italic;}

.title {font-size:1.6em; font-weight:bold;}

.missing .subtitle {display:none;}
.subtitle {font-size:1.1em;}

.tiddler .button {padding:0.2em 0.4em;}

.tagging {margin:0.5em 0.5em 0.5em 0; float:left; display:none;}
.isTag .tagging {display:block;}
.tagged {margin:0.5em; float:right;}
.tagging, .tagged {font-size:0.9em; padding:0.25em;}
.tagging ul, .tagged ul {list-style:none; margin:0.25em; padding:0;}
.tagClear {clear:both;}

.footer {font-size:.9em;}
.footer li {display:inline;}

.annotation {padding:0.5em; margin:0.5em;}

* html .viewer pre {width:99%; padding:0 0 1em 0;}
.viewer {line-height:1.4em; padding-top:0.5em;}
.viewer .button {margin:0 0.25em; padding:0 0.25em;}
.viewer blockquote {line-height:1.5em; padding-left:0.8em;margin-left:2.5em;}
.viewer ul, .viewer ol {margin-left:0.5em; padding-left:1.5em;}

.viewer table, table.twtable {border-collapse:collapse; margin:0.8em 1.0em;}
.viewer th, .viewer td, .viewer tr,.viewer caption,.twtable th, .twtable td, .twtable tr,.twtable caption {padding:3px;}
table.listView {font-size:0.85em; margin:0.8em 1.0em;}
table.listView th, table.listView td, table.listView tr {padding:0px 3px 0px 3px;}

.viewer pre {padding:0.5em; margin-left:0.5em; font-size:1.2em; line-height:1.4em; overflow:auto;}
.viewer code {font-size:1.2em; line-height:1.4em;}

.editor {font-size:1.1em;}
.editor input, .editor textarea {display:block; width:100%; font:inherit;}
.editorFooter {padding:0.25em 0; font-size:.9em;}
.editorFooter .button {padding-top:0px; padding-bottom:0px;}

.fieldsetFix {border:0; padding:0; margin:1px 0px;}

.sparkline {line-height:1em;}
.sparktick {outline:0;}

.zoomer {font-size:1.1em; position:absolute; overflow:hidden;}
.zoomer div {padding:1em;}

* html #backstage {width:99%;}
* html #backstageArea {width:99%;}
#backstageArea {display:none; position:relative; overflow: hidden; z-index:150; padding:0.3em 0.5em;}
#backstageToolbar {position:relative;}
#backstageArea a {font-weight:bold; margin-left:0.5em; padding:0.3em 0.5em;}
#backstageButton {display:none; position:absolute; z-index:175; top:0; right:0;}
#backstageButton a {padding:0.1em 0.4em; margin:0.1em;}
#backstage {position:relative; width:100%; z-index:50;}
#backstagePanel {display:none; z-index:100; position:absolute; width:90%; margin-left:3em; padding:1em;}
.backstagePanelFooter {padding-top:0.2em; float:right;}
.backstagePanelFooter a {padding:0.2em 0.4em;}
#backstageCloak {display:none; z-index:20; position:absolute; width:100%; height:100px;}

.whenBackstage {display:none;}
.backstageVisible .whenBackstage {display:block;}
/*}}}*/
/***
StyleSheet for use when a translation requires any css style changes.
This StyleSheet can be used directly by languages such as Chinese, Japanese and Korean which need larger font sizes.
***/
/*{{{*/
body {font-size:0.8em;}
#sidebarOptions {font-size:1.05em;}
#sidebarOptions a {font-style:normal;}
#sidebarOptions .sliderPanel {font-size:0.95em;}
.subtitle {font-size:0.8em;}
.viewer table.listView {font-size:0.95em;}
/*}}}*/
/*{{{*/
@media print {
#mainMenu, #sidebar, #messageArea, .toolbar, #backstageButton, #backstageArea {display: none !important;}
#displayArea {margin: 1em 1em 0em;}
noscript {display:none;} /* Fixes a feature in Firefox 1.5.0.2 where print preview displays the noscript content */
}
/*}}}*/
<!--{{{-->
<div class='header' macro='gradient vert [[ColorPalette::PrimaryLight]] [[ColorPalette::PrimaryMid]]'>
<div class='headerShadow'>
<span class='siteTitle' refresh='content' tiddler='SiteTitle'></span>&nbsp;
<span class='siteSubtitle' refresh='content' tiddler='SiteSubtitle'></span>
</div>
<div class='headerForeground'>
<span class='siteTitle' refresh='content' tiddler='SiteTitle'></span>&nbsp;
<span class='siteSubtitle' refresh='content' tiddler='SiteSubtitle'></span>
</div>
</div>
<div id='mainMenu' refresh='content' tiddler='MainMenu'></div>
<div id='sidebar'>
<div id='sidebarOptions' refresh='content' tiddler='SideBarOptions'></div>
<div id='sidebarTabs' refresh='content' force='true' tiddler='SideBarTabs'></div>
</div>
<div id='displayArea'>
<div id='messageArea'></div>
<div id='tiddlerDisplay'></div>
</div>
<!--}}}-->
<!--{{{-->
<div class='toolbar' macro='toolbar [[ToolbarCommands::ViewToolbar]]'></div>
<div class='title' macro='view title'></div>
<div class='subtitle'><span macro='view modifier link'></span>, <span macro='view modified date'></span> (<span macro='message views.wikified.createdPrompt'></span> <span macro='view created date'></span>)</div>
<div class='tagging' macro='tagging'></div>
<div class='tagged' macro='tags'></div>
<div class='viewer' macro='view text wikified'></div>
<div class='tagClear'></div>
<!--}}}-->
<!--{{{-->
<div class='toolbar' macro='toolbar [[ToolbarCommands::EditToolbar]]'></div>
<div class='title' macro='view title'></div>
<div class='editor' macro='edit title'></div>
<div macro='annotations'></div>
<div class='editor' macro='edit text'></div>
<div class='editor' macro='edit tags'></div><div class='editorFooter'><span macro='message views.editor.tagPrompt'></span><span macro='tagChooser excludeLists'></span></div>
<!--}}}-->
To get started with this blank [[TiddlyWiki]], you'll need to modify the following tiddlers:
* [[SiteTitle]] & [[SiteSubtitle]]: The title and subtitle of the site, as shown above (after saving, they will also appear in the browser title bar)
* [[MainMenu]]: The menu (usually on the left)
* [[DefaultTiddlers]]: Contains the names of the tiddlers that you want to appear when the TiddlyWiki is opened
You'll also need to enter your username for signing your edits: <<option txtUserName>>
These [[InterfaceOptions]] for customising [[TiddlyWiki]] are saved in your browser

Your username for signing your edits. Write it as a [[WikiWord]] (eg [[JoeBloggs]])

<<option txtUserName>>
<<option chkSaveBackups>> [[SaveBackups]]
<<option chkAutoSave>> [[AutoSave]]
<<option chkRegExpSearch>> [[RegExpSearch]]
<<option chkCaseSensitiveSearch>> [[CaseSensitiveSearch]]
<<option chkAnimate>> [[EnableAnimations]]

----
Also see [[AdvancedOptions]]
<<importTiddlers>>
[[XMPP Wiki]]
/***
|''Name:''|LoadRemoteFileThroughProxy (previous LoadRemoteFileHijack)|
|''Description:''|When the TiddlyWiki file is located on the web (view over http) the content of [[SiteProxy]] tiddler is added in front of the file url. If [[SiteProxy]] does not exist "/proxy/" is added. |
|''Version:''|1.1.0|
|''Date:''|mar 17, 2007|
|''Source:''|http://tiddlywiki.bidix.info/#LoadRemoteFileHijack|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0|
***/
//{{{
version.extensions.LoadRemoteFileThroughProxy = {
 major: 1, minor: 1, revision: 0, 
 date: new Date("mar 17, 2007"), 
 source: "http://tiddlywiki.bidix.info/#LoadRemoteFileThroughProxy"};

if (!window.bidix) window.bidix = {}; // bidix namespace
if (!bidix.core) bidix.core = {};

bidix.core.loadRemoteFile = loadRemoteFile;
loadRemoteFile = function(url,callback,params)
{
 if ((document.location.toString().substr(0,4) == "http") && (url.substr(0,4) == "http")){ 
 url = store.getTiddlerText("SiteProxy", "/proxy/") + url;
 }
 return bidix.core.loadRemoteFile(url,callback,params);
}
//}}}
|''xmppWiki''| <<bosh_rosterLine width:35>> |
<<tiddler TspotOptions>>These [[InterfaceOptions]] for customising [[TiddlyWiki]] are saved in your browser

Your username for signing your edits. Write it as a [[WikiWord]] (eg [[JoeBloggs]])

<<option txtUserName>>
<<option chkSaveBackups>> [[SaveBackups]]
<<option chkAutoSave>> [[AutoSave]]
<<option chkRegExpSearch>> [[RegExpSearch]]
<<option chkCaseSensitiveSearch>> [[CaseSensitiveSearch]]
<<option chkAnimate>> [[EnableAnimations]]

----
Bosh Plugin
<<option txtXmppBoshServerUrl>>
<<bosh>>
<html>
<input type='button' value='show Dashboard' onClick='config.extensions.boshPlugin.pubsub.displayNode(config.extensions.boshPlugin.jid+"/urn:xmpp:microblog:0")'>
<input type='button' value='configure Dashboard' onClick='config.extensions.boshPlugin.pubsub.configureNode(config.extensions.boshPlugin.jid+"/urn:xmpp:microblog:0")'>
<input type='button' value='create Dashboard' onClick='config.extensions.boshPlugin.pubsub.configureNode(config.extensions.boshPlugin.jid+"/urn:xmpp:microblog:0")'>
</html>
<<bosh_Subscriber>>
----
Also see [[AdvancedOptions]]
<!--{{{-->
<!--<span macro='bosh_rosterLine width:50'></span>-->
<div id='topMenu' refresh='content' tiddler='MainMenu'></div>
<div id='sidebar'>
<div id='sidebarOptions' refresh='content' tiddler='SideBarOptions'></div>
<div id='sidebarTabs' refresh='content' force='true' tiddler='SideBarTabs'></div>
</div>
<div id='chatArea'>
<div id='chatAreaTabs'></div>
<div id='chatAreaTabsChoice'></div>
</div>
<div id='displayArea'>
<div id='messageArea'></div>
<div id='tiddlerDisplayHeader'><div id='nodePublishForm'></div></div>
<div id='tiddlerDisplay'></div>
</div>
<!--}}}-->
/***
|''Name:''|PasswordOptionPlugin|
|''Description:''|Extends TiddlyWiki options with non encrypted password option.|
|''Version:''|1.0.2|
|''Date:''|Apr 19, 2007|
|''Source:''|http://tiddlywiki.bidix.info/#PasswordOptionPlugin|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0 (Beta 5)|
***/
//{{{
version.extensions.PasswordOptionPlugin = {
	major: 1, minor: 0, revision: 2, 
	date: new Date("Apr 19, 2007"),
	source: 'http://tiddlywiki.bidix.info/#PasswordOptionPlugin',
	author: 'BidiX (BidiX (at) bidix (dot) info',
	license: '[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D]]',
	coreVersion: '2.2.0 (Beta 5)'
};

config.macros.option.passwordCheckboxLabel = "Save this password on this computer";
config.macros.option.passwordInputType = "password"; // password | text
setStylesheet(".pasOptionInput {width: 11em;}\n","passwordInputTypeStyle");

merge(config.macros.option.types, {
	'pas': {
		elementType: "input",
		valueField: "value",
		eventName: "onkeyup",
		className: "pasOptionInput",
		typeValue: config.macros.option.passwordInputType,
		create: function(place,type,opt,className,desc) {
			// password field
			config.macros.option.genericCreate(place,'pas',opt,className,desc);
			// checkbox linked with this password "save this password on this computer"
			config.macros.option.genericCreate(place,'chk','chk'+opt,className,desc);			
			// text savePasswordCheckboxLabel
			place.appendChild(document.createTextNode(config.macros.option.passwordCheckboxLabel));
		},
		onChange: config.macros.option.genericOnChange
	}
});

merge(config.optionHandlers['chk'], {
	get: function(name) {
		// is there an option linked with this chk ?
		var opt = name.substr(3);
		if (config.options[opt]) 
			saveOptionCookie(opt);
		return config.options[name] ? "true" : "false";
	}
});

merge(config.optionHandlers, {
	'pas': {
 		get: function(name) {
			if (config.options["chk"+name]) {
				return encodeCookie(config.options[name].toString());
			} else {
				return "";
			}
		},
		set: function(name,value) {config.options[name] = decodeCookie(value);}
	}
});

// need to reload options to load passwordOptions
loadOptionsCookie();

/*
if (!config.options['pasPassword'])
	config.options['pasPassword'] = '';

merge(config.optionsDesc,{
		pasPassword: "Test password"
	});
*/
//}}}
<<search>><<closeAll>><<permaview>><<newTiddler>><<newTiddler label:'new article' template:blogEditTemplate>><<newJournal "DD MMM YYYY" "journal">><<saveChanges>><<tiddler TspotSidebar>><<slider chkSliderOptionsPanel OptionsPanel "options »" "Change TiddlyWiki advanced options">>
<<tiddler ToggleLeftSidebar>><<tiddler ToggleRightSidebar>> My TiddlyWiki
#chatArea {width:40%;position:fixed;background-color:transparent; right:0; bottom:0;  line-height:1.6em; font-size:1.1em;}
#chatAreaTabs {background-color:white;display:none;border:solid;padding: 10px 10px 10px 10px;}
#tiddlerDisplayHeader {display:none;align:start;}
#tiddlerDisplay {padding-left:5%;width:80%}


/*{{{*/
/*Blackicity Theme for TiddlyWiki*/
/*Design and CSS by Saq Imtiaz*/
/*Version 1.0*/
/*}}}*/
/*{{{*/
body{	font-family: "Neue Helvetica", Helvetica, "Lucida Grande", Verdana, sans-serif;
	background-color: #fff;
	color: #333;}

#topMenu {position:relative; background:#282826; padding:10px; color:#fff;font-family:'Lucida Grande', Verdana, Sans-Serif;}
#topMenu br {display:none;}

#topMenu a{			color: #999;
			padding: 0px 8px 0px 8px;
			border-right: 1px solid #444;}
#topMenu a:hover {color:#fff; background:transparent;}

#displayArea {margin-left:1em; margin-bottom:2em; margin-top:0.5em;}


a, a:hover{
color:#333;
text-decoration: none;   background:transparent; 
}

.viewer a, .viewer a:hover {border-bottom:1px dotted #333; font-weight:bold;}


.viewer .button, .editorFooter .button{
color: #333;
border: 1px solid #333;
}

.viewer .button:hover,
.editorFooter .button:hover, .viewer .button:active, .viewer .highlight,.editorFooter .button:active, .editorFooter .highlight{
color: #fff;
background: #333;
border-color: #333;
}

.tiddler .viewer {line-height:1.45em;}
.title {color:#222; border-bottom:1px solid#222; font-family:'Lucida Grande', Verdana, Sans-Serif; font-size:1.5em;}
.subtitle, .subtitle a { color: #999999; font-size: 0.95em;margin:0.2em;}
.shadow .title{color:#999;}

.toolbar {font-size:90%;}
.selected .toolbar a {color:#999999;}
.selected .toolbar a:hover {color:#333; background:transparent;border:1px solid #fff;}

.toolbar .button:hover, .toolbar .highlight, .toolbar .marked, .toolbar a.button:active{color:#333; background:transparent;border:1px solid #fff;}

/***
!Sidebar
***/
#sidebar { margin-bottom:2em !important; margin-bottom:1em; right:0;
}

/***
!SidebarOptions
***/
#sidebarOptions { padding-top:2em;background:#f3f3f3;padding-left:0.5em;}

#sidebarOptions a {
			color:#333;
                        background:#f3f3f3;
                        border:1px solid #f3f3f3;
			text-decoration: none;
}

#sidebarOptions	a:hover, #sidebarOptions a:active {
			color:#222;
			background-color:#fff;border:1px solid #fff;
		}

#sidebarOptions input {border:1px solid #ccc; }

#sidebarOptions .sliderPanel {
	background: #f3f3f3; 	font-size: .9em;
}

#sidebarOptions .sliderPanel input {border:1px solid #999;}
#sidebarOptions .sliderPanel .txtOptionInput {border:1px solid #999;width:9em;}

#sidebarOptions .sliderPanel a {font-weight:normal; color:#555;background-color: #f3f3f3; border-bottom:1px dotted #333;}


#sidebarOptions .sliderPanel a:hover {
color:#111;
background-color: #f3f3f3;
border:none;
border-bottom:1px dotted #111;
}
/***
!SidebarTabs
***/
 .listTitle {color:#222;}
#sidebarTabs {background:#f3f3f3;}

#sidebarTabs .tabContents {background:#cfcfcf;}

#sidebarTabs .tabUnselected:hover {color:#999;}

#sidebarTabs .tabSelected{background:#cfcfcf;}

#sidebarTabs .tabContents .tiddlyLink, #sidebarTabs .tabContents .button{color:#666;}
#sidebarTabs .tabContents .tiddlyLink:hover,#sidebarTabs .tabContents .button:hover{color:#222;background:transparent; text-decoration:none;border:none;}

#sidebarTabs .tabContents .button:hover, #sidebarTabs .tabContents .highlight, #sidebarTabs .tabContents .marked, #sidebarTabs .tabContents a.button:active{color:#222;background:transparent;}

#sidebarTabs .txtMoreTab .tabSelected,
#sidebarTabs .txtMoreTab .tab:hover,
#sidebarTabs .txtMoreTab .tabContents{
 color: #111;
 background: #f3f3f3; border:1px solid #f3f3f3;
}

#sidebarTabs .txtMoreTab .tabUnselected {
 color: #555;
 background: #AFAFAF;
}



/***
!Tabs
***/
.tabSelected{color:#fefefe; background:#999; padding-bottom:1px;}
 .tabSelected, .tabSelected:hover {
 color: #111;
 background: #fefefe;
 border: solid 1px #cfcfcf;
}

 .tabUnselected {
 color: #999;
 background: #eee;
 border: solid 1px #cfcfcf;
 padding-bottom:1px;
}
.tabUnselected:hover {text-decoration:none; border:1px solid #cfcfcf;}
.tabContents {background:#fefefe;}





.tagging, .tagged {
border: 1px solid #eee;
background-color: #F7F7F7;
}

.selected .tagging, .selected .tagged {
background-color: #f3f3f3;
border: 1px solid #ccc;
}

.tagging .listTitle, .tagged .listTitle {
color: #bbb;
}

.selected .tagging .listTitle, .selected .tagged .listTitle {
color: #333;
}

.tagging .button, .tagged .button {
color:#ccc;
}
.selected .tagging .button, .selected .tagged .button {
color:#aaa;
}

.highlight, .marked {background:transparent; color:#111; border:none; text-decoration:underline;}

.tagging .button:hover, .tagged .button:hover, .tagging .button:active, .tagged .button:active {
border: none; background:transparent; text-decoration:underline; color:#333;
}



.popup {
background: #cfcfcf;
border: 1px solid #333;
}

.popup li.disabled {
color: #000;
}

.popup li a, .popup li a:visited {
color: #555;
border: none;
}

.popup li a:hover {
background: #f3f3f3;
color: #555;
border: none;
}



#messageArea {

border: 1px dotted #282826;
background: #F3F3F3;
color: #333;
font-size:90%;
}

#messageArea a:hover { background:#f5f5f5; border:none;}


#messageArea .button{
color: #333;
border: 1px solid #282826;
}

#messageArea .button:hover {
color: #fff;
background: #282826;
border-color: #282826;
}






.tiddler {padding-bottom:10px;}

.viewer blockquote {
border-left: 5px solid #282826;
}

.viewer table, .viewer td {
border: 1px solid #282826;
}

.viewer th, thead td {
background: #282826;
border: 1px solid #282826;
color: #fff;
}
.viewer pre {
border: 1px solid #ccc;
background: #f5f5f5;
}

.viewer code {
color: #111; background:#f5f5f5;
}

.viewer hr {
border-top: dashed 1px #222; margin:0 1em;
}

.editor input {
border: 1px solid #ccc; margin-top:5px;
}

.editor textarea {
border: 1px solid #ccc;
}

h1,h2,h3,h4,h5 { color: #282826; background: transparent; padding-bottom:2px; font-family: Arial, Helvetica, sans-serif; }
h1 {font-size:18px;}
h2 {font-size:16px;}
h3 {font-size: 14px;}
/*}}}*/
/%
!info
|Name|ToggleLeftSidebar|
|Source|http://www.TiddlyTools.com/#ToggleLeftSidebar|
|Version|2.0.0|
|Author|Eric Shulman|
|License|http://www.TiddlyTools.com/#LegalStatements|
|~CoreVersion|2.1|
|Type|transclusion|
|Description|show/hide left sidebar (MainMenu)|
Usage
<<<
{{{
<<tiddler ToggleLeftSidebar>>
<<tiddler ToggleLeftSidebar with: label tooltip>>
}}}
Try it: <<tiddler ToggleLeftSidebar##show
	with: {{config.options.chkShowLeftSidebar?'◄':'►'}}>>
<<<
Configuration:
<<<
{{{
config.options.chkShowLeftSidebar (true)
config.options.txtToggleLeftSideBarLabelShow (►)
config.options.txtToggleLeftSideBarLabelHide (◄)
}}}
<<<
!end
!show
<<tiddler {{
	var co=config.options;
	if (co.chkShowLeftSidebar===undefined) co.chkShowLeftSidebar=true;
	var mm=document.getElementById('mainMenu');
	var da=document.getElementById('displayArea');
	if (mm) {
		mm.style.display=co.chkShowLeftSidebar?'block':'none';
		da.style.marginLeft=co.chkShowLeftSidebar?'':'1em';
	}
'';}}>><html><nowiki><a href='javascript:;' title="$2"
onmouseover="
	this.href='javascript:void(eval(decodeURIComponent(%22(function(){try{('
	+encodeURIComponent(encodeURIComponent(this.onclick))
	+')()}catch(e){alert(e.description?e.description:e.toString())}})()%22)))';"
onclick="
	var co=config.options;
	var opt='chkShowLeftSidebar';
	var show=co[opt]=!co[opt];
	var mm=document.getElementById('mainMenu');
	var da=document.getElementById('displayArea');
	if (mm) {
		mm.style.display=show?'block':'none';
		da.style.marginLeft=show?'':'1em';
	}
	saveOptionCookie(opt);
	var labelShow=co.txtToggleLeftSideBarLabelShow||'&#x25BA;';
	var labelHide=co.txtToggleLeftSideBarLabelHide||'&#x25C4;';
	if (this.innerHTML==labelShow||this.innerHTML==labelHide) 
		this.innerHTML=show?labelHide:labelShow;
	this.title=(show?'hide':'show')+' left sidebar';
	var sm=document.getElementById('storyMenu');
	if (sm) config.refreshers.content(sm);
	return false;
">$1</a></html>
!end
%/<<tiddler {{
	var src='ToggleLeftSidebar';
	src+(tiddler&&tiddler.title==src?'##info':'##show');
}} with: {{
	var co=config.options;
	var labelShow=co.txtToggleLeftSideBarLabelShow||'&#x25BA;';
	var labelHide=co.txtToggleLeftSideBarLabelHide||'&#x25C4;';
	'$1'!='$'+'1'?'$1':(co.chkShowLeftSidebar?labelHide:labelShow);
}} {{
	var tip=(config.options.chkShowLeftSidebar?'hide':'show')+' left sidebar';
	'$2'!='$'+'2'?'$2':tip;
}}>>
/%
!info
|Name|ToggleRightSidebar|
|Source|http://www.TiddlyTools.com/#ToggleRightSidebar|
|Version|2.0.0|
|Author|Eric Shulman|
|License|http://www.TiddlyTools.com/#LegalStatements|
|~CoreVersion|2.1|
|Type|transclusion|
|Description|show/hide right sidebar (SideBarOptions)|
Usage
<<<
{{{
<<tiddler ToggleRightSidebar>>
<<tiddler ToggleRightSidebar with: label tooltip>>
}}}
Try it: <<tiddler ToggleRightSidebar##show
	with: {{config.options.chkShowRightSidebar?'►':'◄'}}>>
<<<
Configuration:
<<<
copy/paste the following settings into a tiddler tagged with <<tag systemConfig>> and then modify the values to suit your preferences:
{{{
config.options.chkShowRightSidebar=true;
config.options.txtToggleRightSideBarLabelShow="◄";
config.options.txtToggleRightSideBarLabelHide="►";
}}}
<<<
!end
!show
<<tiddler {{
	var co=config.options;
	if (co.chkShowRightSidebar===undefined) co.chkShowRightSidebar=true;
	var sb=document.getElementById('sidebar');
	var da=document.getElementById('displayArea');
	if (sb) {
		sb.style.display=co.chkShowRightSidebar?'block':'none';
		da.style.marginRight=co.chkShowRightSidebar?'':'1em';
	}
'';}}>><html><nowiki><a href='javascript:;' title="$2"
onmouseover="
	this.href='javascript:void(eval(decodeURIComponent(%22(function(){try{('
	+encodeURIComponent(encodeURIComponent(this.onclick))
	+')()}catch(e){alert(e.description?e.description:e.toString())}})()%22)))';"
onclick="
	var co=config.options;
	var opt='chkShowRightSidebar';
	var show=co[opt]=!co[opt];
	var sb=document.getElementById('sidebar');
	var da=document.getElementById('displayArea');
	if (sb) {
		sb.style.display=show?'block':'none';
		da.style.marginRight=show?'':'1em';
	}
	saveOptionCookie(opt);
	var labelShow=co.txtToggleRightSideBarLabelShow||'&#x25C4;';
	var labelHide=co.txtToggleRightSideBarLabelHide||'&#x25BA;';
	if (this.innerHTML==labelShow||this.innerHTML==labelHide) 
		this.innerHTML=show?labelHide:labelShow;
	this.title=(show?'hide':'show')+' right sidebar';
	var sm=document.getElementById('storyMenu');
	if (sm) config.refreshers.content(sm);
	return false;
">$1</a></html>
!end
%/<<tiddler {{
	var src='ToggleRightSidebar';
	src+(tiddler&&tiddler.title==src?'##info':'##show');
}} with: {{
	var co=config.options;
	var labelShow=co.txtToggleRightSideBarLabelShow||'&#x25C4;';
	var labelHide=co.txtToggleRightSideBarLabelHide||'&#x25BA;';
	'$1'!='$'+'1'?'$1':(co.chkShowRightSidebar?labelHide:labelShow);
}} {{
	var tip=(config.options.chkShowRightSidebar?'hide':'show')+' right sidebar';
	'$2'!='$'+'2'?'$2':tip;
}}>>
|~ViewToolbar|closeTiddler closeOthers +editTiddler > fields syncing permalink references jump|
|~EditToolbar|+saveTiddler -cancelTiddler deleteTiddler|
|~blogViewToolbar|+editTiddler > fields deleteTiddler|
/***
Description: Contains the stuff you need to use Tiddlyspot
Note, you also need UploadPlugin, PasswordOptionPlugin and LoadRemoteFileThroughProxy
from http://tiddlywiki.bidix.info for a complete working Tiddlyspot site.
***/
//{{{

// edit this if you are migrating sites or retrofitting an existing TW
config.tiddlyspotSiteId = 'xmppwiki';

// make it so you can by default see edit controls via http
config.options.chkHttpReadOnly = false;
window.readOnly = false; // make sure of it (for tw 2.2)
window.showBackstage = true; // show backstage too

// disable autosave in d3
if (window.location.protocol != "file:")
	config.options.chkGTDLazyAutoSave = false;

// tweak shadow tiddlers to add upload button, password entry box etc
with (config.shadowTiddlers) {
	SiteUrl = 'http://'+config.tiddlyspotSiteId+'.tiddlyspot.com';
	SideBarOptions = SideBarOptions.replace(/(<<saveChanges>>)/,"$1<<tiddler TspotSidebar>>");
	OptionsPanel = OptionsPanel.replace(/^/,"<<tiddler TspotOptions>>");
	DefaultTiddlers = DefaultTiddlers.replace(/^/,"[[WelcomeToTiddlyspot]] ");
	MainMenu = MainMenu.replace(/^/,"[[WelcomeToTiddlyspot]] ");
}

// create some shadow tiddler content
merge(config.shadowTiddlers,{

'TspotOptions':[
 "tiddlyspot password:",
 "<<option pasUploadPassword>>",
 ""
].join("\n"),

'TspotControls':[
 "| tiddlyspot password:|<<option pasUploadPassword>>|",
 "| site management:|<<upload http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/store.cgi index.html . .  " + config.tiddlyspotSiteId + ">>//(requires tiddlyspot password)//<br>[[control panel|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/controlpanel]], [[download (go offline)|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/download]]|",
 "| links:|[[tiddlyspot.com|http://tiddlyspot.com/]], [[FAQs|http://faq.tiddlyspot.com/]], [[blog|http://tiddlyspot.blogspot.com/]], email [[support|mailto:support@tiddlyspot.com]] & [[feedback|mailto:feedback@tiddlyspot.com]], [[donate|http://tiddlyspot.com/?page=donate]]|"
].join("\n"),

'WelcomeToTiddlyspot':[
 "This document is a ~TiddlyWiki from tiddlyspot.com.  A ~TiddlyWiki is an electronic notebook that is great for managing todo lists, personal information, and all sorts of things.",
 "",
 "@@font-weight:bold;font-size:1.3em;color:#444; //What now?// &nbsp;&nbsp;@@ Before you can save any changes, you need to enter your password in the form below.  Then configure privacy and other site settings at your [[control panel|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/controlpanel]] (your control panel username is //" + config.tiddlyspotSiteId + "//).",
 "<<tiddler TspotControls>>",
 "See also GettingStarted.",
 "",
 "@@font-weight:bold;font-size:1.3em;color:#444; //Working online// &nbsp;&nbsp;@@ You can edit this ~TiddlyWiki right now, and save your changes using the \"save to web\" button in the column on the right.",
 "",
 "@@font-weight:bold;font-size:1.3em;color:#444; //Working offline// &nbsp;&nbsp;@@ A fully functioning copy of this ~TiddlyWiki can be saved onto your hard drive or USB stick.  You can make changes and save them locally without being connected to the Internet.  When you're ready to sync up again, just click \"upload\" and your ~TiddlyWiki will be saved back to tiddlyspot.com.",
 "",
 "@@font-weight:bold;font-size:1.3em;color:#444; //Help!// &nbsp;&nbsp;@@ Find out more about ~TiddlyWiki at [[TiddlyWiki.com|http://tiddlywiki.com]].  Also visit [[TiddlyWiki.org|http://tiddlywiki.org]] for documentation on learning and using ~TiddlyWiki. New users are especially welcome on the [[TiddlyWiki mailing list|http://groups.google.com/group/TiddlyWiki]], which is an excellent place to ask questions and get help.  If you have a tiddlyspot related problem email [[tiddlyspot support|mailto:support@tiddlyspot.com]].",
 "",
 "@@font-weight:bold;font-size:1.3em;color:#444; //Enjoy :)// &nbsp;&nbsp;@@ We hope you like using your tiddlyspot.com site.  Please email [[feedback@tiddlyspot.com|mailto:feedback@tiddlyspot.com]] with any comments or suggestions."
].join("\n"),

'TspotSidebar':[
 "<<upload http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/store.cgi index.html . .  " + config.tiddlyspotSiteId + ">><html><a href='http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/download' class='button'>download</a></html>"
].join("\n")

});
//}}}
| !date | !user | !location | !storeUrl | !uploadDir | !toFilename | !backupdir | !origin |
| 17/06/2011 18:07:11 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 17/06/2011 18:21:48 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 21/06/2011 13:19:11 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 21/06/2011 13:37:22 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 22/06/2011 19:28:17 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . | ok |
| 22/06/2011 19:31:49 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 24/06/2011 18:55:46 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 24/06/2011 19:01:44 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 24/06/2011 19:09:18 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
| 05/07/2011 16:03:31 | YourName | [[/|http://xmppwiki.tiddlyspot.com/]] | [[store.cgi|http://xmppwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://xmppwiki.tiddlyspot.com/index.html]] | . |
/***
|''Name:''|UploadPlugin|
|''Description:''|Save to web a TiddlyWiki|
|''Version:''|4.1.3|
|''Date:''|Feb 24, 2008|
|''Source:''|http://tiddlywiki.bidix.info/#UploadPlugin|
|''Documentation:''|http://tiddlywiki.bidix.info/#UploadPluginDoc|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0|
|''Requires:''|PasswordOptionPlugin|
***/
//{{{
version.extensions.UploadPlugin = {
	major: 4, minor: 1, revision: 3,
	date: new Date("Feb 24, 2008"),
	source: 'http://tiddlywiki.bidix.info/#UploadPlugin',
	author: 'BidiX (BidiX (at) bidix (dot) info',
	coreVersion: '2.2.0'
};

//
// Environment
//

if (!window.bidix) window.bidix = {}; // bidix namespace
bidix.debugMode = false;	// true to activate both in Plugin and UploadService
	
//
// Upload Macro
//

config.macros.upload = {
// default values
	defaultBackupDir: '',	//no backup
	defaultStoreScript: "store.php",
	defaultToFilename: "index.html",
	defaultUploadDir: ".",
	authenticateUser: true	// UploadService Authenticate User
};
	
config.macros.upload.label = {
	promptOption: "Save and Upload this TiddlyWiki with UploadOptions",
	promptParamMacro: "Save and Upload this TiddlyWiki in %0",
	saveLabel: "save to web", 
	saveToDisk: "save to disk",
	uploadLabel: "upload"	
};

config.macros.upload.messages = {
	noStoreUrl: "No store URL in parmeters or options",
	usernameOrPasswordMissing: "Username or password missing"
};

config.macros.upload.handler = function(place,macroName,params) {
	if (readOnly)
		return;
	var label;
	if (document.location.toString().substr(0,4) == "http") 
		label = this.label.saveLabel;
	else
		label = this.label.uploadLabel;
	var prompt;
	if (params[0]) {
		prompt = this.label.promptParamMacro.toString().format([this.destFile(params[0], 
			(params[1] ? params[1]:bidix.basename(window.location.toString())), params[3])]);
	} else {
		prompt = this.label.promptOption;
	}
	createTiddlyButton(place, label, prompt, function() {config.macros.upload.action(params);}, null, null, this.accessKey);
};

config.macros.upload.action = function(params)
{
		// for missing macro parameter set value from options
		if (!params) params = {};
		var storeUrl = params[0] ? params[0] : config.options.txtUploadStoreUrl;
		var toFilename = params[1] ? params[1] : config.options.txtUploadFilename;
		var backupDir = params[2] ? params[2] : config.options.txtUploadBackupDir;
		var uploadDir = params[3] ? params[3] : config.options.txtUploadDir;
		var username = params[4] ? params[4] : config.options.txtUploadUserName;
		var password = config.options.pasUploadPassword; // for security reason no password as macro parameter	
		// for still missing parameter set default value
		if ((!storeUrl) && (document.location.toString().substr(0,4) == "http")) 
			storeUrl = bidix.dirname(document.location.toString())+'/'+config.macros.upload.defaultStoreScript;
		if (storeUrl.substr(0,4) != "http")
			storeUrl = bidix.dirname(document.location.toString()) +'/'+ storeUrl;
		if (!toFilename)
			toFilename = bidix.basename(window.location.toString());
		if (!toFilename)
			toFilename = config.macros.upload.defaultToFilename;
		if (!uploadDir)
			uploadDir = config.macros.upload.defaultUploadDir;
		if (!backupDir)
			backupDir = config.macros.upload.defaultBackupDir;
		// report error if still missing
		if (!storeUrl) {
			alert(config.macros.upload.messages.noStoreUrl);
			clearMessage();
			return false;
		}
		if (config.macros.upload.authenticateUser && (!username || !password)) {
			alert(config.macros.upload.messages.usernameOrPasswordMissing);
			clearMessage();
			return false;
		}
		bidix.upload.uploadChanges(false,null,storeUrl, toFilename, uploadDir, backupDir, username, password); 
		return false; 
};

config.macros.upload.destFile = function(storeUrl, toFilename, uploadDir) 
{
	if (!storeUrl)
		return null;
		var dest = bidix.dirname(storeUrl);
		if (uploadDir && uploadDir != '.')
			dest = dest + '/' + uploadDir;
		dest = dest + '/' + toFilename;
	return dest;
};

//
// uploadOptions Macro
//

config.macros.uploadOptions = {
	handler: function(place,macroName,params) {
		var wizard = new Wizard();
		wizard.createWizard(place,this.wizardTitle);
		wizard.addStep(this.step1Title,this.step1Html);
		var markList = wizard.getElement("markList");
		var listWrapper = document.createElement("div");
		markList.parentNode.insertBefore(listWrapper,markList);
		wizard.setValue("listWrapper",listWrapper);
		this.refreshOptions(listWrapper,false);
		var uploadCaption;
		if (document.location.toString().substr(0,4) == "http") 
			uploadCaption = config.macros.upload.label.saveLabel;
		else
			uploadCaption = config.macros.upload.label.uploadLabel;
		
		wizard.setButtons([
				{caption: uploadCaption, tooltip: config.macros.upload.label.promptOption, 
					onClick: config.macros.upload.action},
				{caption: this.cancelButton, tooltip: this.cancelButtonPrompt, onClick: this.onCancel}
				
			]);
	},
	options: [
		"txtUploadUserName",
		"pasUploadPassword",
		"txtUploadStoreUrl",
		"txtUploadDir",
		"txtUploadFilename",
		"txtUploadBackupDir",
		"chkUploadLog",
		"txtUploadLogMaxLine"		
	],
	refreshOptions: function(listWrapper) {
		var opts = [];
		for(i=0; i<this.options.length; i++) {
			var opt = {};
			opts.push();
			opt.option = "";
			n = this.options[i];
			opt.name = n;
			opt.lowlight = !config.optionsDesc[n];
			opt.description = opt.lowlight ? this.unknownDescription : config.optionsDesc[n];
			opts.push(opt);
		}
		var listview = ListView.create(listWrapper,opts,this.listViewTemplate);
		for(n=0; n<opts.length; n++) {
			var type = opts[n].name.substr(0,3);
			var h = config.macros.option.types[type];
			if (h && h.create) {
				h.create(opts[n].colElements['option'],type,opts[n].name,opts[n].name,"no");
			}
		}
		
	},
	onCancel: function(e)
	{
		backstage.switchTab(null);
		return false;
	},
	
	wizardTitle: "Upload with options",
	step1Title: "These options are saved in cookies in your browser",
	step1Html: "<input type='hidden' name='markList'></input><br>",
	cancelButton: "Cancel",
	cancelButtonPrompt: "Cancel prompt",
	listViewTemplate: {
		columns: [
			{name: 'Description', field: 'description', title: "Description", type: 'WikiText'},
			{name: 'Option', field: 'option', title: "Option", type: 'String'},
			{name: 'Name', field: 'name', title: "Name", type: 'String'}
			],
		rowClasses: [
			{className: 'lowlight', field: 'lowlight'} 
			]}
};

//
// upload functions
//

if (!bidix.upload) bidix.upload = {};

if (!bidix.upload.messages) bidix.upload.messages = {
	//from saving
	invalidFileError: "The original file '%0' does not appear to be a valid TiddlyWiki",
	backupSaved: "Backup saved",
	backupFailed: "Failed to upload backup file",
	rssSaved: "RSS feed uploaded",
	rssFailed: "Failed to upload RSS feed file",
	emptySaved: "Empty template uploaded",
	emptyFailed: "Failed to upload empty template file",
	mainSaved: "Main TiddlyWiki file uploaded",
	mainFailed: "Failed to upload main TiddlyWiki file. Your changes have not been saved",
	//specific upload
	loadOriginalHttpPostError: "Can't get original file",
	aboutToSaveOnHttpPost: 'About to upload on %0 ...',
	storePhpNotFound: "The store script '%0' was not found."
};

bidix.upload.uploadChanges = function(onlyIfDirty,tiddlers,storeUrl,toFilename,uploadDir,backupDir,username,password)
{
	var callback = function(status,uploadParams,original,url,xhr) {
		if (!status) {
			displayMessage(bidix.upload.messages.loadOriginalHttpPostError);
			return;
		}
		if (bidix.debugMode) 
			alert(original.substr(0,500)+"\n...");
		// Locate the storeArea div's 
		var posDiv = locateStoreArea(original);
		if((posDiv[0] == -1) || (posDiv[1] == -1)) {
			alert(config.messages.invalidFileError.format([localPath]));
			return;
		}
		bidix.upload.uploadRss(uploadParams,original,posDiv);
	};
	
	if(onlyIfDirty && !store.isDirty())
		return;
	clearMessage();
	// save on localdisk ?
	if (document.location.toString().substr(0,4) == "file") {
		var path = document.location.toString();
		var localPath = getLocalPath(path);
		saveChanges();
	}
	// get original
	var uploadParams = new Array(storeUrl,toFilename,uploadDir,backupDir,username,password);
	var originalPath = document.location.toString();
	// If url is a directory : add index.html
	if (originalPath.charAt(originalPath.length-1) == "/")
		originalPath = originalPath + "index.html";
	var dest = config.macros.upload.destFile(storeUrl,toFilename,uploadDir);
	var log = new bidix.UploadLog();
	log.startUpload(storeUrl, dest, uploadDir,  backupDir);
	displayMessage(bidix.upload.messages.aboutToSaveOnHttpPost.format([dest]));
	if (bidix.debugMode) 
		alert("about to execute Http - GET on "+originalPath);
	var r = doHttp("GET",originalPath,null,null,username,password,callback,uploadParams,null);
	if (typeof r == "string")
		displayMessage(r);
	return r;
};

bidix.upload.uploadRss = function(uploadParams,original,posDiv) 
{
	var callback = function(status,params,responseText,url,xhr) {
		if(status) {
			var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
			displayMessage(bidix.upload.messages.rssSaved,bidix.dirname(url)+'/'+destfile);
			bidix.upload.uploadMain(params[0],params[1],params[2]);
		} else {
			displayMessage(bidix.upload.messages.rssFailed);			
		}
	};
	// do uploadRss
	if(config.options.chkGenerateAnRssFeed) {
		var rssPath = uploadParams[1].substr(0,uploadParams[1].lastIndexOf(".")) + ".xml";
		var rssUploadParams = new Array(uploadParams[0],rssPath,uploadParams[2],'',uploadParams[4],uploadParams[5]);
		var rssString = generateRss();
		// no UnicodeToUTF8 conversion needed when location is "file" !!!
		if (document.location.toString().substr(0,4) != "file")
			rssString = convertUnicodeToUTF8(rssString);	
		bidix.upload.httpUpload(rssUploadParams,rssString,callback,Array(uploadParams,original,posDiv));
	} else {
		bidix.upload.uploadMain(uploadParams,original,posDiv);
	}
};

bidix.upload.uploadMain = function(uploadParams,original,posDiv) 
{
	var callback = function(status,params,responseText,url,xhr) {
		var log = new bidix.UploadLog();
		if(status) {
			// if backupDir specified
			if ((params[3]) && (responseText.indexOf("backupfile:") > -1))  {
				var backupfile = responseText.substring(responseText.indexOf("backupfile:")+11,responseText.indexOf("\n", responseText.indexOf("backupfile:")));
				displayMessage(bidix.upload.messages.backupSaved,bidix.dirname(url)+'/'+backupfile);
			}
			var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
			displayMessage(bidix.upload.messages.mainSaved,bidix.dirname(url)+'/'+destfile);
			store.setDirty(false);
			log.endUpload("ok");
		} else {
			alert(bidix.upload.messages.mainFailed);
			displayMessage(bidix.upload.messages.mainFailed);
			log.endUpload("failed");			
		}
	};
	// do uploadMain
	var revised = bidix.upload.updateOriginal(original,posDiv);
	bidix.upload.httpUpload(uploadParams,revised,callback,uploadParams);
};

bidix.upload.httpUpload = function(uploadParams,data,callback,params)
{
	var localCallback = function(status,params,responseText,url,xhr) {
		url = (url.indexOf("nocache=") < 0 ? url : url.substring(0,url.indexOf("nocache=")-1));
		if (xhr.status == 404)
			alert(bidix.upload.messages.storePhpNotFound.format([url]));
		if ((bidix.debugMode) || (responseText.indexOf("Debug mode") >= 0 )) {
			alert(responseText);
			if (responseText.indexOf("Debug mode") >= 0 )
				responseText = responseText.substring(responseText.indexOf("\n\n")+2);
		} else if (responseText.charAt(0) != '0') 
			alert(responseText);
		if (responseText.charAt(0) != '0')
			status = null;
		callback(status,params,responseText,url,xhr);
	};
	// do httpUpload
	var boundary = "---------------------------"+"AaB03x";	
	var uploadFormName = "UploadPlugin";
	// compose headers data
	var sheader = "";
	sheader += "--" + boundary + "\r\nContent-disposition: form-data; name=\"";
	sheader += uploadFormName +"\"\r\n\r\n";
	sheader += "backupDir="+uploadParams[3] +
				";user=" + uploadParams[4] +
				";password=" + uploadParams[5] +
				";uploaddir=" + uploadParams[2];
	if (bidix.debugMode)
		sheader += ";debug=1";
	sheader += ";;\r\n"; 
	sheader += "\r\n" + "--" + boundary + "\r\n";
	sheader += "Content-disposition: form-data; name=\"userfile\"; filename=\""+uploadParams[1]+"\"\r\n";
	sheader += "Content-Type: text/html;charset=UTF-8" + "\r\n";
	sheader += "Content-Length: " + data.length + "\r\n\r\n";
	// compose trailer data
	var strailer = new String();
	strailer = "\r\n--" + boundary + "--\r\n";
	data = sheader + data + strailer;
	if (bidix.debugMode) alert("about to execute Http - POST on "+uploadParams[0]+"\n with \n"+data.substr(0,500)+ " ... ");
	var r = doHttp("POST",uploadParams[0],data,"multipart/form-data; ;charset=UTF-8; boundary="+boundary,uploadParams[4],uploadParams[5],localCallback,params,null);
	if (typeof r == "string")
		displayMessage(r);
	return r;
};

// same as Saving's updateOriginal but without convertUnicodeToUTF8 calls
bidix.upload.updateOriginal = function(original, posDiv)
{
	if (!posDiv)
		posDiv = locateStoreArea(original);
	if((posDiv[0] == -1) || (posDiv[1] == -1)) {
		alert(config.messages.invalidFileError.format([localPath]));
		return;
	}
	var revised = original.substr(0,posDiv[0] + startSaveArea.length) + "\n" +
				store.allTiddlersAsHtml() + "\n" +
				original.substr(posDiv[1]);
	var newSiteTitle = getPageTitle().htmlEncode();
	revised = revised.replaceChunk("<title"+">","</title"+">"," " + newSiteTitle + " ");
	revised = updateMarkupBlock(revised,"PRE-HEAD","MarkupPreHead");
	revised = updateMarkupBlock(revised,"POST-HEAD","MarkupPostHead");
	revised = updateMarkupBlock(revised,"PRE-BODY","MarkupPreBody");
	revised = updateMarkupBlock(revised,"POST-SCRIPT","MarkupPostBody");
	return revised;
};

//
// UploadLog
// 
// config.options.chkUploadLog :
//		false : no logging
//		true : logging
// config.options.txtUploadLogMaxLine :
//		-1 : no limit
//      0 :  no Log lines but UploadLog is still in place
//		n :  the last n lines are only kept
//		NaN : no limit (-1)

bidix.UploadLog = function() {
	if (!config.options.chkUploadLog) 
		return; // this.tiddler = null
	this.tiddler = store.getTiddler("UploadLog");
	if (!this.tiddler) {
		this.tiddler = new Tiddler();
		this.tiddler.title = "UploadLog";
		this.tiddler.text = "| !date | !user | !location | !storeUrl | !uploadDir | !toFilename | !backupdir | !origin |";
		this.tiddler.created = new Date();
		this.tiddler.modifier = config.options.txtUserName;
		this.tiddler.modified = new Date();
		store.addTiddler(this.tiddler);
	}
	return this;
};

bidix.UploadLog.prototype.addText = function(text) {
	if (!this.tiddler)
		return;
	// retrieve maxLine when we need it
	var maxLine = parseInt(config.options.txtUploadLogMaxLine,10);
	if (isNaN(maxLine))
		maxLine = -1;
	// add text
	if (maxLine != 0) 
		this.tiddler.text = this.tiddler.text + text;
	// Trunck to maxLine
	if (maxLine >= 0) {
		var textArray = this.tiddler.text.split('\n');
		if (textArray.length > maxLine + 1)
			textArray.splice(1,textArray.length-1-maxLine);
			this.tiddler.text = textArray.join('\n');		
	}
	// update tiddler fields
	this.tiddler.modifier = config.options.txtUserName;
	this.tiddler.modified = new Date();
	store.addTiddler(this.tiddler);
	// refresh and notifiy for immediate update
	story.refreshTiddler(this.tiddler.title);
	store.notify(this.tiddler.title, true);
};

bidix.UploadLog.prototype.startUpload = function(storeUrl, toFilename, uploadDir,  backupDir) {
	if (!this.tiddler)
		return;
	var now = new Date();
	var text = "\n| ";
	var filename = bidix.basename(document.location.toString());
	if (!filename) filename = '/';
	text += now.formatString("0DD/0MM/YYYY 0hh:0mm:0ss") +" | ";
	text += config.options.txtUserName + " | ";
	text += "[["+filename+"|"+location + "]] |";
	text += " [[" + bidix.basename(storeUrl) + "|" + storeUrl + "]] | ";
	text += uploadDir + " | ";
	text += "[[" + bidix.basename(toFilename) + " | " +toFilename + "]] | ";
	text += backupDir + " |";
	this.addText(text);
};

bidix.UploadLog.prototype.endUpload = function(status) {
	if (!this.tiddler)
		return;
	this.addText(" "+status+" |");
};

//
// Utilities
// 

bidix.checkPlugin = function(plugin, major, minor, revision) {
	var ext = version.extensions[plugin];
	if (!
		(ext  && 
			((ext.major > major) || 
			((ext.major == major) && (ext.minor > minor))  ||
			((ext.major == major) && (ext.minor == minor) && (ext.revision >= revision))))) {
			// write error in PluginManager
			if (pluginInfo)
				pluginInfo.log.push("Requires " + plugin + " " + major + "." + minor + "." + revision);
			eval(plugin); // generate an error : "Error: ReferenceError: xxxx is not defined"
	}
};

bidix.dirname = function(filePath) {
	if (!filePath) 
		return;
	var lastpos;
	if ((lastpos = filePath.lastIndexOf("/")) != -1) {
		return filePath.substring(0, lastpos);
	} else {
		return filePath.substring(0, filePath.lastIndexOf("\\"));
	}
};

bidix.basename = function(filePath) {
	if (!filePath) 
		return;
	var lastpos;
	if ((lastpos = filePath.lastIndexOf("#")) != -1) 
		filePath = filePath.substring(0, lastpos);
	if ((lastpos = filePath.lastIndexOf("/")) != -1) {
		return filePath.substring(lastpos + 1);
	} else
		return filePath.substring(filePath.lastIndexOf("\\")+1);
};

bidix.initOption = function(name,value) {
	if (!config.options[name])
		config.options[name] = value;
};

//
// Initializations
//

// require PasswordOptionPlugin 1.0.1 or better
bidix.checkPlugin("PasswordOptionPlugin", 1, 0, 1);

// styleSheet
setStylesheet('.txtUploadStoreUrl, .txtUploadBackupDir, .txtUploadDir {width: 22em;}',"uploadPluginStyles");

//optionsDesc
merge(config.optionsDesc,{
	txtUploadStoreUrl: "Url of the UploadService script (default: store.php)",
	txtUploadFilename: "Filename of the uploaded file (default: in index.html)",
	txtUploadDir: "Relative Directory where to store the file (default: . (downloadService directory))",
	txtUploadBackupDir: "Relative Directory where to backup the file. If empty no backup. (default: ''(empty))",
	txtUploadUserName: "Upload Username",
	pasUploadPassword: "Upload Password",
	chkUploadLog: "do Logging in UploadLog (default: true)",
	txtUploadLogMaxLine: "Maximum of lines in UploadLog (default: 10)"
});

// Options Initializations
bidix.initOption('txtUploadStoreUrl','');
bidix.initOption('txtUploadFilename','');
bidix.initOption('txtUploadDir','');
bidix.initOption('txtUploadBackupDir','');
bidix.initOption('txtUploadUserName','');
bidix.initOption('pasUploadPassword','');
bidix.initOption('chkUploadLog',true);
bidix.initOption('txtUploadLogMaxLine','10');


// Backstage
merge(config.tasks,{
	uploadOptions: {text: "upload", tooltip: "Change UploadOptions and Upload", content: '<<uploadOptions>>'}
});
config.backstageTasks.push("uploadOptions");


//}}}

!Welcome to the XMPP Bosh Plugin
This is a pre-Alpha of a project, that could evolve into so many things: 
#a [[PubSub|http://xmpp.org/extensions/xep-0060.html]] Tiddler Store
#a webinterface for XMPP and PubSub
#a webinterface for [[BuddyCloud|http://buddycloud.org]]
#a nearly standalone IM Application with offline Sync for XMPP Pubsub
# ...

!!How to begin
#Connect to a Jabber Server (use a dummy account, i can´t guarantee for security)
##in the right sidebar under options you can provide the bosh/http-bind url of your server, username and password
#chat with Friends 
#create XMPP Nodes, configure, access and post to them (in order to use the //show Dashboard// and //configure Dashboard//// functions you need to have a node that pubsubed ejabberd would adress as you@yourdomain.org//home you can create it with the Interface beneath)

!!Hints
*uses[[Strophe.js|http://strophe.im/]] to communicate to a Jabber Server via Http
*works with Firefox 4.x and Google Chrome, theres a Problem with Safari and the likes that should be solvable
*doesnt escape whats coming in, so very insecure in the moment
*badly written code (my first JS project & my first bigger Project)

!!Problems
*Ejabberd (XMPP Server) doesnt reliably normalize the publisher of items. So you can fill in the author field of the Atom Items that get posted to the XMPP Server as you like, which is a bad solution. It also lacks the Publish-Only affiliation for nodes, so you cannot give someone write acces to a node without enabling him or her to alter and even delete you posts. 
*The PubSub Functionality of Ejabberd is not very well documented i think. 
*the channel-server from [[BuddyCloud|http://buddycloud.org]] will hopefully solve this problems, as soon as it gets more stable

!! Some Interfaces
|<<bosh_addContact>>|<<bosh_pubsubManager>>|
|<<bosh_pubsubManager>>|<<bosh_configureNode>>|
|<<bosh_retrieveNodeItems>>|<<bosh_publish>>|
!!pubsub
<<bosh_affiliationsManager node:zip//home>>
<!--{{{-->
<b>write new article</b>
<div class='toolbar' macro='toolbar [[ToolbarCommands::EditToolbar]]'></div>
<div class='title' macro='view itemtitle'></div>
<div class='editor' macro='edit itemtitle'></div>
<div macro='annotations'></div>
<div class='editor' macro='edit text'></div>
<div class='editor' macro='edit tags'></div><div class='editor' macro='edit node'></div><div class='editorFooter'><span macro='message views.editor.tagPrompt'></span><span macro='tagChooser excludeLists'></span></div>
<!--}}}-->
<!--{{{-->
<div class='toolbar' macro='toolbar [[ToolbarCommands::blogViewToolbar]]'></div>
<div class='title' macro='view itemtitle'></div>
<div class='subtitle'><span macro='view modifier link'></span>, <span macro='view modified date'></span> (<span macro='message views.wikified.createdPrompt'></span> <span macro='view created date'></span>)</div>
<div class='tagging' macro='tagging'></div>
<div class='viewer' macro='view text wikified'></div>
<div class='tagClear'></div>
<!--}}}-->
/*{{{*/
(function($) {
//standard bosh server & base64 encoded icon for buddies, that don´t ship one viea VCard
config.options.txtXmppBoshServerUrl = 'http://127.0.0.1:5280/xmpp-bind';
config.options.txtBoshUserName = '';
config.options.pasBoshUserPWD = '';
var boshPlugin;
boshPlugin = config.extensions.boshPlugin = {               //basic functions for connecting 
	connection: null,
	tiddler: null,
	jid: null,
	rosterTiddler: "Roster",


log: function(msg) {
    displayMessage(msg);
},

rawInput: function(data) {
  //  config.extensions.boshPlugin.log('RECV: ' + data);
    config.extensions.boshPlugin.rawLog = config.extensions.boshPlugin.rawLog + "\n\nIN: " + data;
},

rawOutput: function(data) {
 //   config.extensions.boshPlugin.log('SENT: ' + data);
    config.extensions.boshPlugin.rawLog = config.extensions.boshPlugin.rawLog + "\n\nOUT: " + data;
},

xmlInput: function(elem) {
//    config.extensions.boshPlugin.log('SENT: ' + elem.body);
},

xmlOutput: function(elem) {
//    config.extensions.boshPlugin.log('SENT: ' + elem.body);
},

onConnect: function(status) {
    if (status == Strophe.Status.CONNECTING) {
        var tid=store.getTiddler("rawLog");
        tid.set(null,"");
	config.extensions.boshPlugin.log('Strophe is connecting.',"rawLog");
    } else if (status == Strophe.Status.CONNFAIL) {
	config.extensions.boshPlugin.log('Strophe failed to connect.');
	$('#connect').get(0).value = 'connect';
    } else if (status == Strophe.Status.DISCONNECTING) {
	config.extensions.boshPlugin.log('Strophe is disconnecting.');
    } else if (status == Strophe.Status.DISCONNECTED) {
	config.extensions.boshPlugin.log('Strophe is disconnected.');
	$('#connect').get(0).value = 'connect';
    } else if (status == Strophe.Status.CONNECTED) {
	config.extensions.boshPlugin.roster.queryRoster();
	// register Handlers for events, this should be done in the respectiv Sub-Objects (chat,pubsub chat)
	config.extensions.boshPlugin.connection.addHandler(boshPlugin.chat.messageIn,null, 'message', null, null,  null);
	config.extensions.boshPlugin.connection.addHandler(boshPlugin.roster.queryRoster,"jabber:iq:roster", "iq", "set");
	config.extensions.boshPlugin.connection.addHandler(boshPlugin.onForm,"jabber:x:data", null, null);
		config.extensions.boshPlugin.log('Strophe is connected.');
	}
},
disconnect: function() {
config.extensions.boshPlugin.connection.disconnect();
},

connect: function(form) {
    config.extensions.boshPlugin.rawLog = "";
    config.extensions.boshPlugin.connection = new Strophe.Connection(config.options.txtXmppBoshServerUrl);
    config.extensions.boshPlugin.connection.rawInput = config.extensions.boshPlugin.rawInput;
    config.extensions.boshPlugin.connection.rawOutput = config.extensions.boshPlugin.rawOutput;
    config.extensions.boshPlugin.connection.xmlInput =  config.extensions.boshPlugin.xmlInput;
    config.extensions.boshPlugin.connection.xmlOutput = config.extensions.boshPlugin.xmlOutput;
    config.extensions.boshPlugin.jid = form.jid;
    if (!store.getTiddler) 
      {
         store.getTiddler=function(title) {return this.tiddlers[title];};
      }
  //  config.extensions.boshPlugin.tiddler = window.story.findContainingTiddler(form).id.substr(7);
    config.options.txtUserName = form.jid;
    config.extensions.boshPlugin.connection.connect(form.jid+"/web",
	       form.pwd,
			       config.extensions.boshPlugin.onConnect);

},
/*
      this is not finished, but should be an universial handler for incomming forms, saving the Outer stanza, render the form to html and 
	  and send it back to where it cam From
*/
onForm: function(iq) {          
	displayMessage("receivedForm");
	var ReturnFrame = iq;
	displayMessage(Strophe.serialize(ReturnFrame));
	$(ReturnFrame).attr('to',$(ReturnFrame).attr('from'));
	$(ReturnFrame).removeAttr('from');
	displayMessage(Strophe.serialize(ReturnFrame));
	$(ReturnFrame).find('x').detach();
	displayMessage(Strophe.serialize("IQ: "+Strophe.serialize(iq)));
	
}
};

/*
   Macros to User with BoshPlugin
*/
// login form
config.macros.bosh = {
	handler: function (place, macroName, params, wikifier, paramString, tiddler) {
		//form = "<html><form><label for='jid'>JID:</label><input name ='jid' type='text' id='jid' value=''/><label for='pass'>Password:</label><input name='pwd' type='password' id='pass'/><input type='button' id='connect' value='connect' onclick='config.extensions.boshPlugin.connect(this.form)' /><input type='button' id='disconnect' value='disconnect' onclick='config.extensions.boshPlugin.disconnect()' /></form></html>";
		//wikify(form,place);
		wikify("JID:<<option txtBoshUserName>>PWD:<<option pasBoshUserPWD>><html><input type='button' id='connect' value='connect' onclick='config.extensions.boshPlugin.connect({jid: config.options.txtBoshUserName,pwd: config.options.pasBoshUserPWD})' /><input type='button' id='disconnect' value='disconnect' onclick='config.extensions.boshPlugin.disconnect()' /></html>",place);
	}
};
})(jQuery);              

/*}}}*/
/*{{{*/
(function($) {
/*
	this object handles all the chat functionality (no MUC at the moment), also rendering a chat window and setting intervalls on incomming 
	messages for user notification	
*/ 
boshPlugin = config.extensions.boshPlugin;

var chat;
chat = config.extensions.boshPlugin.chat = {
conversations: {}, //stores all open conversations
/*
   Callback for incomming Messages
*/
messageIn: function (msg) {      
	var to = msg.getAttribute('to');
	var from = msg.getAttribute('from');
   	var bare_from = Strophe.getBareJidFromJid(from);
   	var type = msg.getAttribute('type');
	var elems = msg.getElementsByTagName('body');
	if (type == "chat" && elems.length > 0) {       //andere Messages sind denkbar, vor allem Error ...
        	var body = elems[0];
        	config.extensions.boshPlugin.log(from + ': ' + Strophe.getText(body)); 		   
		if (chat.conversations.hasOwnProperty(bare_from)) {chat.addMessageToConversation(bare_from,msg)}      // conversation is already created
		else {
  				chat.createConversation(bare_from);                    // new conversation, create it
				chat.addMessageToConversation(bare_from,msg);          // and add the message
		}
		// if the conversation is not visible in the moment, register an Intervall for alerting via blinking icon
		if ($("#outer"+chat.conversations[bare_from].id).css('display') == "none" || $("#chatAreaTabs").css('display') == "none" ) {        
			   chat.conversations[bare_from].alerter = setInterval("config.extensions.boshPlugin.chat.alerter('"+bare_from+"')",1000);
		}
	}
	   // displayMessage("here");
	//}
	return true;
},
alerter: function(jid) {         // blinking icon alerter    
    //$("#choice"+config.extensions.boshPlugin.chat.conversations[jid].id).slideToggle('slow',null);
    $("#choice"+config.extensions.boshPlugin.chat.conversations[jid].id).fadeOut('slow', function() {
	        // Animation complete
			$("#choice"+config.extensions.boshPlugin.chat.conversations[jid].id).fadeIn('fast');
	      });
},                       
/*
  	create a conversation
*/
createConversation: function(from) {
	if (!chat.conversations[from]) {
		chat.conversations[from] = {};
		chat.conversations[from].body = "";
		var id = new Date;
		chat.conversations[from].id = id.getTime();                                                          
		$("#chatAreaTabs").append("<div id='outer"+chat.conversations[from].id+
									"' >" + 
									wikifyStatic("<<bosh_chat to:"+from+">>")+
									"</div>");
		$("#outer"+chat.conversations[from].id).hide();
	    //$("#chatAreaTabsChoice").append("<div id='choice"+chat.conversations[from].id+"' value='"+from+"' onclick='config.extensions.boshPlugin.chat.chatTabsShow(\"" + chat.conversations[from].id + "\")'>"+from+"</div>");
		$("#chatAreaTabsChoice").append("<img  id='choice" + chat.conversations[from].id + "' onclick='config.extensions.boshPlugin.chat.chatTabsShow(\"" + from + "\")'" 
				+ "\") width='30"
				+"' height='30'"
				+  + "' title='"
				+ config.extensions.boshPlugin.roster.contacts[from].jid + "' src='" 
				+ config.extensions.boshPlugin.roster.contacts[from].photo + "'>");
		
		chat.chatTabsShow(from);
	}
},
// ...
deleteConversation: function(conv) {
  $("#outer"+chat.conversations[conv].id).siblings().last().show();
  $("#outer"+chat.conversations[conv].id).remove();   
  $("#choice"+chat.conversations[conv].id).remove();  
  if (!document.getElementById("chatAreaTabs").hasChildNodes()) {
	$("#chatAreaTabs").hide();
  }
  clearInterval(chat.conversations[conv].alerter);
  delete chat.conversations[conv];
},
// add a Message to the conversation
addMessageToConversation: function(conv,msg) {
	var from = msg.getAttribute('from');
	var elems = msg.getElementsByTagName('body');
	var body = elems[0];
	var bare_from = Strophe.getBareJidFromJid(from);
	chat.conversations[conv].body = chat.conversations[conv].body + "\n";
	chat.conversations[conv].body = chat.conversations[conv].body + from + ":  " + Strophe.getText(body);
    var chat_display=document.getElementById(chat.conversations[conv].id); 
	if (chat_display) {
		chat_display.innerHTML = chat.conversations[conv].body;
		chat_display.scrollTop = chat_display.scrollHeight
	}
	//story.refreshAllTiddlers();
},
// show a conversation in the chatTabs window
chatTabsShow: function(from) {
	if ($("#outer"+config.extensions.boshPlugin.chat.conversations[from].id).css('display') == 'block') {  
   		$("#chatAreaTabs").slideToggle('slow'); ;
		clearInterval(config.extensions.boshPlugin.chat.conversations[from].alerter);
	}
	else {
		if ($("#chatAreaTabs").css('display') == 'none') {
		      $("#chatAreaTabs").slideToggle('slow');
		}
		$("#chatAreaTabs > div").hide();          
		clearInterval(config.extensions.boshPlugin.chat.conversations[from].alerter);
		$("#outer"+config.extensions.boshPlugin.chat.conversations[from].id).show();
	}
        $("#outer"+config.extensions.boshPlugin.chat.conversations[from].id).find("#text").focus();
},
// send a Message
sendMessage: function(to, body) {
	if (!to) {displayMessage("Error: no Recipient for Message");}
	else {
		var iq = new Strophe.Builder('message', {to: to,from: boshPlugin.jid, type: 'chat'})
           		.c('body').t(body);
        	config.extensions.boshPlugin.connection.send(iq.tree());
		var bare_to = Strophe.getBareJidFromJid(to);
		if (!chat.conversations.hasOwnProperty(bare_to)) {chat.createConversation(bare_to);}
		chat.addMessageToConversation(bare_to,iq.tree());
	}
},
// notify the other end that you left the conversation
sendGone: function(to, body) {
        if (!to) {displayMessage("Error: no Recipient for Message");}
        else {
                var iq = new Strophe.Builder('message', {to: to,from: boshPlugin.jid})
                        .c('gone',{xmlns: "http://jabber.org/protocol/chatstates"});
                config.extensions.boshPlugin.connection.send(iq.tree());
                var bare_to = Strophe.getBareJidFromJid(to);
                if (!chat.conversations.hasOwnProperty(bare_to)) {chat.createConversation(bare_to);}
                chat.addMessageToConversation(bare_to,iq.tree());
        }
},
}; 
/*
  	Chat Macros
*/
// Renders just an input field for sending a message to someon
config.macros.bosh_writer = {
	handler: function (place, macroName, params, wikifier, paramString, tiddler) {
		var prms = paramString.parseParams(null, null, true);
		var to = getParam(prms, "to");
		var form = "<html><form>";
		if (!to) {to = ""; form = form + "<label for='jid'>JID:</label><input name ='jid' type='text' id='jid' value='"+to+"'/>";}
		else {form = form + "<input type='hidden' name='jid' id='jid' value='"+to+"'/>";}
		form = form + "<label for='pass'>Text:</label><input name='text' width='80%' type='text' id='text' onkeypress='if (event.keyCode == 13){config.extensions.boshPlugin.chat.sendMessage(this.form.jid.value,this.form.text.value)}' /><input type='button' id='send' value='send' onclick='config.extensions.boshPlugin.chat.sendMessage(this.form.jid.value,this.form.text.value)' /></form></html>";
		wikify(form,place);
	}
};

// Renders a single chat Tab
config.macros.bosh_chat = {
        handler: function (place, macroName, params, wikifier, paramString, tiddler) {
                var prms = paramString.parseParams(null, null, true);
                var to = getParam(prms, "to");
		if (!config.extensions.boshPlugin.chat.conversations[to]) {
			config.extensions.boshPlugin.chat.createConversation(to);
		}
		var form = "<div style='font-size:large;float: left;'>"+to+"</div><form name='chat_window' id='chat_window'>";
                form = form + "<div><textarea style='width:100%;' name='chat' id='"+config.extensions.boshPlugin.chat.conversations[to].id+"' rows='6' readonly>";
		form = form + config.extensions.boshPlugin.chat.conversations[to].body;
		form = form + "</textarea></div><br>";
                if (!to) {to = ""; form = form + "<label for='jid'>JID:</label><input name ='jid' type='text' id='jid' value='"+to+"'/>";}
                else {form = form + "<input type='hidden' name='jid' id='jid' value='"+to+"'/>";}
                form = form + "<div height='8px'><input style='width:100%;' rows='1' name='text' width='80%' type='area' id='text' onkeypress='if (event.keyCode == 13){config.extensions.boshPlugin.chat.sendMessage(this.form.jid.value,this.form.text.value);this.form.text.value=null;return false;}' /></div><input type='button' id='send' value='send' onclick='config.extensions.boshPlugin.chat.sendMessage(this.form.jid.value,this.form.text.value);this.form.text.value=null;' /><input type='button' id='close' value='close' onclick='config.extensions.boshPlugin.chat.sendGone(this.form.jid.value);config.extensions.boshPlugin.chat.deleteConversation(this.form.jid.value)' /></form></html>";
                var source = "";
		if (config.extensions.boshPlugin.roster.contacts[to].photo) {
			source = "<html><img style='font-size:large;float: left;top:50%;left:10%;' width='40px' height='40px' src='"+config.extensions.boshPlugin.roster.contacts[to].photo+"'/>";
		}
		source = source + form;
		wikify(source,place);
        }
};

// subscribe to presence of XMPP Jid
config.macros.bosh_presenceSubscribe = {                    
       handler: function (place, macroName, params, wikifier, paramString, tiddler) {
                var prms = paramString.parseParams(null, null, true);
				var jid = getParam(prms, "jid");
				form = "<form >";
				if (!jid) {
					form = form + "<label for='node'>Jid</label><input type='text' id='jid'>";
				}
				else {form = form + "<input type='hidden' id='jid' value='"+jid<+"'>";}
				form = form + "<input type='button' value='authorize jid' onClick='var iq = $pres({to: form.jid.value, type: \"subscribed\"});config.extensions.boshPlugin.connection.sendIQ(iq,null,null);'><input type='button' value='deauthorize jid' onClick='var iq = $pres({to: form.jid.value, type: \"unsubscribe\"});config.extensions.boshPlugin.connection.sendIQ(iq,null,null)'></form>";
				wikify("<html>"+form+"</html>",place);
	}
};

/*
		renders the open Conversations inside the chatArea Div, 
		which should be specified in the PageTemplateTiddler
*/
config.macros.bosh_chatAll = {                                                 //open all conversation in tabbed view
         handler: function (place, macroName, params, wikifier, paramString, tiddler) {
        	var header = "<<tabs conversationsView ";
		var sections = "/%\n";
		for (conv in config.extensions.boshPlugin.chat.conversations) {
			header = header+ '"' + conv + '" " ... " "' + "chatWindowTabs" + "##"+conv+'" ';
			sections = sections + "!"+conv+"\n"+"<<bosh_chat to:"+conv+">>\n!end\n";
			}
		header = header + ">>\n";
		sections = sections + "%/";
		var text = tiddler.text;
		text = sections;
		config.shadowTiddlers.chatWindowTabs= text;	     
		$(".chatArea").replaceWith(wikifyStatic("<div id='chatArea'>"+header+sections+"</div>"));
		wikify(header,place);
		} 
	
};

// Show / Hide the chat Tabs Window
config.macros.bosh_chatToggle = {
		handler: function (place, macroName, params, wikifier, paramString, tiddler) {
		      	// $("#chatArea").toggle('slow');
				var button = "<html><input type='button' text='click' onClick='config.macros.bosh_chatToggle.toggle()'></html>";
				wikify(button,place);
		},
		toggle: function() {
				$("#chatAreaTabs").slideToggle('slow');
		},
	
};
})(jQuery);  

/*}}}*/
(function($) {
// hijacking saveTiddler & removeTiddler, looking for the extended field "node" and optionally "id" for publishing  
// or retracting tiddlers as items. ToDo: modularize the code, pack a function tiddlerToItem(Tiddler) and itemToTiddler(Item)
boshPlugin = config.extensions.boshPlugin;

TiddlyWiki.prototype.saveTiddler_original = TiddlyWiki.prototype.saveTiddler;
TiddlyWiki.prototype.saveTiddler = function(title,newTitle,newBody,modifier,modified,tags,fields,clearChangeCount,created) {
	if (fields && fields.node) {      // does the tiddler have a node field, then update the node
		var published = modified;
		var updated = new Date();
		var id;
		if (fields.id) {               // is there already an ID (means the tiddler has already been published)
			id = fields.id;
		}
		else {            // No? create one! 
			id = config.extensions.boshPlugin.connection.getUniqueId('post');
			newTitle = id;
			fields.id = id;   // and add an id field
			tags = tags + " blog " + fields.node;  // an update the tags
		}
		var post = {title: fields.itemtitle,                            // construct the post
							content: newBody, published: published.formatString("YYYY-0MM-0DDT0hh:0mm:0ssZ"),
							updated: updated.formatString("YYYY-0MM-0DDT0hh:0mm:0ssZ"),author: config.options.txtUserName, id: id}
		config.extensions.boshPlugin.pubsub.publishToNode(fields.node,post);  // Post
   		var tiddler = this.saveTiddler_original(title,newTitle,newBody,modifier,modified,tags,fields); // save the tiddler to the wiki
		tiddler.fields['doNotSave'] = true;   // ... we dont want this tiddler to be persistently stored in the wiki, do we?
	  	return tiddler;
	}
	else {                       // no node field, normal tiddler
		return this.saveTiddler_original(title,newTitle,newBody,modifier,modified,tags,fields);
	}
	
}
TiddlyWiki.prototype.removeTiddler_original = TiddlyWiki.prototype.removeTiddler;
TiddlyWiki.prototype.removeTiddler = function(title) {
	var tiddler = store.fetchTiddler(title);
	if (config.extensions.boshPlugin.connection && tiddler.fields.node && config.extensions.boshPlugin.pubsub.deleteFromNode(tiddler.fields.node,tiddler.fields.id)) {
		displayMessage("Successfully removed Item from Node.");
	}
	this.removeTiddler_original(title);
}
/*
	PubSub Functionality  XEP 0060
*/
var pubsub;
pubsub = config.extensions.boshPlugin.pubsub = {
	iqFrames: {},    // Stores the outer Frames of incomming Nod Config Forms for sending back the answer (could be replaced by boshPlugin.onForm later)
	nodes: {},      // stores discovered nodes (gets overwritten with each discovery attempt)
	// find nodes
	findNodes: function(node) {
		var iq = $iq({type: 'get',to: Strophe.getBareJidFromJid(node)});
		if (Strophe.getResourceFromJid(node)){
			iq.c('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: Strophe.getResourceFromJid(node)});
		}
		else {
			iq.c('query', {xmlns: 'http://jabber.org/protocol/disco#items'});			
		}
		config.extensions.boshPlugin.connection.sendIQ(iq,config.extensions.boshPlugin.pubsub.findNodesCallback);
	},
	findNodesCallback: function(iq) {
		var nodes = [];
		var from = $(iq).attr('from');
		$(iq).find('item').each(function () {
			var node = $(this).attr('node');
            var name = $(this).attr('name') || node;
            displayMessage(name + " -- " + node)
			config.extensions.boshPlugin.pubsub.nodes[from+"/"+node] = name;
		});
	}, 
	// subscribe
	subscribeToNode: function(node, jid) {
		if (!jid) {
			jid = config.extensions.boshPlugin.jid;
		}
		var iq = $iq({type: 'set', to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).c('subscribe',{node: Strophe.getResourceFromJid(node),jid: jid});	
		config.extensions.boshPlugin.connection.sendIQ(iq,function (iq) {displayMessage('sucess subscribing to Node');},function (iq) {displayMessage("Error subscribing to Node: "+Strophe.serialize(iq));});
	},
	// unsubscribe
	unsubscribeFromNode: function(node, jid) {
		if (!jid) {
			jid = config.extensions.boshPlugin.jid;
		}
		var iq = $iq({type: 'set', to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).c('unsubscribe',{node: Strophe.getResourceFromJid(node),jid: jid});	
		config.extensions.boshPlugin.connection.sendIQ(iq,function (iq) {displayMessage('sucess unsubscribing to Node');},function (iq) {displayMessage("Error unsubscribing to Node: "+Strophe.serialize(iq));});
	},
	// retrieve Node items, give the result to the specified callback function ...
	getItemsFromNode: function(node,callback) {
		var iq = $iq({type: 'get', to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).c('items',{node: Strophe.getResourceFromJid(node)});	
		config.extensions.boshPlugin.connection.sendIQ(iq,callback);
	},
    // gets the result of a getItemsFromNode run, converts Atom-ActivityStrea.ms (only small subset: author,title,content,published,modified) 
	// to Tiddler and saves tiddler to the store
	mapItemsToTiddlers: function(iq) {
		//story.closeAllTiddlers();
		var service =  $(iq).attr('from'); 
		var pubsubnode;
		var counter = 0;
		$(iq).find('items').each(function () {
			pubsubnode = $(this).attr('node');
		 $(iq).find('item').each(function () {
			var id = $(this).attr('id');
			$(this).find('entry').each(function () {
				counter = counter + 1;
				var author = "anonymous";
				var itemtitle = "";
				var content = "";
				var published = null;
				var updated = null;
				
				if (this.getElementsByTagName("title")) {
					if (this.getElementsByTagName("title")[0].childNodes[0]) {
						itemtitle = this.getElementsByTagName("title")[0].childNodes[0].nodeValue;
					}                                                                             
					else {itemtitle = 'undefined'}
				}	
				if (this.getElementsByTagName("content")[0]){
                                        if (content = this.getElementsByTagName("content")[0].childNodes[0]){
                                        	content = this.getElementsByTagName("content")[0].childNodes[0].nodeValue;
                                        	}
                                        else {content = "";}
                                }
				if (this.getElementsByTagName("author")[0]){
                                        author = this.getElementsByTagName("author")[0].childNodes[0].nodeValue;
                                }
				if (this.getElementsByTagName("summary")[0]){
                                        content = this.getElementsByTagName("summary")[0].childNodes[0].nodeValue;
                                }
				  if (this.getElementsByTagName("published")[0]){
                                        published = this.getElementsByTagName("published")[0].childNodes[0].nodeValue;
                                	published = new Date(published);
				}
  				if (this.getElementsByTagName("updated")[0]){
                                        updated = this.getElementsByTagName("updated")[0].childNodes[0].nodeValue;
                                	updated = new Date(updated);
				}
				var tiddler = store.createTiddler(id);
				tiddler.fields['doNotSave'] = true;                     // temporary ...
				tiddler.set(null,content,author,updated,"blog excludeLists "+service+"/"+pubsubnode,published,{id: id,node: service+"/"+pubsubnode,itemtitle: itemtitle,template: 'blog'});
				//story.displayTiddler(null,tiddler.title);
                	});
			});
			displayMessage("loaded "+ counter + " entries.");
		});
	},
	// doesnt work ...
	retrieveNodeInfo: function(node,callback) {
		//var iq = $iq({type: 'get', to: Strophe.getBareJidFromJid(node)}).c('query',{xmlns: "http://jabber.org/protocol/disco#info", node: Strophe.getResourceFromJid(node)});
		var iq = $iq({type: 'get', to: 'pubsub.jabber.systemli.org'}).c('query',{xmlns: "http://jabber.org/protocol/disco#info", node: "gaga@jabber.systemli.org/"+Strophe.getResourceFromJid(node)+"/"});
		
		config.extensions.boshPlugin.connection.sendIQ(iq,function(iq,callback) {
		   			var fields = {};
					$('x > field',iq).each(function() {
			        	       var search = /pubsub#(.+)/gi;
							   var result = search.exec($(this).attr('var'));
							   $('value',this).each(function(){
								  fields[result[1]].append($(this).text());
							    	displayMessag(result[1]);
							   }); 
					});
					
			},null);
	},
	// retrieve Items, display them in chronilogical order & render a post input panel on top for posting
	displayNode: function(node) {
					   var title = "";
					   var description = "";
					/* if (!config.extensions.boshPlugin.pubsub.nodes[node]) {              // get Node Info if there is no in the cache ...
							config.extensions.boshPlugin.pubsub.retrieveNodeInfo(node, function() {
								   config.extensions.boshPlugin.pubsub.displayNode(node);
							});
					   }
					   else { 
					     	title = config.extensions.boshPlugin.pubsub.nodes[node].title;
							description = config.extensions.boshPlugin.pubsub.nodes[node].description;	*/				   
					   		config.extensions.boshPlugin.pubsub.getItemsFromNode(node,function(iq){
					   			displayMessage("working on it");
					   			config.extensions.boshPlugin.pubsub.mapItemsToTiddlers(iq);
					   			var tids=store.getTiddlers();
					   			tids=store.sortTiddlers(tids,"modified");                     
					   			story.closeAllTiddlers();
					   			for (var t=0; t<tids.length; t++) {
									if (tids[t].isTagged(node)) {
										story.displayTiddler(null,tids[t].title,'blogViewTemplate',true,true);
									}
					   			}
					   			$('#nodePublishForm').replaceWith(config.extensions.boshPlugin.pubsub.nodePublishForm(node));
					   			if ($('#tiddlerDisplayHeader').css('display') == 'none') {
						 			$('#tiddlerDisplayHeader').slideToggle('slow');
								}
							});
					/*   }   */
	},
	//create the publish Form for a node
	nodePublishForm: function(node) {  
		var form = "<div id='nodePublishForm' style='padding-right:5%;padding-left:5%;margin: 0px auto;width:80%;' ><form name='form' id='form'>";
		form = form + " <input type='hidden' id='node' name='node' value='" + node  + "'/>"
	    form = form + "<label for='title'>Title</label><br/><input style='width:100%;' id='title' name='title'/><br/>";
		form = form + "<label for='content'>Message</label><br/><textarea style='width:100%;' name='content' id'='content'></textarea><br/>";
		form = form + "<input type='button' id='post' value='post' onClick='if (form.node.value && form.title.value && form.content.value) {var published = new Date();var updated = new Date();config.extensions.boshPlugin.pubsub.publishToNode(this.form.node.value,{title: this.form.title.value, content: this.form.content.value, published: published.formatString(\"YYYY-0MM-0DDT0hh:0mm:0ssZ\"),updated: updated.formatString(\"YYYY-0MM-0DDT0hh:0mm:0ssZ\"),author: config.options.txtUserName},function(iq) {displayMessage(\"hier muss eine funktion hin, die die id aus der stanza zieht: \"+Strophe.serialize(iq));config.extensions.boshPlugin.pubsub.displayNode(\""+node+"\")});} else {displayMessage(\"Not all variables set ...\")}'</div>";
		return form;
	},
	// publish a post to a node
	publishToNode: function(node,post,callback) { // post object has post.title,post.content, post.author, post.published,post.updated
		var iq;
		if (post.id) {
			iq = $iq({type: 'set',to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).c('publish',{node: Strophe.getResourceFromJid(node)}).c('item',{id: post.id}).c('entry',{xmlns: 'http://www.w3.org/2005/Atom',xmlnsactivity: "http://activitystrea.ms/spec/1.0/"});	
		}
	    else {
			iq = $iq({type: 'set',to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).c('publish',{node: Strophe.getResourceFromJid(node)}).c('item').c('entry',{xmlns: 'http://www.w3.org/2005/Atom',xmlnsactivity: "http://activitystrea.ms/spec/1.0/"});
		}
		//             <entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/"> 
        if (!callback) {
	    	callback = function (iq) {displayMessage("sucessfully published article");};
		}
		iq = iq.c('title').t(post.title).up().c('content').t(post.content).up().c('author').t(post.author).up().c('published').t(post.published).up().c('updated').t(post.updated);
				config.extensions.boshPlugin.connection.sendIQ(iq,callback,function (iq) {displayMessage("Error while Publishing Article: "+Strophe.serialize(iq));});
	},
	// retract a post from a node
	deleteFromNode: function(node,itemId) { // post object has post.title,post.content, post.author, post.published,post.updated
	var iq = $iq({type: 'set',to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).c('retract',{node: Strophe.getResourceFromJid(node)}).c('item',{id: itemId});
				config.extensions.boshPlugin.connection.sendIQ(iq,function (iq) {displayMessage('succesfully deleted Item');return false;},function (iq) {displayMessage("Error while Deleting Article: "+Strophe.serialize(iq));});
		return true;
	},
	//configure a node
	configureNode: function (node) {
		var iq = $iq({type: 'get', to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub#owner'}).c('configure',{node: Strophe.getResourceFromJid(node)});
		config.extensions.boshPlugin.connection.sendIQ(iq,config.extensions.boshPlugin.pubsub.configureNodeCallback, function (iq) {displayMessage("Error configuring node: "+Strophe.serialize(iq));})
	},
	//callback for configure node
	configureNodeCallback: function(iq) {
		var node;
		$(iq).find('configure').each(function() {
				node = $(this).attr('node');	
			});
		var returnFrame = $iq({type: 'set', to: $(iq).attr('from'), 
				//id: $(iq).attr('id')
				}).c('pubsub',
				 {xmlns: 'http://jabber.org/protocol/pubsub#owner'}).c('configure',{node: node});
		var id = new Date;
		iqFrameId = id.getTime();
		config.extensions.boshPlugin.pubsub.iqFrames[iqFrameId] = returnFrame;
		$(iq).find('x').each(function () {
				config.extensions.boshPlugin.pubsub.renderForm(this,iqFrameId);
			});
	},
	// get node Affiliations
	getAffiliations: function(node) {
		var iq = $iq({type: 'get',to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub#owner'}).c('affiliations',{node: Strophe.getResourceFromJid(node)});
		config.extensions.boshPlugin.connection.sendIQ(iq,config.extensions.boshPlugin.pubsub.getAffiliationsCallback,function (iq) {displayMessage("Error getting Node Affiliations, are you Owner?" + Strophe.serialize(iq));});
	},
	// Callback
	getAffiliationsCallback: function(iq) {
		$(iq).find('affiliation').each(function (){
			//var row = "<tr id='"+$(this).attr('jid')+"'><td>"+$(this).attr('jid')+"</td><td id=>" + $(this).attr + "</td></tr>";
			var oprow = "";
			var affiliation = $(this).attr('affiliation');
			["owner","member","publisher","none","outcat"].forEach(function(option){
				if (option == affiliation) {
					oprow = oprow + "<option selected='yes'>" + option +"</option>";
				}
				else {
					oprow = oprow + "<option>" + option +"</option>";
				}
			});
			var row = "<br>" + $(this).attr('jid') + " " + "<select id='"+$(this).attr('jid')+"'>"+oprow+"</select>";
			$("#affiliationsdiv").append(row);
		});
		
	},
	// create Node
	createNode: function(node) {
		var iq = $iq({type: 'set', to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).c('create',{node: Strophe.getResourceFromJid(node)});
		config.extensions.boshPlugin.connection.sendIQ(iq,function (iq) {displayMessage("sucessfully created node");},function (iq) {displayMessage("Error while creating node: "+Strophe.serialize(iq));});
	},
	// delete Node
	deleteNode: function(node) {
		var iq = $iq({type: 'set', to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub#owner'}).c('delete',{node: Strophe.getResourceFromJid(node)});
		config.extensions.boshPlugin.connection.sendIQ(iq,function (iq) {displayMessage("sucessfully deleted node");},function (iq) {displayMessage("Error while deleting node: "+Strophe.serialize(iq));});
	},
	//associate Node (Collection Nodes)
    associateNode: function(affiliationnode,affiliatednode) {
	  	var iq = $iq({type: 'set', to: Strophe.getBareJidFromJid(affiliationnode)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub#owner'}).c('collection',{node: Strophe.getResourceFromJid(affiliationnode)}).c('associate', {node: affiliatednode});
		config.extensions.boshPlugin.connection.sendIQ(iq,function (iq) {displayMessage("Success associating Nodes.");},function (iq) {displayMessage("Success associating Nodes.");});
	},
	/*
	render an XMPP Data Form to Html, gets the iqFrameIndex of the Iq frame, 
	the filled form should be attached to for sending it back ...
	*/
	renderForm: function(iq, iqFrameIndex) {
	var title;
	var form = "";
	$(iq).find('field').each(function () {              // convert each field type to html Input types ... mybe buggy
		if ($(this).attr('type') == 'boolean') {//form = form + "bool ";
			form = form + "<br><label>"+$(this).attr('label')+"</label><input type='checkbox' id='"
						+$(this).attr('var')+"'  ";
			if (this.getElementsByTagName("value")[0].childNodes[0].nodeValue == 1) {
				form = form + "checked='checked'";
			}
			form = form + "/>";
		}
		if ($(this).attr('type') == 'fixed') {//form = form + "fixed ";
			form = form + "<br><br><label>"+$(this).attr('var')
					+"' value='"+this.getElementsByTagName("value")[0].childNodes[0].nodeValue+"</label><br>";
		}
		if ($(this).attr('type') == 'hidden') {//form = form + "hidden ";
			form = form + "<br><input type='hidden' id='"+$(this).attr('var')
					+"' value='"+this.getElementsByTagName("value")[0].childNodes[0].nodeValue+"'/>";
		}
		if ($(this).attr('type') == 'jid-multi') {//form = form + "jid-multi ";
			var value = "";
			if (this.getElementsByTagName("value")[0].childNodes[0]) {
					value = this.getElementsByTagName("value")[0].childNodes[0].nodeValue;
			}
			form = form + "<br><label>Jid-Multi "+$(this).attr('label')
					+"</label><input type='textarea' rows='2' id='"+$(this).attr('var')+"' value='"+value+"'/>";
		}
		if ($(this).attr('type') == 'jid-single') {//form = form + "jid-single ";
			form = form + "<br><label>Jid-Single "+$(this).attr('label')+"</label><input type='textarea' rows='2' id='"+$(this).attr('var')+"' value='"+this.getElementsByTagName("value")[0].childNodes[0].nodeValue+"'/>";
		}
		if ($(this).attr('type') == 'list-multi') {//form = form + "list-multi ";
			form = form + "<br><label>"+$(this).attr('label')+"</label><select id='"+$(this).attr('var')+"' multiple>";
			var def;
			if ( $('> value', this) ) {   /// get already set values
				def = $('> value', this);
			}
			$(this).find('option').each(function(){
				form = form + "<option value='"+this.getElementsByTagName("value")[0].childNodes[0].nodeValue+"'";
				if (def.has(this.getElementsByTagName("value")[0])) {form = form + "selected='selected'"}
				form = form + ">";
				if ($(this).attr('label')) {form = form +$(this).attr('label');}
				else {form = form + this.getElementsByTagName("value")[0].childNodes[0].nodeValue;}
				form = form +"</option>";
			});
			form = form + "</select>";
		}
		if ($(this).attr('type') == 'list-single') {//form = form + "list-single ";
			form = form + "<br><label>"+$(this).attr('label')+"</label><select id='"+$(this).attr('var')+"'>";
			var def;
			if ( $('> value', this) ) {   /// get already set values
				def = $('> value', this)[0].childNodes[0].nodeValue;
			}
			$(this).find('option').each(function(){
				form = form + "<option value='"+this.getElementsByTagName("value")[0].childNodes[0].nodeValue+"'";
				if (this.getElementsByTagName("value")[0].childNodes[0].nodeValue == def) {
					form = form + " selected='selected'";
				} 
				form = form +">";
				if ($(this).attr('label')) {form = form +$(this).attr('label');}
				else {form = form + this.getElementsByTagName("value")[0].childNodes[0].nodeValue;}
				form = form +"</option>";
			});
			form = form + "</select>";
		}
		if ($(this).attr('type') == 'text-multi') {//form = form + "text-multi ";
				var value = "";
				if (this.getElementsByTagName("value")[0] && this.getElementsByTagName("value")[0].childNodes[0]) {value = this.getElementsByTagName("value")[0].childNodes[0].nodeValue;}
				form = form + "<br><label>TextMulti: "+$(this).attr('label')+"</label><input type='textarea' rows='2' id='"+$(this).attr('var')+"' value='"+ value+"'/>";
		}
		if ($(this).attr('type') == 'text-private') {//form = form + "text-private ";
				var value = "";
				if (this.getElementsByTagName("value")[0].childNodes[0]) {value = this.getElementsByTagName("value")[0].childNodes[0].nodeValue;}
				form = form + "<br><label>TextPrivate "+$(this).attr('label')+"</label><input type='textarea' rows='2' id='"+$(this).attr('var')+"' value='"+ value+"'/>";
		}
		if ($(this).attr('type') == 'text-single') {//form = form + "text-single ";
				var value = "";
				if (this.getElementsByTagName("value")[0].childNodes[0]) {value = this.getElementsByTagName("value")[0].childNodes[0].nodeValue;}
				form = form + "<br><label>textSingle "+$(this).attr('label')+"</label><input type='text' id='"+$(this).attr('var')+"' value='"+ value+"'/>";
		}
	});
	var tiddler = store.createTiddler('form');             // create a tiddler with the form
	tiddler.fields['doNotSave'] = true;
	tiddler.set(null,"<html><div id='"+iqFrameIndex        // add the button to call sendForm to send it away
				+"'><input id='iqFrameForm' type='hidden' value='" 
				+ iqFrameIndex + "'><form id='DataForm'>"+form 
				+"</form><br><input type='button' label='send' value='send' onClick='config.extensions.boshPlugin.pubsub.sendForm(\""
				+iqFrameIndex+"\");story.closeTiddler(\"form\");store.removeTiddler(\"form\")'></div></html>");
	story.displayTiddler(null,tiddler.title);              // display the form tiddler with the form
    },
    /*
		converts the HTML Form back to XMPP Data Form and sends it away
	*/
	sendForm: function(iqFrameIndex){                      
			displayMessage("gotit");
    		var iq = $iq({type: 'set'}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub'}).configure;
    		var returnIq = config.extensions.boshPlugin.pubsub.iqFrames[iqFrameIndex];
    		delete config.extensions.boshPlugin.pubsub.iqFrames[iqFrameIndex];
    		returnIq.c('x',{type: 'form', xmlns: 'jabber:x:data', type: 'submit'});
     		var form = $('#'+iqFrameIndex);        // finds the form via the id attribute not a good solution i quess ...
			$('select',form).each(function() {
    			var id = $(this).attr('id');
    			returnIq.c('field',{var: id});
    			$('option:selected',this).each(function(option,selected){ 
  					returnIq.c('value').t($(selected).text()).up();
  				});
  				returnIq.up();
  			});
	  		$("input[type=checkbox]",form).each(function() {
				var value;
				if ($(this).attr('checked')) {
				returnIq.c('field',{var: $(this).attr('id')}).c('value').t('1').up().up();
				}
				else {
				returnIq.c('field',{var: $(this).attr('id')}).c('value').t('0').up().up();
				}
			});
			$("input[type!=checkbox]",form).each(function() {
				returnIq.c('field',{var: $(this).attr('id')}).c('value').t($(this).attr('value')).up().up();
			});

       		config.extensions.boshPlugin.connection.sendIQ(returnIq, function() {
			displayMessage("success updating node Configuration");},function(iq) {displayMessage("error updating node Configuration: " + Strophe.serialize(iq));});
    }

};
/*
  		PubSub Macros
*/  
config.macros.bosh_retrieveNodeItems = {
	 handler: function (place, macroName, params, wikifier, paramString, tiddler) {
		var prms = paramString.parseParams(null, null, true);
		var node = getParam(prms, "node");
		var form = "<html><form>";
		if (!node) {
			form = form + "<label for='name'>Nodename (eg. shouts@pubsub.<br/>jabber.mydomain.org)</label><input name='jid' type='text' id='name'/>";
		}
		else {
			form = form + "<input name='jid' type='hidden' value='"+node+"' id='name'/>";
		}
			
		form = form + "<input type='button' id='Add' value='Get Items from Node' onclick='config.extensions.boshPlugin.pubsub.getItemsFromNode(this.form.jid.value)' /></form></html>";
		wikify(form,place);
	 }
};

config.macros.bosh_createNode = {                            ///create a pubsub node
         handler: function (place, macroName, params, wikifier, paramString, tiddler) {
                var prms = paramString.parseParams(null, null, true);
				var node = getParam(prms, "node");    
				var form = "<html><form>";
			   	if (!node) {
						form = form + "<label for='node'>pubsub node: </label><input name='node' type='text' id='node'/>";
					}
					else {
						form = form + "<input name='node:' type='hidden' id='node' value='"+node+"'/>";
				}
				form = form + "<input type='button' value='create Node' onclick='config.extensions.boshPlugin.pubsub.createNode(form.node.value);'></form></html>";
				wikify(form,place);
         }
};

config.macros.bosh_Subscriber = {                        //subscripe unsubscribe to a node
	handler: function (place, macroName, params, wikifier, paramString, tiddler) {
		var prms = paramString.parseParams(null, null, true);
		var node = getParam(prms, "node");
		var form = "<html><form name='form' id='form'>";
		if (!node) {
			form = form + "<label for='node'>pubsub node</label><input name='node' type='text' id='node'/>";
		}
		else {
			form = form + "<input name='node' type='hidden' id='node' value='"+node+"'/>";
		}
		form = form + "<input type='button' value='subscribe' onclick='config.extensions.boshPlugin.pubsub.subscribeToNode(form.node.value,config.extensions.boshPlugin.jid)'/><input type='button' value='unsubscribe' onclick='config.extensions.boshPlugin.pubsub.unsubscribeFromNode(form.node.value,config.extensions.boshPlugin.jid)'/> </form></html>";
		wikify(form,place);
	}
};
config.macros.bosh_configureNode = {                 // configure a node
         handler: function (place, macroName, params, wikifier, paramString, tiddler) {
                var prms = paramString.parseParams(null, null, true);
				var node = getParam(prms, "node");
				var form = "<form id='form'>";
				if (!node) {form = form + "<label for='node'>pubsub node</label><input type='text' id='node'>";}
				else {form = form + "<input type='hidden' id='node' value='"+node+"'>";}
				form = form + '<input type="button" id="configure node" value="configure node" onClick="config.extensions.boshPlugin.pubsub.configureNode(form.node.value)" ></form>';
				wikify("<html>"+form+"</html>",place);
		}
};

config.macros.bosh_publish = {
        handler: function (place, macroName, params, wikifier, paramString, tiddler) {
		var prms = paramString.parseParams(null, null, true);
		var node = getParam(prms, "node");
		var form = "<html><div width='100%' ><form name='form' id='form'>";
		if (!node) {
                        form = form + "<label for='node'>PubSub Node</label><input id='node' name='node'/><br/>";
                }
		else {
                        form = form + " <input type='hidden' id='node' name='node' value='" + node  + "'/>"
                }
		form = form + "<label for='title'>Title</label><br/><input width='100%' id='title' name='title'/><br/>";
		form = form + "<label for='content'>Message</label><br/><textarea width='100%' name='content' id'='content'></textarea><br/>";
		form = form + "<input type='button' id='post' value='post' onClick='if (form.node.value && form.title.value && form.content.value) {var published = new Date();var updated = new Date();config.extensions.boshPlugin.pubsub.publishToNode(this.form.node.value,{title: this.form.title.value, content: this.form.content.value, published: published.formatString(\"YYYY-0MM-0DDT0hh:0mm:0ssZ\"),updated: updated.formatString(\"YYYY-0MM-0DDT0hh:0mm:0ssZ\"),author: config.options.txtUserName});} else {displayMessage(\"Not all variables set ...\")}'</div></html>";
		wikify(form,place);
	}
};

config.macros.bosh_lLog = {
        handler: function (place, macroName, params, wikifier, paramString, tiddler) {
		html = "<html><textarea>" + config.extensions.boshPlugin.rawLog + "</textarea></html>";
		wikify(html,place);
		}
};
config.macros.bosh_pubsubManager = {
		getNodes:  function (form) {
			config.extensions.boshPlugin.pubsub.findNodes(form.service.value);
			var options = "";
			for (var node in config.extensions.boshPlugin.pubsub.nodes){
				options = options + "<option id='" + node + "'>"+node+"</options>"
			}
			$('#form_pubsubmanager_NodeSelect').replaceWith("<select id='form_pubsubmanager_NodeSelect' onchange='config.macros.bosh_pubsubManager.renderFormForNode(this.value)'>" + options + "</select>");
			
		},
		renderFormForNode: function (node) {
			var form =  "<html><input type='button' value='delete node' onClick='config.extensions.boshPlugin.pubsub.deleteNode(\""+node+"\")'></html>"
			form = form + wikifyStatic("<<bosh_retrieveNodeItems node:'"+node+"'>>");
			form = form + wikifyStatic("<<bosh_Subscriber node:'"+node+"'>>");
			form = form + wikifyStatic("<<bosh_configureNode node:'"+node+"'>>");
			form = form + wikifyStatic("<<bosh_publish node:'"+node+"'>>");
			form = form + wikifyStatic("<<bosh_affiliationsManager node:'"+node+"'>>");      
			
			$('#FormForNode').replaceWith("<div id='FormForNode'> <h2>Node: " + node + "</h2><br>"+form + "</div>");
		
		},
        handler: function (place, macroName, params, wikifier, paramString, tiddler) {
			var form = "<html><form id='form_pubsubmanager'><label for='service'>pubsubserver:</label><input type='text' id='service'><input type='button' value='discover nodes' onClick='config.macros.bosh_pubsubManager.getNodes(form);'/><select id='form_pubsubmanager_NodeSelect' onchange='' ></select><div id='FormForNode'></div></form></html>";
			form = form + "<<bosh_createNode>>";
			wikify(form,place);
	},
};	

config.macros.bosh_affiliationsManager = {
		handler: function (place, macroName, params, wikifier, paramString, tiddler) {
			var prms = paramString.parseParams(null, null, true);
			var node = getParam(prms, "node");
			config.extensions.boshPlugin.pubsub.getAffiliations(node);
			var form = "<html><div id='affiliationsdiv'></div><form id='addJidForm'><label for='jid'>Add Jid:</label><input type='text' id='jid'><input type='button' value='add' onClick='config.macros.bosh_affiliationsManager.addJid(form.jid.value)'><input type='button' value='save user affiliations' onClick='config.macros.bosh_affiliationsManager.sendForm(form,\""+node+"\")'><form></html>";
	
			wikify(form,place);
		},
		addJid: function(jid) {
			$("#affiliationsdiv").append("<br>" + jid + " <select id='" + jid + "''><option>owner</option><option>member</option><option>publisher</option><option>publish-only</option><option>none</option><option>outcast</option></select>");
		},
		sendForm: function(form,node) {
			var iq = $iq({type: 'set', to: Strophe.getBareJidFromJid(node)}).c('pubsub',{xmlns: 'http://jabber.org/protocol/pubsub#owner'}).c('affiliations',{node: Strophe.getResourceFromJid(node)});
			$("#affiliationsdiv").find('select').each(function() {
				iq.c('affiliation',{jid: $(this).attr('id'),affiliation: $(this).attr('value')}).up();
			});
			boshPlugin.connection.sendIQ(iq,function (){displayMessage("success updating Affiliations");},function (iq){displayMessage("Error updating Affiliations " + Strophe.serialize(iq));});
	//		displayMessage(Strophe.serialize(iq));
		},
	};
                                              

})(jQuery);
(function($) {
// standard icon bas64encoded
config.options.txtBoshAnonymousIcon = 'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZiS0dEAAAAAAAA+UO7fwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAl2cEFnAAAAMAAAADAAzu6MVwAAC6dJREFUaN7NmmmMHMd1x/9V3dXXdM/RMzvD2eVwd7VLmodEyhTJRGYgiIJDEZZhS45tKUGQwDBgIHEQI0HyyREkIECAfAmQAEGgLwFywB8dJEAuSSZNSmYoUL4oUYwkcsnV3rMzs9sz3dPd1d1V+bCHlrID7xAjyg/oT9Ooer96R733egg+Jjl58iSWl5f1J5988lA+n/+spmm/Til9WErphmG4tLq6+hdvv/32P/q+379169Y976MMW/ETJ07gxRdfxOXLl4tPP/30bzYajW/XarUv1+v1Q6Ojo3a9Xqe1Wq1gGMaBJEnWFhYW3nNdN1lbW/vlAFhcXIRpmpPHjh37o8nJyW+5rnvYdV2DEEKEEFAUBaZpolKplGu12qFisfjOK6+8MiOl/OUAOHv2bP706dPfnp6e/j3LslwASNMUlmWhWq2iVCrBtm3k83kUCgW33++vrqysXFxeXk7vZT86bIBGo3FgdHT0XD6fN9M0Ra/XQ7lcxp49e2AYBiilIIRASglN02ihUPjU6Oio/dhjj8EwjE8W4Ny5cyiVSvuKxeJIPp+HpmnYs2cPXNcFIeRnN6cUtm0/YBhG49KlS3jhhRdw6tSpgfYcmgs999xzuHjxovH4449/cWpq6rO5XE5xHAf5fP7nKg8AhBBEUWRzzounT58mN27cCF9++WX/2WefFdevX7+/ANPT05VnnnnmawcPHvy667pVQgjZcpdfICyXyx154IEHzoyNjZ08duyYsrCwMPvuu+9Gu9lXHRYAgF8dGxv7k0ajsZdSiv8vq+wEklLCMAwUCgUln8/XSqXSWcbYwWaz6QH47n0FWFpa8mZmZvqlUgmu60LTdVBCsdMAQgikaYokSRDHHFEcI44iJAlHq9WSnHO+srISNJvNXWekoQF4nveT995776/jOP6tYjF/yM7ZbrFYgGlaUJkGCSBLM/CEg0d9RL0WMn8BvSBciZTapUySZhRFS61W683FxcXL9x2gWCz2Lly48PcPHTn0vSJZ/FbJTL/R2GMr7SiPsaoDwzRBFRUqOJjw4GSLMEQbcTOeOX+j+MKdjjU3M3MrllImjz766K73HRrAlStXcPTo0ej7l15//48/h7mj+yBVBbjWIhivENiCQGYElEgoRIBSCVDAUMARhOtTU2d9191wv0HKimEGMYS4DQCIOdbWfETVAuyTUxKWIUE/kowIAXgCEYS41efw33j1ZdxLNTHkUiJRC8XqvkCOfCZJcUqk3NSYhGPirmCWEgg5we1VOnftA/I3//FTea1er8P3/U8OYHx8CpUKThw5Yv6VUPZ+JSKVgswy4uo+iATiZOPpx8BaD/jxnIMrK58K5+J9kWXlSKfT6XLO+58IwPj4ODzPK+Tzpd8/dcr8KmDniALSTQia7WxDcU4RcIpOoGG+l8dsOA6uj9mqqj5CCDnDGIu63e4PhRADFXVDiYHZ2Vnl8OHDTxmG/WwQKPr4uECWcTh5D901HfOzZawvM+RzBBks9FMD/URB0u+BUqqUSqUxxtixVqtldTqdXd3AQwXYv3//w67r/qGqsr3Npo6xsTZ03Ue53Mf4PiCMJwGNoVJp49o1BgkFcrM3UFUVSZIgy7KK4zhWp9Pp3DeAw4cPw/O8eqVS+aZt28c55+Cc4ebNCgzDQLcbwDAkqtUIExMhlpZsEKLB0HUkSQLLsqAoCuI4xvr6ejmKImdQHe65nM7lbNy8edOoVqvPFQqFLyqKwoCNWmdtzcDSkg3bzsA5Q5pStNsmbt8ug1KKNE1BCIGibISgrutwHGfEsqzRJEkG0uOeLSAk6PT+/Wdc1/2Grusu5xyEkM0HAAh6PQXdro75+T2QkkKIDUAhBCi9++wcx6mPjIycq1Qq/wNg19loIAt86RHgz74A/PlvAAqlBcswf9s0zQPbSm0CbIiEaQpomkCaEkhJdgBuvC+E2F6bMabZtv1UtVp98GOxwNdOAz+dgxEnONDnsI/VfWOfMzudCoXGooo0zaAoFASAxMbFlaYEWXb3FUwIAaUUQggkSQJd17d/MwxjwrbtUwCubi4zPICrd2Ad3Yvf2TeCb/IUBUpEXHdXx4IslG82w+6ap627lfKopJRBSEi5AbHzBt55+pqmIUkSaJq2bTVCCCWEMGD7HIYHMFrEp/eN4A8eGseRVhfgKVArAOuBLwrJ/34/mC81J9Pkd71KBaGVA1UASiU+4upQFGU7iIUQkFJuA8RxvBwEwY8BiN3qtVsAWrDwcK2IibIDtLoAJYDrAI4J2ou4+StvrBhnOh11wbZxtVzBrSkLIDFint84ys1JxJbinHMoirIdzFmWwfO85VardXu3yg9kAQCi24ecbQIdH5ioAZQCGgN0Bmde15ZaqZI92O3Sh7rrmHlCAXmC4u23FHRXm1jzXEhNA1EUpFkGKSUc58O0zzkHANu27Vy73d61UruthWTBRBQlUBY6EK4Na6IGkxKQbgj/zgq+8+/R5I2bE5NnbhaKeocCxgmO41/mePDBANYPOyhf6IHwBCGlaAcBeMLBmAaqKICUiOMYiqIkYRj+Z7vdnh02AObX0BICP+hF+F7JxnWeot/xMTu7iu+8NYt/WCWjFbVS+dxadcS8WRnBrbSAxSZDFAuUfhTi5OseDL+FXthBK/BQK7VQ1tpY62WIUgWccyRJ0ltfX/8Xz/PuDN2FXAuiYMEbLcD721fx/iOT+FdLg/rBKnrv/x349J92615HX85su8R0HbE3gvaFKi5fDlFotjCx9zbU8S5Maw2HAmB8FKgVPVy5E+LKUoacUwQhxBNCNHftP4MAdPrAGzOAowPnHoI40oBn6cAHq8DU1wFnn60UCgWmaRo45wg5R0QIwlBDT3OhHJzDmQNAyAF1FagWNzIZoRSmacIwzLTfD5aEEMHHArAlvRj4r7c2np1yGICiKGRDGQNZlsH3fax769DFOmrjPRQsoNf/MIMpBGhFGWZiHb0wbLZarX8Kw3BpEH2GNhsNgmA+iqL3syyThBCoqgpVVaFpGhSVodlleOcDYKEFlPMAUwBVAXQGGKaBfCFfdBynHMfxru8AYIgtZb/fDy3LmiCEfAaASgjZqPPTDEzLoS8dzLclSlaC/fUMqgJ4fQXXVutYF3UoKmMJ570gCP7b9/1dNzVDm0okSZKoqtphjKVpmupxHEMIgTiOoCgWqNNAalZxJ2sjuLMEk6VY4y6a6V4QlQFSglLqEkL0QfYd6liFMUZN0wRjDGmagnOOOI4RBAEIIbCsHAI6htVuAYBAzs6DqGRn4SN2MQz++ACklNtD3a0YAADP85CmKcKwv12JpmkKc7Ot3BJCiMAui7gtGUoQ5/N5bG5MNvT48BS3ymfLsuA4DnRdB6UUcRz/zBxISikwQCEHDMkCnHOjVqt9OkmSk2EYsi0LfLTropRC0zRomgbGGLbiZMd7GSFkIAsMBaBUKk1Wq9W/tCzrZBzHLEkSqKoKxthdXddO2VmJbomUUsgBP1cOBcCyLLtcLjcMwzDiOAbnHGmaIo5jhGEIRVGQbVag5K62Ex/9gpNhwBgYCoAQQgIQW+6RZRnCMNwOXlVVwTlHr9cDYwyMsbsamZ1LfSIAYRjOeZ73zwA+bxjGQcaYZVkWoigCpRS5XG7jeLNs2zJpmoIxtr3GZgYbGGAoN7Hv+4Hv+1ellK9zzu8IIZiUshDHMaOUkmKxSBhj2z7POYcQApZlbUNIKdHv99/pdDr/5vt+fF8BAGBkZCQ5fvz48vnz569KKc9HUXS91+vNAGiZpllWFMXSNI3our795d40TUgpkWVZGkXRUrfbfbXT6VwMw3DX063Brr1fIFt/J2g0GnjttdcIANZoNOqu6/5aLpd7wjTNRw3DmAjD0FRVVVBK16MoejeKoh/4vn+p0+m8ubi4uIwB3GioAD9PGo0G5ubmUKlUypVK5Wgul/t8mqZPUUoXhRDf9X3/0uzs7K3nn38+eOmll7C4uDjQ+v8HNWpR3FE5fRkAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTAtMDItMTZUMjI6Mzc6NDUtMDc6MDC9Cg3+AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDEwLTAyLTE2VDIyOjM3OjQ1LTA3OjAwzFe1QgAAADJ0RVh0TGljZW5zZQBodHRwOi8vZW4ud2lraXBlZGlhLm9yZy93aWtpL1B1YmxpY19kb21haW4//erPAAAAEHRFWHRTb3VyY2UAV1BDbGlwYXJ04R6OYQAAACR0RVh0U291cmNlX1VSTABodHRwOi8vd3d3LndwY2xpcGFydC5jb20vNJf0nwAAAABJRU5ErkJggg=='
boshPlugin = config.extensions.boshPlugin;
/*
Manages the Roster, and the Rosterline, where (online) Contacts are displayed
*/
var roster;
roster = config.extensions.boshPlugin.roster = {
contacts: {}, // stores roster contacts
vCardQue: [],
runningRecursiveVCardRequest: 0,
// get it from the server
queryRoster: function() {
config.extensions.boshPlugin.log("querying Roster...");
var iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
config.extensions.boshPlugin.connection.sendIQ(iq,config.extensions.boshPlugin.roster.onRoster);
},
    // and when you got it, populate your contacts Object
onRoster: function(iq) {
       config.extensions.boshPlugin.log("got Roster...");
var all_jids = new Array();
            // We add ourselves
            all_jids.push(boshPlugin.jid);
            config.extensions.boshPlugin.roster.contacts[boshPlugin.jid] = {}; // Roster Contact für uns selber
config.extensions.boshPlugin.roster.contacts[boshPlugin.jid].jid = boshPlugin.jid;
config.extensions.boshPlugin.roster.contacts[boshPlugin.jid].name = boshPlugin.jid;
config.extensions.boshPlugin.roster.contacts[boshPlugin.jid].presence = "online";
config.extensions.boshPlugin.roster.contacts[boshPlugin.jid].resources = {};
config.extensions.boshPlugin.roster.contacts[boshPlugin.jid].photohash = "";
    config.extensions.boshPlugin.roster.contacts[boshPlugin.jid].divid = Math.floor(Math.random() * 2000);
            // and the others
       $(iq).find('item').each(function () {
                          var jid = $(this).attr('jid');
                          all_jids.push(jid);
var name = $(this).attr('name') || jid;
var id = jid;
config.extensions.boshPlugin.roster.contacts[id] = {};
config.extensions.boshPlugin.roster.contacts[id].jid = jid;
config.extensions.boshPlugin.roster.contacts[id].name = name;
config.extensions.boshPlugin.roster.contacts[id].presence = "offline";
config.extensions.boshPlugin.roster.contacts[id].resources = {};
config.extensions.boshPlugin.roster.contacts[id].photohash = "";
config.extensions.boshPlugin.roster.contacts[id].divid = Math.floor(Math.random() * 2000);
});
       //config.extensions.boshPlugin.roster.refreshRosterTiddler();
       //config.extensions.boshPlugin.roster.getRosterVCards(all_jids, function() {config.extensions.boshPlugin.connection.send($pres());});
config.extensions.boshPlugin.connection.send($pres()); //sends its presence because the getRoster function is called after login, shouldnt be here ...
config.extensions.boshPlugin.connection.addHandler(config.extensions.boshPlugin.roster.onPresence, null, "presence"); // ad a Handler for incomming Presence Stanzas

},
/*
On Presence, exmines, if someone comes online, calls requestVCard to get the Icon, adds / removes contact to/from rosterline ...
*/
onPresence: function (presence) {
var ptype = $(presence).attr('type');
var from = $(presence).attr('from');
var bare_jid = Strophe.getBareJidFromJid(from);
var resource = Strophe.getResourceFromJid(from);
var id = bare_jid;
var photohash = $("x > photo",presence).text();
if (photohash != config.extensions.boshPlugin.roster.contacts[id].photohash) { // check, wheter the avatar has changed
//config.extensions.boshPlugin.roster.requestVCard(id);
config.extensions.boshPlugin.roster.contacts[id].photohash = photohash;
}
if (ptype !== 'error') {
if (ptype == 'subscribe') {
displayMessage(bare_jid +" wants to subscribe to your presence ...");
}
if (ptype == 'unavailable'){
displayMessage(from + " went offline ...");
delete config.extensions.boshPlugin.roster.contacts[id].resources[resource];

}
else {
config.extensions.boshPlugin.roster.contacts[id].resources[resource] = {};
var show = $(presence).find("show").text();
displayMessage(id + " " +show);
if (show === "" || show === "chat") {
config.extensions.boshPlugin.roster.contacts[id].resources[resource].online = "online";
} else {
config.extensions.boshPlugin.roster.contacts[id].resources[resource].online = "away";
}
config.extensions.boshPlugin.roster.contacts[id].resources[resource].status = $(presence).find("status").text();
}
}

var onlineResourcesExist = 0;
for(var prop in config.extensions.boshPlugin.roster.contacts[id].resources) {
onlineResourcesExist = 1;
}
if (onlineResourcesExist == 1) {
//config.extensions.boshPlugin.roster.requestVCard(id);
if (config.extensions.boshPlugin.roster.contacts[id].presence = "offline") {
// Contact has been offline and just came online
// and we add him to the rosterline
config.extensions.boshPlugin.roster.addContactToRosterline(id);
}
config.extensions.boshPlugin.roster.contacts[id].presence = "online";
//config.extensions.boshPlugin.roster.refreshRosterLine();
}
else {
if (config.extensions.boshPlugin.roster.contacts[id].presence = "online") {
// contact was online and went offline, so we remove him from the rosterline
config.extensions.boshPlugin.roster.removeContactFromRosterline(id);
}
config.extensions.boshPlugin.roster.contacts[id].presence = "offline";
     }
//store.notify("MainMenu",1);
//refreshDisplay(true);
return true;
},
// add the image to the rosterline
addContactToRosterline: function(id){
if (config.extensions.boshPlugin.roster.contacts[id].photo) {
var photo = config.extensions.boshPlugin.roster.contacts[id].photo;
     roster = document.createElement("div");
     $(roster).attr("style", "float:left;width:"+config.macros.bosh_rosterLine.width+";");
     $(roster).addClass("rosterItem");
     var img = document.createElement("img");
     $(roster).attr("id",config.extensions.boshPlugin.roster.contacts[id].divid);
     img.addEventListener('click',function(event){
     jQuery(this).next().css("top",event.layerY-10);
     jQuery(this).next().css("left",event.layerX-10);
     jQuery(this).next().css("width","100px");
     jQuery(this).next().toggle();
     },true);
     //$(img).addClass("rosterItem");
     $(img).attr("width",config.macros.bosh_rosterLine.width);
     $(img).attr("height",config.macros.bosh_rosterLine.width);
     $(img).attr("title",config.extensions.boshPlugin.roster.contacts[id].jid);
     $(img).attr("src",photo);
     $(roster).append(img);
     var menu = document.createElement("span");
        $(menu).attr("id","menu");
        $(menu).attr("style","background-color:#E8E8E8;z-index:2;margin:3px;position:absolute;width:0px;display:none;");
     $(menu).append("<a onclick='config.extensions.boshPlugin.chat.createConversation(\"" + id + "\");jQuery(this).parent().hide();'>start chat</a><br><a onclick='config.extensions.boshPlugin.pubsub.displayNode(\"" + id + "/urn:xmpp:microblog:0\");jQuery(this).parent().hide();'>show dashboard</a>");
     $(menu).bind('mouseout',function(e){
     if (!e) var e = window.event;
var tg = (window.event) ? e.srcElement : e.target;
if (tg.id != 'menu') return;
var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
while (reltg != tg && reltg.nodeName != 'BODY')
reltg= reltg.parentNode
if (reltg== tg) return;
jQuery(this).css("left","0px");
     jQuery(this).hide();
     },false);
     $(roster).append(menu);
$('#'+config.extensions.boshPlugin.roster.contacts[id].divid).remove(); ///remove previous existing Entry
$(roster).hide();
$('#rosterline').append(roster);
$('#'+config.extensions.boshPlugin.roster.contacts[id].divid).fadeIn('slow');
}
else {
config.extensions.boshPlugin.roster.recRequestVCard(id,config.extensions.boshPlugin.refreshRosterLine);
}
},
// remove someone from roesterline

removeContactFromRosterline: function(id) {
$('#'+config.extensions.boshPlugin.roster.contacts[id].divid).fadeOut('slow');
$('#'+config.extensions.boshPlugin.roster.contacts[id].divid).remove();
},
// rebuild the rosterline
refreshRosterLine: function() {
$('#rosterline > *').remove();
for (buddy in config.extensions.boshPlugin.roster.contacts) {
if (buddy.presence = 'online') {
config.extensions.boshPlugin.roster.addContactToRosterline(buddy);
}
}
},
recRequestVCard: function(id,callback) {
config.extensions.boshPlugin.roster.vCardQue.push(id);
if (config.extensions.boshPlugin.roster.runningRecursiveVCardRequest == 0) {
config.extensions.boshPlugin.roster.getRosterVCardsFromVCardQue(callback);
}
},
        getRosterVCardsFromVCardQue: function(callback) {
var limit = 5;
config.extensions.boshPlugin.roster.runningRecursiveVCardRequest = 1;
if (config.extensions.boshPlugin.roster.vCardQue.length < limit) limit = config.extensions.boshPlugin.roster.vCardQue.length;
for (var i = 0; i < limit;i++) {
var iq = $iq({type: 'get',to: config.extensions.boshPlugin.roster.vCardQue.shift()}).c('vCard', {xmlns: 'vcard-temp'});
if (i < limit) boshPlugin.connection.sendIQ(iq,config.extensions.boshPlugin.roster.requestVCardCallback,null);
else {
boshPlugin.connection.sendIQ(iq,function() {
config.extensions.boshPlugin.roster.requestVCardCallback;
config.extensions.boshPlugin.roster.getRosterVCardsFromVCardQue(config.extensions.boshPlugin.roster.vCardQue,callback);
},function() {
config.extensions.boshPlugin.roster.requestVCardCallback;
config.extensions.boshPlugin.roster.getRosterVCardsFromVCardQue(config.extensions.boshPlugin.roster.vCardQue,callback);
});
}
}
if (config.extensions.boshPlugin.roster.vCardQue.length == 0 ) {
config.extensions.boshPlugin.roster.runningRecursiveVCardRequest = 0;
if(typeof callback == 'function'){
         callback.call(this);
}
}

},
// request the VCard of a contact
requestVCard: function(jid) {
var iq = $iq({type: 'get',to: jid}).c('vCard', {xmlns: 'vcard-temp'});
boshPlugin.connection.sendIQ(iq,config.extensions.boshPlugin.roster.requestVCardCallback,null);
},
// on VCard just extracts the base64 encoded icon and stores the data-url for the user in it´s contact
// if no icon is found, the standard icon is used.
requestVCardCallback: function(iq) {
var jid = Strophe.getBareJidFromJid($(iq).attr("from"));
//displayMessage("VCard success: "+jid);
var photo = $(iq).find('PHOTO');
if (photo && $(photo).find('BINVAL').text()) {
var type = $(photo).find('TYPE').text();
var binary = $(photo).find('BINVAL').text();
binary.replace(/"|'/,"");
         config.extensions.boshPlugin.roster.contacts[jid].photo = "data:"+type+";base64,"+binary;
}
else {
config.extensions.boshPlugin.roster.contacts[jid].photo = "data:image/png;base64,"+config.options.txtBoshAnonymousIcon;
}
if (config.extensions.boshPlugin.roster.contacts[jid].presence == "online"){ // check wheter contact is online
config.extensions.boshPlugin.roster.addContactToRosterline(jid); //add/replace entry
}

},
//addContactToRoster
addToRoster: function(name,jid,subscribe) {
var iq = $iq({type: "set"}).c("query", {xmlns: "jabber:iq:roster"}).c("item", {name: name,jid: jid});
boshPlugin.connection.sendIQ(iq);
displayMessage("added "+jid+" to roster");
if (subscribe) {
var subscribe = $pres({to: jid, "type": "subscribe"});
boshPlugin.connection.send(subscribe);
displayMessage("requested Subscription for "+jid);
}
}

};

/*
Roster Macros
*/
config.macros.bosh_rosterLine = { //show the rostericons
        width: "",
handler: function (place, macroName, params, wikifier, paramString, tiddler) {
var prms = paramString.parseParams(null, null, true);
                config.macros.bosh_rosterLine.width = getParam(prms, "width");
var roster = "";
//var rosterLine = "";
wikify("<html><span style='text-align:left;' id='rosterline'>"+roster+"</span></html>",place);
config.extensions.boshPlugin.roster.refreshRosterLine();

},

};
config.macros.bosh_addContact = {
handler: function (place, macroName, params, wikifier, paramString, tiddler) {
var form = "<html><form><label for='name'>Name</label><input name='name' type='text' id='name'/><label for='jid'>JID:</label><input name ='jid' type='text' id='jid'/><label for='jid'>Presence Abonnieren?</label><input type='checkbox' name='requestPres'><input type='button' id='Add' value='Add' onclick='config.extensions.boshPlugin.roster.addToRoster(this.form.name.value,this.form.jid.value,this.form.requestPres.checked)' /></form></html>";
wikify(form,place);
}
};
})(jQuery); 
// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var Base64 = (function () {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    var obj = {
        /**
         * Encodes a string in base64
         * @param {String} input The string to encode in base64.
         */
        encode: function (input) {
            var output = "";
            var chr1, chr2, chr3;
            var enc1, enc2, enc3, enc4;
            var i = 0;

            do {
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);

                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;

                if (isNaN(chr2)) {
                    enc3 = enc4 = 64;
                } else if (isNaN(chr3)) {
                    enc4 = 64;
                }

                output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
                    keyStr.charAt(enc3) + keyStr.charAt(enc4);
            } while (i < input.length);

            return output;
        },

        /**
         * Decodes a base64 string.
         * @param {String} input The string to decode.
         */
        decode: function (input) {
            var output = "";
            var chr1, chr2, chr3;
            var enc1, enc2, enc3, enc4;
            var i = 0;

            // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
            input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

            do {
                enc1 = keyStr.indexOf(input.charAt(i++));
                enc2 = keyStr.indexOf(input.charAt(i++));
                enc3 = keyStr.indexOf(input.charAt(i++));
                enc4 = keyStr.indexOf(input.charAt(i++));

                chr1 = (enc1 << 2) | (enc2 >> 4);
                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                chr3 = ((enc3 & 3) << 6) | enc4;

                output = output + String.fromCharCode(chr1);

                if (enc3 != 64) {
                    output = output + String.fromCharCode(chr2);
                }
                if (enc4 != 64) {
                    output = output + String.fromCharCode(chr3);
                }
            } while (i < input.length);

            return output;
        }
    };

    return obj;
})();
/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

var MD5 = (function () {
    /*
     * Configurable variables. You may need to tweak these to be compatible with
     * the server-side, but the defaults work in most cases.
     */
    var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase */
    var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance */
    var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode */

    /*
     * Add integers, wrapping at 2^32. This uses 16-bit operations internally
     * to work around bugs in some JS interpreters.
     */
    var safe_add = function (x, y) {
        var lsw = (x & 0xFFFF) + (y & 0xFFFF);
        var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
        return (msw << 16) | (lsw & 0xFFFF);
    };

    /*
     * Bitwise rotate a 32-bit number to the left.
     */
    var bit_rol = function (num, cnt) {
        return (num << cnt) | (num >>> (32 - cnt));
    };

    /*
     * Convert a string to an array of little-endian words
     * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
     */
    var str2binl = function (str) {
        var bin = [];
        var mask = (1 << chrsz) - 1;
        for(var i = 0; i < str.length * chrsz; i += chrsz)
        {
            bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
        }
        return bin;
    };

    /*
     * Convert an array of little-endian words to a string
     */
    var binl2str = function (bin) {
        var str = "";
        var mask = (1 << chrsz) - 1;
        for(var i = 0; i < bin.length * 32; i += chrsz)
        {
            str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
        }
        return str;
    };

    /*
     * Convert an array of little-endian words to a hex string.
     */
    var binl2hex = function (binarray) {
        var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
        var str = "";
        for(var i = 0; i < binarray.length * 4; i++)
        {
            str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
                hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
        }
        return str;
    };

    /*
     * Convert an array of little-endian words to a base-64 string
     */
    var binl2b64 = function (binarray) {
        var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        var str = "";
        var triplet, j;
        for(var i = 0; i < binarray.length * 4; i += 3)
        {
            triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16) |
                (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) |
                ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
            for(j = 0; j < 4; j++)
            {
                if(i * 8 + j * 6 > binarray.length * 32) { str += b64pad; }
                else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
            }
        }
        return str;
    };

    /*
     * These functions implement the four basic operations the algorithm uses.
     */
    var md5_cmn = function (q, a, b, x, s, t) {
        return safe_add(bit_rol(safe_add(safe_add(a, q),safe_add(x, t)), s),b);
    };

    var md5_ff = function (a, b, c, d, x, s, t) {
        return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
    };

    var md5_gg = function (a, b, c, d, x, s, t) {
        return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
    };

    var md5_hh = function (a, b, c, d, x, s, t) {
        return md5_cmn(b ^ c ^ d, a, b, x, s, t);
    };

    var md5_ii = function (a, b, c, d, x, s, t) {
        return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
    };

    /*
     * Calculate the MD5 of an array of little-endian words, and a bit length
     */
    var core_md5 = function (x, len) {
        /* append padding */
        x[len >> 5] |= 0x80 << ((len) % 32);
        x[(((len + 64) >>> 9) << 4) + 14] = len;

        var a =  1732584193;
        var b = -271733879;
        var c = -1732584194;
        var d =  271733878;

        var olda, oldb, oldc, oldd;
        for (var i = 0; i < x.length; i += 16)
        {
            olda = a;
            oldb = b;
            oldc = c;
            oldd = d;

            a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
            d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
            c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
            b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
            a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
            d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
            c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
            b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
            a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
            d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
            c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
            b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
            a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
            d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
            c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
            b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

            a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
            d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
            c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
            b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
            a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
            d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
            c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
            b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
            a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
            d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
            c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
            b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
            a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
            d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
            c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
            b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

            a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
            d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
            c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
            b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
            a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
            d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
            c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
            b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
            a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
            d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
            c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
            b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
            a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
            d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
            c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
            b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

            a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
            d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
            c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
            b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
            a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
            d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
            c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
            b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
            a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
            d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
            c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
            b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
            a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
            d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
            c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
            b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

            a = safe_add(a, olda);
            b = safe_add(b, oldb);
            c = safe_add(c, oldc);
            d = safe_add(d, oldd);
        }
        return [a, b, c, d];
    };


    /*
     * Calculate the HMAC-MD5, of a key and some data
     */
    var core_hmac_md5 = function (key, data) {
        var bkey = str2binl(key);
        if(bkey.length > 16) { bkey = core_md5(bkey, key.length * chrsz); }

        var ipad = new Array(16), opad = new Array(16);
        for(var i = 0; i < 16; i++)
        {
            ipad[i] = bkey[i] ^ 0x36363636;
            opad[i] = bkey[i] ^ 0x5C5C5C5C;
        }

        var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
        return core_md5(opad.concat(hash), 512 + 128);
    };

    var obj = {
        /*
         * These are the functions you'll usually want to call.
         * They take string arguments and return either hex or base-64 encoded
         * strings.
         */
        hexdigest: function (s) {
            return binl2hex(core_md5(str2binl(s), s.length * chrsz));
        },

        b64digest: function (s) {
            return binl2b64(core_md5(str2binl(s), s.length * chrsz));
        },

        hash: function (s) {
            return binl2str(core_md5(str2binl(s), s.length * chrsz));
        },

        hmac_hexdigest: function (key, data) {
            return binl2hex(core_hmac_md5(key, data));
        },

        hmac_b64digest: function (key, data) {
            return binl2b64(core_hmac_md5(key, data));
        },

        hmac_hash: function (key, data) {
            return binl2str(core_hmac_md5(key, data));
        },

        /*
         * Perform a simple self-test to see if the VM is working
         */
        test: function () {
            return MD5.hexdigest("abc") === "900150983cd24fb0d6963f7d28e17f72";
        }
    };

    return obj;
})();
/*
    This program is distributed under the terms of the MIT license.
    Please see the LICENSE file for details.

    Copyright 2006-2008, OGG, LLC
*/

/* jslint configuration: */
/*global document, window, setTimeout, clearTimeout, console,
    XMLHttpRequest, ActiveXObject,
    Base64, MD5,
    Strophe, $build, $msg, $iq, $pres */

/** File: strophe.js
 *  A JavaScript library for XMPP BOSH.
 *
 *  This is the JavaScript version of the Strophe library.  Since JavaScript
 *  has no facilities for persistent TCP connections, this library uses
 *  Bidirectional-streams Over Synchronous HTTP (BOSH) to emulate
 *  a persistent, stateful, two-way connection to an XMPP server.  More
 *  information on BOSH can be found in XEP 124.
 */

/** PrivateFunction: Function.prototype.bind
 *  Bind a function to an instance.
 *
 *  This Function object extension method creates a bound method similar
 *  to those in Python.  This means that the 'this' object will point
 *  to the instance you want.  See
 *  <a href='https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind'>MDC's bind() documentation</a> and 
 *  <a href='http://benjamin.smedbergs.us/blog/2007-01-03/bound-functions-and-function-imports-in-javascript/'>Bound Functions and Function Imports in JavaScript</a>
 *  for a complete explanation.
 *
 *  This extension already exists in some browsers (namely, Firefox 3), but
 *  we provide it to support those that don't.
 *
 *  Parameters:
 *    (Object) obj - The object that will become 'this' in the bound function.
 *    (Object) argN - An option argument that will be prepended to the 
 *      arguments given for the function call
 *
 *  Returns:
 *    The bound function.
 */
if (!Function.prototype.bind) {
    Function.prototype.bind = function (obj /*, arg1, arg2, ... */)
    {
        var func = this;
        var _slice = Array.prototype.slice;
        var _concat = Array.prototype.concat;
        var _args = _slice.call(arguments, 1);
        
        return function () {
            return func.apply(obj ? obj : this,
                              _concat.call(_args,
                                           _slice.call(arguments, 0)));
        };
    };
}

/** PrivateFunction: Array.prototype.indexOf
 *  Return the index of an object in an array.
 *
 *  This function is not supplied by some JavaScript implementations, so
 *  we provide it if it is missing.  This code is from:
 *  http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
 *
 *  Parameters:
 *    (Object) elt - The object to look for.
 *    (Integer) from - The index from which to start looking. (optional).
 *
 *  Returns:
 *    The index of elt in the array or -1 if not found.
 */
if (!Array.prototype.indexOf)
{
    Array.prototype.indexOf = function(elt /*, from*/)
    {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0) {
            from += len;
        }

        for (; from < len; from++) {
            if (from in this && this[from] === elt) {
                return from;
            }
        }

        return -1;
    };
}

/* All of the Strophe globals are defined in this special function below so
 * that references to the globals become closures.  This will ensure that
 * on page reload, these references will still be available to callbacks
 * that are still executing.
 */

(function (callback) {
var Strophe;

/** Function: $build
 *  Create a Strophe.Builder.
 *  This is an alias for 'new Strophe.Builder(name, attrs)'.
 *
 *  Parameters:
 *    (String) name - The root element name.
 *    (Object) attrs - The attributes for the root element in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $build(name, attrs) { return new Strophe.Builder(name, attrs); }
/** Function: $msg
 *  Create a Strophe.Builder with a <message/> element as the root.
 *
 *  Parmaeters:
 *    (Object) attrs - The <message/> element attributes in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $msg(attrs) { return new Strophe.Builder("message", attrs); }
/** Function: $iq
 *  Create a Strophe.Builder with an <iq/> element as the root.
 *
 *  Parameters:
 *    (Object) attrs - The <iq/> element attributes in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $iq(attrs) { return new Strophe.Builder("iq", attrs); }
/** Function: $pres
 *  Create a Strophe.Builder with a <presence/> element as the root.
 *
 *  Parameters:
 *    (Object) attrs - The <presence/> element attributes in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $pres(attrs) { return new Strophe.Builder("presence", attrs); }

/** Class: Strophe
 *  An object container for all Strophe library functions.
 *
 *  This class is just a container for all the objects and constants
 *  used in the library.  It is not meant to be instantiated, but to
 *  provide a namespace for library objects, constants, and functions.
 */
Strophe = {
    /** Constant: VERSION
     *  The version of the Strophe library. Unreleased builds will have
     *  a version of head-HASH where HASH is a partial revision.
     */
    VERSION: "4bb20a7",

    /** Constants: XMPP Namespace Constants
     *  Common namespace constants from the XMPP RFCs and XEPs.
     *
     *  NS.HTTPBIND - HTTP BIND namespace from XEP 124.
     *  NS.BOSH - BOSH namespace from XEP 206.
     *  NS.CLIENT - Main XMPP client namespace.
     *  NS.AUTH - Legacy authentication namespace.
     *  NS.ROSTER - Roster operations namespace.
     *  NS.PROFILE - Profile namespace.
     *  NS.DISCO_INFO - Service discovery info namespace from XEP 30.
     *  NS.DISCO_ITEMS - Service discovery items namespace from XEP 30.
     *  NS.MUC - Multi-User Chat namespace from XEP 45.
     *  NS.SASL - XMPP SASL namespace from RFC 3920.
     *  NS.STREAM - XMPP Streams namespace from RFC 3920.
     *  NS.BIND - XMPP Binding namespace from RFC 3920.
     *  NS.SESSION - XMPP Session namespace from RFC 3920.
     */
    NS: {
        HTTPBIND: "http://jabber.org/protocol/httpbind",
        BOSH: "urn:xmpp:xbosh",
        CLIENT: "jabber:client",
        AUTH: "jabber:iq:auth",
        ROSTER: "jabber:iq:roster",
        PROFILE: "jabber:iq:profile",
        DISCO_INFO: "http://jabber.org/protocol/disco#info",
        DISCO_ITEMS: "http://jabber.org/protocol/disco#items",
        MUC: "http://jabber.org/protocol/muc",
        SASL: "urn:ietf:params:xml:ns:xmpp-sasl",
        STREAM: "http://etherx.jabber.org/streams",
        BIND: "urn:ietf:params:xml:ns:xmpp-bind",
        SESSION: "urn:ietf:params:xml:ns:xmpp-session",
        VERSION: "jabber:iq:version",
        STANZAS: "urn:ietf:params:xml:ns:xmpp-stanzas"
    },

    /** Function: addNamespace
     *  This function is used to extend the current namespaces in
     *	Strophe.NS.  It takes a key and a value with the key being the
     *	name of the new namespace, with its actual value.
     *	For example:
     *	Strophe.addNamespace('PUBSUB', "http://jabber.org/protocol/pubsub");
     *
     *  Parameters:
     *    (String) name - The name under which the namespace will be
     *      referenced under Strophe.NS
     *    (String) value - The actual namespace.
     */
    addNamespace: function (name, value)
    {
	Strophe.NS[name] = value;
    },

    /** Constants: Connection Status Constants
     *  Connection status constants for use by the connection handler
     *  callback.
     *
     *  Status.ERROR - An error has occurred
     *  Status.CONNECTING - The connection is currently being made
     *  Status.CONNFAIL - The connection attempt failed
     *  Status.AUTHENTICATING - The connection is authenticating
     *  Status.AUTHFAIL - The authentication attempt failed
     *  Status.CONNECTED - The connection has succeeded
     *  Status.DISCONNECTED - The connection has been terminated
     *  Status.DISCONNECTING - The connection is currently being terminated
     *  Status.ATTACHED - The connection has been attached
     */
    Status: {
        ERROR: 0,
        CONNECTING: 1,
        CONNFAIL: 2,
        AUTHENTICATING: 3,
        AUTHFAIL: 4,
        CONNECTED: 5,
        DISCONNECTED: 6,
        DISCONNECTING: 7,
        ATTACHED: 8
    },

    /** Constants: Log Level Constants
     *  Logging level indicators.
     *
     *  LogLevel.DEBUG - Debug output
     *  LogLevel.INFO - Informational output
     *  LogLevel.WARN - Warnings
     *  LogLevel.ERROR - Errors
     *  LogLevel.FATAL - Fatal errors
     */
    LogLevel: {
        DEBUG: 0,
        INFO: 1,
        WARN: 2,
        ERROR: 3,
        FATAL: 4
    },

    /** PrivateConstants: DOM Element Type Constants
     *  DOM element types.
     *
     *  ElementType.NORMAL - Normal element.
     *  ElementType.TEXT - Text data element.
     */
    ElementType: {
        NORMAL: 1,
        TEXT: 3
    },

    /** PrivateConstants: Timeout Values
     *  Timeout values for error states.  These values are in seconds.
     *  These should not be changed unless you know exactly what you are
     *  doing.
     *
     *  TIMEOUT - Timeout multiplier. A waiting request will be considered
     *      failed after Math.floor(TIMEOUT * wait) seconds have elapsed.
     *      This defaults to 1.1, and with default wait, 66 seconds.
     *  SECONDARY_TIMEOUT - Secondary timeout multiplier. In cases where
     *      Strophe can detect early failure, it will consider the request
     *      failed if it doesn't return after
     *      Math.floor(SECONDARY_TIMEOUT * wait) seconds have elapsed.
     *      This defaults to 0.1, and with default wait, 6 seconds.
     */
    TIMEOUT: 1.1,
    SECONDARY_TIMEOUT: 0.1,

    /** Function: forEachChild
     *  Map a function over some or all child elements of a given element.
     *
     *  This is a small convenience function for mapping a function over
     *  some or all of the children of an element.  If elemName is null, all
     *  children will be passed to the function, otherwise only children
     *  whose tag names match elemName will be passed.
     *
     *  Parameters:
     *    (XMLElement) elem - The element to operate on.
     *    (String) elemName - The child element tag name filter.
     *    (Function) func - The function to apply to each child.  This
     *      function should take a single argument, a DOM element.
     */
    forEachChild: function (elem, elemName, func)
    {
        var i, childNode;

        for (i = 0; i < elem.childNodes.length; i++) {
            childNode = elem.childNodes[i];
            if (childNode.nodeType == Strophe.ElementType.NORMAL &&
                (!elemName || this.isTagEqual(childNode, elemName))) {
                func(childNode);
            }
        }
    },

    /** Function: isTagEqual
     *  Compare an element's tag name with a string.
     *
     *  This function is case insensitive.
     *
     *  Parameters:
     *    (XMLElement) el - A DOM element.
     *    (String) name - The element name.
     *
     *  Returns:
     *    true if the element's tag name matches _el_, and false
     *    otherwise.
     */
    isTagEqual: function (el, name)
    {
        return el.tagName.toLowerCase() == name.toLowerCase();
    },

    /** PrivateVariable: _xmlGenerator
     *  _Private_ variable that caches a DOM document to
     *  generate elements.
     */
    _xmlGenerator: null,

    /** PrivateFunction: _makeGenerator
     *  _Private_ function that creates a dummy XML DOM document to serve as
     *  an element and text node generator.
     */
    _makeGenerator: function () {
        var doc;

        if (window.ActiveXObject) {
            doc = this._getIEXmlDom();
            doc.appendChild(doc.createElement('strophe'));
        } else {
            doc = document.implementation
                .createDocument('jabber:client', 'strophe', null);
        }

        return doc;
    },

    /** Function: xmlGenerator
     *  Get the DOM document to generate elements.
     *
     *  Returns:
     *    The currently used DOM document.
     */
    xmlGenerator: function () {
        if (!Strophe._xmlGenerator) {
            Strophe._xmlGenerator = Strophe._makeGenerator();
        }
        return Strophe._xmlGenerator;
    },

    /** PrivateFunction: _getIEXmlDom
     *  Gets IE xml doc object
     *
     *  Returns:
     *    A Microsoft XML DOM Object
     *  See Also:
     *    http://msdn.microsoft.com/en-us/library/ms757837%28VS.85%29.aspx
     */
    _getIEXmlDom : function() {
        var doc = null;
        var docStrings = [
            "Msxml2.DOMDocument.6.0",
            "Msxml2.DOMDocument.5.0",
            "Msxml2.DOMDocument.4.0",
            "MSXML2.DOMDocument.3.0",
            "MSXML2.DOMDocument",
            "MSXML.DOMDocument",
            "Microsoft.XMLDOM"
        ];

        for (var d = 0; d < docStrings.length; d++) {
            if (doc === null) {
                try {
                    doc = new ActiveXObject(docStrings[d]);
                } catch (e) {
                    doc = null;
                }
            } else {
                break;
            }
        }

        return doc;
    },

    /** Function: xmlElement
     *  Create an XML DOM element.
     *
     *  This function creates an XML DOM element correctly across all
     *  implementations. Note that these are not HTML DOM elements, which
     *  aren't appropriate for XMPP stanzas.
     *
     *  Parameters:
     *    (String) name - The name for the element.
     *    (Array|Object) attrs - An optional array or object containing
     *      key/value pairs to use as element attributes. The object should
     *      be in the format {'key': 'value'} or {key: 'value'}. The array
     *      should have the format [['key1', 'value1'], ['key2', 'value2']].
     *    (String) text - The text child data for the element.
     *
     *  Returns:
     *    A new XML DOM element.
     */
    xmlElement: function (name)
    {
        if (!name) { return null; }

        var node = Strophe.xmlGenerator().createElement(name);

        // FIXME: this should throw errors if args are the wrong type or
        // there are more than two optional args
        var a, i, k;
        for (a = 1; a < arguments.length; a++) {
            if (!arguments[a]) { continue; }
            if (typeof(arguments[a]) == "string" ||
                typeof(arguments[a]) == "number") {
                node.appendChild(Strophe.xmlTextNode(arguments[a]));
            } else if (typeof(arguments[a]) == "object" &&
                       typeof(arguments[a].sort) == "function") {
                for (i = 0; i < arguments[a].length; i++) {
                    if (typeof(arguments[a][i]) == "object" &&
                        typeof(arguments[a][i].sort) == "function") {
                        node.setAttribute(arguments[a][i][0],
                                          arguments[a][i][1]);
                    }
                }
            } else if (typeof(arguments[a]) == "object") {
                for (k in arguments[a]) {
                    if (arguments[a].hasOwnProperty(k)) {
                        node.setAttribute(k, arguments[a][k]);
                    }
                }
            }
        }

        return node;
    },

    /*  Function: xmlescape
     *  Excapes invalid xml characters.
     *
     *  Parameters:
     *     (String) text - text to escape.
     *
     *	Returns:
     *      Escaped text.
     */
    xmlescape: function(text)
    {
	text = text.replace(/\&/g, "&amp;");
        text = text.replace(/</g,  "&lt;");
        text = text.replace(/>/g,  "&gt;");
        return text;
    },

    /** Function: xmlTextNode
     *  Creates an XML DOM text node.
     *
     *  Provides a cross implementation version of document.createTextNode.
     *
     *  Parameters:
     *    (String) text - The content of the text node.
     *
     *  Returns:
     *    A new XML DOM text node.
     */
    xmlTextNode: function (text)
    {
	//ensure text is escaped
	text = Strophe.xmlescape(text);

        return Strophe.xmlGenerator().createTextNode(text);
    },

    /** Function: getText
     *  Get the concatenation of all text children of an element.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    A String with the concatenated text of all text element children.
     */
    getText: function (elem)
    {
        if (!elem) { return null; }

        var str = "";
        if (elem.childNodes.length === 0 && elem.nodeType ==
            Strophe.ElementType.TEXT) {
            str += elem.nodeValue;
        }

        for (var i = 0; i < elem.childNodes.length; i++) {
            if (elem.childNodes[i].nodeType == Strophe.ElementType.TEXT) {
                str += elem.childNodes[i].nodeValue;
            }
        }

        return str;
    },

    /** Function: copyElement
     *  Copy an XML DOM element.
     *
     *  This function copies a DOM element and all its descendants and returns
     *  the new copy.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    A new, copied DOM element tree.
     */
    copyElement: function (elem)
    {
        var i, el;
        if (elem.nodeType == Strophe.ElementType.NORMAL) {
            el = Strophe.xmlElement(elem.tagName);

            for (i = 0; i < elem.attributes.length; i++) {
                el.setAttribute(elem.attributes[i].nodeName.toLowerCase(),
                                elem.attributes[i].value);
            }

            for (i = 0; i < elem.childNodes.length; i++) {
                el.appendChild(Strophe.copyElement(elem.childNodes[i]));
            }
        } else if (elem.nodeType == Strophe.ElementType.TEXT) {
            el = Strophe.xmlTextNode(elem.nodeValue);
        }

        return el;
    },

    /** Function: escapeNode
     *  Escape the node part (also called local part) of a JID.
     *
     *  Parameters:
     *    (String) node - A node (or local part).
     *
     *  Returns:
     *    An escaped node (or local part).
     */
    escapeNode: function (node)
    {
        return node.replace(/^\s+|\s+$/g, '')
            .replace(/\\/g,  "\\5c")
            .replace(/ /g,   "\\20")
            .replace(/\"/g,  "\\22")
            .replace(/\&/g,  "\\26")
            .replace(/\'/g,  "\\27")
            .replace(/\//g,  "\\2f")
            .replace(/:/g,   "\\3a")
            .replace(/</g,   "\\3c")
            .replace(/>/g,   "\\3e")
            .replace(/@/g,   "\\40");
    },

    /** Function: unescapeNode
     *  Unescape a node part (also called local part) of a JID.
     *
     *  Parameters:
     *    (String) node - A node (or local part).
     *
     *  Returns:
     *    An unescaped node (or local part).
     */
    unescapeNode: function (node)
    {
        return node.replace(/\\20/g, " ")
            .replace(/\\22/g, '"')
            .replace(/\\26/g, "&")
            .replace(/\\27/g, "'")
            .replace(/\\2f/g, "/")
            .replace(/\\3a/g, ":")
            .replace(/\\3c/g, "<")
            .replace(/\\3e/g, ">")
            .replace(/\\40/g, "@")
            .replace(/\\5c/g, "\\");
    },

    /** Function: getNodeFromJid
     *  Get the node portion of a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the node.
     */
    getNodeFromJid: function (jid)
    {
        if (jid.indexOf("@") < 0) { return null; }
        return jid.split("@")[0];
    },

    /** Function: getDomainFromJid
     *  Get the domain portion of a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the domain.
     */
    getDomainFromJid: function (jid)
    {
        var bare = Strophe.getBareJidFromJid(jid);
        if (bare.indexOf("@") < 0) {
            return bare;
        } else {
            var parts = bare.split("@");
            parts.splice(0, 1);
            return parts.join('@');
        }
    },

    /** Function: getResourceFromJid
     *  Get the resource portion of a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the resource.
     */
    getResourceFromJid: function (jid)
    {
        var s = jid.split("/");
        if (s.length < 2) { return null; }
        s.splice(0, 1);
        return s.join('/');
    },

    /** Function: getBareJidFromJid
     *  Get the bare JID from a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the bare JID.
     */
    getBareJidFromJid: function (jid)
    {
        return jid ? jid.split("/")[0] : null;
    },

    /** Function: log
     *  User overrideable logging function.
     *
     *  This function is called whenever the Strophe library calls any
     *  of the logging functions.  The default implementation of this
     *  function does nothing.  If client code wishes to handle the logging
     *  messages, it should override this with
     *  > Strophe.log = function (level, msg) {
     *  >   (user code here)
     *  > };
     *
     *  Please note that data sent and received over the wire is logged
     *  via Strophe.Connection.rawInput() and Strophe.Connection.rawOutput().
     *
     *  The different levels and their meanings are
     *
     *    DEBUG - Messages useful for debugging purposes.
     *    INFO - Informational messages.  This is mostly information like
     *      'disconnect was called' or 'SASL auth succeeded'.
     *    WARN - Warnings about potential problems.  This is mostly used
     *      to report transient connection errors like request timeouts.
     *    ERROR - Some error occurred.
     *    FATAL - A non-recoverable fatal error occurred.
     *
     *  Parameters:
     *    (Integer) level - The log level of the log message.  This will
     *      be one of the values in Strophe.LogLevel.
     *    (String) msg - The log message.
     */
    log: function (level, msg)
    {
        return;
    },

    /** Function: debug
     *  Log a message at the Strophe.LogLevel.DEBUG level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    debug: function(msg)
    {
        this.log(this.LogLevel.DEBUG, msg);
    },

    /** Function: info
     *  Log a message at the Strophe.LogLevel.INFO level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    info: function (msg)
    {
        this.log(this.LogLevel.INFO, msg);
    },

    /** Function: warn
     *  Log a message at the Strophe.LogLevel.WARN level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    warn: function (msg)
    {
        this.log(this.LogLevel.WARN, msg);
    },

    /** Function: error
     *  Log a message at the Strophe.LogLevel.ERROR level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    error: function (msg)
    {
        this.log(this.LogLevel.ERROR, msg);
    },

    /** Function: fatal
     *  Log a message at the Strophe.LogLevel.FATAL level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    fatal: function (msg)
    {
        this.log(this.LogLevel.FATAL, msg);
    },

    /** Function: serialize
     *  Render a DOM element and all descendants to a String.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    The serialized element tree as a String.
     */
    serialize: function (elem)
    {
        var result;

        if (!elem) { return null; }

        if (typeof(elem.tree) === "function") {
            elem = elem.tree();
        }

        var nodeName = elem.nodeName;
        var i, child;

        if (elem.getAttribute("_realname")) {
            nodeName = elem.getAttribute("_realname");
        }

        result = "<" + nodeName;
        for (i = 0; i < elem.attributes.length; i++) {
               if(elem.attributes[i].nodeName != "_realname") {
                 result += " " + elem.attributes[i].nodeName.toLowerCase() +
                "='" + elem.attributes[i].value
                    .replace(/&/g, "&amp;")
                       .replace(/\'/g, "&apos;")
                       .replace(/</g, "&lt;") + "'";
               }
        }

        if (elem.childNodes.length > 0) {
            result += ">";
            for (i = 0; i < elem.childNodes.length; i++) {
                child = elem.childNodes[i];
                if (child.nodeType == Strophe.ElementType.NORMAL) {
                    // normal element, so recurse
                    result += Strophe.serialize(child);
                } else if (child.nodeType == Strophe.ElementType.TEXT) {
                    // text element
                    result += child.nodeValue;
                }
            }
            result += "</" + nodeName + ">";
        } else {
            result += "/>";
        }

        return result;
    },

    /** PrivateVariable: _requestId
     *  _Private_ variable that keeps track of the request ids for
     *  connections.
     */
    _requestId: 0,

    /** PrivateVariable: Strophe.connectionPlugins
     *  _Private_ variable Used to store plugin names that need
     *  initialization on Strophe.Connection construction.
     */
    _connectionPlugins: {},

    /** Function: addConnectionPlugin
     *  Extends the Strophe.Connection object with the given plugin.
     *
     *  Paramaters:
     *    (String) name - The name of the extension.
     *    (Object) ptype - The plugin's prototype.
     */
    addConnectionPlugin: function (name, ptype)
    {
        Strophe._connectionPlugins[name] = ptype;
    }
};

/** Class: Strophe.Builder
 *  XML DOM builder.
 *
 *  This object provides an interface similar to JQuery but for building
 *  DOM element easily and rapidly.  All the functions except for toString()
 *  and tree() return the object, so calls can be chained.  Here's an
 *  example using the $iq() builder helper.
 *  > $iq({to: 'you', from: 'me', type: 'get', id: '1'})
 *  >     .c('query', {xmlns: 'strophe:example'})
 *  >     .c('example')
 *  >     .toString()
 *  The above generates this XML fragment
 *  > <iq to='you' from='me' type='get' id='1'>
 *  >   <query xmlns='strophe:example'>
 *  >     <example/>
 *  >   </query>
 *  > </iq>
 *  The corresponding DOM manipulations to get a similar fragment would be
 *  a lot more tedious and probably involve several helper variables.
 *
 *  Since adding children makes new operations operate on the child, up()
 *  is provided to traverse up the tree.  To add two children, do
 *  > builder.c('child1', ...).up().c('child2', ...)
 *  The next operation on the Builder will be relative to the second child.
 */

/** Constructor: Strophe.Builder
 *  Create a Strophe.Builder object.
 *
 *  The attributes should be passed in object notation.  For example
 *  > var b = new Builder('message', {to: 'you', from: 'me'});
 *  or
 *  > var b = new Builder('messsage', {'xml:lang': 'en'});
 *
 *  Parameters:
 *    (String) name - The name of the root element.
 *    (Object) attrs - The attributes for the root element in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder.
 */
Strophe.Builder = function (name, attrs)
{
    // Set correct namespace for jabber:client elements
    if (name == "presence" || name == "message" || name == "iq") {
        if (attrs && !attrs.xmlns) {
            attrs.xmlns = Strophe.NS.CLIENT;
        } else if (!attrs) {
            attrs = {xmlns: Strophe.NS.CLIENT};
        }
    }

    // Holds the tree being built.
    this.nodeTree = Strophe.xmlElement(name, attrs);

    // Points to the current operation node.
    this.node = this.nodeTree;
};

Strophe.Builder.prototype = {
    /** Function: tree
     *  Return the DOM tree.
     *
     *  This function returns the current DOM tree as an element object.  This
     *  is suitable for passing to functions like Strophe.Connection.send().
     *
     *  Returns:
     *    The DOM tree as a element object.
     */
    tree: function ()
    {
        return this.nodeTree;
    },

    /** Function: toString
     *  Serialize the DOM tree to a String.
     *
     *  This function returns a string serialization of the current DOM
     *  tree.  It is often used internally to pass data to a
     *  Strophe.Request object.
     *
     *  Returns:
     *    The serialized DOM tree in a String.
     */
    toString: function ()
    {
        return Strophe.serialize(this.nodeTree);
    },

    /** Function: up
     *  Make the current parent element the new current element.
     *
     *  This function is often used after c() to traverse back up the tree.
     *  For example, to add two children to the same element
     *  > builder.c('child1', {}).up().c('child2', {});
     *
     *  Returns:
     *    The Stophe.Builder object.
     */
    up: function ()
    {
        this.node = this.node.parentNode;
        return this;
    },

    /** Function: attrs
     *  Add or modify attributes of the current element.
     *
     *  The attributes should be passed in object notation.  This function
     *  does not move the current element pointer.
     *
     *  Parameters:
     *    (Object) moreattrs - The attributes to add/modify in object notation.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    attrs: function (moreattrs)
    {
        for (var k in moreattrs) {
            if (moreattrs.hasOwnProperty(k)) {
                this.node.setAttribute(k, moreattrs[k]);
            }
        }
        return this;
    },

    /** Function: c
     *  Add a child to the current element and make it the new current
     *  element.
     *
     *  This function moves the current element pointer to the child.  If you
     *  need to add another child, it is necessary to use up() to go back
     *  to the parent in the tree.
     *
     *  Parameters:
     *    (String) name - The name of the child.
     *    (Object) attrs - The attributes of the child in object notation.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    c: function (name, attrs)
    {
        var child = Strophe.xmlElement(name, attrs);
        this.node.appendChild(child);
        this.node = child;
        return this;
    },

    /** Function: cnode
     *  Add a child to the current element and make it the new current
     *  element.
     *
     *  This function is the same as c() except that instead of using a
     *  name and an attributes object to create the child it uses an
     *  existing DOM element object.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    cnode: function (elem)
    {
        var xmlGen = Strophe.xmlGenerator();
        var newElem = xmlGen.importNode ? xmlGen.importNode(elem, true) : Strophe.copyElement(elem);
        this.node.appendChild(newElem);
        this.node = newElem;
        return this;
    },

    /** Function: t
     *  Add a child text element.
     *
     *  This *does not* make the child the new current element since there
     *  are no children of text elements.
     *
     *  Parameters:
     *    (String) text - The text data to append to the current element.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    t: function (text)
    {
        var child = Strophe.xmlTextNode(text);
        this.node.appendChild(child);
        return this;
    }
};


/** PrivateClass: Strophe.Handler
 *  _Private_ helper class for managing stanza handlers.
 *
 *  A Strophe.Handler encapsulates a user provided callback function to be
 *  executed when matching stanzas are received by the connection.
 *  Handlers can be either one-off or persistant depending on their
 *  return value. Returning true will cause a Handler to remain active, and
 *  returning false will remove the Handler.
 *
 *  Users will not use Strophe.Handler objects directly, but instead they
 *  will use Strophe.Connection.addHandler() and
 *  Strophe.Connection.deleteHandler().
 */

/** PrivateConstructor: Strophe.Handler
 *  Create and initialize a new Strophe.Handler.
 *
 *  Parameters:
 *    (Function) handler - A function to be executed when the handler is run.
 *    (String) ns - The namespace to match.
 *    (String) name - The element name to match.
 *    (String) type - The element type to match.
 *    (String) id - The element id attribute to match.
 *    (String) from - The element from attribute to match.
 *    (Object) options - Handler options
 *
 *  Returns:
 *    A new Strophe.Handler object.
 */
Strophe.Handler = function (handler, ns, name, type, id, from, options)
{
    this.handler = handler;
    this.ns = ns;
    this.name = name;
    this.type = type;
    this.id = id;
    this.options = options || {matchbare: false};

    // default matchBare to false if undefined
    if (!this.options.matchBare) {
        this.options.matchBare = false;
    }

    if (this.options.matchBare) {
        this.from = from ? Strophe.getBareJidFromJid(from) : null;
    } else {
        this.from = from;
    }

    // whether the handler is a user handler or a system handler
    this.user = true;
};

Strophe.Handler.prototype = {
    /** PrivateFunction: isMatch
     *  Tests if a stanza matches the Strophe.Handler.
     *
     *  Parameters:
     *    (XMLElement) elem - The XML element to test.
     *
     *  Returns:
     *    true if the stanza matches and false otherwise.
     */
    isMatch: function (elem)
    {
        var nsMatch;
        var from = null;

        if (this.options.matchBare) {
            from = Strophe.getBareJidFromJid(elem.getAttribute('from'));
        } else {
            from = elem.getAttribute('from');
        }

        nsMatch = false;
        if (!this.ns) {
            nsMatch = true;
        } else {
            var that = this;
            Strophe.forEachChild(elem, null, function (elem) {
                if (elem.getAttribute("xmlns") == that.ns) {
                    nsMatch = true;
                }
            });

            nsMatch = nsMatch || elem.getAttribute("xmlns") == this.ns;
        }

        if (nsMatch &&
            (!this.name || Strophe.isTagEqual(elem, this.name)) &&
            (!this.type || elem.getAttribute("type") == this.type) &&
            (!this.id || elem.getAttribute("id") == this.id) &&
            (!this.from || from == this.from)) {
                return true;
        }

        return false;
    },

    /** PrivateFunction: run
     *  Run the callback on a matching stanza.
     *
     *  Parameters:
     *    (XMLElement) elem - The DOM element that triggered the
     *      Strophe.Handler.
     *
     *  Returns:
     *    A boolean indicating if the handler should remain active.
     */
    run: function (elem)
    {
        var result = null;
        try {
            result = this.handler(elem);
        } catch (e) {
            if (e.sourceURL) {
                Strophe.fatal("error: " + this.handler +
                              " " + e.sourceURL + ":" +
                              e.line + " - " + e.name + ": " + e.message);
            } else if (e.fileName) {
                if (typeof(console) != "undefined") {
                    console.trace();
                    console.error(this.handler, " - error - ", e, e.message);
                }
                Strophe.fatal("error: " + this.handler + " " +
                              e.fileName + ":" + e.lineNumber + " - " +
                              e.name + ": " + e.message);
            } else {
                Strophe.fatal("error: " + this.handler);
            }

            throw e;
        }

        return result;
    },

    /** PrivateFunction: toString
     *  Get a String representation of the Strophe.Handler object.
     *
     *  Returns:
     *    A String.
     */
    toString: function ()
    {
        return "{Handler: " + this.handler + "(" + this.name + "," +
            this.id + "," + this.ns + ")}";
    }
};

/** PrivateClass: Strophe.TimedHandler
 *  _Private_ helper class for managing timed handlers.
 *
 *  A Strophe.TimedHandler encapsulates a user provided callback that
 *  should be called after a certain period of time or at regular
 *  intervals.  The return value of the callback determines whether the
 *  Strophe.TimedHandler will continue to fire.
 *
 *  Users will not use Strophe.TimedHandler objects directly, but instead
 *  they will use Strophe.Connection.addTimedHandler() and
 *  Strophe.Connection.deleteTimedHandler().
 */

/** PrivateConstructor: Strophe.TimedHandler
 *  Create and initialize a new Strophe.TimedHandler object.
 *
 *  Parameters:
 *    (Integer) period - The number of milliseconds to wait before the
 *      handler is called.
 *    (Function) handler - The callback to run when the handler fires.  This
 *      function should take no arguments.
 *
 *  Returns:
 *    A new Strophe.TimedHandler object.
 */
Strophe.TimedHandler = function (period, handler)
{
    this.period = period;
    this.handler = handler;

    this.lastCalled = new Date().getTime();
    this.user = true;
};

Strophe.TimedHandler.prototype = {
    /** PrivateFunction: run
     *  Run the callback for the Strophe.TimedHandler.
     *
     *  Returns:
     *    true if the Strophe.TimedHandler should be called again, and false
     *      otherwise.
     */
    run: function ()
    {
        this.lastCalled = new Date().getTime();
        return this.handler();
    },

    /** PrivateFunction: reset
     *  Reset the last called time for the Strophe.TimedHandler.
     */
    reset: function ()
    {
        this.lastCalled = new Date().getTime();
    },

    /** PrivateFunction: toString
     *  Get a string representation of the Strophe.TimedHandler object.
     *
     *  Returns:
     *    The string representation.
     */
    toString: function ()
    {
        return "{TimedHandler: " + this.handler + "(" + this.period +")}";
    }
};

/** PrivateClass: Strophe.Request
 *  _Private_ helper class that provides a cross implementation abstraction
 *  for a BOSH related XMLHttpRequest.
 *
 *  The Strophe.Request class is used internally to encapsulate BOSH request
 *  information.  It is not meant to be used from user's code.
 */

/** PrivateConstructor: Strophe.Request
 *  Create and initialize a new Strophe.Request object.
 *
 *  Parameters:
 *    (XMLElement) elem - The XML data to be sent in the request.
 *    (Function) func - The function that will be called when the
 *      XMLHttpRequest readyState changes.
 *    (Integer) rid - The BOSH rid attribute associated with this request.
 *    (Integer) sends - The number of times this same request has been
 *      sent.
 */
Strophe.Request = function (elem, func, rid, sends)
{
    this.id = ++Strophe._requestId;
    this.xmlData = elem;
    this.data = Strophe.serialize(elem);
    // save original function in case we need to make a new request
    // from this one.
    this.origFunc = func;
    this.func = func;
    this.rid = rid;
    this.date = NaN;
    this.sends = sends || 0;
    this.abort = false;
    this.dead = null;
    this.age = function () {
        if (!this.date) { return 0; }
        var now = new Date();
        return (now - this.date) / 1000;
    };
    this.timeDead = function () {
        if (!this.dead) { return 0; }
        var now = new Date();
        return (now - this.dead) / 1000;
    };
    this.xhr = this._newXHR();
};

Strophe.Request.prototype = {
    /** PrivateFunction: getResponse
     *  Get a response from the underlying XMLHttpRequest.
     *
     *  This function attempts to get a response from the request and checks
     *  for errors.
     *
     *  Throws:
     *    "parsererror" - A parser error occured.
     *
     *  Returns:
     *    The DOM element tree of the response.
     */
    getResponse: function ()
    {
        var node = null;
        if (this.xhr.responseXML && this.xhr.responseXML.documentElement) {
            node = this.xhr.responseXML.documentElement;
            if (node.tagName == "parsererror") {
                Strophe.error("invalid response received");
                Strophe.error("responseText: " + this.xhr.responseText);
                Strophe.error("responseXML: " +
                              Strophe.serialize(this.xhr.responseXML));
                throw "parsererror";
            }
        } else if (this.xhr.responseText) {
            Strophe.error("invalid response received");
            Strophe.error("responseText: " + this.xhr.responseText);
            Strophe.error("responseXML: " +
                          Strophe.serialize(this.xhr.responseXML));
        }

        return node;
    },

    /** PrivateFunction: _newXHR
     *  _Private_ helper function to create XMLHttpRequests.
     *
     *  This function creates XMLHttpRequests across all implementations.
     *
     *  Returns:
     *    A new XMLHttpRequest.
     */
    _newXHR: function ()
    {
        var xhr = null;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
            if (xhr.overrideMimeType) {
                xhr.overrideMimeType("text/xml");
            }
        } else if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        // use Function.bind() to prepend ourselves as an argument
        xhr.onreadystatechange = this.func.bind(null, this);

        return xhr;
    }
};

/** Class: Strophe.Connection
 *  XMPP Connection manager.
 *
 *  Thie class is the main part of Strophe.  It manages a BOSH connection
 *  to an XMPP server and dispatches events to the user callbacks as
 *  data arrives.  It supports SASL PLAIN, SASL DIGEST-MD5, and legacy
 *  authentication.
 *
 *  After creating a Strophe.Connection object, the user will typically
 *  call connect() with a user supplied callback to handle connection level
 *  events like authentication failure, disconnection, or connection
 *  complete.
 *
 *  The user will also have several event handlers defined by using
 *  addHandler() and addTimedHandler().  These will allow the user code to
 *  respond to interesting stanzas or do something periodically with the
 *  connection.  These handlers will be active once authentication is
 *  finished.
 *
 *  To send data to the connection, use send().
 */

/** Constructor: Strophe.Connection
 *  Create and initialize a Strophe.Connection object.
 *
 *  Parameters:
 *    (String) service - The BOSH service URL.
 *
 *  Returns:
 *    A new Strophe.Connection object.
 */
Strophe.Connection = function (service)
{
    /* The path to the httpbind service. */
    this.service = service;
    /* The connected JID. */
    this.jid = "";
    /* request id for body tags */
    this.rid = Math.floor(Math.random() * 4294967295);
    /* The current session ID. */
    this.sid = null;
    this.streamId = null;
    /* stream:features */
    this.features = null;

    // SASL
    this.do_session = false;
    this.do_bind = false;

    // handler lists
    this.timedHandlers = [];
    this.handlers = [];
    this.removeTimeds = [];
    this.removeHandlers = [];
    this.addTimeds = [];
    this.addHandlers = [];

    this._idleTimeout = null;
    this._disconnectTimeout = null;

    this.authenticated = false;
    this.disconnecting = false;
    this.connected = false;

    this.errors = 0;

    this.paused = false;

    // default BOSH values
    this.hold = 1;
    this.wait = 60;
    this.window = 5;

    this._data = [];
    this._requests = [];
    this._uniqueId = Math.round(Math.random() * 10000);

    this._sasl_success_handler = null;
    this._sasl_failure_handler = null;
    this._sasl_challenge_handler = null;

    // setup onIdle callback every 1/10th of a second
    this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);

    // initialize plugins
    for (var k in Strophe._connectionPlugins) {
        if (Strophe._connectionPlugins.hasOwnProperty(k)) {
	    var ptype = Strophe._connectionPlugins[k];
            // jslint complaints about the below line, but this is fine
            var F = function () {};
            F.prototype = ptype;
            this[k] = new F();
	    this[k].init(this);
        }
    }
};

Strophe.Connection.prototype = {
    /** Function: reset
     *  Reset the connection.
     *
     *  This function should be called after a connection is disconnected
     *  before that connection is reused.
     */
    reset: function ()
    {
        this.rid = Math.floor(Math.random() * 4294967295);

        this.sid = null;
        this.streamId = null;

        // SASL
        this.do_session = false;
        this.do_bind = false;

        // handler lists
        this.timedHandlers = [];
        this.handlers = [];
        this.removeTimeds = [];
        this.removeHandlers = [];
        this.addTimeds = [];
        this.addHandlers = [];

        this.authenticated = false;
        this.disconnecting = false;
        this.connected = false;

        this.errors = 0;

        this._requests = [];
        this._uniqueId = Math.round(Math.random()*10000);
    },

    /** Function: pause
     *  Pause the request manager.
     *
     *  This will prevent Strophe from sending any more requests to the
     *  server.  This is very useful for temporarily pausing while a lot
     *  of send() calls are happening quickly.  This causes Strophe to
     *  send the data in a single request, saving many request trips.
     */
    pause: function ()
    {
        this.paused = true;
    },

    /** Function: resume
     *  Resume the request manager.
     *
     *  This resumes after pause() has been called.
     */
    resume: function ()
    {
        this.paused = false;
    },

    /** Function: getUniqueId
     *  Generate a unique ID for use in <iq/> elements.
     *
     *  All <iq/> stanzas are required to have unique id attributes.  This
     *  function makes creating these easy.  Each connection instance has
     *  a counter which starts from zero, and the value of this counter
     *  plus a colon followed by the suffix becomes the unique id. If no
     *  suffix is supplied, the counter is used as the unique id.
     *
     *  Suffixes are used to make debugging easier when reading the stream
     *  data, and their use is recommended.  The counter resets to 0 for
     *  every new connection for the same reason.  For connections to the
     *  same server that authenticate the same way, all the ids should be
     *  the same, which makes it easy to see changes.  This is useful for
     *  automated testing as well.
     *
     *  Parameters:
     *    (String) suffix - A optional suffix to append to the id.
     *
     *  Returns:
     *    A unique string to be used for the id attribute.
     */
    getUniqueId: function (suffix)
    {
        if (typeof(suffix) == "string" || typeof(suffix) == "number") {
            return ++this._uniqueId + ":" + suffix;
        } else {
            return ++this._uniqueId + "";
        }
    },

    /** Function: connect
     *  Starts the connection process.
     *
     *  As the connection process proceeds, the user supplied callback will
     *  be triggered multiple times with status updates.  The callback
     *  should take two arguments - the status code and the error condition.
     *
     *  The status code will be one of the values in the Strophe.Status
     *  constants.  The error condition will be one of the conditions
     *  defined in RFC 3920 or the condition 'strophe-parsererror'.
     *
     *  Please see XEP 124 for a more detailed explanation of the optional
     *  parameters below.
     *
     *  Parameters:
     *    (String) jid - The user's JID.  This may be a bare JID,
     *      or a full JID.  If a node is not supplied, SASL ANONYMOUS
     *      authentication will be attempted.
     *    (String) pass - The user's password.
     *    (Function) callback The connect callback function.
     *    (Integer) wait - The optional HTTPBIND wait value.  This is the
     *      time the server will wait before returning an empty result for
     *      a request.  The default setting of 60 seconds is recommended.
     *      Other settings will require tweaks to the Strophe.TIMEOUT value.
     *    (Integer) hold - The optional HTTPBIND hold value.  This is the
     *      number of connections the server will hold at one time.  This
     *      should almost always be set to 1 (the default).
     */
    connect: function (jid, pass, callback, wait, hold)
    {
        this.jid = jid;
        this.pass = pass;
        this.connect_callback = callback;
        this.disconnecting = false;
        this.connected = false;
        this.authenticated = false;
        this.errors = 0;

        this.wait = wait || this.wait;
        this.hold = hold || this.hold;

        // parse jid for domain and resource
        this.domain = Strophe.getDomainFromJid(this.jid);

        // build the body tag
        var body = this._buildBody().attrs({
            to: this.domain,
            "xml:lang": "en",
            wait: this.wait,
            hold: this.hold,
            content: "text/xml; charset=utf-8",
            ver: "1.6",
            "xmpp:version": "1.0",
            "xmlns:xmpp": Strophe.NS.BOSH
        });

        this._changeConnectStatus(Strophe.Status.CONNECTING, null);

        this._requests.push(
            new Strophe.Request(body.tree(),
                                this._onRequestStateChange.bind(
                                    this, this._connect_cb.bind(this)),
                                body.tree().getAttribute("rid")));
        this._throttledRequestHandler();
    },

    /** Function: attach
     *  Attach to an already created and authenticated BOSH session.
     *
     *  This function is provided to allow Strophe to attach to BOSH
     *  sessions which have been created externally, perhaps by a Web
     *  application.  This is often used to support auto-login type features
     *  without putting user credentials into the page.
     *
     *  Parameters:
     *    (String) jid - The full JID that is bound by the session.
     *    (String) sid - The SID of the BOSH session.
     *    (String) rid - The current RID of the BOSH session.  This RID
     *      will be used by the next request.
     *    (Function) callback The connect callback function.
     *    (Integer) wait - The optional HTTPBIND wait value.  This is the
     *      time the server will wait before returning an empty result for
     *      a request.  The default setting of 60 seconds is recommended.
     *      Other settings will require tweaks to the Strophe.TIMEOUT value.
     *    (Integer) hold - The optional HTTPBIND hold value.  This is the
     *      number of connections the server will hold at one time.  This
     *      should almost always be set to 1 (the default).
     *    (Integer) wind - The optional HTTBIND window value.  This is the
     *      allowed range of request ids that are valid.  The default is 5.
     */
    attach: function (jid, sid, rid, callback, wait, hold, wind)
    {
        this.jid = jid;
        this.sid = sid;
        this.rid = rid;
        this.connect_callback = callback;

        this.domain = Strophe.getDomainFromJid(this.jid);

        this.authenticated = true;
        this.connected = true;

        this.wait = wait || this.wait;
        this.hold = hold || this.hold;
        this.window = wind || this.window;

        this._changeConnectStatus(Strophe.Status.ATTACHED, null);
    },

    /** Function: xmlInput
     *  User overrideable function that receives XML data coming into the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.xmlInput = function (elem) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (XMLElement) elem - The XML data received by the connection.
     */
    xmlInput: function (elem)
    {
        return;
    },

    /** Function: xmlOutput
     *  User overrideable function that receives XML data sent to the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.xmlOutput = function (elem) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (XMLElement) elem - The XMLdata sent by the connection.
     */
    xmlOutput: function (elem)
    {
        return;
    },

    /** Function: rawInput
     *  User overrideable function that receives raw data coming into the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.rawInput = function (data) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (String) data - The data received by the connection.
     */
    rawInput: function (data)
    {
        return;
    },

    /** Function: rawOutput
     *  User overrideable function that receives raw data sent to the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.rawOutput = function (data) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (String) data - The data sent by the connection.
     */
    rawOutput: function (data)
    {
        return;
    },

    /** Function: send
     *  Send a stanza.
     *
     *  This function is called to push data onto the send queue to
     *  go out over the wire.  Whenever a request is sent to the BOSH
     *  server, all pending data is sent and the queue is flushed.
     *
     *  Parameters:
     *    (XMLElement |
     *     [XMLElement] |
     *     Strophe.Builder) elem - The stanza to send.
     */
    send: function (elem)
    {
        if (elem === null) { return ; }
        if (typeof(elem.sort) === "function") {
            for (var i = 0; i < elem.length; i++) {
                this._queueData(elem[i]);
            }
        } else if (typeof(elem.tree) === "function") {
            this._queueData(elem.tree());
        } else {
            this._queueData(elem);
        }

        this._throttledRequestHandler();
        clearTimeout(this._idleTimeout);
        this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
    },

    /** Function: flush
     *  Immediately send any pending outgoing data.
     *
     *  Normally send() queues outgoing data until the next idle period
     *  (100ms), which optimizes network use in the common cases when
     *  several send()s are called in succession. flush() can be used to
     *  immediately send all pending data.
     */
    flush: function ()
    {
        // cancel the pending idle period and run the idle function
        // immediately
        clearTimeout(this._idleTimeout);
        this._onIdle();
    },

    /** Function: sendIQ
     *  Helper function to send IQ stanzas.
     *
     *  Parameters:
     *    (XMLElement) elem - The stanza to send.
     *    (Function) callback - The callback function for a successful request.
     *    (Function) errback - The callback function for a failed or timed
     *      out request.  On timeout, the stanza will be null.
     *    (Integer) timeout - The time specified in milliseconds for a
     *      timeout to occur.
     *
     *  Returns:
     *    The id used to send the IQ.
    */
    sendIQ: function(elem, callback, errback, timeout) {
        var timeoutHandler = null;
        var that = this;

        if (typeof(elem.tree) === "function") {
            elem = elem.tree();
        }
	var id = elem.getAttribute('id');

	// inject id if not found
	if (!id) {
	    id = this.getUniqueId("sendIQ");
	    elem.setAttribute("id", id);
	}

	var handler = this.addHandler(function (stanza) {
	    // remove timeout handler if there is one
            if (timeoutHandler) {
                that.deleteTimedHandler(timeoutHandler);
            }

            var iqtype = stanza.getAttribute('type');
	    if (iqtype == 'result') {
		if (callback) {
                    callback(stanza);
                }
	    } else if (iqtype == 'error') {
		if (errback) {
                    errback(stanza);
                }
	    } else {
                throw {
                    name: "StropheError",
                    message: "Got bad IQ type of " + iqtype
                };
            }
	}, null, 'iq', null, id);

	// if timeout specified, setup timeout handler.
	if (timeout) {
	    timeoutHandler = this.addTimedHandler(timeout, function () {
                // get rid of normal handler
                that.deleteHandler(handler);

	        // call errback on timeout with null stanza
                if (errback) {
		    errback(null);
                }
		return false;
	    });
	}

	this.send(elem);

	return id;
    },

    /** PrivateFunction: _queueData
     *  Queue outgoing data for later sending.  Also ensures that the data
     *  is a DOMElement.
     */
    _queueData: function (element) {
        if (element === null ||
            !element.tagName ||
            !element.childNodes) {
            throw {
                name: "StropheError",
                message: "Cannot queue non-DOMElement."
            };
        }

        this._data.push(element);
    },

    /** PrivateFunction: _sendRestart
     *  Send an xmpp:restart stanza.
     */
    _sendRestart: function ()
    {
        this._data.push("restart");

        this._throttledRequestHandler();
        clearTimeout(this._idleTimeout);
        this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
    },

    /** Function: addTimedHandler
     *  Add a timed handler to the connection.
     *
     *  This function adds a timed handler.  The provided handler will
     *  be called every period milliseconds until it returns false,
     *  the connection is terminated, or the handler is removed.  Handlers
     *  that wish to continue being invoked should return true.
     *
     *  Because of method binding it is necessary to save the result of
     *  this function if you wish to remove a handler with
     *  deleteTimedHandler().
     *
     *  Note that user handlers are not active until authentication is
     *  successful.
     *
     *  Parameters:
     *    (Integer) period - The period of the handler.
     *    (Function) handler - The callback function.
     *
     *  Returns:
     *    A reference to the handler that can be used to remove it.
     */
    addTimedHandler: function (period, handler)
    {
        var thand = new Strophe.TimedHandler(period, handler);
        this.addTimeds.push(thand);
        return thand;
    },

    /** Function: deleteTimedHandler
     *  Delete a timed handler for a connection.
     *
     *  This function removes a timed handler from the connection.  The
     *  handRef parameter is *not* the function passed to addTimedHandler(),
     *  but is the reference returned from addTimedHandler().
     *
     *  Parameters:
     *    (Strophe.TimedHandler) handRef - The handler reference.
     */
    deleteTimedHandler: function (handRef)
    {
        // this must be done in the Idle loop so that we don't change
        // the handlers during iteration
        this.removeTimeds.push(handRef);
    },

    /** Function: addHandler
     *  Add a stanza handler for the connection.
     *
     *  This function adds a stanza handler to the connection.  The
     *  handler callback will be called for any stanza that matches
     *  the parameters.  Note that if multiple parameters are supplied,
     *  they must all match for the handler to be invoked.
     *
     *  The handler will receive the stanza that triggered it as its argument.
     *  The handler should return true if it is to be invoked again;
     *  returning false will remove the handler after it returns.
     *
     *  As a convenience, the ns parameters applies to the top level element
     *  and also any of its immediate children.  This is primarily to make
     *  matching /iq/query elements easy.
     *
     *  The options argument contains handler matching flags that affect how
     *  matches are determined. Currently the only flag is matchBare (a
     *  boolean). When matchBare is true, the from parameter and the from
     *  attribute on the stanza will be matched as bare JIDs instead of
     *  full JIDs. To use this, pass {matchBare: true} as the value of
     *  options. The default value for matchBare is false.
     *
     *  The return value should be saved if you wish to remove the handler
     *  with deleteHandler().
     *
     *  Parameters:
     *    (Function) handler - The user callback.
     *    (String) ns - The namespace to match.
     *    (String) name - The stanza name to match.
     *    (String) type - The stanza type attribute to match.
     *    (String) id - The stanza id attribute to match.
     *    (String) from - The stanza from attribute to match.
     *    (String) options - The handler options
     *
     *  Returns:
     *    A reference to the handler that can be used to remove it.
     */
    addHandler: function (handler, ns, name, type, id, from, options)
    {
        var hand = new Strophe.Handler(handler, ns, name, type, id, from, options);
        this.addHandlers.push(hand);
        return hand;
    },

    /** Function: deleteHandler
     *  Delete a stanza handler for a connection.
     *
     *  This function removes a stanza handler from the connection.  The
     *  handRef parameter is *not* the function passed to addHandler(),
     *  but is the reference returned from addHandler().
     *
     *  Parameters:
     *    (Strophe.Handler) handRef - The handler reference.
     */
    deleteHandler: function (handRef)
    {
        // this must be done in the Idle loop so that we don't change
        // the handlers during iteration
        this.removeHandlers.push(handRef);
    },

    /** Function: disconnect
     *  Start the graceful disconnection process.
     *
     *  This function starts the disconnection process.  This process starts
     *  by sending unavailable presence and sending BOSH body of type
     *  terminate.  A timeout handler makes sure that disconnection happens
     *  even if the BOSH server does not respond.
     *
     *  The user supplied connection callback will be notified of the
     *  progress as this process happens.
     *
     *  Parameters:
     *    (String) reason - The reason the disconnect is occuring.
     */
    disconnect: function (reason)
    {
        this._changeConnectStatus(Strophe.Status.DISCONNECTING, reason);

        Strophe.info("Disconnect was called because: " + reason);
        if (this.connected) {
            // setup timeout handler
            this._disconnectTimeout = this._addSysTimedHandler(
                3000, this._onDisconnectTimeout.bind(this));
            this._sendTerminate();
        }
    },

    /** PrivateFunction: _changeConnectStatus
     *  _Private_ helper function that makes sure plugins and the user's
     *  callback are notified of connection status changes.
     *
     *  Parameters:
     *    (Integer) status - the new connection status, one of the values
     *      in Strophe.Status
     *    (String) condition - the error condition or null
     */
    _changeConnectStatus: function (status, condition)
    {
        // notify all plugins listening for status changes
        for (var k in Strophe._connectionPlugins) {
            if (Strophe._connectionPlugins.hasOwnProperty(k)) {
                var plugin = this[k];
                if (plugin.statusChanged) {
                    try {
                        plugin.statusChanged(status, condition);
                    } catch (err) {
                        Strophe.error("" + k + " plugin caused an exception " +
                                      "changing status: " + err);
                    }
                }
            }
        }

        // notify the user's callback
        if (this.connect_callback) {
            try {
                this.connect_callback(status, condition);
            } catch (e) {
                Strophe.error("User connection callback caused an " +
                              "exception: " + e);
            }
        }
    },

    /** PrivateFunction: _buildBody
     *  _Private_ helper function to generate the <body/> wrapper for BOSH.
     *
     *  Returns:
     *    A Strophe.Builder with a <body/> element.
     */
    _buildBody: function ()
    {
        var bodyWrap = $build('body', {
            rid: this.rid++,
            xmlns: Strophe.NS.HTTPBIND
        });

        if (this.sid !== null) {
            bodyWrap.attrs({sid: this.sid});
        }

        return bodyWrap;
    },

    /** PrivateFunction: _removeRequest
     *  _Private_ function to remove a request from the queue.
     *
     *  Parameters:
     *    (Strophe.Request) req - The request to remove.
     */
    _removeRequest: function (req)
    {
        Strophe.debug("removing request");

        var i;
        for (i = this._requests.length - 1; i >= 0; i--) {
            if (req == this._requests[i]) {
                this._requests.splice(i, 1);
            }
        }

        // IE6 fails on setting to null, so set to empty function
        req.xhr.onreadystatechange = function () {};

        this._throttledRequestHandler();
    },

    /** PrivateFunction: _restartRequest
     *  _Private_ function to restart a request that is presumed dead.
     *
     *  Parameters:
     *    (Integer) i - The index of the request in the queue.
     */
    _restartRequest: function (i)
    {
        var req = this._requests[i];
        if (req.dead === null) {
            req.dead = new Date();
        }

        this._processRequest(i);
    },

    /** PrivateFunction: _processRequest
     *  _Private_ function to process a request in the queue.
     *
     *  This function takes requests off the queue and sends them and
     *  restarts dead requests.
     *
     *  Parameters:
     *    (Integer) i - The index of the request in the queue.
     */
    _processRequest: function (i)
    {
        var req = this._requests[i];
        var reqStatus = -1;

        try {
            if (req.xhr.readyState == 4) {
                reqStatus = req.xhr.status;
            }
        } catch (e) {
            Strophe.error("caught an error in _requests[" + i +
                          "], reqStatus: " + reqStatus);
        }

        if (typeof(reqStatus) == "undefined") {
            reqStatus = -1;
        }

        // make sure we limit the number of retries
        if (req.sends > 5) {
            this._onDisconnectTimeout();
            return;
        }

        var time_elapsed = req.age();
        var primaryTimeout = (!isNaN(time_elapsed) &&
                              time_elapsed > Math.floor(Strophe.TIMEOUT * this.wait));
        var secondaryTimeout = (req.dead !== null &&
                                req.timeDead() > Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait));
        var requestCompletedWithServerError = (req.xhr.readyState == 4 &&
                                               (reqStatus < 1 ||
                                                reqStatus >= 500));
        if (primaryTimeout || secondaryTimeout ||
            requestCompletedWithServerError) {
            if (secondaryTimeout) {
                Strophe.error("Request " +
                              this._requests[i].id +
                              " timed out (secondary), restarting");
            }
            req.abort = true;
            req.xhr.abort();
            // setting to null fails on IE6, so set to empty function
            req.xhr.onreadystatechange = function () {};
            this._requests[i] = new Strophe.Request(req.xmlData,
                                                    req.origFunc,
                                                    req.rid,
                                                    req.sends);
            req = this._requests[i];
        }

        if (req.xhr.readyState === 0) {
            Strophe.debug("request id " + req.id +
                          "." + req.sends + " posting");

            req.date = new Date();
            try {
                req.xhr.open("POST", this.service, true);
            } catch (e2) {
                Strophe.error("XHR open failed.");
                if (!this.connected) {
                    this._changeConnectStatus(Strophe.Status.CONNFAIL,
                                              "bad-service");
                }
                this.disconnect();
                return;
            }

            // Fires the XHR request -- may be invoked immediately
            // or on a gradually expanding retry window for reconnects
            var sendFunc = function () {
                req.xhr.send(req.data);
            };

            // Implement progressive backoff for reconnects --
            // First retry (send == 1) should also be instantaneous
            if (req.sends > 1) {
                // Using a cube of the retry number creats a nicely
                // expanding retry window
                var backoff = Math.pow(req.sends, 3) * 1000;
                setTimeout(sendFunc, backoff);
            } else {
                sendFunc();
            }

            req.sends++;

            this.xmlOutput(req.xmlData);
            this.rawOutput(req.data);
        } else {
            Strophe.debug("_processRequest: " +
                          (i === 0 ? "first" : "second") +
                          " request has readyState of " +
                          req.xhr.readyState);
        }
    },

    /** PrivateFunction: _throttledRequestHandler
     *  _Private_ function to throttle requests to the connection window.
     *
     *  This function makes sure we don't send requests so fast that the
     *  request ids overflow the connection window in the case that one
     *  request died.
     */
    _throttledRequestHandler: function ()
    {
        if (!this._requests) {
            Strophe.debug("_throttledRequestHandler called with " +
                          "undefined requests");
        } else {
            Strophe.debug("_throttledRequestHandler called with " +
                          this._requests.length + " requests");
        }

        if (!this._requests || this._requests.length === 0) {
            return;
        }

        if (this._requests.length > 0) {
            this._processRequest(0);
        }

        if (this._requests.length > 1 &&
            Math.abs(this._requests[0].rid -
                     this._requests[1].rid) < this.window) {
            this._processRequest(1);
        }
    },

    /** PrivateFunction: _onRequestStateChange
     *  _Private_ handler for Strophe.Request state changes.
     *
     *  This function is called when the XMLHttpRequest readyState changes.
     *  It contains a lot of error handling logic for the many ways that
     *  requests can fail, and calls the request callback when requests
     *  succeed.
     *
     *  Parameters:
     *    (Function) func - The handler for the request.
     *    (Strophe.Request) req - The request that is changing readyState.
     */
    _onRequestStateChange: function (func, req)
    {
        Strophe.debug("request id " + req.id +
                      "." + req.sends + " state changed to " +
                      req.xhr.readyState);

        if (req.abort) {
            req.abort = false;
            return;
        }

        // request complete
        var reqStatus;
        if (req.xhr.readyState == 4) {
            reqStatus = 0;
            try {
                reqStatus = req.xhr.status;
            } catch (e) {
                // ignore errors from undefined status attribute.  works
                // around a browser bug
            }

            if (typeof(reqStatus) == "undefined") {
                reqStatus = 0;
            }

            if (this.disconnecting) {
                if (reqStatus >= 400) {
                    this._hitError(reqStatus);
                    return;
                }
            }

            var reqIs0 = (this._requests[0] == req);
            var reqIs1 = (this._requests[1] == req);

            if ((reqStatus > 0 && reqStatus < 500) || req.sends > 5) {
                // remove from internal queue
                this._removeRequest(req);
                Strophe.debug("request id " +
                              req.id +
                              " should now be removed");
            }

            // request succeeded
            if (reqStatus == 200) {
                // if request 1 finished, or request 0 finished and request
                // 1 is over Strophe.SECONDARY_TIMEOUT seconds old, we need to
                // restart the other - both will be in the first spot, as the
                // completed request has been removed from the queue already
                if (reqIs1 ||
                    (reqIs0 && this._requests.length > 0 &&
                     this._requests[0].age() > Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait))) {
                    this._restartRequest(0);
                }
                // call handler
                Strophe.debug("request id " +
                              req.id + "." +
                              req.sends + " got 200");
                func(req);
                this.errors = 0;
            } else {
                Strophe.error("request id " +
                              req.id + "." +
                              req.sends + " error " + reqStatus +
                              " happened");
                if (reqStatus === 0 ||
                    (reqStatus >= 400 && reqStatus < 600) ||
                    reqStatus >= 12000) {
                    this._hitError(reqStatus);
                    if (reqStatus >= 400 && reqStatus < 500) {
                        this._changeConnectStatus(Strophe.Status.DISCONNECTING,
                                                  null);
                        this._doDisconnect();
                    }
                }
            }

            if (!((reqStatus > 0 && reqStatus < 500) ||
                  req.sends > 5)) {
                this._throttledRequestHandler();
            }
        }
    },

    /** PrivateFunction: _hitError
     *  _Private_ function to handle the error count.
     *
     *  Requests are resent automatically until their error count reaches
     *  5.  Each time an error is encountered, this function is called to
     *  increment the count and disconnect if the count is too high.
     *
     *  Parameters:
     *    (Integer) reqStatus - The request status.
     */
    _hitError: function (reqStatus)
    {
        this.errors++;
        Strophe.warn("request errored, status: " + reqStatus +
                     ", number of errors: " + this.errors);
        if (this.errors > 4) {
            this._onDisconnectTimeout();
        }
    },

    /** PrivateFunction: _doDisconnect
     *  _Private_ function to disconnect.
     *
     *  This is the last piece of the disconnection logic.  This resets the
     *  connection and alerts the user's connection callback.
     */
    _doDisconnect: function ()
    {
        Strophe.info("_doDisconnect was called");
        this.authenticated = false;
        this.disconnecting = false;
        this.sid = null;
        this.streamId = null;
        this.rid = Math.floor(Math.random() * 4294967295);

        // tell the parent we disconnected
        if (this.connected) {
            this._changeConnectStatus(Strophe.Status.DISCONNECTED, null);
            this.connected = false;
        }

        // delete handlers
        this.handlers = [];
        this.timedHandlers = [];
        this.removeTimeds = [];
        this.removeHandlers = [];
        this.addTimeds = [];
        this.addHandlers = [];
    },

    /** PrivateFunction: _dataRecv
     *  _Private_ handler to processes incoming data from the the connection.
     *
     *  Except for _connect_cb handling the initial connection request,
     *  this function handles the incoming data for all requests.  This
     *  function also fires stanza handlers that match each incoming
     *  stanza.
     *
     *  Parameters:
     *    (Strophe.Request) req - The request that has data ready.
     */
    _dataRecv: function (req)
    {
        try {
            var elem = req.getResponse();
        } catch (e) {
            if (e != "parsererror") { throw e; }
            this.disconnect("strophe-parsererror");
        }
        if (elem === null) { return; }

        this.xmlInput(elem);
        this.rawInput(Strophe.serialize(elem));

        // remove handlers scheduled for deletion
        var i, hand;
        while (this.removeHandlers.length > 0) {
            hand = this.removeHandlers.pop();
            i = this.handlers.indexOf(hand);
            if (i >= 0) {
                this.handlers.splice(i, 1);
            }
        }

        // add handlers scheduled for addition
        while (this.addHandlers.length > 0) {
            this.handlers.push(this.addHandlers.pop());
        }

        // handle graceful disconnect
        if (this.disconnecting && this._requests.length === 0) {
            this.deleteTimedHandler(this._disconnectTimeout);
            this._disconnectTimeout = null;
            this._doDisconnect();
            return;
        }

        var typ = elem.getAttribute("type");
        var cond, conflict;
        if (typ !== null && typ == "terminate") {
            // Don't process stanzas that come in after disconnect
            if (this.disconnecting) {
                return;
            }

            // an error occurred
            cond = elem.getAttribute("condition");
            conflict = elem.getElementsByTagName("conflict");
            if (cond !== null) {
                if (cond == "remote-stream-error" && conflict.length > 0) {
                    cond = "conflict";
                }
                this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
            } else {
                this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
            }
            this.disconnect();
            return;
        }

        // send each incoming stanza through the handler chain
        var that = this;
        Strophe.forEachChild(elem, null, function (child) {
            var i, newList;
            // process handlers
            newList = that.handlers;
            that.handlers = [];
            for (i = 0; i < newList.length; i++) {
                var hand = newList[i];
                if (hand.isMatch(child) &&
                    (that.authenticated || !hand.user)) {
                    if (hand.run(child)) {
                        that.handlers.push(hand);
                    }
                } else {
                    that.handlers.push(hand);
                }
            }
        });
    },

    /** PrivateFunction: _sendTerminate
     *  _Private_ function to send initial disconnect sequence.
     *
     *  This is the first step in a graceful disconnect.  It sends
     *  the BOSH server a terminate body and includes an unavailable
     *  presence if authentication has completed.
     */
    _sendTerminate: function ()
    {
        Strophe.info("_sendTerminate was called");
        var body = this._buildBody().attrs({type: "terminate"});

        if (this.authenticated) {
            body.c('presence', {
                xmlns: Strophe.NS.CLIENT,
                type: 'unavailable'
            });
        }

        this.disconnecting = true;

        var req = new Strophe.Request(body.tree(),
                                      this._onRequestStateChange.bind(
                                          this, this._dataRecv.bind(this)),
                                      body.tree().getAttribute("rid"));

        this._requests.push(req);
        this._throttledRequestHandler();
    },

    /** PrivateFunction: _connect_cb
     *  _Private_ handler for initial connection request.
     *
     *  This handler is used to process the initial connection request
     *  response from the BOSH server. It is used to set up authentication
     *  handlers and start the authentication process.
     *
     *  SASL authentication will be attempted if available, otherwise
     *  the code will fall back to legacy authentication.
     *
     *  Parameters:
     *    (Strophe.Request) req - The current request.
     */
    _connect_cb: function (req)
    {
        Strophe.info("_connect_cb was called");

        this.connected = true;
        var bodyWrap = req.getResponse();
        if (!bodyWrap) { return; }

        this.xmlInput(bodyWrap);
        this.rawInput(Strophe.serialize(bodyWrap));

        var typ = bodyWrap.getAttribute("type");
        var cond, conflict;
        if (typ !== null && typ == "terminate") {
            // an error occurred
            cond = bodyWrap.getAttribute("condition");
            conflict = bodyWrap.getElementsByTagName("conflict");
            if (cond !== null) {
                if (cond == "remote-stream-error" && conflict.length > 0) {
                    cond = "conflict";
                }
                this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
            } else {
                this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
            }
            return;
        }

        // check to make sure we don't overwrite these if _connect_cb is
        // called multiple times in the case of missing stream:features
        if (!this.sid) {
            this.sid = bodyWrap.getAttribute("sid");
        }
        if (!this.stream_id) {
            this.stream_id = bodyWrap.getAttribute("authid");
        }
        var wind = bodyWrap.getAttribute('requests');
        if (wind) { this.window = parseInt(wind, 10); }
        var hold = bodyWrap.getAttribute('hold');
        if (hold) { this.hold = parseInt(hold, 10); }
        var wait = bodyWrap.getAttribute('wait');
        if (wait) { this.wait = parseInt(wait, 10); }


        var do_sasl_plain = false;
        var do_sasl_digest_md5 = false;
        var do_sasl_anonymous = false;

        var mechanisms = bodyWrap.getElementsByTagName("mechanism");
        var i, mech, auth_str, hashed_auth_str;
        if (mechanisms.length > 0) {
            for (i = 0; i < mechanisms.length; i++) {
                mech = Strophe.getText(mechanisms[i]);
                if (mech == 'DIGEST-MD5') {
                    do_sasl_digest_md5 = true;
                } else if (mech == 'PLAIN') {
                    do_sasl_plain = true;
                } else if (mech == 'ANONYMOUS') {
                    do_sasl_anonymous = true;
                }
            }
        } else {
            // we didn't get stream:features yet, so we need wait for it
            // by sending a blank poll request
            var body = this._buildBody();
            this._requests.push(
                new Strophe.Request(body.tree(),
                                    this._onRequestStateChange.bind(
                                        this, this._connect_cb.bind(this)),
                                    body.tree().getAttribute("rid")));
            this._throttledRequestHandler();
            return;
        }

        if (Strophe.getNodeFromJid(this.jid) === null &&
            do_sasl_anonymous) {
            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._sasl_success_handler = this._addSysHandler(
                this._sasl_success_cb.bind(this), null,
                "success", null, null);
            this._sasl_failure_handler = this._addSysHandler(
                this._sasl_failure_cb.bind(this), null,
                "failure", null, null);

            this.send($build("auth", {
                xmlns: Strophe.NS.SASL,
                mechanism: "ANONYMOUS"
            }).tree());
        } else if (Strophe.getNodeFromJid(this.jid) === null) {
            // we don't have a node, which is required for non-anonymous
            // client connections
            this._changeConnectStatus(Strophe.Status.CONNFAIL,
                                      'x-strophe-bad-non-anon-jid');
            this.disconnect();
        } else if (do_sasl_digest_md5) {
            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._sasl_challenge_handler = this._addSysHandler(
                this._sasl_challenge1_cb.bind(this), null,
                "challenge", null, null);
            this._sasl_failure_handler = this._addSysHandler(
                this._sasl_failure_cb.bind(this), null,
                "failure", null, null);

            this.send($build("auth", {
                xmlns: Strophe.NS.SASL,
                mechanism: "DIGEST-MD5"
            }).tree());
        } else if (do_sasl_plain) {
            // Build the plain auth string (barejid null
            // username null password) and base 64 encoded.
            auth_str = Strophe.getBareJidFromJid(this.jid);
            auth_str = auth_str + "\u0000";
            auth_str = auth_str + Strophe.getNodeFromJid(this.jid);
            auth_str = auth_str + "\u0000";
            auth_str = auth_str + this.pass;

            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._sasl_success_handler = this._addSysHandler(
                this._sasl_success_cb.bind(this), null,
                "success", null, null);
            this._sasl_failure_handler = this._addSysHandler(
                this._sasl_failure_cb.bind(this), null,
                "failure", null, null);

            hashed_auth_str = Base64.encode(auth_str);
            this.send($build("auth", {
                xmlns: Strophe.NS.SASL,
                mechanism: "PLAIN"
            }).t(hashed_auth_str).tree());
        } else {
            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._addSysHandler(this._auth1_cb.bind(this), null, null,
                                null, "_auth_1");

            this.send($iq({
                type: "get",
                to: this.domain,
                id: "_auth_1"
            }).c("query", {
                xmlns: Strophe.NS.AUTH
            }).c("username", {}).t(Strophe.getNodeFromJid(this.jid)).tree());
        }
    },

    /** PrivateFunction: _sasl_challenge1_cb
     *  _Private_ handler for DIGEST-MD5 SASL authentication.
     *
     *  Parameters:
     *    (XMLElement) elem - The challenge stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_challenge1_cb: function (elem)
    {
        var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;

        var challenge = Base64.decode(Strophe.getText(elem));
        var cnonce = MD5.hexdigest(Math.random() * 1234567890);
        var realm = "";
        var host = null;
        var nonce = "";
        var qop = "";
        var matches;

        // remove unneeded handlers
        this.deleteHandler(this._sasl_failure_handler);

        while (challenge.match(attribMatch)) {
            matches = challenge.match(attribMatch);
            challenge = challenge.replace(matches[0], "");
            matches[2] = matches[2].replace(/^"(.+)"$/, "$1");
            switch (matches[1]) {
            case "realm":
                realm = matches[2];
                break;
            case "nonce":
                nonce = matches[2];
                break;
            case "qop":
                qop = matches[2];
                break;
            case "host":
                host = matches[2];
                break;
            }
        }

        var digest_uri = "xmpp/" + this.domain;
        if (host !== null) {
            digest_uri = digest_uri + "/" + host;
        }

        var A1 = MD5.hash(Strophe.getNodeFromJid(this.jid) +
                          ":" + realm + ":" + this.pass) +
            ":" + nonce + ":" + cnonce;
        var A2 = 'AUTHENTICATE:' + digest_uri;

        var responseText = "";
        responseText += 'username=' +
            this._quote(Strophe.getNodeFromJid(this.jid)) + ',';
        responseText += 'realm=' + this._quote(realm) + ',';
        responseText += 'nonce=' + this._quote(nonce) + ',';
        responseText += 'cnonce=' + this._quote(cnonce) + ',';
        responseText += 'nc="00000001",';
        responseText += 'qop="auth",';
        responseText += 'digest-uri=' + this._quote(digest_uri) + ',';
        responseText += 'response=' + this._quote(
            MD5.hexdigest(MD5.hexdigest(A1) + ":" +
                          nonce + ":00000001:" +
                          cnonce + ":auth:" +
                          MD5.hexdigest(A2))) + ',';
        responseText += 'charset="utf-8"';

        this._sasl_challenge_handler = this._addSysHandler(
            this._sasl_challenge2_cb.bind(this), null,
            "challenge", null, null);
        this._sasl_success_handler = this._addSysHandler(
            this._sasl_success_cb.bind(this), null,
            "success", null, null);
        this._sasl_failure_handler = this._addSysHandler(
            this._sasl_failure_cb.bind(this), null,
            "failure", null, null);

        this.send($build('response', {
            xmlns: Strophe.NS.SASL
        }).t(Base64.encode(responseText)).tree());

        return false;
    },

    /** PrivateFunction: _quote
     *  _Private_ utility function to backslash escape and quote strings.
     *
     *  Parameters:
     *    (String) str - The string to be quoted.
     *
     *  Returns:
     *    quoted string
     */
    _quote: function (str)
    {
        return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
        //" end string workaround for emacs
    },


    /** PrivateFunction: _sasl_challenge2_cb
     *  _Private_ handler for second step of DIGEST-MD5 SASL authentication.
     *
     *  Parameters:
     *    (XMLElement) elem - The challenge stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_challenge2_cb: function (elem)
    {
        // remove unneeded handlers
        this.deleteHandler(this._sasl_success_handler);
        this.deleteHandler(this._sasl_failure_handler);

        this._sasl_success_handler = this._addSysHandler(
            this._sasl_success_cb.bind(this), null,
            "success", null, null);
        this._sasl_failure_handler = this._addSysHandler(
            this._sasl_failure_cb.bind(this), null,
            "failure", null, null);
        this.send($build('response', {xmlns: Strophe.NS.SASL}).tree());
        return false;
    },

    /** PrivateFunction: _auth1_cb
     *  _Private_ handler for legacy authentication.
     *
     *  This handler is called in response to the initial <iq type='get'/>
     *  for legacy authentication.  It builds an authentication <iq/> and
     *  sends it, creating a handler (calling back to _auth2_cb()) to
     *  handle the result
     *
     *  Parameters:
     *    (XMLElement) elem - The stanza that triggered the callback.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _auth1_cb: function (elem)
    {
        // build plaintext auth iq
        var iq = $iq({type: "set", id: "_auth_2"})
            .c('query', {xmlns: Strophe.NS.AUTH})
            .c('username', {}).t(Strophe.getNodeFromJid(this.jid))
            .up()
            .c('password').t(this.pass);

        if (!Strophe.getResourceFromJid(this.jid)) {
            // since the user has not supplied a resource, we pick
            // a default one here.  unlike other auth methods, the server
            // cannot do this for us.
            this.jid = Strophe.getBareJidFromJid(this.jid) + '/strophe';
        }
        iq.up().c('resource', {}).t(Strophe.getResourceFromJid(this.jid));

        this._addSysHandler(this._auth2_cb.bind(this), null,
                            null, null, "_auth_2");

        this.send(iq.tree());

        return false;
    },

    /** PrivateFunction: _sasl_success_cb
     *  _Private_ handler for succesful SASL authentication.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_success_cb: function (elem)
    {
        Strophe.info("SASL authentication succeeded.");

        // remove old handlers
        this.deleteHandler(this._sasl_failure_handler);
        this._sasl_failure_handler = null;
        if (this._sasl_challenge_handler) {
            this.deleteHandler(this._sasl_challenge_handler);
            this._sasl_challenge_handler = null;
        }

        this._addSysHandler(this._sasl_auth1_cb.bind(this), null,
                            "stream:features", null, null);

        // we must send an xmpp:restart now
        this._sendRestart();

        return false;
    },

    /** PrivateFunction: _sasl_auth1_cb
     *  _Private_ handler to start stream binding.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_auth1_cb: function (elem)
    {
        // save stream:features for future usage
        this.features = elem;

        var i, child;

        for (i = 0; i < elem.childNodes.length; i++) {
            child = elem.childNodes[i];
            if (child.nodeName == 'bind') {
                this.do_bind = true;
            }

            if (child.nodeName == 'session') {
                this.do_session = true;
            }
        }

        if (!this.do_bind) {
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        } else {
            this._addSysHandler(this._sasl_bind_cb.bind(this), null, null,
                                null, "_bind_auth_2");

            var resource = Strophe.getResourceFromJid(this.jid);
            if (resource) {
                this.send($iq({type: "set", id: "_bind_auth_2"})
                          .c('bind', {xmlns: Strophe.NS.BIND})
                          .c('resource', {}).t(resource).tree());
            } else {
                this.send($iq({type: "set", id: "_bind_auth_2"})
                          .c('bind', {xmlns: Strophe.NS.BIND})
                          .tree());
            }
        }

        return false;
    },

    /** PrivateFunction: _sasl_bind_cb
     *  _Private_ handler for binding result and session start.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_bind_cb: function (elem)
    {
        if (elem.getAttribute("type") == "error") {
            Strophe.info("SASL binding failed.");
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        }

        // TODO - need to grab errors
        var bind = elem.getElementsByTagName("bind");
        var jidNode;
        if (bind.length > 0) {
            // Grab jid
            jidNode = bind[0].getElementsByTagName("jid");
            if (jidNode.length > 0) {
                this.jid = Strophe.getText(jidNode[0]);

                if (this.do_session) {
                    this._addSysHandler(this._sasl_session_cb.bind(this),
                                        null, null, null, "_session_auth_2");

                    this.send($iq({type: "set", id: "_session_auth_2"})
                                  .c('session', {xmlns: Strophe.NS.SESSION})
                                  .tree());
                } else {
                    this.authenticated = true;
                    this._changeConnectStatus(Strophe.Status.CONNECTED, null);
                }
            }
        } else {
            Strophe.info("SASL binding failed.");
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        }
    },

    /** PrivateFunction: _sasl_session_cb
     *  _Private_ handler to finish successful SASL connection.
     *
     *  This sets Connection.authenticated to true on success, which
     *  starts the processing of user handlers.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_session_cb: function (elem)
    {
        if (elem.getAttribute("type") == "result") {
            this.authenticated = true;
            this._changeConnectStatus(Strophe.Status.CONNECTED, null);
        } else if (elem.getAttribute("type") == "error") {
            Strophe.info("Session creation failed.");
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        }

        return false;
    },

    /** PrivateFunction: _sasl_failure_cb
     *  _Private_ handler for SASL authentication failure.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_failure_cb: function (elem)
    {
        // delete unneeded handlers
        if (this._sasl_success_handler) {
            this.deleteHandler(this._sasl_success_handler);
            this._sasl_success_handler = null;
        }
        if (this._sasl_challenge_handler) {
            this.deleteHandler(this._sasl_challenge_handler);
            this._sasl_challenge_handler = null;
        }

        this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
        return false;
    },

    /** PrivateFunction: _auth2_cb
     *  _Private_ handler to finish legacy authentication.
     *
     *  This handler is called when the result from the jabber:iq:auth
     *  <iq/> stanza is returned.
     *
     *  Parameters:
     *    (XMLElement) elem - The stanza that triggered the callback.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _auth2_cb: function (elem)
    {
        if (elem.getAttribute("type") == "result") {
            this.authenticated = true;
            this._changeConnectStatus(Strophe.Status.CONNECTED, null);
        } else if (elem.getAttribute("type") == "error") {
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            this.disconnect();
        }

        return false;
    },

    /** PrivateFunction: _addSysTimedHandler
     *  _Private_ function to add a system level timed handler.
     *
     *  This function is used to add a Strophe.TimedHandler for the
     *  library code.  System timed handlers are allowed to run before
     *  authentication is complete.
     *
     *  Parameters:
     *    (Integer) period - The period of the handler.
     *    (Function) handler - The callback function.
     */
    _addSysTimedHandler: function (period, handler)
    {
        var thand = new Strophe.TimedHandler(period, handler);
        thand.user = false;
        this.addTimeds.push(thand);
        return thand;
    },

    /** PrivateFunction: _addSysHandler
     *  _Private_ function to add a system level stanza handler.
     *
     *  This function is used to add a Strophe.Handler for the
     *  library code.  System stanza handlers are allowed to run before
     *  authentication is complete.
     *
     *  Parameters:
     *    (Function) handler - The callback function.
     *    (String) ns - The namespace to match.
     *    (String) name - The stanza name to match.
     *    (String) type - The stanza type attribute to match.
     *    (String) id - The stanza id attribute to match.
     */
    _addSysHandler: function (handler, ns, name, type, id)
    {
        var hand = new Strophe.Handler(handler, ns, name, type, id);
        hand.user = false;
        this.addHandlers.push(hand);
        return hand;
    },

    /** PrivateFunction: _onDisconnectTimeout
     *  _Private_ timeout handler for handling non-graceful disconnection.
     *
     *  If the graceful disconnect process does not complete within the
     *  time allotted, this handler finishes the disconnect anyway.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _onDisconnectTimeout: function ()
    {
        Strophe.info("_onDisconnectTimeout was called");

        // cancel all remaining requests and clear the queue
        var req;
        while (this._requests.length > 0) {
            req = this._requests.pop();
            req.abort = true;
            req.xhr.abort();
            // jslint complains, but this is fine. setting to empty func
            // is necessary for IE6
            req.xhr.onreadystatechange = function () {};
        }

        // actually disconnect
        this._doDisconnect();

        return false;
    },

    /** PrivateFunction: _onIdle
     *  _Private_ handler to process events during idle cycle.
     *
     *  This handler is called every 100ms to fire timed handlers that
     *  are ready and keep poll requests going.
     */
    _onIdle: function ()
    {
        var i, thand, since, newList;

        // add timed handlers scheduled for addition
        // NOTE: we add before remove in the case a timed handler is
        // added and then deleted before the next _onIdle() call.
        while (this.addTimeds.length > 0) {
            this.timedHandlers.push(this.addTimeds.pop());
        }

        // remove timed handlers that have been scheduled for deletion
        while (this.removeTimeds.length > 0) {
            thand = this.removeTimeds.pop();
            i = this.timedHandlers.indexOf(thand);
            if (i >= 0) {
                this.timedHandlers.splice(i, 1);
            }
        }

        // call ready timed handlers
        var now = new Date().getTime();
        newList = [];
        for (i = 0; i < this.timedHandlers.length; i++) {
            thand = this.timedHandlers[i];
            if (this.authenticated || !thand.user) {
                since = thand.lastCalled + thand.period;
                if (since - now <= 0) {
                    if (thand.run()) {
                        newList.push(thand);
                    }
                } else {
                    newList.push(thand);
                }
            }
        }
        this.timedHandlers = newList;

        var body, time_elapsed;

        // if no requests are in progress, poll
        if (this.authenticated && this._requests.length === 0 &&
            this._data.length === 0 && !this.disconnecting) {
            Strophe.info("no requests during idle cycle, sending " +
                         "blank request");
            this._data.push(null);
        }

        if (this._requests.length < 2 && this._data.length > 0 &&
            !this.paused) {
            body = this._buildBody();
            for (i = 0; i < this._data.length; i++) {
                if (this._data[i] !== null) {
                    if (this._data[i] === "restart") {
                        body.attrs({
                            to: this.domain,
                            "xml:lang": "en",
                            "xmpp:restart": "true",
                            "xmlns:xmpp": Strophe.NS.BOSH
                        });
                    } else {
                        body.cnode(this._data[i]).up();
                    }
                }
            }
            delete this._data;
            this._data = [];
            this._requests.push(
                new Strophe.Request(body.tree(),
                                    this._onRequestStateChange.bind(
                                        this, this._dataRecv.bind(this)),
                                    body.tree().getAttribute("rid")));
            this._processRequest(this._requests.length - 1);
        }

        if (this._requests.length > 0) {
            time_elapsed = this._requests[0].age();
            if (this._requests[0].dead !== null) {
                if (this._requests[0].timeDead() >
                    Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait)) {
                    this._throttledRequestHandler();
                }
            }

            if (time_elapsed > Math.floor(Strophe.TIMEOUT * this.wait)) {
                Strophe.warn("Request " +
                             this._requests[0].id +
                             " timed out, over " + Math.floor(Strophe.TIMEOUT * this.wait) +
                             " seconds since last activity");
                this._throttledRequestHandler();
            }
        }

        // reactivate the timer
        clearTimeout(this._idleTimeout);
        this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
    }
};

if (callback) {
    callback(Strophe, $build, $msg, $iq, $pres);
}

})(function () {
    window.Strophe = arguments[0];
    window.$build = arguments[1];
    window.$msg = arguments[2];
    window.$iq = arguments[3];
    window.$pres = arguments[4];
});
/***
|''Name:''|TaggedTemplatePlugin|
|''Description:''|Apply a view/edit template based on a tag |
|''Author:''|PaulDowney (psd (at) osmosoft (dot) com)|
|''CodeRepository:''|http://svn.tiddlywiki.org/Trunk/contributors/PaulDowney/plugins/TaggedTemplatePlugin |
|''Version:''|1.1|
|''License:''|[[BSD License|http://www.opensource.org/licenses/bsd-license.php]] |
|''Comments:''|Please make comments at http://groups.google.co.uk/group/TiddlyWikiDev |
|''~CoreVersion:''|2.4|
|''Overrides''|Story.prototype.chooseTemplateForTiddler() |
!!Documentation
This plugin enables the application of a view or edit template based on a tag name. 

The priority of searching for duplicate templates has been designed to allow partially defined themes, in which missing templates are defaulted to ones defined as tiddlers.  Collation sequence is used to disambiguate collisions when a tiddler it tagged with more than one tag with an associated template. For example, tiddler tagged "Foo Bar" will result in a hunt for a template as follows:
# in the current theme by tag:
##[[CurrentTheme]]##barViewTemplate
##[[CurrentTheme]]##fooViewTemplate
# in a template for the tag:
##[[barViewTemplate]]
##[[fooViewTemplate]]
# global in the current theme
##[[CurrentTheme]]##ViewTemplate
# in the global tiddler
##[[ViewTemplate]]
This plugin is used by RippleRap, TiddlyResume and a number of other verticals which use nested themes was based on Eric Shulman's [[TaggedTemplateTweak|http://www.TiddlyTools.com/#TaggedTemplateTweak]] plugin which at the time didn't support themes and searches in a different order.
!!Code
***/
//{{{
/*jslint onevar: false nomen: false plusplus: false */
/*global Story, store, config */
if (!version.extensions.TaggedTemplatePlugin) {
    version.extensions.TaggedTemplatePlugin = {installed: true};

    Story.prototype._chooseTemplateForTiddler = Story.prototype.chooseTemplateForTiddler;
    Story.prototype.chooseTemplateForTiddler = function (title, n)
    {
        var slice, i, tags = [], tagTiddler;
        var tiddler = store.getTiddler(title);

        // translate number into template name
        var template = ["ViewTemplate", "EditTemplate"][n ? n - 1 : 0];

        // canonicalise tags
        if (tiddler) {
            for (i = 0; i < tiddler.tags.length; i++) {
                tags.push(tiddler.tags[i].toLowerCase());
            }
            tags.sort();
        }

        // search theme for tagged template in the current theme
        if (tags.length && store.tiddlerExists(config.options.txtTheme)) {
            for (i = 0; i < tags.length; i++) {
                slice = config.options.txtTheme + '##' + tags[i] + template;
                if (store.getTiddlerText(slice)) {
                    return slice;
                }
            }
        }

        // search theme for tagged template tiddler
        for (i = 0; i < tags.length; i++) {
            tagTiddler = tags[i] + template;
            if (store.tiddlerExists(tagTiddler)) {
                return tagTiddler;
            }
        }

        // search theme for template
        if (store.tiddlerExists(config.options.txtTheme)) {
            slice = config.options.txtTheme + '##' + template;
            if (store.getTiddlerText(slice)) {
                return slice;
            }
        }

        return this._chooseTemplateForTiddler.apply(this, arguments);
    };
}
//}}}