File: script.c

package info (click to toggle)
bezerk 0.3.2-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 920 kB
  • ctags: 1,027
  • sloc: ansic: 12,291; sh: 1,822; makefile: 92; tcl: 56
file content (136 lines) | stat: -rw-r--r-- 4,466 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Bezerk
 * Copyright (C) 1998 Tony Gale.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */
 
#include <glib.h>
#include <string.h>
#include <ctype.h>

#include "bezerk.h"
#include "message.h"
#include "util.h"
#include "reply.h"
#include "send.h"
#include "irc.h"
#include "ch_utils.h"
#include "debug.h"

extern GdkColor colour_white;
extern GdkColor colour_blue;
extern GdkColor colour_red;

int process_alias(Connection *connection, char *alias, char *comm_string, BezWindow *window)
{
  Message *message;
  GSList *args, *arg_entry;
  char *current, *comm_cpy, *alias_cpy;
  GString *result;
  int arg_num;

  bs_function_enter();

  comm_cpy = g_strdup(comm_string);
  args = split_args(comm_cpy);
  alias_cpy = g_strdup(alias);
  current = bs_strtok(alias_cpy, " \t");

  result = g_string_new ("");

  while (current) {
    if (*current == '$') {
      current++;
      if (!*current) {
	  continue;
      } else if (*current == '#') {
	if ( window && (window->type == BEZ_CHANNEL) && window->channel.current_channel) {
	  g_string_append(result, window->channel.current_channel->name);
	  g_string_append(result, " ");
	}
      } else if (!strncmp("me", current, 2)) {
	g_string_append(result, BEZ_BASE_WINDOW(window)->connection->nick);
	g_string_append(result, " ");
      } else if (isdigit(*current)) {
	arg_num = *current - '0';
	current++;
	if (isdigit(*current)) {
	  arg_num *= 10;
	  arg_num += *current - '0';
	  current++;
	}
	if (arg_num > 0) {
	  arg_entry = g_slist_nth(args, arg_num-1);
	  if (arg_entry) {
	    g_string_append(result, arg_entry->data);
	    g_string_append(result, " ");
	  }
	  if (*current == '-') {
	    arg_entry = g_slist_nth(args, arg_num);
	    while(arg_entry) {
	      g_string_append(result, arg_entry->data);
	      g_string_append(result, " ");
	      arg_entry = g_slist_nth(args, ++arg_num);
	    }
	    /*g_string_append(result, " ");*/
	  }
	} else if ( (arg_num == 0) && comm_string) {
	  g_string_append(result, comm_string);
	  g_string_append(result, " ");
	}
      }
    } else {
      g_string_append(result, current);
      g_string_append(result, " ");
    }
    current = bs_strtok(NULL, " \t");
  }

  g_free(comm_cpy);
  g_free(alias_cpy);
  comm_cpy = g_strdup(result->str);

  if (result->str[0] == '/') {
    send_issue_command(comm_cpy+1, window);
  } else if (window->type == BEZ_MESSAGE) {
    irc_send_format(connection->sd, "PRIVMSG %s :%s", window->message.nick, comm_cpy);
    message = message_new(MT_NICK, connection, window->message.nick);
    message->parts = g_slist_append(message->parts, 
				   message_part_new(&colour_blue, NULL, NULL, "<"));
    message->parts = g_slist_append(message->parts,
				   message_part_new(&colour_white,NULL, NULL,
						    BEZ_BASE_WINDOW(window)->connection->nick));
    message->parts = g_slist_append(message->parts,
				   message_part_new(&colour_blue, NULL, NULL, "> "));
    message->parts = message_part_parse_new(message->parts, &colour_white, comm_cpy);    
  
  } else if ( (window->type == BEZ_CHANNEL) && window->channel.current_channel) {
    irc_send_format(connection->sd, "PRIVMSG %s :%s", window->channel.current_channel->name, comm_cpy);
    message = message_new(MT_CHANNEL, connection, window->channel.current_channel->name);
    message->parts = g_slist_append(message->parts, 
				   message_part_new(&colour_blue, NULL, NULL, "<"));
    message->parts = g_slist_append(message->parts,
				   message_part_new(&colour_white,NULL, NULL,
						    BEZ_BASE_WINDOW(window)->connection->nick));
    message->parts = g_slist_append(message->parts,
				   message_part_new(&colour_blue, NULL, NULL, "> "));
    message->parts = message_part_parse_new(message->parts, &colour_white, comm_cpy);
  }

  g_free(comm_cpy);

  bs_function_leave();
  return(1);
}