src/irc_conn.c
changeset 27 e6639132bead
parent 24 08a26d0b9afd
child 28 9c1050bc8709
equal deleted inserted replaced
26:aec062af155d 27:e6639132bead
    13 {
    13 {
    14     (void) line;
    14     (void) line;
    15     (void) arg;
    15     (void) arg;
    16 
    16 
    17     // update state
    17     // update state
       
    18     conn->registering = false;
    18     conn->registered = true;
    19     conn->registered = true;
    19 
    20 
    20     log_info("registered");
    21     // trigger callback
       
    22     if (conn->callbacks.on_registered)
       
    23         conn->callbacks.on_registered(conn, conn->cb_arg);
    21 }
    24 }
    22 
    25 
    23 /*
    26 /*
    24  * PING <server1> [ <server2> ]
    27  * PING <server1> [ <server2> ]
    25  *
    28  *
    76             }
    79             }
    77         }
    80         }
    78     }
    81     }
    79 }
    82 }
    80 
    83 
    81 err_t irc_conn_create (struct irc_conn **conn_ptr, struct sock_stream *sock, const struct irc_conn_config *config, struct error_info *err)
    84 err_t irc_conn_create (struct irc_conn **conn_ptr, struct sock_stream *sock, const struct irc_conn_callbacks *callbacks, 
       
    85         void *cb_arg, struct error_info *err)
    82 {
    86 {
    83     struct irc_conn *conn;
    87     struct irc_conn *conn;
    84 
    88 
    85     // alloc new state struct
    89     // alloc new state struct
    86     if ((conn = calloc(1, sizeof(struct irc_conn))) == NULL)
    90     if ((conn = calloc(1, sizeof(struct irc_conn))) == NULL)
    87         return SET_ERROR(err, ERR_CALLOC);
    91         return SET_ERROR(err, ERR_CALLOC);
    88 
    92 
       
    93     // init state
       
    94     conn->callbacks = *callbacks;
       
    95     conn->cb_arg = cb_arg;
       
    96 
    89     // initialize command handlers
    97     // initialize command handlers
    90     STAILQ_INIT(&conn->handlers);
    98     STAILQ_INIT(&conn->handlers);
    91     
    99     
    92     // add the core handlers 
   100     // add the core handlers 
    93     if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL)))
   101     if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL)))
    95 
   103 
    96     // create the line_proto, with our on_line handler
   104     // create the line_proto, with our on_line handler
    97     if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err))
   105     if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err))
    98         return ERROR_CODE(err);
   106         return ERROR_CODE(err);
    99 
   107 
   100     // send the initial messages
       
   101     if (
       
   102             irc_conn_NICK(conn, config->nickname)
       
   103         ||  irc_conn_USER(conn, config->username, config->realname)
       
   104     )
       
   105         return ERROR_CODE(err);
       
   106 
       
   107     // ok
   108     // ok
   108     *conn_ptr = conn;
   109     *conn_ptr = conn;
   109 
   110 
   110     return SUCCESS;
   111     return SUCCESS;
   111 }
   112 }
   127 
   128 
   128     // ok
   129     // ok
   129     return SUCCESS;
   130     return SUCCESS;
   130 }
   131 }
   131 
   132 
       
   133 err_t irc_conn_register (struct irc_conn *conn, const struct irc_conn_register_info *info)
       
   134 {
       
   135     err_t err;
       
   136 
       
   137     // assert state
       
   138     if (conn->registering || conn->registered)
       
   139         // XXX: stupid error code
       
   140         return ERR_IRC_CONN_REGISTER_STATE;
       
   141 
       
   142     // send the initial messages
       
   143     if (
       
   144             (err = irc_conn_NICK(conn, info->nickname))
       
   145         ||  (err = irc_conn_USER(conn, info->username, info->realname))
       
   146     )
       
   147         return err;
       
   148 
       
   149     // set state
       
   150     conn->registering = true;
       
   151     
       
   152     // ok
       
   153     return SUCCESS;
       
   154 }
       
   155 
   132 err_t irc_conn_send (struct irc_conn *conn, const struct irc_line *line)
   156 err_t irc_conn_send (struct irc_conn *conn, const struct irc_line *line)
   133 {
   157 {
   134     char line_buf[IRC_LINE_MAX + 2];
   158     char line_buf[IRC_LINE_MAX + 2];
   135     err_t err;
   159     err_t err;
   136     int ret;
   160     int ret;