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