Writing Your Own XChat Plugins

Talk about computers, hardware, applications, and consumer electronics.
User avatar
Depressing
Posts: 1977
Joined: 2008.09.26 (06:46)
NUMA Profile: http://nmaps.net/user/rennaT
MBTI Type: ISTJ
Location: Trenton, Ontario, Canada
Contact:

Postby Tanner » 2010.11.09 (18:12)

The XChat plugin architecture is really good. It supports a wide range of languages and can integrate on a core level with the actual program. It's also documented so poorly that it's practically unusable. This is my attempt at clarifying some of the information set forth here. I guess the easiest way to show you how to do it would be to show you how I do it. This will be relevant to Windows systems only. You won't necessarily need to know C (I barely do) but you'll need to know some basic programming for this to make sense.

Things you'll need:
- Microsoft Visual C++ Express (I've been having trouble getting the 2010 version to actually download so though I would imagine it works, I can't be entirely certain. This is a link to the 2008 edition which I know works.)
- The main XChat plugin header
- An X-Macro file with the same name as your plugin (More on this later.)
- Some actual code. (We'll be using a slight variation on the sample code from the original doc)

The code:

Code: Select all

#include "xchat-plugin.h"
#include <string.h>

#define PNAME "AutoOp"
#define PDESC "Auto Ops anyone that joins. Use /aop to turn on or off."
#define PVERSION "0.1"

static xchat_plugin *ph;   /* plugin handle */
static int enable = 1;

static int join_cb(char *word[], void *userdata)
{
   //xchat_printf(ph, "%s \n", word[2]);
   if (enable)
   {
	if(strncmp(word[2],"#music",512)==0){
              /* Op ANYONE who joins */
      	      xchat_commandf(ph, "OP %s", word[1]);
	}
   }
   /* word[1] is the nickname, as in the Settings->Advanced->TextEvents window in xchat */

   return XCHAT_EAT_NONE;  /* don't eat this event, xchat needs to see it! */
}

static int aop_cb(char *word[], char *word_eol[], void *userdata)
{
   if (!enable)
   {
      enable = 1;
      xchat_print(ph, "AutoOping now enabled!\n");
   } else
   {
      enable = 0;
      xchat_print(ph, "AutoOping now disabled!\n");
   }

   return XCHAT_EAT_ALL;   /* eat this command so xchat and other plugins can't process it */
}

void xchat_plugin_get_info(char **name, char **desc, char **version, void **reserved)
{
   *name = PNAME;
   *desc = PDESC;
   *version = PVERSION;
}

int xchat_plugin_init(xchat_plugin *plugin_handle,
                      char **plugin_name,
                      char **plugin_desc,
                      char **plugin_version,
                      char *arg)
{
   /* we need to save this for use with any xchat_* functions */
   ph = plugin_handle;

   /* tell xchat our info */
   *plugin_name = PNAME;
   *plugin_desc = PDESC;
   *plugin_version = PVERSION;

   xchat_hook_command(ph, "aop", XCHAT_PRI_NORM, aop_cb, "Usage: aop, Turns OFF/ON Auto Oping", 0);
   xchat_hook_print(ph, "Join", XCHAT_PRI_NORM, join_cb, 0);

   xchat_print(ph, "AutoOpPlugin loaded successfully!\n");

   return 1;       /* return 1 for success */
}
The X-Macro:

Code: Select all

EXPORTS
  xchat_plugin_init
  xchat_plugin_get_info
The instructions:
1. Have you already downloaded and installed Visual C++? If not, do so now.

2. Creating a working directory for this project. Download the plugin header to the directory. Copy the main code into a blank text file and rename it to what you want you plugin to be called with ".c" as the extension (ex: aop.c). Do the same with the X-Macro except end it with ".def" instead of ".c". You should now have three files: aop.c, aop.def and xchat-plugin.h.

3. Here's where things get tricky. You can't just compile that code with the main Visual C++ IDE (at least, not as far as I know), you need to use the Visual Studio Command Prompt. You can use the Windows Search to find the shortcut or you can try making your own. Just make a new shortcut and, for the location, put this:

Code: Select all

%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86
That looks like it will only work for 64-bit systems. If you have a 32-bit system, you'll likely need to search. If you find it, could you post what the shortcut has for a target so that I can update this post. I would also edit the shortcut afterwards and change the "Start in" field to the working directory for this program.

You should now have a shortcut called Visual Studio Command Prompt or something like that that brings up a command prompt when you open it.

4. Now you just have to enter the commands that are listed in the original documents. No, wait! There's a typo in the command that you have to enter. I mean, honestly, XChat, that is pretty bad. The correct command is:

Code: Select all

cl -O1 -LD -GS -DWIN32 -c aop.c
link /DLL /out:aop.dll /SUBSYSTEM:WINDOWS aop.obj /def:aop.def /base:0x00d40000 
But that's too much typing each time so I made you a batch file:

Code: Select all

cl -O1 -LD -GS -DWIN32 -c %1.c
link /DLL /out:%1.dll /SUBSYSTEM:WINDOWS %1.obj /def:%1.def /base:0x00d40000 
Just copy that into a blank file in the working directory. Rename it to msvc.bat and then, from the Visual Studio Command Prompt, type "msvc aop" or replace "aop" with whatever you've called your plugin. If all goes well, you should now have a bunch of different files, including an aop.dll that you can load into XChat for testing!

Congrats, you just compiled your first XChat core plugin!

Let me know if you have any questions or comments!
Image
'rret donc d'niaser 'vec mon sirop d'erable, calis, si j't'r'vois icitte j'pellerais la police, tu l'veras l'criss de poutine de cul t'auras en prison, tabarnak

Who is online

Users browsing this forum: No registered users and 4 guests