97 // initialize command handlers |
96 // initialize command handlers |
98 STAILQ_INIT(&conn->handlers); |
97 STAILQ_INIT(&conn->handlers); |
99 |
98 |
100 // add the core handlers |
99 // add the core handlers |
101 if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL))) |
100 if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL))) |
102 return ERROR_CODE(err); |
101 goto error; |
103 |
102 |
104 // create the line_proto, with our on_line handler |
103 // create the line_proto, with our on_line handler |
105 if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err)) |
104 if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err)) |
106 return ERROR_CODE(err); |
105 goto error; |
107 |
106 |
108 // ok |
107 // ok |
109 *conn_ptr = conn; |
108 *conn_ptr = conn; |
110 |
109 |
111 return SUCCESS; |
110 return SUCCESS; |
|
111 |
|
112 error: |
|
113 // release |
|
114 irc_conn_destroy(conn); |
|
115 |
|
116 return ERROR_CODE(err); |
|
117 } |
|
118 |
|
119 void irc_conn_destroy (struct irc_conn *conn) |
|
120 { |
|
121 struct irc_cmd_chain *next = STAILQ_FIRST(&conn->handlers); |
|
122 |
|
123 // release the line_proto |
|
124 if (conn->lp) |
|
125 line_proto_release(conn->lp); |
|
126 |
|
127 // clean up any handler chains |
|
128 while (next) { |
|
129 struct irc_cmd_chain *node = next; |
|
130 |
|
131 // update next |
|
132 next = STAILQ_NEXT(node, node); |
|
133 |
|
134 // free |
|
135 free(node); |
|
136 } |
|
137 |
|
138 // free the irc_conn itself |
|
139 free(conn); |
112 } |
140 } |
113 |
141 |
114 err_t irc_conn_register_handler_chain (struct irc_conn *conn, struct irc_cmd_handler *handlers, void *arg) |
142 err_t irc_conn_register_handler_chain (struct irc_conn *conn, struct irc_cmd_handler *handlers, void *arg) |
115 { |
143 { |
116 struct irc_cmd_chain *item; |
144 struct irc_cmd_chain *item; |