Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d99aa45be34b79cb63772ead90af541b3ad08c66 (plain) (blame)
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
/*******************************************************************************
 * Copyright (c) 2004, 2009 Composent, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Composent, Inc. - initial API and implementation
 *    Jacek Pospychala <jacek.pospychala@pl.ibm.com> - bug 197604, 197329
 ******************************************************************************/
package org.eclipse.ecf.internal.provider.irc.container;

import java.net.InetSocketAddress;
import java.util.*;
import org.eclipse.ecf.core.*;
import org.eclipse.ecf.core.events.*;
import org.eclipse.ecf.core.identity.*;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.internal.provider.irc.Activator;
import org.eclipse.ecf.internal.provider.irc.Messages;
import org.eclipse.ecf.internal.provider.irc.datashare.IIRCDatashareContainer;
import org.eclipse.ecf.internal.provider.irc.datashare.IRCDatashareContainer;
import org.eclipse.ecf.internal.provider.irc.identity.IRCID;
import org.eclipse.ecf.internal.provider.irc.identity.IRCNamespace;
import org.eclipse.ecf.presence.chatroom.*;
import org.eclipse.ecf.presence.history.IHistory;
import org.eclipse.ecf.presence.history.IHistoryManager;
import org.eclipse.ecf.presence.im.IChatMessage.Type;
import org.eclipse.ecf.presence.im.IChatMessageSender;
import org.eclipse.equinox.concurrent.future.TimeoutException;
import org.eclipse.osgi.util.NLS;
import org.schwering.irc.lib.*;
import org.schwering.irc.lib.ssl.SSLIRCConnection;

/**
 * IRC 'root' container implementation. This class implements the
 * IChatRoomManager and the IChatRoomContainer interfaces, allowing it to
 * function as both a manager of IRC channels and as an IRC channel itself.
 * 
 */
public class IRCRootContainer extends IRCAbstractContainer implements
		IContainer, IChatMessageSender, IChatRoomInvitationSender,
		IChatRoomManager, IChatRoomContainer, IRCMessageChannel,
		IChatRoomContainerOptionsAdapter {

	private static final long CONNECT_TIMEOUT = new Long(System.getProperty(
			"org.eclipse.ecf.provider.irc.connectTimeout", "60000"))
			.longValue();

	protected IRCConnection connection = null;

	protected ReplyHandler replyHandler = null;

	protected Map channels = new HashMap();

	protected String username;

	protected String encoding = null;

	private ArrayList invitationListeners;

	protected Object connectLock = new Object();
	protected boolean connectWaiting = false;
	protected Exception connectException = null;

	/**
	 * The datashare container implementation owned by this container.
	 */
	private IIRCDatashareContainer datashareContainer;

	/**
	 * Used for determining whether the USERHOST reply should be hijacked or
	 * not.
	 */
	private boolean retrieveUserhost;

	public IRCRootContainer(ID localID) throws IDCreateException {
		this.localID = localID;
		this.unknownID = IDFactory.getDefault().createStringID(
				Messages.IRCRootContainer_0);
		this.replyHandler = new ReplyHandler();
		invitationListeners = new ArrayList();
		datashareContainer = createDatashareContainer();
	}

	/**
	 * Creates and returns a datashare container implementation.
	 * 
	 * @return a datashare container implementation for this container, or
	 *         <code>null</code> if it could not be created
	 */
	private IIRCDatashareContainer createDatashareContainer() {
		if (hasDatashare() && hasNIO()) {
			return new IRCDatashareContainer(this);
		}
		return null;
	}

	/**
	 * Checks and returns whether the datashare NIO APIs are available in the
	 * current OSGi environment.
	 * 
	 * @return <code>true</code> if the datashare NIO APIs are available,
	 *         <code>false</code> otherwise
	 */
	private boolean hasDatashare() {
		return Activator.getDefault().hasDatashare();
	}

	/**
	 * Checks and returns there is Java 1.4 NIO support in the current Java
	 * runtime environment.
	 * 
	 * @return <code>true</code> if the Java 1.4 NIO APIs are available,
	 *         <code>false</code> otherwise
	 */
	private boolean hasNIO() {
		try {
			Class.forName("java.nio.channels.SocketChannel"); //$NON-NLS-1$
			return true;
		} catch (ClassNotFoundException e) {
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ecf.core.IContainer#connect(org.eclipse.ecf.core.identity.ID,
	 * org.eclipse.ecf.core.security.IConnectContext)
	 */
	public void connect(ID connectID, IConnectContext connectContext)
			throws ContainerConnectException {
		if (connection != null)
			throw new ContainerConnectException(
					Messages.IRCRootContainer_Exception_Already_Connected);
		if (connectID == null)
			throw new ContainerConnectException(
					Messages.IRCRootContainer_Exception_TargetID_Null);
		if (!(connectID instanceof IRCID))
			throw new ContainerConnectException(NLS.bind(
					Messages.IRCRootContainer_Exception_TargetID_Wrong_Type,
					new Object[] { targetID, IRCID.class.getName() }));
		if (connectWaiting)
			throw new ContainerConnectException(
					Messages.IRCRootContainer_Connecting);

		fireContainerEvent(new ContainerConnectingEvent(this.getID(),
				connectID, connectContext));
		// Get password via callback in connectContext
		String pw = getPasswordFromConnectContext(connectContext);
		IRCID tID = (IRCID) connectID;
		String host = tID.getHost();
		int port = tID.getPort();
		String pass = pw;
		String nick = tID.getUser();
		String user = nick;
		this.username = user;
		String name = null;
		boolean ssl = false;
		if (!ssl) {
			connection = new IRCConnection(host, new int[] { port }, pass,
					nick, user, name);
		} else {
			connection = new SSLIRCConnection(host, new int[] { port }, pass,
					nick, user, name);
		}
		// connection setup
		connection.addIRCEventListener(getIRCEventListener());
		connection.setPong(true);
		connection.setDaemon(false);
		connection.setColors(true);
		if (encoding != null)
			connection.setEncoding(encoding);
		trace(Messages.IRCRootContainer_Connecting_To + targetID);
		synchronized (connectLock) {
			connectWaiting = true;
			connectException = null;
			try {
				connection.connect();
				long timeout = CONNECT_TIMEOUT + System.currentTimeMillis();
				while (connectWaiting && (timeout > System.currentTimeMillis())) {
					connectLock.wait(2000);
				}
				if (connectWaiting)
					throw new TimeoutException(NLS.bind(
							Messages.IRCRootContainer_Connect_Timeout,
							tID.getName()), CONNECT_TIMEOUT);
				if (connectException != null)
					throw connectException;
				this.targetID = tID;
				fireContainerEvent(new ContainerConnectedEvent(getID(),
						this.targetID));

				if (datashareContainer != null) {
					// now that we've connected successfully, we send a USERHOST
					// message to the server so that we can attempt to retrieve
					// our current IP
					retrieveUserhost = true;
					connection.doUserhost(nick);
				}
			} catch (Exception e) {
				this.targetID = null;
				throw new ContainerConnectException(NLS.bind(
						Messages.IRCRootContainer_Exception_Connect_Failed,
						connectID.getName()), e);
			} finally {
				connectWaiting = false;
				connectException = null;
			}
		}
	}

	protected void handleDisconnected() {
		for (Iterator i = channels.values().iterator(); i.hasNext();) {
			IRCChannelContainer c = (IRCChannelContainer) i.next();
			c.disconnect();
		}
		fireContainerEvent(new ContainerDisconnectedEvent(getID(), targetID));
		channels.clear();
	}

	protected void handleErrorIfConnecting(String message) {
		synchronized (connectLock) {
			if (connectWaiting)
				this.connectException = new Exception(message);
		}
	}

	protected IRCEventListener getIRCEventListener() {
		return new IRCEventListener() {
			public void onRegistered() {
				trace("handleOnRegistered()"); //$NON-NLS-1$
				synchronized (connectLock) {
					connectWaiting = false;
					connectLock.notify();
				}
			}

			public void onDisconnected() {
				trace("handleOnDisconnected()"); //$NON-NLS-1$
				fireContainerEvent(new ContainerDisconnectingEvent(getID(),
						targetID));
				synchronized (connectLock) {
					if (connectWaiting) {
						if (connectException == null)
							connectException = new Exception(
									Messages.IRCRootContainer_Exception_Unexplained_Disconnect);
						connectWaiting = false;
						connectLock.notify();
					}
				}
				if (targetID != null) {
					showMessage(null, NLS.bind(
							Messages.IRCRootContainer_Disconnected,
							targetID.getName()));
					handleDisconnected();
				}
			}

			public void onError(String arg0) {
				trace("handleOnError(" + arg0 + ")"); //$NON-NLS-1$ //$NON-NLS-2$
				showMessage(null,
						NLS.bind(Messages.IRCRootContainer_Error, arg0));
				handleErrorIfConnecting(arg0);
			}

			public void onError(int arg0, String arg1) {
				String msg = arg0 + "," + arg1; //$NON-NLS-1$
				trace("handleOnError(" + msg + ")"); //$NON-NLS-1$ //$NON-NLS-2$
				showMessage(null,
						NLS.bind(Messages.IRCRootContainer_Error, msg));
				handleErrorIfConnecting(arg0 + msg);
			}

			public void onInvite(String arg0, IRCUser arg1, String arg2) {
				handleInvite(createIDFromString(arg0),
						createIDFromString(arg1.getNick()));
			}

			public void onJoin(String arg0, IRCUser arg1) {
				trace("handleOnJoin(" + arg0 + "," + arg1 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				IRCChannelContainer container = getChannel(arg0);
				if (container != null) {
					container.setIRCUser(arg1);
				}
			}

			public void onKick(String channelName, IRCUser kicker,
					String kicked, String reason) {
				trace("handleOnKick(" + channelName + "," + kicker + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
						+ kicked + "," + reason + ")"); //$NON-NLS-1$ //$NON-NLS-2$
				// retrieve the channel that this kick is happening at
				IRCChannelContainer channel = getChannel(channelName);
				if (channel != null) {
					// display a message to indicate that a user has been kicked
					// from the channel
					showMessage(channelName, NLS.bind(
							Messages.IRCRootContainer_UserKicked, new Object[] {
									kicker.getNick(), kicked, channelName,
									reason }));
					// check if we are the ones that have been kicked
					if (kicked.equals(((IRCID) targetID).getUsername())) {
						// fire disconnection events for this channel container
						channel.fireContainerEvent(new ContainerDisconnectingEvent(
								channel.getID(), channel.targetID));
						channel.firePresenceListeners(false,
								new String[] { kicked });
						channel.fireContainerEvent(new ContainerDisconnectedEvent(
								channel.getID(), channel.targetID));
					} else {
						channel.firePresenceListeners(false,
								new String[] { kicked });
					}
				}
			}

			public void onMode(String arg0, IRCUser arg1, IRCModeParser arg2) {
				trace("handleOnMode(" + arg0 + "," + arg1 + "," + arg2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			}

			public void onMode(IRCUser arg0, String arg1, String arg2) {
				trace("handleOnMode(" + arg0 + "," + arg1 + "," + arg2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			}

			public void onNick(IRCUser arg0, String arg1) {
				trace("handleOnNick(" + arg0 + "," + arg1 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}

			public void onNotice(String arg0, IRCUser arg1, String arg2) {
				trace("handleOnNotice(" + arg0 + "," + arg1 + "," + arg2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				showMessage(arg0, arg2);
			}

			public void onPart(String arg0, IRCUser arg1, String arg2) {
				trace("handleOnPart(" + arg0 + "," + arg1 + "," + arg2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				IRCChannelContainer channel = (IRCChannelContainer) channels
						.get(arg0);
				if (channel != null) {
					channel.firePresenceListeners(false,
							new String[] { getIRCUserName(arg1) });
				}
			}

			public void onPing(String arg0) {
				trace("handleOnPing(" + arg0 + ")"); //$NON-NLS-1$ //$NON-NLS-2$
				synchronized (IRCRootContainer.this) {
					if (connection != null) {
						connection.doPong(arg0);
					}
				}
			}

			public void onPrivmsg(String arg0, IRCUser arg1, String arg2) {
				trace("handleOnPrivmsg(" + arg0 + "," + arg1 + "," + arg2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ 
				if (arg2.equals("\01VERSION\01")) { //$NON-NLS-1$
					showMessage(null, NLS.bind(
							Messages.IRCRootContainer_CTCP_VERSION_Request,
							arg1.toString()));
				} else if (arg2.startsWith("\01ECF ") && arg2.endsWith("\01")) { //$NON-NLS-1$ //$NON-NLS-2$
					if (datashareContainer != null) {
						// special ECF message, retrieve the ip address of the
						// remote peer and initiate a socket connection
						String identifier = arg2
								.substring(5, arg2.length() - 1);
						StringTokenizer tokenizer = new StringTokenizer(
								identifier, ":"); //$NON-NLS-1$
						datashareContainer.enqueue(new InetSocketAddress(
								tokenizer.nextToken(), Integer
										.parseInt(tokenizer.nextToken())));
					}
				} else {
					showMessage(arg0, arg1.toString(), arg2);
				}

			}

			public void onQuit(IRCUser arg0, String arg1) {
				trace("handleOnQuit(" + arg0 + "," + arg1 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				for (Iterator i = channels.values().iterator(); i.hasNext();) {
					IRCChannelContainer container = (IRCChannelContainer) i
							.next();
					container.handleUserQuit(getIRCUserName(arg0));
				}
			}

			public void onReply(int arg0, String arg1, String arg2) {
				trace("handleOnReply(" + arg0 + "|" + arg1 + "|" + arg2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				replyHandler.handleReply(arg0, arg1, arg2);
			}

			public void onTopic(String arg0, IRCUser arg1, String arg2) {
				trace("handleOnTopic(" + arg0 + "," + arg1 + "," + arg2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				handleSetSubject(arg0, arg1, arg2);
			}

			public void unknown(String arg0, String arg1, String arg2,
					String arg3) {
				trace("handleUnknown(" + arg0 + "," + arg1 + "," + arg2 + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
						+ arg3 + ")"); //$NON-NLS-1$
				showMessage(null, NLS.bind(
						Messages.IRCRootContainer_Unknown_Message,
						new Object[] { arg0, arg1, arg2, arg3 }));
			}
		};
	}

	protected String getIRCUserName(IRCUser user) {
		return user == null ? null : user.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.IContainer#disconnect()
	 */
	public void disconnect() {
		if (connection != null) {
			connection.close();
			connection = null;
			targetID = null;
			retrieveUserhost = false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.IContainer#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class serviceType) {
		if (serviceType == null) {
			return null;
		} else if (serviceType.isInstance(this)) {
			return this;
		} else if (datashareContainer != null
				&& serviceType.isInstance(datashareContainer)) {
			return datashareContainer;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.IContainer#getConnectNamespace()
	 */
	public Namespace getConnectNamespace() {
		return IDFactory.getDefault().getNamespaceByName(
				IRCNamespace.IRC_SCHEME);
	}

	public IChatRoomInfo getChatRoomInfo(final String roomName) {
		if (roomName == null)
			return new IChatRoomInfo() {
				public IChatRoomContainer createChatRoomContainer()
						throws ContainerCreateException {
					return IRCRootContainer.this;
				}

				public ID getConnectedID() {
					return IRCRootContainer.this.getConnectedID();
				}

				public String getDescription() {
					return ""; //$NON-NLS-1$
				}

				public String getName() {
					return ROOT_ROOMNAME;
				}

				public int getParticipantsCount() {
					return 0;
				}

				public ID getRoomID() {
					return getSystemID();
				}

				public String getSubject() {
					return ""; //$NON-NLS-1$
				}

				public boolean isModerated() {
					return false;
				}

				public boolean isPersistent() {
					return false;
				}

				public boolean requiresPassword() {
					return false;
				}

				public Object getAdapter(Class adapter) {
					return null;
				}
			};
		return new IChatRoomInfo() {
			public IChatRoomContainer createChatRoomContainer()
					throws ContainerCreateException {
				try {
					IRCChannelContainer newChannelContainer = new IRCChannelContainer(
							IRCRootContainer.this, IDFactory.getDefault()
									.createGUID());
					addChannel(roomName, newChannelContainer);
					return newChannelContainer;
				} catch (Exception e) {
					throw new ContainerCreateException(
							Messages.IRCRootContainer_Exception_Create_ChatRoom,
							e);
				}
			}

			public ID getConnectedID() {
				return IRCRootContainer.this.getConnectedID();
			}

			public String getDescription() {
				return ""; //$NON-NLS-1$
			}

			public String getName() {
				return roomName;
			}

			public int getParticipantsCount() {
				return 0;
			}

			public ID getRoomID() {
				return createIDFromString(roomName);
			}

			public String getSubject() {
				return ""; //$NON-NLS-1$
			}

			public boolean isModerated() {
				return false;
			}

			public boolean isPersistent() {
				return false;
			}

			public boolean requiresPassword() {
				return false;
			}

			public Object getAdapter(Class adapter) {
				return null;
			}
		};
	}

	public IChatRoomInfo[] getChatRoomInfos() {
		return new IChatRoomInfo[0];
	}

	public IChatRoomManager[] getChildren() {
		return new IChatRoomManager[0];
	}

	public IChatRoomManager getParent() {
		return null;
	}

	public void addChatRoomParticipantListener(
			IChatRoomParticipantListener participantListener) {
		// for root container, no participant listening
	}

	public void removeChatRoomParticipantListener(
			IChatRoomParticipantListener participantListener) {
		// for root container, no participant listening
	}

	public IChatRoomMessageSender getChatRoomMessageSender() {
		return new IChatRoomMessageSender() {
			public void sendMessage(String message) throws ECFException {
				if (isCommand(message))
					parseCommandAndSend(message, null, null);
				else
					showErrorMessage(null, NLS.bind(
							Messages.IRCRootContainer_Command_Error, message,
							COMMAND_PREFIX));
			}
		};
	}

	/**
	 * Returns string from the buffer beginning up to nearest COMMAND_DELIM.
	 * Strips trailing COMMAND_DELIMs.
	 * 
	 * @param buffer
	 * @return String up to first COMMAND_DELIM occurrence in buffer.
	 */
	protected String nextToken(StringBuffer buffer) {
		if (buffer.length() == 0) {
			return null;
		}

		String token;

		int index = buffer.indexOf(COMMAND_DELIM);

		if (index == -1) { // no delim until the end of buffer
			token = buffer.toString();
			buffer.delete(0, buffer.length());
			return token;
		}

		token = buffer.substring(0, index);

		/*
		 * if (token.length() == buffer.length()) { //index = token.length() -
		 * 1; }
		 */

		// trim trailing command delims
		while ((buffer.length() > index)
				&& (buffer.indexOf(COMMAND_DELIM, index) == index)) {
			index += COMMAND_DELIM.length();
		}
		if (index > 0) {
			buffer.delete(0, index);
		}

		return token;
	}

	protected void parseCommandAndSend(String commandMessage,
			String channelName, String ircUser) {
		synchronized (this) {
			if (connection != null) {
				try {
					String lowerCase = commandMessage.toLowerCase();
					StringBuffer command = new StringBuffer(commandMessage);

					if (lowerCase.startsWith("/msg ")) { //$NON-NLS-1$
						commandMessage = commandMessage.substring(5);
						int index = commandMessage.indexOf(COMMAND_DELIM);
						if (index != -1) {
							connection.doPrivmsg(
									commandMessage.substring(0, index),
									commandMessage.substring(index + 1));
						}
					} else if (lowerCase.startsWith("/privmsg ")) { //$NON-NLS-1$
						commandMessage = commandMessage.substring(9);
						int index = commandMessage.indexOf(COMMAND_DELIM);
						if (index != -1) {
							connection.doPrivmsg(
									commandMessage.substring(0, index),
									commandMessage.substring(index + 1));
						}
					} else if (lowerCase.startsWith("/op ")) { //$NON-NLS-1$
						nextToken(command); // skip command

						String nick = nextToken(command);

						if (nick != null) {
							connection.doMode(channelName, "+o " + nick); //$NON-NLS-1$
						}
					} else if (lowerCase.startsWith("/dop ")) { //$NON-NLS-1$
						nextToken(command); // skip command

						String nick = nextToken(command);

						if (nick != null) {
							connection.doMode(channelName, "-o " + nick); //$NON-NLS-1$
						}
					} else if (lowerCase.startsWith("/ban ")) { //$NON-NLS-1$
						nextToken(command); // skip command

						String nick = nextToken(command);

						if (nick != null) {
							connection.doMode(channelName, "+b " + nick); //$NON-NLS-1$
						}
					} else if (lowerCase.startsWith("/unban ")) { //$NON-NLS-1$
						nextToken(command); // skip command

						String nick = nextToken(command);

						if (nick != null) {
							connection.doMode(channelName, "-b " + nick); //$NON-NLS-1$ 
						}
					} else if (lowerCase.startsWith("/kick ")) { //$NON-NLS-1$
						nextToken(command); // skip command

						String nick = nextToken(command);

						if (nick.startsWith("#")) { //$NON-NLS-1$
							channelName = nick;
							nick = nextToken(command);
						}

						String comment = command.toString(); // rest of command

						if (comment.length() > 0) {
							connection.doKick(channelName, nick, comment);
						} else {
							connection.doKick(channelName, nick);
						}
					} else if (lowerCase.startsWith("/mode ")) { //$NON-NLS-1$
						commandMessage = commandMessage.substring(6);
						int index = commandMessage.indexOf(COMMAND_DELIM);
						if (index != -1) {
							connection.doMode(channelName, commandMessage);
						}
					} else if (lowerCase.startsWith("/me ")) { //$NON-NLS-1$
						nextToken(command); // skip command

						String message = command.toString();
						if (message.length() > 0) {
							message = "\01ACTION " + message + "\01"; //$NON-NLS-1$ //$NON-NLS-2$
							connection.doPrivmsg(channelName, message);
							showMessage(channelName, ircUser, message);
						}
					} else {
						String[] tokens = parseCommandTokens(commandMessage);
						handleCommandMessage(tokens, channelName);
					}
				} catch (Exception e) {
					showErrorMessage(channelName, NLS.bind(
							Messages.IRCRootContainer_Exception_Parse,
							new Object[] { e.getClass().getName(),
									e.getLocalizedMessage() }));
					traceStack(e, "PARSE ERROR: " + commandMessage); //$NON-NLS-1$
				}
			} else {
				trace("parseMessageAndSend(" + commandMessage //$NON-NLS-1$
						+ ") Not connected for IRCContainer " + getID()); //$NON-NLS-1$
			}
		}
	}

	private synchronized void handleCommandMessage(String[] tokens,
			String channelName) {
		// Look at first one and switch
		String origCommand = tokens[0];
		String command = origCommand;
		while (command.startsWith(COMMAND_PREFIX))
			command = command.substring(1);
		String[] args = new String[tokens.length - 1];
		System.arraycopy(tokens, 1, args, 0, tokens.length - 1);
		if (command.equalsIgnoreCase(JOIN_COMMAND)) {
			if (args.length > 1) {
				connection.doJoin(args[0], args[1]);
			} else if (args.length > 0) {
				connection.doJoin(args[0]);
			}
		} else if (command.equalsIgnoreCase(LIST_COMMAND)) {
			if (args.length > 0) {
				connection.doList(args[0]);
			} else
				connection.doList();
		} else if (command.equalsIgnoreCase(PART_COMMAND)) {
			if (args.length > 1) {
				connection.doPart(args[0], args[1]);
			} else if (args.length > 0) {
				connection.doPart(args[0]);
			}
		} else if (command.equalsIgnoreCase(NICK_COMMAND)) {
			if (args.length > 0) {
				connection.doNick(args[0]);
			}
		} else if (command.equalsIgnoreCase(NOTICE_COMMAND)) {
			if (args.length > 1) {
				connection.doNotice(args[0], args[1]);
			}
		} else if (command.equalsIgnoreCase(WHOIS_COMMAND)) {
			if (args.length > 0) {
				connection.doWhois(args[0]);
			}
		} else if (command.equalsIgnoreCase(QUIT_COMMAND)) {
			if (args.length > 0) {
				connection.doQuit(args[0]);
			} else {
				connection.doQuit();
			}
		} else if (command.equalsIgnoreCase(AWAY_COMMAND)) {
			if (args.length > 0) {
				connection.doAway(args[0]);
			} else {
				connection.doAway();
			}
		} else if (command.equalsIgnoreCase(TOPIC_COMMAND)) {
			if (args.length > 1) {
				StringBuffer sb = new StringBuffer();
				for (int i = 1; i < args.length; i++) {
					if (i > 1) {
						sb.append(COMMAND_DELIM);
					}
					sb.append(args[i]);
				}
				connection.doTopic(args[0], sb.toString());
			} else if (args.length > 0) {
				connection.doTopic(args[0]);
			}
		} else if (command.equalsIgnoreCase(INVITE_COMMAND)) {
			if (args.length > 1) {
				connection.doInvite(args[0], args[1]);
			}
		} else {
			String msg = NLS
					.bind(Messages.IRCRootContainer_Command_Unrecognized,
							origCommand);
			trace(msg + " in IRCContainer: " + getID()); //$NON-NLS-1$
			showErrorMessage(channelName, msg);
		}
	}

	protected void handleInvite(ID channelID, ID fromID) {
		synchronized (invitationListeners) {
			for (int i = 0; i < invitationListeners.size(); i++) {
				IChatRoomInvitationListener icril = (IChatRoomInvitationListener) invitationListeners
						.get(i);
				icril.handleInvitationReceived(channelID, fromID, null, null);
			}
		}
	}

	protected IRCChannelContainer getChannel(String channel) {
		if (channel == null)
			return null;
		IRCChannelContainer container = getContainerForChannel(channel);
		if (container == null)
			return null;
		return container;
	}

	protected void showMessage(String channel, String msg) {
		IRCChannelContainer msgChannel = getChannel(channel);
		if (msgChannel != null)
			msgChannel.fireChatRoomMessageEvent(createIDFromString(channel),
					msg);
		else
			fireChatRoomMessageEvent((channel == null) ? getSystemID()
					: createIDFromString(channel), msg);
	}

	protected void showMessage(String channel, String user, String msg) {
		IRCChannelContainer msgChannel = getChannel(channel);
		if (msgChannel != null) {
			msgChannel.fireChatRoomMessageEvent(createIDFromString(user), msg);
		} else {
			fireChatMessageEvent(createIDFromString(user), msg);
			for (Iterator it = channels.values().iterator(); it.hasNext();) {
				msgChannel = (IRCChannelContainer) it.next();
				msgChannel.fireChatMessageEvent(createIDFromString(user), msg);
			}
		}
	}

	void showErrorMessage(String channel, String msg) {
		IRCChannelContainer msgChannel = getChannel(channel);
		if (msgChannel != null)
			msgChannel.fireChatRoomMessageEvent(
					(username == null) ? getSystemID()
							: createIDFromString(username), msg);
		else
			fireChatRoomMessageEvent((username == null) ? getSystemID()
					: createIDFromString(username), msg);
	}

	ID getSystemID() {
		if (targetID == null)
			return unknownID;
		try {
			return IDFactory.getDefault().createStringID(
					((IRCID) targetID).getHost());
		} catch (IDCreateException e) {
			Activator.log(
					"ID creation exception in IRCContainer.getSystemID()", e); //$NON-NLS-1$
			return unknownID;
		}
	}

	protected void handle353Reply(String channel, String[] strings) {
		IRCChannelContainer container = getChannel(channel);
		if (container == null) {
			showMessage(null,
					NLS.bind(Messages.IRCRootContainer_353_Error, channel));
		} else
			container.firePresenceListeners(true, strings);
	}

	protected class ReplyHandler {
		public void handleReply(int code, String arg1, String arg2) {
			String[] users = parseUsers(arg1);
			switch (code) {
			case 353:
				handle353Reply(users[2], parseUserNames(arg2));
				break;
			case 311:
				showMessage(null,
						NLS.bind(Messages.IRCRootContainer_Whois, users[1]));
				showMessage(null, trimUsername(users[2]) + "@" + users[3]); //$NON-NLS-1$
				break;
			case 312:
				showMessage(null, NLS.bind(Messages.IRCRootContainer_Server,
						new Object[] { users[2], arg2 }));
				break;
			case 317:
				showMessage(null,
						NLS.bind(Messages.IRCRootContainer_Idle, users[2]));
				break;
			case 318:
				showMessage(null, Messages.IRCRootContainer_Whois_End);
				break;
			case 319:
				showMessage(null,
						NLS.bind(Messages.IRCRootContainer_Channels, arg2));
				break;
			case 320:
				break;
			case 331:
			case 332:
				// Subject changes
				String[] args = parseCommandTokens(arg1);
				String channel = (args.length == 2) ? args[1]
						: ((args.length == 1) ? args[0] : null);
				handleSetSubject(channel, null, arg2);
				break;
			case 302:
				if (retrieveUserhost) {
					if (datashareContainer != null) {
						// set the retrieved ip address from the
						arg2 = arg2.trim();
						String ip = arg2.substring(arg2.lastIndexOf('@') + 1);
						datashareContainer.setIP(ip);
					}
					retrieveUserhost = false;
					break;
				}
			default:
				// first user always expected to be us
				if (users.length < 2)
					showMessage(null, arg2);
				else {
					showMessage(users[1], concat(users, 2, arg2));
				}
			}
		}

		private String trimUsername(String un) {
			int eq = un.indexOf('=');
			return un.substring(eq + 1);
		}
	}

	protected void handleSetSubject(String channelName, IRCUser user,
			String newSubject) {
		IRCChannelContainer channel = (IRCChannelContainer) channels
				.get(channelName);
		if (channel == null) {
			showMessage(null, newSubject);
			fireSubjectListeners(null, newSubject);
		} else {
			String nickname = (user == null) ? null : user.getNick();
			ID fromID = (user == null) ? null
					: createIDFromString(getIRCUserName(user));
			// Put out message to channel
			if (nickname == null)
				showMessage(channelName, newSubject);
			else
				showMessage(channelName, NLS.bind(
						Messages.IRCRootContainer_TopicChange, new Object[] {
								nickname, newSubject }));
			// Also notify subject listeners
			channel.fireSubjectListeners(fromID, newSubject);
		}
	}

	protected void doJoinChannel(String channelName, String key) {
		if (connection != null) {
			if (key == null || key.equals("")) { //$NON-NLS-1$
				connection.doJoin(channelName);
			} else {
				connection.doJoin(channelName, key);
			}
		}
	}

	protected void doPartChannel(String channelName) {
		if (connection != null) {
			connection.doPart(channelName);
		}
	}

	protected void doSendChannelMessage(String channelName, String ircUser,
			String msg) {
		if (connection != null) {
			// If it's a command,
			if (isCommand(msg)) {
				parseCommandAndSend(msg, channelName, ircUser);
			} else {
				connection.doPrivmsg(channelName, msg);
				showMessage(channelName, ircUser, msg);
			}
		}
	}

	protected void doSendSubjectChangeMessage(String channelName, String topic)
			throws ECFException {
		if (connection == null)
			throw new ECFException(
					Messages.IRCRootContainer_Exception_Unexplained_Disconnect);
		connection.doTopic(channelName, topic);
	}

	protected void addChannel(String channel, IRCChannelContainer container) {
		channels.put(channel, container);
	}

	protected IRCChannelContainer getContainerForChannel(String channel) {
		return (IRCChannelContainer) channels.get(channel);
	}

	protected void removeChannel(String channel) {
		channels.remove(channel);
	}

	public boolean setEncoding(String encoding) {
		if (connection == null) {
			this.encoding = encoding;
			return true;
		}
		return false;
	}

	public void addInvitationListener(IChatRoomInvitationListener listener) {
		if (listener != null) {
			synchronized (invitationListeners) {
				if (!invitationListeners.contains(listener)) {
					invitationListeners.add(listener);
				}
			}
		}
	}

	public void removeInvitationListener(IChatRoomInvitationListener listener) {
		if (listener != null) {
			synchronized (invitationListeners) {
				invitationListeners.remove(listener);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ecf.presence.chatroom.IChatRoomManager#createChatRoom(java
	 * .lang.String, java.util.Map)
	 */
	public IChatRoomInfo createChatRoom(String roomname, Map properties)
			throws ChatRoomCreateException {
		throw new ChatRoomCreateException(roomname,
				Messages.IRCRootContainer_Exception_Create_Not_Supported, null);
	}

	protected IHistoryManager chatRoomHistoryManager = new IHistoryManager() {

		public IHistory getHistory(ID chatRoomID, Map options) {
			// TODO Auto-generated method stub
			return null;
		}

		public boolean isActive() {
			// TODO Auto-generated method stub
			return false;
		}

		public void setActive(boolean active) {

		}

		public Object getAdapter(Class adapter) {
			return null;
		}

	};

	public IHistoryManager getHistoryManager() {
		return chatRoomHistoryManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ecf.presence.chatroom.IChatRoomManager#getInvitationSender()
	 */
	public IChatRoomInvitationSender getInvitationSender() {
		return this;
	}

	public void sendInvitation(ID room, ID targetUser, String subject,
			String body) throws ECFException {
		if (connection == null)
			throw new ECFException(
					Messages.IRCRootContainer_EXCEPTION_CONNECTION_CANNOT_BE_NULL);
		connection.doInvite(targetUser.getName(), room.getName());
	}

	public void sendChatMessage(ID toID, ID threadID, Type type,
			String subject, String body, Map properties) throws ECFException {
		sendChatMessage(toID, body);
	}

	public void sendChatMessage(ID toID, String body) throws ECFException {
		if (toID == null) {
			throw new ECFException(
					Messages.IRCRootContainer_EXCEPTION_TARGETID_CANNOT_BE_NULL);
		}

		// FIXME: temporary workaround to allow for the sending of messages to
		// users that are operators
		String name = toID.getName();
		if (name.charAt(0) == '@') {
			name = name.substring(1);
		}
		connection.doPrivmsg(name, body);
	}

	public IChatMessageSender getPrivateMessageSender() {
		return this;
	}

	public ID[] getChatRoomParticipants() {
		// root channel has no participants
		return new ID[0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ecf.presence.chatroom.IChatRoomContainer#getChatRoomAdminSender
	 * ()
	 */
	public IChatRoomAdminSender getChatRoomAdminSender() {
		return null;
	}

}

Back to the top