![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
Greetings everyone, I've been working on a bot in Java to function as a file server. If you're familiar with mIRC then you probably are used to seeing the SysReset script responding to the !list command in channels. Basically, I'm trying to create a program independent bot to do the same. This is a portion of the method I wrote to achieve this (this is the relevant portion):
if(params.indexOf('\001' + "trigger") != -1)
{
System.out.println("A CTCP chat request has been received.");
ircSend("privmsg " + prefix.substring(0, prefix.indexOf('!')) + " :DCC CHAT chat " + myAddress(false) + " 8862");
ServerSocket ctcpServerSocket = null;
try
{
ctcpServerSocket = new ServerSocket(8862);
}
catch(Exception e)
{
System.err.println("CTCP socket not created.");
}
ctcpServerSocket.setSoTimeout(10000);
try
{
ctcpServerSocket.accept();
}
catch(Exception e)
{
System.err.println("DCC connection failed.");
}
ctcpServerSocket.close();
}So, the bot logs in to an IRC server and then joins a channel. When a user in the channel input the command '/ctcp MyBot trigger' this method sends out the command to initialize a DCC chat. It creates a server socket to where the initiator of the DCC chat should connect. However, something's not working and I can't figure out what that is. I'd appreciate any help, and would be happy to clarify anything that's unclear.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#2 |
|
King of Portal
|
Well turns out I figured out the problem. The method that I created, myAddress() returns a string in one of two formats. It returns my IP address in the format #.#.#.# when you pass a boolean value of true, or it returns the IP address without the periods (####) if you pass a boolean value of false. However, the DCC Chat protocol doesn't work this way. It actually requires an IP4 address to be send into the string. Which basically means I have to take the address a.b.c.d and convert it into a long in the following manner:
ip4 = (a * 256 ^ 3) + (b * 256 ^ 2) + (c * 256 ^ 1) + (d * 256 ^ 0) ip4 = a * 16777216 + b * 65536 + c * 256 + d That little conversion solved the problem and established the connection. The work on the bot is actually progressing nicely. Gotta figure out some more stuff though, so I may be back with other questions.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#3 |
|
King of Portal
|
ok here's the code
int removeSlot = 0;
for(int i = 0; i < botSetup.getUsers(); i++)
{
if(chatNick.equals(botSetup.getUserNick(i)))
{
removeSlot = i;
}
}
botSetup.removeUser(removeSlot);Line 1: initializes an int called removeSlot to 0 Line 2: creates a for loop whose starting value is 0 and its upper value is determined by the method getUsers() which basically returns the size of an ArrayList, the for loop increases i by 1 each time. Line 3: { Line 4: an if statement that checks whether or not chatNick is equal to the string recovered from the ArrayList Line 5: { Line 6: removeSlot records the index at which this equality occurs Line 7: } Line 8: } Line 9: the method removeUser(removeSlot) should remove the element at the given index. This is the problem statement. It is the one that generates the IndexOutOfBounds exception. I've tried printing to the console each variable so as to determine where an error might occur, but all variables seem to be in accord. getUsers() indeed returned an value of 1 (the ArrayList only contains 1 object) and the index at which said object occurred was obviously 0. Any ideas that could help me get this code working?
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
Professional Programmer
|
Well if everything is as you say, the removeUser() method is not properly implemented. So, we should see that code.
2. : nothing to do with your problem : if you call botSetup.removeUser(removeSlot); won't it remove the first element of the arrayList even if : if(chatNick.equals(botSetup.getUserNick(i))) doesn't happen ?
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
King of Portal
|
Yeah xavier I actually figured it out, the removeUser() method was referring to another array. I accidentally copy/pasted something and forgot to change the array it referenced, so problem solved and everything is in order now. Thanks for the input though.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#6 |
|
Professional Programmer
|
Don't mention it
![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#7 |
|
King of Portal
|
Is there any way to get Java to read the contents of a folder? Obviously, I would provide the pathway to the folder (or at least I would imagine I would have to). But are there any classes in Java that allow for such a procedure? So for instance, I have a folder called 'contents' located in my C:\ drive. It contains five files: file1.txt, file2.txt, file3.txt, file4.txt, file5.txt. How would I go about reading the names of each of the files by specifying only the path to the folder? Is such a thing possible in Java?
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#8 |
|
Professional Programmer
|
http://java.sun.com/j2se/1.4.2/docs/...a/io/File.html look at the list method
That was an easy one :p .. google helps
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#9 |
|
King of Portal
|
The following class is a Thread which executes from another segment of code. The purpose of this Thread is to execute a DCC Send operation via IRC. For more info on the DCC protocol please read the following link. There the DCC protocol of IRC is explained.
As it stands, the run method does three things: 1 - Opens the file to send. 2 - Creates the sockets for the transaction. 3 - Creates the streams from the sockets for the transfer. Here it is: import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.io.File;
import java.io.RandomAccessFile;
public class DCCSend extends Thread
{
// Nickname of user for send
private String sendNick;
private int sendPort;
// Sockets and I/O streams
private ServerSocket dccSendServerSocket = null;
private Socket dccSendSocket = null;
private BufferedReader dccSendReader = null;
private DataOutputStream dccSendWriter = null;
private PrintWriter ircStreamWriter = null;
// Bot configuration
private SetupContainer botSetup;
// The file in question
private String fileName;
//****************************************************************************************************//
/////////////////
// Constructor //
/////////////////
public DCCSend(String sendNick, int sendPort, String fileName, PrintWriter ircStreamWriter, SetupContainer botSetup)
{
super("dccSendEngaged");
this.sendNick = sendNick;
this.sendPort = sendPort;
this.fileName = fileName;
this.ircStreamWriter = ircStreamWriter;
this.botSetup = botSetup;
}
//****************************************************************************************************//
/////////////
// Methods //
/////////////
public void run()
{
System.err.println("DCC Send request has been received.");
System.err.println("Initiating " + sendNick + "'s send...");
boolean proceed = true;;
RandomAccessFile dccFile = null;
try
{
dccFile = new RandomAccessFile(new File(botSetup.getPath() + fileName), "r");
System.err.println("CLEAR: DCC Send file read");
}
catch(Exception e)
{
System.err.println("ERROR: Failed to open file for DCC Send.");
proceed = false;
}
if(proceed)
{
try
{
dccSendServerSocket = new ServerSocket(sendPort , 1);
dccSendServerSocket.setSoTimeout(10000);
ircSend("privmsg " + sendNick + " :\001DCC SEND " + fileName + " " + botSetup.getLocalHost() + " " + sendPort + " " + dccFile.length() + "\001");
dccSendSocket = dccSendServerSocket.accept();
dccSendServerSocket.close();
System.err.println("CLEAR: DCC Send socket");
}
catch(Exception e)
{
System.err.println("ERROR: DCC sockets failed to engage");
proceed = false;
}
}
if(proceed)
{
try
{
dccSendReader = new BufferedReader(new InputStreamReader(dccSendSocket.getInputStream()));
dccSendWriter = new DataOutputStream(dccSendSocket.getOutputStream());
System.err.println("CLEAR: DCC Send streams");
}
catch(Exception e)
{
System.err.println("ERROR: Unable to create I/O streams for DCC Send");
proceed = false;
}
}
}
// Sends a message to the IRC server and displays it in the console window
private boolean ircSend(String msg)
{
// Display the raw message being sent
System.out.println("IRC: '" + msg + "'");
// Attempt to send message
try
{
ircStreamWriter.println(msg);
}
catch(Exception e)
{
System.err.println("ERROR: ircSend(" + msg + ")");
return false;
}
return true;
}
}So, my problem is the following: can anyone show me or suggest some sort of way to finish the run() method off so that I can send a file and do all those things specified by the DCC Send method. Any help is appreciated. P.S. Thanks for the help with the file question Xavier. I incorporated it and it worked out nicely.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
I've only quickly glanced at your code, and these are a few things I noticed. You should call the "flush()" method after "println()" when using a PrintWriter object. Since you are using the BufferedReader class, make sure that all information being received on it are readable characters. If not, you'll need to use an InputStream.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|