Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, April 19, 2024

File: [Platon] / dead / mail2sms / mail2sms.c (download)

Revision 1.25, Fri Nov 16 19:30:01 2001 UTC (22 years, 5 months ago) by host8


Changes since 1.24: +8 -1 lines

Patched bug in declaration/definition of global variable debug.

/*
 * Author: Lubomir Host 'rajo' <host8@kepler.fmph.uniba.sk>
 * $Log: mail2sms.c,v $
 * Revision 1.25  2001/11/16 19:30:01  host8
 * Patched bug in declaration/definition of global variable debug.
 *
 * Revision 1.24  2001/11/12 16:08:00  host8
 * Big improvements in http. connect() doesn't work correctly yet.
 *
 * Revision 1.23  2001/10/30 17:24:02  host8
 * New implementation of http request.
 *
 * Revision 1.22  2001/10/27 20:32:43  host8
 * mail2sms.c splited to multiple files.
 * Little improvements in Makefile.in.
 *
 * Revision 1.21  2001/10/25 20:05:28  host8
 * First implementation of login with password.
 *
 * Revision 1.20  2001/10/25 17:37:58  host8
 * Support only some sms-gates available.
 *
 * Revision 1.19  2001/10/25 09:37:57  host8
 * Added support for selection only some sms-gates.
 *
 * Revision 1.18  2001/10/24 19:24:49  host8
 * Added parameter -p (look for password).
 *
 * Revision 1.17  2001/10/02 16:24:47  jombik9
 * Added suport for Subject header.
 * Removed white characters in SMS message.
 * Minor bugfixies & lots of improvements.
 * Unneccessary files removed from CVS repository.
 */
/* README {{{1
 * - riadky, na ktorych sa nachadzaju zatvorky `{{{' alebo `}}}'
*   su urcene pre editor ViM 6.0 . ViM 6.0 podla tychto znaciek
*   rozoznava, kde sa zacina a konci jedna cast textu, kt. mozno
*   skryt. Preto prosim neprepisuj tieto riadky, pokial nevies
*   co robis.                                                D.
 * }}}1
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
//#include <netdb.h>

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include "mail2sms.h"
#include "http.h"
#include "strings.h"
#include "passwd.h"
#include "sendsms.h"
#include "mail.h"

#if defined(ENABLE_AZET) || defined(ENABLE_WHIZDIARY) || defined(ENABLE_SPISNET) || defined(ENABLE_GLOBTELNET) || defined(ENABLE_MTNSMS)
/* empty */
#else
# error Please re-run ./configure with at least one argument --enable-* !
#endif

/* global variables {{{ */
char *message, *scratch, *keybd;
GLOB g;
//WWW www;
SERVER servers[] = {
#ifdef ENABLE_AZET
    //{ AZET,       "azet",       "62.168.116.69",   80, 420 },
    { AZET,       "azet",       "sms.azet.sk",   80, 420 },
#endif
#ifdef ENABLE_WHIZDIARY
    //{ WHIZDIARY,  "whizdiary",  "196.38.82.17",    80, 0 },
    { WHIZDIARY,  "whizdiary",  "www.whizdiary.com",    80, 0 },
#endif
#ifdef ENABLE_SPISNET
    //{ SPISNET,    "spisnet",    "195.168.233.131", 80, 132 },
    { SPISNET,    "spisnet",    "sms.spisnet.sk", 80, 132 },
#endif
#ifdef ENABLE_GLOBTELNET
    //{ GLOBTELNET, "globtelnet", "213.151.200.27",  80, 0 },
    { GLOBTELNET, "globtelnet", "www.globtelnet.sk",  80, 0 },
#endif
#ifdef ENABLE_MTNSMS
    //    { MTNSMS,     "mtnsms",     "196.36.119.197",  80, 0 },
    //{ MTNSMS,     "mtnsms",     "196.36.119.186",  80, 0 },
    { MTNSMS,     "mtnsms",     "www.mtnsms.com",  80, 0 },
#endif
    { 0, NULL, NULL, 0, 0 }
};
int id=-1; /* index to servers[] */

#ifdef DEBUG
    int debug = 0;
#endif

/* }}} */
// Function my_free() {{{
void my_free(void *ptr)
{
    /* Normal function 'free()' is buggy,
     * she crash for free(NULL);
     */
    if (ptr != NULL)
        free(ptr);
}
// }}}
// Function void_read_at_exit() {{{
void void_read_at_exit(void)
{
    int koniec = 0;
    fd_set fds;

    while (!koniec) { // prazdne citanie zo stdin, ked sa neposiela smska
        FD_ZERO(&fds);
        FD_SET(0,&fds);
        switch (select(FD_SETSIZE,&fds,0,0,0)) {
            case -1 :
                perror("");
                exit(errno);
                break;
            default:
                if (FD_ISSET(0,&fds)) {
                    if (fgets(keybd, KEYBD_LEN-1, stdin)!=NULL);
                    else koniec=1;
                }
                break;
        } // end switch ( select() )
    } // end while (!koniec)

    my_free(message);
    my_free(scratch);
    my_free(keybd);
    exit(0);
} // }}}
// Function print_usage() {{{
void print_usage(char *argv0)
{
    fprintf(stderr, "Usage: %s [OPTIONS] [MESSAGE] ...\n", argv0);
    fprintf(stderr, "       --number   povinne tel. cislo [421903838302]\n");
    fprintf(stderr, "       --gate     ["
                            // #ifdef ENABLE_AZET {{{
#ifdef ENABLE_AZET
#  define TMP_SOMETHING_ENABLED
                            "azet"
#endif
                            // }}} #ifdef ENABLE_AZET
                            // #ifdef ENABLE_WHIZDIARY {{{
#ifdef ENABLE_WHIZDIARY
#  ifndef TMP_SOMETHING_ENABLED
#    define TMP_SOMETHING_ENABLED
#  else
                            "|"
#  endif
                            "whizdiary"
#endif
                            // }}} #ifdef ENABLE_WHIZDIARY
                            // #ifdef ENABLE_SPISNET {{{
#ifdef ENABLE_SPISNET
#  ifndef TMP_SOMETHING_ENABLED
#    define TMP_SOMETHING_ENABLED
#  else
        "|"
#  endif
        "spisnet"
#endif
        // }}} #ifdef ENABLE_SPISNET
        // #ifdef ENABLE_GLOBTELNET {{{
#ifdef ENABLE_GLOBTELNET
#  ifndef TMP_SOMETHING_ENABLED
#    define TMP_SOMETHING_ENABLED
#  else
        "|"
#  endif
        "globtelnet"
#endif
        // }}}#ifdef ENABLE_GLOBTELNET
        // #ifdef ENABLE_MTNSMS {{{
#ifdef ENABLE_MTNSMS
#  ifndef TMP_SOMETHING_ENABLED
#    define TMP_SOMETHING_ENABLED
#  else
        "|"
#  endif
        "mtnsms"
#endif
        // }}} #ifdef ENABLE_MTNSMS
        "]\n");
#undef TMP_SOMETHING_ENABLED
    fprintf(stderr, "       --mail     standardny vstup je mail\n");
    fprintf(stderr, "       --nomail   standardny vstup nie je mail\n");
    fprintf(stderr, "       --reverse  zmena poradia smsiek [123] -> [321]\n");
#ifdef DEBUG
    fprintf(stderr, "       --debug    vypis ladiacich informacii\n");
#endif
    fprintf(stderr, "       --help     vypis tejto informacie a koniec\n");
} // }}}
// Function main() {{{
int main(int argc, char *argv[])
{
    int koniec=0;
    fd_set fds;

    g.is_mail = 0;
    g.reverse = 0;
#ifdef DEBUG
    debug = 0;
#endif

    if (argc < 2) {
        print_usage(argv[0]);
        return(0);
    }

    while (1) { // Cmdline parsing {{{
        int c, i;
        int option_index = 0;
        static struct option long_options[] =
        {
            {"number", 1, 0, 'n'},
            {"gate", 1, 0, 'g'},
            {"passwd", 0, 0, 'p'},
            {"mail", 0, 0, 'm'},
            {"nomail", 0, 0, 'M'},
            {"reverse", 0, 0, 'r'},
#ifdef DEBUG
            {"debug", 2, 0, 'd'},
#endif
            {"help", 0, 0, 'h'},
            {0, 0, 0, 0}
        };

        c = getopt_long (argc, argv,
#ifdef DEBUG
                                "n:g:pmMrd::h",
#else
                                "n:g:pmMrh",
#endif
                                long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
            case 'n':
                g.number = optarg; // priradenie tel. cisla
#ifdef DEBUG
                fprintf(stderr, "n: number = %s\n", g.number);
#endif
                break;
            case 'g':
#ifdef DEBUG
                fprintf(stderr, "%s...", optarg);
#endif
                for (i=0; servers[i].desc != NULL; i++) {
                    if (strcmp(servers[i].desc, optarg) == 0) {
                        id = servers[i].id;
#ifdef DEBUG
                        fprintf(stderr, "%d\n", id);
#endif
                        break;
                    }
                }
                if (id == -1) {
                    fprintf(stderr, "\nInvalid option '%s'\n", optarg);
                    exit(1);
                }
                break;
            case 'p':
#ifdef DEBUG
                fprintf(stderr, "p: \n");
#endif
                break;
            case 'm':
                g.is_mail = 1;
#ifdef DEBUG
                fprintf(stderr, "m: is_mail = %d\n", g.is_mail);
#endif
                break;
            case 'M':
                g.is_mail = 0;
#ifdef DEBUG
                fprintf(stderr, "M: is_mail = %d\n", g.is_mail);
#endif
                break;
            case 'r':
                g.reverse = 1;
#ifdef DEBUG
                fprintf(stderr, "r: reverse = %d\n", g.reverse);
#endif
                break;
#ifdef DEBUG
            case 'd':
                if (optarg!=NULL) {
                    debug = atoi(optarg);
                }
                else debug = 1;
                fprintf(stderr, "d: debug = %d\n", debug);
#endif
                break;
            case 'h':
                print_usage(argv[0]);
                return 0;
                break;
            case '?':
            default:
                break;

        }
    } // }}}

    sleep(1);

    message = (char *) malloc(MESSAGE_LEN*sizeof(char));
    scratch = (char *) malloc(SCRATCH_LEN*sizeof(char));
    keybd = (char *) malloc(KEYBD_LEN*sizeof(char));

    // Preprocessing message {{{
    switch (id) {
#ifdef ENABLE_AZET
        case AZET:
            //komu=0903&predvolba=0903&cislo=838302&sprava=caf+pokus1%0adaskfjkadf+%3Csipky%3E%0ahalo&oslovenie=Ahoj+Milacik%21+&velkost=420&odoslat=Ok&odoslat.x=0&odoslat.y=0",
            strcpy(message, "komu=");
            strncat(message, g.number, 4);
            strcat(message, "&predvolba=");
            strncat(message, g.number, 4);
            strcat(message, "&cislo=");
            strncat(message, g.number + 4, (strlen(g.number) - 4) );
            strcat(message, "&sprava=");
            break;
#endif
#ifdef ENABLE_WHIZDIARY
        case WHIZDIARY:
            strcpy(message, "theAction=Send+Message&cellnum=%2B");
            strncat(message, g.number, strlen(g.number));
            strcat(message, "&message=");
            break;
#endif
#ifdef ENABLE_SPISNET
        case SPISNET:
            strcpy(message, "smsn=");
            strncat(message, g.number, strlen(g.number));
            strcat(message, "&chars=0");
            strcat(message, "&smsm=");
            break;
#endif
#ifdef ENABLE_GLOBTELNET
        case GLOBTELNET:
            strcpy(message, "f_Slot_num=&f_Redirect=%2Fsms%2Fsms.php3"
                                    "&f_TelNumber=");
            strncat(message, g.number, strlen(g.number));
            strcat(message, "&x=25&y=10");
            strcat(message, "&f_SMSBody=");
            break;
#endif
#ifdef ENABLE_MTNSMS
        case MTNSMS:
            strcpy(message, "dummy=&smsToNumbers=");
            strncat(message, g.number, strlen(g.number));
            strcat(message, "&smsMessage=");
            break;
#endif
        default:
            fprintf(stderr, "Somethings go wrong, invalid id in "
                                    "preprocessing message!\n");
            exit(1);
            break;
    } /* end switch (id) */
    // }}} Preprocessing message

    if (optind < argc) {
#ifdef DEBUG
        if (debug)
            fprintf (stderr, "non-option ARGV-elements:\n");
#endif
        while (optind < argc) {
#ifdef DEBUG
            if (debug)
                fprintf (stderr, "%s ", argv[optind]);
#endif
            strcat(message, argv[optind++]);
            if (argv[optind] != NULL) strcat(message, "+");
        }
#ifdef DEBUG
        if (debug)
            fprintf (stderr, "\n");
#endif
    }
#ifdef DEBUG
    fprintf(stderr, "message: %s\n", message);
#endif

    while (!koniec) {
        FD_ZERO(&fds);
        FD_SET(0,&fds);
        switch (select(FD_SETSIZE,&fds,0,0,0)) {
            case -1 :
                exit(errno);
                break;
            default:
                if (FD_ISSET(0,&fds)) {
                    if (fgets(keybd, KEYBD_LEN-1, stdin)!=NULL) {
                        //            printf("Reading: '%s'\n", keybd);
                        if (g.is_mail) {
                            analyze_mail(keybd);
                        }
                        else {
                            add_str_to_sms(keybd);
                        }
                    }
                    else koniec=1;
                }
                break;
        } // end switch ( select() )
    } // end while (!koniec)
    strcat(message, g.text);
    // Postprocessing message {{{
    switch (id) {
#ifdef ENABLE_AZET
        case AZET:
            strcat(message, "&oslovenie=Ahoj+Milacik%21+&velkost=420"
                                    "&odoslat=Ok&odoslat.x=0&odoslat.y=0");
            break;
#endif
#ifdef ENABLE_WHIZDIARY
        case WHIZDIARY:
            strcat(message, "&charsLeft=");
            strcat(message, "130&theNetwork=NONE&I4.x=0&I4.y=0");
            break;
#endif
#ifdef ENABLE_SPISNET
        case SPISNET:
            //    "smsn=421903123123&chars=0&smsm=sorry%2C+test&submit=Odoslat+SMS+spr%E1vu"
            strcat(message, "&submit=Odoslat+SMS+spr%E1vu");
            break;
#endif
#ifdef ENABLE_GLOBTELNET
        case GLOBTELNET:
            /* empty */
            break;
#endif
#ifdef ENABLE_MTNSMS
        case MTNSMS:
            strcat(message, "&smsSig=0&smsSigDyna=rajo"
                                    "&lenSSig=5&lenLSig=5&lenSysSig=11");
            /* empty */
            break;
#endif
        default:
            fprintf(stderr, "Somethings go wrong, invalid id in "
                                    "postprocessing message!\n");
            exit(1);
            break;
    } /* end switch (id) */
    // }}} Postprocessing message

    if (
#ifdef DEBUG
                            sendsms(message, debug-1) != 0
#else
                            sendsms(message, 0) != 0
#endif
       ) {
        sleep(1);
        fprintf(stderr, "Zlyhalo posielanie SMSky, "
                                "skusam poslat este raz ...\n");
#ifdef DEBUG
        sendsms(message, debug-1);
#else
        sendsms(message, 0);
#endif
    }
    my_free(message);
    my_free(scratch);
    my_free(keybd);
    return 0;
} // }}}
/* Modeline for ViM {{{1
 * vim:set ts=4:
 * vim600:fdm=marker fdl=0 fdc=3:
 * }}}1 */

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top