1 /*
2  * ninredir.c:
3  *
4  * this file contains code dealing with /rel* commands for Ninja IRc
5  *
6  * these currently include /relm, /relsm, /reln, /relsn
7  *
8  * written by Kraig Amador and Joshua J. Drake
9  *
10  */
11 
12 #include "irc.h"
13 #include "dma.h"
14 #include "output.h"
15 #include "edit.h"
16 #include "ircaux.h"
17 
18 #include "ninja.h"
19 #include "ninredir.h"
20 
21 /* /rel and /rels lists */
22 #define MAX_RELAYS 10
23 static u_char *redirect_msgs[MAX_RELAYS] = { 0, 0, 0, 0, 0,  0, 0, 0, 0, 0 };
24 static u_char *redirect_sent_msg[MAX_RELAYS] = { 0, 0, 0, 0, 0,  0, 0, 0, 0, 0 };
25 static u_char *redirect_notice[MAX_RELAYS] = { 0, 0, 0, 0, 0,  0, 0, 0, 0, 0 };
26 static u_char *redirect_sent_notice[MAX_RELAYS] = { 0, 0, 0, 0, 0,  0, 0, 0, 0, 0 };
27 
28 
29 /*
30  * the /relm and /relsm commands..
31  * (relay message and relay sent message)
32  */
33 void
ninja_redirect(command,args,subargs)34 ninja_redirect(command, args, subargs)
35    u_char *command, *args, *subargs;
36 {
37    u_char **list = NULL, *tmp, tmpbuf[1024];
38    char *curchan = get_channel_by_refnum(0);
39    int x, number = -1;
40 
41    *tmpbuf = '\0';
42    if (!command || !*command)
43      return;
44    upper(command);
45    if (my_strcmp(command, "RELM") == 0)
46       list = redirect_msgs;
47    else
48    if (my_strcmp(command, "RELSM") == 0)
49       list = redirect_sent_msg;
50    else
51    if (my_strcmp(command, "RELN") == 0)
52       list = redirect_notice;
53    else
54    if (my_strcmp(command, "RELSN") == 0)
55       list = redirect_sent_notice;
56    else
57      {
58 	put_error("Unknown relay command!");
59 	return;
60      }
61 
62    /* check for args.. */
63    tmp = next_arg(args, &args);
64    if (!tmp)
65      {
66 	/* nothing?  default to relaying the last one to the current channel..
67 	 * that is if we have a current channel and a last one ;)
68 	 */
69 	if (curchan && list[0])
70 	  {
71 	     my_strncpy(tmpbuf, curchan, sizeof(tmpbuf)-1);
72 	     tmpbuf[sizeof(tmpbuf)-1] = '\0';
73 	     number = 0;
74 	  }
75 	else
76 	  {
77 	     usage(command, "[-l] or [<number> [<nick/channel>]]");
78 	     return;
79 	  }
80      }
81 
82    /* list? */
83    if (my_stricmp(tmp, "-l") == 0)
84      {
85 	for (x = MAX_RELAYS-1; x >= 0; x--)
86 	  {
87 	     if (list[x])
88 	       put_info("/%s %2.2d %s", command, x + 1, list[x]);
89 	  }
90 	return;
91      }
92 
93    /* got a #? */
94    if (number == -1
95        && tmp
96        && isdigit(*tmp))
97      {
98 	number = my_atoi(tmp);
99 	if (number < 1
100 	    || number > MAX_RELAYS
101 	    || !list[number-1])
102 	  {
103 	     put_error("%s: invalid message number: %s", command, tmp);
104 	     return;
105 	  }
106 	number--;
107      }
108 
109    /* see if we got a number/target */
110    while ((tmp = next_arg(args, &args)))
111      {
112 	if (*tmpbuf)
113 	  my_strmcat(tmpbuf, ",", sizeof(tmpbuf)-1);
114 	my_strmcat(tmpbuf, tmp, sizeof(tmpbuf)-1);
115      }
116 
117    /* where are we sending it? */
118    if (number == -1)
119      {
120 	put_error("%s: no message number specified", command);
121 	return;
122      }
123 
124    /* no target?  how about a current channel? */
125    if (!*tmpbuf)
126      {
127 	if (curchan)
128 	  {
129 	     my_strncpy(tmpbuf, curchan, sizeof(tmpbuf)-1);
130 	     tmpbuf[sizeof(tmpbuf)-1] = '\0';
131 	  }
132 	else
133 	  {
134 	     put_error("%s: no target specified", command);
135 	     return;
136 	  }
137      }
138 
139    /* if that one exists, send it.. */
140    if (list[number])
141      send_text(tmpbuf, list[number], "PRIVMSG");
142 }
143 
144 /*
145  * adds a redirect (/relm or /relsm)
146  */
147 void
148 #ifdef HAVE_STDARG_H
addredirect(int cmd,u_char * format,...)149 addredirect(int cmd, u_char *format,...)
150 #else
151 addredirect(cmd, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
152    int cmd;
153    u_char *format, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7, *arg8,
154     *arg9, *arg10;
155 #endif
156 {
157    int x, y;
158    u_char **list = NULL;
159    u_char tmpbuf[1024];
160 #ifdef HAVE_STDARG_H
161    va_list vl;
162 
163    va_start(vl, format);
164    vsnprintf(tmpbuf, sizeof(tmpbuf)-1, format, vl);
165    va_end(vl);
166 #else
167    snprintf(tmpbuf, sizeof(tmpbuf)-1, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
168 #endif
169    tmpbuf[sizeof(tmpbuf)-1] = '\0';
170 
171    if (cmd == REDIRECT_MSG)
172      list = redirect_msgs;
173    else if (cmd == REDIRECT_SENT_MSG)
174      list = redirect_sent_msg;
175    else if (cmd == REDIRECT_NOTICE)
176      list = redirect_notice;
177    else if (cmd == REDIRECT_SENT_NOTICE)
178      list = redirect_sent_notice;
179    else
180      return;
181 
182    /* shift them up */
183    for (x = 9, y = 8; x >= 1; x--, y--)
184       dma_strcpy(&list[x], list[y]);
185    /* add the new one */
186    dma_strcpy(&list[0], tmpbuf);
187 }
188 
189