GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Jun-2008, 02:51
Seraphis Seraphis is offline
Junior Member
 
Join Date: Jul 2006
Posts: 34
Seraphis is on a distinguished road

Character/String Searching for a Bot


The general idea for this program is that it's a bot, which grabs text from a chat window (a rich text field) scans through it for commands, and sends text back to the chat.

Here's the code, will explain below.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <windows.h>

void stn(LPCTSTR text, HWND hwnd)
{
	for (int x=0;x<=lstrlen(text);x++)
	{
		SendMessage(hwnd, WM_CHAR, text[x], NULL);
	}
	SendMessage(hwnd, WM_IME_KEYDOWN, VK_RETURN, NULL);
}

int main ()
{
	HWND hwndchat, hwndedit;

	hwndchat = FindWindow("#32770", "Chat");
	hwndedit = FindWindowEx(hwndchat, NULL, "RichEdit20A", NULL);

	if (!hwndchat)
	{
		cout << "ERROR 101" << endl;
	}

	if (!hwndedit)
	{
		cout << "ERROR 102" << endl;
	}
	

	char buf[1000];
	SendMessage(hwndedit, WM_GETTEXT, 999, (LPARAM)buf);
	buf[999] = 0;

	while (true)
	{
		Sleep(1000);
		
		char newbuf[1000];
		SendMessage(hwndedit, WM_GETTEXT, 999, (LPARAM)newbuf);
		newbuf[999] = 0;

		if (strcmp(buf, newbuf) != 0)
		{
			cout << "Updated\n";
			SendMessage(hwndedit, WM_GETTEXT, 999, (LPARAM)buf);
			buf[999] = 0;		
		}
	}

	_getch();

	return 0;
}

Essentially... The program looks for the window and the rich text field and passes them to their handles...

It then grabs the initial text currently in the chat field and places it into the buffer "buf"

Then, the application loop begins and it checks to see if the text inside the chat field has changed from the text in the "buf" buffer... If so, then it would search for a command. If buf and newbuff are the same, it will wait until the chat has been updated to check again for a command.

Now here's my issue... If I were to use strstr(str1,str2) and search for the term "admins" in the buffer, it would find it every time the screen updates from previous times the command has been used... causing it to spam.

So here's an example situation of what I want to do.

EXAMPLE:

The chat room has the following text in it when the bot is loaded up:
Code:
John: Blah blah Mark: Blah Chris: blah blah BLAH! John: blah?
This is, at the start of the bot, loaded into the buffer "buf" and then the application loop begins.

The application loop grabs the text from the chat and puts it into the buffer "newbuf" after sleeping 1000 m/s

The bot then determines that there has been no new chat activity due to its comparison of "newbuf" and "buf" returning null. The loop goes on...

Somebody types into the chat and the chatroom now looks like:
Code:
John: Blah blah Mark: Blah Chris: blah blah BLAH! John: blah? Tod: Huh?

My goal is to fill into a third buffer, the text that wasn't present in "buf" when the comparison between "buf" and "newbuf" returns a value... In other words grab "Tod: Huh?" and put it into a buffer so I can check it for commands.

Ideas?

Right now all it does is say Updated in the console whenever somebody enters new text into the chat.
  #2  
Old 27-Jun-2008, 11:36
Seraphis Seraphis is offline
Junior Member
 
Join Date: Jul 2006
Posts: 34
Seraphis is on a distinguished road

Re: Character/String Searching for a Bot


I cant belive I couldn't figure this out, must've been because I wrote that at 5 am... Figured it out... heres my code for reference purposes.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <windows.h>

void stn(LPCTSTR text, HWND hwnd)
{
	for (int x=0;x<=lstrlen(text);x++)
	{
		SendMessage(hwnd, WM_CHAR, text[x], NULL);
	}
	SendMessage(hwnd, WM_IME_KEYDOWN, VK_RETURN, NULL);
}

int main ()
{
	HWND hwndchat, hwndedit;

	hwndchat = FindWindow("#32770", "Chat");
	hwndedit = FindWindowEx(hwndchat, NULL, "RichEdit20A", NULL);

	if (!hwndchat)
	{
		cout << "ERROR 101" << endl;
	}

	if (!hwndedit)
	{
		cout << "ERROR 102" << endl;
	}
	

	char buf[1000];
	SendMessage(hwndedit, WM_GETTEXT, 999, (LPARAM)buf);
	buf[999] = 0;

	while (true)
	{
		Sleep(1000);
		
		char newbuf[1000];
		SendMessage(hwndedit, WM_GETTEXT, 999, (LPARAM)newbuf);
		newbuf[999] = 0;

		if (strcmp(buf, newbuf) != 0)
		{
			char clipbuf[100];

			for (int x=0, i=0;x<=sizeof(newbuf);x++)
			{
				if (buf[x] != newbuf[x])
				{
					clipbuf[i] = newbuf[x];
					i++;
				}
			}

			cout << clipbuf << "\n";
			SendMessage(hwndedit, WM_GETTEXT, 999, (LPARAM)buf);
			buf[999] = 0;
		}
	}

	_getch();

	return 0;
}
 
 

Recent GIDBlogConfiguring iptables for Webmin Servers Index Module by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
searching for bluetooth services using bluez sdp ThinkBlue C Programming Language 0 26-Mar-2007 07:10
Searching for free web hosting No Adds over 1gb eK3eKyToPa Free Web Hosting 1 14-Mar-2007 13:23
Generic searching through vectors zdenek C++ Forum 1 10-Oct-2006 08:44
Searching and inserting in linked list s_scientist C++ Forum 3 10-Apr-2005 19:27

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:42.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.