<commit_before>package com.ep.hippyjava.bot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;

import com.ep.hippyjava.HippyJava;
import com.ep.hippyjava.eventsystem.EventHandler;
import com.ep.hippyjava.eventsystem.Listener;
import com.ep.hippyjava.eventsystem.events.MessageRecivedEvent;
import com.ep.hippyjava.model.HipchatUser;
import com.ep.hippyjava.model.Room;
import com.ep.hippyjava.networking.Connection;
import com.ep.hippyjava.utils.NotificationColor;
import com.ep.hippyjava.utils.NotificationType;

public abstract class HippyBot implements Bot, Listener {

    private Connection con;
    private Room selected;

    @Override
    public void run(Connection con) {
        HippyJava.events.registerEvents(new Listener() {
            @SuppressWarnings("unused")
            @EventHandler
            public void messageEvent(MessageRecivedEvent event) {
                recieveMessage(event.body(), event.from(), event.getRoom());
            }
        });
        this.con = con;
        try {
            con.connect();
            con.login(username(), password());
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        onLoad();
    }

    @Override
    public void sendMessage(String message) {
        if (selected == null)
            return;
        sendMessage(message, selected);
    }

    /**
     * Change the currently selected room. You must be in the room in order to change to it, to join a room, invoke the method
     * {@link HippyBot#joinRoom(String)}.
     * The room name provide must be the JID name of the room. If {@link HippyBot#apiKey()} returns a valid
     * API Key, then the room name provided can be the normal name of the room.
     * @param name
     */
    public void changeRoom(String name) {
        if (findRoom(name) != null)
            changeRoom(findRoom(name));
    }
    
    /**
     * Join a room with the given name.
     * The room name provided must be the JID name of the room. If {@link HippyBot#apiKey()} returns a valid API Key, then
     * the room name provided can be the normal name of the room.
     * @param name
     * @return
     *        Returns whether the operation was successful or not.
     */
    public boolean joinRoom(String name) {
        try {
            if (apiKey().equals(""))
                con.joinRoom(name, nickname());
            else
                con.joinRoom(apiKey(), name, nickname());
            selected = findRoom(name);
            return true;
        } catch (XMPPException e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public Room getSelectedRoom() {
        return selected;
    }

    @Override
    public void changeRoom(Room room) {
        this.selected = room;
    }

    /**
     * Send a message a room. You must be connected to the room to send this message, if your not connected to the room, then please
     * invoke the method {@link HippyBot#joinRoom(String)}
     * @param name
     *            The name of the room.
     * @param message
     *               The message to send
     */
    public void sendMessageToRoom(String name, String message) {
        if (findRoom(name) != null)
            sendMessage(message, findRoom(name));
    }
    
    /**
     * Send a private message to someone. The parameter "to" must be the JID URL of the user, you can convert a nick such as
     * "<NAME>" to a JID URL by invoking the method {@link HippyBot#nickToJID(String)} and using the String returned as the
     * JID URL.
     * @param message
     *              The message to send.
     * @param to
     *          The JID of the user to send this message to.
     * @return
     *        Whether this operation was successful or not.
     */
    public boolean sendPM(String message, String to) {
        if (to.indexOf("@") == -1) { //oh noes its not a JID! The user didnt follow the rules!
            to = nickToJID(to);
            if (to.indexOf("@") == -1) //Ok I just dont know anymore
                return false;
        }
        try {
            getConnection().sendPM(message, to);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /**
     * Send a private message to someone.
     * @param message
     *              The message to send.
     * @param to
     *          The user to send the message to.
     * @return<commit_msg>Added fallback in sendPM(String, String)<commit_after>package com.ep.hippyjava.bot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;

import com.ep.hippyjava.HippyJava;
import com.ep.hippyjava.eventsystem.EventHandler;
import com.ep.hippyjava.eventsystem.Listener;
import com.ep.hippyjava.eventsystem.events.MessageRecivedEvent;
import com.ep.hippyjava.model.HipchatUser;
import com.ep.hippyjava.model.Room;
import com.ep.hippyjava.networking.Connection;
import com.ep.hippyjava.utils.NotificationColor;
import com.ep.hippyjava.utils.NotificationType;

public abstract class HippyBot implements Bot, Listener {

    private Connection con;
    private Room selected;

    @Override
    public void run(Connection con) {
        HippyJava.events.registerEvents(new Listener() {
            @SuppressWarnings("unused")
            @EventHandler
            public void messageEvent(MessageRecivedEvent event) {
                recieveMessage(event.body(), event.from(), event.getRoom());
            }
        });
        this.con = con;
        try {
            con.connect();
            con.login(username(), password());
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        onLoad();
    }

    @Override
    public void sendMessage(String message) {
        if (selected == null)
            return;
        sendMessage(message, selected);
    }

    /**
     * Change the currently selected room. You must be in the room in order to change to it, to join a room, invoke the method
     * {@link HippyBot#joinRoom(String)}.
     * The room name provide must be the JID name of the room. If {@link HippyBot#apiKey()} returns a valid
     * API Key, then the room name provided can be the normal name of the room.
     * @param name
     */
    public void changeRoom(String name) {
        if (findRoom(name) != null)
            changeRoom(findRoom(name));
    }
    
    /**
     * Join a room with the given name.
     * The room name provided must be the JID name of the room. If {@link HippyBot#apiKey()} returns a valid API Key, then
     * the room name provided can be the normal name of the room.
     * @param name
     * @return
     *        Returns whether the operation was successful or not.
     */
    public boolean joinRoom(String name) {
        try {
            if (apiKey().equals(""))
                con.joinRoom(name, nickname());
            else
                con.joinRoom(apiKey(), name, nickname());
            selected = findRoom(name);
            return true;
        } catch (XMPPException e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public Room getSelectedRoom() {
        return selected;
    }

    @Override
    public void changeRoom(Room room) {
        this.selected = room;
    }

    /**
     * Send a message a room. You must be connected to the room to send this message, if your not connected to the room, then please
     * invoke the method {@link HippyBot#joinRoom(String)}
     * @param name
     *            The name of the room.
     * @param message
     *               The message to send
     */
    public void sendMessageToRoom(String name, String message) {
        if (findRoom(name) != null)
            sendMessage(message, findRoom(name));
    }
    
    /**
     * Send a private message to someone. The parameter "to" must be the JID URL of the user, you can convert a nick such as
     * "<NAME>" to a JID URL by invoking the method {@link HippyBot#nickToJID(String)} and using the String returned as the
     * JID URL.
     * @param message
     *              The message to send.
     * @param to
     *          The JID of the user to send this message to.
     * @return
     *        Whether this operation was successful or not.
     */
    public boolean sendPM(String message, String to) {
        if (to.indexOf("@") == -1) { //oh noes its not a JID! The user didnt follow the rules!
            HipchatUser user = findUser(to);
            if (user != null)
                to = nickToJID(user.getName());
            else //Ok I just dont know anymore
                return false;
        }
        try {
            getConnection().sendPM(message, to);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /**
     * Send a private message to someone.
     * @param message
     *              The message to send.
     * @param to
     *          The user to send the message to.
     * @return
{'fraction_non_alphanumeric': 0.09366590767956945, 'fraction_numerical': 0.000310494721589733, 'mean_word_length': 2.098108368066688, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4611529', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2645, 'n_words': 889}