Contiki 3.x
shell-irc.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include <string.h>
36 #include <stddef.h>
37 
38 #include "contiki.h"
39 #include "shell.h"
40 #include "ircc.h"
41 
42 
43 /*---------------------------------------------------------------------------*/
44 PROCESS(shell_irc_process, "irc");
45 SHELL_COMMAND(irc_command,
46  "irc",
47  "irc <server> <nick>: start an IRC chat",
48  &shell_irc_process);
49 /*---------------------------------------------------------------------------*/
50 
51 #define MAX_SERVERLEN 16
52 #define MAX_NICKLEN 16
53 
54 static uip_ipaddr_t serveraddr;
55 static char server[MAX_SERVERLEN + 1];
56 static char nick[MAX_NICKLEN + 1];
57 
58 static struct ircc_state s;
59 
60 static unsigned char running;
61 
62 /*---------------------------------------------------------------------------*/
63 void
64 ircc_text_output(struct ircc_state *s, char *text1, char *text2)
65 {
66  char buf1[MAX_NICKLEN + MAX_SERVERLEN];
67  int len;
68 
69  strncpy(buf1, text1, sizeof(buf1));
70  len = strlen(buf1);
71  if(len < sizeof(buf1) - 1) {
72  buf1[len] = ' ';
73  buf1[len + 1] = 0;
74  }
75  shell_output_str(&irc_command, buf1, text2);
76 }
77 /*---------------------------------------------------------------------------*/
78 static void
79 parse_line(char *line)
80 {
81  if(line[0] == '/') {
82  if(strncmp(&line[1], "join", 4) == 0) {
83  ircc_join(&s, &line[6]);
84  ircc_text_output(&s, "Join", &line[6]);
85  } else if(strncmp(&line[1], "list", 4) == 0) {
86  ircc_list(&s);
87  ircc_text_output(&s, "Channel list", "");
88  } else if(strncmp(&line[1], "part", 4) == 0) {
89  ircc_part(&s);
90  ircc_text_output(&s, "Leaving channel", "");
91  } else if(strncmp(&line[1], "quit", 4) == 0) {
92  ircc_quit(&s);
93  } else if(strncmp(&line[1], "me", 2) == 0) {
94  ircc_actionmsg(&s, &line[4]);
95  ircc_text_output(&s, "*", &line[4]);
96  } else {
97  ircc_text_output(&s, &line[1], "Not implemented");
98  }
99  } else {
100  ircc_msg(&s, &line[0]);
101  ircc_text_output(&s, nick, line);
102  }
103 }
104 /*---------------------------------------------------------------------------*/
105 void
106 ircc_sent(struct ircc_state *s)
107 {
108 
109 }
110 /*---------------------------------------------------------------------------*/
111 void
112 ircc_closed(struct ircc_state *s)
113 {
114  ircc_text_output(s, server, "connection closed");
115  running = 0;
116 }
117 /*---------------------------------------------------------------------------*/
118 void
119 ircc_connected(struct ircc_state *s)
120 {
121  ircc_text_output(s, server, "connected");
122 }
123 /*---------------------------------------------------------------------------*/
124 PROCESS_THREAD(shell_irc_process, ev, data)
125 {
126  char *next;
127  struct shell_input *input;
128 
129  PROCESS_BEGIN();
130 
131  next = strchr(data, ' ');
132  if(next == NULL) {
133  shell_output_str(&irc_command,
134  "irc <server> <nick>: server as address", "");
135  PROCESS_EXIT();
136  }
137  *next = 0;
138  ++next;
139  strncpy(server, data, sizeof(server));
140  strncpy(nick, next, sizeof(nick));
141 
142  running = 1;
143 
144  uiplib_ipaddrconv(server, &serveraddr);
145  ircc_connect(&s, server, &serveraddr, nick);
146  while(running) {
148 
149  if(ev == shell_event_input) {
150  input = data;
151  if(input->len1 > 0) {
152  parse_line(input->data1);
153  }
154  } else if(ev == tcpip_event) {
155  ircc_appcall(data);
156 #if 0
157  } else if(ev == resolv_event_found) {
158  /* Either found a hostname, or not. */
159  if((char *)data != NULL &&
160  resolv_lookup((char *)data, &ipaddr) == RESOLV_STATUS_CACHED) {
161  uip_ipaddr_copy(serveraddr, ipaddr);
162  ircc_connect(&s, server, serveraddr, nick);
163  } else {
164  shell_output_str(&irc_command, "Host not found.", "");
165  }
166 #endif /* 0 */
167  }
168  }
169 
170  PROCESS_END();
171 }
172 /*---------------------------------------------------------------------------*/
173 void
174 shell_irc_init(void)
175 {
176  shell_register_command(&irc_command);
177 }
178 /*---------------------------------------------------------------------------*/
Hostname is fresh and usable.
Definition: resolv.h:68
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Main header file for the Contiki shell
#define NULL
The null pointer.
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
CCIF process_event_t resolv_event_found
Event that is broadcasted when a DNS name has been resolved.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
Structure for shell input data.
Definition: shell.h:365
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:75
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219