1 |
/* Copyright (C) 2003, 2004 Stephane Thiell |
2 |
* |
3 |
* This file is part of pxyservd (from pxys) |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
* |
19 |
*/ |
20 |
#define RCSID "$Id: irc_client.c,v 1.6 2004/01/17 19:44:29 mbuna Exp $" |
21 |
|
22 |
#include "irc_client.h" |
23 |
#include <assert.h> |
24 |
#include <stdlib.h> |
25 |
#include <sys/types.h> |
26 |
#include <sys/socket.h> |
27 |
#include <netinet/in.h> |
28 |
#include <arpa/inet.h> |
29 |
#include <stdio.h> |
30 |
#include <string.h> |
31 |
|
32 |
#include <peak/peak.h> |
33 |
|
34 |
#include "cfgloader.h" |
35 |
#include "debug.h" |
36 |
#include "irc_cmd.h" |
37 |
#include "irc_msg.h" |
38 |
#include "irc_network.h" |
39 |
#include "irc_numnicks.h" |
40 |
#include "irc_send.h" |
41 |
#include "irc_struct.h" |
42 |
#include "irc_userbase.h" |
43 |
#include "pxyservd_log.h" |
44 |
|
45 |
#if NICKLEN > 9 |
46 |
#define MYNICKLEN 9 |
47 |
#else |
48 |
#define MYNICKLEN NICKLEN |
49 |
#endif |
50 |
|
51 |
extern time_t gBirthTime; |
52 |
|
53 |
peak_dict cmds_map; |
54 |
|
55 |
static char my_netnum[6]; |
56 |
|
57 |
static const peak_dict_init_entry my_commands[] = |
58 |
{ |
59 |
{ "evreg", cmd_evreg }, |
60 |
{ "evshow", cmd_evshow }, |
61 |
{ "grem", cmd_grem }, |
62 |
{ "help", cmd_help }, |
63 |
{ "info", cmd_info }, |
64 |
{ "noscan", cmd_noscan }, |
65 |
{ "proxytop", cmd_proxytop }, |
66 |
{ "pxstats", cmd_pxstats }, |
67 |
{ "recheck", cmd_recheck }, |
68 |
{ "servers", cmd_servers }, |
69 |
{ "showcmds", cmd_showcmds }, |
70 |
{ "stats", cmd_stats }, |
71 |
{ "status", cmd_status } |
72 |
}; |
73 |
|
74 |
void |
75 |
irc_client_init() |
76 |
{ |
77 |
cmds_map = peak_dict_create(&peak_dict_stringcase_key_ops, |
78 |
&peak_dict_null_value_ops, |
79 |
my_commands, |
80 |
PEAK_DICT_INIT_ENTRY_COUNT(my_commands)); |
81 |
assert(cmds_map != NULL); |
82 |
} |
83 |
|
84 |
void |
85 |
irc_client_register() |
86 |
{ |
87 |
char addr64[8]; |
88 |
ClientAddr caddr; |
89 |
char *nick; |
90 |
|
91 |
inttobase64(addr64, gConfig->client.userip.s_addr, 6); |
92 |
snprintf(my_netnum, sizeof(my_netnum), "%s%s", gMe.yy, MYCLIENT_NUM64); |
93 |
|
94 |
nick = gConfig->client.nickname; |
95 |
|
96 |
send_raw("%s %s %s %d %ld %s %s %s %s %s :%s" CRLF, gMe.yy, TOK_NICK, |
97 |
nick, 1, gBirthTime, |
98 |
gConfig->client.username, gConfig->client.hostname, |
99 |
gConfig->client.usermode, addr64, my_netnum, |
100 |
gConfig->client.realname); |
101 |
|
102 |
caddr.ip4 = gConfig->client.userip; |
103 |
|
104 |
irc_userbase_add(nick, |
105 |
gConfig->client.username, |
106 |
gBirthTime, |
107 |
gConfig->client.usermode, |
108 |
0, |
109 |
caddr, |
110 |
my_netnum); |
111 |
} |
112 |
|
113 |
void |
114 |
irc_client_unregister() |
115 |
{ |
116 |
irc_userbase_remove(my_netnum); |
117 |
} |
118 |
|
119 |
void |
120 |
irc_client_handle_private(toktabptr ttab) |
121 |
{ |
122 |
cmd_func f; |
123 |
struct Client *cptr; |
124 |
const char *dst = ttab->tok[0]; |
125 |
const char *cmd = ttab->tok[3]; |
126 |
char ipbuf[32]; |
127 |
|
128 |
assert(cmd != NULL && ttab->size >= 4); |
129 |
if (*cmd == ':') |
130 |
cmd++; |
131 |
|
132 |
if (!*cmd) |
133 |
return; |
134 |
|
135 |
if ((cptr = irc_network_find_client(yxx_to_int(dst))) == NULL) |
136 |
return; /* doh */ |
137 |
|
138 |
if (!(cptr->flags & CLIENT_FLAG_OPER)) |
139 |
return; /* Non opers go play balls. */ |
140 |
|
141 |
if (!inet_ntop(cptr->flags & CLIENT_FLAG_IPV6 ? AF_INET6 : AF_INET, |
142 |
&cptr->addr, ipbuf, sizeof(ipbuf))) |
143 |
return; |
144 |
|
145 |
switch (ttab->size) |
146 |
{ |
147 |
case 4: |
148 |
log_write(LOGID_OPERCMDS, "<%s!%s@%s> %s", cptr->nick, cptr->user, |
149 |
ipbuf, cmd); |
150 |
break; |
151 |
case 5: |
152 |
log_write(LOGID_OPERCMDS, "<%s!%s@%s> %s %s", cptr->nick, cptr->user, |
153 |
ipbuf, cmd, ttab->tok[4]); |
154 |
break; |
155 |
case 6: |
156 |
log_write(LOGID_OPERCMDS, "<%s!%s@%s> %s %s %s", cptr->nick, cptr->user, |
157 |
ipbuf, cmd, ttab->tok[4], ttab->tok[5]); |
158 |
break; |
159 |
default: |
160 |
log_write(LOGID_OPERCMDS, "<%s!%s@%s> %s %s %s (...)", cptr->nick, |
161 |
cptr->user, ipbuf, cmd, ttab->tok[4], ttab->tok[5]); |
162 |
break; |
163 |
} |
164 |
|
165 |
/* Get client pointer here instead of in every cmd handlers.. */ |
166 |
if ((f = (cmd_func)peak_dict_get_value(cmds_map, cmd))) |
167 |
(*f)(cptr, ttab); |
168 |
else |
169 |
send_client_to_one(dst, "Sorry, command not found."); |
170 |
} |