Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions vilebot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jaxb.version>2.3.2</jaxb.version>
</properties>

<build>
Expand Down Expand Up @@ -163,5 +164,18 @@
<artifactId>commons-text</artifactId>
<version>1.6</version>
</dependency>
<!-- API -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${jaxb.version}</version>
</dependency>

<!-- Runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>${jaxb.version}</version>
</dependency>
</dependencies>
</project>
60 changes: 49 additions & 11 deletions vilebot/src/main/java/com/oldterns/vilebot/Vilebot.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,56 @@
*/
package com.oldterns.vilebot;

import com.oldterns.vilebot.handlers.admin.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.oldterns.vilebot.handlers.admin.AdminManagement;
import com.oldterns.vilebot.handlers.admin.AdminPing;
import com.oldterns.vilebot.handlers.admin.Auth;
import com.oldterns.vilebot.handlers.admin.GetLog;
import com.oldterns.vilebot.handlers.admin.NickChange;
import com.oldterns.vilebot.handlers.admin.Quit;
import com.oldterns.vilebot.handlers.user.AnswerQuestion;
import com.oldterns.vilebot.handlers.user.Ascii;
import com.oldterns.vilebot.handlers.user.ChatLogger;
import com.oldterns.vilebot.handlers.user.Church;
import com.oldterns.vilebot.handlers.user.Countdown;
import com.oldterns.vilebot.handlers.user.Decide;
import com.oldterns.vilebot.handlers.user.Excuses;
import com.oldterns.vilebot.handlers.user.FakeNews;
import com.oldterns.vilebot.handlers.user.FoobarNews;
import com.oldterns.vilebot.handlers.user.Fortune;
import com.oldterns.vilebot.handlers.user.GetInfoOn;
import com.oldterns.vilebot.handlers.user.Help;
import com.oldterns.vilebot.handlers.user.ImageToAscii;
import com.oldterns.vilebot.handlers.user.Inspiration;
import com.oldterns.vilebot.handlers.user.Jaziz;
import com.oldterns.vilebot.handlers.user.Jokes;
import com.oldterns.vilebot.handlers.user.Kaomoji;
import com.oldterns.vilebot.handlers.user.Karma;
import com.oldterns.vilebot.handlers.user.KarmaRoll;
import com.oldterns.vilebot.handlers.user.LastMessageSed;
import com.oldterns.vilebot.handlers.user.LastSeen;
import com.oldterns.vilebot.handlers.user.Markov;
import com.oldterns.vilebot.handlers.user.News;
import com.oldterns.vilebot.handlers.user.Omgword;
import com.oldterns.vilebot.handlers.user.Ops;
import com.oldterns.vilebot.handlers.user.*;
import com.oldterns.vilebot.handlers.user.QuotesAndFacts;
import com.oldterns.vilebot.handlers.user.RemindMe;
import com.oldterns.vilebot.handlers.user.RockPaperScissors;
import com.oldterns.vilebot.handlers.user.Trivia;
import com.oldterns.vilebot.handlers.user.Ttc;
import com.oldterns.vilebot.handlers.user.TwitterCorrection;
import com.oldterns.vilebot.handlers.user.UrlTitleAnnouncer;
import com.oldterns.vilebot.handlers.user.UrlTweetAnnouncer;
import com.oldterns.vilebot.handlers.user.UserPing;
import com.oldterns.vilebot.handlers.user.Userlists;
import com.oldterns.vilebot.handlers.user.Weather;
import com.oldterns.vilebot.util.BaseNick;
import org.pircbotx.Configuration;
import org.pircbotx.MultiBotManager;
Expand All @@ -21,14 +67,6 @@
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Vilebot
extends ListenerAdapter
{
Expand Down Expand Up @@ -78,7 +116,7 @@ public static void main( String[] args )

Configuration botConfiguration =
new Configuration.Builder().setName( ircNick ).setLogin( ircUser ).setRealName( ircRealName ).addServer( ircServerAddress,
ircPort ).addAutoJoinChannel( ircChannel ).setAutoReconnect( true ).addListener( new Vilebot() ).addListener( new AdminManagement() ).addListener( new AdminPing() ).addListener( new Auth() ).addListener( new GetLog() ).addListener( new com.oldterns.vilebot.handlers.admin.Help() ).addListener( new NickChange() ).addListener( new com.oldterns.vilebot.handlers.admin.Ops() ).addListener( new Quit() ).addListener( new AnswerQuestion() ).addListener( new Ascii() ).addListener( new ChatLogger() ).addListener( new Church() ).addListener( new Countdown() ).addListener( new Decide() ).addListener( new Excuses() ).addListener( new FakeNews() ).addListener( new Fortune() ).addListener( new GetInfoOn() ).addListener( new Help() ).addListener( new ImageToAscii() ).addListener( new Inspiration() ).addListener( new Jaziz() ).addListener( new Jokes() ).addListener( new Kaomoji() ).addListener( new Karma() ).addListener( new KarmaRoll() ).addListener( new LastMessageSed() ).addListener( new LastSeen() ).addListener( new Markov() ).addListener( new News() ).addListener( new Omgword() ).addListener( new Ops() ).addListener( new QuotesAndFacts() ).addListener( new RemindMe() ).addListener( new RockPaperScissors() ).addListener( new Trivia() ).addListener( new Ttc() ).addListener( new TwitterCorrection() ).addListener( new UrlTitleAnnouncer() ).addListener( new UrlTweetAnnouncer() ).addListener( new Userlists() ).addListener( new UserPing() ).addListener( new Weather() ).buildConfiguration();
ircPort ).addAutoJoinChannel( ircChannel ).setAutoReconnect( true ).addListener( new Vilebot() ).addListener( new AdminManagement() ).addListener( new AdminPing() ).addListener( new Auth() ).addListener( new GetLog() ).addListener( new com.oldterns.vilebot.handlers.admin.Help() ).addListener( new NickChange() ).addListener( new com.oldterns.vilebot.handlers.admin.Ops() ).addListener( new Quit() ).addListener( new AnswerQuestion() ).addListener( new Ascii() ).addListener( new ChatLogger() ).addListener( new Church() ).addListener( new Countdown() ).addListener( new Decide() ).addListener( new Excuses() ).addListener( new FakeNews() ).addListener( new Fortune() ).addListener( new GetInfoOn() ).addListener( new Help() ).addListener( new ImageToAscii() ).addListener( new Inspiration() ).addListener( new Jaziz() ).addListener( new Jokes() ).addListener( new Kaomoji() ).addListener( new Karma() ).addListener( new KarmaRoll() ).addListener( new LastMessageSed() ).addListener( new LastSeen() ).addListener( new Markov() ).addListener( new News() ).addListener( new Omgword() ).addListener( new Ops() ).addListener( new QuotesAndFacts() ).addListener( new RemindMe() ).addListener( new RockPaperScissors() ).addListener( new Trivia() ).addListener( new Ttc() ).addListener( new TwitterCorrection() ).addListener( new UrlTitleAnnouncer() ).addListener( new UrlTweetAnnouncer() ).addListener( new Userlists() ).addListener( new UserPing() ).addListener( new Weather() ).addListener( new FoobarNews() ).buildConfiguration();

botManager.addBot( botConfiguration );
}
Expand Down
23 changes: 22 additions & 1 deletion vilebot/src/main/java/com/oldterns/vilebot/db/QuoteFactDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/
package com.oldterns.vilebot.db;

import java.util.Set;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import redis.clients.jedis.Jedis;

Expand All @@ -21,6 +23,25 @@ public class QuoteFactDB

private static final int COUNT = 5;

public static Set<String> getQuotableFactableNicks()
{
Jedis jedis = pool.getResource();
try
{
Set<String> hasQuotes = jedis.keys( keyOfQuoteSetsPrefix + "*" );
Set<String> hasFacts = jedis.keys( keyOfFactSetsPrefix + "*" );

Set<String> out = new HashSet<>();
out.addAll( hasQuotes.stream().map( s -> s.substring( keyOfQuoteSetsPrefix.length() ) ).collect( Collectors.toSet() ) );
out.addAll( hasFacts.stream().map( s -> s.substring( keyOfFactSetsPrefix.length() ) ).collect( Collectors.toSet() ) );
return out;
}
finally
{
pool.returnResource( jedis );
}
}

/**
* Add a quote to the quote set of a noun.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright 2019 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps keep license consistent with rest of the project? Though both Apache and MIT are both permissive, so this might just be nitpicking.

package com.oldterns.vilebot.handlers.user;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.oldterns.vilebot.Vilebot;
import com.oldterns.vilebot.db.QuoteFactDB;
import com.oldterns.vilebot.util.LimitCommand;
import com.oldterns.vilebot.util.NewsParser;
import com.oldterns.vilebot.util.rss.Channel;
import com.oldterns.vilebot.util.rss.Item;
import com.oldterns.vilebot.util.rss.RSS;
import org.pircbotx.hooks.types.GenericMessageEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FoobarNews
extends NewsParser
{
private static final Pattern FOOBAR_NEWS_PATTERN = Pattern.compile( "^!foobarnews(?: ([a-zA-Z]+)|)" );

private static final Pattern FOOBAR_NEWS_HELP_PATTERN = Pattern.compile( "^!foobarnews help" );

private static final Logger logger = LoggerFactory.getLogger( FoobarNews.class );

private final String HELP_MESSAGE = generateHelpMessage();

private final String HELP_COMMAND = "'!news help'";

public static LimitCommand limitCommand = new LimitCommand( 100, 1 );

private static final String RESTRICTED_CHANNEL = Vilebot.getConfig().get( "ircChannel1" );

@Override
public void onGenericMessage( final GenericMessageEvent event )
{
String text = event.getMessage();
Matcher matcher = FOOBAR_NEWS_PATTERN.matcher( text );
Matcher helpMatcher = FOOBAR_NEWS_HELP_PATTERN.matcher( text );

if ( helpMatcher.matches() )
{
for ( String line : HELP_MESSAGE.split( "\n" ) )
{
event.respondPrivateMessage( line );
}
}
else if ( matcher.matches() )
{
String category = matcher.group( 1 );
if ( category != null && category.equals( "foobar" ) )
{
category = null;
}
else if ( category != null && !QuoteFactDB.getQuotableFactableNicks().contains( category ) )
{
event.respond( category + " is not a nick that has facts or quotes." );
return;
}
HashMap<String, URL> newsFeedsByCategory = new HashMap<>();
try
{
Path newsFile = Files.createTempFile( "FoobarNews", ".xml" );
logger.info( "Foobar news temp file: " + newsFile.toString() );

RSS rssFeed = createRssFeed( category );
JAXBContext context = JAXBContext.newInstance( RSS.class );
Marshaller marshaller = context.createMarshaller();
try ( FileOutputStream fileOutputStream = new FileOutputStream( newsFile.toFile() ) )
{
marshaller.marshal( rssFeed, fileOutputStream );
URL newsFileURL = newsFile.toUri().toURL();
category = ( category != null ) ? category.toLowerCase() : "foobar";

newsFeedsByCategory.put( category, newsFileURL );
logger.info( newsFileURL.toString() );
currentNews( event, matcher, newsFeedsByCategory, category, HELP_COMMAND, limitCommand,
RESTRICTED_CHANNEL, logger );
}
catch ( IOException e )
{
throw e;
}
}
catch ( IOException | JAXBException e )
{
event.respond( "I could not create temporary file for the news :(" );
}
}
}

private RSS createRssFeed( String topic )
{
final int ITEM_COUNT = 3;
final String VERSION = "2.0";

RSS out = new RSS();
out.version = VERSION;

Channel channel = new Channel();
channel.title = "The Foobar News";
channel.link = "http://localhost";
channel.description = "The latest in #thefoobar";
channel.items = new ArrayList<>();

Markov markov = new Markov();
if ( topic != null )
{
markov.trainOnNick( topic );
}
else
{
markov.train();
}

for ( int i = 0; i < ITEM_COUNT; i++ )
{
Item item = new Item();
item.title = markov.generatePhrase( 50 );
item.link = "http://localhost";
item.description = "";
channel.items.add( item );
}

out.channel = channel;
return out;
}

@Override
protected String generateHelpMessage()
{
StringBuilder sb = new StringBuilder();

sb.append( "Foobar News Categories (example: !foobarnews foobar):" );
sb.append( "\n" );

sb.append( " { foobar }" );
for ( String nick : QuoteFactDB.getQuotableFactableNicks() )
{
sb.append( " { " + nick + " }" );
}

return sb.toString();
}

}
Loading