/
CoreConnService.java
849 lines (744 loc) · 25.7 KB
/
CoreConnService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/**
QuasselDroid - Quassel client for Android
Copyright (C) 2011 Ken Børge Viktil
Copyright (C) 2011 Magnus Fjell
Copyright (C) 2011 Martin Sandsmark <martin.sandsmark@kde.org>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version, or under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License and the
GNU Lesser General Public License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
package com.iskrembilen.quasseldroid.service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Typeface;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.ResultReceiver;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import com.iskrembilen.quasseldroid.Buffer;
import com.iskrembilen.quasseldroid.BufferInfo;
import com.iskrembilen.quasseldroid.IrcMessage;
import com.iskrembilen.quasseldroid.IrcUser;
import com.iskrembilen.quasseldroid.Network;
import com.iskrembilen.quasseldroid.NetworkCollection;
import com.iskrembilen.quasseldroid.QuasseldroidNotificationManager;
import com.iskrembilen.quasseldroid.R;
import com.iskrembilen.quasseldroid.IrcMessage.Flag;
import com.iskrembilen.quasseldroid.io.CoreConnection;
/**
* This Service holds the connection to the core from the phone, it handles all
* the communication with the core. It talks to CoreConnection
*/
public class CoreConnService extends Service {
private static final String TAG = CoreConnService.class.getSimpleName();
/**
* Id for result code in the resultReciver that is going to notify the
* activity currently on screen about the change
*/
public static final int CONNECTION_DISCONNECTED = 0;
public static final int CONNECTION_CONNECTED = 1;
public static final int NEW_CERTIFICATE = 2;
public static final int UNSUPPORTED_PROTOCOL = 3;
public static final int INIT_PROGRESS = 4;
public static final int INIT_DONE = 5;
public static final int CONNECTION_CONNECTING = 6;
public static final String STATUS_KEY = "status";
public static final String CERT_KEY = "certificate";
public static final String PROGRESS_KEY = "networkname";
private Pattern URLPattern = Pattern.compile("((mailto\\:|(news|(ht|f)tp(s?))\\://){1}\\S+)", Pattern.CASE_INSENSITIVE);
private CoreConnection coreConn;
private final IBinder binder = new LocalBinder();
Handler incomingHandler;
ArrayList<ResultReceiver> statusReceivers;
SharedPreferences preferences;
private NetworkCollection networks;
private QuasseldroidNotificationManager notificationManager;
private boolean preferenceParseColors;
private OnSharedPreferenceChangeListener preferenceListener;
/**
* Class for clients to access. Because we know this service always runs in
* the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public CoreConnService getService() {
return CoreConnService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public void onHighlightsRead(int bufferId) {
notificationManager.notifyHighlightsRead(bufferId);
}
@Override
public void onCreate() {
super.onCreate();
incomingHandler = new IncomingHandler();
notificationManager = new QuasseldroidNotificationManager(this);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferenceParseColors = preferences.getBoolean(getString(R.string.preference_colored_text), false);
preferenceListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(getString(R.string.preference_colored_text))) {
preferenceParseColors = preferences.getBoolean(getString(R.string.preference_colored_text), false);
}
}
};
preferences.registerOnSharedPreferenceChangeListener(preferenceListener);
statusReceivers = new ArrayList<ResultReceiver>();
}
@Override
public void onDestroy() {
this.disconnectFromCore();
}
public Handler getHandler() {
return incomingHandler;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
handleIntent(intent);
}
return START_STICKY;
}
/**
* Handle the data in the intent, and use it to connect with CoreConnect
*
* @param intent
*/
private void handleIntent(Intent intent) {
if (coreConn != null) {
this.disconnectFromCore();
}
Bundle connectData = intent.getExtras();
String address = connectData.getString("address");
int port = connectData.getInt("port");
String username = connectData.getString("username");
String password = connectData.getString("password");
Boolean ssl = connectData.getBoolean("ssl");
Log.i(TAG, "Connecting to core: " + address + ":" + port
+ " with username " + username);
networks = new NetworkCollection();
coreConn = new CoreConnection(address, port, username, password, ssl,
this);
}
public void sendMessage(int bufferId, String message) {
coreConn.sendMessage(bufferId, message);
}
public void markBufferAsRead(int bufferId) {
coreConn.requestMarkBufferAsRead(bufferId);
}
public void setLastSeen(int bufferId, int msgId) {
notificationManager.notifyHighlightsRead(bufferId);
coreConn.requestSetLastMsgRead(bufferId, msgId);
networks.getBufferById(bufferId).setLastSeenMessage(msgId);
}
public void setMarkerLine(int bufferId, int msgId) {
coreConn.requestSetMarkerLine(bufferId, msgId);
networks.getBufferById(bufferId).setMarkerLineMessage(msgId);
}
public void unhideTempHiddenBuffer(int bufferId) {
coreConn.requestUnhideTempHiddenBuffer(bufferId);
networks.getBufferById(bufferId).setTemporarilyHidden(false);
}
public Buffer getBuffer(int bufferId, Observer obs) {
Buffer buffer = networks.getBufferById(bufferId);
if(obs != null && buffer != null)
buffer.addObserver(obs);
return buffer;
}
public void getMoreBacklog(int bufferId, int amount){
Log.d(TAG, "Fetching more backlog");
coreConn.requestMoreBacklog(bufferId, amount);
}
public NetworkCollection getNetworkList(Observer obs) {
return networks;
}
/**
* Checks if there is a highlight in the message and then sets the flag of
* that message to highlight
*
* @param buffer
* the buffer the message belongs to
* @param message
* the message to check
*/
public void checkMessageForHighlight(Buffer buffer, IrcMessage message) {
if (message.type == IrcMessage.Type.Plain) {
String nick = networks.getNetworkById(buffer.getInfo().networkId).getNick();
if(nick == null) {
Log.e(TAG, "Nick is null in check message for highlight");
return;
} else if(nick.equals("")) return;
Pattern regexHighlight = Pattern.compile(".*(?<!(\\w|\\d))" + Pattern.quote(networks.getNetworkById(buffer.getInfo().networkId).getNick()) + "(?!(\\w|\\d)).*", Pattern.CASE_INSENSITIVE);
Matcher matcher = regexHighlight.matcher(message.content);
if (matcher.find()) {
message.setFlag(IrcMessage.Flag.Highlight);
// FIXME: move to somewhere proper
}
}
}
/**
* Check if a message contains a URL that we need to support to open in a
* browser Set the url fields in the message so we can get it later
*
* @param message
* ircmessage to check
*/
public void checkForURL(IrcMessage message) {
Matcher matcher = URLPattern.matcher(message.content);
if (matcher.find()) {
message.addURL(this, matcher.group(0));
}
}
/**
* Parse mIRC style codes in IrcMessage
*/
public void parseStyleCodes(IrcMessage message) {
if(!preferenceParseColors) return;
final char boldIndicator = 2;
final char normalIndicator = 15;
final char italicIndicator = 29;
final char underlineIndicator = 31;
final char colorIndicator = 3;
String content = message.content.toString();
if (content.indexOf(boldIndicator) == -1
&& content.indexOf(italicIndicator) == -1
&& content.indexOf(underlineIndicator) == -1
&& content.indexOf(colorIndicator) == -1)
return;
SpannableStringBuilder newString = new SpannableStringBuilder(message.content);
int start, end, endSearchOffset, startIndicatorLength, style, fg, bg;
while (true) {
content = newString.toString();
end = -1;
startIndicatorLength = 1;
style = 0;
fg = -1;
bg = -1;
start = content.indexOf(boldIndicator);
if (start != -1) {
end = content.indexOf(boldIndicator, start+1);
style = Typeface.BOLD;
}
if (start == -1) {
start = content.indexOf(italicIndicator);
if (start != -1) {
end = content.indexOf(italicIndicator, start+1);
style = Typeface.ITALIC;
}
}
if (start == -1) {
start = content.indexOf(underlineIndicator);
if (start != -1) {
end = content.indexOf(underlineIndicator, start+1);
style = -1;
}
}
endSearchOffset = start + 1;
// Colors?
if (start == -1) {
start = content.indexOf(colorIndicator);
if (start != -1) {
// Note that specifying colour codes here is optional, as the same indicator will cancel existing colours
endSearchOffset = start + 1;
if (endSearchOffset < content.length()) {
if (Character.isDigit(content.charAt(endSearchOffset))) {
if (endSearchOffset+1 < content.length() && Character.isDigit(content.charAt(endSearchOffset + 1))) {
fg = Integer.parseInt(content.substring(endSearchOffset, endSearchOffset + 2));
endSearchOffset += 2;
} else {
fg = Integer.parseInt(content.substring(endSearchOffset, endSearchOffset+1));
endSearchOffset += 1;
}
if (endSearchOffset < content.length() && content.charAt(endSearchOffset) == ',') {
if (endSearchOffset+1 < content.length() && Character.isDigit(content.charAt(endSearchOffset+1))) {
endSearchOffset++;
if (endSearchOffset+1 < content.length() && Character.isDigit(content.charAt(endSearchOffset + 1))) {
bg = Integer.parseInt(content.substring(endSearchOffset, endSearchOffset + 2));
endSearchOffset += 2;
} else {
bg = Integer.parseInt(content.substring(endSearchOffset, endSearchOffset + 1));
endSearchOffset += 1;
}
}
}
}
}
startIndicatorLength = endSearchOffset - start;
end = content.indexOf(colorIndicator, endSearchOffset);
}
}
if (start == -1)
break;
int norm = content.indexOf(normalIndicator, start+1);
if (norm != -1 && (end == -1 || norm < end))
end = norm;
if (end == -1)
end = content.length();
if (end - (start + startIndicatorLength) > 0) {
// Only set spans if there's any text between start & end
if (style == -1) {
newString.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
} else {
newString.setSpan(new StyleSpan(style), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
if (fg != -1) {
newString.setSpan(new ForegroundColorSpan(getResources()
.getColor(mircCodeToColor(fg))), start, end,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
if (bg != -1) {
newString.setSpan(new BackgroundColorSpan(getResources()
.getColor(mircCodeToColor(bg))), start, end,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
// Intentionally don't remove "normal" indicators or color here, as they are multi-purpose
if (end < content.length() && (content.charAt(end) == boldIndicator
|| content.charAt(end) == italicIndicator
|| content.charAt(end) == underlineIndicator))
newString.delete(end, end+1);
newString.delete(start, start + startIndicatorLength);
}
// NOW we remove the "normal" and color indicator
while (true) {
content = newString.toString();
int normPos = content.indexOf(normalIndicator);
if (normPos != -1)
newString.delete(normPos, normPos+1);
int colorPos = content.indexOf(colorIndicator);
if (colorPos != -1)
newString.delete(colorPos, colorPos+1);
if (normPos == -1 && colorPos == -1)
break;
}
message.content = newString;
}
private int mircCodeToColor(int code) {
int color;
switch (code) {
case 0: // white
color = R.color.ircmessage_white;
break;
case 1: // black
color = R.color.ircmessage_black;
break;
case 2: // blue (navy)
color = R.color.ircmessage_blue;
break;
case 3: // green
color = R.color.ircmessage_green;
break;
case 4: // red
color = R.color.ircmessage_red;
break;
case 5: // brown (maroon)
color = R.color.ircmessage_brown;
break;
case 6: // purple
color = R.color.ircmessage_purple;
break;
case 7: // orange (olive)
color = R.color.ircmessage_orange;
break;
case 8: // yellow
color = R.color.ircmessage_yellow;
break;
case 9: // light green (lime)
color = R.color.ircmessage_light_green;
break;
case 10: // teal (a green/blue cyan)
color = R.color.ircmessage_teal;
break;
case 11: // light cyan (cyan) (aqua)
color = R.color.ircmessage_light_cyan;
break;
case 12: // light blue (royal)
color = R.color.ircmessage_light_blue;
break;
case 13: // pink (light purple) (fuchsia)
color = R.color.ircmessage_pink;
break;
case 14: // grey
color = R.color.ircmessage_gray;
break;
default:
color = R.color.ircmessage_normal_color;
}
return color;
}
public void disconnectFromCore() {
if (coreConn != null)
coreConn.disconnect();
}
public boolean isConnected() {
return coreConn.isConnected();
}
/**
* Register a resultReceiver with this server, that will get notification
* when a change happens with the connection to the core Like when the
* connection is lost.
*
* @param resultReceiver
* - Receiver that will get the status updates
*/
public void registerStatusReceiver(ResultReceiver resultReceiver) {
statusReceivers.add(resultReceiver);
if (coreConn != null && coreConn.isConnected()) {
resultReceiver.send(CoreConnService.CONNECTION_CONNECTED, null);
} else {
resultReceiver.send(CoreConnService.CONNECTION_DISCONNECTED, null);
}
}
/**
* Unregister a receiver when you don't want any more updates.
*
* @param resultReceiver
*/
public void unregisterStatusReceiver(ResultReceiver resultReceiver) {
statusReceivers.remove(resultReceiver);
}
private void sendStatusMessage(int messageId, Bundle bundle) {
for (ResultReceiver statusReceiver : statusReceivers) {
statusReceiver.send(messageId, bundle);
}
}
/**
* Handler of incoming messages from CoreConnection, since it's in another
* read thread.
*/
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (msg == null) {
Log.e(TAG, "Msg was null?");
return;
}
Buffer buffer;
IrcMessage message;
Bundle bundle;
IrcUser user;
String bufferName;
switch (msg.what) {
case R.id.NEW_BACKLOGITEM_TO_SERVICE:
/**
* New message on one buffer so update that buffer with the new
* message
*/
message = (IrcMessage) msg.obj;
buffer = networks.getBufferById(message.bufferInfo.id);
if (buffer == null) {
Log.e(TAG, "A messages buffer is null:" + message);
return;
}
if (!buffer.hasMessage(message)) {
/**
* Check if we are highlighted in the message, TODO: Add
* support for custom highlight masks
*/
checkMessageForHighlight(buffer, message);
checkForURL(message);
parseStyleCodes(message);
buffer.addBacklogMessage(message);
} else {
Log.e(TAG, "Getting message buffer already have " + buffer.getInfo().name);
}
break;
case R.id.NEW_MESSAGE_TO_SERVICE:
/**
* New message on one buffer so update that buffer with the new
* message
*/
message = (IrcMessage) msg.obj;
buffer = networks.getBufferById(message.bufferInfo.id);
if (buffer == null) {
Log.e(TAG, "A messages buffer is null: " + message);
return;
}
if (!buffer.hasMessage(message)) {
/**
* Check if we are highlighted in the message, TODO: Add
* support for custom highlight masks
*/
checkMessageForHighlight(buffer, message);
parseStyleCodes(message);
if ((message.isHighlighted() && !buffer.isDisplayed()) || (buffer.getInfo().type == BufferInfo.Type.QueryBuffer && ((message.flags & Flag.Self.getValue()) == 0))) {
notificationManager.notifyHighlight(buffer.getInfo().id);
}
checkForURL(message);
buffer.addMessage(message);
if (buffer.isTemporarilyHidden()) {
unhideTempHiddenBuffer(buffer.getInfo().id);
}
} else {
Log.e(TAG, "Getting message buffer already have " + buffer.toString());
}
break;
case R.id.NEW_BUFFER_TO_SERVICE:
/**
* New buffer received, so update out channel holder with the
* new buffer
*/
buffer = (Buffer) msg.obj;
networks.addBuffer(buffer);
break;
case R.id.ADD_MULTIPLE_BUFFERS:
/**
* Complete list of buffers received
*/
for (Buffer tmp : (Collection<Buffer>) msg.obj) {
networks.addBuffer(tmp);
}
break;
case R.id.ADD_NETWORK:
networks.addNetwork((Network)msg.obj);
break;
case R.id.SET_LAST_SEEN_TO_SERVICE:
/**
* Setting last seen message id in a buffer
*/
buffer = networks.getBufferById(msg.arg1);
if (buffer != null) {
buffer.setLastSeenMessage(msg.arg2);
//if(buffer.hasUnseenHighlight()) {FIXME
notificationManager.notifyHighlightsRead(buffer.getInfo().id);
//}
} else {
Log.e(TAG, "Getting set last seen message on unknown buffer: " + msg.arg1);
}
break;
case R.id.SET_MARKERLINE_TO_SERVICE:
/**
* Setting marker line message id in a buffer
*/
buffer = networks.getBufferById(msg.arg1);
if (buffer != null) {
buffer.setMarkerLineMessage(msg.arg2);
} else {
Log.e(TAG, "Getting set marker line message on unknown buffer: " + msg.arg1);
}
break;
case R.id.CONNECTING:
/**
* CoreConn has connected to a core
*/
notificationManager.notifyConnecting();
sendStatusMessage(CoreConnService.CONNECTION_CONNECTING, null);
break;
case R.id.LOST_CONNECTION:
/**
* Lost connection with core, update notification
*/
for (ResultReceiver statusReceiver : statusReceivers) {
if (msg.obj != null) { // Have description of what is wrong,
// used only for login atm
bundle = new Bundle();
bundle.putString(CoreConnService.STATUS_KEY, (String) msg.obj);
statusReceiver.send(CoreConnService.CONNECTION_DISCONNECTED, bundle);
} else {
statusReceiver.send(CoreConnService.CONNECTION_DISCONNECTED, null);
}
}
notificationManager.notifyDisconnected();
break;
case R.id.NEW_USER_ADDED:
/**
* New IrcUser added
*/
user = (IrcUser) msg.obj;
if (networks.getNetworkById(msg.arg1) == null) return; //FIXME
networks.getNetworkById(msg.arg1).onUserJoined(user);
break;
case R.id.SET_BUFFER_ORDER:
/**
* Buffer order changed so set the new one
*/
ArrayList<Integer> a = (((Bundle)msg.obj).getIntegerArrayList("keys"));
ArrayList<Integer> b = ((Bundle)msg.obj).getIntegerArrayList("buffers");
ArrayList<Integer> c = new ArrayList<Integer>();
for(Network net : networks.getNetworkList()) {
c.add(net.getStatusBuffer().getInfo().id);
for(Buffer buf : net.getBuffers().getRawBufferList()) {
if(buf.getInfo().id == 4) System.out.println(buf.getInfo().name);
c.add(buf.getInfo().id);
}
}
Collections.sort(a);
Collections.sort(b);
Collections.sort(c);
if(networks == null) throw new RuntimeException("Networks are null when setting buffer order");
if(networks.getBufferById(msg.arg1) == null)
return;
//throw new RuntimeException("Buffer is null when setting buffer order, bufferid " + msg.arg1 + " order " + msg.arg2 + " for this buffers keys: " + a.toString() + " corecon buffers: " + b.toString() + " service buffers: " + c.toString());
networks.getBufferById(msg.arg1).setOrder(msg.arg2);
break;
case R.id.SET_BUFFER_TEMP_HIDDEN:
/**
* Buffer has been marked as temporary hidden, update buffer
*/
networks.getBufferById(msg.arg1).setTemporarilyHidden((Boolean) msg.obj);
break;
case R.id.SET_BUFFER_PERM_HIDDEN:
/**
* Buffer has been marked as permanently hidden, update buffer
*/
networks.getBufferById(msg.arg1).setPermanentlyHidden((Boolean) msg.obj);
break;
case R.id.INVALID_CERTIFICATE:
/**
* Received a mismatching certificate
*/
case R.id.NEW_CERTIFICATE:
/**
* Received a new, unseen certificate
*/
bundle = new Bundle();
bundle.putString(CERT_KEY, (String) msg.obj);
sendStatusMessage(CoreConnService.NEW_CERTIFICATE, bundle);
break;
case R.id.SET_BUFFER_ACTIVE:
/**
* Set buffer as active or parted
*/
networks.getBufferById(msg.arg1).setActive((Boolean)msg.obj);
break;
case R.id.UNSUPPORTED_PROTOCOL:
/**
* The protocol version of the core is not supported so tell user it is to old
*/
sendStatusMessage(CoreConnService.UNSUPPORTED_PROTOCOL, null);
break;
case R.id.INIT_PROGRESS:
bundle = new Bundle();
bundle.putString(PROGRESS_KEY, (String)msg.obj);
sendStatusMessage(CoreConnService.INIT_PROGRESS, bundle);
break;
case R.id.INIT_DONE:
/**
* CoreConn has connected to a core
*/
notificationManager.notifyConnected();
sendStatusMessage(CoreConnService.INIT_DONE, null);
break;
case R.id.USER_PARTED:
bundle = (Bundle) msg.obj;
if (networks.getNetworkById(msg.arg1) == null) { // sure why not
System.err.println("Unable to find network for message");
return;
}
networks.getNetworkById(msg.arg1).onUserParted(bundle.getString("nick"), bundle.getString("buffer"));
break;
case R.id.USER_QUIT:
if (networks.getNetworkById(msg.arg1) == null) {
System.err.println("Unable to find buffer for message");
return;
}
networks.getNetworkById(msg.arg1).onUserQuit((String)msg.obj);
break;
case R.id.USER_JOINED:
if (networks.getNetworkById(msg.arg1) == null) {
System.err.println("Unable to find buffer for message");
return;
}
bundle = (Bundle) msg.obj;
user = networks.getNetworkById(msg.arg1).getUserByNick(bundle.getString("nick"));
String modes = (String)bundle.get("mode");
networks.getNetworkById(msg.arg1).getBuffers().getBuffer(msg.arg2).getUsers().addUser(user, modes);
break;
case R.id.USER_CHANGEDNICK:
if (networks.getNetworkById(msg.arg1) == null) {
System.err.println("Unable to find buffer for message");
return;
}
bundle = (Bundle) msg.obj;
user = networks.getNetworkById(msg.arg1).getUserByNick(bundle.getString("oldNick"));
if (user == null) {
System.err.println("Unable to find user " + bundle.getString("oldNick"));
return;
}
user.changeNick(bundle.getString("newNick"));
break;
case R.id.USER_ADD_MODE:
if (networks.getNetworkById(msg.arg1) == null) {
System.err.println("Unable to find buffer for message");
return;
}
bundle = (Bundle) msg.obj;
bufferName = bundle.getString("channel");
user = networks.getNetworkById(msg.arg1).getUserByNick(bundle.getString("nick"));
for(Buffer buf : networks.getNetworkById(msg.arg1).getBuffers().getRawBufferList()) {
if(buf.getInfo().name.equals(bufferName)) {
buf.getUsers().addUserMode(user, bundle.getString("mode"));
break;
}
}
break;
case R.id.USER_REMOVE_MODE:
if (networks.getNetworkById(msg.arg1) == null) {
System.err.println("Unable to find buffer for message");
return;
}
bundle = (Bundle) msg.obj;
bufferName = bundle.getString("channel");
user = networks.getNetworkById(msg.arg1).getUserByNick(bundle.getString("nick"));
for(Buffer buf : networks.getNetworkById(msg.arg1).getBuffers().getRawBufferList()) {
if(buf.getInfo().name.equals(bufferName)) {
buf.getUsers().removeUserMode(user, bundle.getString("mode"));
break;
}
}
break;
}
}
}
public boolean isInitComplete() {
if(coreConn == null) return false;
return coreConn.isInitComplete();
}
public boolean isUserAway(String nick, int networkId) {
Network network = networks.getNetworkById(networkId);
if (network == null) return false;
IrcUser user = network.getUserByNick(nick);
if (user == null) return false;
return user.away;
}
public boolean isUserOnline(String nick, int networkId) {
Network network = networks.getNetworkById(networkId);
if (network == null) return true;
return !network.hasNick(nick);
}
}