80
examples/echo-bot.js
Archivo normal
80
examples/echo-bot.js
Archivo normal
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Simple example: Echo Bot
|
||||
* A bot that echoes back any message sent to it
|
||||
*/
|
||||
|
||||
const { Client } = require('@xmpp/client');
|
||||
const { xml } = require('@xmpp/client');
|
||||
|
||||
// Bot configuration
|
||||
const BOT_JID = 'echobot@localhost';
|
||||
const BOT_PASSWORD = 'password';
|
||||
const SERVER = 'localhost';
|
||||
const PORT = 5222;
|
||||
|
||||
// Create XMPP client
|
||||
const client = new Client({
|
||||
service: `xmpp://${SERVER}:${PORT}`,
|
||||
domain: 'localhost',
|
||||
username: 'echobot',
|
||||
password: BOT_PASSWORD
|
||||
});
|
||||
|
||||
// Handle connection
|
||||
client.on('online', async (address) => {
|
||||
console.log(`Echo Bot online as ${address.toString()}`);
|
||||
|
||||
// Send initial presence
|
||||
await client.send(xml('presence'));
|
||||
|
||||
console.log('Echo Bot ready to receive messages!');
|
||||
});
|
||||
|
||||
// Handle incoming stanzas
|
||||
client.on('stanza', async (stanza) => {
|
||||
// Only process message stanzas
|
||||
if (stanza.is('message')) {
|
||||
const from = stanza.attrs.from;
|
||||
const body = stanza.getChildText('body');
|
||||
const type = stanza.attrs.type || 'chat';
|
||||
|
||||
// Ignore messages without body or from ourselves
|
||||
if (!body || from === BOT_JID) return;
|
||||
|
||||
console.log(`Message from ${from}: ${body}`);
|
||||
|
||||
// Create echo response
|
||||
const response = xml(
|
||||
'message',
|
||||
{ type, to: from },
|
||||
xml('body', {}, `Echo: ${body}`)
|
||||
);
|
||||
|
||||
// Send response
|
||||
await client.send(response);
|
||||
console.log(`Sent echo to ${from}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
client.on('error', (err) => {
|
||||
console.error('Error:', err);
|
||||
});
|
||||
|
||||
// Handle offline
|
||||
client.on('offline', () => {
|
||||
console.log('Echo Bot offline');
|
||||
});
|
||||
|
||||
// Start the client
|
||||
client.start().catch(console.error);
|
||||
|
||||
// Handle process termination
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\nShutting down Echo Bot...');
|
||||
await client.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
console.log('Starting Echo Bot...');
|
||||
console.log(`Connecting to ${SERVER}:${PORT} as ${BOT_JID}`);
|
||||
49
examples/modules/mod_welcome.js
Archivo normal
49
examples/modules/mod_welcome.js
Archivo normal
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Example Module: Welcome Message
|
||||
* Sends a welcome message to users when they first log in
|
||||
*/
|
||||
|
||||
const ltx = require('ltx');
|
||||
|
||||
module.exports = {
|
||||
name: 'welcome_message',
|
||||
version: '1.0.0',
|
||||
description: 'Sends welcome messages to new users',
|
||||
author: 'Example',
|
||||
|
||||
load(module) {
|
||||
const { logger, sessionManager, config } = module.api;
|
||||
|
||||
// Get welcome message from config
|
||||
const welcomeMsg = module.api.getConfig('message',
|
||||
'Welcome to our XMPP server! Enjoy your stay.');
|
||||
|
||||
const sendWelcome = module.api.getConfig('sendOnLogin', true);
|
||||
|
||||
logger.info('Welcome message module loaded');
|
||||
|
||||
if (sendWelcome) {
|
||||
// Hook into session authentication
|
||||
sessionManager.on('session:authenticated', (session) => {
|
||||
logger.info(`Sending welcome message to ${session.jid}`);
|
||||
|
||||
// Create welcome message
|
||||
const message = new ltx.Element('message', {
|
||||
to: session.jid,
|
||||
from: module.host,
|
||||
type: 'chat'
|
||||
}).c('body').t(welcomeMsg).up()
|
||||
.c('subject').t('Welcome!');
|
||||
|
||||
// Send message
|
||||
session.send(message);
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(`Welcome message: "${welcomeMsg}"`);
|
||||
},
|
||||
|
||||
unload(module) {
|
||||
module.api.logger.info('Welcome message module unloaded');
|
||||
}
|
||||
};
|
||||
128
examples/simple-client.js
Archivo normal
128
examples/simple-client.js
Archivo normal
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* Example: Simple XMPP Client
|
||||
* Demonstrates basic XMPP client functionality
|
||||
*/
|
||||
|
||||
const { Client } = require('@xmpp/client');
|
||||
const { xml } = require('@xmpp/client');
|
||||
const readline = require('readline');
|
||||
|
||||
// Configuration
|
||||
const config = {
|
||||
service: 'xmpp://localhost:5222',
|
||||
domain: 'localhost',
|
||||
username: 'user1',
|
||||
password: 'password'
|
||||
};
|
||||
|
||||
// Create client
|
||||
const client = new Client(config);
|
||||
|
||||
// Setup readline for input
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
prompt: '> '
|
||||
});
|
||||
|
||||
// Connection established
|
||||
client.on('online', async (address) => {
|
||||
console.log(`Connected as ${address.toString()}`);
|
||||
|
||||
// Send presence
|
||||
await client.send(xml('presence'));
|
||||
|
||||
console.log('\nCommands:');
|
||||
console.log(' /send <jid> <message> - Send a message');
|
||||
console.log(' /presence [status] - Set presence');
|
||||
console.log(' /quit - Disconnect');
|
||||
console.log('');
|
||||
|
||||
rl.prompt();
|
||||
});
|
||||
|
||||
// Handle incoming stanzas
|
||||
client.on('stanza', (stanza) => {
|
||||
if (stanza.is('message')) {
|
||||
const from = stanza.attrs.from;
|
||||
const body = stanza.getChildText('body');
|
||||
|
||||
if (body) {
|
||||
console.log(`\n[${from}]: ${body}`);
|
||||
rl.prompt();
|
||||
}
|
||||
} else if (stanza.is('presence')) {
|
||||
const from = stanza.attrs.from;
|
||||
const type = stanza.attrs.type;
|
||||
const show = stanza.getChildText('show');
|
||||
const status = stanza.getChildText('status');
|
||||
|
||||
if (type === 'unavailable') {
|
||||
console.log(`\n${from} is now offline`);
|
||||
} else {
|
||||
const state = show || 'available';
|
||||
const statusText = status ? ` (${status})` : '';
|
||||
console.log(`\n${from} is now ${state}${statusText}`);
|
||||
}
|
||||
rl.prompt();
|
||||
}
|
||||
});
|
||||
|
||||
// Handle user input
|
||||
rl.on('line', async (line) => {
|
||||
const parts = line.trim().split(' ');
|
||||
const command = parts[0];
|
||||
|
||||
try {
|
||||
if (command === '/send' && parts.length >= 3) {
|
||||
const to = parts[1];
|
||||
const message = parts.slice(2).join(' ');
|
||||
|
||||
await client.send(
|
||||
xml('message', { type: 'chat', to },
|
||||
xml('body', {}, message)
|
||||
)
|
||||
);
|
||||
|
||||
console.log(`Sent to ${to}: ${message}`);
|
||||
|
||||
} else if (command === '/presence') {
|
||||
const status = parts.slice(1).join(' ');
|
||||
|
||||
const presence = xml('presence');
|
||||
if (status) {
|
||||
presence.append(xml('status', {}, status));
|
||||
}
|
||||
|
||||
await client.send(presence);
|
||||
console.log('Presence updated');
|
||||
|
||||
} else if (command === '/quit') {
|
||||
await client.stop();
|
||||
process.exit(0);
|
||||
|
||||
} else if (command.startsWith('/')) {
|
||||
console.log('Unknown command');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
}
|
||||
|
||||
rl.prompt();
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
client.on('error', (err) => {
|
||||
console.error('Error:', err.message);
|
||||
});
|
||||
|
||||
// Start client
|
||||
console.log('Connecting to XMPP server...');
|
||||
client.start().catch(console.error);
|
||||
|
||||
// Handle process termination
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\nDisconnecting...');
|
||||
await client.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
Referencia en una nueva incidencia
Block a user