My favorites | Sign in
Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php

/*
* Author:
* Abhinav Singh
*
* Contact:
* mailsforabhinav@gmail.com
* admin@abhinavsingh.com
*
* Site:
* http://abhinavsingh.com
* http://abhinavsingh.com/blog
*
* Source:
* http://code.google.com/p/jaxl
*
* About:
* JAXL stands for "Just Another XMPP Library"
* For geeks, JAXL stands for "Jabber XMPP Library"
*
* I wrote this library while developing Gtalkbots (http://gtalkbots.com)
* I have highly customized it to work with Gtalk Servers and inspite of
* production level usage at Gtalkbots, I recommend still not to use this
* for any live project.
*
* Feel free to add me in Gtalk and drop an IM.
*
*/

/*
* ==================================== IMPORTANT =========================================
* This is a sample application code showing the power of JAXL. How without knowing the
* inner tit-bits of XMPP protocols you can develop cool applications.
*
* To run this sample application you need:
* 1. JAXL Library (http://code.google.com/p/jaxl)
* 2. XML_RSS Package (pear install XML_RSS)
* 3. Create a directory cache/dzone where this application will cache all rss files
* 4. Include jaxl4dzone.class.php in index.php, instead of jaxl.class.php and run
* 'php index.php' from command line to start this application
* 5. For more info read http://abhinavsingh.com/blog/2009/01/how-to-get-dzone-feeds-as-im-using-jaxl-add-dzonegtalkbotscom/
* ==================================== IMPORTANT =========================================
*/

/* Include XMPP Class */
include_once("xmpp.class.php");

/* Include RSS Parser Class */
require_once "XML/RSS.php";

class JAXL extends XMPP {

/* Initialize Service Variables */
var $restrictJid = FALSE; // If set to TRUE, this bot will
// reply only to Jid's specified
// in $authorizedJid array
var $authorizedJid = array();
var $maxResults = 5;
var $count = 0;

function eventMessage($fromJid, $content, $offline = FALSE) {

// If the message is not an offliner
if(!$offline) {
if($this->restrictJid && !in_array($this->getBareJid($fromJid),$this->authorizedJid)) { // Block un-authorized users
$this->logger->logger($fromJid." tried using this service by sending:\n".$content);
$message = 'You are not authorized to use this service';
$this->sendMessage($fromJid,$message);
return TRUE;
}
else {
$content = strtolower(trim($content));

// Build RSS Feed URL depending upon passed IM
if($content == "options") {
$message = "Available options are:\n";
$message .= "*1. frontpage:* Will send you latest links on frontpage.\n";
$message .= "*2. queue:* Will send you latest links in queue.\n";
$message .= "*3. tag:* Send in a tag e.g. php,java,xml and you will get latest links for the particular tag\n";
$this->sendMessage($fromJid,$message);
return TRUE;
}
else if($content == "frontpage") $url = "http://www.dzone.com/links/feed/frontpage/rss.xml";
else if($content == "queue") $url = "http://www.dzone.com/links/feed/queue/rss.xml";
else $url = "http://www.dzone.com/links/feed/frontpage/".$content."/rss.xml";

// Generate cache file location
$cache_file = "cache/dzone/".md5($url)."-".$content.".rss";

// If cache file is not older than 900 seconds (15 min)
if(file_exists($cache_file) && (filemtime($cache_file) > time() - 900)) {
$this->logger->logger("RSS already found cached...");
}
else { // else fetch a fresh copy of RSS
$this->logger->logger("Fetching RSS for url:\n".$url);
$head = $this->fetchWebURL($url);
if($head['errno'] == 0 && $head['http_code'] == 200) {
$this->logger->logger("Fetched successfully...");
$data = $head['content'];
$fh = fopen($cache_file,"w");
fwrite($fh,$data);
fclose($fh);
}
else {
// Send a message saying no such feed exists
$this->logger->logger("Failed to fetch the RSS feed...");
$message = 'No feed exists for the passed keyword: '.$content;
$this->sendMessage($fromJid,$message);
return TRUE;
}
}

// Parse RSS Feed
$r = &new XML_RSS($cache_file);
$r->parse();
$NumberOfFeeds = count($r->getItems());
if($NumberOfFeeds > 0) {
$message = '';
foreach($r->getItems() as $key => $item) {
if($this->count < $this->maxResults) {
$message .= "*".$item["title"]."*\n";
$message .= $item["link"]."\n";
$message .= $item["description"]."\n\n";
$this->count++;
}
else {
break;
}
}
$message .= "powered by *Jaxl* http://code.google.com/p/jaxl"."\n\n";
$this->sendMessage($fromJid,$message);
$this->count = 0;
return TRUE;
}
else {
// Send a message saying no feed found
$message = 'No feed was found for passed keyword: '.$content;
$this->sendMessage($fromJid,$message);
return TRUE;
}
}
}
else {
// Send an appropriate message
$this->logger->logger($fromJid." sent an offliner saying:\n".$content);
return TRUE;
}
}

function eventPresence($fromJid, $status, $photo) {
// Not used here. See jaxl.class.php for it's use case
}

function eventNewEMail($total,$thread,$url,$participation,$messages,$date,$senders,$labels,$subject,$snippet) {
// Not used here. See jaxl4gmail.class.php for it's use case
}

function setStatus() {
$this->sendStatus("Type *options* to get started");
print "Setting Status...\n";
print "Done\n";
}

function fetchWebURL($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "jaxl4dzone", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}

}

?>
Show details Hide details

Change log

r40 by mailsforabhinav on May 20, 2009   Diff
Checking in with updates comments and
introducing jaxl4broadcast.class.php for
broadcasting IM across your buddy list
Go to: 
Project members, sign in to write a code review

Older revisions

r37 by mailsforabhinav on Jan 13, 2009   Diff
[No log message]
r36 by mailsforabhinav on Jan 07, 2009   Diff
Sample application for dzone rss feeds
All revisions of this file

File info

Size: 7637 bytes, 194 lines
Hosted by Google Code