ejabberdctl register username jabber.joyreactor.ru password
import org.jivesoftware.smack. * ;
Roster. setDefaultSubscriptionMode ( SubscriptionMode. accept_all ) ;
XMPPConnection icqConnection = new XMPPConnection ( ICQ_HOST ) ;
icqConnection. connect ( ) ;
SASLAuthentication. supportSASLMechanism ( "PLAIN" , 0 ) ; // To connect to EJabberd
icqConnection. login ( ICQ_LOGIN, ICQ_PASS ) ;
Roster. setDefaultSubscriptionMode ( SubscriptionMode. accept_all ) ;
XMPPConnection icqConnection = new XMPPConnection ( ICQ_HOST ) ;
icqConnection. connect ( ) ;
SASLAuthentication. supportSASLMechanism ( "PLAIN" , 0 ) ; // To connect to EJabberd
icqConnection. login ( ICQ_LOGIN, ICQ_PASS ) ;
RosterListener rosterListener = new RosterListener ( ) {send message:
public void entriesAdded ( Collection < String > arg0 ) {
Roster roster = connection. getRoster ( ) ;
RosterGroup gr = roster. getGroup ( "Users" ) ;
if ( gr == null )
gr = roster createGroup ( "Users" ) ;
for ( String entry : arg0 ) {
try {
String [ ] groups = { "Users" } ;
if ( roster. getEntry ( entry ) == null )
roster. createEntry ( entry, null , groups ) ;
} catch ( XMPPException ex ) {
// TODO: Handle exception.
}
}
}
public void entriesUpdated ( Collection < String > arg0 ) {
Roster roster = connection. getRoster ( ) ;
RosterGroup gr = roster. getGroup ( "Users" ) ;
if ( gr == null )
gr = roster createGroup ( "Users" ) ;
for ( String entry : arg0 ) {
try {
String [ ] groups = { "Users" } ;
if ( roster. getEntry ( entry ) == null )
roster. createEntry ( entry, null , groups ) ;
} catch ( XMPPException ex ) {
// TODO: Handle exception.
}
}
}
public void entriesDeleted ( Collection < String > arg0 ) { }
public void presenceChanged ( Presence packet ) {
try {
org. jivesoftware . smack packet . Presence prs = ( org. Jivesoftware . Smack . Packet . Presence ) packet ;
if ( ! prs. getFrom ( ) . split ( "@" ) [ 1 ] . equalsIgnoreCase ( ICQ_TRANSPORT ) )
// TODO: Process presence.
} catch ( Exception ex ) {
// TODO: Handle exception.
}
}
} ;
PacketListener icqListener = new PacketListener ( ) {
public void processPacket ( Packet packet ) {
try {
org. jivesoftware . smack packet . Message msg = ( org. Jivesoftware . Smack . Packet . Message ) packet ;
if ( msg. getType ( ) ! = org. jivesoftware . smack . packet . Message . Type . error &&
msg. getFrom ( ) . split ( "@" ) [ 1 ] . equalsIgnoreCase ( ICQ_TRANSPORT ) )
// TODO: Process message.
} catch ( Exception ex ) {
// TODO: Handle exception.
}
}
} ;
icqConnection. getRoster ( ) . addRosterListener ( icqRosterListener ) ;
icqConnection. addPacketListener ( icqListener, new PacketTypeFilter ( org. jivesoftware . smack . packet . Message . class ) ) ;
org. jivesoftware . smack packet . Message message = new org. jivesoftware . smack packet . Message ( "587641264" + "@" + ICQ_TRANSPORT ) ;
message. setBody ( "Hello world !!!" ) ;
icqConnection. sendPacket ( message ) ;
Source: https://habr.com/ru/post/79911/
All Articles