1package eu.siacs.conversations.services;
2
3import java.text.ParseException;
4import java.text.SimpleDateFormat;
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.Date;
8import java.util.Hashtable;
9import java.util.List;
10import java.util.Locale;
11import java.util.Random;
12
13import org.openintents.openpgp.util.OpenPgpApi;
14import org.openintents.openpgp.util.OpenPgpServiceConnection;
15
16import net.java.otr4j.OtrException;
17import net.java.otr4j.session.Session;
18import net.java.otr4j.session.SessionStatus;
19
20import eu.siacs.conversations.crypto.PgpEngine;
21import eu.siacs.conversations.crypto.PgpEngine.OpenPgpException;
22import eu.siacs.conversations.entities.Account;
23import eu.siacs.conversations.entities.Contact;
24import eu.siacs.conversations.entities.Conversation;
25import eu.siacs.conversations.entities.Message;
26import eu.siacs.conversations.entities.MucOptions;
27import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
28import eu.siacs.conversations.entities.Presences;
29import eu.siacs.conversations.persistance.DatabaseBackend;
30import eu.siacs.conversations.persistance.OnPhoneContactsMerged;
31import eu.siacs.conversations.ui.OnAccountListChangedListener;
32import eu.siacs.conversations.ui.OnConversationListChangedListener;
33import eu.siacs.conversations.ui.OnRosterFetchedListener;
34import eu.siacs.conversations.utils.ExceptionHelper;
35import eu.siacs.conversations.utils.MessageParser;
36import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener;
37import eu.siacs.conversations.utils.PhoneHelper;
38import eu.siacs.conversations.utils.UIHelper;
39import eu.siacs.conversations.xml.Element;
40import eu.siacs.conversations.xmpp.OnBindListener;
41import eu.siacs.conversations.xmpp.OnIqPacketReceived;
42import eu.siacs.conversations.xmpp.OnJinglePacketReceived;
43import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
44import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
45import eu.siacs.conversations.xmpp.OnStatusChanged;
46import eu.siacs.conversations.xmpp.OnTLSExceptionReceived;
47import eu.siacs.conversations.xmpp.XmppConnection;
48import eu.siacs.conversations.xmpp.stanzas.IqPacket;
49import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
50import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
51import eu.siacs.conversations.xmpp.stanzas.jingle.JinglePacket;
52import android.app.AlarmManager;
53import android.app.PendingIntent;
54import android.app.Service;
55import android.content.Context;
56import android.content.Intent;
57import android.content.SharedPreferences;
58import android.database.ContentObserver;
59import android.database.DatabaseUtils;
60import android.net.ConnectivityManager;
61import android.net.NetworkInfo;
62import android.os.Binder;
63import android.os.Bundle;
64import android.os.IBinder;
65import android.os.PowerManager;
66import android.os.PowerManager.WakeLock;
67import android.os.SystemClock;
68import android.preference.PreferenceManager;
69import android.provider.ContactsContract;
70import android.util.Log;
71
72public class XmppConnectionService extends Service {
73
74 protected static final String LOGTAG = "xmppService";
75 public DatabaseBackend databaseBackend;
76
77 public long startDate;
78
79 private static final int PING_MAX_INTERVAL = 300;
80 private static final int PING_MIN_INTERVAL = 10;
81 private static final int PING_TIMEOUT = 5;
82 private static final int CONNECT_TIMEOUT = 60;
83
84 private List<Account> accounts;
85 private List<Conversation> conversations = null;
86
87 public OnConversationListChangedListener convChangedListener = null;
88 private OnAccountListChangedListener accountChangedListener = null;
89 private OnTLSExceptionReceived tlsException = null;
90
91 public void setOnTLSExceptionReceivedListener(
92 OnTLSExceptionReceived listener) {
93 tlsException = listener;
94 }
95
96 private Random mRandom = new Random(System.currentTimeMillis());
97
98 private ContentObserver contactObserver = new ContentObserver(null) {
99 @Override
100 public void onChange(boolean selfChange) {
101 super.onChange(selfChange);
102 Log.d(LOGTAG, "contact list has changed");
103 mergePhoneContactsWithRoster(null);
104 }
105 };
106
107 private XmppConnectionService service = this;
108
109 private final IBinder mBinder = new XmppConnectionBinder();
110 private OnMessagePacketReceived messageListener = new OnMessagePacketReceived() {
111
112 @Override
113 public void onMessagePacketReceived(Account account,
114 MessagePacket packet) {
115 Message message = null;
116 boolean notify = true;
117 if ((packet.getType() == MessagePacket.TYPE_CHAT)) {
118 String pgpBody = MessageParser.getPgpBody(packet);
119 if (pgpBody != null) {
120 message = MessageParser.parsePgpChat(pgpBody, packet,
121 account, service);
122 message.markUnread();
123 } else if (packet.hasChild("body")
124 && (packet.getBody().startsWith("?OTR"))) {
125 message = MessageParser.parseOtrChat(packet, account,
126 service);
127 if (message != null) {
128 message.markUnread();
129 }
130 } else if (packet.hasChild("body")) {
131 message = MessageParser.parsePlainTextChat(packet, account,
132 service);
133 message.markUnread();
134 } else if (packet.hasChild("received")
135 || (packet.hasChild("sent"))) {
136 message = MessageParser.parseCarbonMessage(packet, account,
137 service);
138 if (message != null) {
139 message.getConversation().markRead();
140 }
141 notify = false;
142 }
143
144 } else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
145 message = MessageParser
146 .parseGroupchat(packet, account, service);
147 if (message != null) {
148 if (message.getStatus() == Message.STATUS_RECIEVED) {
149 message.markUnread();
150 } else {
151 message.getConversation().markRead();
152 notify = false;
153 }
154 }
155 } else if (packet.getType() == MessagePacket.TYPE_ERROR) {
156 message = MessageParser.parseError(packet, account, service);
157 } else if (packet.getType() == MessagePacket.TYPE_NORMAL) {
158 if (packet.hasChild("x")) {
159 Element x = packet.findChild("x");
160 if (x.hasChild("invite")) {
161 findOrCreateConversation(account, packet.getFrom(),
162 true);
163 if (convChangedListener != null) {
164 convChangedListener.onConversationListChanged();
165 }
166 Log.d(LOGTAG,
167 "invitation received to " + packet.getFrom());
168 }
169
170 } else {
171 // Log.d(LOGTAG, "unparsed message " + packet.toString());
172 }
173 }
174 if ((message == null) || (message.getBody() == null)) {
175 return;
176 }
177 if (packet.hasChild("delay")) {
178 try {
179 String stamp = packet.findChild("delay").getAttribute(
180 "stamp");
181 stamp = stamp.replace("Z", "+0000");
182 Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
183 .parse(stamp);
184 message.setTime(date.getTime());
185 } catch (ParseException e) {
186 Log.d(LOGTAG, "error trying to parse date" + e.getMessage());
187 }
188 }
189 Conversation conversation = message.getConversation();
190 conversation.getMessages().add(message);
191 if (packet.getType() != MessagePacket.TYPE_ERROR) {
192 databaseBackend.createMessage(message);
193 }
194 if (convChangedListener != null) {
195 convChangedListener.onConversationListChanged();
196 } else {
197 UIHelper.updateNotification(getApplicationContext(),
198 getConversations(), message.getConversation(), notify);
199 }
200 }
201 };
202 private OnStatusChanged statusListener = new OnStatusChanged() {
203
204 @Override
205 public void onStatusChanged(Account account) {
206 if (accountChangedListener != null) {
207 accountChangedListener.onAccountListChangedListener();
208 }
209 if (account.getStatus() == Account.STATUS_ONLINE) {
210 List<Conversation> conversations = getConversations();
211 for (int i = 0; i < conversations.size(); ++i) {
212 if (conversations.get(i).getAccount() == account) {
213 sendUnsendMessages(conversations.get(i));
214 }
215 }
216 scheduleWakeupCall(PING_MAX_INTERVAL, true);
217 } else if (account.getStatus() == Account.STATUS_OFFLINE) {
218 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
219 int timeToReconnect = mRandom.nextInt(50) + 10;
220 scheduleWakeupCall(timeToReconnect, false);
221 }
222
223 } else if (account.getStatus() == Account.STATUS_REGISTRATION_SUCCESSFULL) {
224 databaseBackend.updateAccount(account);
225 reconnectAccount(account, true);
226 }
227 }
228 };
229
230 private OnPresencePacketReceived presenceListener = new OnPresencePacketReceived() {
231
232 @Override
233 public void onPresencePacketReceived(Account account,
234 PresencePacket packet) {
235 if (packet.hasChild("x","http://jabber.org/protocol/muc#user")) {
236 Conversation muc = findMuc(
237 packet.getAttribute("from").split("/")[0], account);
238 if (muc != null) {
239 muc.getMucOptions().processPacket(packet);
240 } else {
241 Log.d(LOGTAG,account.getJid()+": could not find muc for received muc package "+packet.toString());
242 }
243 } else if (packet.hasChild("x","http://jabber.org/protocol/muc")) {
244 Conversation muc = findMuc(packet.getAttribute("from").split("/")[0], account);
245 if (muc != null) {
246 Log.d(LOGTAG,account.getJid()+": reading muc status packet "+packet.toString());
247 int error = muc.getMucOptions().getError();
248 muc.getMucOptions().processPacket(packet);
249 if ((muc.getMucOptions().getError() != error)
250 && (convChangedListener != null)) {
251 Log.d(LOGTAG, "muc error status changed");
252 convChangedListener.onConversationListChanged();
253 }
254 }
255 } else {
256 String[] fromParts = packet.getAttribute("from").split("/");
257 String type = packet.getAttribute("type");
258 if (fromParts[0].equals(account.getJid())) {
259 if (fromParts.length==2) {
260 if (type == null) {
261 account.updatePresence(fromParts[1],Presences.parseShow(packet.findChild("show")));
262 } else if (type.equals("unavailable")) {
263 account.removePresence(fromParts[1]);
264 }
265 }
266
267 } else {
268 Contact contact = findContact(account, fromParts[0]);
269 if (contact == null) {
270 // most likely roster not synced
271 return;
272 }
273 if (type == null) {
274 if (fromParts.length == 2) {
275 contact.updatePresence(fromParts[1], Presences.parseShow(packet.findChild("show")));
276 PgpEngine pgp = getPgpEngine();
277 if (pgp != null) {
278 Element x = packet.findChild("x","jabber:x:signed");
279 if (x != null) {
280 try {
281 Element status = packet.findChild("status");
282 String msg;
283 if (status!=null) {
284 msg = status.getContent();
285 } else {
286 msg = "";
287 }
288 contact.setPgpKeyId(pgp.fetchKeyId(msg, x
289 .getContent()));
290 } catch (OpenPgpException e) {
291 Log.d(LOGTAG, "faulty pgp. just ignore");
292 }
293 }
294 }
295 databaseBackend.updateContact(contact);
296 } else {
297 // Log.d(LOGTAG,"presence without resource "+packet.toString());
298 }
299 } else if (type.equals("unavailable")) {
300 if (fromParts.length != 2) {
301 // Log.d(LOGTAG,"received presence with no resource "+packet.toString());
302 } else {
303 contact.removePresence(fromParts[1]);
304 databaseBackend.updateContact(contact);
305 }
306 } else if (type.equals("subscribe")) {
307 if (contact
308 .getSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT)) {
309 sendPresenceUpdatesTo(contact);
310 contact.setSubscriptionOption(Contact.Subscription.FROM);
311 contact.resetSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
312 replaceContactInConversation(contact.getJid(),
313 contact);
314 databaseBackend.updateContact(contact);
315 if ((contact
316 .getSubscriptionOption(Contact.Subscription.ASKING))
317 && (!contact
318 .getSubscriptionOption(Contact.Subscription.TO))) {
319 requestPresenceUpdatesFrom(contact);
320 }
321 } else {
322 // TODO: ask user to handle it maybe
323 }
324 } else {
325 //Log.d(LOGTAG, packet.toString());
326 }
327 replaceContactInConversation(contact.getJid(), contact);
328 }
329 }
330 }
331 };
332
333 private OnIqPacketReceived unknownIqListener = new OnIqPacketReceived() {
334
335 @Override
336 public void onIqPacketReceived(Account account, IqPacket packet) {
337 if (packet.hasChild("query")) {
338 Element query = packet.findChild("query");
339 String xmlns = query.getAttribute("xmlns");
340 if ((xmlns != null) && (xmlns.equals("jabber:iq:roster"))) {
341 processRosterItems(account, query);
342 mergePhoneContactsWithRoster(null);
343 }
344 }
345 }
346 };
347
348 private OnJinglePacketReceived jingleListener = new OnJinglePacketReceived() {
349
350 @Override
351 public void onJinglePacketReceived(Account account, JinglePacket packet) {
352 Log.d(LOGTAG,account.getJid()+": jingle packet received"+packet.toString());
353 }
354 };
355
356 private OpenPgpServiceConnection pgpServiceConnection;
357 private PgpEngine mPgpEngine = null;
358 private Intent pingIntent;
359 private PendingIntent pendingPingIntent = null;
360 private WakeLock wakeLock;
361
362 public PgpEngine getPgpEngine() {
363 if (pgpServiceConnection.isBound()) {
364 if (this.mPgpEngine == null) {
365 this.mPgpEngine = new PgpEngine(new OpenPgpApi(
366 getApplicationContext(),
367 pgpServiceConnection.getService()));
368 }
369 return mPgpEngine;
370 } else {
371 return null;
372 }
373
374 }
375
376 protected Conversation findMuc(String name, Account account) {
377 for (Conversation conversation : this.conversations) {
378 if (conversation.getContactJid().split("/")[0].equals(name)
379 && (conversation.getAccount() == account)) {
380 return conversation;
381 }
382 }
383 return null;
384 }
385
386 private void processRosterItems(Account account, Element elements) {
387 String version = elements.getAttribute("ver");
388 if (version != null) {
389 account.setRosterVersion(version);
390 databaseBackend.updateAccount(account);
391 }
392 for (Element item : elements.getChildren()) {
393 if (item.getName().equals("item")) {
394 String jid = item.getAttribute("jid");
395 String subscription = item.getAttribute("subscription");
396 Contact contact = databaseBackend.findContact(account, jid);
397 if (contact == null) {
398 if (!subscription.equals("remove")) {
399 String name = item.getAttribute("name");
400 if (name == null) {
401 name = jid.split("@")[0];
402 }
403 contact = new Contact(account, name, jid, null);
404 contact.parseSubscriptionFromElement(item);
405 databaseBackend.createContact(contact);
406 }
407 } else {
408 if (subscription.equals("remove")) {
409 databaseBackend.deleteContact(contact);
410 replaceContactInConversation(contact.getJid(), null);
411 } else {
412 contact.parseSubscriptionFromElement(item);
413 databaseBackend.updateContact(contact);
414 replaceContactInConversation(contact.getJid(), contact);
415 }
416 }
417 }
418 }
419 }
420
421 private void replaceContactInConversation(String jid, Contact contact) {
422 List<Conversation> conversations = getConversations();
423 for (int i = 0; i < conversations.size(); ++i) {
424 if ((conversations.get(i).getContactJid().equals(jid))) {
425 conversations.get(i).setContact(contact);
426 break;
427 }
428 }
429 }
430
431 public class XmppConnectionBinder extends Binder {
432 public XmppConnectionService getService() {
433 return XmppConnectionService.this;
434 }
435 }
436
437 @Override
438 public int onStartCommand(Intent intent, int flags, int startId) {
439 this.wakeLock.acquire();
440 // Log.d(LOGTAG,"calling start service. caller was:"+intent.getAction());
441 ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
442 .getSystemService(Context.CONNECTIVITY_SERVICE);
443 NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
444 boolean isConnected = activeNetwork != null
445 && activeNetwork.isConnected();
446
447 for (Account account : accounts) {
448 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
449 if (!isConnected) {
450 account.setStatus(Account.STATUS_NO_INTERNET);
451 if (statusListener != null) {
452 statusListener.onStatusChanged(account);
453 }
454 } else {
455 if (account.getStatus() == Account.STATUS_NO_INTERNET) {
456 account.setStatus(Account.STATUS_OFFLINE);
457 if (statusListener != null) {
458 statusListener.onStatusChanged(account);
459 }
460 }
461
462 // TODO 3 remaining cases
463 if (account.getStatus() == Account.STATUS_ONLINE) {
464 long lastReceived = account.getXmppConnection().lastPaketReceived;
465 long lastSent = account.getXmppConnection().lastPingSent;
466 if (lastSent - lastReceived >= PING_TIMEOUT * 1000) {
467 Log.d(LOGTAG, account.getJid() + ": ping timeout");
468 this.reconnectAccount(account, true);
469 } else if (SystemClock.elapsedRealtime() - lastReceived >= PING_MIN_INTERVAL * 1000) {
470 account.getXmppConnection().sendPing();
471 account.getXmppConnection().lastPingSent = SystemClock
472 .elapsedRealtime();
473 this.scheduleWakeupCall(2, false);
474 }
475 } else if (account.getStatus() == Account.STATUS_OFFLINE) {
476 if (account.getXmppConnection() == null) {
477 account.setXmppConnection(this
478 .createConnection(account));
479 }
480 account.getXmppConnection().lastPingSent = SystemClock
481 .elapsedRealtime();
482 new Thread(account.getXmppConnection()).start();
483 } else if ((account.getStatus() == Account.STATUS_CONNECTING)
484 && ((SystemClock.elapsedRealtime() - account
485 .getXmppConnection().lastConnect) / 1000 >= CONNECT_TIMEOUT)) {
486 Log.d(LOGTAG, account.getJid()
487 + ": time out during connect reconnecting");
488 reconnectAccount(account, true);
489 } else {
490 Log.d(LOGTAG,
491 "seconds since last connect:"
492 + ((SystemClock.elapsedRealtime() - account
493 .getXmppConnection().lastConnect) / 1000));
494 Log.d(LOGTAG,
495 account.getJid() + ": status="
496 + account.getStatus());
497 // TODO notify user of ssl cert problem or auth problem
498 // or what ever
499 }
500 // in any case. reschedule wakup call
501 this.scheduleWakeupCall(PING_MAX_INTERVAL, true);
502 }
503 if (accountChangedListener != null) {
504 accountChangedListener.onAccountListChangedListener();
505 }
506 }
507 }
508 if (wakeLock.isHeld()) {
509 wakeLock.release();
510 }
511 return START_STICKY;
512 }
513
514 @Override
515 public void onCreate() {
516 ExceptionHelper.init(getApplicationContext());
517 databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
518 this.accounts = databaseBackend.getAccounts();
519
520 this.getConversations();
521
522 getContentResolver().registerContentObserver(
523 ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
524 this.pgpServiceConnection = new OpenPgpServiceConnection(
525 getApplicationContext(), "org.sufficientlysecure.keychain");
526 this.pgpServiceConnection.bindToService();
527
528 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
529 this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
530 "XmppConnection");
531 }
532
533 @Override
534 public void onDestroy() {
535 super.onDestroy();
536 for (Account account : accounts) {
537 if (account.getXmppConnection() != null) {
538 disconnect(account, true);
539 }
540 }
541 }
542
543 protected void scheduleWakeupCall(int seconds, boolean ping) {
544 long timeToWake = SystemClock.elapsedRealtime() + seconds * 1000;
545 Context context = getApplicationContext();
546 AlarmManager alarmManager = (AlarmManager) context
547 .getSystemService(Context.ALARM_SERVICE);
548
549 if (ping) {
550 if (this.pingIntent == null) {
551 this.pingIntent = new Intent(context, EventReceiver.class);
552 this.pingIntent.setAction("ping");
553 this.pingIntent.putExtra("time", timeToWake);
554 this.pendingPingIntent = PendingIntent.getBroadcast(context, 0,
555 this.pingIntent, 0);
556 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
557 timeToWake, pendingPingIntent);
558 // Log.d(LOGTAG,"schedule ping in "+seconds+" seconds");
559 } else {
560 long scheduledTime = this.pingIntent.getLongExtra("time", 0);
561 if (scheduledTime < SystemClock.elapsedRealtime()
562 || (scheduledTime > timeToWake)) {
563 this.pingIntent.putExtra("time", timeToWake);
564 alarmManager.cancel(this.pendingPingIntent);
565 this.pendingPingIntent = PendingIntent.getBroadcast(
566 context, 0, this.pingIntent, 0);
567 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
568 timeToWake, pendingPingIntent);
569 // Log.d(LOGTAG,"reschedule old ping to ping in "+seconds+" seconds");
570 }
571 }
572 } else {
573 Intent intent = new Intent(context, EventReceiver.class);
574 intent.setAction("ping_check");
575 PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0,
576 intent, 0);
577 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake,
578 alarmIntent);
579 }
580
581 }
582
583 public XmppConnection createConnection(Account account) {
584 SharedPreferences sharedPref = PreferenceManager
585 .getDefaultSharedPreferences(getApplicationContext());
586 account.setResource(sharedPref.getString("resource", "mobile").toLowerCase(Locale.getDefault()));
587 XmppConnection connection = new XmppConnection(account, this.wakeLock);
588 connection.setOnMessagePacketReceivedListener(this.messageListener);
589 connection.setOnStatusChangedListener(this.statusListener);
590 connection.setOnPresencePacketReceivedListener(this.presenceListener);
591 connection
592 .setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener);
593 connection.setOnJinglePacketReceivedListener(this.jingleListener);
594 connection
595 .setOnTLSExceptionReceivedListener(new OnTLSExceptionReceived() {
596
597 @Override
598 public void onTLSExceptionReceived(String fingerprint,
599 Account account) {
600 Log.d(LOGTAG, "tls exception arrived in service");
601 if (tlsException != null) {
602 tlsException.onTLSExceptionReceived(fingerprint,
603 account);
604 }
605 }
606 });
607 connection.setOnBindListener(new OnBindListener() {
608
609 @Override
610 public void onBind(Account account) {
611 databaseBackend.clearPresences(account);
612 account.clearPresences(); // self presences
613 if (account.getXmppConnection().hasFeatureRosterManagment()) {
614 updateRoster(account, null);
615 }
616 connectMultiModeConversations(account);
617 if (convChangedListener != null) {
618 convChangedListener.onConversationListChanged();
619 }
620 }
621 });
622 return connection;
623 }
624
625 synchronized public void sendMessage(Message message, String presence) {
626 Account account = message.getConversation().getAccount();
627 Conversation conv = message.getConversation();
628 boolean saveInDb = false;
629 boolean addToConversation = false;
630 if (account.getStatus() == Account.STATUS_ONLINE) {
631 MessagePacket packet;
632 if (message.getEncryption() == Message.ENCRYPTION_OTR) {
633 if (!conv.hasValidOtrSession()) {
634 // starting otr session. messages will be send later
635 conv.startOtrSession(getApplicationContext(), presence,true);
636 } else if (conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
637 // otr session aleary exists, creating message packet
638 // accordingly
639 packet = prepareMessagePacket(account, message,
640 conv.getOtrSession());
641 account.getXmppConnection().sendMessagePacket(packet);
642 message.setStatus(Message.STATUS_SEND);
643 }
644 saveInDb = true;
645 addToConversation = true;
646 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
647 message.getConversation().endOtrIfNeeded();
648 long keyId = message.getConversation().getContact()
649 .getPgpKeyId();
650 packet = new MessagePacket();
651 packet.setType(MessagePacket.TYPE_CHAT);
652 packet.setFrom(message.getConversation().getAccount()
653 .getFullJid());
654 packet.setTo(message.getCounterpart());
655 packet.setBody("This is an XEP-0027 encryted message");
656 packet.addChild("x","jabber:x:encrypted").setContent(this.getPgpEngine().encrypt(keyId,
657 message.getBody()));
658 account.getXmppConnection().sendMessagePacket(packet);
659 message.setStatus(Message.STATUS_SEND);
660 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
661 saveInDb = true;
662 addToConversation = true;
663 } else {
664 message.getConversation().endOtrIfNeeded();
665 // don't encrypt
666 if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
667 message.setStatus(Message.STATUS_SEND);
668 saveInDb = true;
669 addToConversation = true;
670 }
671
672 packet = prepareMessagePacket(account, message, null);
673 account.getXmppConnection().sendMessagePacket(packet);
674 }
675 } else {
676 // account is offline
677 saveInDb = true;
678 addToConversation = true;
679
680 }
681 if (saveInDb) {
682 databaseBackend.createMessage(message);
683 }
684 if (addToConversation) {
685 conv.getMessages().add(message);
686 if (convChangedListener != null) {
687 convChangedListener.onConversationListChanged();
688 }
689 }
690
691 }
692
693 private void sendUnsendMessages(Conversation conversation) {
694 for (int i = 0; i < conversation.getMessages().size(); ++i) {
695 if ((conversation.getMessages().get(i).getStatus() == Message.STATUS_UNSEND)&&(conversation.getMessages().get(i).getEncryption() == Message.ENCRYPTION_NONE)) {
696 Message message = conversation.getMessages().get(i);
697 MessagePacket packet = prepareMessagePacket(
698 conversation.getAccount(), message, null);
699 conversation.getAccount().getXmppConnection()
700 .sendMessagePacket(packet);
701 message.setStatus(Message.STATUS_SEND);
702 if (conversation.getMode() == Conversation.MODE_SINGLE) {
703 databaseBackend.updateMessage(message);
704 } else {
705 databaseBackend.deleteMessage(message);
706 conversation.getMessages().remove(i);
707 i--;
708 }
709 }
710 }
711 }
712
713 public MessagePacket prepareMessagePacket(Account account, Message message,
714 Session otrSession) {
715 MessagePacket packet = new MessagePacket();
716 if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
717 packet.setType(MessagePacket.TYPE_CHAT);
718 if (otrSession != null) {
719 try {
720 packet.setBody(otrSession.transformSending(message
721 .getBody()));
722 } catch (OtrException e) {
723 Log.d(LOGTAG,
724 account.getJid()
725 + ": could not encrypt message to "
726 + message.getCounterpart());
727 }
728 packet.addChild("private","urn:xmpp:carbons:2");
729 packet.addChild("no-copy","urn:xmpp:hints");
730 packet.setTo(otrSession.getSessionID().getAccountID() + "/"
731 + otrSession.getSessionID().getUserID());
732 packet.setFrom(account.getFullJid());
733 } else {
734 packet.setBody(message.getBody());
735 packet.setTo(message.getCounterpart());
736 packet.setFrom(account.getJid());
737 }
738 } else if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
739 packet.setType(MessagePacket.TYPE_GROUPCHAT);
740 packet.setBody(message.getBody());
741 packet.setTo(message.getCounterpart().split("/")[0]);
742 packet.setFrom(account.getJid());
743 }
744 return packet;
745 }
746
747 private void getRoster(Account account,
748 final OnRosterFetchedListener listener) {
749 List<Contact> contacts = databaseBackend.getContactsByAccount(account);
750 for (int i = 0; i < contacts.size(); ++i) {
751 contacts.get(i).setAccount(account);
752 }
753 if (listener != null) {
754 listener.onRosterFetched(contacts);
755 }
756 }
757
758 public List<Contact> getRoster(Account account) {
759 List<Contact> contacts = databaseBackend.getContactsByAccount(account);
760 for (int i = 0; i < contacts.size(); ++i) {
761 contacts.get(i).setAccount(account);
762 }
763 return contacts;
764 }
765
766 public void updateRoster(final Account account,
767 final OnRosterFetchedListener listener) {
768 IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET);
769 if (!"".equals(account.getRosterVersion())) {
770 Log.d(LOGTAG, account.getJid() + ": fetching roster version "
771 + account.getRosterVersion());
772 } else {
773 Log.d(LOGTAG, account.getJid() + ": fetching roster");
774 }
775 iqPacket.query("jabber:iq:roster").setAttribute("ver", account.getRosterVersion());
776 account.getXmppConnection().sendIqPacket(iqPacket,
777 new OnIqPacketReceived() {
778
779 @Override
780 public void onIqPacketReceived(final Account account,
781 IqPacket packet) {
782 Element roster = packet.findChild("query");
783 if (roster != null) {
784 Log.d(LOGTAG, account.getJid()
785 + ": processing roster");
786 processRosterItems(account, roster);
787 StringBuilder mWhere = new StringBuilder();
788 mWhere.append("jid NOT IN(");
789 List<Element> items = roster.getChildren();
790 for (int i = 0; i < items.size(); ++i) {
791 mWhere.append(DatabaseUtils
792 .sqlEscapeString(items.get(i)
793 .getAttribute("jid")));
794 if (i != items.size() - 1) {
795 mWhere.append(",");
796 }
797 }
798 mWhere.append(") and accountUuid = \"");
799 mWhere.append(account.getUuid());
800 mWhere.append("\"");
801 List<Contact> contactsToDelete = databaseBackend
802 .getContacts(mWhere.toString());
803 for (Contact contact : contactsToDelete) {
804 databaseBackend.deleteContact(contact);
805 replaceContactInConversation(contact.getJid(),
806 null);
807 }
808
809 } else {
810 Log.d(LOGTAG, account.getJid()
811 + ": empty roster returend");
812 }
813 mergePhoneContactsWithRoster(new OnPhoneContactsMerged() {
814
815 @Override
816 public void phoneContactsMerged() {
817 if (listener != null) {
818 getRoster(account, listener);
819 }
820 }
821 });
822 }
823 });
824 }
825
826 public void mergePhoneContactsWithRoster(
827 final OnPhoneContactsMerged listener) {
828 PhoneHelper.loadPhoneContacts(getApplicationContext(),
829 new OnPhoneContactsLoadedListener() {
830 @Override
831 public void onPhoneContactsLoaded(
832 Hashtable<String, Bundle> phoneContacts) {
833 List<Contact> contacts = databaseBackend
834 .getContactsByAccount(null);
835 for (int i = 0; i < contacts.size(); ++i) {
836 Contact contact = contacts.get(i);
837 if (phoneContacts.containsKey(contact.getJid())) {
838 Bundle phoneContact = phoneContacts.get(contact
839 .getJid());
840 String systemAccount = phoneContact
841 .getInt("phoneid")
842 + "#"
843 + phoneContact.getString("lookup");
844 contact.setSystemAccount(systemAccount);
845 contact.setPhotoUri(phoneContact
846 .getString("photouri"));
847 contact.setDisplayName(phoneContact
848 .getString("displayname"));
849 databaseBackend.updateContact(contact);
850 replaceContactInConversation(contact.getJid(),
851 contact);
852 } else {
853 if ((contact.getSystemAccount() != null)
854 || (contact.getProfilePhoto() != null)) {
855 contact.setSystemAccount(null);
856 contact.setPhotoUri(null);
857 databaseBackend.updateContact(contact);
858 replaceContactInConversation(
859 contact.getJid(), contact);
860 }
861 }
862 }
863 if (listener != null) {
864 listener.phoneContactsMerged();
865 }
866 }
867 });
868 }
869
870 public List<Conversation> getConversations() {
871 if (this.conversations == null) {
872 Hashtable<String, Account> accountLookupTable = new Hashtable<String, Account>();
873 for (Account account : this.accounts) {
874 accountLookupTable.put(account.getUuid(), account);
875 }
876 this.conversations = databaseBackend
877 .getConversations(Conversation.STATUS_AVAILABLE);
878 for (Conversation conv : this.conversations) {
879 Account account = accountLookupTable.get(conv.getAccountUuid());
880 conv.setAccount(account);
881 conv.setContact(findContact(account, conv.getContactJid()));
882 conv.setMessages(databaseBackend.getMessages(conv, 50));
883 }
884 }
885 Collections.sort(this.conversations, new Comparator<Conversation>() {
886 @Override
887 public int compare(Conversation lhs, Conversation rhs) {
888 return (int) (rhs.getLatestMessage().getTimeSent() - lhs
889 .getLatestMessage().getTimeSent());
890 }
891 });
892 return this.conversations;
893 }
894
895 public List<Account> getAccounts() {
896 return this.accounts;
897 }
898
899 public Contact findContact(Account account, String jid) {
900 Contact contact = databaseBackend.findContact(account, jid);
901 if (contact != null) {
902 contact.setAccount(account);
903 }
904 return contact;
905 }
906
907 public Conversation findOrCreateConversation(Account account, String jid,
908 boolean muc) {
909 for (Conversation conv : this.getConversations()) {
910 if ((conv.getAccount().equals(account))
911 && (conv.getContactJid().split("/")[0].equals(jid))) {
912 return conv;
913 }
914 }
915 Conversation conversation = databaseBackend.findConversation(account,
916 jid);
917 if (conversation != null) {
918 conversation.setStatus(Conversation.STATUS_AVAILABLE);
919 conversation.setAccount(account);
920 if (muc) {
921 conversation.setMode(Conversation.MODE_MULTI);
922 } else {
923 conversation.setMode(Conversation.MODE_SINGLE);
924 }
925 conversation.setMessages(databaseBackend.getMessages(conversation,
926 50));
927 this.databaseBackend.updateConversation(conversation);
928 conversation.setContact(findContact(account,
929 conversation.getContactJid()));
930 } else {
931 String conversationName;
932 Contact contact = findContact(account, jid);
933 if (contact != null) {
934 conversationName = contact.getDisplayName();
935 } else {
936 conversationName = jid.split("@")[0];
937 }
938 if (muc) {
939 conversation = new Conversation(conversationName, account, jid,
940 Conversation.MODE_MULTI);
941 } else {
942 conversation = new Conversation(conversationName, account, jid,
943 Conversation.MODE_SINGLE);
944 }
945 conversation.setContact(contact);
946 this.databaseBackend.createConversation(conversation);
947 }
948 this.conversations.add(conversation);
949 if ((account.getStatus() == Account.STATUS_ONLINE)&&(conversation.getMode() == Conversation.MODE_MULTI)) {
950 joinMuc(conversation);
951 }
952 if (this.convChangedListener != null) {
953 this.convChangedListener.onConversationListChanged();
954 }
955 return conversation;
956 }
957
958 public void archiveConversation(Conversation conversation) {
959 if (conversation.getMode() == Conversation.MODE_MULTI) {
960 leaveMuc(conversation);
961 } else {
962 conversation.endOtrIfNeeded();
963 }
964 this.databaseBackend.updateConversation(conversation);
965 this.conversations.remove(conversation);
966 if (this.convChangedListener != null) {
967 this.convChangedListener.onConversationListChanged();
968 }
969 }
970
971 public int getConversationCount() {
972 return this.databaseBackend.getConversationCount();
973 }
974
975 public void createAccount(Account account) {
976 databaseBackend.createAccount(account);
977 this.accounts.add(account);
978 this.reconnectAccount(account, false);
979 if (accountChangedListener != null)
980 accountChangedListener.onAccountListChangedListener();
981 }
982
983 public void deleteContact(Contact contact) {
984 IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
985 Element query = iq.query("jabber:iq:roster");
986 query.addChild("item").setAttribute("jid", contact.getJid()).setAttribute("subscription", "remove");
987 contact.getAccount().getXmppConnection().sendIqPacket(iq, null);
988 replaceContactInConversation(contact.getJid(), null);
989 databaseBackend.deleteContact(contact);
990 }
991
992 public void updateAccount(Account account) {
993 databaseBackend.updateAccount(account);
994 reconnectAccount(account, false);
995 if (accountChangedListener != null)
996 accountChangedListener.onAccountListChangedListener();
997 }
998
999 public void deleteAccount(Account account) {
1000 if (account.getXmppConnection() != null) {
1001 this.disconnect(account, true);
1002 }
1003 databaseBackend.deleteAccount(account);
1004 this.accounts.remove(account);
1005 if (accountChangedListener != null)
1006 accountChangedListener.onAccountListChangedListener();
1007 }
1008
1009 public void setOnConversationListChangedListener(
1010 OnConversationListChangedListener listener) {
1011 this.convChangedListener = listener;
1012 }
1013
1014 public void removeOnConversationListChangedListener() {
1015 this.convChangedListener = null;
1016 }
1017
1018 public void setOnAccountListChangedListener(
1019 OnAccountListChangedListener listener) {
1020 this.accountChangedListener = listener;
1021 }
1022
1023 public void removeOnAccountListChangedListener() {
1024 this.accountChangedListener = null;
1025 }
1026
1027 public void connectMultiModeConversations(Account account) {
1028 List<Conversation> conversations = getConversations();
1029 for (int i = 0; i < conversations.size(); i++) {
1030 Conversation conversation = conversations.get(i);
1031 if ((conversation.getMode() == Conversation.MODE_MULTI)
1032 && (conversation.getAccount() == account)) {
1033 joinMuc(conversation);
1034 }
1035 }
1036 }
1037
1038 public void joinMuc(Conversation conversation) {
1039 String[] mucParts = conversation.getContactJid().split("/");
1040 String muc;
1041 String nick;
1042 if (mucParts.length == 2) {
1043 muc = mucParts[0];
1044 nick = mucParts[1];
1045 } else {
1046 muc = mucParts[0];
1047 nick = conversation.getAccount().getUsername();
1048 }
1049 PresencePacket packet = new PresencePacket();
1050 packet.setAttribute("to", muc + "/" + nick);
1051 Element x = new Element("x");
1052 x.setAttribute("xmlns", "http://jabber.org/protocol/muc");
1053 if (conversation.getMessages().size() != 0) {
1054 long lastMsgTime = conversation.getLatestMessage().getTimeSent();
1055 long diff = (System.currentTimeMillis() - lastMsgTime) / 1000 - 1;
1056 x.addChild("history").setAttribute("seconds", diff + "");
1057 }
1058 packet.addChild(x);
1059 Log.d(LOGTAG,conversation.getAccount().getJid()+": joining muc "+packet.toString());
1060 conversation.getAccount().getXmppConnection()
1061 .sendPresencePacket(packet);
1062 }
1063
1064 private OnRenameListener renameListener = null;
1065
1066 public void setOnRenameListener(OnRenameListener listener) {
1067 this.renameListener = listener;
1068 }
1069
1070 public void renameInMuc(final Conversation conversation, final String nick) {
1071 final MucOptions options = conversation.getMucOptions();
1072 if (options.online()) {
1073 options.setOnRenameListener(new OnRenameListener() {
1074
1075 @Override
1076 public void onRename(boolean success) {
1077 if (renameListener != null) {
1078 renameListener.onRename(success);
1079 }
1080 if (success) {
1081 databaseBackend.updateConversation(conversation);
1082 }
1083 }
1084 });
1085 options.flagAboutToRename();
1086 PresencePacket packet = new PresencePacket();
1087 packet.setAttribute("to",
1088 conversation.getContactJid().split("/")[0] + "/" + nick);
1089 packet.setAttribute("from", conversation.getAccount().getFullJid());
1090
1091 conversation.getAccount().getXmppConnection()
1092 .sendPresencePacket(packet, null);
1093 } else {
1094 String jid = conversation.getContactJid().split("/")[0] + "/"
1095 + nick;
1096 conversation.setContactJid(jid);
1097 databaseBackend.updateConversation(conversation);
1098 if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) {
1099 joinMuc(conversation);
1100 }
1101 }
1102 }
1103
1104 public void leaveMuc(Conversation conversation) {
1105 PresencePacket packet = new PresencePacket();
1106 packet.setAttribute("to", conversation.getContactJid().split("/")[0] + "/" + conversation.getMucOptions().getNick());
1107 packet.setAttribute("from", conversation.getAccount().getFullJid());
1108 packet.setAttribute("type", "unavailable");
1109 Log.d(LOGTAG,"send leaving muc " + packet);
1110 conversation.getAccount().getXmppConnection()
1111 .sendPresencePacket(packet);
1112 conversation.getMucOptions().setOffline();
1113 }
1114
1115 public void disconnect(Account account, boolean force) {
1116 if ((account.getStatus() == Account.STATUS_ONLINE)
1117 || (account.getStatus() == Account.STATUS_DISABLED)) {
1118 if (!force) {
1119 List<Conversation> conversations = getConversations();
1120 for (int i = 0; i < conversations.size(); i++) {
1121 Conversation conversation = conversations.get(i);
1122 if (conversation.getAccount() == account) {
1123 if (conversation.getMode() == Conversation.MODE_MULTI) {
1124 leaveMuc(conversation);
1125 } else {
1126 conversation.endOtrIfNeeded();
1127 }
1128 }
1129 }
1130 }
1131 account.getXmppConnection().disconnect(force);
1132 }
1133 }
1134
1135 @Override
1136 public IBinder onBind(Intent intent) {
1137 return mBinder;
1138 }
1139
1140 public void updateContact(Contact contact) {
1141 databaseBackend.updateContact(contact);
1142 replaceContactInConversation(contact.getJid(), contact);
1143 }
1144
1145 public void updateMessage(Message message) {
1146 databaseBackend.updateMessage(message);
1147 }
1148
1149 public void createContact(Contact contact) {
1150 SharedPreferences sharedPref = PreferenceManager
1151 .getDefaultSharedPreferences(getApplicationContext());
1152 boolean autoGrant = sharedPref.getBoolean("grant_new_contacts", true);
1153 if (autoGrant) {
1154 contact.setSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
1155 contact.setSubscriptionOption(Contact.Subscription.ASKING);
1156 }
1157 databaseBackend.createContact(contact);
1158 IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
1159 Element query = new Element("query");
1160 query.setAttribute("xmlns", "jabber:iq:roster");
1161 Element item = new Element("item");
1162 item.setAttribute("jid", contact.getJid());
1163 item.setAttribute("name", contact.getJid());
1164 query.addChild(item);
1165 iq.addChild(query);
1166 Account account = contact.getAccount();
1167 account.getXmppConnection().sendIqPacket(iq, null);
1168 if (autoGrant) {
1169 requestPresenceUpdatesFrom(contact);
1170 }
1171 replaceContactInConversation(contact.getJid(), contact);
1172 }
1173
1174 public void requestPresenceUpdatesFrom(Contact contact) {
1175 // Requesting a Subscription type=subscribe
1176 PresencePacket packet = new PresencePacket();
1177 packet.setAttribute("type", "subscribe");
1178 packet.setAttribute("to", contact.getJid());
1179 packet.setAttribute("from", contact.getAccount().getJid());
1180 Log.d(LOGTAG, packet.toString());
1181 contact.getAccount().getXmppConnection().sendPresencePacket(packet);
1182 }
1183
1184 public void stopPresenceUpdatesFrom(Contact contact) {
1185 // Unsubscribing type='unsubscribe'
1186 PresencePacket packet = new PresencePacket();
1187 packet.setAttribute("type", "unsubscribe");
1188 packet.setAttribute("to", contact.getJid());
1189 packet.setAttribute("from", contact.getAccount().getJid());
1190 Log.d(LOGTAG, packet.toString());
1191 contact.getAccount().getXmppConnection().sendPresencePacket(packet);
1192 }
1193
1194 public void stopPresenceUpdatesTo(Contact contact) {
1195 // Canceling a Subscription type=unsubscribed
1196 PresencePacket packet = new PresencePacket();
1197 packet.setAttribute("type", "unsubscribed");
1198 packet.setAttribute("to", contact.getJid());
1199 packet.setAttribute("from", contact.getAccount().getJid());
1200 Log.d(LOGTAG, packet.toString());
1201 contact.getAccount().getXmppConnection().sendPresencePacket(packet);
1202 }
1203
1204 public void sendPresenceUpdatesTo(Contact contact) {
1205 // type='subscribed'
1206 PresencePacket packet = new PresencePacket();
1207 packet.setAttribute("type", "subscribed");
1208 packet.setAttribute("to", contact.getJid());
1209 packet.setAttribute("from", contact.getAccount().getJid());
1210 Log.d(LOGTAG, packet.toString());
1211 contact.getAccount().getXmppConnection().sendPresencePacket(packet);
1212 }
1213
1214 public void sendPgpPresence(Account account, String signature) {
1215 PresencePacket packet = new PresencePacket();
1216 packet.setAttribute("from", account.getFullJid());
1217 Element status = new Element("status");
1218 status.setContent("online");
1219 packet.addChild(status);
1220 Element x = new Element("x");
1221 x.setAttribute("xmlns", "jabber:x:signed");
1222 x.setContent(signature);
1223 packet.addChild(x);
1224 account.getXmppConnection().sendPresencePacket(packet);
1225 }
1226
1227 public void generatePgpAnnouncement(Account account)
1228 throws PgpEngine.UserInputRequiredException {
1229 if (account.getStatus() == Account.STATUS_ONLINE) {
1230 String signature = getPgpEngine().generateSignature("online");
1231 account.setKey("pgp_signature", signature);
1232 databaseBackend.updateAccount(account);
1233 sendPgpPresence(account, signature);
1234 }
1235 }
1236
1237 public void updateConversation(Conversation conversation) {
1238 this.databaseBackend.updateConversation(conversation);
1239 }
1240
1241 public Contact findContact(String uuid) {
1242 Contact contact = this.databaseBackend.getContact(uuid);
1243 for (Account account : getAccounts()) {
1244 if (contact.getAccountUuid().equals(account.getUuid())) {
1245 contact.setAccount(account);
1246 }
1247 }
1248 return contact;
1249 }
1250
1251 public void removeOnTLSExceptionReceivedListener() {
1252 this.tlsException = null;
1253 }
1254
1255 // TODO dont let thread sleep but schedule wake up
1256 public void reconnectAccount(final Account account, final boolean force) {
1257 new Thread(new Runnable() {
1258
1259 @Override
1260 public void run() {
1261 if (account.getXmppConnection() != null) {
1262 disconnect(account, force);
1263 }
1264 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
1265 if (account.getXmppConnection() == null) {
1266 account.setXmppConnection(createConnection(account));
1267 }
1268 Thread thread = new Thread(account.getXmppConnection());
1269 thread.start();
1270 scheduleWakeupCall((int) (CONNECT_TIMEOUT * 1.2), false);
1271 }
1272 }
1273 }).start();
1274 }
1275
1276 public void updateConversationInGui() {
1277 if (convChangedListener != null) {
1278 convChangedListener.onConversationListChanged();
1279 }
1280 }
1281
1282 public void sendConversationSubject(Conversation conversation,
1283 String subject) {
1284 MessagePacket packet = new MessagePacket();
1285 packet.setType(MessagePacket.TYPE_GROUPCHAT);
1286 packet.setTo(conversation.getContactJid().split("/")[0]);
1287 Element subjectChild = new Element("subject");
1288 subjectChild.setContent(subject);
1289 packet.addChild(subjectChild);
1290 packet.setFrom(conversation.getAccount().getJid());
1291 Account account = conversation.getAccount();
1292 if (account.getStatus() == Account.STATUS_ONLINE) {
1293 account.getXmppConnection().sendMessagePacket(packet);
1294 }
1295 }
1296
1297 public void inviteToConference(Conversation conversation,
1298 List<Contact> contacts) {
1299 for (Contact contact : contacts) {
1300 MessagePacket packet = new MessagePacket();
1301 packet.setTo(conversation.getContactJid().split("/")[0]);
1302 packet.setFrom(conversation.getAccount().getFullJid());
1303 Element x = new Element("x");
1304 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
1305 Element invite = new Element("invite");
1306 invite.setAttribute("to", contact.getJid());
1307 x.addChild(invite);
1308 packet.addChild(x);
1309 Log.d(LOGTAG, packet.toString());
1310 conversation.getAccount().getXmppConnection()
1311 .sendMessagePacket(packet);
1312 }
1313
1314 }
1315}