got rid of a lot of redundent header files as we now globally generate
[nivanova/samba-autobuild/.git] / source3 / smbd / message.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB messaging
5    Copyright (C) Andrew Tridgell 1992-1995
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 /*
22    This file handles the messaging system calls for winpopup style
23    messages
24 */
25
26
27 #include "includes.h"
28
29 /* look in server.c for some explanation of these variables */
30 extern int DEBUGLEVEL;
31
32
33 static char msgbuf[1600];
34 static int msgpos=0;
35 static fstring msgfrom="";
36 static fstring msgto="";
37
38 /****************************************************************************
39 deliver the message
40 ****************************************************************************/
41 static void msg_deliver(void)
42 {
43   pstring s;
44   fstring name;
45   FILE *f;
46   int i;
47
48   if (! (*lp_msg_command()))
49     {
50       DEBUG(1,("no messaging command specified\n"));
51       msgpos = 0;
52       return;
53     }
54
55   /* put it in a temporary file */
56   sprintf(s,"/tmp/msg.XXXXXX");
57   strcpy(name,(char *)mktemp(s));
58
59   f = fopen(name,"w");
60   if (!f)
61     {
62       DEBUG(1,("can't open message file %s\n",name));
63       return;
64     }
65
66   for (i=0;i<msgpos;)
67     {
68       if (msgbuf[i]=='\r' && i<(msgpos-1) && msgbuf[i+1]=='\n')
69         i++;
70       fputc(msgbuf[i++],f);
71     }
72
73   fclose(f);
74
75
76   /* run the command */
77   if (*lp_msg_command())
78     {
79       strcpy(s,lp_msg_command());
80       string_sub(s,"%s",name);
81       string_sub(s,"%f",msgfrom);
82       string_sub(s,"%t",msgto);
83       standard_sub(-1,s);
84       smbrun(s,NULL);
85     }
86
87   msgpos = 0;
88 }
89
90
91
92 /****************************************************************************
93   reply to a sends
94 ****************************************************************************/
95 int reply_sends(char *inbuf,char *outbuf)
96 {
97   int len;
98   char *orig,*dest,*msg;
99   int outsize = 0;
100
101   msgpos = 0;
102
103
104   if (! (*lp_msg_command()))
105     return(ERROR(ERRSRV,ERRmsgoff));
106
107   outsize = set_message(outbuf,0,0,True);
108
109   orig = smb_buf(inbuf)+1;
110   dest = skip_string(orig,1)+1;
111   msg = skip_string(dest,1)+1;
112
113   strcpy(msgfrom,orig);
114   strcpy(msgto,dest);
115
116   len = SVAL(msg,0);
117   len = MIN(len,1600-msgpos);
118
119   memcpy(&msgbuf[msgpos],msg+2,len);
120   msgpos += len;
121
122   DEBUG(3,("%s SMBsends (from %s to %s)\n",timestring(),orig,dest));
123
124   msg_deliver();
125
126   return(outsize);
127 }
128
129
130 /****************************************************************************
131   reply to a sendstrt
132 ****************************************************************************/
133 int reply_sendstrt(char *inbuf,char *outbuf)
134 {
135   char *orig,*dest;
136   int outsize = 0;
137
138   if (! (*lp_msg_command()))
139     return(ERROR(ERRSRV,ERRmsgoff));
140
141   outsize = set_message(outbuf,1,0,True);
142
143   msgpos = 0;
144
145   orig = smb_buf(inbuf)+1;
146   dest = skip_string(orig,1)+1;
147
148   strcpy(msgfrom,orig);
149   strcpy(msgto,dest);
150
151   DEBUG(3,("%s SMBsendstrt (from %s to %s)\n",timestring(),orig,dest));
152
153   return(outsize);
154 }
155
156
157 /****************************************************************************
158   reply to a sendtxt
159 ****************************************************************************/
160 int reply_sendtxt(char *inbuf,char *outbuf)
161 {
162   int len;
163   int outsize = 0;
164   char *msg;
165
166   if (! (*lp_msg_command()))
167     return(ERROR(ERRSRV,ERRmsgoff));
168
169   outsize = set_message(outbuf,0,0,True);
170
171   msg = smb_buf(inbuf) + 1;
172
173   len = SVAL(msg,0);
174   len = MIN(len,1600-msgpos);
175
176   memcpy(&msgbuf[msgpos],msg+2,len);
177   msgpos += len;
178
179   DEBUG(3,("%s SMBsendtxt\n",timestring()));
180
181   return(outsize);
182 }
183
184
185 /****************************************************************************
186   reply to a sendend
187 ****************************************************************************/
188 int reply_sendend(char *inbuf,char *outbuf)
189 {
190   int outsize = 0;
191
192   if (! (*lp_msg_command()))
193     return(ERROR(ERRSRV,ERRmsgoff));
194
195   outsize = set_message(outbuf,0,0,True);
196
197   DEBUG(3,("%s SMBsendend\n",timestring()));
198
199   msg_deliver();
200
201   return(outsize);
202 }
203