1
2 /*-
3 *
4 * New BSD License 2006
5 *
6 * Copyright (c) 2006, Jorgen Lundman
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * 1 Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2 Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * 3 Neither the name of the stuff nor the names of its contributors
20 * may be used to endorse or promote products derived from this
21 * software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <ctype.h>
40
41 #include "lion.h"
42
43 #include "object.h"
44 #include "say.h"
45
46
47
48
49 //
50 // This is called from our userinput.c if the type of object is unregistered
51 //
unreg_userinput(lion_t * handle,void * user_data,int status,int size,char * line)52 int unreg_userinput( lion_t *handle,
53 void *user_data, int status, int size, char *line)
54 {
55 object_t *node = (object_t *) user_data; // Get our object struct
56
57 // We should _always_ have a object node here, since we are only called
58 // from userinput.c if we did have one. But, for sanity....
59
60 if (!node)
61 return 0;
62
63
64
65
66 // Check which event we got and process...
67
68 switch( status ) {
69
70
71 case LION_CONNECTION_LOST:
72 printf("Connection '%p' was lost: %d:%s\n", handle,
73 size, line);
74
75 node->handle = NULL;
76 object_free( node );
77 break;
78
79 case LION_CONNECTION_CLOSED:
80 printf("Connection '%p' was gracefully closed.\n", handle);
81
82 node->handle = NULL;
83 object_free( node );
84 break;
85
86 // We have a new connection, send the greeting:
87 // This isn't called here. We leave it for debugging reasons.
88 case LION_CONNECTION_CONNECTED:
89 printf("Connection '%p' is connected. \n", handle);
90
91 // Send greeting
92 lion_printf(handle, "220 Welcome. (lion template sample)\r\n");
93 break;
94
95 case LION_BUFFER_USED:
96 break;
97
98 case LION_BUFFER_EMPTY:
99 break;
100
101 case LION_INPUT:
102
103 // If they issue "user <nick>" we take the nick and make them
104 // registered.
105 if (!strncasecmp("user ", line, 5)) {
106
107 if (isalnum(line[5])) {
108
109 node->username = strdup(&line[5]);
110 node->type = OBJECT_TYPE_REGISTERED;
111
112 // btw "handle" is the same as "node->handle" here.
113 lion_printf(handle, "200 OK\r\n");
114 say_all("%s connected (%s).\r\n", node->username,
115 lion_ssl_enabled(handle) ? "SSL" : "plain");
116 break;
117
118 }
119
120 }
121
122 if (!strncasecmp("quit", line, 4)) {
123 lion_close(handle);
124 break;
125 }
126
127
128 printf("input '%s'\n", line);
129 lion_printf(handle, "500 Please issue USER <nick>\r\n");
130 break;
131
132 case LION_BINARY:
133 break;
134
135 }
136
137
138
139 return 0;
140
141 }
142
143
144