View Javadoc

1   /***
2    * 
3    */
4   package hu.elte.tribus.services;
5   
6   import hu.elte.tribus.interfaces.AnsverFinder;
7   import hu.elte.tribus.interfaces.ConversationDao;
8   import hu.elte.tribus.interfaces.OnlineUserBean;
9   import hu.elte.tribus.interfaces.QaDao;
10  import hu.elte.tribus.interfaces.UserDao;
11  import hu.elte.tribus.interfaces.UserTracker;
12  import hu.elte.tribus.model.Conversation;
13  import hu.elte.tribus.model.QA;
14  import hu.elte.tribus.model.Topic;
15  import hu.elte.tribus.model.User;
16  
17  import java.util.Date;
18  import java.util.List;
19  
20  import org.apache.log4j.Logger;
21  import org.schwering.irc.lib.IRCConnection;
22  import org.schwering.irc.lib.IRCEventListener;
23  import org.schwering.irc.lib.IRCModeParser;
24  import org.schwering.irc.lib.IRCUser;
25  
26  /***
27   * @author kocka
28   *
29   */
30  public class EuniceIrcEventListener implements IRCEventListener {
31  
32  	private IRCConnection connection; 
33  
34  	private UserTracker tracker;
35  
36  	private AnsverFinder ansverFinder;
37  
38  	private UserDao userDao;
39  
40  	private QaDao qaDao;
41  
42  	private ConversationDao conversationDao;
43  	
44  	private final static Logger logger = Logger.getLogger(EuniceIrcEventListener.class);
45  	
46  	public void onDisconnected() {
47  		logger.error("disconnected");
48  	}
49  
50  	public void onError(String arg0) {
51  		logger.error("irc error: " +arg0);
52  	}
53  
54  	public void onError(int arg0, String arg1) {
55  		logger.error("irc error: "+arg0+" "+arg1);
56  	}
57  
58  	public void onInvite(String arg0, IRCUser user, String arg2) {
59  		// TODO Auto-generated method stub
60  		
61  	}
62  
63  	public void onJoin(final String chan, final IRCUser user) {
64  				logger.info("join channel:"+chan+" by user"+user.getNick());
65  				User regUser = userDao.getUser(user.getNick());
66  				Topic topic = qaDao.getTopicByName(chan.replaceAll("#", ""));
67  				if(regUser!= null && topic != null) {
68  					tracker.userLoggedIn(regUser.getUsername());
69  					Conversation conversation = new Conversation();
70  					conversation.setUser(regUser);
71  					conversation.setTopic(topic);
72  					conversation.setStartdate(new Date());
73  					conversationDao.saveConversation(conversation);
74  				}
75  	}
76  
77  	public void onKick(String arg0, IRCUser arg1, String arg2, String arg3) {
78  		logger.info("kicked from channel:"+arg0+" by user"+arg1.getNick());
79  		//conversations here must be closed
80  	}
81  
82  	public void onMode(String arg0, IRCUser arg1, IRCModeParser arg2) {
83  		// TODO Auto-generated method stub
84  		
85  	}
86  
87  	public void onMode(IRCUser arg0, String arg1, String arg2) {
88  		// TODO Auto-generated method stub
89  		
90  	}
91  
92  	public void onNick(IRCUser arg0, String arg1) {
93  		// TODO Auto-generated method stub
94  		
95  	}
96  
97  	public void onNotice(String target, IRCUser user, String message) {
98  		logger.info("Notice from "+user.getNick()+": "+message);
99  		User regUser = userDao.getUser(user.getNick());
100 		
101 	}
102 
103 	public void onPart(String arg0, IRCUser arg1, String arg2) {
104 		// TODO Auto-generated method stub
105 		
106 	}
107 
108 	public void onPing(String arg0) {
109 		// TODO Auto-generated method stub
110 		
111 	}
112 
113 	Conversation getConversationForUser(IRCUser user, String channel) {
114 		String userName = user.getNick();
115 		for( OnlineUserBean userBean : (List<OnlineUserBean>)tracker.listOfOnlineUsers()){
116 			if(userName.equals(userBean.getUser().getUsername())) {
117 				if(userBean.getConversation() != null) {
118 					return userBean.getConversation();
119 				}
120 				//no conversation, start one
121 				Conversation conversation = new Conversation();
122 			}
123 		}
124 		//user not online? log him in
125 		return null;
126 	}
127 
128 	public void onPrivmsg(final String target, final IRCUser user, final String message) {
129 				//if echoes, dont answer :)
130 				if(connection.getNick().equals(user.getNick())) {
131 					return;
132 				}
133 				//check if user is registered
134 				User regUser = userDao.getUser(user.getNick());
135 				if(regUser == null) {
136 					connection.doPrivmsg(target, user.getNick()+": Sorry you are not yet registered in eunice.");
137 					return;
138 				}
139 				if(regUser.getStatus() != User.ENUM_STATUS_REG_ACCEPTED) {
140 					connection.doPrivmsg(target, user.getNick()+": Sorry you are not allowed to ask from eunice.");
141 					return;
142 				}
143 				if(connection.getNick().equals(target)) {
144 					logger.info("Private message from "+user.getNick()+": "+message);
145 				} else {
146 					logger.info(message);
147 				}
148 				
149 				try {
150 					QA qa  = ansverFinder.getQuestionAndAnsver(message, getConversationForUser(user, target));
151 					connection.doPrivmsg(target, qa.getAnswer());
152 				} catch (QueryException e) {
153 					logger.error("Query error", e);
154 					connection.doPrivmsg(target,"There was an error processing your request.");
155 				}
156 				return;
157 	}
158 
159 	public void onQuit(IRCUser user, String msg) {
160 		logger.info("User quited: "+user.getNick()+" - "+msg);
161 		tracker.userLoggedOut(user.getNick());
162 	}
163 
164 	public void onRegistered() {
165 		// TODO Auto-generated method stub
166 		
167 	}
168 
169 	public void onReply(int arg0, String arg1, String arg2) {
170 		// TODO Auto-generated method stub
171 		
172 	}
173 
174 	public void onTopic(String arg0, IRCUser arg1, String arg2) {
175 		// TODO Auto-generated method stub
176 		
177 	}
178 
179 	public void unknown(String arg0, String arg1, String arg2, String arg3) {
180 		// TODO Auto-generated method stub
181 		
182 	}
183 
184 }