Clean a bit - to be continued...
[seven-1.x.git] / servlink / control.c
blob462303f7728458ce92251e13d7ef9a0d49c1e366
1 /************************************************************************
2 * IRC - Internet Relay Chat, servlink/servlink.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 1, or (at your option)
7 * any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "setup.h"
21 #include <sys/types.h>
23 #include <assert.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <string.h>
27 #ifdef HAVE_LIBZ
28 #include <zlib.h>
29 #endif
31 #include "servlink.h"
32 #include "io.h"
33 #include "control.h"
35 static cmd_handler cmd_set_zip_out_level;
36 static cmd_handler cmd_start_zip_out;
37 static cmd_handler cmd_start_zip_in;
38 static cmd_handler cmd_init;
40 struct command_def command_table[] = {
41 {CMD_SET_ZIP_OUT_LEVEL, cmd_set_zip_out_level, COMMAND_FLAG_DATA},
42 {CMD_START_ZIP_OUT, cmd_start_zip_out, 0},
43 {CMD_START_ZIP_IN, cmd_start_zip_in, 0},
44 {CMD_INJECT_RECVQ, process_recvq, COMMAND_FLAG_DATA},
45 {CMD_INJECT_SENDQ, process_sendq, COMMAND_FLAG_DATA},
46 {CMD_INIT, cmd_init, 0},
47 {CMD_ZIPSTATS, send_zipstats, 0},
48 {0, 0, 0}
51 void
52 cmd_set_zip_out_level(struct ctrl_command *cmd)
54 #ifdef HAVE_LIBZ
55 out_state.zip_state.level = *cmd->data;
56 if((out_state.zip_state.level < -1) || (out_state.zip_state.level > 9))
57 send_error("invalid compression level %d", out_state.zip_state.level);
58 #else
59 send_error("can't set compression level - no libz support!");
60 #endif
63 void
64 cmd_start_zip_out(struct ctrl_command *cmd)
66 #ifdef HAVE_LIBZ
67 int ret;
69 if(out_state.zip)
70 send_error("can't start compression - already started!");
72 out_state.zip_state.z_stream.total_in = 0;
73 out_state.zip_state.z_stream.total_out = 0;
74 out_state.zip_state.z_stream.zalloc = (alloc_func) 0;
75 out_state.zip_state.z_stream.zfree = (free_func) 0;
76 out_state.zip_state.z_stream.data_type = Z_ASCII;
78 if(out_state.zip_state.level <= 0)
79 out_state.zip_state.level = Z_DEFAULT_COMPRESSION;
81 if((ret = deflateInit(&out_state.zip_state.z_stream, out_state.zip_state.level)) != Z_OK)
82 send_error("deflateInit failed: %s", zError(ret));
84 out_state.zip = 1;
85 #else
86 send_error("can't start compression - no libz support!");
87 #endif
90 void
91 cmd_start_zip_in(struct ctrl_command *cmd)
93 #ifdef HAVE_LIBZ
94 int ret;
96 if(in_state.zip)
97 send_error("can't start decompression - already started!");
99 in_state.zip_state.z_stream.total_in = 0;
100 in_state.zip_state.z_stream.total_out = 0;
101 in_state.zip_state.z_stream.zalloc = (alloc_func) 0;
102 in_state.zip_state.z_stream.zfree = (free_func) 0;
103 in_state.zip_state.z_stream.data_type = Z_ASCII;
104 if((ret = inflateInit(&in_state.zip_state.z_stream)) != Z_OK)
105 send_error("inflateInit failed: %s", zError(ret));
106 in_state.zip = 1;
107 #else
108 send_error("can't start decompression - no libz support!");
109 #endif
113 void
114 cmd_init(struct ctrl_command *cmd)
116 if(in_state.active || out_state.active)
117 send_error("CMD_INIT sent twice!");
119 in_state.active = 1;
120 out_state.active = 1;
121 CONTROL.read_cb = read_ctrl;
122 CONTROL.write_cb = NULL;
123 LOCAL.read_cb = read_data;
124 LOCAL.write_cb = NULL;
125 REMOTE.read_cb = read_net;
126 REMOTE.write_cb = NULL;