gandaro

Untitled

May 10th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. /*
  2.  *  xchat-rainbow - insert color codes in messages to make rainbows
  3.  *  Copyright (C) 2012  Jakob Kramer <[email protected]>
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License version 2 as
  7.  *  published by the Free Software Foundation.
  8.  *
  9.  *  This program is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  */
  14.  
  15. #include <wchar.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18.  
  19. #include "xchat-plugin.h"
  20.  
  21. #define COLORS_NUM 6
  22. #define STEPSIZE 9
  23.  
  24. xchat_plugin *ph;
  25.  
  26. const unsigned short COLORS[] = {
  27.     4, 7, 8, 3, 2, 6
  28. };
  29.  
  30. int rainbow(char *word[], char *word_eol[], void *userdata) {
  31.     size_t length = mbstowcs(NULL, word_eol[2], 0);
  32.  
  33.     if (!length) {
  34.         xchat_print(ph, "You must specify a message.\n");
  35.         return XCHAT_EAT_ALL;
  36.     }
  37.  
  38.     size_t stepsize = length / COLORS_NUM + 1;
  39.     size_t end_length = length + 3 * (length / stepsize +
  40.                                       (length % stepsize ? 1 : 0)) + 1;
  41.  
  42.     wchar_t *message = malloc(sizeof(wchar_t[length+1]));
  43.     mbstowcs(message, word_eol[2], length+1);
  44.  
  45.     wchar_t *formatted = malloc(sizeof(wchar_t[end_length]));
  46.  
  47.     for (size_t i = 0, j = 0, color = COLORS[0]; i < length; i++, j++,
  48.          color = COLORS[(i+1) / stepsize % COLORS_NUM]) {
  49.         if ((i+1) % stepsize == 0 || i == 0) {
  50.             formatted[j] = L'\003';
  51.             formatted[j+1] = L'0';
  52.             formatted[j+2] = L'0' + color;
  53.             j += 3;
  54.         }
  55.  
  56.         formatted[j] = message[i];
  57.     }
  58.  
  59.     formatted[end_length-1] = '\0';
  60.  
  61.     xchat_commandf(ph, "SAY %ls", formatted);
  62.  
  63.     free(formatted);
  64.     free(message);
  65.     return XCHAT_EAT_ALL;
  66. }
  67.  
  68. int xchat_plugin_init(xchat_plugin *plugin_handle, char **plugin_name,
  69.                       char **plugin_desc, char **plugin_version, char *arg) {
  70.     ph = plugin_handle;
  71.  
  72.     *plugin_name = "Rainbows";
  73.     *plugin_desc = "Make good-looking rainbows.";
  74.     *plugin_version = "0.1.dev";
  75.  
  76.     xchat_hook_command(ph, "RAINBOW", XCHAT_PRI_NORM, &rainbow,
  77.                        "Usage: /RAINBOW MESSAGE, Gives MESSAGE a colorful "
  78.                        "look.", NULL);
  79.  
  80.     return 1;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment