Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Linnea Samila
/
Moya
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit fad7c9a0
authored
Jul 29, 2016
by
Tuomas Riihimäki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Irc bot logging improvements
1 parent
136ad0f2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
148 additions
and
58 deletions
code/moya-beans-client/ejbModule/fi/codecrew/moya/beans/BotBeanLocal.java
code/moya-beans/ejbModule/fi/codecrew/moya/beans/BotBean.java
code/moya-beans/ejbModule/fi/codecrew/moya/beans/moyamessage/IrcBotMoyaEventTopicListener.java
code/moya-web/src/main/java/fi/codecrew/moya/servlet/IrcServlet.java
code/moya-beans-client/ejbModule/fi/codecrew/moya/beans/BotBeanLocal.java
View file @
fad7c9a
package
fi
.
codecrew
.
moya
.
beans
;
import
java.util.Set
;
import
javax.ejb.Local
;
import
fi.iudex.utils.irc.IrcBot
;
...
...
@@ -7,6 +9,8 @@ import fi.iudex.utils.irc.IrcBot;
@Local
public
interface
BotBeanLocal
{
void
add
(
IrcBot
bot
);
public
void
addBot
(
IrcBot
bot
);
Set
<
String
>
getIgnoreTypes
();
}
code/moya-beans/ejbModule/fi/codecrew/moya/beans/BotBean.java
View file @
fad7c9a
package
fi
.
codecrew
.
moya
.
beans
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
javax.ejb.EJB
;
import
javax.ejb.LocalBean
;
import
javax.ejb.Lock
;
import
javax.ejb.LockType
;
import
javax.ejb.Singleton
;
import
javax.jms.JMSException
;
import
javax.jms.Message
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
fi.codecrew.moya.facade.EventFacade
;
import
fi.codecrew.moya.facade.EventUserFacade
;
import
fi.codecrew.moya.facade.UserFacade
;
import
fi.codecrew.moya.model.EventUser
;
import
fi.codecrew.moya.model.LanEvent
;
import
fi.codecrew.moya.model.User
;
import
fi.codecrew.moya.utilities.moyamessage.MoyaEventMessage
;
import
fi.iudex.utils.irc.IrcBot
;
/**
...
...
@@ -10,9 +28,10 @@ import fi.iudex.utils.irc.IrcBot;
*/
@Singleton
@LocalBean
@Lock
(
LockType
.
READ
)
public
class
BotBean
implements
BotBeanLocal
{
private
IrcBot
bot
;
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
BotBean
.
class
)
;
/**
* Default constructor.
...
...
@@ -20,10 +39,59 @@ public class BotBean implements BotBeanLocal {
public
BotBean
()
{
}
@Override
public
void
add
(
IrcBot
bot
)
{
this
.
setBot
(
bot
);
private
IrcBot
bot
;
@EJB
private
EventFacade
eventfacade
;
@EJB
private
UserFacade
userfacade
;
@EJB
private
EventUserFacade
eventuserfacade
;
private
final
Set
<
String
>
ignoreTypes
=
ConcurrentHashMap
.
newKeySet
();
public
void
message
(
Message
message
)
{
try
{
MoyaEventMessage
msg
=
message
.
getBody
(
MoyaEventMessage
.
class
);
// do not flood irc-channel, message only really needed ones
if
(
msg
.
getEventtype
()
!=
null
&&
getIgnoreTypes
().
contains
(
msg
.
getEventtype
()))
return
;
String
event
=
"unknown event"
;
if
(
msg
.
getEventId
()
!=
null
)
{
LanEvent
e
=
eventfacade
.
find
(
msg
.
getEventId
());
if
(
e
!=
null
)
event
=
e
.
getName
();
}
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
event
).
append
(
" "
);
sb
.
append
(
msg
.
getEventtype
()).
append
(
" "
);
if
(
msg
.
getCurrentUserId
()
!=
null
)
{
User
user
=
userfacade
.
find
(
msg
.
getCurrentUserId
());
if
(
user
!=
null
)
{
sb
.
append
(
"user: "
).
append
(
user
.
getLogin
()).
append
(
" "
);
}
}
if
(
msg
.
getEventUserId
()
!=
null
)
{
EventUser
eu
=
eventuserfacade
.
find
(
msg
.
getEventUserId
());
if
(
eu
!=
null
&&
!
eu
.
getUser
().
getId
().
equals
(
msg
.
getCurrentUserId
()))
{
sb
.
append
(
"for user: "
).
append
(
eu
.
getUser
().
getLogin
()).
append
(
": "
);
}
}
sb
.
append
(
msg
.
getDescription
());
bot
.
say
(
sb
.
toString
());
}
catch
(
JMSException
e
)
{
logger
.
warn
(
"Error sending message"
,
e
);
bot
.
say
(
"Error parsing message "
+
e
.
getMessage
());
}
}
@Override
public
Set
<
String
>
getIgnoreTypes
()
{
return
ignoreTypes
;
}
public
IrcBot
getBot
()
{
...
...
@@ -34,4 +102,10 @@ public class BotBean implements BotBeanLocal {
this
.
bot
=
bot
;
}
@Override
public
void
addBot
(
IrcBot
bot
)
{
// TODO Auto-generated method stub
}
}
code/moya-beans/ejbModule/fi/codecrew/moya/beans/moyamessage/IrcBotMoyaEventTopicListener.java
View file @
fad7c9a
...
...
@@ -31,59 +31,18 @@ public class IrcBotMoyaEventTopicListener implements MessageListener {
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
IrcBotMoyaEventTopicListener
.
class
);
private
static
List
<
MoyaEventType
>
ignoreTypes
=
Arrays
.
asList
(
MoyaEventType
.
LOGIN_SUCCESSFULL
);
@EJB
private
BotBean
botbean
;
@EJB
private
EventFacade
eventfacade
;
@EJB
private
UserFacade
userfacade
;
@EJB
private
EventUserFacade
eventuserfacade
;
/**
* @see MessageListener#onMessage(Message)
*/
public
void
onMessage
(
Message
message
)
{
try
{
MoyaEventMessage
msg
=
message
.
getBody
(
MoyaEventMessage
.
class
);
// do not flood irc-channel, message only really needed ones
if
(
msg
.
getEventtype
()
!=
null
&&
ignoreTypes
.
contains
(
msg
.
getEventtype
()))
return
;
String
event
=
"unknown event"
;
if
(
msg
.
getEventId
()
!=
null
)
{
LanEvent
e
=
eventfacade
.
find
(
msg
.
getEventId
());
if
(
e
!=
null
)
event
=
e
.
getName
();
}
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
event
).
append
(
" "
);
sb
.
append
(
msg
.
getEventtype
()).
append
(
" "
);
if
(
msg
.
getCurrentUserId
()
!=
null
)
{
User
user
=
userfacade
.
find
(
msg
.
getCurrentUserId
());
if
(
user
!=
null
)
{
sb
.
append
(
"user: "
).
append
(
user
.
getLogin
()).
append
(
" "
);
}
}
if
(
msg
.
getEventUserId
()
!=
null
)
{
EventUser
eu
=
eventuserfacade
.
find
(
msg
.
getEventUserId
());
if
(
eu
!=
null
&&
!
eu
.
getUser
().
getId
().
equals
(
msg
.
getCurrentUserId
()))
{
sb
.
append
(
"for user: "
).
append
(
eu
.
getUser
().
getLogin
()).
append
(
": "
);
}
}
sb
.
append
(
msg
.
getDescription
());
//botbean.getBot().say(toString(event, " ", msg.getEventtype(), " User ", msg.getDescription()));
botbean
.
getBot
().
say
(
sb
.
toString
());
logger
.
warn
(
"Received moya event message for irc bot {}"
,
message
);
}
catch
(
JMSException
e
)
{
logger
.
warn
(
"Exception while getting jms message for IRCbot"
,
e
);
botbean
.
getBot
().
say
(
"Caught exception while logging message"
+
e
.
getCause
());
// botbean.getBot().say(toString(event, " ", msg.getEventtype(), " User
// ", msg.getDescription()));
if
(
botbean
!=
null
)
{
botbean
.
message
(
message
);
}
}
}
code/moya-web/src/main/java/fi/codecrew/moya/servlet/IrcServlet.java
View file @
fad7c9a
...
...
@@ -40,7 +40,6 @@ import java.io.IOException;
import
java.util.ArrayList
;
import
javax.ejb.EJB
;
import
javax.ejb.Startup
;
import
javax.servlet.Servlet
;
import
javax.servlet.ServletConfig
;
import
javax.servlet.ServletException
;
...
...
@@ -49,8 +48,6 @@ import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
net.sf.jerklib.Profile
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -61,6 +58,7 @@ import fi.codecrew.moya.beans.PlaceBeanLocal;
import
fi.codecrew.moya.utilities.SystemProperty
;
import
fi.iudex.utils.irc.IrcBot
;
import
fi.iudex.utils.irc.IrcBotListener
;
import
net.sf.jerklib.Profile
;
/**
* Servlet implementation class SshServlet
...
...
@@ -105,13 +103,66 @@ public class IrcServlet extends HttpServlet {
logger
.
info
(
"Got irc server system property {}"
,
ircserver
);
if
(
ircserver
!=
null
)
{
logger
.
info
(
"Starting IRC client with server {}"
,
ircserver
);
IrcBot
bot
=
new
IrcBot
(
ircserver
,
6667
,
SystemProperty
.
MOYA_IRC_SERVERPASS
.
getValueOrNull
(),
SystemProperty
.
MOYA_IRC_CHANNEL
.
getValueOrDefault
(),
new
Profile
(
"moyabot"
,
"Moya bot"
,
"moya-bot"
));
botbean
.
add
(
bot
);
IrcBot
bot
=
new
IrcBot
(
ircserver
,
6667
,
SystemProperty
.
MOYA_IRC_SERVERPASS
.
getValueOrNull
(),
SystemProperty
.
MOYA_IRC_CHANNEL
.
getValueOrDefault
(),
new
Profile
(
"moyabot"
,
"Moya bot"
,
"moya-bot"
));
botbean
.
addBot
(
bot
);
bot
.
addCmdListener
(
"addIgnore"
,
new
MessgaeIgnoreListener
());
bot
.
addCmdListener
(
"rmIgnore"
,
new
RemoveMessgaeIgnoreListener
());
bot
.
addCmdListener
(
"lsIgnores"
,
new
ListMessgaeIgnoreListener
());
bots
.
add
(
bot
);
bot
.
start
();
}
}
private
class
MessgaeIgnoreListener
implements
IrcBotListener
{
@Override
public
String
getUsageMessage
()
{
return
"Add value as message to be ignored"
;
}
@Override
public
void
executeIrc
(
IrcBot
bot
,
String
data
)
{
botbean
.
getIgnoreTypes
().
add
(
data
);
bot
.
say
(
"Added "
+
data
+
" as ignored message type"
);
}
}
private
class
RemoveMessgaeIgnoreListener
implements
IrcBotListener
{
@Override
public
String
getUsageMessage
()
{
return
"remove value from message ignore list"
;
}
@Override
public
void
executeIrc
(
IrcBot
bot
,
String
data
)
{
botbean
.
getIgnoreTypes
().
remove
(
data
);
bot
.
say
(
"Removed "
+
data
+
" from ignored message types"
);
}
}
private
class
ListMessgaeIgnoreListener
implements
IrcBotListener
{
@Override
public
String
getUsageMessage
()
{
return
"List message ignore list entries"
;
}
@Override
public
void
executeIrc
(
IrcBot
bot
,
String
data
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"Ignored messagetypes: "
);
for
(
String
d
:
botbean
.
getIgnoreTypes
())
{
sb
.
append
(
d
).
append
(
", "
);
}
bot
.
say
(
sb
.
toString
());
}
}
/**
* @see Servlet#destroy()
*/
...
...
@@ -126,7 +177,8 @@ public class IrcServlet extends HttpServlet {
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
for
(
IrcBot
b
:
bots
)
{
try
{
b
.
stop
();
...
...
@@ -145,7 +197,8 @@ public class IrcServlet extends HttpServlet {
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
// TODO Auto-generated method stub
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment