/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/iobroker.xmpp@0.0.2/build/main.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var __create=Object.create,__defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty,__copyProps=(e,t,s,a)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let i of __getOwnPropNames(t))__hasOwnProp.call(e,i)||i===s||__defProp(e,i,{get:()=>t[i],enumerable:!(a=__getOwnPropDesc(t,i))||a.enumerable});return e},__toESM=(e,t,s)=>(s=null!=e?__create(__getProtoOf(e)):{},__copyProps(!t&&e&&e.__esModule?s:__defProp(s,"default",{value:e,enumerable:!0}),e)),utils=__toESM(require("@iobroker/adapter-core")),import_client=require("@xmpp/client");class Xmpp extends utils.Adapter{constructor(e={}){super({...e,name:"xmpp"}),this.xmpp=(0,import_client.client)(),this.xmpp_connected=!1,this.stateChange_callbacks=[],this.jids={admin_jids:[],allow_messages_from_jids:[],allow_subscribe_from_jids:[],send_all_messages_to_jids:[]},this.on("ready",this.onReady.bind(this)),this.on("stateChange",this.onStateChange.bind(this)),this.on("message",this.onMessage.bind(this)),this.on("unload",this.onUnload.bind(this))}async onReady(){this.setState("info.connection",!1,!0),this.xmpp_connected=!0;let e=this.config.users.filter((e=>e.admin)).map((e=>e.jid)),t=this.config.users.filter((e=>e.allow_messages)).map((e=>e.jid)),s=this.config.users.filter((e=>e.allow_subscribe)).map((e=>e.jid)),a=this.config.users.filter((e=>e.send_all_messages)).map((e=>e.jid));var i;switch(this.jids.admin_jids=e,this.jids.allow_messages_from_jids=t,this.jids.allow_subscribe_from_jids=s,this.jids.send_all_messages_to_jids=a,this.config.tls){case"plain":case"starttls":default:i="xmpp";break;case"ssl":i="xmpps"}this.xmpp=(0,import_client.client)({service:`${i}://${this.config.hostname}:${this.config.port}`,domain:this.config.hostname,resource:"iobroker",username:this.config.username,password:this.config.password}),this.xmpp.on("error",(e=>{this.log.error("XMPP error: "+e)})),this.xmpp.on("offline",(()=>{this.setState("info.connection",!1,!0),this.xmpp_connected=!1,this.log.info("XMPP disconnected")})),this.xmpp.on("stanza",(async e=>{try{const s=(0,import_client.jid)(e.attrs.from),a=s.getResource().toString(),i=s.bare().toString(),o=(0,import_client.jid)(e.attrs.to).bare().toString();if(e.is("message")){if(t.includes(i)){let t=e.getChildText("body");this.setStateAsync("last_message.from.resource",a,!0),this.setStateAsync("last_message.from.user",i,!0),this.setStateAsync("last_message.to.user",o,!0),this.setStateAsync("last_message.message",t,!0);let s={"from.resource":a,"from.user":i,"to.user":o,message:t};this.setStateAsync("last_message.object",JSON.stringify(s),!0)}}else e.is("presence")&&(this.config.username,this.config.hostname)}catch(e){let t;"string"==typeof e?t=e.toUpperCase():e instanceof Error&&(t=e.message),t&&this.log.error(t)}})),this.xmpp.on("online",(async t=>{await this.xmpp.send((0,import_client.xml)("presence")),this.setState("info.connection",!0,!0),this.xmpp_connected=!0;const s=e.map((e=>(0,import_client.xml)("message",{to:e,type:"chat"},(0,import_client.xml)("body",{},"XMPP Adapter is now online"))));await this.xmpp.sendMany(s).catch(console.error)})),this.xmpp.start().catch(this.log.error),await this.setObjectNotExistsAsync("last_message.object",{type:"state",common:{name:"the last received message as json object",type:"string",role:"json",read:!0,write:!1},native:{}}),await this.setObjectNotExistsAsync("last_message.from.resource",{type:"state",common:{name:"resource of user that send the last received message",type:"string",role:"text",read:!0,write:!1},native:{}}),await this.setObjectNotExistsAsync("last_message.from.user",{type:"state",common:{name:"user that send the last received message",type:"string",role:"text",read:!0,write:!1},native:{}}),await this.setObjectNotExistsAsync("last_message.to.user",{type:"state",common:{name:"user the last received message was send to",type:"string",role:"text",read:!0,write:!1},native:{}}),await this.setObjectNotExistsAsync("last_message.message",{type:"state",common:{name:" text of the last received message",type:"string",role:"text",read:!0,write:!1},native:{}}),this.subscribeStates("testVariable"),await this.setStateAsync("testVariable",!0),await this.setStateAsync("testVariable",{val:!0,ack:!0}),await this.setStateAsync("testVariable",{val:!0,ack:!0,expire:30});let o=await this.checkPasswordAsync("admin","iobroker");this.log.info("check user admin pw iobroker: "+o),o=await this.checkGroupAsync("admin","admin"),this.log.info("check group user admin group admin: "+o)}onUnload(e){try{this.xmpp.send((0,import_client.xml)("presence",{type:"unavailable"})).then((()=>{this.xmpp.stop().then((()=>{e()}))}))}catch(t){e()}}onStateChange(e,t){t?(t.ack&&"last_message.object"===e&&this.stateChange_callbacks.map((async(e,s,a)=>{var i,o;let n=null!=(o=null==(i=t.val)?void 0:i.toString())?o:"null";e(JSON.parse(n)),delete a[s]})),this.log.info(`state ${e} changed: ${t.val} (ack = ${t.ack})`)):this.log.info(`state ${e} deleted`)}onMessage(e){if(this.xmpp_connected){if("object"==typeof e&&e.message&&"send"===e.command){this.log.info("send command");let t="",s=this.jids.send_all_messages_to_jids;switch(typeof e.message){case"string":t=e.message;break;case"object":"object"==typeof e.message.recipients?s=e.message.recipients:"string"==typeof e.message.to&&(s=[e.message.to]),"string"==typeof e.message.message&&(t=e.message.message)}const a=s.map((e=>(0,import_client.xml)("message",{to:e,type:"chat"},(0,import_client.xml)("body",{},t))));this.xmpp.sendMany(a).catch(console.error),e.callback&&this.stateChange_callbacks.push((t=>{this.sendTo(e.from,e.command,t,e.callback)}))}}else this.log.error("XMPP not connected! refusing sendTo")}}require.main!==module?module.exports=e=>new Xmpp(e):new Xmpp;
//# sourceMappingURL=/sm/728a61ea26e893d82eae11e898c9bc2400634104fc64dbd3a61750fa4582194d.map