Previous Next Table of Contents

3.4 Writing scripts and aliases

One of the main features of PyIRC is that it allows you to write your own commands and to modify its behaviour, in the Python language. To do so, you have to know how the windows are named in PyIRC, and how the commands are parsed by the client.

Windows' names

Each window in PyIRC has a name, distinct from other window's. This is really simple:

Calls to functions

All the functions called from the command line will have string parameters. There is no error checking, you have to check the parameters yourself. The parameters on the command line are separated by space or tab characters. If the user forgets one parameter, it will be replaced by an empty string. So, if the user types:

/leave
the program will call:
leave('')
as the function expects one parameter.

Imagine that you have an user function called test(), which takes tree parameters.

/test 1                will give          test('1','','')
/test 1 2                "   "            test('1','2','')
/test 1 2 3              "   "            test('1','2','3')
/test 1 2 3 4            "   "            test('1','2','3 4')
/test 1 2 3 4 5          "   "            test('1','2','3 4 5')
etc.

Writing your own aliases

As you can see in the previous section, you have to verify the parameters to your function, especially if it is to be called from the command line. This is the only restriction to aliases.

Suppose that you think that /join is too long, that you are really lazy, and you only want to type /j each time. Then you can use the power of Python to create a simple alias. Just put this text in your .pyircrc file, or anywhere else:

j=join
Then, j will be a perfect alias for join.

Now, you think that you are tired to type the # before each channel name. You can redefine the j alias to be like this one this is a simple Python function definition :

def j(channel, key=""):
  # We check the args, and if 'channel' is not valid, add a # before it.
  if channel!='' and channel[0]!='#' and channel[0]!='&':
    join('#'+channel, key)
  else:
    join(channel, key)

Now, what can you do if your aliases are a mess, and you want to call the standard function? All you have to do is to call the function from the pyirc module. For example, let us rewrite the preceding example in another way:

def join(channel, key=""):
  # We check the args, and if 'channel' is not valid, add a # before it.
  if channel!='' and channel[0]!='#' and channel[0]!='&':
    pyirc.join('#'+channel, key)
  else:
    pyirc.join(channel, key)

j=join
By calling pyirc.join instead of join, you are sure that you will get the expected result.


Previous Next Table of Contents