Need To Retrieve Hostmask From IRC

PHP IRC Bot Needs Hostmask for Users

Page 1 of 1

2 Replies - 2318 Views - Last Post: 04 October 2009 - 05:50 PM Rate Topic: -----

#1 Magic_Man  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -5
  • View blog
  • Posts: 85
  • Joined: 03-March 09

Need To Retrieve Hostmask From IRC

Posted 04 October 2009 - 04:51 PM

I've been working on my PHP-based IRC bot for sometime now, and just got around to implementing a user system with various levels with a MySQL DB. Everything works fine, but I currently have to copy and paste the user's hostmask into the command to add them to the database. I've been looking around the web for a class or some other info on retrieving a hostmask by passing a nickname to the bot, but I haven't yet found anything that fits my needs.

In short, instead of typing '!setuser <hostmask> <level>', I'd like to be able to type '!setuser <nick> <level>', and have to bot grab the hostmask to insert it into the database. As a show of good faith, and per the rules here at DIC, I've included some source for adding the hostmask to the DB.

function set_user($host, $level) {
	
		$con = mysql_connect("<removed>", "<removed>", "<removed>");
		mysql_select_db("<removed>", $con);

		$result = mysql_query("SELECT * FROM users");

		while($row = mysql_fetch_array($result)) {
			if($row['hostmask'] == $host) {
				mysql_query("UPDATE users SET level = '$level' WHERE hostmask = '$host'");
				$match = true;
			}
		}

		if($match == false) {
		
			mysql_query("INSERT INTO users (hostmask, level) VALUES ('$host', '$level')");
		}
		mysql_close($con);
		
		return;
	}


Thanks in advance for any help on this.

Is This A Good Question/Topic? 0
  • +

Replies To: Need To Retrieve Hostmask From IRC

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: Need To Retrieve Hostmask From IRC

Posted 04 October 2009 - 05:34 PM

I assume you mean look up the hostname from the IRC network. To do this you should try a /who command. This will give you the Nick, modes along with their full hostmask on pretty much any IRC network.

You type !setuser nick level, the bot issues a /who command on that nick, gets the host as a response, parse it, then insert it into the database or execute your update.


On a separate note, just to help speed your code up a bit, instead of grabbing all your users, looping through them all and executing updates as you find them, do away completely with the select and while loop and execute the update directly. Remember update queries can have a global effect and you actually change all users simultaneously in your update as you have written it there. There is no need for the while loop at all because the update itself will update all records that match host. This will reduce the number of update queries you issue (all which update all users matching host) and will cut out processing time.

If you still don't understand what I am getting at, think of it this way...


You have a list of users:

Martyr2 martyr2@blahblahhost.com
John john@blablahotherhost.com
Jack jack@someotherhost.com
Martyr3 martyr2@blahblahhost.com

You are looping through each user in your loop looking for "martyr2@blahblahhost.com" so you execute an update when you reach Martyr2. But this update is going to update both Martyr2 and Martyr3 since the hosts match. Then you are going to continue looping and when you get to Martyr3, you are going to update again which is going to update Martyr2 and Martyr3 again since both match the mask again.

Do away with the looping of users altogether and just issue ONE update which will update any users matching "martyr2@blahblahhost.com" and it will update Martyr2 and Martyr3 together. Done. No looping, ONE update query and you save yourself time both on the database and on the server-side coding.

:)
Was This Post Helpful? 0
  • +
  • -

#3 Magic_Man  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -5
  • View blog
  • Posts: 85
  • Joined: 03-March 09

Re: Need To Retrieve Hostmask From IRC

Posted 04 October 2009 - 05:50 PM

I see what you're getting at. One thing I tried is doing a /dns on the user, so I could parse that single line, but my bot's having trouble retrieving that information from the server. I see now, that in the log it's getting the /who info. I suppose I can add that to an array and check from there. Thank you for your help. I'll also looking into improving my DB code with your suggestions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1