~ubuntu-branches/ubuntu/utopic/sip-tester/utopic

« back to all changes in this revision

Viewing changes to fortune.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell, Paul Belanger, Mark Purcell
  • Date: 2011-11-03 21:56:17 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20111103215617-nnxicj1oto9e8ix5
Tags: 1:3.2-1
[ Paul Belanger ]
* New Upstream Release (Closes: #623915).
* Switch to dpkg-source 3.0 (quilt) format
* Building with PCAP play.
* Switch back to Debhelper.
* debian/patches/spelling-error-in-binary: Fix lintian warning

[ Mark Purcell ]
* Drop pabs from Uploaders: at his request
* fix debhelper-overrides-need-versioned-build-depends
* fix description-synopsis-starts-with-article

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This program is free software; you can redistribute it and/or modify
 
3
 *  it under the terms of the GNU General Public License as published by
 
4
 *  the Free Software Foundation; either version 2 of the License, or
 
5
 *  (at your option) any later version.
 
6
 *
 
7
 *  This program is distributed in the hope that it will be useful,
 
8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *  GNU General Public License for more details.
 
11
 *
 
12
 *  You should have received a copy of the GNU General Public License
 
13
 *  along with this program; if not, write to the Free Software
 
14
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 *
 
16
 *  Author : Richard GAYRAUD - 04 Nov 2003
 
17
 *           Olivier Jacques
 
18
 *           From Hewlett Packard Company.
 
19
 *           Charles P Wright from IBM Research
 
20
 */
 
21
#include "sipp.hpp"
 
22
 
 
23
/* This is a fun sample of creating your own extensible keyword. */
 
24
int fortune(call *call, MessageComponent *comp, char *buf, int len) {
 
25
        int pipes[2];
 
26
        char localbuf[SIPP_MAX_MSG_SIZE];
 
27
        char *p = localbuf;
 
28
        int ret;
 
29
        int written = 0;
 
30
 
 
31
        if (pipe(pipes) == -1) {
 
32
                ERROR("Could not create pipes!\n");
 
33
        }
 
34
 
 
35
        switch (fork()) {
 
36
        case -1:
 
37
                ERROR("Fork failed: %s\n", strerror(errno));
 
38
        case 0:
 
39
                /* We are the child. */
 
40
                close(pipes[0]);
 
41
                dup2(pipes[1], fileno(stdout));
 
42
                dup2(pipes[1], fileno(stderr));
 
43
                close(fileno(stdin));
 
44
                system("/usr/bin/fortune");
 
45
                exit (127);
 
46
        default:
 
47
                /* We are the parent*/
 
48
                close(pipes[1]);
 
49
                while ((ret = read(pipes[0], p, sizeof(localbuf) - (p - localbuf))) > 0) {
 
50
                        p += ret;
 
51
                }
 
52
                *p = '\0';
 
53
                close(pipes[0]);
 
54
 
 
55
                if (len > p - localbuf) {
 
56
                        len = p -localbuf;
 
57
                }
 
58
 
 
59
                p = localbuf;
 
60
                while(len-- > 0) {
 
61
                        if (*p == '\n') {
 
62
                                if (len < 3) {
 
63
                                        break;
 
64
                                }
 
65
                                *buf++ = '\r';
 
66
                                *buf++ = '\n';
 
67
                                *buf++ = ' ';
 
68
                                written += 3;
 
69
                                p++;
 
70
                        } else {
 
71
                                *buf++ = *p++;
 
72
                                written++;
 
73
                        }
 
74
                }
 
75
                break;
 
76
        }
 
77
 
 
78
        return written;
 
79
}
 
80
 
 
81
/* On initialization we register our keywords. */
 
82
extern "C" int init(void) {
 
83
        registerKeyword("fortune", fortune);
 
84
        return 0;
 
85
}