1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package pcuserveur.lib;
21
22 import java.io.BufferedReader;
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.OutputStreamWriter;
27 import java.net.Socket;
28 import java.net.UnknownHostException;
29 import java.security.NoSuchAlgorithmException;
30 import java.sql.SQLException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.Map.Entry;
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 public class GestionnaireConnexion implements Vide , Runnable {
85
86
87
88
89
90
91
92
93
94
95
96
97 public static final char CR = '\r';
98
99
100
101
102
103
104
105 private static final String CRLF = GestionnaireConnexion.CR + "" + GestionnaireConnexion.LF;
106
107
108
109
110
111 private static final char LF = '\n';
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 private final Utilisateur client = new Utilisateur ();
131
132
133
134
135
136
137
138
139
140
141 private BufferedReader entree;
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 private final Set<Etat> etats = new HashSet<Etat> ();
167
168
169
170
171
172
173
174
175
176
177
178 private boolean lance = true;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 private final HashMap<String, Canal> listeCanaux = new HashMap<String, Canal> ();
194
195
196
197
198
199
200
201
202
203 private final ArrayList<String> listeNomAccepte = new ArrayList<String> ();
204
205
206
207
208
209
210
211
212
213 private final ArrayList<String> listeNomRefuse = new ArrayList<String> ();
214
215
216
217
218
219
220
221
222
223
224 private int numeroGC;
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 private Serveur serveur;
250
251
252
253
254
255
256
257
258
259
260
261
262
263 private Socket socket;
264
265
266
267
268
269
270
271
272
273
274
275
276 private BufferedWriter sortie;
277
278
279
280
281
282
283 public GestionnaireConnexion () {
284
285 }
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 private synchronized List<String> analyseMessage (final String message) throws CommandeIncorrecteException, IOException {
304 final List<String> res = Collections.synchronizedList (new ArrayList<String> ());
305 final StringBuffer tmp = new StringBuffer ();
306 boolean b = false;
307 int i = 0, nbg = 0;
308
309 if (message == null) {
310 throw new IOException ();
311 }
312
313 while (i < message.length ()) {
314 if (message.charAt (i) == '"') {
315 nbg++;
316 }
317 i++;
318 }
319
320 if (nbg % 2 != 0) {
321 throw new CommandeIncorrecteException ();
322 }
323
324 i = 0;
325
326
327 while (i < message.length ()) {
328
329 if (message.charAt (i) == '"') {
330 b = !b;
331 i++;
332 }
333
334
335 if (b) {
336 while ((i < message.length ()) && (message.charAt (i) != '"')) {
337 tmp.append (message.charAt (i));
338 i++;
339 }
340 res.add (tmp.toString ());
341 tmp.delete (0, tmp.length ());
342 }
343 else {
344
345 while ((i < message.length ()) && (message.charAt (i) == ' ')) {
346 i++;
347 }
348
349
350 while ((i < message.length ()) && (message.charAt (i) != ' ') && (message.charAt (i) != '"')) {
351 tmp.append (message.charAt (i));
352 i++;
353 }
354
355 if (!tmp.toString ().equals ("")) {
356 res.add (tmp.toString ());
357 }
358 tmp.delete (0, tmp.length ());
359 }
360 }
361 if (res.size () == 0) {
362 throw new CommandeIncorrecteException ();
363 }
364
365 return res;
366 }
367
368
369
370
371
372
373
374
375
376
377
378
379
380 private synchronized void cnl_changerSujet (final String nom, final String sujet) throws IOException {
381 String reponse = "";
382
383 if (this.etats.contains (Etat.authentifie)) {
384 if (this.client.estAdministrateur ()) {
385 try {
386 this.serveur.changerSujetCanal (nom, sujet);
387
388 reponse = "+OK_CNL_CHANGERSUJET " + nom + " \"" + sujet + "\"";
389 }
390 catch (final CanalInexistantException e) {
391 reponse = "-ERR_CANALINTROUVABLE";
392 }
393 reponse += " " + nom;
394 }
395 else {
396 reponse = "-ERR_NONADMINISTRATEUR";
397 }
398 }
399 else {
400 reponse = "-ERR_ETAT";
401 }
402 this.envoyer (reponse);
403 }
404
405
406
407
408
409
410
411
412
413
414
415
416
417 private synchronized void cnl_creer (final String nom, final String sujet) throws IOException {
418 String reponse = "";
419 boolean nbc = false;
420
421 if (this.etats.contains (Etat.authentifie)) {
422 if (this.client.estAdministrateur ()) {
423 try {
424 this.serveur.creerCanal (nom, sujet);
425
426 reponse = "+OK_CNL_CREER " + nom + " \"" + sujet + "\"";
427 }
428 catch (final CanalExistantException e) {
429 reponse = "-ERR_CANALEXISTANT";
430 }
431 catch (final NbCanauxMaxException e1) {
432 nbc = true;
433 }
434 }
435 else {
436 reponse = "-ERR_NONADMINISTRATEUR";
437 }
438 }
439 else {
440 reponse = "-ERR_ETAT";
441 }
442 if (nbc) {
443 reponse = "-ERR_NBCANAUXMAX";
444 }
445 this.envoyer (reponse);
446 }
447
448
449
450
451
452
453
454
455
456
457
458 private synchronized void cnl_supprimer (final String nom) throws IOException {
459 String reponse = "";
460
461 if (this.etats.contains (Etat.authentifie)) {
462 if (this.client.estAdministrateur ()) {
463 try {
464 this.serveur.supprimerCanal (nom);
465
466 reponse = "+OK_CNL_SUPPRIMER";
467 }
468 catch (final CanalInexistantException e) {
469 reponse = "-ERR_CANALINTROUVABLE";
470 }
471 reponse += " " + nom;
472 }
473 else {
474 reponse = "-ERR_NONADMINISTRATEUR";
475 }
476 }
477 else {
478 reponse = "-ERR_ETAT";
479 }
480 this.envoyer (reponse);
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495 public synchronized void connexionDirecte (final String login, final String ip) throws IOException {
496 String reponse = "";
497
498 if (this.etats.contains (Etat.authentifie)) {
499 try {
500 if (!this.listeNomAccepte.contains (login)) {
501 if (this.listeNomRefuse.contains (login)) {
502 this.listeNomRefuse.remove (login);
503 }
504 GestionnaireConnexion gc = new GestionnaireConnexion ();
505
506 gc = this.serveur.connexion_directe (login);
507
508 gc.envoyer ("CONNEXIONDIRECTE " + this.client.getLogin () + " " + ip);
509
510 this.listeNomAccepte.add (login);
511
512 gc = null;
513 }
514 }
515 catch (final LoginIntrouvableException e) {
516 reponse = "-ERR_LOGININTROUVABLE " + login;
517 }
518 }
519 else {
520 reponse = "-ERR_ETAT";
521 }
522 this.envoyer (reponse);
523 }
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539 public synchronized void connexionDirecteAccepte (final String login, final String ip, final String port_connexion) throws IOException {
540 String reponse = "";
541
542 if (this.etats.contains (Etat.authentifie)) {
543 try {
544 if (!this.listeNomAccepte.contains (login)) {
545 if (this.listeNomRefuse.contains (login)) {
546 this.listeNomRefuse.remove (login);
547 }
548 this.listeNomAccepte.add (login);
549 this.etats.add (Etat.connexion_directe);
550
551 GestionnaireConnexion gc = new GestionnaireConnexion ();
552
553 gc = this.serveur.connexion_directe_accepte (login);
554
555 gc.envoyer ("+OK_CONNEXIONDIRECTE " + ip + " " + port_connexion);
556
557 gc = null;
558 }
559 }
560 catch (final LoginIntrouvableException e) {
561 reponse = "-ERR_LOGININTROUVABLE " + login;
562 }
563 }
564 else {
565 reponse = "-ERR_ETAT";
566 }
567 this.envoyer (reponse);
568 }
569
570
571
572
573
574
575
576
577
578
579
580 public synchronized void connexionDirecteRefuse (final String login) throws IOException {
581 String reponse = "";
582
583 if (this.etats.contains (Etat.authentifie)) {
584 try {
585 if (!this.listeNomRefuse.contains (login)) {
586 this.listeNomRefuse.add (login);
587 this.etats.add (Etat.connexion_directe);
588
589 GestionnaireConnexion gc = new GestionnaireConnexion ();
590
591 gc = this.serveur.connexion_directe_refuse (login);
592
593 gc.envoyer ("+KO_CONNEXIONDIRECTE " + login);
594 gc.enleverNom (login);
595
596 gc = null;
597 }
598 }
599 catch (final LoginIntrouvableException e) {
600 reponse = "-ERR_LOGININTROUVABLE " + login;
601 }
602 }
603 else {
604 reponse = "-ERR_ETAT";
605 }
606 this.envoyer (reponse);
607 }
608
609
610
611
612
613
614
615
616
617 public void enleverCanal (final String nom_canal) {
618 this.listeCanaux.remove (nom_canal);
619 }
620
621
622
623
624
625
626
627
628
629 public void enleverNom (final String login) {
630 this.listeNomAccepte.remove (login);
631 }
632
633
634
635
636
637
638
639
640
641
642
643 public synchronized void envoyer (final String message) throws IOException {
644 this.sortie.write (message + GestionnaireConnexion.CRLF);
645 this.sortie.flush ();
646 }
647
648
649
650
651
652
653
654
655
656 public synchronized boolean estVide () {
657 return this.socket == null;
658 }
659
660
661
662
663
664
665
666
667 public synchronized Utilisateur getClient () {
668 return this.client;
669 }
670
671
672
673
674
675
676
677
678 public String getIp () {
679 return this.socket.getInetAddress ().toString ().substring (1);
680 }
681
682
683
684
685
686
687
688
689
690
691
692
693
694 private synchronized void identifier (final String login, final String mdp) throws IOException {
695 String reponse = "";
696
697 if (this.etats.contains (Etat.non_authentifie)) {
698 try {
699 this.serveur.verifierLoginMdp (login, mdp, this.client);
700 this.etats.remove (Etat.non_authentifie);
701 this.etats.add (Etat.authentifie);
702 reponse = "+OK_IDENTIFIER " + login + " " + (this.client.estAdministrateur () ? "admin" : "nonadmin");
703 }
704 catch (final SQLException e) {
705 Serveur.logServeur.logMessage ("Erreur avec la base de donnée");
706 Serveur.logServeur.logException ("GestionnaireConnexion", "identifier", e);
707 reponse = "-ERR_INTERNE";
708 }
709 catch (final IdentifierException e) {
710 reponse = "-ERR_ECHECIDENT";
711 Serveur.logCoDeco.logMessage (login + " n'a pas pu se connecté");
712 }
713 catch (final NoSuchAlgorithmException e) {
714 Serveur.logServeur.logMessage ("Algorithme non trouvé");
715 Serveur.logServeur.logException ("GestionnaireConnexion", "identifier", e);
716 reponse = "-ERR_INTERNE";
717 }
718 }
719 else {
720 reponse = "-ERR_ETAT";
721 }
722 this.envoyer (reponse);
723 }
724
725
726
727
728
729
730
731
732 public String infoCanaux () {
733 final StringBuffer res = new StringBuffer ();
734 final int i = 0;
735
736 for (final Entry<String, Canal> entry:this.listeCanaux.entrySet ()) {
737 final Canal valeur = entry.getValue ();
738
739 res.append (valeur.getNom ());
740 if (i != this.listeCanaux.size () - 1) {
741 res.append (" ");
742 }
743 }
744
745 return res.toString ();
746 }
747
748
749
750
751
752
753
754
755
756
757
758
759 private synchronized void infoMembre (final String login) throws IOException {
760 String reponse = "";
761
762 if (this.etats.contains (Etat.authentifie)) {
763 try {
764 reponse = "+OK_INFOMEMBRE " + this.serveur.informationMembre (login);
765 }
766 catch (final LoginIntrouvableException e) {
767 reponse = "-ERR_LOGININTROUVABLE " + login;
768 }
769 }
770 else {
771 reponse = "-ERR_ETAT";
772 }
773 this.envoyer (reponse);
774 }
775
776
777
778
779
780
781
782
783
784
785
786
787
788 public synchronized void initialisationGestionnaire (final Socket socket1, final Serveur serveur1, final int numeroGC1) {
789 this.socket = socket1;
790 this.serveur = serveur1;
791 this.numeroGC = numeroGC1;
792 this.lance = true;
793
794 try {
795 this.etats.add (Etat.non_authentifie);
796 this.sortie = new BufferedWriter (new OutputStreamWriter (this.socket.getOutputStream (), "UTF-8"));
797 this.entree = new BufferedReader (new InputStreamReader (this.socket.getInputStream (), "UTF-8"));
798 }
799 catch (final UnknownHostException e) {
800 Serveur.logServeur.logMessage ("Hote inconnu");
801 Serveur.logServeur.logException ("GestionnaireConnexion", "initialisationGestionnaire", e);
802 }
803 catch (final IOException e) {
804 Serveur.logServeur.logMessage ("Erreur E/S lors de la mise en service du socket");
805 Serveur.logServeur.logException ("GestionnaireConnexion", "initialisationGestionnaire", e);
806 }
807 catch (final NullPointerException e) {
808 Serveur.logServeur.logMessage ("Ajout non authorise d'un etat");
809 Serveur.logServeur.logException ("GestionnaireConnexion", "initialisationGestionnaire", e);
810 }
811 catch (final ClassCastException e) {
812 Serveur.logServeur.logMessage ("Ajout d'un etat qui existe deja");
813 Serveur.logServeur.logException ("GestionnaireConnexion", "initialisationGestionnaire", e);
814 }
815 }
816
817
818
819
820
821
822
823
824
825
826
827
828
829 public synchronized void lister (final String nomCanal) throws IOException {
830 String reponse = "";
831
832 if (this.etats.contains (Etat.authentifie)) {
833 try {
834 reponse = "+OK_LISTER" + GestionnaireConnexion.CR + this.serveur.listerCanaux (nomCanal);
835 }
836 catch (final CanalInexistantException e) {
837 reponse = "-ERR_CANALINTROUVABLE " + nomCanal;
838 }
839 }
840 else {
841 reponse = "-ERR_ETAT";
842 }
843 this.envoyer (reponse);
844 }
845
846
847
848
849
850
851
852
853
854
855
856 public synchronized void membre (final String nom_canal) throws IOException {
857 String reponse = "";
858
859 if (this.etats.contains (Etat.authentifie)) {
860 final int tmp = this.serveur.canalExistant (nom_canal);
861
862 if (tmp != -1) {
863 reponse = "+OK_MEMBRE " + nom_canal + GestionnaireConnexion.CR + this.listeCanaux.get (nom_canal).listerMembre ();
864 }
865 else {
866 reponse = "-ERR_CANALINTROUVABLE " + nom_canal;
867 }
868 }
869 else {
870 reponse = "-ERR_ETAT";
871 }
872 this.envoyer (reponse);
873 }
874
875
876
877
878
879
880
881
882
883
884
885
886
887 private synchronized void message (final String nom_canal, final String message) throws IOException {
888 String reponse = "";
889
890 if (this.etats.contains (Etat.authentifie) && this.etats.contains (Etat.connexion_canal)) {
891 if (this.serveur.canalExistant (nom_canal) != -1) {
892 if (this.listeCanaux.containsKey (nom_canal)) {
893 this.listeCanaux.get (nom_canal).notifier (Evenement.PARLE, this.client.getLogin (), message);
894 reponse = "+OK_MESSAGE";
895 }
896 else {
897 reponse = "-ERR_NONCONNECTE";
898 }
899 }
900 else {
901 reponse = "-ERR_CANALINTROUVABLE";
902 }
903 reponse += " " + nom_canal;
904 }
905 else {
906 reponse = "-ERR_ETAT";
907 }
908 this.envoyer (reponse);
909 }
910
911
912
913
914
915
916
917
918
919 private synchronized void motd () throws IOException {
920 String reponse = this.serveur.requeteMOTD ();
921
922 if (reponse.compareTo ("") == 0) {
923 reponse = "-ERR_INTERNE";
924 }
925 else {
926 reponse = "+OK_MOTD \"" + reponse + "\"";
927 }
928 this.envoyer (reponse);
929 }
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948 public void notifier (final String nom_canal, final Evenement evenement, final String login, final String message) throws IOException {
949 String res = "NOTIFIER " + nom_canal + " " + evenement.toString ();
950
951 switch (evenement) {
952 case REJOINT:
953 case PARTI:
954 case EJECTE:
955 res += " " + login;
956 break;
957 case BANNI:
958 res += " " + login;
959 break;
960 case PARLE:
961 res += " " + login + " \"" + message + "\"";
962 break;
963
964 case SUJETCHANGE:
965 res += " " + "\"" + message + "\"";
966 break;
967
968 case SUPPRIME:
969 break;
970 }
971 this.envoyer (res);
972 }
973
974
975
976
977
978
979
980
981
982 public synchronized void quitter (final String msg) {
983
984 if (this.etats.contains (Etat.connexion_canal)) {
985 for (final Entry<String, Canal> entry:this.listeCanaux.entrySet ()) {
986 final Canal valeur = entry.getValue ();
987
988 try {
989 valeur.notifier (Evenement.PARLE, this.client.getLogin (), msg);
990 valeur.enleverGC (this);
991 }
992 catch (final NonConnecteException e) {
993 Serveur.logCoDeco.logMessage (this.client.getLogin () + " n'est pas connecté au canal");
994 Serveur.logCoDeco.logException ("GestionnaireException", "quitter", e);
995 }
996 catch (final IOException e) {
997 Serveur.logServeur.logMessage ("Erreur d'entrée sortie");
998 Serveur.logServeur.logException ("GestionnaireException", "quitter", e);
999 }
1000 }
1001 }
1002
1003
1004 if (this.etats.contains (Etat.connexion_directe)) {
1005 for (int i = 0;i < this.listeNomAccepte.size ();i++) {
1006 GestionnaireConnexion gc = new GestionnaireConnexion ();
1007
1008 try {
1009 gc = this.serveur.connexion_directe (this.listeNomAccepte.get (i));
1010
1011 gc.listeNomAccepte.remove (this.client.getLogin ());
1012 gc = null;
1013 }
1014 catch (final LoginIntrouvableException e) {
1015 Serveur.logCoDeco.logMessage (this.client.getLogin () + " n'est pas connecté");
1016 Serveur.logCoDeco.logException ("GestionnaireException", "quitter", e);
1017 }
1018 }
1019 }
1020
1021
1022 if (this.serveur != null) {
1023 this.serveur.deconnexion (this.numeroGC);
1024 }
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 private synchronized void quitterCanal (final String nom_canal) throws IOException {
1038 String reponse = "";
1039
1040 if (this.etats.contains (Etat.authentifie) && this.etats.contains (Etat.connexion_canal)) {
1041 final int tmp = this.serveur.canalExistant (nom_canal);
1042
1043 if (tmp != -1) {
1044 if (!this.listeCanaux.containsKey (nom_canal)) {
1045 reponse = "-ERR_NONCONNECTE " + nom_canal;
1046 }
1047 else {
1048 try {
1049 this.listeCanaux.get (nom_canal).enleverGC (this);
1050
1051 reponse = "+OK_QUITTERCANAL " + nom_canal;
1052 }
1053 catch (final NonConnecteException e) {
1054 reponse = "-ERR_NONCONNECTE " + nom_canal;
1055 }
1056 this.enleverCanal (nom_canal);
1057 if (this.listeCanaux.isEmpty ()) {
1058 this.etats.remove (Etat.connexion_canal);
1059 }
1060 }
1061 }
1062 else {
1063 reponse = "-ERR_CANALINTROUVABLE " + nom_canal;
1064 }
1065 }
1066 else {
1067 reponse = "-ERR_ETAT";
1068 }
1069 this.envoyer (reponse);
1070 }
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082 private synchronized void rejoindre (final String nom_canal) throws IOException {
1083 String reponse = "";
1084
1085 if (this.etats.contains (Etat.authentifie)) {
1086 final int tmp = this.serveur.canalExistant (nom_canal);
1087
1088 if (tmp != -1) {
1089 try {
1090 this.listeCanaux.put (nom_canal, this.serveur.getListeCanaux ().get (tmp));
1091 this.listeCanaux.get (nom_canal).ajouterGC (this);
1092 if (!this.etats.contains (Etat.connexion_canal)) {
1093 this.etats.add (Etat.connexion_canal);
1094 }
1095
1096 reponse = "+OK_REJOINDRE " + nom_canal;
1097 }
1098 catch (final DejaConnecteException e) {
1099 reponse = "-ERR_DEJACONNECTE " + nom_canal;
1100 }
1101 }
1102 else {
1103 reponse = "-ERR_CANALINTROUVABLE " + nom_canal;
1104 }
1105 }
1106 else {
1107 reponse = "-ERR_ETAT";
1108 }
1109 this.envoyer (reponse);
1110 }
1111
1112
1113
1114
1115
1116
1117 @Override
1118 public void run () {
1119 String message = "";
1120 List<String> retour = Collections.synchronizedList (new ArrayList<String> ());
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130 while (this.lance) {
1131 boolean err_syntaxe = false;
1132 String cmd = "";
1133 int size = 0;
1134
1135 try {
1136 message = this.entree.readLine ();
1137 if (message != null) {
1138 retour = this.analyseMessage (message);
1139 }
1140 }
1141 catch (final IOException e1) {
1142 if (this.lance) {
1143 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1144 }
1145 break;
1146 }
1147 catch (final CommandeIncorrecteException e) {
1148 try {
1149 this.envoyer ("-ERR_SYNTAXE");
1150 }
1151 catch (final IOException e1) {
1152 Serveur.logServeur.logMessage ("Erreur d'entrée sortie");
1153 Serveur.logServeur.logException ("GestionnaireConnexion", "run", e);
1154 break;
1155 }
1156 continue;
1157 }
1158 size = retour.size ();
1159 if (size != 0) {
1160 cmd = retour.get (0);
1161 }
1162 else {
1163 break;
1164 }
1165
1166
1167 if (0 == cmd.compareToIgnoreCase ("IDENTIFIER")) {
1168 if (size == 3) {
1169 try {
1170 this.identifier (retour.get (1), retour.get (2));
1171 }
1172 catch (final IOException e) {
1173 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1174 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1175 break;
1176 }
1177 }
1178 else {
1179 err_syntaxe = true;
1180 }
1181 }
1182 else if (0 == cmd.compareToIgnoreCase ("QUITTER")) {
1183 if (size == 1) {
1184 this.quitter ("");
1185 this.lance = false;
1186 }
1187 else if (size == 2) {
1188 this.quitter (retour.get (1));
1189 this.lance = false;
1190 }
1191 else {
1192 err_syntaxe = true;
1193 }
1194 }
1195 else if (0 == cmd.compareToIgnoreCase ("REJOINDRE")) {
1196 if (size == 2) {
1197 try {
1198 this.rejoindre (retour.get (1));
1199 }
1200 catch (final IOException e) {
1201 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1202 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1203 break;
1204 }
1205 }
1206 else {
1207 err_syntaxe = true;
1208 }
1209 }
1210 else if (0 == cmd.compareToIgnoreCase ("MESSAGE")) {
1211 if (size == 3) {
1212 try {
1213 this.message (retour.get (1), retour.get (2));
1214 }
1215 catch (final IOException e) {
1216 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1217 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1218 break;
1219 }
1220 }
1221 else {
1222 err_syntaxe = true;
1223 }
1224 }
1225 else if (0 == cmd.compareToIgnoreCase ("QUITTERCANAL")) {
1226 if (size == 2) {
1227 try {
1228 this.quitterCanal (retour.get (1));
1229 }
1230 catch (final IOException e) {
1231 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1232 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1233 break;
1234 }
1235 }
1236 else {
1237 err_syntaxe = true;
1238 }
1239 }
1240 else if (0 == cmd.compareToIgnoreCase ("MEMBRE")) {
1241 if (size == 2) {
1242 try {
1243 this.membre (retour.get (1));
1244 }
1245 catch (final IOException e) {
1246 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1247 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1248 break;
1249 }
1250 }
1251 else {
1252 err_syntaxe = true;
1253 }
1254 }
1255
1256
1257 else if (0 == cmd.compareToIgnoreCase ("CNL_CREER")) {
1258 if (size == 3) {
1259 try {
1260 this.cnl_creer (retour.get (1), retour.get (2));
1261 }
1262 catch (final IOException e) {
1263 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1264 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1265 break;
1266 }
1267 }
1268 else {
1269 err_syntaxe = true;
1270 }
1271 }
1272 else if (0 == cmd.compareToIgnoreCase ("CNL_SUPPRIMER")) {
1273 if (size == 2) {
1274 try {
1275 this.cnl_supprimer (retour.get (1));
1276 }
1277 catch (final IOException e) {
1278 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1279 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1280 break;
1281 }
1282 }
1283 else {
1284 err_syntaxe = true;
1285 }
1286 }
1287 else if (0 == cmd.compareToIgnoreCase ("CNL_CHANGERSUJET")) {
1288 if (size == 3) {
1289 try {
1290 this.cnl_changerSujet (retour.get (1), retour.get (2));
1291 }
1292 catch (final IOException e) {
1293 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1294 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1295 break;
1296 }
1297 }
1298 else {
1299 err_syntaxe = true;
1300 }
1301 }
1302 else if (0 == cmd.compareToIgnoreCase ("UTL_EJECTER")) {
1303 if (size == 3) {
1304 try {
1305 this.utl_ejecter (retour.get (1), retour.get (2));
1306 }
1307 catch (final IOException e) {
1308 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1309 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1310 break;
1311 }
1312 }
1313 else {
1314 err_syntaxe = true;
1315 }
1316 }
1317 else if (0 == cmd.compareToIgnoreCase ("SRV_RELANCER")) {
1318 try {
1319 if (size == 2) {
1320 this.srv_relancer (retour.get (1));
1321 }
1322 else if (retour.size () == 1) {
1323 this.srv_relancer ("");
1324 }
1325 else {
1326 err_syntaxe = true;
1327 }
1328 }
1329 catch (final IOException e) {
1330 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1331 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1332 break;
1333 }
1334 }
1335 else if (0 == cmd.compareToIgnoreCase ("SRV_STOPPER")) {
1336 try {
1337 if (size == 2) {
1338 this.srv_stopper (retour.get (1));
1339 }
1340 else if (size == 1) {
1341 this.srv_stopper ("");
1342 }
1343 else {
1344 err_syntaxe = true;
1345 }
1346 }
1347 catch (final IOException e) {
1348 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1349 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1350 break;
1351 }
1352 }
1353 else if (0 == cmd.compareToIgnoreCase ("SRV_LISTER")) {
1354 if (size == 1) {
1355 try {
1356 this.srv_lister ();
1357 }
1358 catch (final IOException e) {
1359 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1360 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1361 break;
1362 }
1363 }
1364 else {
1365 err_syntaxe = true;
1366 }
1367 }
1368 else if (0 == cmd.compareToIgnoreCase ("UTL_BANNIR")) {
1369 if (size == 2) {
1370 try {
1371 this.utl_bannir (retour.get (1));
1372 }
1373 catch (final IOException e) {
1374 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1375 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1376 break;
1377 }
1378 }
1379 else {
1380 err_syntaxe = true;
1381 }
1382 }
1383 else if (0 == cmd.compareToIgnoreCase ("LISTER")) {
1384 try {
1385 if (size == 2) {
1386 this.lister (retour.get (1));
1387 }
1388 else if (size == 1) {
1389 this.lister ("");
1390 }
1391 else {
1392 err_syntaxe = true;
1393 }
1394 }
1395 catch (final IOException e) {
1396 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1397 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1398 break;
1399 }
1400 }
1401 else if (0 == cmd.compareToIgnoreCase ("INFOMEMBRE")) {
1402 if (retour.size () == 2) {
1403 try {
1404 this.infoMembre (retour.get (1));
1405 }
1406 catch (final IOException e) {
1407 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1408 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1409 break;
1410 }
1411 }
1412 else {
1413 err_syntaxe = true;
1414 }
1415 }
1416 else if (0 == cmd.compareToIgnoreCase ("MOTD")) {
1417 if (retour.size () == 1) {
1418 try {
1419 this.motd ();
1420 }
1421 catch (final IOException e) {
1422 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1423 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1424 break;
1425 }
1426 }
1427 else {
1428 err_syntaxe = true;
1429 }
1430 }
1431 else if (0 == cmd.compareToIgnoreCase ("CONNEXIONDIRECTE")) {
1432 if (retour.size () == 3) {
1433 try {
1434 this.connexionDirecte (retour.get (1), retour.get (2));
1435 }
1436 catch (final IOException e) {
1437 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1438 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1439 break;
1440 }
1441 }
1442 else {
1443 err_syntaxe = true;
1444 }
1445 }
1446 else if (0 == cmd.compareToIgnoreCase ("+OK_CONNEXIONDIRECTE")) {
1447 if (retour.size () == 4) {
1448 try {
1449 this.connexionDirecteAccepte (retour.get (1), retour.get (2), retour.get (3));
1450 }
1451 catch (final IOException e) {
1452 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1453 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1454 break;
1455 }
1456 }
1457 else {
1458 err_syntaxe = true;
1459 }
1460 }
1461 else if (0 == cmd.compareToIgnoreCase ("+KO_CONNEXIONDIRECTE")) {
1462 if (retour.size () == 2) {
1463 try {
1464 this.connexionDirecteRefuse (retour.get (1));
1465 }
1466 catch (final IOException e) {
1467 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1468 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1469 break;
1470 }
1471 }
1472 else {
1473 err_syntaxe = true;
1474 }
1475 }
1476 else {
1477 try {
1478 this.envoyer ("-ERR_CMDINCONNUE " + cmd);
1479 }
1480 catch (final IOException e) {
1481 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1482 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1483 break;
1484 }
1485 }
1486 if (err_syntaxe) {
1487 try {
1488 this.envoyer ("-ERR_SYNTAXE");
1489 this.sortie.flush ();
1490 }
1491 catch (final IOException e) {
1492 Serveur.logCoDeco.logMessage ("Déconnexion sauvage de " + this.client.getLogin ());
1493 this.quitter ("déconnexion sauvage de " + this.client.getLogin ());
1494 break;
1495 }
1496 }
1497 }
1498 }
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508 public void setLance (final boolean lance1) {
1509 this.lance = lance1;
1510 }
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520 public synchronized void srv_lister () throws IOException {
1521 String rep = "";
1522
1523 if (this.etats.contains (Etat.authentifie)) {
1524 if (this.client.estAdministrateur ()) {
1525 rep = "+OK_SRV_LISTER" + GestionnaireConnexion.CR + this.serveur.listerServeur ();
1526
1527 this.envoyer (rep);
1528 }
1529 else {
1530 this.envoyer ("-ERR_NONADMINISTRATEUR");
1531 }
1532 }
1533 else {
1534 this.envoyer ("-ERR_ETAT");
1535 }
1536 }
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549 private synchronized void srv_relancer (final String s) throws IOException {
1550 if (this.etats.contains (Etat.authentifie)) {
1551 if (this.client.estAdministrateur ()) {
1552 this.serveur.relancerServeur (s);
1553 }
1554 else {
1555 this.envoyer ("-ERR_NONADMINISTRATEUR");
1556 }
1557 }
1558 else {
1559 this.envoyer ("-ERR_ETAT");
1560 }
1561 }
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574 private synchronized void srv_stopper (final String s) throws IOException {
1575 if (this.etats.contains (Etat.authentifie)) {
1576 if (this.client.estAdministrateur ()) {
1577 this.serveur.stopperServeur (s);
1578 }
1579 else {
1580 this.envoyer ("-ERR_NONADMINISTRATEUR");
1581 }
1582 }
1583 else {
1584 this.envoyer ("-ERR_ETAT");
1585 }
1586 }
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598 public void stopper (final String message) {
1599 try {
1600 if (message.equals ("")) {
1601 this.envoyer ("FINCONNEXION");
1602 }
1603 else {
1604 this.envoyer ("FINCONNEXION " + message);
1605 }
1606 }
1607 catch (final IOException e) {
1608 Serveur.logServeur.logMessage ("Erreur d'entrée sortie");
1609 Serveur.logServeur.logException ("GestionnaireConnexion", "run", e);
1610 }
1611 this.vider ();
1612 }
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624 public synchronized void utl_bannir (final String login) throws IOException {
1625 String reponse = "";
1626
1627 if (this.etats.contains (Etat.authentifie)) {
1628 if (this.client.estAdministrateur ()) {
1629 try {
1630 this.serveur.bannirUtilisateur (login);
1631
1632 reponse = "+OK_UTL_BANNIR " + login;
1633 }
1634 catch (final LoginIntrouvableException e1) {
1635 reponse = "-ERR_LOGININTROUVABLE " + login;
1636 }
1637 catch (NonConnecteException e) {
1638 reponse = "-ERR_NONCONNECTE " + login;
1639 }
1640 }
1641 else {
1642 reponse = "-ERR_NONADMINISTRATEUR";
1643 }
1644 }
1645 else {
1646 reponse = "-ERR_ETAT";
1647 }
1648 this.envoyer (reponse);
1649 }
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663 private synchronized void utl_ejecter (final String login, final String nom_canal) throws IOException {
1664 String reponse = "";
1665
1666 if (this.etats.contains (Etat.authentifie)) {
1667 if (this.client.estAdministrateur ()) {
1668 try {
1669 this.serveur.ejecterUtilisateur (login, nom_canal);
1670
1671 reponse = "+OK_UTL_EJECTER " + login + " " + nom_canal;
1672 }
1673 catch (final CanalInexistantException e) {
1674 reponse = "-ERR_CANALINTROUVABLE " + nom_canal;
1675 }
1676 catch (final LoginIntrouvableException e1) {
1677 reponse = "-ERR_LOGININTROUVABLE " + login;
1678 }
1679 catch (final NonConnecteException e2) {
1680 reponse = "-ERR_NONCONNECTE " + login + " " + nom_canal;
1681 }
1682 }
1683 else {
1684 reponse = "-ERR_NONADMINISTRATEUR";
1685 }
1686 }
1687 else {
1688 reponse = "-ERR_ETAT";
1689 }
1690 this.envoyer (reponse);
1691 }
1692
1693
1694
1695
1696
1697
1698
1699 public synchronized void vider () {
1700 try {
1701 this.sortie.close ();
1702 this.entree.close ();
1703 this.socket.close ();
1704 }
1705 catch (final IOException e) {
1706 Serveur.logServeur.logMessage ("Erreur d'entrée sortie");
1707 Serveur.logServeur.logException ("GestionnaireConnexion", "run", e);
1708 }
1709
1710 this.socket = null;
1711 this.serveur = null;
1712
1713 this.client.vider ();
1714 this.etats.clear ();
1715 this.listeCanaux.clear ();
1716
1717 this.numeroGC = 0;
1718 this.lance = false;
1719 this.sortie = null;
1720 this.entree = null;
1721 }
1722 }