Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Extracting for notification

Posted by Ted (Ted), 10 June 2003
I have a forum script on my free script site (http://scriptles.bluwall.com) which is quite popular. There has been a reguest to have it so that subscribers can notified by email when a new subject is posted.
(note: the download version of Forum2 on Scriptles does not have this yet)


So far this has been done:
I have set up an opt-in which works fine. Email address are written to a file - good.
MailIt! is the admin and mailer. It works fine and sends out the message when typed in from the admin - but I don't want to do that for every new subject!

What I want to do is:
1 grab the title and date of the new subject from the forum script as it is being written
2 send that to the mailing function in MailIt! (shouldn't be a problem - just a little external script)
3 and MailIt! sends out the email notification.

But how on earth do I grab the title and date of the new subject so it can be used.



I'm not a fully blown programmer (getting there slowly) and just cannot figure this out.

Hope you can help.


This is the entire post new thread sub-routine (with notes) from the existing forum script.

##########################################################################
# posts a new thread to the forum
# JHT modified - added to headers array
sub postThread {
     if (!opendir (DIR, "$forumdir")) {
           mkdir ("$forumdir", 0700);
     }
     open(NUM,"$forumdir/num.txt");                  ## writes to num.txt adds one number
     $num = <NUM>;
     $num += 1;
     close(NUM);
     open(NUM,">$forumdir/num.txt");
     print NUM $num;
     close(NUM);
     
     open(MONTHS,"$forumdir/months.txt");
     @months = <MONTHS>;
     close(MONTHS);
     $addit = "true";
     foreach $month (@months) {
           if ($month =~ /$headerfile/) {
                 $addit = "false";
           }
     }
     if ($addit eq "true") {                         ## write to months.txt each month
           open(MONTHS,"$forumdir/months.txt");
           @monthlines = <MONTHS>;
           close(MONTHS);
           open(MONTHS,">$forumdir/months.txt");
           unshift (@monthlines, "$headerfile\n");
           foreach $monthline (@monthlines) {
                 print MONTHS $monthline;
           }
           close(MONTHS);
     }
     
     open(HEADER,"$forumdir/$headerfile.txt");       ## writes a file for each
     @headers = <HEADER>;
     close(HEADER);
     open(HEADER,">$forumdir/$headerfile.txt");
     unshift (@headers, "$num\_$subject\_$name\_$date\_0\_ \n");  # JHT modified: last field -  
     foreach $headerline (@headers) {
           print HEADER $headerline;
     }
     close(HEADER);                                  ## creates messages directory if not there
     if (!opendir (DIR, "$forumdir/messages")) {
           mkdir ("$forumdir/messages", 0777);
     }
open(NEWTHREAD,">$forumdir/messages/$num.txt");    ########## The new Thread (subject)

  ################ Is this where I send the stuff so it can be mailed?##############
  ################ I will still need the rest of the script to work so #############
  ################## it doesn't get messed up and ruin the forum ###################

print NEWTHREAD <<"EOF";
<threadinfo>                           ## writes new file based on num.txt
<num>$num</num>                         # Creates file in messages directory.
<headerfile>$headerfile</headerfile>    # That file is the new subject.
<subject>$subject</subject>             # It's number will equal
</threadinfo>                           # the latest number on num.txt.
                                                   
<msg>                                   # Can it create another temporary file
<name>$name</name>                      # that can be used by a mailer or what?
<email>$email</email>                   # Or do I use an external script and do the stuff?
<website>$website</website>
<date>$date</date>
<body>
$body
</body>
</msg>
EOF

     close(NEWTHREAD);
}
##########################################################################





Posted by admin (Graham Ellis), 10 June 2003
It looks to me like the subject is in a variable called $subject, and the current date (time too?) in a variable called $date.   If that's the case, then your email body might read like:

$body = "blah blah $subject blah blah $date blah blah".

I don't see where these variable are being set up, so I'm guessing that they're global  (of course, I'm also guessing what they contain too).

I'm not sure if this is the sort of pointer that you're looking for ... this forum runs on YaBB - also a Perl forum - with which I'm reasonable familiar with.   It's a little bit ike being back to "first base" with your code but ... taking the changes gently ... it should work for you.   Do post a follow up if you need further advice

Graham

Posted by Ted (Ted), 11 June 2003
I did a find on $subject and got to just before the 'sub postThread' where another sub-routine determines whether to post a reply or new thread. I realised this is what I want .....

This section of it is the key bit

     $subject = $in{'subject'};
     $subject =~ s/\cM//g;
     $subject =~ s/"/"/g;
     $subject =~ s/_/ /g;
     if ($in{'post'} eq "thread") {&postThread;}     ## posts new thread -calls postThread
     elsif ($in{'post'} eq "reply") {&postReply;}



I found that if I did this (twas a guess at first):

if ($in{'post'} eq "thread") {&postThread;}  ## posts new thread -calls postThread
if ($in{'post'} eq "thread") {&exTract;}     ### Extract Subject & Date for notification
elsif ($in{'post'} eq "reply") {&postReply;}

I could call up another script (notify.pl) which writes to a file the data needed (subject and date with a little message and link)

Everytime a new subject is posted the file is overwritten with the new data. Works a treat. (Yippeeee) I didn't realise you could have two 'if' statements in a row (nobody ever tells you that!), I thought it had to have an else - learn something new every day.


All I have to do now is figure out how make MailIt! send the file using the notify.pl.


Thanks - Ted

Posted by admin (Graham Ellis), 11 June 2003
Perl is a truely remarkable language!

Many other languages are very restricting in what they allow you to do, but in Perl the syntax is very flexible indeed ... not always a good thing as it can encourage a newcomer to develop code that's totally incomprehensible when it comes to maintenance.   The fact that there's always (much) more than one way of doing anything doesn't help the newcomer either ... but it makes Perl a fabulously powerful tool in which you can do a lot of work very quickly - once you know it somewhat.

If I take your statement:

Code:
if ($in{'post'} eq "thread") {&postThread;}


I can re-write it in any of the following ways:

Code:
if ($in{'post'} eq "thread") {postThread();}

($in{'post'} eq "thread")  && postThread();

$in{'post'} eq "thread"  and postThread;   # If postThread already defined

postThread() if ($in{post} eq 'thread');

$in{post} ne 'thread' or postThread();

$in{post} eq 'thread' ? postThread() : "";


and also in many combinations based on the above.  I sometimes think that Perl is an art rather than a science, but it's based on scientific principles.  Rather like painting - had Michaelangelo not got his science right and the paint on the ceiling had fallen off when it had dried, we wouldn't have still been admiring his work to this day!



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho